2020-03-02 20:47:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
#include "src/resolver/resolver.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-01 20:01:39 +00:00
|
|
|
#include <algorithm>
|
2021-07-15 19:09:25 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <iomanip>
|
2021-09-15 17:37:00 +00:00
|
|
|
#include <limits>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
2020-04-07 12:57:42 +00:00
|
|
|
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/ast/alias.h"
|
|
|
|
#include "src/ast/array.h"
|
2020-04-07 12:47:23 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2020-09-22 22:07:13 +00:00
|
|
|
#include "src/ast/bitcast_expression.h"
|
2020-04-07 12:54:10 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
2020-07-21 13:42:13 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2020-04-07 12:54:29 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/ast/depth_texture.h"
|
2021-05-12 12:54:21 +00:00
|
|
|
#include "src/ast/disable_validation_decoration.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/discard_statement.h"
|
|
|
|
#include "src/ast/fallthrough_statement.h"
|
2021-07-02 19:27:42 +00:00
|
|
|
#include "src/ast/for_loop_statement.h"
|
2020-04-07 12:55:25 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2021-04-07 08:09:21 +00:00
|
|
|
#include "src/ast/internal_decoration.h"
|
2021-06-28 23:04:43 +00:00
|
|
|
#include "src/ast/interpolate_decoration.h"
|
2020-04-07 12:55:51 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/ast/matrix.h"
|
2021-04-30 17:14:19 +00:00
|
|
|
#include "src/ast/override_decoration.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/ast/pointer.h"
|
2020-04-07 12:56:24 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/ast/sampled_texture.h"
|
|
|
|
#include "src/ast/sampler.h"
|
|
|
|
#include "src/ast/storage_texture.h"
|
2021-03-18 17:59:54 +00:00
|
|
|
#include "src/ast/struct_block_decoration.h"
|
2020-04-07 12:56:45 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2021-10-21 20:38:54 +00:00
|
|
|
#include "src/ast/traverse_expressions.h"
|
2021-05-05 09:09:41 +00:00
|
|
|
#include "src/ast/type_name.h"
|
2020-04-07 19:27:21 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2020-04-07 12:57:12 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/ast/vector.h"
|
2021-04-06 20:41:07 +00:00
|
|
|
#include "src/ast/workgroup_decoration.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/array.h"
|
2021-06-17 19:56:14 +00:00
|
|
|
#include "src/sem/atomic_type.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/call.h"
|
2021-07-26 22:19:48 +00:00
|
|
|
#include "src/sem/depth_multisampled_texture_type.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/sem/depth_texture_type.h"
|
2021-07-14 09:44:41 +00:00
|
|
|
#include "src/sem/for_loop_statement.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/function.h"
|
2021-07-14 09:44:41 +00:00
|
|
|
#include "src/sem/if_statement.h"
|
|
|
|
#include "src/sem/loop_statement.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/member_accessor_expression.h"
|
2021-04-20 16:09:21 +00:00
|
|
|
#include "src/sem/multisampled_texture_type.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/sem/pointer_type.h"
|
2021-05-18 10:28:48 +00:00
|
|
|
#include "src/sem/reference_type.h"
|
2021-04-21 13:47:12 +00:00
|
|
|
#include "src/sem/sampled_texture_type.h"
|
|
|
|
#include "src/sem/sampler_type.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/statement.h"
|
2021-04-20 20:47:51 +00:00
|
|
|
#include "src/sem/storage_texture_type.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/struct.h"
|
2021-07-14 09:44:41 +00:00
|
|
|
#include "src/sem/switch_statement.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/variable.h"
|
2021-07-08 21:23:33 +00:00
|
|
|
#include "src/utils/defer.h"
|
2021-04-13 13:32:33 +00:00
|
|
|
#include "src/utils/get_or_create.h"
|
2021-03-15 13:37:41 +00:00
|
|
|
#include "src/utils/math.h"
|
2021-09-08 15:18:36 +00:00
|
|
|
#include "src/utils/reverse.h"
|
2021-05-19 16:11:04 +00:00
|
|
|
#include "src/utils/scoped_assignment.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
2021-03-09 15:08:48 +00:00
|
|
|
namespace resolver {
|
2021-02-08 19:53:42 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
using IntrinsicType = tint::sem::IntrinsicType;
|
2021-02-08 19:53:42 +00:00
|
|
|
|
2021-04-21 13:47:12 +00:00
|
|
|
bool IsValidStorageTextureDimension(ast::TextureDimension dim) {
|
2021-04-20 20:47:51 +00:00
|
|
|
switch (dim) {
|
2021-04-21 13:47:12 +00:00
|
|
|
case ast::TextureDimension::k1d:
|
|
|
|
case ast::TextureDimension::k2d:
|
|
|
|
case ast::TextureDimension::k2dArray:
|
|
|
|
case ast::TextureDimension::k3d:
|
2021-04-20 20:47:51 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:47:12 +00:00
|
|
|
bool IsValidStorageTextureImageFormat(ast::ImageFormat format) {
|
2021-04-20 20:47:51 +00:00
|
|
|
switch (format) {
|
2021-04-21 13:47:12 +00:00
|
|
|
case ast::ImageFormat::kR32Uint:
|
|
|
|
case ast::ImageFormat::kR32Sint:
|
|
|
|
case ast::ImageFormat::kR32Float:
|
|
|
|
case ast::ImageFormat::kRg32Uint:
|
|
|
|
case ast::ImageFormat::kRg32Sint:
|
|
|
|
case ast::ImageFormat::kRg32Float:
|
|
|
|
case ast::ImageFormat::kRgba8Unorm:
|
|
|
|
case ast::ImageFormat::kRgba8Snorm:
|
|
|
|
case ast::ImageFormat::kRgba8Uint:
|
|
|
|
case ast::ImageFormat::kRgba8Sint:
|
|
|
|
case ast::ImageFormat::kRgba16Uint:
|
|
|
|
case ast::ImageFormat::kRgba16Sint:
|
|
|
|
case ast::ImageFormat::kRgba16Float:
|
|
|
|
case ast::ImageFormat::kRgba32Uint:
|
|
|
|
case ast::ImageFormat::kRgba32Sint:
|
|
|
|
case ast::ImageFormat::kRgba32Float:
|
2021-04-20 20:47:51 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:54:21 +00:00
|
|
|
/// @returns true if the decoration list contains a
|
|
|
|
/// ast::DisableValidationDecoration with the validation mode equal to
|
|
|
|
/// `validation`
|
|
|
|
bool IsValidationDisabled(const ast::DecorationList& decorations,
|
|
|
|
ast::DisabledValidation validation) {
|
|
|
|
for (auto* decoration : decorations) {
|
|
|
|
if (auto* dv = decoration->As<ast::DisableValidationDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (dv->validation == validation) {
|
2021-05-12 12:54:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-30 19:22:30 +00:00
|
|
|
/// @returns true if the decoration list does not contains a
|
|
|
|
/// ast::DisableValidationDecoration with the validation mode equal to
|
|
|
|
/// `validation`
|
|
|
|
bool IsValidationEnabled(const ast::DecorationList& decorations,
|
|
|
|
ast::DisabledValidation validation) {
|
|
|
|
return !IsValidationDisabled(decorations, validation);
|
|
|
|
}
|
|
|
|
|
2021-06-18 19:58:27 +00:00
|
|
|
// Helper to stringify a pipeline IO decoration.
|
|
|
|
std::string deco_to_str(const ast::Decoration* deco) {
|
|
|
|
std::stringstream str;
|
|
|
|
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
str << "builtin(" << builtin->builtin << ")";
|
2021-06-18 19:58:27 +00:00
|
|
|
} else if (auto* location = deco->As<ast::LocationDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
str << "location(" << location->value << ")";
|
2021-06-18 19:58:27 +00:00
|
|
|
}
|
|
|
|
return str.str();
|
|
|
|
}
|
2021-02-08 19:53:42 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::Resolver(ProgramBuilder* builder)
|
2021-05-06 16:04:03 +00:00
|
|
|
: builder_(builder),
|
|
|
|
diagnostics_(builder->Diagnostics()),
|
2021-06-01 19:06:31 +00:00
|
|
|
intrinsic_table_(IntrinsicTable::Create(*builder)) {}
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::~Resolver() = default;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::set_referenced_from_function_if_needed(VariableInfo* var,
|
|
|
|
bool local) {
|
2020-06-22 20:52:24 +00:00
|
|
|
if (current_function_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-09 20:45:09 +00:00
|
|
|
|
|
|
|
if (var->kind != VariableKind::kGlobal) {
|
2020-06-22 20:52:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
if (local) {
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->local_referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Resolve() {
|
2021-05-05 09:09:41 +00:00
|
|
|
if (builder_->Diagnostics().contains_errors()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool result = ResolveInternal();
|
2021-02-03 17:51:09 +00:00
|
|
|
|
2021-05-06 16:04:03 +00:00
|
|
|
if (!result && !diagnostics_.contains_errors()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "resolving failed, but no error was raised";
|
2021-05-05 09:09:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
// Even if resolving failed, create all the semantic nodes for information we
|
|
|
|
// did generate.
|
|
|
|
CreateSemanticNodes();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:12:57 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#plain-types-section
|
2021-06-21 20:51:16 +00:00
|
|
|
bool Resolver::IsPlain(const sem::Type* type) const {
|
2021-06-17 19:56:14 +00:00
|
|
|
return type->is_scalar() || type->Is<sem::Atomic>() ||
|
|
|
|
type->Is<sem::Vector>() || type->Is<sem::Matrix>() ||
|
|
|
|
type->Is<sem::Array>() || type->Is<sem::Struct>();
|
2021-06-09 09:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#storable-types
|
2021-06-21 20:51:16 +00:00
|
|
|
bool Resolver::IsStorable(const sem::Type* type) const {
|
2021-06-17 19:56:14 +00:00
|
|
|
return IsPlain(type) || type->Is<sem::Texture>() || type->Is<sem::Sampler>();
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 20:46:24 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#host-shareable-types
|
2021-06-21 20:51:16 +00:00
|
|
|
bool Resolver::IsHostShareable(const sem::Type* type) const {
|
2021-04-19 22:51:23 +00:00
|
|
|
if (type->IsAnyOf<sem::I32, sem::U32, sem::F32>()) {
|
2021-03-18 20:46:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-19 22:51:23 +00:00
|
|
|
if (auto* vec = type->As<sem::Vector>()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
return IsHostShareable(vec->type());
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
2021-04-19 22:51:23 +00:00
|
|
|
if (auto* mat = type->As<sem::Matrix>()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
return IsHostShareable(mat->type());
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
2021-05-07 20:58:34 +00:00
|
|
|
if (auto* arr = type->As<sem::Array>()) {
|
|
|
|
return IsHostShareable(arr->ElemType());
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* str = type->As<sem::Struct>()) {
|
|
|
|
for (auto* member : str->Members()) {
|
2021-05-05 09:09:41 +00:00
|
|
|
if (!IsHostShareable(member->Type())) {
|
2021-03-18 20:46:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-17 19:56:14 +00:00
|
|
|
if (auto* atomic = type->As<sem::Atomic>()) {
|
|
|
|
return IsHostShareable(atomic->Type());
|
|
|
|
}
|
2021-03-18 20:46:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::ResolveInternal() {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(&builder_->AST());
|
|
|
|
|
2021-03-19 18:45:30 +00:00
|
|
|
// Process everything else in the order they appear in the module. This is
|
|
|
|
// necessary for validation of use-before-declaration.
|
|
|
|
for (auto* decl : builder_->AST().GlobalDeclarations()) {
|
2021-06-09 14:32:14 +00:00
|
|
|
if (auto* td = decl->As<ast::TypeDecl>()) {
|
|
|
|
Mark(td);
|
|
|
|
if (!TypeDecl(td)) {
|
2021-04-19 19:16:12 +00:00
|
|
|
return false;
|
2021-03-19 18:45:30 +00:00
|
|
|
}
|
|
|
|
} else if (auto* func = decl->As<ast::Function>()) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(func);
|
2021-03-19 18:45:30 +00:00
|
|
|
if (!Function(func)) {
|
2020-06-15 20:55:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-19 18:45:30 +00:00
|
|
|
} else if (auto* var = decl->As<ast::Variable>()) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(var);
|
2021-04-06 20:18:57 +00:00
|
|
|
if (!GlobalVariable(var)) {
|
2021-03-19 18:45:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-16 01:15:43 +00:00
|
|
|
} else {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_)
|
2021-04-16 01:15:43 +00:00
|
|
|
<< "unhandled global declaration: " << decl->TypeInfo().name;
|
|
|
|
return false;
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 18:45:30 +00:00
|
|
|
|
2021-09-30 17:29:50 +00:00
|
|
|
AllocateOverridableConstantIds();
|
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
if (!ValidatePipelineStages()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
bool result = true;
|
2021-04-19 19:16:12 +00:00
|
|
|
for (auto* node : builder_->ASTNodes().Objects()) {
|
|
|
|
if (marked_.count(node) == 0) {
|
2021-10-14 20:30:29 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_) << "AST node '" << node->TypeInfo().name
|
|
|
|
<< "' was not reached by the resolver\n"
|
2021-10-15 17:33:10 +00:00
|
|
|
<< "At: " << node->source << "\n"
|
2021-10-14 20:30:29 +00:00
|
|
|
<< "Pointer: " << node;
|
2021-05-05 09:09:41 +00:00
|
|
|
result = false;
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
return result;
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* Resolver::Type(const ast::Type* ty) {
|
2021-04-21 13:47:12 +00:00
|
|
|
Mark(ty);
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = [&]() -> sem::Type* {
|
2021-04-30 20:20:19 +00:00
|
|
|
if (ty->Is<ast::Void>()) {
|
|
|
|
return builder_->create<sem::Void>();
|
|
|
|
}
|
|
|
|
if (ty->Is<ast::Bool>()) {
|
|
|
|
return builder_->create<sem::Bool>();
|
|
|
|
}
|
|
|
|
if (ty->Is<ast::I32>()) {
|
|
|
|
return builder_->create<sem::I32>();
|
|
|
|
}
|
|
|
|
if (ty->Is<ast::U32>()) {
|
|
|
|
return builder_->create<sem::U32>();
|
|
|
|
}
|
|
|
|
if (ty->Is<ast::F32>()) {
|
|
|
|
return builder_->create<sem::F32>();
|
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::Vector>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
2021-10-19 18:38:54 +00:00
|
|
|
if (auto* vector = builder_->create<sem::Vector>(el, t->width)) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (ValidateVector(vector, t->source)) {
|
2021-07-05 20:21:35 +00:00
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::Matrix>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
2021-10-19 18:38:54 +00:00
|
|
|
if (auto* column_type = builder_->create<sem::Vector>(el, t->rows)) {
|
2021-07-05 20:21:35 +00:00
|
|
|
if (auto* matrix =
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->create<sem::Matrix>(column_type, t->columns)) {
|
|
|
|
if (ValidateMatrix(matrix, t->source)) {
|
2021-07-05 20:21:35 +00:00
|
|
|
return matrix;
|
2021-06-21 17:08:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::Array>()) {
|
2021-05-07 20:58:34 +00:00
|
|
|
return Array(t);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
2021-06-17 19:56:14 +00:00
|
|
|
if (auto* t = ty->As<ast::Atomic>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
2021-10-19 18:38:54 +00:00
|
|
|
auto* a = builder_->create<sem::Atomic>(el);
|
2021-06-17 19:56:14 +00:00
|
|
|
if (!ValidateAtomic(t, a)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-04-30 20:20:19 +00:00
|
|
|
if (auto* t = ty->As<ast::Pointer>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
|
|
|
auto access = t->access;
|
2021-06-04 22:17:37 +00:00
|
|
|
if (access == ast::kUndefined) {
|
2021-10-15 17:33:10 +00:00
|
|
|
access = DefaultAccessForStorageClass(t->storage_class);
|
2021-06-04 22:17:37 +00:00
|
|
|
}
|
2021-10-19 18:38:54 +00:00
|
|
|
return builder_->create<sem::Pointer>(el, t->storage_class, access);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::Sampler>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return builder_->create<sem::Sampler>(t->kind);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::SampledTexture>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
2021-10-19 18:38:54 +00:00
|
|
|
return builder_->create<sem::SampledTexture>(t->dim, el);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::MultisampledTexture>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
2021-10-19 18:38:54 +00:00
|
|
|
return builder_->create<sem::MultisampledTexture>(t->dim, el);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (auto* t = ty->As<ast::DepthTexture>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return builder_->create<sem::DepthTexture>(t->dim);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
2021-07-26 22:19:48 +00:00
|
|
|
if (auto* t = ty->As<ast::DepthMultisampledTexture>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return builder_->create<sem::DepthMultisampledTexture>(t->dim);
|
2021-07-26 22:19:48 +00:00
|
|
|
}
|
2021-04-30 20:20:19 +00:00
|
|
|
if (auto* t = ty->As<ast::StorageTexture>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* el = Type(t->type)) {
|
2021-06-04 20:41:47 +00:00
|
|
|
if (!ValidateStorageTexture(t)) {
|
2021-05-21 14:32:38 +00:00
|
|
|
return nullptr;
|
2021-05-14 17:51:13 +00:00
|
|
|
}
|
2021-10-19 18:38:54 +00:00
|
|
|
return builder_->create<sem::StorageTexture>(t->dim, t->format,
|
|
|
|
t->access, el);
|
2021-04-30 20:20:19 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-05-06 08:20:52 +00:00
|
|
|
if (ty->As<ast::ExternalTexture>()) {
|
2021-05-05 18:00:52 +00:00
|
|
|
return builder_->create<sem::ExternalTexture>();
|
|
|
|
}
|
2021-05-05 09:09:41 +00:00
|
|
|
if (auto* t = ty->As<ast::TypeName>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto it = named_type_info_.find(t->name);
|
2021-05-20 08:44:57 +00:00
|
|
|
if (it == named_type_info_.end()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("unknown type '" + builder_->Symbols().NameFor(t->name) + "'",
|
|
|
|
t->source);
|
2021-05-05 09:09:41 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-05-20 08:44:57 +00:00
|
|
|
return it->second.sem;
|
2021-05-05 09:09:41 +00:00
|
|
|
}
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_)
|
2021-04-30 20:20:19 +00:00
|
|
|
<< "Unhandled ast::Type: " << ty->TypeInfo().name;
|
|
|
|
return nullptr;
|
|
|
|
}();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (s) {
|
|
|
|
builder_->Sem().Add(ty, s);
|
2021-04-21 13:47:12 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:56:14 +00:00
|
|
|
bool Resolver::ValidateAtomic(const ast::Atomic* a, const sem::Atomic* s) {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#atomic-types
|
|
|
|
// T must be either u32 or i32.
|
|
|
|
if (!s->Type()->IsAnyOf<sem::U32, sem::I32>()) {
|
2021-08-11 19:59:44 +00:00
|
|
|
AddError("atomic only supports i32 or u32 types",
|
2021-10-15 17:33:10 +00:00
|
|
|
a->type ? a->type->source : a->source);
|
2021-06-17 19:56:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-04 20:41:47 +00:00
|
|
|
bool Resolver::ValidateStorageTexture(const ast::StorageTexture* t) {
|
2021-10-15 17:33:10 +00:00
|
|
|
switch (t->access) {
|
2021-10-14 10:10:45 +00:00
|
|
|
case ast::Access::kWrite:
|
|
|
|
break;
|
2021-06-04 20:41:47 +00:00
|
|
|
case ast::Access::kUndefined:
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("storage texture missing access control", t->source);
|
2021-06-04 20:41:47 +00:00
|
|
|
return false;
|
2021-10-14 10:10:45 +00:00
|
|
|
default:
|
|
|
|
AddError("storage textures currently only support 'write' access control",
|
2021-10-15 17:33:10 +00:00
|
|
|
t->source);
|
2021-06-04 20:41:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!IsValidStorageTextureDimension(t->dim)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("cube dimensions for storage textures are not supported",
|
2021-10-15 17:33:10 +00:00
|
|
|
t->source);
|
2021-06-04 20:41:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!IsValidStorageTextureImageFormat(t->format)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-06-04 20:41:47 +00:00
|
|
|
"image format must be one of the texel formats specified for storage "
|
|
|
|
"textues in https://gpuweb.github.io/gpuweb/wgsl/#texel-formats",
|
2021-10-15 17:33:10 +00:00
|
|
|
t->source);
|
2021-06-04 20:41:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
Resolver::VariableInfo* Resolver::Variable(const ast::Variable* var,
|
2021-07-22 13:24:59 +00:00
|
|
|
VariableKind kind,
|
|
|
|
uint32_t index /* = 0 */) {
|
2021-05-12 10:41:21 +00:00
|
|
|
if (variable_to_info_.count(var)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-10-15 17:33:10 +00:00
|
|
|
<< "Variable " << builder_->Symbols().NameFor(var->symbol)
|
2021-06-24 11:27:36 +00:00
|
|
|
<< " already resolved";
|
2021-05-12 10:41:21 +00:00
|
|
|
return nullptr;
|
2021-04-16 01:15:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 10:41:21 +00:00
|
|
|
std::string type_name;
|
2021-05-18 10:28:48 +00:00
|
|
|
const sem::Type* storage_type = nullptr;
|
|
|
|
|
|
|
|
// If the variable has a declared type, resolve it.
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* ty = var->type) {
|
2021-05-12 10:41:21 +00:00
|
|
|
type_name = ty->FriendlyName(builder_->Symbols());
|
2021-05-18 10:28:48 +00:00
|
|
|
storage_type = Type(ty);
|
|
|
|
if (!storage_type) {
|
2021-05-12 10:41:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-05-05 09:09:41 +00:00
|
|
|
}
|
2021-05-12 10:41:21 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
std::string rhs_type_name;
|
|
|
|
const sem::Type* rhs_type = nullptr;
|
|
|
|
|
2021-05-12 10:41:21 +00:00
|
|
|
// Does the variable have a constructor?
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* ctor = var->constructor) {
|
|
|
|
if (!Expression(var->constructor)) {
|
2021-05-12 10:41:21 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the constructor's type
|
2021-05-18 10:28:48 +00:00
|
|
|
rhs_type_name = TypeNameOf(ctor);
|
|
|
|
rhs_type = TypeOf(ctor);
|
2021-05-12 10:41:21 +00:00
|
|
|
if (!rhs_type) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the variable has no declared type, infer it from the RHS
|
2021-05-18 10:28:48 +00:00
|
|
|
if (!storage_type) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var->is_const && kind == VariableKind::kGlobal) {
|
|
|
|
AddError("global var declaration must specify a type", var->source);
|
2021-06-04 15:28:47 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
type_name = rhs_type_name;
|
|
|
|
storage_type = rhs_type->UnwrapRef(); // Implicit load of RHS
|
2021-05-12 10:41:21 +00:00
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
} else if (var->is_const && kind != VariableKind::kParameter &&
|
|
|
|
!ast::HasDecoration<ast::OverrideDecoration>(var->decorations)) {
|
|
|
|
AddError("let declaration must have an initializer", var->source);
|
2021-07-19 19:50:52 +00:00
|
|
|
return nullptr;
|
2021-10-15 17:33:10 +00:00
|
|
|
} else if (!var->type) {
|
2021-07-19 19:50:52 +00:00
|
|
|
AddError(
|
|
|
|
(kind == VariableKind::kGlobal)
|
|
|
|
? "module scope var declaration requires a type and initializer"
|
|
|
|
: "function scope var declaration requires a type or initializer",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-05-05 09:09:41 +00:00
|
|
|
return nullptr;
|
2021-04-28 13:50:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (!storage_type) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-05-18 10:28:48 +00:00
|
|
|
<< "failed to determine storage type for variable '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(var->symbol) + "'\n"
|
|
|
|
<< "Source: " << var->source;
|
2021-05-18 10:28:48 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto storage_class = var->declared_storage_class;
|
|
|
|
if (storage_class == ast::StorageClass::kNone && !var->is_const) {
|
2021-08-27 14:54:47 +00:00
|
|
|
// No declared storage class. Infer from usage / type.
|
|
|
|
if (kind == VariableKind::kLocal) {
|
|
|
|
storage_class = ast::StorageClass::kFunction;
|
|
|
|
} else if (storage_type->UnwrapRef()->is_handle()) {
|
2021-05-18 10:28:48 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables
|
|
|
|
// If the store type is a texture type or a sampler type, then the
|
|
|
|
// variable declaration must not have a storage class decoration. The
|
|
|
|
// storage class will always be handle.
|
|
|
|
storage_class = ast::StorageClass::kUniformConstant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto access = var->declared_access;
|
2021-06-04 22:17:37 +00:00
|
|
|
if (access == ast::Access::kUndefined) {
|
|
|
|
access = DefaultAccessForStorageClass(storage_class);
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* type = storage_type;
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var->is_const) {
|
2021-05-18 10:28:48 +00:00
|
|
|
// Variable declaration. Unlike `let`, `var` has storage.
|
|
|
|
// Variables are always of a reference type to the declared storage type.
|
2021-06-04 22:17:37 +00:00
|
|
|
type =
|
|
|
|
builder_->create<sem::Reference>(storage_type, storage_class, access);
|
2021-05-18 10:28:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 14:54:47 +00:00
|
|
|
if (rhs_type &&
|
|
|
|
!ValidateVariableConstructor(var, storage_class, storage_type, type_name,
|
|
|
|
rhs_type, rhs_type_name)) {
|
2021-05-18 10:28:48 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 13:24:59 +00:00
|
|
|
auto* info =
|
|
|
|
variable_infos_.Create(var, const_cast<sem::Type*>(type), type_name,
|
|
|
|
storage_class, access, kind, index);
|
2021-04-16 01:15:43 +00:00
|
|
|
variable_to_info_.emplace(var, info);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:20:19 +00:00
|
|
|
ast::Access Resolver::DefaultAccessForStorageClass(
|
2021-06-04 22:17:37 +00:00
|
|
|
ast::StorageClass storage_class) {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#storage-class
|
|
|
|
switch (storage_class) {
|
|
|
|
case ast::StorageClass::kStorage:
|
|
|
|
case ast::StorageClass::kUniform:
|
|
|
|
case ast::StorageClass::kUniformConstant:
|
|
|
|
return ast::Access::kRead;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ast::Access::kReadWrite;
|
|
|
|
}
|
|
|
|
|
2021-09-30 17:29:50 +00:00
|
|
|
void Resolver::AllocateOverridableConstantIds() {
|
|
|
|
// The next pipeline constant ID to try to allocate.
|
|
|
|
uint16_t next_constant_id = 0;
|
|
|
|
|
|
|
|
// Allocate constant IDs in global declaration order, so that they are
|
|
|
|
// deterministic.
|
|
|
|
// TODO(crbug.com/tint/1192): If a transform changes the order or removes an
|
|
|
|
// unused constant, the allocation may change on the next Resolver pass.
|
|
|
|
for (auto* decl : builder_->AST().GlobalDeclarations()) {
|
|
|
|
auto* var = decl->As<ast::Variable>();
|
|
|
|
if (!var) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto* override_deco =
|
2021-10-15 17:33:10 +00:00
|
|
|
ast::GetDecoration<ast::OverrideDecoration>(var->decorations);
|
2021-09-30 17:29:50 +00:00
|
|
|
if (!override_deco) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t constant_id;
|
2021-10-15 17:33:10 +00:00
|
|
|
if (override_deco->has_value) {
|
|
|
|
constant_id = static_cast<uint16_t>(override_deco->value);
|
2021-09-30 17:29:50 +00:00
|
|
|
} else {
|
|
|
|
// No ID was specified, so allocate the next available ID.
|
|
|
|
constant_id = next_constant_id;
|
|
|
|
while (constant_ids_.count(constant_id)) {
|
|
|
|
if (constant_id == UINT16_MAX) {
|
|
|
|
TINT_ICE(Resolver, builder_->Diagnostics())
|
|
|
|
<< "no more pipeline constant IDs available";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
constant_id++;
|
|
|
|
}
|
|
|
|
next_constant_id = constant_id + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
variable_to_info_[var]->constant_id = constant_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
bool Resolver::ValidateVariableConstructor(const ast::Variable* var,
|
2021-08-27 14:54:47 +00:00
|
|
|
ast::StorageClass storage_class,
|
2021-05-18 10:28:48 +00:00
|
|
|
const sem::Type* storage_type,
|
|
|
|
const std::string& type_name,
|
|
|
|
const sem::Type* rhs_type,
|
|
|
|
const std::string& rhs_type_name) {
|
|
|
|
auto* value_type = rhs_type->UnwrapRef(); // Implicit load of RHS
|
|
|
|
|
|
|
|
// Value type has to match storage type
|
2021-05-18 15:26:40 +00:00
|
|
|
if (storage_type != value_type) {
|
2021-10-15 17:33:10 +00:00
|
|
|
std::string decl = var->is_const ? "let" : "var";
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("cannot initialize " + decl + " of type '" + type_name +
|
|
|
|
"' with value of type '" + rhs_type_name + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-05-18 10:28:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-27 14:54:47 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var->is_const) {
|
2021-08-27 14:54:47 +00:00
|
|
|
switch (storage_class) {
|
|
|
|
case ast::StorageClass::kPrivate:
|
|
|
|
case ast::StorageClass::kFunction:
|
|
|
|
break; // Allowed an initializer
|
|
|
|
default:
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#var-and-let
|
|
|
|
// Optionally has an initializer expression, if the variable is in the
|
|
|
|
// private or function storage classes.
|
|
|
|
AddError("var of storage class '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
std::string(ast::ToString(storage_class)) +
|
2021-08-27 14:54:47 +00:00
|
|
|
"' cannot have an initializer. var initializers are only "
|
|
|
|
"supported for the storage classes "
|
|
|
|
"'private' and 'function'",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-08-27 14:54:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::GlobalVariable(const ast::Variable* var) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDefinition(var->symbol, var->source,
|
2021-06-29 21:30:37 +00:00
|
|
|
/* check_global_scope_only */ true)) {
|
2021-04-06 20:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* info = Variable(var, VariableKind::kGlobal);
|
2021-04-16 01:15:43 +00:00
|
|
|
if (!info) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-04 22:29:22 +00:00
|
|
|
variable_stack_.Set(var->symbol, info);
|
2021-04-06 20:18:57 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var->is_const && info->storage_class == ast::StorageClass::kNone) {
|
|
|
|
AddError("global variables must have a storage class", var->source);
|
2021-04-06 20:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (var->is_const && !(info->storage_class == ast::StorageClass::kNone)) {
|
|
|
|
AddError("global constants shouldn't have a storage class", var->source);
|
2021-04-06 20:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : var->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
2021-05-13 20:32:32 +00:00
|
|
|
|
|
|
|
if (auto* override_deco = deco->As<ast::OverrideDecoration>()) {
|
|
|
|
// Track the constant IDs that are specified in the shader.
|
2021-10-15 17:33:10 +00:00
|
|
|
if (override_deco->has_value) {
|
|
|
|
constant_ids_.emplace(override_deco->value, info);
|
2021-05-13 20:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 19:16:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(var->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto bp = var->BindingPoint()) {
|
|
|
|
info->binding_point = {bp.group->value, bp.binding->value};
|
2021-04-06 20:18:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 20:20:33 +00:00
|
|
|
if (!ValidateGlobalVariable(info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(
|
|
|
|
info->storage_class, const_cast<sem::Type*>(info->type->UnwrapRef()),
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("while instantiating variable " +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(var->symbol),
|
|
|
|
var->source);
|
2021-04-06 20:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:09:25 +00:00
|
|
|
// TODO(bclayton): Call this at the end of resolve on all uniform and storage
|
|
|
|
// referenced structs
|
|
|
|
if (!ValidateStorageClassLayout(info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateStorageClassLayout(const sem::Struct* str,
|
|
|
|
ast::StorageClass sc) {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#storage-class-layout-constraints
|
|
|
|
|
|
|
|
auto is_uniform_struct_or_array = [sc](const sem::Type* ty) {
|
|
|
|
return sc == ast::StorageClass::kUniform &&
|
|
|
|
ty->IsAnyOf<sem::Array, sem::Struct>();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto is_uniform_struct = [sc](const sem::Type* ty) {
|
|
|
|
return sc == ast::StorageClass::kUniform && ty->Is<sem::Struct>();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto required_alignment_of = [&](const sem::Type* ty) {
|
2021-07-22 18:56:54 +00:00
|
|
|
uint32_t actual_align = ty->Align();
|
2021-07-15 19:09:25 +00:00
|
|
|
uint32_t required_align = actual_align;
|
|
|
|
if (is_uniform_struct_or_array(ty)) {
|
|
|
|
required_align = utils::RoundUp(16u, actual_align);
|
|
|
|
}
|
|
|
|
return required_align;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto member_name_of = [this](const sem::StructMember* sm) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return builder_->Symbols().NameFor(sm->Declaration()->symbol);
|
2021-07-15 19:09:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto type_name_of = [this](const sem::StructMember* sm) {
|
2021-10-15 17:33:10 +00:00
|
|
|
return sm->Declaration()->type->FriendlyName(builder_->Symbols());
|
2021-07-15 19:09:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(amaiorano): Output struct and member decorations so that this output
|
|
|
|
// can be copied verbatim back into source
|
|
|
|
auto get_struct_layout_string = [&](const sem::Struct* st) -> std::string {
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
if (st->Members().empty()) {
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "Validation should have ensured that "
|
|
|
|
"structs have at least one member";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto* const last_member = st->Members().back();
|
|
|
|
const uint32_t last_member_struct_padding_offset =
|
|
|
|
last_member->Offset() + last_member->Size();
|
|
|
|
|
|
|
|
// Compute max widths to align output
|
|
|
|
const auto offset_w =
|
|
|
|
static_cast<int>(::log10(last_member_struct_padding_offset)) + 1;
|
|
|
|
const auto size_w = static_cast<int>(::log10(st->Size())) + 1;
|
|
|
|
const auto align_w = static_cast<int>(::log10(st->Align())) + 1;
|
|
|
|
|
|
|
|
auto print_struct_begin_line = [&](size_t align, size_t size,
|
|
|
|
std::string struct_name) {
|
|
|
|
ss << "/* " << std::setw(offset_w) << " "
|
|
|
|
<< "align(" << std::setw(align_w) << align << ") size("
|
|
|
|
<< std::setw(size_w) << size << ") */ struct " << struct_name
|
|
|
|
<< " {\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
auto print_struct_end_line = [&]() {
|
|
|
|
ss << "/* "
|
|
|
|
<< std::setw(offset_w + size_w + align_w) << " "
|
|
|
|
<< "*/ };";
|
|
|
|
};
|
|
|
|
|
|
|
|
auto print_member_line = [&](size_t offset, size_t align, size_t size,
|
|
|
|
std::string s) {
|
|
|
|
ss << "/* offset(" << std::setw(offset_w) << offset << ") align("
|
|
|
|
<< std::setw(align_w) << align << ") size(" << std::setw(size_w)
|
|
|
|
<< size << ") */ " << s << ";\n";
|
|
|
|
};
|
|
|
|
|
|
|
|
print_struct_begin_line(st->Align(), st->Size(),
|
|
|
|
st->FriendlyName(builder_->Symbols()));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < st->Members().size(); ++i) {
|
|
|
|
auto* const m = st->Members()[i];
|
|
|
|
|
|
|
|
// Output field alignment padding, if any
|
|
|
|
auto* const prev_member = (i == 0) ? nullptr : str->Members()[i - 1];
|
|
|
|
if (prev_member) {
|
|
|
|
uint32_t padding =
|
|
|
|
m->Offset() - (prev_member->Offset() + prev_member->Size());
|
|
|
|
if (padding > 0) {
|
|
|
|
size_t padding_offset = m->Offset() - padding;
|
|
|
|
print_member_line(padding_offset, 1, padding,
|
|
|
|
"// -- implicit field alignment padding --");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output member
|
|
|
|
std::string member_name = member_name_of(m);
|
|
|
|
print_member_line(m->Offset(), m->Align(), m->Size(),
|
|
|
|
member_name_of(m) + " : " + type_name_of(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output struct size padding, if any
|
|
|
|
uint32_t struct_padding = st->Size() - last_member_struct_padding_offset;
|
|
|
|
if (struct_padding > 0) {
|
|
|
|
print_member_line(last_member_struct_padding_offset, 1, struct_padding,
|
|
|
|
"// -- implicit struct size padding --");
|
|
|
|
}
|
|
|
|
|
|
|
|
print_struct_end_line();
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ast::IsHostShareable(sc)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < str->Members().size(); ++i) {
|
|
|
|
auto* const m = str->Members()[i];
|
|
|
|
uint32_t required_align = required_alignment_of(m->Type());
|
|
|
|
|
|
|
|
// Validate that member is at a valid byte offset
|
|
|
|
if (m->Offset() % required_align != 0) {
|
|
|
|
AddError("the offset of a struct member of type '" + type_name_of(m) +
|
2021-10-15 17:33:10 +00:00
|
|
|
"' in storage class '" + ast::ToString(sc) +
|
2021-07-15 19:09:25 +00:00
|
|
|
"' must be a multiple of " + std::to_string(required_align) +
|
|
|
|
" bytes, but '" + member_name_of(m) +
|
|
|
|
"' is currently at offset " + std::to_string(m->Offset()) +
|
|
|
|
". Consider setting [[align(" +
|
|
|
|
std::to_string(required_align) + ")]] on this member",
|
2021-10-15 17:33:10 +00:00
|
|
|
m->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
|
|
AddNote("see layout of struct:\n" + get_struct_layout_string(str),
|
2021-10-15 17:33:10 +00:00
|
|
|
str->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
|
|
if (auto* member_str = m->Type()->As<sem::Struct>()) {
|
|
|
|
AddNote("and layout of struct member:\n" +
|
|
|
|
get_struct_layout_string(member_str),
|
2021-10-15 17:33:10 +00:00
|
|
|
member_str->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For uniform buffers, validate that the number of bytes between the
|
|
|
|
// previous member of type struct and the current is a multiple of 16 bytes.
|
|
|
|
auto* const prev_member = (i == 0) ? nullptr : str->Members()[i - 1];
|
|
|
|
if (prev_member && is_uniform_struct(prev_member->Type())) {
|
|
|
|
const uint32_t prev_to_curr_offset = m->Offset() - prev_member->Offset();
|
|
|
|
if (prev_to_curr_offset % 16 != 0) {
|
|
|
|
AddError(
|
|
|
|
"uniform storage requires that the number of bytes between the "
|
|
|
|
"start of the previous member of type struct and the current "
|
|
|
|
"member be a multiple of 16 bytes, but there are currently " +
|
|
|
|
std::to_string(prev_to_curr_offset) + " bytes between '" +
|
|
|
|
member_name_of(prev_member) + "' and '" + member_name_of(m) +
|
|
|
|
"'. Consider setting [[align(16)]] on this member",
|
2021-10-15 17:33:10 +00:00
|
|
|
m->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
|
|
AddNote("see layout of struct:\n" + get_struct_layout_string(str),
|
2021-10-15 17:33:10 +00:00
|
|
|
str->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
|
|
auto* prev_member_str = prev_member->Type()->As<sem::Struct>();
|
|
|
|
AddNote("and layout of previous member struct:\n" +
|
|
|
|
get_struct_layout_string(prev_member_str),
|
2021-10-15 17:33:10 +00:00
|
|
|
prev_member_str->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For uniform buffer array members, validate that array elements are
|
|
|
|
// aligned to 16 bytes
|
|
|
|
if (auto* arr = m->Type()->As<sem::Array>()) {
|
|
|
|
if (sc == ast::StorageClass::kUniform) {
|
|
|
|
// We already validated that this array member is itself aligned to 16
|
|
|
|
// bytes above, so we only need to validate that stride is a multiple of
|
|
|
|
// 16 bytes.
|
|
|
|
if (arr->Stride() % 16 != 0) {
|
|
|
|
AddError(
|
|
|
|
"uniform storage requires that array elements be aligned to 16 "
|
|
|
|
"bytes, but array stride of '" +
|
|
|
|
member_name_of(m) + "' is currently " +
|
|
|
|
std::to_string(arr->Stride()) +
|
|
|
|
". Consider setting [[stride(" +
|
|
|
|
std::to_string(
|
|
|
|
utils::RoundUp(required_align, arr->Stride())) +
|
|
|
|
")]] on the array type",
|
2021-10-15 17:33:10 +00:00
|
|
|
m->Declaration()->type->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
AddNote("see layout of struct:\n" + get_struct_layout_string(str),
|
2021-10-15 17:33:10 +00:00
|
|
|
str->Declaration()->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If member is struct, recurse
|
|
|
|
if (auto* str_member = m->Type()->As<sem::Struct>()) {
|
|
|
|
// Cache result of struct + storage class pair
|
|
|
|
if (valid_struct_storage_layouts_.emplace(str_member, sc).second) {
|
|
|
|
if (!ValidateStorageClassLayout(str_member, sc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateStorageClassLayout(const VariableInfo* info) {
|
|
|
|
if (auto* str = info->type->UnwrapRef()->As<sem::Struct>()) {
|
|
|
|
if (!ValidateStorageClassLayout(str, info->storage_class)) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("see declaration of variable", info->declaration->source);
|
2021-07-15 19:09:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:18:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:20:33 +00:00
|
|
|
bool Resolver::ValidateGlobalVariable(const VariableInfo* info) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(info->declaration->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : info->declaration->decorations) {
|
|
|
|
if (info->declaration->is_const) {
|
2021-05-13 20:32:32 +00:00
|
|
|
if (auto* override_deco = deco->As<ast::OverrideDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (override_deco->has_value) {
|
|
|
|
uint32_t id = override_deco->value;
|
2021-05-13 20:32:32 +00:00
|
|
|
auto itr = constant_ids_.find(id);
|
|
|
|
if (itr != constant_ids_.end() && itr->second != info) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("pipeline constant IDs must be unique", deco->source);
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("a pipeline constant with an ID of " + std::to_string(id) +
|
|
|
|
" was previously declared "
|
|
|
|
"here:",
|
|
|
|
ast::GetDecoration<ast::OverrideDecoration>(
|
2021-10-15 17:33:10 +00:00
|
|
|
itr->second->declaration->decorations)
|
|
|
|
->source);
|
2021-05-13 20:32:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (id > 65535) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("pipeline constant IDs must be between 0 and 65535",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-05-13 20:32:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for constants", deco->source);
|
2021-05-10 19:16:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-24 15:53:26 +00:00
|
|
|
bool is_shader_io_decoration =
|
2021-06-28 23:04:43 +00:00
|
|
|
deco->IsAnyOf<ast::BuiltinDecoration, ast::InterpolateDecoration,
|
2021-07-12 12:12:32 +00:00
|
|
|
ast::InvariantDecoration, ast::LocationDecoration>();
|
2021-06-24 15:53:26 +00:00
|
|
|
bool has_io_storage_class =
|
|
|
|
info->storage_class == ast::StorageClass::kInput ||
|
|
|
|
info->storage_class == ast::StorageClass::kOutput;
|
|
|
|
if (!(deco->IsAnyOf<ast::BindingDecoration, ast::GroupDecoration,
|
|
|
|
ast::InternalDecoration>()) &&
|
|
|
|
(!is_shader_io_decoration || !has_io_storage_class)) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for variables", deco->source);
|
2021-05-10 19:16:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto binding_point = info->declaration->BindingPoint();
|
2021-05-10 19:16:46 +00:00
|
|
|
switch (info->storage_class) {
|
|
|
|
case ast::StorageClass::kUniform:
|
|
|
|
case ast::StorageClass::kStorage:
|
|
|
|
case ast::StorageClass::kUniformConstant: {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#resource-interface
|
|
|
|
// Each resource variable must be declared with both group and binding
|
|
|
|
// attributes.
|
|
|
|
if (!binding_point) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-05-18 10:28:48 +00:00
|
|
|
"resource variables require [[group]] and [[binding]] "
|
|
|
|
"decorations",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-05-10 19:16:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (binding_point.binding || binding_point.group) {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#attribute-binding
|
|
|
|
// Must only be applied to a resource variable
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-05-10 19:16:46 +00:00
|
|
|
"non-resource variables must not have [[group]] or [[binding]] "
|
|
|
|
"decorations",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-05-10 19:16:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 20:41:47 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#variable-declaration
|
|
|
|
// The access mode always has a default, and except for variables in the
|
|
|
|
// storage storage class, must not be written.
|
|
|
|
if (info->storage_class != ast::StorageClass::kStorage &&
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->declared_access != ast::Access::kUndefined) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-08-05 15:18:29 +00:00
|
|
|
"only variables in <storage> storage class may declare an access mode",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-06-04 20:41:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
switch (info->storage_class) {
|
|
|
|
case ast::StorageClass::kStorage: {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables
|
|
|
|
// A variable in the storage storage class is a storage buffer variable.
|
|
|
|
// Its store type must be a host-shareable structure type with block
|
|
|
|
// attribute, satisfying the storage class constraints.
|
|
|
|
|
2021-06-04 20:41:47 +00:00
|
|
|
auto* str = info->type->UnwrapRef()->As<sem::Struct>();
|
2021-05-14 17:51:13 +00:00
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
if (!str) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-06-04 20:41:47 +00:00
|
|
|
"variables declared in the <storage> storage class must be of a "
|
|
|
|
"structure type",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-04-21 17:55:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str->IsBlockDecorated()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-04-21 17:55:12 +00:00
|
|
|
"structure used as a storage buffer must be declared with the "
|
|
|
|
"[[block]] decoration",
|
2021-10-15 17:33:10 +00:00
|
|
|
str->Declaration()->source);
|
|
|
|
if (info->declaration->source.range.begin.line) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("structure used as storage buffer here",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-04-21 17:55:12 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-04-19 20:20:33 +00:00
|
|
|
}
|
2021-04-21 17:55:12 +00:00
|
|
|
case ast::StorageClass::kUniform: {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables
|
|
|
|
// A variable in the uniform storage class is a uniform buffer variable.
|
|
|
|
// Its store type must be a host-shareable structure type with block
|
|
|
|
// attribute, satisfying the storage class constraints.
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* str = info->type->UnwrapRef()->As<sem::Struct>();
|
2021-04-21 17:55:12 +00:00
|
|
|
if (!str) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-04-21 17:55:12 +00:00
|
|
|
"variables declared in the <uniform> storage class must be of a "
|
|
|
|
"structure type",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-04-21 17:55:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-19 20:20:33 +00:00
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
if (!str->IsBlockDecorated()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-04-21 17:55:12 +00:00
|
|
|
"structure used as a uniform buffer must be declared with the "
|
|
|
|
"[[block]] decoration",
|
2021-10-15 17:33:10 +00:00
|
|
|
str->Declaration()->source);
|
|
|
|
if (info->declaration->source.range.begin.line) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("structure used as uniform buffer here",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-04-21 17:55:12 +00:00
|
|
|
}
|
|
|
|
return false;
|
2021-04-19 20:20:33 +00:00
|
|
|
}
|
2021-06-16 19:40:13 +00:00
|
|
|
|
|
|
|
for (auto* member : str->Members()) {
|
|
|
|
if (auto* arr = member->Type()->As<sem::Array>()) {
|
|
|
|
if (arr->IsRuntimeSized()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-06-16 19:40:13 +00:00
|
|
|
"structure containing a runtime sized array "
|
|
|
|
"cannot be used as a uniform buffer",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
|
|
|
AddNote("structure is declared here", str->Declaration()->source);
|
2021-06-16 19:40:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
break;
|
2021-04-19 20:20:33 +00:00
|
|
|
}
|
2021-04-21 17:55:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
2021-04-19 20:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!info->declaration->is_const) {
|
2021-08-05 15:18:29 +00:00
|
|
|
if (!ValidateAtomicVariable(info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 18:01:51 +00:00
|
|
|
return ValidateVariable(info);
|
2021-04-19 20:20:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:29 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#atomic-types
|
|
|
|
// Atomic types may only be instantiated by variables in the workgroup storage
|
|
|
|
// class or by storage buffer variables with a read_write access mode.
|
|
|
|
bool Resolver::ValidateAtomicVariable(const VariableInfo* info) {
|
|
|
|
auto sc = info->storage_class;
|
|
|
|
auto access = info->access;
|
|
|
|
auto* type = info->type->UnwrapRef();
|
2021-10-15 17:33:10 +00:00
|
|
|
auto source = info->declaration->type ? info->declaration->type->source
|
|
|
|
: info->declaration->source;
|
2021-08-05 15:18:29 +00:00
|
|
|
|
|
|
|
if (type->Is<sem::Atomic>()) {
|
|
|
|
if (sc != ast::StorageClass::kWorkgroup) {
|
|
|
|
AddError(
|
|
|
|
"atomic variables must have <storage> or <workgroup> storage class",
|
2021-08-11 19:59:44 +00:00
|
|
|
source);
|
2021-08-05 15:18:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (type->IsAnyOf<sem::Struct, sem::Array>()) {
|
|
|
|
auto found = atomic_composite_info_.find(type);
|
|
|
|
if (found != atomic_composite_info_.end()) {
|
|
|
|
if (sc != ast::StorageClass::kStorage &&
|
|
|
|
sc != ast::StorageClass::kWorkgroup) {
|
|
|
|
AddError(
|
|
|
|
"atomic variables must have <storage> or <workgroup> storage class",
|
|
|
|
source);
|
|
|
|
AddNote("atomic sub-type of '" +
|
|
|
|
type->FriendlyName(builder_->Symbols()) +
|
|
|
|
"' is declared here",
|
|
|
|
found->second);
|
|
|
|
return false;
|
|
|
|
} else if (sc == ast::StorageClass::kStorage &&
|
|
|
|
access != ast::Access::kReadWrite) {
|
|
|
|
AddError(
|
|
|
|
"atomic variables in <storage> storage class must have read_write "
|
|
|
|
"access mode",
|
|
|
|
source);
|
|
|
|
AddNote("atomic sub-type of '" +
|
|
|
|
type->FriendlyName(builder_->Symbols()) +
|
|
|
|
"' is declared here",
|
|
|
|
found->second);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-10 18:01:51 +00:00
|
|
|
bool Resolver::ValidateVariable(const VariableInfo* info) {
|
|
|
|
auto* var = info->declaration;
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* storage_type = info->type->UnwrapRef();
|
2021-06-09 09:12:57 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var->is_const && !IsStorable(storage_type)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(storage_type->FriendlyName(builder_->Symbols()) +
|
|
|
|
" cannot be used as the type of a var",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-06-09 09:12:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (var->is_const && info->kind != VariableKind::kParameter &&
|
2021-07-26 22:11:58 +00:00
|
|
|
!(storage_type->IsConstructible() || storage_type->Is<sem::Pointer>())) {
|
|
|
|
AddError(storage_type->FriendlyName(builder_->Symbols()) +
|
|
|
|
" cannot be used as the type of a let",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-07-26 22:11:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (auto* r = storage_type->As<sem::Array>()) {
|
2021-05-07 20:58:34 +00:00
|
|
|
if (r->IsRuntimeSized()) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError("runtime arrays may only appear as the last member of a struct",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-03-19 14:04:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 16:09:21 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (auto* r = storage_type->As<sem::MultisampledTexture>()) {
|
2021-04-21 13:47:12 +00:00
|
|
|
if (r->dim() != ast::TextureDimension::k2d) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("only 2d multisampled textures are supported", var->source);
|
2021-04-20 16:09:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (!r->type()->UnwrapRef()->is_numeric_scalar()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("texture_multisampled_2d<type>: type must be f32, i32 or u32",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-04-20 16:09:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (storage_type->is_handle() &&
|
2021-10-15 17:33:10 +00:00
|
|
|
var->declared_storage_class != ast::StorageClass::kNone) {
|
2021-05-10 18:01:51 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#module-scope-variables
|
2021-05-18 10:28:48 +00:00
|
|
|
// If the store type is a texture type or a sampler type, then the
|
|
|
|
// variable declaration must not have a storage class decoration. The
|
|
|
|
// storage class will always be handle.
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("variables of type '" + info->type_name +
|
|
|
|
"' must not have a storage class",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source);
|
2021-05-10 18:01:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (IsValidationEnabled(var->decorations,
|
2021-06-30 19:22:30 +00:00
|
|
|
ast::DisabledValidation::kIgnoreStorageClass) &&
|
2021-10-15 17:33:10 +00:00
|
|
|
(var->declared_storage_class == ast::StorageClass::kInput ||
|
|
|
|
var->declared_storage_class == ast::StorageClass::kOutput)) {
|
|
|
|
AddError("invalid use of input/output storage class", var->source);
|
2021-06-24 15:53:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-19 14:04:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:24:04 +00:00
|
|
|
bool Resolver::ValidateFunctionParameter(const ast::Function* func,
|
|
|
|
const VariableInfo* info) {
|
2021-06-18 19:58:27 +00:00
|
|
|
if (!ValidateVariable(info)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-28 14:05:25 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : info->declaration->decorations) {
|
2021-06-30 19:22:30 +00:00
|
|
|
if (!func->IsEntryPoint() && !deco->Is<ast::InternalDecoration>()) {
|
2021-07-08 14:02:56 +00:00
|
|
|
AddError(
|
|
|
|
"decoration is not valid for non-entry point function parameters",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-24 16:20:16 +00:00
|
|
|
return false;
|
2021-07-21 12:16:35 +00:00
|
|
|
} else if (!deco->IsAnyOf<ast::BuiltinDecoration, ast::InvariantDecoration,
|
2021-07-23 13:23:20 +00:00
|
|
|
ast::LocationDecoration,
|
|
|
|
ast::InterpolateDecoration,
|
2021-06-24 16:20:16 +00:00
|
|
|
ast::InternalDecoration>() &&
|
2021-06-30 19:22:30 +00:00
|
|
|
(IsValidationEnabled(
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->decorations,
|
2021-06-30 19:22:30 +00:00
|
|
|
ast::DisabledValidation::kEntryPointParameter) &&
|
|
|
|
IsValidationEnabled(
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->decorations,
|
2021-07-07 20:41:00 +00:00
|
|
|
ast::DisabledValidation::
|
|
|
|
kIgnoreConstructibleFunctionParameter))) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for function parameters", deco->source);
|
2021-06-24 16:20:16 +00:00
|
|
|
return false;
|
2021-06-09 16:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-29 15:24:04 +00:00
|
|
|
|
|
|
|
if (auto* ref = info->type->As<sem::Pointer>()) {
|
|
|
|
auto sc = ref->StorageClass();
|
|
|
|
if (!(sc == ast::StorageClass::kFunction ||
|
|
|
|
sc == ast::StorageClass::kPrivate ||
|
2021-10-28 15:00:39 +00:00
|
|
|
sc == ast::StorageClass::kWorkgroup) &&
|
|
|
|
IsValidationEnabled(info->declaration->decorations,
|
|
|
|
ast::DisabledValidation::kIgnoreStorageClass)) {
|
2021-06-29 15:24:04 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "function parameter of pointer type cannot be in '" << sc
|
|
|
|
<< "' storage class";
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(ss.str(), info->declaration->source);
|
2021-06-29 15:24:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPlain(info->type)) {
|
2021-07-20 18:28:31 +00:00
|
|
|
if (!info->type->IsConstructible() &&
|
2021-06-30 19:22:30 +00:00
|
|
|
IsValidationEnabled(
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->decorations,
|
2021-07-07 20:41:00 +00:00
|
|
|
ast::DisabledValidation::kIgnoreConstructibleFunctionParameter)) {
|
|
|
|
AddError("store type of function parameter must be a constructible type",
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-06-29 15:24:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (!info->type->IsAnyOf<sem::Texture, sem::Sampler, sem::Pointer>()) {
|
|
|
|
AddError("store type of function parameter cannot be " +
|
|
|
|
info->type->FriendlyName(builder_->Symbols()),
|
2021-10-15 17:33:10 +00:00
|
|
|
info->declaration->source);
|
2021-06-29 15:24:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-18 19:58:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-09 16:01:29 +00:00
|
|
|
|
2021-06-18 19:58:27 +00:00
|
|
|
bool Resolver::ValidateBuiltinDecoration(const ast::BuiltinDecoration* deco,
|
2021-07-08 14:02:56 +00:00
|
|
|
const sem::Type* storage_type,
|
2021-10-05 15:02:17 +00:00
|
|
|
const bool is_input) {
|
2021-06-18 19:58:27 +00:00
|
|
|
auto* type = storage_type->UnwrapRef();
|
2021-07-08 14:02:56 +00:00
|
|
|
const auto stage = current_function_
|
2021-10-15 17:33:10 +00:00
|
|
|
? current_function_->declaration->PipelineStage()
|
2021-07-08 14:02:56 +00:00
|
|
|
: ast::PipelineStage::kNone;
|
|
|
|
std::stringstream stage_name;
|
|
|
|
stage_name << stage;
|
|
|
|
bool is_stage_mismatch = false;
|
2021-07-23 13:23:20 +00:00
|
|
|
bool is_output = !is_input;
|
2021-10-15 17:33:10 +00:00
|
|
|
switch (deco->builtin) {
|
2021-06-23 17:35:42 +00:00
|
|
|
case ast::Builtin::kPosition:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
2021-07-23 13:23:20 +00:00
|
|
|
!((is_input && stage == ast::PipelineStage::kFragment) ||
|
|
|
|
(is_output && stage == ast::PipelineStage::kVertex))) {
|
|
|
|
is_stage_mismatch = true;
|
2021-07-08 14:02:56 +00:00
|
|
|
}
|
2021-07-22 18:31:34 +00:00
|
|
|
if (!(type->is_float_vector() && type->As<sem::Vector>()->Width() == 4)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'vec4<f32>'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-23 17:35:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Builtin::kGlobalInvocationId:
|
|
|
|
case ast::Builtin::kLocalInvocationId:
|
2021-09-13 17:11:58 +00:00
|
|
|
case ast::Builtin::kNumWorkgroups:
|
2021-06-23 17:35:42 +00:00
|
|
|
case ast::Builtin::kWorkgroupId:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kCompute && is_input)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
2021-06-23 17:35:42 +00:00
|
|
|
if (!(type->is_unsigned_integer_vector() &&
|
2021-07-22 18:31:34 +00:00
|
|
|
type->As<sem::Vector>()->Width() == 3)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'vec3<u32>'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-23 17:35:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Builtin::kFragDepth:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kFragment && !is_input)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
2021-06-23 17:35:42 +00:00
|
|
|
if (!type->Is<sem::F32>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'f32'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-23 17:35:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-18 19:58:27 +00:00
|
|
|
case ast::Builtin::kFrontFacing:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kFragment && is_input)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
2021-06-18 19:58:27 +00:00
|
|
|
if (!type->Is<sem::Bool>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'bool'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-18 19:58:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-23 17:35:42 +00:00
|
|
|
case ast::Builtin::kLocalInvocationIndex:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kCompute && is_input)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
|
|
|
if (!type->Is<sem::U32>()) {
|
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'u32'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-07-08 14:02:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-23 17:35:42 +00:00
|
|
|
case ast::Builtin::kVertexIndex:
|
|
|
|
case ast::Builtin::kInstanceIndex:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kVertex && is_input)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
|
|
|
if (!type->Is<sem::U32>()) {
|
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'u32'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-07-08 14:02:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-18 19:58:27 +00:00
|
|
|
case ast::Builtin::kSampleMask:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kFragment)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
|
|
|
if (!type->Is<sem::U32>()) {
|
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'u32'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-07-08 14:02:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2021-06-18 19:58:27 +00:00
|
|
|
case ast::Builtin::kSampleIndex:
|
2021-07-08 14:02:56 +00:00
|
|
|
if (stage != ast::PipelineStage::kNone &&
|
|
|
|
!(stage == ast::PipelineStage::kFragment && is_input)) {
|
|
|
|
is_stage_mismatch = true;
|
|
|
|
}
|
2021-06-18 19:58:27 +00:00
|
|
|
if (!type->Is<sem::U32>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("store type of " + deco_to_str(deco) + " must be 'u32'",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-18 19:58:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-07-08 14:02:56 +00:00
|
|
|
|
2021-10-05 15:02:17 +00:00
|
|
|
if (is_stage_mismatch) {
|
|
|
|
AddError(deco_to_str(deco) + " cannot be used in " +
|
|
|
|
(is_input ? "input of " : "output of ") + stage_name.str() +
|
|
|
|
" pipeline stage",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-10-05 15:02:17 +00:00
|
|
|
return false;
|
2021-07-08 14:02:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 19:58:27 +00:00
|
|
|
return true;
|
2021-04-06 14:07:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 23:04:43 +00:00
|
|
|
bool Resolver::ValidateInterpolateDecoration(
|
|
|
|
const ast::InterpolateDecoration* deco,
|
|
|
|
const sem::Type* storage_type) {
|
|
|
|
auto* type = storage_type->UnwrapRef();
|
|
|
|
|
2021-10-21 23:08:44 +00:00
|
|
|
if (type->is_integer_scalar_or_vector() &&
|
|
|
|
deco->type != ast::InterpolationType::kFlat) {
|
2021-06-28 23:04:43 +00:00
|
|
|
AddError(
|
2021-10-21 23:08:44 +00:00
|
|
|
"interpolation type must be 'flat' for integral user-defined IO types",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-28 23:04:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (deco->type == ast::InterpolationType::kFlat &&
|
|
|
|
deco->sampling != ast::InterpolationSampling::kNone) {
|
2021-06-28 23:04:43 +00:00
|
|
|
AddError("flat interpolation attribute must not have a sampling parameter",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-28 23:04:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-21 16:13:42 +00:00
|
|
|
bool Resolver::ValidateFunction(const ast::Function* func,
|
|
|
|
const FunctionInfo* info) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDefinition(func->symbol, func->source,
|
2021-06-29 21:30:37 +00:00
|
|
|
/* check_global_scope_only */ true)) {
|
2021-03-19 14:04:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:41:03 +00:00
|
|
|
auto workgroup_deco_count = 0;
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : func->decorations) {
|
2021-07-08 14:02:56 +00:00
|
|
|
if (deco->Is<ast::WorkgroupDecoration>()) {
|
2021-05-14 20:41:03 +00:00
|
|
|
workgroup_deco_count++;
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->PipelineStage() != ast::PipelineStage::kCompute) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-05-14 20:41:03 +00:00
|
|
|
"the workgroup_size attribute is only valid for compute stages",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-05-14 20:41:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-08 14:02:56 +00:00
|
|
|
} else if (!deco->IsAnyOf<ast::StageDecoration,
|
|
|
|
ast::InternalDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for functions", deco->source);
|
2021-05-14 20:41:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->params.size() > 255) {
|
|
|
|
AddError("functions may declare at most 255 parameters", func->source);
|
2021-07-17 17:45:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* param : func->params) {
|
2021-06-29 15:24:04 +00:00
|
|
|
if (!ValidateFunctionParameter(func, variable_to_info_.at(param))) {
|
2021-03-19 14:04:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:24:29 +00:00
|
|
|
if (!info->return_type->Is<sem::Void>()) {
|
2021-07-20 18:28:31 +00:00
|
|
|
if (!info->return_type->IsConstructible()) {
|
2021-07-07 20:41:00 +00:00
|
|
|
AddError("function return type must be a constructible type",
|
2021-10-15 17:33:10 +00:00
|
|
|
func->return_type->source);
|
2021-06-21 20:51:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->body) {
|
|
|
|
if (!func->body->Last() ||
|
|
|
|
!func->body->Last()->Is<ast::ReturnStatement>()) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError("non-void function must end with a return statement",
|
2021-10-15 17:33:10 +00:00
|
|
|
func->source);
|
2021-04-07 08:09:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-30 19:22:30 +00:00
|
|
|
} else if (IsValidationEnabled(
|
2021-10-15 17:33:10 +00:00
|
|
|
func->decorations,
|
2021-05-12 12:54:21 +00:00
|
|
|
ast::DisabledValidation::kFunctionHasNoBody)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-10-15 17:33:10 +00:00
|
|
|
<< "Function " << builder_->Symbols().NameFor(func->symbol)
|
2021-05-12 12:54:21 +00:00
|
|
|
<< " has no body";
|
2021-03-19 14:04:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : func->return_type_decorations) {
|
2021-06-28 09:53:37 +00:00
|
|
|
if (!func->IsEntryPoint()) {
|
2021-07-08 14:02:56 +00:00
|
|
|
AddError(
|
|
|
|
"decoration is not valid for non-entry point function return types",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-03-19 14:04:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-23 13:23:20 +00:00
|
|
|
if (!deco->IsAnyOf<ast::BuiltinDecoration, ast::InternalDecoration,
|
|
|
|
ast::LocationDecoration, ast::InterpolateDecoration,
|
|
|
|
ast::InvariantDecoration>() &&
|
2021-10-15 17:33:10 +00:00
|
|
|
(IsValidationEnabled(info->declaration->decorations,
|
2021-07-23 13:23:20 +00:00
|
|
|
ast::DisabledValidation::kEntryPointParameter) &&
|
2021-10-15 17:33:10 +00:00
|
|
|
IsValidationEnabled(info->declaration->decorations,
|
2021-07-23 13:23:20 +00:00
|
|
|
ast::DisabledValidation::
|
|
|
|
kIgnoreConstructibleFunctionParameter))) {
|
2021-06-28 09:53:37 +00:00
|
|
|
AddError("decoration is not valid for entry point return types",
|
2021-10-15 17:33:10 +00:00
|
|
|
deco->source);
|
2021-06-28 09:53:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-19 14:04:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:51:47 +00:00
|
|
|
if (func->IsEntryPoint()) {
|
2021-04-21 16:13:42 +00:00
|
|
|
if (!ValidateEntryPoint(func, info)) {
|
2021-04-06 15:51:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-21 16:13:42 +00:00
|
|
|
bool Resolver::ValidateEntryPoint(const ast::Function* func,
|
|
|
|
const FunctionInfo* info) {
|
2021-04-06 15:51:47 +00:00
|
|
|
// Use a lambda to validate the entry point decorations for a type.
|
2021-05-18 10:28:48 +00:00
|
|
|
// Persistent state is used to track which builtins and locations have
|
|
|
|
// already been seen, in order to catch conflicts.
|
|
|
|
// TODO(jrprice): This state could be stored in FunctionInfo instead, and
|
|
|
|
// then passed to sem::Function since it would be useful there too.
|
2021-04-06 15:51:47 +00:00
|
|
|
std::unordered_set<ast::Builtin> builtins;
|
|
|
|
std::unordered_set<uint32_t> locations;
|
|
|
|
enum class ParamOrRetType {
|
|
|
|
kParameter,
|
|
|
|
kReturnType,
|
|
|
|
};
|
2021-06-18 19:58:27 +00:00
|
|
|
|
2021-04-06 15:51:47 +00:00
|
|
|
// Inner lambda that is applied to a type and all of its members.
|
2021-10-19 18:38:54 +00:00
|
|
|
auto validate_entry_point_decorations_inner = [&](const ast::DecorationList&
|
|
|
|
decos,
|
|
|
|
sem::Type* ty,
|
|
|
|
Source source,
|
|
|
|
ParamOrRetType param_or_ret,
|
|
|
|
bool is_struct_member) {
|
|
|
|
// Scan decorations for pipeline IO attributes.
|
|
|
|
// Check for overlap with attributes that have been seen previously.
|
|
|
|
const ast::Decoration* pipeline_io_attribute = nullptr;
|
2021-10-21 23:08:44 +00:00
|
|
|
const ast::InterpolateDecoration* interpolate_attribute = nullptr;
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::InvariantDecoration* invariant_attribute = nullptr;
|
|
|
|
for (auto* deco : decos) {
|
|
|
|
auto is_invalid_compute_shader_decoration = false;
|
|
|
|
if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
|
|
|
|
if (pipeline_io_attribute) {
|
|
|
|
AddError("multiple entry point IO attributes", deco->source);
|
|
|
|
AddNote("previously consumed " + deco_to_str(pipeline_io_attribute),
|
2021-10-15 17:33:10 +00:00
|
|
|
pipeline_io_attribute->source);
|
2021-10-19 18:38:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pipeline_io_attribute = deco;
|
|
|
|
|
|
|
|
if (builtins.count(builtin->builtin)) {
|
|
|
|
AddError(deco_to_str(builtin) +
|
|
|
|
" attribute appears multiple times as pipeline " +
|
|
|
|
(param_or_ret == ParamOrRetType::kParameter ? "input"
|
|
|
|
: "output"),
|
|
|
|
func->source);
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-06 15:51:47 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
if (!ValidateBuiltinDecoration(
|
|
|
|
builtin, ty,
|
|
|
|
/* is_input */ param_or_ret == ParamOrRetType::kParameter)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
builtins.emplace(builtin->builtin);
|
|
|
|
} else if (auto* location = deco->As<ast::LocationDecoration>()) {
|
|
|
|
if (pipeline_io_attribute) {
|
|
|
|
AddError("multiple entry point IO attributes", deco->source);
|
|
|
|
AddNote("previously consumed " + deco_to_str(pipeline_io_attribute),
|
2021-10-15 17:33:10 +00:00
|
|
|
pipeline_io_attribute->source);
|
2021-10-19 18:38:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pipeline_io_attribute = deco;
|
2021-07-08 15:12:36 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool is_input = param_or_ret == ParamOrRetType::kParameter;
|
|
|
|
if (!ValidateLocationDecoration(location, ty, locations, source,
|
|
|
|
is_input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (auto* interpolate = deco->As<ast::InterpolateDecoration>()) {
|
|
|
|
if (func->PipelineStage() == ast::PipelineStage::kCompute) {
|
|
|
|
is_invalid_compute_shader_decoration = true;
|
|
|
|
} else if (!ValidateInterpolateDecoration(interpolate, ty)) {
|
|
|
|
return false;
|
2021-07-29 16:55:27 +00:00
|
|
|
}
|
2021-10-21 23:08:44 +00:00
|
|
|
interpolate_attribute = interpolate;
|
2021-10-19 18:38:54 +00:00
|
|
|
} else if (auto* invariant = deco->As<ast::InvariantDecoration>()) {
|
|
|
|
if (func->PipelineStage() == ast::PipelineStage::kCompute) {
|
|
|
|
is_invalid_compute_shader_decoration = true;
|
|
|
|
}
|
|
|
|
invariant_attribute = invariant;
|
|
|
|
}
|
|
|
|
if (is_invalid_compute_shader_decoration) {
|
|
|
|
std::string input_or_output =
|
|
|
|
param_or_ret == ParamOrRetType::kParameter ? "inputs" : "output";
|
|
|
|
AddError(
|
|
|
|
"decoration is not valid for compute shader " + input_or_output,
|
|
|
|
deco->source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 15:51:47 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
if (IsValidationEnabled(decos,
|
|
|
|
ast::DisabledValidation::kEntryPointParameter)) {
|
|
|
|
if (is_struct_member && ty->Is<sem::Struct>()) {
|
|
|
|
AddError("nested structures cannot be used for entry point IO", source);
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 14:55:39 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
if (!ty->Is<sem::Struct>() && !pipeline_io_attribute) {
|
|
|
|
std::string err = "missing entry point IO attribute";
|
|
|
|
if (!is_struct_member) {
|
|
|
|
err +=
|
|
|
|
(param_or_ret == ParamOrRetType::kParameter ? " on parameter"
|
|
|
|
: " on return type");
|
|
|
|
}
|
|
|
|
AddError(err, source);
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-07 20:28:04 +00:00
|
|
|
|
2021-10-21 23:08:44 +00:00
|
|
|
if (pipeline_io_attribute &&
|
|
|
|
pipeline_io_attribute->Is<ast::LocationDecoration>()) {
|
|
|
|
if (ty->is_integer_scalar_or_vector() && !interpolate_attribute) {
|
|
|
|
// TODO(crbug.com/tint/1224): Make these errors once downstream
|
|
|
|
// usages have caught up (no sooner than M99).
|
|
|
|
if (func->PipelineStage() == ast::PipelineStage::kVertex &&
|
|
|
|
param_or_ret == ParamOrRetType::kReturnType) {
|
|
|
|
AddWarning(
|
|
|
|
"integral user-defined vertex outputs must have a flat "
|
|
|
|
"interpolation attribute",
|
|
|
|
source);
|
|
|
|
}
|
|
|
|
if (func->PipelineStage() == ast::PipelineStage::kFragment &&
|
|
|
|
param_or_ret == ParamOrRetType::kParameter) {
|
|
|
|
AddWarning(
|
|
|
|
"integral user-defined fragment inputs must have a flat "
|
|
|
|
"interpolation attribute",
|
|
|
|
source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
if (invariant_attribute) {
|
|
|
|
bool has_position = false;
|
|
|
|
if (pipeline_io_attribute) {
|
|
|
|
if (auto* builtin =
|
|
|
|
pipeline_io_attribute->As<ast::BuiltinDecoration>()) {
|
|
|
|
has_position = (builtin->builtin == ast::Builtin::kPosition);
|
2021-07-28 18:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-19 18:38:54 +00:00
|
|
|
if (!has_position) {
|
|
|
|
AddError(
|
|
|
|
"invariant attribute must only be applied to a position "
|
|
|
|
"builtin",
|
|
|
|
invariant_attribute->source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2021-04-06 15:51:47 +00:00
|
|
|
|
|
|
|
// Outer lambda for validating the entry point decorations for a type.
|
|
|
|
auto validate_entry_point_decorations = [&](const ast::DecorationList& decos,
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* ty, Source source,
|
2021-04-06 15:51:47 +00:00
|
|
|
ParamOrRetType param_or_ret) {
|
|
|
|
if (!validate_entry_point_decorations_inner(decos, ty, source, param_or_ret,
|
2021-07-28 18:51:37 +00:00
|
|
|
/*is_struct_member*/ false)) {
|
2021-04-06 15:51:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-10 17:38:01 +00:00
|
|
|
if (auto* str = ty->As<sem::Struct>()) {
|
2021-05-07 14:49:34 +00:00
|
|
|
for (auto* member : str->Members()) {
|
2021-05-05 09:09:41 +00:00
|
|
|
if (!validate_entry_point_decorations_inner(
|
2021-10-15 17:33:10 +00:00
|
|
|
member->Declaration()->decorations, member->Type(),
|
|
|
|
member->Declaration()->source, param_or_ret,
|
2021-07-28 18:51:37 +00:00
|
|
|
/*is_struct_member*/ true)) {
|
2021-07-23 13:23:20 +00:00
|
|
|
AddNote("while analysing entry point '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(func->symbol) + "'",
|
|
|
|
func->source);
|
2021-04-06 15:51:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* param : info->parameters) {
|
2021-04-06 15:51:47 +00:00
|
|
|
if (!validate_entry_point_decorations(
|
2021-10-15 17:33:10 +00:00
|
|
|
param->declaration->decorations, param->type,
|
|
|
|
param->declaration->source, ParamOrRetType::kParameter)) {
|
2021-04-06 15:51:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 16:13:42 +00:00
|
|
|
// Clear IO sets after parameter validation. Builtin and location attributes
|
|
|
|
// in return types should be validated independently from those used in
|
|
|
|
// parameters.
|
|
|
|
builtins.clear();
|
|
|
|
locations.clear();
|
|
|
|
|
2021-04-30 19:24:29 +00:00
|
|
|
if (!info->return_type->Is<sem::Void>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!validate_entry_point_decorations(func->return_type_decorations,
|
|
|
|
info->return_type, func->source,
|
2021-04-06 15:51:47 +00:00
|
|
|
ParamOrRetType::kReturnType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->PipelineStage() == ast::PipelineStage::kVertex &&
|
2021-04-21 16:13:42 +00:00
|
|
|
builtins.count(ast::Builtin::kPosition) == 0) {
|
|
|
|
// Check module-scope variables, as the SPIR-V sanitizer generates these.
|
|
|
|
bool found = false;
|
|
|
|
for (auto* var : info->referenced_module_vars) {
|
|
|
|
if (auto* builtin = ast::GetDecoration<ast::BuiltinDecoration>(
|
2021-10-15 17:33:10 +00:00
|
|
|
var->declaration->decorations)) {
|
|
|
|
if (builtin->builtin == ast::Builtin::kPosition) {
|
2021-04-21 16:13:42 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 09:50:11 +00:00
|
|
|
if (!found) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-04-21 16:13:42 +00:00
|
|
|
"a vertex shader must include the 'position' builtin in its return "
|
|
|
|
"type",
|
2021-10-15 17:33:10 +00:00
|
|
|
func->source);
|
2021-04-21 16:13:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->PipelineStage() == ast::PipelineStage::kCompute) {
|
|
|
|
if (!ast::HasDecoration<ast::WorkgroupDecoration>(func->decorations)) {
|
2021-06-29 18:39:44 +00:00
|
|
|
AddError(
|
|
|
|
"a compute shader must include 'workgroup_size' in its "
|
|
|
|
"attributes",
|
2021-10-15 17:33:10 +00:00
|
|
|
func->source);
|
2021-06-29 18:39:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 19:16:46 +00:00
|
|
|
// Validate there are no resource variable binding collisions
|
|
|
|
std::unordered_map<sem::BindingPoint, const ast::Variable*> binding_points;
|
|
|
|
for (auto* var_info : info->referenced_module_vars) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var_info->declaration->BindingPoint()) {
|
2021-05-10 19:16:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto bp = var_info->binding_point;
|
|
|
|
auto res = binding_points.emplace(bp, var_info->declaration);
|
2021-05-12 13:30:51 +00:00
|
|
|
if (!res.second &&
|
2021-10-15 17:33:10 +00:00
|
|
|
IsValidationEnabled(var_info->declaration->decorations,
|
2021-06-30 19:22:30 +00:00
|
|
|
ast::DisabledValidation::kBindingPointCollision) &&
|
2021-10-15 17:33:10 +00:00
|
|
|
IsValidationEnabled(res.first->second->decorations,
|
2021-06-30 19:22:30 +00:00
|
|
|
ast::DisabledValidation::kBindingPointCollision)) {
|
2021-05-10 19:16:46 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#resource-interface
|
|
|
|
// Bindings must not alias within a shader stage: two different
|
|
|
|
// variables in the resource interface of a given shader must not have
|
|
|
|
// the same group and binding values, when considered as a pair of
|
|
|
|
// values.
|
2021-10-15 17:33:10 +00:00
|
|
|
auto func_name = builder_->Symbols().NameFor(info->declaration->symbol);
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("entry point '" + func_name +
|
|
|
|
"' references multiple variables that use the "
|
|
|
|
"same resource binding [[group(" +
|
|
|
|
std::to_string(bp.group) + "), binding(" +
|
|
|
|
std::to_string(bp.binding) + ")]]",
|
2021-10-15 17:33:10 +00:00
|
|
|
var_info->declaration->source);
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("first resource binding usage declared here",
|
2021-10-15 17:33:10 +00:00
|
|
|
res.first->second->source);
|
2021-05-10 19:16:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:04:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Function(const ast::Function* func) {
|
2021-04-30 19:24:29 +00:00
|
|
|
auto* info = function_infos_.Create<FunctionInfo>(func);
|
2021-02-16 18:45:45 +00:00
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
if (func->IsEntryPoint()) {
|
|
|
|
entry_points_.emplace_back(info);
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:11:04 +00:00
|
|
|
TINT_SCOPED_ASSIGNMENT(current_function_, info);
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2021-11-04 22:29:22 +00:00
|
|
|
variable_stack_.Push();
|
2021-07-22 13:24:59 +00:00
|
|
|
uint32_t parameter_index = 0;
|
2021-08-26 15:40:06 +00:00
|
|
|
std::unordered_map<Symbol, Source> parameter_names;
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* param : func->params) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(param);
|
2021-08-23 19:01:19 +00:00
|
|
|
|
2021-08-26 15:40:06 +00:00
|
|
|
{ // Check the parameter name is unique for the function
|
2021-10-15 17:33:10 +00:00
|
|
|
auto emplaced = parameter_names.emplace(param->symbol, param->source);
|
2021-08-26 15:40:06 +00:00
|
|
|
if (!emplaced.second) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto name = builder_->Symbols().NameFor(param->symbol);
|
|
|
|
AddError("redefinition of parameter '" + name + "'", param->source);
|
2021-08-26 15:40:06 +00:00
|
|
|
AddNote("previous definition is here", emplaced.first->second);
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-23 19:01:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 13:24:59 +00:00
|
|
|
auto* param_info =
|
|
|
|
Variable(param, VariableKind::kParameter, parameter_index++);
|
2021-04-16 01:15:43 +00:00
|
|
|
if (!param_info) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : param->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(param->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
|
2021-11-04 22:29:22 +00:00
|
|
|
variable_stack_.Set(param->symbol, param_info);
|
2021-04-30 19:24:29 +00:00
|
|
|
info->parameters.emplace_back(param_info);
|
2021-03-31 15:42:17 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(param->declared_storage_class,
|
|
|
|
param_info->type, param->source)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("while instantiating parameter " +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(param->symbol),
|
|
|
|
param->source);
|
2021-03-31 17:23:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-28 15:00:39 +00:00
|
|
|
if (auto* ptr = param_info->type->As<sem::Pointer>()) {
|
|
|
|
// For MSL, we push module-scope variables into the entry point as pointer
|
|
|
|
// parameters, so we also need to handle their store type.
|
|
|
|
if (!ApplyStorageClassUsageToType(
|
|
|
|
ptr->StorageClass(), const_cast<sem::Type*>(ptr->StoreType()),
|
|
|
|
param->source)) {
|
|
|
|
AddNote("while instantiating parameter " +
|
|
|
|
builder_->Symbols().NameFor(param->symbol),
|
|
|
|
param->source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 17:23:07 +00:00
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* str = param_info->type->As<sem::Struct>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
switch (func->PipelineStage()) {
|
2021-03-31 15:42:17 +00:00
|
|
|
case ast::PipelineStage::kVertex:
|
2021-05-07 14:49:34 +00:00
|
|
|
str->AddUsage(sem::PipelineStageUsage::kVertexInput);
|
2021-03-31 15:42:17 +00:00
|
|
|
break;
|
|
|
|
case ast::PipelineStage::kFragment:
|
2021-05-07 14:49:34 +00:00
|
|
|
str->AddUsage(sem::PipelineStageUsage::kFragmentInput);
|
2021-03-31 15:42:17 +00:00
|
|
|
break;
|
|
|
|
case ast::PipelineStage::kCompute:
|
2021-05-07 14:49:34 +00:00
|
|
|
str->AddUsage(sem::PipelineStageUsage::kComputeInput);
|
2021-03-31 15:42:17 +00:00
|
|
|
break;
|
|
|
|
case ast::PipelineStage::kNone:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* ty = func->return_type) {
|
2021-05-05 09:09:41 +00:00
|
|
|
info->return_type = Type(ty);
|
|
|
|
info->return_type_name = ty->FriendlyName(builder_->Symbols());
|
2021-04-30 19:24:29 +00:00
|
|
|
if (!info->return_type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info->return_type = builder_->create<sem::Void>();
|
2021-05-05 09:09:41 +00:00
|
|
|
info->return_type_name =
|
|
|
|
info->return_type->FriendlyName(builder_->Symbols());
|
2021-04-30 19:24:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* str = info->return_type->As<sem::Struct>()) {
|
2021-03-31 15:44:07 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, str,
|
2021-10-15 17:33:10 +00:00
|
|
|
func->source)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("while instantiating return type for " +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(func->symbol),
|
|
|
|
func->source);
|
2021-03-31 15:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
switch (func->PipelineStage()) {
|
2021-03-31 15:42:17 +00:00
|
|
|
case ast::PipelineStage::kVertex:
|
2021-05-07 14:49:34 +00:00
|
|
|
str->AddUsage(sem::PipelineStageUsage::kVertexOutput);
|
2021-03-31 15:42:17 +00:00
|
|
|
break;
|
|
|
|
case ast::PipelineStage::kFragment:
|
2021-05-07 14:49:34 +00:00
|
|
|
str->AddUsage(sem::PipelineStageUsage::kFragmentOutput);
|
2021-03-31 15:42:17 +00:00
|
|
|
break;
|
|
|
|
case ast::PipelineStage::kCompute:
|
2021-05-07 14:49:34 +00:00
|
|
|
str->AddUsage(sem::PipelineStageUsage::kComputeOutput);
|
2021-03-31 15:42:17 +00:00
|
|
|
break;
|
|
|
|
case ast::PipelineStage::kNone:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-22 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->body) {
|
|
|
|
Mark(func->body);
|
2021-07-14 09:44:41 +00:00
|
|
|
if (current_compound_statement_) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-07-14 09:44:41 +00:00
|
|
|
<< "Resolver::Function() called with a current compound statement";
|
2021-05-15 14:48:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-20 15:17:08 +00:00
|
|
|
auto* sem_block = builder_->create<sem::FunctionBlockStatement>(func);
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Sem().Add(func->body, sem_block);
|
|
|
|
if (!Scope(sem_block, [&] { return Statements(func->body->statements); })) {
|
2021-04-07 08:09:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
2021-11-04 22:29:22 +00:00
|
|
|
variable_stack_.Pop();
|
2020-04-06 21:07:41 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : func->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(func->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : func->return_type_decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(func->return_type_decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
|
2021-05-19 08:15:18 +00:00
|
|
|
// Set work-group size defaults.
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
info->workgroup_size[i].value = 1;
|
|
|
|
info->workgroup_size[i].overridable_const = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* workgroup =
|
2021-10-15 17:33:10 +00:00
|
|
|
ast::GetDecoration<ast::WorkgroupDecoration>(func->decorations)) {
|
|
|
|
auto values = workgroup->Values();
|
2021-07-28 21:24:06 +00:00
|
|
|
auto any_i32 = false;
|
|
|
|
auto any_u32 = false;
|
2021-05-19 13:40:08 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
// Each argument to this decoration can either be a literal, an
|
|
|
|
// identifier for a module-scope constants, or nullptr if not specified.
|
|
|
|
|
2021-07-28 21:24:06 +00:00
|
|
|
auto* expr = values[i];
|
|
|
|
if (!expr) {
|
2021-05-19 13:40:08 +00:00
|
|
|
// Not specified, just use the default.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:24:06 +00:00
|
|
|
if (!Expression(expr)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
2021-07-28 21:24:06 +00:00
|
|
|
constexpr const char* kErrBadType =
|
2021-09-02 10:05:19 +00:00
|
|
|
"workgroup_size argument must be either literal or module-scope "
|
2021-07-28 21:24:06 +00:00
|
|
|
"constant of type i32 or u32";
|
|
|
|
constexpr const char* kErrInconsistentType =
|
2021-09-02 10:05:19 +00:00
|
|
|
"workgroup_size arguments must be of the same type, either i32 "
|
2021-07-28 21:24:06 +00:00
|
|
|
"or u32";
|
|
|
|
|
|
|
|
auto* ty = TypeOf(expr);
|
|
|
|
bool is_i32 = ty->UnwrapRef()->Is<sem::I32>();
|
|
|
|
bool is_u32 = ty->UnwrapRef()->Is<sem::U32>();
|
|
|
|
if (!is_i32 && !is_u32) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(kErrBadType, expr->source);
|
2021-07-28 21:24:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
2021-07-28 21:24:06 +00:00
|
|
|
any_i32 = any_i32 || is_i32;
|
|
|
|
any_u32 = any_u32 || is_u32;
|
|
|
|
if (any_i32 && any_u32) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(kErrInconsistentType, expr->source);
|
2021-07-28 21:24:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* ident = expr->As<ast::IdentifierExpression>()) {
|
|
|
|
// We have an identifier of a module-scope constant.
|
2021-11-04 22:29:22 +00:00
|
|
|
VariableInfo* var = variable_stack_.Get(ident->symbol);
|
|
|
|
if (!var || !(var->declaration->is_const)) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(kErrBadType, expr->source);
|
2021-05-19 13:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Capture the constant if an [[override]] attribute is present.
|
|
|
|
if (ast::HasDecoration<ast::OverrideDecoration>(
|
2021-10-15 17:33:10 +00:00
|
|
|
var->declaration->decorations)) {
|
2021-05-19 13:40:08 +00:00
|
|
|
info->workgroup_size[i].overridable_const = var->declaration;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
expr = var->declaration->constructor;
|
2021-07-28 21:24:06 +00:00
|
|
|
if (!expr) {
|
2021-05-19 13:40:08 +00:00
|
|
|
// No constructor means this value must be overriden by the user.
|
|
|
|
info->workgroup_size[i].value = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-02 10:05:19 +00:00
|
|
|
} else if (!expr->Is<ast::ScalarConstructorExpression>()) {
|
|
|
|
AddError(
|
|
|
|
"workgroup_size argument must be either a literal or a "
|
|
|
|
"module-scope constant",
|
2021-10-15 17:33:10 +00:00
|
|
|
values[i]->source);
|
2021-09-02 10:05:19 +00:00
|
|
|
return false;
|
2021-05-19 13:40:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:24:06 +00:00
|
|
|
auto val = ConstantValueOf(expr);
|
|
|
|
if (!val) {
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "could not resolve constant workgroup_size constant value";
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
// Validate and set the default value for this dimension.
|
2021-07-28 21:24:06 +00:00
|
|
|
if (is_i32 ? val.Elements()[0].i32 < 1 : val.Elements()[0].u32 < 1) {
|
2021-09-02 10:05:19 +00:00
|
|
|
AddError("workgroup_size argument must be at least 1",
|
2021-10-15 17:33:10 +00:00
|
|
|
values[i]->source);
|
2021-05-19 13:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-28 21:24:06 +00:00
|
|
|
|
|
|
|
info->workgroup_size[i].value =
|
|
|
|
is_i32 ? static_cast<uint32_t>(val.Elements()[0].i32)
|
|
|
|
: val.Elements()[0].u32;
|
2021-05-19 13:40:08 +00:00
|
|
|
}
|
2021-05-19 08:15:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 19:24:29 +00:00
|
|
|
if (!ValidateFunction(func, info)) {
|
2021-03-26 12:47:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-24 22:11:34 +00:00
|
|
|
// Register the function information _after_ processing the statements. This
|
|
|
|
// allows us to catch a function calling itself when determining the call
|
|
|
|
// information as this function doesn't exist until it's finished.
|
2021-10-15 17:33:10 +00:00
|
|
|
symbol_to_function_[func->symbol] = info;
|
2021-04-30 19:24:29 +00:00
|
|
|
function_to_info_.emplace(func, info);
|
2021-02-24 22:11:34 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Statements(const ast::StatementList& stmts) {
|
2021-03-09 10:26:57 +00:00
|
|
|
for (auto* stmt : stmts) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(stmt);
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Statement(stmt)) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 17:53:56 +00:00
|
|
|
if (!ValidateStatements(stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateStatements(const ast::StatementList& stmts) {
|
2021-09-09 12:38:19 +00:00
|
|
|
bool unreachable = false;
|
2021-06-21 17:53:56 +00:00
|
|
|
for (auto* stmt : stmts) {
|
2021-09-09 12:38:19 +00:00
|
|
|
if (unreachable) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("code is unreachable", stmt->source);
|
2021-09-09 12:38:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* nested_stmt = stmt;
|
|
|
|
while (auto* block = nested_stmt->As<ast::BlockStatement>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (block->Empty()) {
|
2021-09-09 12:38:19 +00:00
|
|
|
break;
|
2021-06-21 17:53:56 +00:00
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
nested_stmt = block->statements.back();
|
2021-09-09 12:38:19 +00:00
|
|
|
}
|
|
|
|
if (nested_stmt->IsAnyOf<ast::ReturnStatement, ast::BreakStatement,
|
|
|
|
ast::ContinueStatement, ast::DiscardStatement>()) {
|
|
|
|
unreachable = true;
|
2021-06-21 17:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Statement(const ast::Statement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
if (stmt->Is<ast::CaseStatement>()) {
|
|
|
|
AddError("case statement can only be used inside a switch statement",
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->source);
|
2021-07-14 09:44:41 +00:00
|
|
|
return false;
|
2021-05-15 14:48:46 +00:00
|
|
|
}
|
|
|
|
if (stmt->Is<ast::ElseStatement>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-05-15 14:48:46 +00:00
|
|
|
<< "Resolver::Statement() encountered an Else statement. Else "
|
|
|
|
"statements are embedded in If statements, so should never be "
|
|
|
|
"encountered as top-level statements";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-14 09:44:41 +00:00
|
|
|
// Compound statements. These create their own sem::CompoundStatement
|
|
|
|
// bindings.
|
|
|
|
if (auto* b = stmt->As<ast::BlockStatement>()) {
|
|
|
|
return BlockStatement(b);
|
|
|
|
}
|
|
|
|
if (auto* l = stmt->As<ast::ForLoopStatement>()) {
|
|
|
|
return ForLoopStatement(l);
|
|
|
|
}
|
|
|
|
if (auto* l = stmt->As<ast::LoopStatement>()) {
|
|
|
|
return LoopStatement(l);
|
|
|
|
}
|
|
|
|
if (auto* i = stmt->As<ast::IfStatement>()) {
|
|
|
|
return IfStatement(i);
|
|
|
|
}
|
|
|
|
if (auto* s = stmt->As<ast::SwitchStatement>()) {
|
|
|
|
return SwitchStatement(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-Compound statements
|
|
|
|
sem::Statement* sem_statement =
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem_statement);
|
|
|
|
TINT_SCOPED_ASSIGNMENT(current_statement_, sem_statement);
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = stmt->As<ast::AssignmentStatement>()) {
|
2021-03-31 13:26:43 +00:00
|
|
|
return Assignment(a);
|
2020-04-07 12:47:23 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::BreakStatement>()) {
|
2021-07-14 09:44:41 +00:00
|
|
|
if (!sem_statement->FindFirstParent<sem::LoopBlockStatement>() &&
|
|
|
|
!sem_statement->FindFirstParent<sem::SwitchCaseBlockStatement>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("break statement must be in a loop or switch case",
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->source);
|
2021-03-09 15:06:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:10 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = stmt->As<ast::CallStatement>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!Expression(c->expr)) {
|
2021-06-11 13:22:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!ValidateCallStatement(c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-07-21 13:42:13 +00:00
|
|
|
}
|
2021-09-30 17:29:50 +00:00
|
|
|
if (auto* c = stmt->As<ast::ContinueStatement>()) {
|
2021-03-09 10:26:57 +00:00
|
|
|
// Set if we've hit the first continue statement in our parent loop
|
2021-07-17 18:09:26 +00:00
|
|
|
if (auto* block =
|
|
|
|
current_block_->FindFirstParent<
|
|
|
|
sem::LoopBlockStatement, sem::LoopContinuingBlockStatement>()) {
|
|
|
|
if (auto* loop_block = block->As<sem::LoopBlockStatement>()) {
|
2021-09-30 17:29:50 +00:00
|
|
|
if (!loop_block->FirstContinue()) {
|
2021-07-17 18:09:26 +00:00
|
|
|
const_cast<sem::LoopBlockStatement*>(loop_block)
|
2021-09-30 17:29:50 +00:00
|
|
|
->SetFirstContinue(c, loop_block->Decls().size());
|
2021-07-17 18:09:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AddError("continuing blocks must not contain a continue statement",
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->source);
|
2021-07-17 18:09:26 +00:00
|
|
|
return false;
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("continue statement must be in a loop", stmt->source);
|
2021-03-09 10:26:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:29 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::DiscardStatement>()) {
|
2021-07-17 18:09:26 +00:00
|
|
|
if (auto* continuing =
|
|
|
|
sem_statement
|
|
|
|
->FindFirstParent<sem::LoopContinuingBlockStatement>()) {
|
|
|
|
AddError("continuing blocks must not contain a discard statement",
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->source);
|
2021-07-17 18:09:26 +00:00
|
|
|
if (continuing != sem_statement->Parent()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("see continuing block here", continuing->Declaration()->source);
|
2021-07-17 18:09:26 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-25 14:33:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::FallthroughStatement>()) {
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* r = stmt->As<ast::ReturnStatement>()) {
|
2021-03-22 23:20:17 +00:00
|
|
|
return Return(r);
|
2020-04-07 12:56:24 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
|
2021-03-17 22:47:33 +00:00
|
|
|
return VariableDeclStatement(v);
|
2020-04-07 12:57:12 +00:00
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2021-10-14 20:30:29 +00:00
|
|
|
AddError("unknown statement type for type determination: " +
|
|
|
|
std::string(stmt->TypeInfo().name),
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->source);
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::CaseStatement(const ast::CaseStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem = builder_->create<sem::SwitchCaseBlockStatement>(
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->body, current_compound_statement_);
|
2021-07-14 09:44:41 +00:00
|
|
|
builder_->Sem().Add(stmt, sem);
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Sem().Add(stmt->body, sem);
|
|
|
|
Mark(stmt->body);
|
|
|
|
for (auto* sel : stmt->selectors) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(sel);
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
return Scope(sem, [&] { return Statements(stmt->body->statements); });
|
2021-03-09 15:17:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::IfStatement(const ast::IfStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem =
|
|
|
|
builder_->create<sem::IfStatement>(stmt, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem);
|
|
|
|
return Scope(sem, [&] {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!Expression(stmt->condition)) {
|
2021-07-14 09:44:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 15:17:28 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* cond_type = TypeOf(stmt->condition)->UnwrapRef();
|
2021-07-14 09:44:41 +00:00
|
|
|
if (!cond_type->Is<sem::Bool>()) {
|
|
|
|
AddError("if statement condition must be bool, got " +
|
|
|
|
cond_type->FriendlyName(builder_->Symbols()),
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->condition->source);
|
2021-07-14 09:44:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 15:17:28 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(stmt->body);
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* body = builder_->create<sem::BlockStatement>(
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->body, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt->body, body);
|
|
|
|
if (!Scope(body, [&] { return Statements(stmt->body->statements); })) {
|
2021-05-15 14:48:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 15:17:28 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* else_stmt : stmt->else_statements) {
|
2021-07-14 09:44:41 +00:00
|
|
|
Mark(else_stmt);
|
|
|
|
if (!ElseStatement(else_stmt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::ElseStatement(const ast::ElseStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem =
|
|
|
|
builder_->create<sem::ElseStatement>(stmt, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem);
|
|
|
|
return Scope(sem, [&] {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* cond = stmt->condition) {
|
2021-04-19 19:16:12 +00:00
|
|
|
if (!Expression(cond)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-10 13:46:06 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* else_cond_type = TypeOf(cond)->UnwrapRef();
|
2021-05-20 14:22:28 +00:00
|
|
|
if (!else_cond_type->Is<sem::Bool>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("else statement condition must be bool, got " +
|
|
|
|
else_cond_type->FriendlyName(builder_->Symbols()),
|
2021-10-15 17:33:10 +00:00
|
|
|
cond->source);
|
2021-05-10 13:46:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-31 20:35:46 +00:00
|
|
|
}
|
2021-07-14 09:44:41 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(stmt->body);
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* body = builder_->create<sem::BlockStatement>(
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->body, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt->body, body);
|
|
|
|
return Scope(body, [&] { return Statements(stmt->body->statements); });
|
2021-07-14 09:44:41 +00:00
|
|
|
});
|
2021-03-09 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::BlockStatement(const ast::BlockStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem = builder_->create<sem::BlockStatement>(
|
|
|
|
stmt->As<ast::BlockStatement>(), current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem);
|
2021-10-15 17:33:10 +00:00
|
|
|
return Scope(sem, [&] { return Statements(stmt->statements); });
|
2021-07-14 09:44:41 +00:00
|
|
|
}
|
2021-05-15 14:48:46 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::LoopStatement(const ast::LoopStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem =
|
|
|
|
builder_->create<sem::LoopStatement>(stmt, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem);
|
|
|
|
return Scope(sem, [&] {
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(stmt->body);
|
2021-07-14 09:44:41 +00:00
|
|
|
|
|
|
|
auto* body = builder_->create<sem::LoopBlockStatement>(
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->body, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt->body, body);
|
2021-07-14 09:44:41 +00:00
|
|
|
return Scope(body, [&] {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!Statements(stmt->body->statements)) {
|
2021-05-15 14:48:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (stmt->continuing) {
|
|
|
|
Mark(stmt->continuing);
|
|
|
|
if (!stmt->continuing->Empty()) {
|
|
|
|
auto* continuing =
|
|
|
|
builder_->create<sem::LoopContinuingBlockStatement>(
|
|
|
|
stmt->continuing, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt->continuing, continuing);
|
|
|
|
if (!Scope(continuing, [&] {
|
|
|
|
return Statements(stmt->continuing->statements);
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-14 09:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2021-05-15 14:48:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::ForLoopStatement(const ast::ForLoopStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem = builder_->create<sem::ForLoopStatement>(
|
|
|
|
stmt, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem);
|
|
|
|
return Scope(sem, [&] {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* initializer = stmt->initializer) {
|
2021-07-14 09:44:41 +00:00
|
|
|
Mark(initializer);
|
|
|
|
if (!Statement(initializer)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* condition = stmt->condition) {
|
2021-07-14 09:44:41 +00:00
|
|
|
if (!Expression(condition)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
|
2021-07-29 16:55:27 +00:00
|
|
|
if (!TypeOf(condition)->UnwrapRef()->Is<sem::Bool>()) {
|
2021-07-14 09:44:41 +00:00
|
|
|
AddError(
|
|
|
|
"for-loop condition must be bool, got " + TypeNameOf(condition),
|
2021-10-15 17:33:10 +00:00
|
|
|
condition->source);
|
2021-07-14 09:44:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* continuing = stmt->continuing) {
|
2021-07-14 09:44:41 +00:00
|
|
|
Mark(continuing);
|
|
|
|
if (!Statement(continuing)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(stmt->body);
|
2021-07-14 09:44:41 +00:00
|
|
|
|
|
|
|
auto* body = builder_->create<sem::LoopBlockStatement>(
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->body, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt->body, body);
|
|
|
|
return Scope(body, [&] { return Statements(stmt->body->statements); });
|
2021-07-14 09:44:41 +00:00
|
|
|
});
|
2021-07-02 19:27:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Expression(const ast::Expression* root) {
|
|
|
|
std::vector<const ast::Expression*> sorted;
|
2021-10-21 20:38:54 +00:00
|
|
|
if (!ast::TraverseExpressions<ast::TraverseOrder::RightToLeft>(
|
|
|
|
root, diagnostics_, [&](const ast::Expression* expr) {
|
|
|
|
Mark(expr);
|
|
|
|
sorted.emplace_back(expr);
|
|
|
|
return ast::TraverseAction::Descend;
|
|
|
|
})) {
|
2021-09-08 15:18:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* expr : utils::Reverse(sorted)) {
|
|
|
|
bool ok = false;
|
|
|
|
if (auto* array = expr->As<ast::ArrayAccessorExpression>()) {
|
|
|
|
ok = ArrayAccessor(array);
|
|
|
|
} else if (auto* bin_op = expr->As<ast::BinaryExpression>()) {
|
|
|
|
ok = Binary(bin_op);
|
|
|
|
} else if (auto* bitcast = expr->As<ast::BitcastExpression>()) {
|
|
|
|
ok = Bitcast(bitcast);
|
|
|
|
} else if (auto* call = expr->As<ast::CallExpression>()) {
|
|
|
|
ok = Call(call);
|
|
|
|
} else if (auto* ctor = expr->As<ast::ConstructorExpression>()) {
|
|
|
|
ok = Constructor(ctor);
|
|
|
|
} else if (auto* ident = expr->As<ast::IdentifierExpression>()) {
|
|
|
|
ok = Identifier(ident);
|
|
|
|
} else if (auto* member = expr->As<ast::MemberAccessorExpression>()) {
|
|
|
|
ok = MemberAccessor(member);
|
|
|
|
} else if (auto* unary = expr->As<ast::UnaryOpExpression>()) {
|
|
|
|
ok = UnaryOp(unary);
|
2021-10-21 23:04:44 +00:00
|
|
|
} else if (expr->Is<ast::PhonyExpression>()) {
|
|
|
|
ok = true; // No-op
|
2021-09-08 15:18:36 +00:00
|
|
|
} else {
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "unhandled expression type: " << expr->TypeInfo().name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
2021-06-17 19:56:14 +00:00
|
|
|
|
|
|
|
return true;
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::ArrayAccessor(const ast::ArrayAccessorExpression* expr) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* idx = expr->index;
|
|
|
|
auto* res = TypeOf(expr->array);
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* parent_type = res->UnwrapRef();
|
2021-05-07 20:58:34 +00:00
|
|
|
const sem::Type* ret = nullptr;
|
|
|
|
if (auto* arr = parent_type->As<sem::Array>()) {
|
|
|
|
ret = arr->ElemType();
|
2021-04-19 22:51:23 +00:00
|
|
|
} else if (auto* vec = parent_type->As<sem::Vector>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
ret = vec->type();
|
2021-04-19 22:51:23 +00:00
|
|
|
} else if (auto* mat = parent_type->As<sem::Matrix>()) {
|
|
|
|
ret = builder_->create<sem::Vector>(mat->type(), mat->rows());
|
2020-04-07 12:57:42 +00:00
|
|
|
} else {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot index type '" + TypeNameOf(expr->array) + "'",
|
|
|
|
expr->source);
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
2021-06-16 18:50:13 +00:00
|
|
|
if (!TypeOf(idx)->UnwrapRef()->IsAnyOf<sem::I32, sem::U32>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("index must be of type 'i32' or 'u32', found: '" +
|
|
|
|
TypeNameOf(idx) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
idx->source);
|
2021-06-16 18:50:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent_type->Is<sem::Array>() || parent_type->Is<sem::Matrix>()) {
|
|
|
|
if (!res->Is<sem::Reference>()) {
|
|
|
|
// TODO(bclayton): expand this to allow any const_expr expression
|
|
|
|
// https://github.com/gpuweb/gpuweb/issues/1272
|
|
|
|
auto* scalar = idx->As<ast::ScalarConstructorExpression>();
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!scalar || !scalar->literal->As<ast::IntLiteral>()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("index must be signed or unsigned integer literal",
|
2021-10-15 17:33:10 +00:00
|
|
|
idx->source);
|
2021-06-16 18:50:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
// If we're extracting from a reference, we return a reference.
|
|
|
|
if (auto* ref = res->As<sem::Reference>()) {
|
2021-06-04 22:17:37 +00:00
|
|
|
ret = builder_->create<sem::Reference>(ret, ref->StorageClass(),
|
|
|
|
ref->Access());
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Bitcast(const ast::BitcastExpression* expr) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* ty = Type(expr->type);
|
2021-06-03 08:17:44 +00:00
|
|
|
if (!ty) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-16 17:42:13 +00:00
|
|
|
if (ty->Is<sem::Pointer>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot cast to a pointer", expr->source);
|
2021-06-16 17:42:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
SetExprInfo(expr, ty, expr->type->FriendlyName(builder_->Symbols()));
|
2020-04-07 12:57:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Call(const ast::CallExpression* call) {
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(call->func);
|
|
|
|
auto* ident = call->func;
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol);
|
2021-02-03 21:02:25 +00:00
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
auto intrinsic_type = sem::ParseIntrinsicType(name);
|
2021-02-08 22:31:44 +00:00
|
|
|
if (intrinsic_type != IntrinsicType::kNone) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!IntrinsicCall(call, intrinsic_type)) {
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-09 20:17:59 +00:00
|
|
|
if (!FunctionCall(call)) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-18 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-21 08:50:40 +00:00
|
|
|
return ValidateCall(call);
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::ValidateCall(const ast::CallExpression* call) {
|
2021-08-21 08:50:40 +00:00
|
|
|
if (TypeOf(call)->Is<sem::Void>()) {
|
|
|
|
bool is_call_statement = false;
|
|
|
|
if (current_statement_) {
|
|
|
|
if (auto* call_stmt =
|
|
|
|
As<ast::CallStatement>(current_statement_->Declaration())) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (call_stmt->expr == call) {
|
2021-08-21 08:50:40 +00:00
|
|
|
is_call_statement = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_call_statement) {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#function-call-expr
|
|
|
|
// If the called function does not return a value, a function call
|
|
|
|
// statement should be used instead.
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* ident = call->func;
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol);
|
2021-08-21 08:50:40 +00:00
|
|
|
// A function call is made to either a user declared function or an
|
|
|
|
// intrinsic. function_calls_ only maps CallExpression to user declared
|
|
|
|
// functions
|
|
|
|
bool is_function = function_calls_.count(call) != 0;
|
|
|
|
AddError((is_function ? "function" : "intrinsic") + std::string(" '") +
|
|
|
|
name + "' does not return a value",
|
2021-10-15 17:33:10 +00:00
|
|
|
call->source);
|
2021-08-21 08:50:40 +00:00
|
|
|
return false;
|
2021-06-11 13:22:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:50:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:36:04 +00:00
|
|
|
bool Resolver::ValidateCallStatement(const ast::CallStatement*) {
|
2021-06-11 13:22:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::IntrinsicCall(const ast::CallExpression* call,
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::IntrinsicType intrinsic_type) {
|
2021-04-28 12:38:13 +00:00
|
|
|
std::vector<const sem::Type*> arg_tys;
|
2021-10-15 17:33:10 +00:00
|
|
|
arg_tys.reserve(call->args.size());
|
|
|
|
for (auto* expr : call->args) {
|
2021-02-08 22:31:44 +00:00
|
|
|
arg_tys.emplace_back(TypeOf(expr));
|
|
|
|
}
|
|
|
|
|
2021-06-01 19:06:31 +00:00
|
|
|
auto* result =
|
2021-10-15 17:33:10 +00:00
|
|
|
intrinsic_table_->Lookup(intrinsic_type, arg_tys, call->source);
|
2021-06-01 19:06:31 +00:00
|
|
|
if (!result) {
|
2021-02-08 22:42:54 +00:00
|
|
|
return false;
|
2020-07-21 17:44:44 +00:00
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2021-06-10 17:31:54 +00:00
|
|
|
if (result->IsDeprecated()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddWarning("use of deprecated intrinsic", call->source);
|
2021-06-10 17:31:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 13:38:05 +00:00
|
|
|
auto* out = builder_->create<sem::Call>(call, result, current_statement_);
|
|
|
|
builder_->Sem().Add(call, out);
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(call, result->ReturnType());
|
2021-06-03 16:07:34 +00:00
|
|
|
|
|
|
|
current_function_->intrinsic_calls.emplace_back(
|
|
|
|
IntrinsicCallInfo{call, result});
|
|
|
|
|
2021-07-16 13:38:05 +00:00
|
|
|
if (IsTextureIntrinsic(intrinsic_type) &&
|
|
|
|
!ValidateTextureIntrinsicFunction(call, out)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateTextureIntrinsicFunction(
|
|
|
|
const ast::CallExpression* ast_call,
|
|
|
|
const sem::Call* sem_call) {
|
|
|
|
auto* intrinsic = sem_call->Target()->As<sem::Intrinsic>();
|
|
|
|
if (!intrinsic) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string func_name = intrinsic->str();
|
2021-10-14 21:17:29 +00:00
|
|
|
auto& signature = intrinsic->Signature();
|
|
|
|
auto index = signature.IndexOf(sem::ParameterUsage::kOffset);
|
2021-07-16 13:38:05 +00:00
|
|
|
if (index > -1) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* param = ast_call->args[index];
|
2021-07-16 13:38:05 +00:00
|
|
|
if (param->Is<ast::TypeConstructorExpression>()) {
|
|
|
|
auto values = ConstantValueOf(param);
|
|
|
|
if (!values.IsValid()) {
|
2021-07-21 03:34:34 +00:00
|
|
|
AddError(
|
|
|
|
"'" + func_name + "' offset parameter must be a const_expression",
|
2021-10-15 17:33:10 +00:00
|
|
|
param->source);
|
2021-07-16 13:38:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!values.Type()->Is<sem::Vector>() ||
|
|
|
|
!values.ElementType()->Is<sem::I32>()) {
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "failed to resolve '" + func_name + "' offset parameter type";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto offset : values.Elements()) {
|
|
|
|
auto offset_value = offset.i32;
|
|
|
|
if (offset_value < -8 || offset_value > 7) {
|
|
|
|
AddError("each offset component of '" + func_name +
|
|
|
|
"' must be at least -8 and at most 7. "
|
|
|
|
"found: '" +
|
|
|
|
std::to_string(offset_value) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
param->source);
|
2021-07-16 13:38:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-21 03:34:34 +00:00
|
|
|
AddError(
|
|
|
|
"'" + func_name + "' offset parameter must be a const_expression",
|
2021-10-15 17:33:10 +00:00
|
|
|
param->source);
|
2021-07-16 13:38:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
return true;
|
2020-06-01 13:43:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 20:17:59 +00:00
|
|
|
bool Resolver::FunctionCall(const ast::CallExpression* call) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* ident = call->func;
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol);
|
2021-06-09 20:17:59 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto callee_func_it = symbol_to_function_.find(ident->symbol);
|
2021-06-09 20:17:59 +00:00
|
|
|
if (callee_func_it == symbol_to_function_.end()) {
|
|
|
|
if (current_function_ &&
|
2021-10-15 17:33:10 +00:00
|
|
|
current_function_->declaration->symbol == ident->symbol) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError("recursion is not permitted. '" + name +
|
2021-06-24 11:27:36 +00:00
|
|
|
"' attempted to call itself.",
|
2021-10-15 17:33:10 +00:00
|
|
|
call->source);
|
2021-06-09 20:17:59 +00:00
|
|
|
} else {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("unable to find called function: " + name, call->source);
|
2021-06-09 20:17:59 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto* callee_func = callee_func_it->second;
|
|
|
|
|
|
|
|
if (current_function_) {
|
|
|
|
callee_func->callsites.push_back(call);
|
|
|
|
|
|
|
|
// Note: Requires called functions to be resolved first.
|
|
|
|
// This is currently guaranteed as functions must be declared before
|
|
|
|
// use.
|
|
|
|
current_function_->transitive_calls.add(callee_func);
|
|
|
|
for (auto* transitive_call : callee_func->transitive_calls) {
|
|
|
|
current_function_->transitive_calls.add(transitive_call);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
for (auto* var : callee_func->referenced_module_vars) {
|
|
|
|
set_referenced_from_function_if_needed(var, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 18:14:02 +00:00
|
|
|
function_calls_.emplace(call,
|
|
|
|
FunctionCallInfo{callee_func, current_statement_});
|
|
|
|
SetExprInfo(call, callee_func->return_type, callee_func->return_type_name);
|
|
|
|
|
|
|
|
if (!ValidateFunctionCall(call, callee_func)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateFunctionCall(const ast::CallExpression* call,
|
2021-08-31 16:14:46 +00:00
|
|
|
const FunctionInfo* target) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* ident = call->func;
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol);
|
2021-07-20 18:14:02 +00:00
|
|
|
|
2021-08-31 16:14:46 +00:00
|
|
|
if (target->declaration->IsEntryPoint()) {
|
|
|
|
// https://www.w3.org/TR/WGSL/#function-restriction
|
|
|
|
// An entry point must never be the target of a function call.
|
|
|
|
AddError("entry point functions cannot be the target of a function call",
|
2021-10-15 17:33:10 +00:00
|
|
|
call->source);
|
2021-08-31 16:14:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (call->args.size() != target->parameters.size()) {
|
|
|
|
bool more = call->args.size() > target->parameters.size();
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("too " + (more ? std::string("many") : std::string("few")) +
|
|
|
|
" arguments in call to '" + name + "', expected " +
|
2021-08-31 16:14:46 +00:00
|
|
|
std::to_string(target->parameters.size()) + ", got " +
|
2021-10-15 17:33:10 +00:00
|
|
|
std::to_string(call->args.size()),
|
|
|
|
call->source);
|
2021-06-09 20:17:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (size_t i = 0; i < call->args.size(); ++i) {
|
2021-08-31 16:14:46 +00:00
|
|
|
const VariableInfo* param = target->parameters[i];
|
2021-10-15 17:33:10 +00:00
|
|
|
const ast::Expression* arg_expr = call->args[i];
|
2021-06-09 20:17:59 +00:00
|
|
|
auto* arg_type = TypeOf(arg_expr)->UnwrapRef();
|
|
|
|
|
|
|
|
if (param->type != arg_type) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("type mismatch for argument " + std::to_string(i + 1) +
|
|
|
|
" in call to '" + name + "', expected '" +
|
|
|
|
param->type->FriendlyName(builder_->Symbols()) + "', got '" +
|
|
|
|
arg_type->FriendlyName(builder_->Symbols()) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
arg_expr->source);
|
2021-06-09 20:17:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (param->declaration->type->Is<ast::Pointer>()) {
|
2021-07-20 18:14:02 +00:00
|
|
|
auto is_valid = false;
|
|
|
|
if (auto* ident_expr = arg_expr->As<ast::IdentifierExpression>()) {
|
2021-11-04 22:29:22 +00:00
|
|
|
VariableInfo* var = variable_stack_.Get(ident_expr->symbol);
|
|
|
|
if (!var) {
|
2021-07-20 18:14:02 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_) << "failed to resolve identifier";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (var->kind == VariableKind::kParameter) {
|
|
|
|
is_valid = true;
|
|
|
|
}
|
|
|
|
} else if (auto* unary = arg_expr->As<ast::UnaryOpExpression>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (unary->op == ast::UnaryOp::kAddressOf) {
|
2021-07-20 18:14:02 +00:00
|
|
|
if (auto* ident_unary =
|
2021-10-15 17:33:10 +00:00
|
|
|
unary->expr->As<ast::IdentifierExpression>()) {
|
2021-11-04 22:29:22 +00:00
|
|
|
VariableInfo* var = variable_stack_.Get(ident_unary->symbol);
|
|
|
|
if (!var) {
|
2021-07-20 18:14:02 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "failed to resolve identifier";
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (var->declaration->is_const) {
|
2021-07-20 18:14:02 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "Resolver::FunctionCall() encountered an address-of "
|
|
|
|
"expression of a constant identifier expression";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
is_valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:17:59 +00:00
|
|
|
|
2021-09-29 18:56:17 +00:00
|
|
|
if (!is_valid &&
|
|
|
|
IsValidationEnabled(
|
2021-10-15 17:33:10 +00:00
|
|
|
param->declaration->decorations,
|
2021-09-29 18:56:17 +00:00
|
|
|
ast::DisabledValidation::kIgnoreInvalidPointerArgument)) {
|
2021-07-20 18:14:02 +00:00
|
|
|
AddError(
|
|
|
|
"expected an address-of expression of a variable identifier "
|
|
|
|
"expression or a function parameter",
|
2021-10-15 17:33:10 +00:00
|
|
|
arg_expr->source);
|
2021-07-20 18:14:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:17:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Constructor(const ast::ConstructorExpression* expr) {
|
2021-03-15 21:21:33 +00:00
|
|
|
if (auto* type_ctor = expr->As<ast::TypeConstructorExpression>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* type = Type(type_ctor->type);
|
2021-06-03 08:17:44 +00:00
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-15 21:21:33 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto type_name = type_ctor->type->FriendlyName(builder_->Symbols());
|
2021-05-05 09:09:41 +00:00
|
|
|
|
2021-03-15 21:21:33 +00:00
|
|
|
// Now that the argument types have been determined, make sure that they
|
|
|
|
// obey the constructor type rules laid out in
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#type-constructor-expr.
|
2021-07-13 12:18:13 +00:00
|
|
|
bool ok = true;
|
2021-05-05 09:09:41 +00:00
|
|
|
if (auto* vec_type = type->As<sem::Vector>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
ok = ValidateVectorConstructor(type_ctor, vec_type, type_name);
|
|
|
|
} else if (auto* mat_type = type->As<sem::Matrix>()) {
|
|
|
|
ok = ValidateMatrixConstructor(type_ctor, mat_type, type_name);
|
|
|
|
} else if (type->is_scalar()) {
|
|
|
|
ok = ValidateScalarConstructor(type_ctor, type, type_name);
|
|
|
|
} else if (auto* arr_type = type->As<sem::Array>()) {
|
|
|
|
ok = ValidateArrayConstructor(type_ctor, arr_type);
|
|
|
|
} else if (auto* struct_type = type->As<sem::Struct>()) {
|
|
|
|
ok = ValidateStructureConstructor(type_ctor, struct_type);
|
2021-08-13 15:20:42 +00:00
|
|
|
} else {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("type is not constructible", type_ctor->source);
|
2021-08-13 15:20:42 +00:00
|
|
|
return false;
|
2021-07-13 12:18:13 +00:00
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
2021-07-09 16:32:00 +00:00
|
|
|
}
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, type, type_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* scalar_ctor = expr->As<ast::ScalarConstructorExpression>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(scalar_ctor->literal);
|
|
|
|
auto* type = TypeOf(scalar_ctor->literal);
|
2021-06-03 08:17:44 +00:00
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, type);
|
|
|
|
return true;
|
2021-03-15 21:21:33 +00:00
|
|
|
}
|
2021-07-13 12:18:13 +00:00
|
|
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "unexpected constructor expression type";
|
|
|
|
return false;
|
2021-03-15 21:21:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 16:32:00 +00:00
|
|
|
bool Resolver::ValidateStructureConstructor(
|
|
|
|
const ast::TypeConstructorExpression* ctor,
|
|
|
|
const sem::Struct* struct_type) {
|
2021-08-13 15:20:42 +00:00
|
|
|
if (!struct_type->IsConstructible()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("struct constructor has non-constructible type", ctor->source);
|
2021-08-13 15:20:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (ctor->values.size() > 0) {
|
|
|
|
if (ctor->values.size() != struct_type->Members().size()) {
|
|
|
|
std::string fm =
|
|
|
|
ctor->values.size() < struct_type->Members().size() ? "few" : "many";
|
2021-07-09 16:32:00 +00:00
|
|
|
AddError("struct constructor has too " + fm + " inputs: expected " +
|
|
|
|
std::to_string(struct_type->Members().size()) + ", found " +
|
2021-10-15 17:33:10 +00:00
|
|
|
std::to_string(ctor->values.size()),
|
|
|
|
ctor->source);
|
2021-07-09 16:32:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto* member : struct_type->Members()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* value = ctor->values[member->Index()];
|
2021-07-09 16:32:00 +00:00
|
|
|
if (member->Type() != TypeOf(value)->UnwrapRef()) {
|
|
|
|
AddError(
|
|
|
|
"type in struct constructor does not match struct member type: "
|
|
|
|
"expected '" +
|
|
|
|
member->Type()->FriendlyName(builder_->Symbols()) +
|
|
|
|
"', found '" + TypeNameOf(value) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
value->source);
|
2021-07-09 16:32:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-11 15:51:26 +00:00
|
|
|
bool Resolver::ValidateArrayConstructor(
|
|
|
|
const ast::TypeConstructorExpression* ctor,
|
|
|
|
const sem::Array* array_type) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto& values = ctor->values;
|
2021-06-11 15:51:26 +00:00
|
|
|
auto* elem_type = array_type->ElemType();
|
|
|
|
for (auto* value : values) {
|
|
|
|
auto* value_type = TypeOf(value)->UnwrapRef();
|
|
|
|
if (value_type != elem_type) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-06-11 15:51:26 +00:00
|
|
|
"type in array constructor does not match array type: "
|
|
|
|
"expected '" +
|
|
|
|
elem_type->FriendlyName(builder_->Symbols()) + "', found '" +
|
|
|
|
TypeNameOf(value) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
value->source);
|
2021-06-11 15:51:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_type->IsRuntimeSized()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot init a runtime-sized array", ctor->source);
|
2021-06-11 15:51:26 +00:00
|
|
|
return false;
|
2021-08-13 15:20:42 +00:00
|
|
|
} else if (!elem_type->IsConstructible()) {
|
|
|
|
AddError("array constructor has non-constructible element type",
|
2021-10-15 17:33:10 +00:00
|
|
|
ctor->type->As<ast::Array>()->type->source);
|
2021-08-13 15:20:42 +00:00
|
|
|
return false;
|
2021-06-11 15:51:26 +00:00
|
|
|
} else if (!values.empty() && (values.size() != array_type->Count())) {
|
|
|
|
std::string fm = values.size() < array_type->Count() ? "few" : "many";
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("array constructor has too " + fm + " elements: expected " +
|
|
|
|
std::to_string(array_type->Count()) + ", found " +
|
|
|
|
std::to_string(values.size()),
|
2021-10-15 17:33:10 +00:00
|
|
|
ctor->source);
|
2021-06-11 15:51:26 +00:00
|
|
|
return false;
|
|
|
|
} else if (values.size() > array_type->Count()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("array constructor has too many elements: expected " +
|
|
|
|
std::to_string(array_type->Count()) + ", found " +
|
|
|
|
std::to_string(values.size()),
|
2021-10-15 17:33:10 +00:00
|
|
|
ctor->source);
|
2021-06-11 15:51:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-28 13:50:43 +00:00
|
|
|
bool Resolver::ValidateVectorConstructor(
|
|
|
|
const ast::TypeConstructorExpression* ctor,
|
2021-07-13 12:18:13 +00:00
|
|
|
const sem::Vector* vec_type,
|
|
|
|
const std::string& type_name) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto& values = ctor->values;
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* elem_type = vec_type->type();
|
2021-03-15 21:21:33 +00:00
|
|
|
size_t value_cardinality_sum = 0;
|
|
|
|
for (auto* value : values) {
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* value_type = TypeOf(value)->UnwrapRef();
|
2021-03-15 21:21:33 +00:00
|
|
|
if (value_type->is_scalar()) {
|
|
|
|
if (elem_type != value_type) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-03-15 21:21:33 +00:00
|
|
|
"type in vector constructor does not match vector type: "
|
|
|
|
"expected '" +
|
|
|
|
elem_type->FriendlyName(builder_->Symbols()) + "', found '" +
|
2021-04-28 13:50:43 +00:00
|
|
|
TypeNameOf(value) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
value->source);
|
2021-03-15 21:21:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_cardinality_sum++;
|
2021-04-19 22:51:23 +00:00
|
|
|
} else if (auto* value_vec = value_type->As<sem::Vector>()) {
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* value_elem_type = value_vec->type();
|
2021-03-15 21:21:33 +00:00
|
|
|
// A mismatch of vector type parameter T is only an error if multiple
|
|
|
|
// arguments are present. A single argument constructor constitutes a
|
|
|
|
// type conversion expression.
|
2021-07-19 17:35:39 +00:00
|
|
|
if (elem_type != value_elem_type && values.size() > 1u) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-03-15 21:21:33 +00:00
|
|
|
"type in vector constructor does not match vector type: "
|
|
|
|
"expected '" +
|
|
|
|
elem_type->FriendlyName(builder_->Symbols()) + "', found '" +
|
|
|
|
value_elem_type->FriendlyName(builder_->Symbols()) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
value->source);
|
2021-03-15 21:21:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:31:34 +00:00
|
|
|
value_cardinality_sum += value_vec->Width();
|
2021-03-15 21:21:33 +00:00
|
|
|
} else {
|
|
|
|
// A vector constructor can only accept vectors and scalars.
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("expected vector or scalar type in vector constructor; found: " +
|
|
|
|
value_type->FriendlyName(builder_->Symbols()),
|
2021-10-15 17:33:10 +00:00
|
|
|
value->source);
|
2021-03-15 21:21:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:23:31 +00:00
|
|
|
// A correct vector constructor must either be a zero-value expression,
|
|
|
|
// a single-value initializer (splat) expression, or the number of components
|
|
|
|
// of all constructor arguments must add up to the vector cardinality.
|
2021-07-22 18:31:34 +00:00
|
|
|
if (value_cardinality_sum > 1 && value_cardinality_sum != vec_type->Width()) {
|
2021-03-15 21:21:33 +00:00
|
|
|
if (values.empty()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-03-15 21:21:33 +00:00
|
|
|
<< "constructor arguments expected to be non-empty!";
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
const Source& values_start = values[0]->source;
|
|
|
|
const Source& values_end = values[values.size() - 1]->source;
|
2021-07-13 12:18:13 +00:00
|
|
|
AddError("attempted to construct '" + type_name + "' with " +
|
2021-06-24 11:27:36 +00:00
|
|
|
std::to_string(value_cardinality_sum) + " component(s)",
|
|
|
|
Source::Combine(values_start, values_end));
|
2021-03-15 21:21:33 +00:00
|
|
|
return false;
|
2020-04-07 12:46:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:21:35 +00:00
|
|
|
bool Resolver::ValidateVector(const sem::Vector* ty, const Source& source) {
|
|
|
|
if (!ty->type()->is_scalar()) {
|
|
|
|
AddError("vector element type must be 'bool', 'f32', 'i32' or 'u32'",
|
|
|
|
source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateMatrix(const sem::Matrix* ty, const Source& source) {
|
|
|
|
if (!ty->is_float_matrix()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("matrix element type must be 'f32'", source);
|
2021-06-21 17:08:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2021-07-05 20:21:35 +00:00
|
|
|
}
|
2021-06-21 17:08:05 +00:00
|
|
|
|
2021-04-28 13:50:43 +00:00
|
|
|
bool Resolver::ValidateMatrixConstructor(
|
|
|
|
const ast::TypeConstructorExpression* ctor,
|
2021-07-13 12:18:13 +00:00
|
|
|
const sem::Matrix* matrix_type,
|
|
|
|
const std::string& type_name) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto& values = ctor->values;
|
2021-03-18 15:43:14 +00:00
|
|
|
// Zero Value expression
|
|
|
|
if (values.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateMatrix(matrix_type, ctor->source)) {
|
2021-06-21 17:08:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* elem_type = matrix_type->type();
|
2021-10-25 19:20:31 +00:00
|
|
|
auto num_elements = matrix_type->columns() * matrix_type->rows();
|
|
|
|
|
|
|
|
// Print a generic error for an invalid matrix constructor, showing the
|
|
|
|
// available overloads.
|
|
|
|
auto print_error = [&]() {
|
2021-10-15 17:33:10 +00:00
|
|
|
const Source& values_start = values[0]->source;
|
|
|
|
const Source& values_end = values[values.size() - 1]->source;
|
2021-10-25 19:20:31 +00:00
|
|
|
auto elem_type_name = elem_type->FriendlyName(builder_->Symbols());
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "invalid constructor for " + type_name << std::endl << std::endl;
|
|
|
|
ss << "3 candidates available:" << std::endl;
|
|
|
|
ss << " " << type_name << "()" << std::endl;
|
|
|
|
ss << " " << type_name << "(" << elem_type_name << ",...,"
|
|
|
|
<< elem_type_name << ")"
|
|
|
|
<< " // " << std::to_string(num_elements) << " arguments" << std::endl;
|
|
|
|
ss << " " << type_name << "(";
|
|
|
|
for (uint32_t c = 0; c < matrix_type->columns(); c++) {
|
|
|
|
if (c > 0) {
|
|
|
|
ss << ", ";
|
|
|
|
}
|
|
|
|
ss << VectorPretty(matrix_type->rows(), elem_type);
|
|
|
|
}
|
|
|
|
ss << ")" << std::endl;
|
|
|
|
AddError(ss.str(), Source::Combine(values_start, values_end));
|
|
|
|
};
|
|
|
|
|
|
|
|
const sem::Type* expected_arg_type = nullptr;
|
|
|
|
if (num_elements == values.size()) {
|
|
|
|
// Column-major construction from scalar elements.
|
|
|
|
expected_arg_type = matrix_type->type();
|
|
|
|
} else if (matrix_type->columns() == values.size()) {
|
|
|
|
// Column-by-column construction from vectors.
|
|
|
|
expected_arg_type = matrix_type->ColumnType();
|
|
|
|
} else {
|
|
|
|
print_error();
|
2021-03-18 15:43:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* value : values) {
|
2021-10-25 19:20:31 +00:00
|
|
|
if (TypeOf(value)->UnwrapRef() != expected_arg_type) {
|
|
|
|
print_error();
|
2021-03-18 15:43:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-18 15:32:21 +00:00
|
|
|
bool Resolver::ValidateScalarConstructor(
|
|
|
|
const ast::TypeConstructorExpression* ctor,
|
2021-07-13 12:18:13 +00:00
|
|
|
const sem::Type* type,
|
|
|
|
const std::string& type_name) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (ctor->values.size() == 0) {
|
2021-06-18 15:32:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (ctor->values.size() > 1) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("expected zero or one value in constructor, got " +
|
2021-10-15 17:33:10 +00:00
|
|
|
std::to_string(ctor->values.size()),
|
|
|
|
ctor->source);
|
2021-06-18 15:32:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate constructor
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* value = ctor->values[0];
|
2021-06-18 15:32:21 +00:00
|
|
|
auto* value_type = TypeOf(value)->UnwrapRef();
|
|
|
|
|
|
|
|
using Bool = sem::Bool;
|
|
|
|
using I32 = sem::I32;
|
|
|
|
using U32 = sem::U32;
|
|
|
|
using F32 = sem::F32;
|
|
|
|
|
2021-07-19 17:35:39 +00:00
|
|
|
const bool is_valid = (type->Is<Bool>() && value_type->is_scalar()) ||
|
|
|
|
(type->Is<I32>() && value_type->is_scalar()) ||
|
|
|
|
(type->Is<U32>() && value_type->is_scalar()) ||
|
|
|
|
(type->Is<F32>() && value_type->is_scalar());
|
2021-06-18 15:32:21 +00:00
|
|
|
if (!is_valid) {
|
2021-07-13 12:18:13 +00:00
|
|
|
AddError("cannot construct '" + type_name + "' with a value of type '" +
|
|
|
|
TypeNameOf(value) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
ctor->source);
|
2021-06-18 15:32:21 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Identifier(const ast::IdentifierExpression* expr) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto symbol = expr->symbol;
|
2021-11-04 22:29:22 +00:00
|
|
|
if (VariableInfo* var = variable_stack_.Get(symbol)) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, var->type, var->type_name);
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2021-02-16 21:15:01 +00:00
|
|
|
var->users.push_back(expr);
|
2020-12-08 21:07:24 +00:00
|
|
|
set_referenced_from_function_if_needed(var, true);
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-09-30 17:29:50 +00:00
|
|
|
if (current_statement_) {
|
2021-05-18 10:28:48 +00:00
|
|
|
// If identifier is part of a loop continuing block, make sure it
|
|
|
|
// doesn't refer to a variable that is bypassed by a continue statement
|
|
|
|
// in the loop's body block.
|
2021-05-19 21:22:07 +00:00
|
|
|
if (auto* continuing_block =
|
2021-09-30 17:29:50 +00:00
|
|
|
current_statement_
|
2021-05-19 21:22:07 +00:00
|
|
|
->FindFirstParent<sem::LoopContinuingBlockStatement>()) {
|
2021-03-22 17:25:56 +00:00
|
|
|
auto* loop_block =
|
2021-05-19 21:22:07 +00:00
|
|
|
continuing_block->FindFirstParent<sem::LoopBlockStatement>();
|
2021-09-30 17:29:50 +00:00
|
|
|
if (loop_block->FirstContinue()) {
|
2021-05-15 14:48:46 +00:00
|
|
|
auto& decls = loop_block->Decls();
|
2021-03-22 17:25:56 +00:00
|
|
|
// If our identifier is in loop_block->decls, make sure its index is
|
|
|
|
// less than first_continue
|
2021-10-15 17:33:10 +00:00
|
|
|
auto iter =
|
|
|
|
std::find_if(decls.begin(), decls.end(),
|
|
|
|
[&symbol](auto* v) { return v->symbol == symbol; });
|
2021-03-22 17:25:56 +00:00
|
|
|
if (iter != decls.end()) {
|
|
|
|
auto var_decl_index =
|
|
|
|
static_cast<size_t>(std::distance(decls.begin(), iter));
|
2021-09-30 17:29:50 +00:00
|
|
|
if (var_decl_index >= loop_block->NumDeclsAtFirstContinue()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("continue statement bypasses declaration of '" +
|
2021-09-30 17:29:50 +00:00
|
|
|
builder_->Symbols().NameFor(symbol) + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
loop_block->FirstContinue()->source);
|
2021-09-30 17:29:50 +00:00
|
|
|
AddNote("identifier '" + builder_->Symbols().NameFor(symbol) +
|
|
|
|
"' declared here",
|
2021-10-15 17:33:10 +00:00
|
|
|
(*iter)->source);
|
2021-09-30 17:29:50 +00:00
|
|
|
AddNote("identifier '" + builder_->Symbols().NameFor(symbol) +
|
|
|
|
"' referenced in continuing block here",
|
2021-10-15 17:33:10 +00:00
|
|
|
expr->source);
|
2021-03-22 17:25:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-11 22:02:42 +00:00
|
|
|
auto iter = symbol_to_function_.find(symbol);
|
2021-01-11 16:24:32 +00:00
|
|
|
if (iter != symbol_to_function_.end()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("missing '(' for function call", expr->source.End());
|
2021-02-24 13:31:22 +00:00
|
|
|
return false;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
std::string name = builder_->Symbols().NameFor(symbol);
|
2021-04-16 19:07:51 +00:00
|
|
|
if (sem::ParseIntrinsicType(name) != IntrinsicType::kNone) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("missing '(' for intrinsic call", expr->source.End());
|
2021-02-24 13:31:22 +00:00
|
|
|
return false;
|
2020-10-14 18:26:31 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("identifier must be declared before use: " + name, expr->source);
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::MemberAccessor(const ast::MemberAccessorExpression* expr) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* structure = TypeOf(expr->structure);
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* storage_type = structure->UnwrapRef();
|
2020-04-24 00:40:45 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
const sem::Type* ret = nullptr;
|
2021-02-24 14:15:02 +00:00
|
|
|
std::vector<uint32_t> swizzle;
|
2021-02-03 23:55:56 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (auto* str = storage_type->As<sem::Struct>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(expr->member);
|
|
|
|
auto symbol = expr->member->symbol;
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
const sem::StructMember* member = nullptr;
|
2021-05-07 14:49:34 +00:00
|
|
|
for (auto* m : str->Members()) {
|
2021-07-23 16:43:01 +00:00
|
|
|
if (m->Name() == symbol) {
|
2021-05-05 09:09:41 +00:00
|
|
|
ret = m->Type();
|
2021-04-09 13:56:08 +00:00
|
|
|
member = m;
|
2020-04-23 22:26:52 +00:00
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-02-17 20:13:34 +00:00
|
|
|
"struct member " + builder_->Symbols().NameFor(symbol) + " not found",
|
2021-10-15 17:33:10 +00:00
|
|
|
expr->source);
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
// If we're extracting from a reference, we return a reference.
|
|
|
|
if (auto* ref = structure->As<sem::Reference>()) {
|
2021-06-04 22:17:37 +00:00
|
|
|
ret = builder_->create<sem::Reference>(ret, ref->StorageClass(),
|
|
|
|
ref->Access());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2021-04-09 13:56:08 +00:00
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
builder_->Sem().Add(expr, builder_->create<sem::StructMemberAccess>(
|
2021-04-09 13:56:08 +00:00
|
|
|
expr, ret, current_statement_, member));
|
2021-05-18 10:28:48 +00:00
|
|
|
} else if (auto* vec = storage_type->As<sem::Vector>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
Mark(expr->member);
|
|
|
|
std::string s = builder_->Symbols().NameFor(expr->member->symbol);
|
2021-05-07 14:49:34 +00:00
|
|
|
auto size = s.size();
|
|
|
|
swizzle.reserve(s.size());
|
2021-02-24 14:15:02 +00:00
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
for (auto c : s) {
|
2021-02-24 14:15:02 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'x':
|
|
|
|
case 'r':
|
|
|
|
swizzle.emplace_back(0);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
case 'g':
|
|
|
|
swizzle.emplace_back(1);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
case 'b':
|
|
|
|
swizzle.emplace_back(2);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'a':
|
|
|
|
swizzle.emplace_back(3);
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("invalid vector swizzle character",
|
2021-10-15 17:33:10 +00:00
|
|
|
expr->member->source.Begin() + swizzle.size());
|
2021-02-24 14:15:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-01 00:37:40 +00:00
|
|
|
|
2021-07-22 18:31:34 +00:00
|
|
|
if (swizzle.back() >= vec->Width()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("invalid vector swizzle member", expr->member->source);
|
2021-06-01 00:37:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-24 14:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (size < 1 || size > 4) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("invalid vector swizzle size", expr->member->source);
|
2021-02-24 14:15:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 16:24:32 +00:00
|
|
|
|
2021-03-01 20:01:39 +00:00
|
|
|
// All characters are valid, check if they're being mixed
|
|
|
|
auto is_rgba = [](char c) {
|
|
|
|
return c == 'r' || c == 'g' || c == 'b' || c == 'a';
|
|
|
|
};
|
|
|
|
auto is_xyzw = [](char c) {
|
|
|
|
return c == 'x' || c == 'y' || c == 'z' || c == 'w';
|
|
|
|
};
|
2021-05-07 14:49:34 +00:00
|
|
|
if (!std::all_of(s.begin(), s.end(), is_rgba) &&
|
|
|
|
!std::all_of(s.begin(), s.end(), is_xyzw)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("invalid mixing of vector swizzle characters rgba with xyzw",
|
2021-10-15 17:33:10 +00:00
|
|
|
expr->member->source);
|
2021-03-01 20:01:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:05:34 +00:00
|
|
|
if (size == 1) {
|
|
|
|
// A single element swizzle is just the type of the vector.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = vec->type();
|
2021-05-18 10:28:48 +00:00
|
|
|
// If we're extracting from a reference, we return a reference.
|
|
|
|
if (auto* ref = structure->As<sem::Reference>()) {
|
2021-06-04 22:17:37 +00:00
|
|
|
ret = builder_->create<sem::Reference>(ret, ref->StorageClass(),
|
|
|
|
ref->Access());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
2021-02-24 14:15:02 +00:00
|
|
|
// The vector will have a number of components equal to the length of
|
2021-04-08 14:08:47 +00:00
|
|
|
// the swizzle.
|
2021-04-19 22:51:23 +00:00
|
|
|
ret = builder_->create<sem::Vector>(vec->type(),
|
|
|
|
static_cast<uint32_t>(size));
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2021-04-09 13:56:08 +00:00
|
|
|
builder_->Sem().Add(
|
2021-04-16 19:07:51 +00:00
|
|
|
expr, builder_->create<sem::Swizzle>(expr, ret, current_statement_,
|
|
|
|
std::move(swizzle)));
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2021-09-13 19:30:19 +00:00
|
|
|
AddError(
|
|
|
|
"invalid member accessor expression. Expected vector or struct, got '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
TypeNameOf(expr->structure) + "'",
|
|
|
|
expr->structure->source);
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Binary(const ast::BinaryExpression* expr) {
|
2021-04-19 22:51:23 +00:00
|
|
|
using Bool = sem::Bool;
|
|
|
|
using F32 = sem::F32;
|
|
|
|
using I32 = sem::I32;
|
|
|
|
using U32 = sem::U32;
|
|
|
|
using Matrix = sem::Matrix;
|
|
|
|
using Vector = sem::Vector;
|
2021-03-16 13:26:03 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef();
|
|
|
|
auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef();
|
2021-03-16 13:26:03 +00:00
|
|
|
|
|
|
|
auto* lhs_vec = lhs_type->As<Vector>();
|
2021-04-13 13:32:33 +00:00
|
|
|
auto* lhs_vec_elem_type = lhs_vec ? lhs_vec->type() : nullptr;
|
2021-03-16 13:26:03 +00:00
|
|
|
auto* rhs_vec = rhs_type->As<Vector>();
|
2021-04-13 13:32:33 +00:00
|
|
|
auto* rhs_vec_elem_type = rhs_vec ? rhs_vec->type() : nullptr;
|
2021-03-16 13:26:03 +00:00
|
|
|
|
2021-04-01 19:58:37 +00:00
|
|
|
const bool matching_vec_elem_types =
|
|
|
|
lhs_vec_elem_type && rhs_vec_elem_type &&
|
|
|
|
(lhs_vec_elem_type == rhs_vec_elem_type) &&
|
2021-07-22 18:31:34 +00:00
|
|
|
(lhs_vec->Width() == rhs_vec->Width());
|
2021-03-16 13:26:03 +00:00
|
|
|
|
2021-04-01 14:59:27 +00:00
|
|
|
const bool matching_types = matching_vec_elem_types || (lhs_type == rhs_type);
|
|
|
|
|
2021-03-16 13:26:03 +00:00
|
|
|
// Binary logical expressions
|
|
|
|
if (expr->IsLogicalAnd() || expr->IsLogicalOr()) {
|
|
|
|
if (matching_types && lhs_type->Is<Bool>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (expr->IsOr() || expr->IsAnd()) {
|
|
|
|
if (matching_types && lhs_type->Is<Bool>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (matching_types && lhs_vec_elem_type && lhs_vec_elem_type->Is<Bool>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arithmetic expressions
|
|
|
|
if (expr->IsArithmetic()) {
|
|
|
|
// Binary arithmetic expressions over scalars
|
2021-05-27 18:25:06 +00:00
|
|
|
if (matching_types && lhs_type->is_numeric_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary arithmetic expressions over vectors
|
|
|
|
if (matching_types && lhs_vec_elem_type &&
|
2021-05-27 18:25:06 +00:00
|
|
|
lhs_vec_elem_type->is_numeric_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-27 21:07:56 +00:00
|
|
|
// Binary arithmetic expressions with mixed scalar and vector operands
|
|
|
|
if (lhs_vec_elem_type && (lhs_vec_elem_type == rhs_type)) {
|
|
|
|
if (expr->IsModulo()) {
|
|
|
|
if (rhs_type->is_integer_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-05-27 21:07:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (rhs_type->is_numeric_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-05-27 21:07:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-16 13:26:03 +00:00
|
|
|
}
|
2021-05-27 21:07:56 +00:00
|
|
|
if (rhs_vec_elem_type && (rhs_vec_elem_type == lhs_type)) {
|
|
|
|
if (expr->IsModulo()) {
|
|
|
|
if (lhs_type->is_integer_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, rhs_type);
|
2021-05-27 21:07:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (lhs_type->is_numeric_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, rhs_type);
|
2021-05-27 21:07:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-16 13:26:03 +00:00
|
|
|
}
|
2021-05-27 21:07:56 +00:00
|
|
|
}
|
2021-03-16 13:26:03 +00:00
|
|
|
|
2021-05-27 21:07:56 +00:00
|
|
|
// Matrix arithmetic
|
2021-05-31 15:53:20 +00:00
|
|
|
auto* lhs_mat = lhs_type->As<Matrix>();
|
|
|
|
auto* lhs_mat_elem_type = lhs_mat ? lhs_mat->type() : nullptr;
|
|
|
|
auto* rhs_mat = rhs_type->As<Matrix>();
|
|
|
|
auto* rhs_mat_elem_type = rhs_mat ? rhs_mat->type() : nullptr;
|
|
|
|
// Addition and subtraction of float matrices
|
|
|
|
if ((expr->IsAdd() || expr->IsSubtract()) && lhs_mat_elem_type &&
|
|
|
|
lhs_mat_elem_type->Is<F32>() && rhs_mat_elem_type &&
|
|
|
|
rhs_mat_elem_type->Is<F32>() &&
|
|
|
|
(lhs_mat->columns() == rhs_mat->columns()) &&
|
|
|
|
(lhs_mat->rows() == rhs_mat->rows())) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, rhs_type);
|
2021-05-31 15:53:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-27 21:07:56 +00:00
|
|
|
if (expr->IsMultiply()) {
|
2021-03-16 13:26:03 +00:00
|
|
|
// Multiplication of a matrix and a scalar
|
|
|
|
if (lhs_type->Is<F32>() && rhs_mat_elem_type &&
|
|
|
|
rhs_mat_elem_type->Is<F32>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, rhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() &&
|
|
|
|
rhs_type->Is<F32>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vector times matrix
|
|
|
|
if (lhs_vec_elem_type && lhs_vec_elem_type->Is<F32>() &&
|
2021-04-01 19:58:37 +00:00
|
|
|
rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>() &&
|
2021-07-22 18:31:34 +00:00
|
|
|
(lhs_vec->Width() == rhs_mat->rows())) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Vector>(lhs_vec->type(),
|
|
|
|
rhs_mat->columns()));
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix times vector
|
|
|
|
if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() &&
|
2021-04-01 19:58:37 +00:00
|
|
|
rhs_vec_elem_type && rhs_vec_elem_type->Is<F32>() &&
|
2021-07-22 18:31:34 +00:00
|
|
|
(lhs_mat->columns() == rhs_vec->Width())) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Vector>(rhs_vec->type(),
|
|
|
|
lhs_mat->rows()));
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix times matrix
|
|
|
|
if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() &&
|
2021-04-01 19:58:37 +00:00
|
|
|
rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>() &&
|
|
|
|
(lhs_mat->columns() == rhs_mat->rows())) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Matrix>(
|
|
|
|
builder_->create<sem::Vector>(lhs_mat_elem_type,
|
|
|
|
lhs_mat->rows()),
|
|
|
|
rhs_mat->columns()));
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison expressions
|
|
|
|
if (expr->IsComparison()) {
|
|
|
|
if (matching_types) {
|
|
|
|
// Special case for bools: only == and !=
|
|
|
|
if (lhs_type->Is<Bool>() && (expr->IsEqual() || expr->IsNotEqual())) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Bool>());
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the rest, we can compare i32, u32, and f32
|
|
|
|
if (lhs_type->IsAnyOf<I32, U32, F32>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Bool>());
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for vectors
|
|
|
|
if (matching_vec_elem_types) {
|
|
|
|
if (lhs_vec_elem_type->Is<Bool>() &&
|
|
|
|
(expr->IsEqual() || expr->IsNotEqual())) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Vector>(
|
2021-07-22 18:31:34 +00:00
|
|
|
builder_->create<sem::Bool>(), lhs_vec->Width()));
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-27 18:25:06 +00:00
|
|
|
if (lhs_vec_elem_type->is_numeric_scalar()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, builder_->create<sem::Vector>(
|
2021-07-22 18:31:34 +00:00
|
|
|
builder_->create<sem::Bool>(), lhs_vec->Width()));
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary bitwise operations
|
|
|
|
if (expr->IsBitwise()) {
|
2021-06-01 17:42:41 +00:00
|
|
|
if (matching_types && lhs_type->is_integer_scalar_or_vector()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bit shift expressions
|
|
|
|
if (expr->IsBitshift()) {
|
|
|
|
// Type validation rules are the same for left or right shift, despite
|
|
|
|
// differences in computation rules (i.e. right shift can be arithmetic or
|
|
|
|
// logical depending on lhs type).
|
|
|
|
|
|
|
|
if (lhs_type->IsAnyOf<I32, U32>() && rhs_type->Is<U32>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs_vec_elem_type && lhs_vec_elem_type->IsAnyOf<I32, U32>() &&
|
|
|
|
rhs_vec_elem_type && rhs_vec_elem_type->Is<U32>()) {
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(expr, lhs_type);
|
2021-03-16 13:26:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("Binary expression operand types are invalid for this operation: " +
|
|
|
|
lhs_type->FriendlyName(builder_->Symbols()) + " " +
|
2021-10-15 17:33:10 +00:00
|
|
|
FriendlyName(expr->op) + " " +
|
2021-06-24 11:27:36 +00:00
|
|
|
rhs_type->FriendlyName(builder_->Symbols()),
|
2021-10-15 17:33:10 +00:00
|
|
|
expr->source);
|
2021-03-16 13:26:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::UnaryOp(const ast::UnaryOpExpression* unary) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* expr_type = TypeOf(unary->expr);
|
2021-05-18 10:28:48 +00:00
|
|
|
if (!expr_type) {
|
2020-04-07 19:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
std::string type_name;
|
|
|
|
const sem::Type* type = nullptr;
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
switch (unary->op) {
|
2021-06-28 14:09:33 +00:00
|
|
|
case ast::UnaryOp::kNot:
|
|
|
|
// Result type matches the deref'd inner type.
|
2021-10-15 17:33:10 +00:00
|
|
|
type_name = TypeNameOf(unary->expr);
|
2021-06-28 14:09:33 +00:00
|
|
|
type = expr_type->UnwrapRef();
|
|
|
|
if (!type->Is<sem::Bool>() && !type->is_bool_vector()) {
|
|
|
|
AddError("cannot logical negate expression of type '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
TypeNameOf(unary->expr),
|
|
|
|
unary->expr->source);
|
2021-06-28 14:09:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-06-17 08:35:54 +00:00
|
|
|
case ast::UnaryOp::kComplement:
|
2021-06-28 14:09:33 +00:00
|
|
|
// Result type matches the deref'd inner type.
|
2021-10-15 17:33:10 +00:00
|
|
|
type_name = TypeNameOf(unary->expr);
|
2021-06-28 14:09:33 +00:00
|
|
|
type = expr_type->UnwrapRef();
|
|
|
|
if (!type->is_integer_scalar_or_vector()) {
|
|
|
|
AddError("cannot bitwise complement expression of type '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
TypeNameOf(unary->expr),
|
|
|
|
unary->expr->source);
|
2021-06-28 14:09:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
case ast::UnaryOp::kNegation:
|
|
|
|
// Result type matches the deref'd inner type.
|
2021-10-15 17:33:10 +00:00
|
|
|
type_name = TypeNameOf(unary->expr);
|
2021-05-18 10:28:48 +00:00
|
|
|
type = expr_type->UnwrapRef();
|
2021-06-28 14:09:33 +00:00
|
|
|
if (!(type->IsAnyOf<sem::F32, sem::I32>() ||
|
|
|
|
type->is_signed_integer_vector() || type->is_float_vector())) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot negate expression of type '" + TypeNameOf(unary->expr),
|
|
|
|
unary->expr->source);
|
2021-06-28 14:09:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-18 10:28:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ast::UnaryOp::kAddressOf:
|
|
|
|
if (auto* ref = expr_type->As<sem::Reference>()) {
|
2021-07-28 03:57:22 +00:00
|
|
|
if (ref->StoreType()->UnwrapRef()->is_handle()) {
|
|
|
|
AddError(
|
|
|
|
"cannot take the address of expression in handle storage class",
|
2021-10-15 17:33:10 +00:00
|
|
|
unary->expr->source);
|
2021-07-28 03:57:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-11-04 19:55:57 +00:00
|
|
|
|
|
|
|
auto* array = unary->expr->As<ast::ArrayAccessorExpression>();
|
|
|
|
auto* member = unary->expr->As<ast::MemberAccessorExpression>();
|
|
|
|
if ((array && TypeOf(array->array)->UnwrapRef()->Is<sem::Vector>()) ||
|
|
|
|
(member &&
|
|
|
|
TypeOf(member->structure)->UnwrapRef()->Is<sem::Vector>())) {
|
|
|
|
AddError("cannot take the address of a vector component",
|
|
|
|
unary->expr->source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-04 22:17:37 +00:00
|
|
|
type = builder_->create<sem::Pointer>(
|
|
|
|
ref->StoreType(), ref->StorageClass(), ref->Access());
|
2021-05-18 10:28:48 +00:00
|
|
|
} else {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot take the address of expression", unary->expr->source);
|
2021-05-18 10:28:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ast::UnaryOp::kIndirection:
|
|
|
|
if (auto* ptr = expr_type->As<sem::Pointer>()) {
|
2021-06-04 22:17:37 +00:00
|
|
|
type = builder_->create<sem::Reference>(
|
|
|
|
ptr->StoreType(), ptr->StorageClass(), ptr->Access());
|
2021-05-18 10:28:48 +00:00
|
|
|
} else {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("cannot dereference expression of type '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
TypeNameOf(unary->expr) + "'",
|
|
|
|
unary->expr->source);
|
2021-05-18 10:28:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:18:13 +00:00
|
|
|
SetExprInfo(unary, type);
|
2020-04-07 19:27:21 +00:00
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) {
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::Variable* var = stmt->variable;
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(var);
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDefinition(var->symbol, var->source)) {
|
2021-04-06 14:07:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* info = Variable(var, VariableKind::kLocal);
|
2021-05-12 10:41:21 +00:00
|
|
|
if (!info) {
|
|
|
|
return false;
|
2021-02-26 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : var->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
2021-08-17 16:09:00 +00:00
|
|
|
if (!deco->Is<ast::InternalDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decorations are not valid on local variables", deco->source);
|
2021-08-17 16:09:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:29:22 +00:00
|
|
|
variable_stack_.Set(var->symbol, info);
|
2021-07-14 09:44:41 +00:00
|
|
|
if (current_block_) { // Not all statements are inside a block
|
|
|
|
current_block_->AddDecl(var);
|
|
|
|
}
|
2021-03-17 22:47:33 +00:00
|
|
|
|
2021-05-10 18:01:51 +00:00
|
|
|
if (!ValidateVariable(info)) {
|
2021-04-06 14:07:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!var->is_const &&
|
|
|
|
IsValidationEnabled(var->decorations,
|
2021-07-28 22:43:36 +00:00
|
|
|
ast::DisabledValidation::kIgnoreStorageClass)) {
|
|
|
|
if (!info->type->UnwrapRef()->IsConstructible()) {
|
|
|
|
AddError("function variable must have a constructible type",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->type ? var->type->source : var->source);
|
2021-07-28 22:43:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (info->storage_class != ast::StorageClass::kFunction) {
|
2021-03-17 22:47:33 +00:00
|
|
|
if (info->storage_class != ast::StorageClass::kNone) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("function variable has a non-function storage class",
|
2021-10-15 17:33:10 +00:00
|
|
|
stmt->source);
|
2021-03-17 22:47:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info->storage_class = ast::StorageClass::kFunction;
|
2021-02-26 18:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 12:47:58 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(info->storage_class, info->type,
|
2021-10-15 17:33:10 +00:00
|
|
|
var->source)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("while instantiating variable " +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(var->symbol),
|
|
|
|
var->source);
|
2021-03-17 22:47:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:25:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
sem::Type* Resolver::TypeDecl(const ast::TypeDecl* named_type) {
|
2021-05-20 08:44:57 +00:00
|
|
|
sem::Type* result = nullptr;
|
|
|
|
if (auto* alias = named_type->As<ast::Alias>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
result = Type(alias->type);
|
2021-05-20 08:44:57 +00:00
|
|
|
} else if (auto* str = named_type->As<ast::Struct>()) {
|
|
|
|
result = Structure(str);
|
|
|
|
} else {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_) << "Unhandled TypeDecl";
|
2021-05-20 08:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
named_type_info_.emplace(named_type->name, TypeDeclInfo{named_type, result});
|
2021-05-20 08:44:57 +00:00
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
if (!ValidateTypeDecl(named_type)) {
|
2021-05-20 08:44:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder_->Sem().Add(named_type, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:32:14 +00:00
|
|
|
bool Resolver::ValidateTypeDecl(const ast::TypeDecl* named_type) const {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto iter = named_type_info_.find(named_type->name);
|
2021-05-20 08:44:57 +00:00
|
|
|
if (iter == named_type_info_.end()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "ValidateTypeDecl called() before TypeDecl()";
|
2021-05-20 08:44:57 +00:00
|
|
|
}
|
|
|
|
if (iter->second.ast != named_type) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("type with the name '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(named_type->name) +
|
2021-06-24 11:27:36 +00:00
|
|
|
"' was already declared",
|
2021-10-15 17:33:10 +00:00
|
|
|
named_type->source);
|
|
|
|
AddNote("first declared here", iter->second.ast->source);
|
2021-05-20 08:44:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* Resolver::TypeOf(const ast::Expression* expr) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto it = expr_info_.find(expr);
|
|
|
|
if (it != expr_info_.end()) {
|
2021-05-20 15:10:48 +00:00
|
|
|
return const_cast<sem::Type*>(it->second.type);
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-28 13:50:43 +00:00
|
|
|
std::string Resolver::TypeNameOf(const ast::Expression* expr) {
|
|
|
|
auto it = expr_info_.find(expr);
|
|
|
|
if (it != expr_info_.end()) {
|
|
|
|
return it->second.type_name;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* Resolver::TypeOf(const ast::Literal* lit) {
|
2021-04-28 13:50:43 +00:00
|
|
|
if (lit->Is<ast::SintLiteral>()) {
|
|
|
|
return builder_->create<sem::I32>();
|
|
|
|
}
|
|
|
|
if (lit->Is<ast::UintLiteral>()) {
|
|
|
|
return builder_->create<sem::U32>();
|
|
|
|
}
|
|
|
|
if (lit->Is<ast::FloatLiteral>()) {
|
|
|
|
return builder_->create<sem::F32>();
|
|
|
|
}
|
|
|
|
if (lit->Is<ast::BoolLiteral>()) {
|
|
|
|
return builder_->create<sem::Bool>();
|
|
|
|
}
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_)
|
2021-04-28 13:50:43 +00:00
|
|
|
<< "Unhandled literal type: " << lit->TypeInfo().name;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-07-13 12:18:13 +00:00
|
|
|
void Resolver::SetExprInfo(const ast::Expression* expr,
|
|
|
|
const sem::Type* type,
|
|
|
|
std::string type_name) {
|
2021-03-31 20:43:26 +00:00
|
|
|
if (expr_info_.count(expr)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-07-13 12:18:13 +00:00
|
|
|
<< "SetExprInfo() called twice for the same expression";
|
|
|
|
}
|
|
|
|
if (type_name.empty()) {
|
|
|
|
type_name = type->FriendlyName(builder_->Symbols());
|
2021-03-31 20:43:26 +00:00
|
|
|
}
|
2021-07-13 12:18:13 +00:00
|
|
|
auto constant_value = EvaluateConstantValue(expr, type);
|
|
|
|
expr_info_.emplace(
|
|
|
|
expr, ExpressionInfo{type, std::move(type_name), current_statement_,
|
|
|
|
std::move(constant_value)});
|
2021-01-29 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
bool Resolver::ValidatePipelineStages() {
|
2021-06-24 08:56:16 +00:00
|
|
|
auto check_workgroup_storage = [&](FunctionInfo* func,
|
|
|
|
FunctionInfo* entry_point) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto stage = entry_point->declaration->PipelineStage();
|
2021-06-24 08:56:16 +00:00
|
|
|
if (stage != ast::PipelineStage::kCompute) {
|
|
|
|
for (auto* var : func->local_referenced_module_vars) {
|
|
|
|
if (var->storage_class == ast::StorageClass::kWorkgroup) {
|
|
|
|
std::stringstream stage_name;
|
|
|
|
stage_name << stage;
|
|
|
|
for (auto* user : var->users) {
|
|
|
|
auto it = expr_info_.find(user->As<ast::Expression>());
|
|
|
|
if (it != expr_info_.end()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (func->declaration->symbol ==
|
|
|
|
it->second.statement->Function()->symbol) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("workgroup memory cannot be used by " +
|
|
|
|
stage_name.str() + " pipeline stage",
|
2021-10-15 17:33:10 +00:00
|
|
|
user->source);
|
2021-06-24 08:56:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("variable is declared here", var->declaration->source);
|
2021-06-24 08:56:16 +00:00
|
|
|
if (func != entry_point) {
|
|
|
|
TraverseCallChain(entry_point, func, [&](FunctionInfo* f) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("called by function '" +
|
|
|
|
builder_->Symbols().NameFor(f->declaration->symbol) +
|
|
|
|
"'",
|
|
|
|
f->declaration->source);
|
2021-06-24 08:56:16 +00:00
|
|
|
});
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("called by entry point '" +
|
|
|
|
builder_->Symbols().NameFor(
|
2021-10-15 17:33:10 +00:00
|
|
|
entry_point->declaration->symbol) +
|
2021-06-24 11:27:36 +00:00
|
|
|
"'",
|
2021-10-15 17:33:10 +00:00
|
|
|
entry_point->declaration->source);
|
2021-06-24 08:56:16 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto* entry_point : entry_points_) {
|
|
|
|
if (!check_workgroup_storage(entry_point, entry_point)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto* func : entry_point->transitive_calls) {
|
|
|
|
if (!check_workgroup_storage(func, entry_point)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 16:07:34 +00:00
|
|
|
auto check_intrinsic_calls = [&](FunctionInfo* func,
|
|
|
|
FunctionInfo* entry_point) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto stage = entry_point->declaration->PipelineStage();
|
2021-06-03 16:07:34 +00:00
|
|
|
for (auto& call : func->intrinsic_calls) {
|
|
|
|
if (!call.intrinsic->SupportedStages().Contains(stage)) {
|
|
|
|
std::stringstream err;
|
|
|
|
err << "built-in cannot be used by " << stage << " pipeline stage";
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(err.str(), call.call->source);
|
2021-06-03 16:07:34 +00:00
|
|
|
if (func != entry_point) {
|
|
|
|
TraverseCallChain(entry_point, func, [&](FunctionInfo* f) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("called by function '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(f->declaration->symbol) +
|
2021-06-24 11:27:36 +00:00
|
|
|
"'",
|
2021-10-15 17:33:10 +00:00
|
|
|
f->declaration->source);
|
2021-06-03 16:07:34 +00:00
|
|
|
});
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("called by entry point '" +
|
|
|
|
builder_->Symbols().NameFor(
|
2021-10-15 17:33:10 +00:00
|
|
|
entry_point->declaration->symbol) +
|
2021-06-24 11:27:36 +00:00
|
|
|
"'",
|
2021-10-15 17:33:10 +00:00
|
|
|
entry_point->declaration->source);
|
2021-06-03 16:07:34 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto* entry_point : entry_points_) {
|
|
|
|
if (!check_intrinsic_calls(entry_point, entry_point)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto* func : entry_point->transitive_calls) {
|
|
|
|
if (!check_intrinsic_calls(func, entry_point)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CALLBACK>
|
|
|
|
void Resolver::TraverseCallChain(FunctionInfo* from,
|
|
|
|
FunctionInfo* to,
|
|
|
|
CALLBACK&& callback) const {
|
|
|
|
for (auto* f : from->transitive_calls) {
|
|
|
|
if (f == to) {
|
|
|
|
callback(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (f->transitive_calls.contains(to)) {
|
|
|
|
TraverseCallChain(f, to, callback);
|
|
|
|
callback(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-06-03 16:07:34 +00:00
|
|
|
<< "TraverseCallChain() 'from' does not transitively call 'to'";
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::CreateSemanticNodes() const {
|
2021-02-03 17:51:09 +00:00
|
|
|
auto& sem = builder_->Sem();
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
// Collate all the 'ancestor_entry_points' - this is a map of function
|
|
|
|
// symbol to all the entry points that transitively call the function.
|
2021-03-03 19:54:44 +00:00
|
|
|
std::unordered_map<Symbol, std::vector<Symbol>> ancestor_entry_points;
|
2021-06-03 16:07:34 +00:00
|
|
|
for (auto* entry_point : entry_points_) {
|
|
|
|
for (auto* call : entry_point->transitive_calls) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto& vec = ancestor_entry_points[call->declaration->symbol];
|
|
|
|
vec.emplace_back(entry_point->declaration->symbol);
|
2021-03-03 19:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Variables
|
2021-07-22 13:24:59 +00:00
|
|
|
std::unordered_map<const tint::ast::Variable*, sem::Parameter*> sem_params;
|
2021-02-03 17:51:09 +00:00
|
|
|
for (auto it : variable_to_info_) {
|
|
|
|
auto* var = it.first;
|
|
|
|
auto* info = it.second;
|
2021-05-13 20:32:32 +00:00
|
|
|
|
|
|
|
sem::Variable* sem_var = nullptr;
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (ast::HasDecoration<ast::OverrideDecoration>(var->decorations)) {
|
2021-05-13 20:32:32 +00:00
|
|
|
// Create a pipeline overridable constant.
|
2021-09-30 17:29:50 +00:00
|
|
|
sem_var = builder_->create<sem::GlobalVariable>(var, info->type,
|
|
|
|
info->constant_id);
|
2021-07-22 13:24:59 +00:00
|
|
|
} else {
|
|
|
|
switch (info->kind) {
|
|
|
|
case VariableKind::kGlobal:
|
|
|
|
sem_var = builder_->create<sem::GlobalVariable>(
|
|
|
|
var, info->type, info->storage_class, info->access,
|
|
|
|
info->binding_point);
|
|
|
|
break;
|
|
|
|
case VariableKind::kLocal:
|
|
|
|
sem_var = builder_->create<sem::LocalVariable>(
|
|
|
|
var, info->type, info->storage_class, info->access);
|
|
|
|
break;
|
2021-07-22 13:24:59 +00:00
|
|
|
case VariableKind::kParameter: {
|
|
|
|
auto* param = builder_->create<sem::Parameter>(
|
|
|
|
var, info->index, info->type, info->storage_class, info->access);
|
|
|
|
sem_var = param;
|
|
|
|
sem_params.emplace(var, param);
|
2021-07-22 13:24:59 +00:00
|
|
|
break;
|
2021-07-22 13:24:59 +00:00
|
|
|
}
|
2021-07-22 13:24:59 +00:00
|
|
|
}
|
2021-05-13 20:32:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
std::vector<const sem::VariableUser*> users;
|
2021-02-16 21:15:01 +00:00
|
|
|
for (auto* user : info->users) {
|
|
|
|
// Create semantic node for the identifier expression if necessary
|
|
|
|
auto* sem_expr = sem.Get(user);
|
|
|
|
if (sem_expr == nullptr) {
|
2021-07-13 12:18:13 +00:00
|
|
|
auto& expr_info = expr_info_.at(user);
|
|
|
|
auto* type = expr_info.type;
|
|
|
|
auto* stmt = expr_info.statement;
|
|
|
|
auto* sem_user = builder_->create<sem::VariableUser>(
|
|
|
|
user, type, stmt, sem_var, expr_info.constant_value);
|
2021-04-07 08:09:01 +00:00
|
|
|
sem_var->AddUser(sem_user);
|
|
|
|
sem.Add(user, sem_user);
|
|
|
|
} else {
|
2021-04-16 19:07:51 +00:00
|
|
|
auto* sem_user = sem_expr->As<sem::VariableUser>();
|
2021-04-07 08:09:01 +00:00
|
|
|
if (!sem_user) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_) << "expected sem::VariableUser, got "
|
|
|
|
<< sem_expr->TypeInfo().name;
|
2021-04-07 08:09:01 +00:00
|
|
|
}
|
|
|
|
sem_var->AddUser(sem_user);
|
2021-02-16 21:15:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 08:09:01 +00:00
|
|
|
sem.Add(var, sem_var);
|
2021-02-03 17:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) {
|
2021-11-04 22:29:22 +00:00
|
|
|
std::vector<const sem::GlobalVariable*> out;
|
2021-02-03 17:51:09 +00:00
|
|
|
out.reserve(in.size());
|
|
|
|
for (auto* info : in) {
|
2021-11-04 22:29:22 +00:00
|
|
|
out.emplace_back(sem.Get<sem::GlobalVariable>(info->declaration));
|
2021-02-03 17:51:09 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Functions
|
2021-04-16 19:07:51 +00:00
|
|
|
std::unordered_map<FunctionInfo*, sem::Function*> func_info_to_sem_func;
|
2021-02-03 16:43:20 +00:00
|
|
|
for (auto it : function_to_info_) {
|
|
|
|
auto* func = it.first;
|
|
|
|
auto* info = it.second;
|
2021-03-03 19:54:44 +00:00
|
|
|
|
2021-07-22 13:24:59 +00:00
|
|
|
std::vector<sem::Parameter*> parameters;
|
2021-07-22 13:24:59 +00:00
|
|
|
parameters.reserve(info->parameters.size());
|
|
|
|
for (auto* p : info->parameters) {
|
2021-07-22 13:24:59 +00:00
|
|
|
parameters.emplace_back(sem_params.at(p->declaration));
|
2021-07-22 13:24:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
auto* sem_func = builder_->create<sem::Function>(
|
2021-10-19 18:38:54 +00:00
|
|
|
info->declaration, info->return_type, parameters,
|
|
|
|
remap_vars(info->referenced_module_vars),
|
2021-11-04 22:29:22 +00:00
|
|
|
remap_vars(info->local_referenced_module_vars), info->callsites,
|
|
|
|
ancestor_entry_points[func->symbol], info->workgroup_size);
|
2021-02-08 22:31:44 +00:00
|
|
|
func_info_to_sem_func.emplace(info, sem_func);
|
|
|
|
sem.Add(func, sem_func);
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::CallExpressions
|
2021-02-08 22:31:44 +00:00
|
|
|
for (auto it : function_calls_) {
|
|
|
|
auto* call = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto info = it.second;
|
|
|
|
auto* sem_func = func_info_to_sem_func.at(info.function);
|
2021-04-16 19:07:51 +00:00
|
|
|
sem.Add(call, builder_->create<sem::Call>(call, sem_func, info.statement));
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create semantic nodes for all remaining expression types
|
2021-02-16 18:45:45 +00:00
|
|
|
for (auto it : expr_info_) {
|
2021-02-09 17:38:05 +00:00
|
|
|
auto* expr = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto& info = it.second;
|
2021-02-09 17:38:05 +00:00
|
|
|
if (sem.Get(expr)) {
|
|
|
|
// Expression has already been assigned a semantic node
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-13 12:18:13 +00:00
|
|
|
sem.Add(expr, builder_->create<sem::Expression>(
|
2021-10-19 18:38:54 +00:00
|
|
|
expr, info.type, info.statement, info.constant_value));
|
2021-02-03 16:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-07 20:58:34 +00:00
|
|
|
sem::Array* Resolver::Array(const ast::Array* arr) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto source = arr->source;
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* elem_type = Type(arr->type);
|
2021-08-05 15:18:29 +00:00
|
|
|
if (!elem_type) {
|
2021-03-15 10:43:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:29 +00:00
|
|
|
if (!IsPlain(elem_type)) { // Check must come before GetDefaultAlignAndSize()
|
|
|
|
AddError(elem_type->FriendlyName(builder_->Symbols()) +
|
2021-06-24 11:27:36 +00:00
|
|
|
" cannot be used as an element type of an array",
|
|
|
|
source);
|
2021-06-03 08:24:55 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:29 +00:00
|
|
|
uint32_t el_align = elem_type->Align();
|
|
|
|
uint32_t el_size = elem_type->Size();
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(arr->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:43:11 +00:00
|
|
|
// Look for explicit stride via [[stride(n)]] decoration
|
2021-04-19 19:16:12 +00:00
|
|
|
uint32_t explicit_stride = 0;
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : arr->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
2021-05-07 20:58:34 +00:00
|
|
|
if (auto* sd = deco->As<ast::StrideDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
explicit_stride = sd->stride;
|
2021-05-07 20:58:34 +00:00
|
|
|
if (!ValidateArrayStrideDecoration(sd, el_size, el_align, source)) {
|
2021-04-16 02:36:44 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-05-07 20:58:34 +00:00
|
|
|
continue;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
2021-05-07 20:58:34 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for array types", deco->source);
|
2021-05-07 20:58:34 +00:00
|
|
|
return nullptr;
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
|
|
// Calculate implicit stride
|
2021-09-15 17:37:00 +00:00
|
|
|
uint64_t implicit_stride = utils::RoundUp<uint64_t>(el_align, el_size);
|
2021-05-07 20:58:34 +00:00
|
|
|
|
2021-09-15 17:37:00 +00:00
|
|
|
uint64_t stride = explicit_stride ? explicit_stride : implicit_stride;
|
2021-05-07 20:58:34 +00:00
|
|
|
|
2021-09-02 13:49:59 +00:00
|
|
|
// Evaluate the constant array size expression.
|
|
|
|
// sem::Array uses a size of 0 for a runtime-sized array.
|
|
|
|
uint32_t count = 0;
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* count_expr = arr->count) {
|
2021-09-02 13:49:59 +00:00
|
|
|
if (!Expression(count_expr)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto size_source = count_expr->source;
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
|
|
auto* ty = TypeOf(count_expr)->UnwrapRef();
|
|
|
|
if (!ty->is_integer_scalar()) {
|
|
|
|
AddError("array size must be integer scalar", size_source);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* ident = count_expr->As<ast::IdentifierExpression>()) {
|
|
|
|
// Make sure the identifier is a non-overridable module-scope constant.
|
2021-11-04 22:29:22 +00:00
|
|
|
VariableInfo* var = variable_stack_.Get(ident->symbol);
|
|
|
|
if (!var || var->kind != VariableKind::kGlobal ||
|
2021-10-15 17:33:10 +00:00
|
|
|
!var->declaration->is_const) {
|
2021-09-02 13:49:59 +00:00
|
|
|
AddError("array size identifier must be a module-scope constant",
|
|
|
|
size_source);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (ast::HasDecoration<ast::OverrideDecoration>(
|
2021-10-15 17:33:10 +00:00
|
|
|
var->declaration->decorations)) {
|
2021-09-02 13:49:59 +00:00
|
|
|
AddError("array size expression must not be pipeline-overridable",
|
|
|
|
size_source);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
count_expr = var->declaration->constructor;
|
2021-09-02 13:49:59 +00:00
|
|
|
} else if (!count_expr->Is<ast::ScalarConstructorExpression>()) {
|
|
|
|
AddError(
|
|
|
|
"array size expression must be either a literal or a module-scope "
|
|
|
|
"constant",
|
|
|
|
size_source);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto count_val = ConstantValueOf(count_expr);
|
|
|
|
if (!count_val) {
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "could not resolve array size expression";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ty->is_signed_integer_scalar() ? count_val.Elements()[0].i32 < 1
|
|
|
|
: count_val.Elements()[0].u32 < 1u) {
|
|
|
|
AddError("array size must be at least 1", size_source);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = count_val.Elements()[0].u32;
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:37:00 +00:00
|
|
|
auto size = std::max<uint64_t>(count, 1) * stride;
|
|
|
|
if (size > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
std::stringstream msg;
|
|
|
|
msg << "array size in bytes must not exceed 0x" << std::hex
|
|
|
|
<< std::numeric_limits<uint32_t>::max() << ", but is 0x" << std::hex
|
|
|
|
<< size;
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(msg.str(), arr->source);
|
2021-09-15 17:37:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (stride > std::numeric_limits<uint32_t>::max() ||
|
|
|
|
implicit_stride > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "calculated array stride exceeds uint32";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto* out = builder_->create<sem::Array>(
|
|
|
|
elem_type, count, el_align, static_cast<uint32_t>(size),
|
|
|
|
static_cast<uint32_t>(stride), static_cast<uint32_t>(implicit_stride));
|
2021-05-07 20:58:34 +00:00
|
|
|
|
2021-08-05 15:18:29 +00:00
|
|
|
if (!ValidateArray(out, source)) {
|
2021-05-07 20:58:34 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:29 +00:00
|
|
|
if (elem_type->Is<sem::Atomic>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
atomic_composite_info_.emplace(out, arr->type->source);
|
2021-08-05 15:18:29 +00:00
|
|
|
} else {
|
|
|
|
auto found = atomic_composite_info_.find(elem_type);
|
|
|
|
if (found != atomic_composite_info_.end()) {
|
|
|
|
atomic_composite_info_.emplace(out, found->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 20:58:34 +00:00
|
|
|
bool Resolver::ValidateArray(const sem::Array* arr, const Source& source) {
|
|
|
|
auto* el_ty = arr->ElemType();
|
2021-04-21 16:45:02 +00:00
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* el_str = el_ty->As<sem::Struct>()) {
|
|
|
|
if (el_str->IsBlockDecorated()) {
|
2021-04-21 16:45:02 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#attributes
|
|
|
|
// A structure type with the block attribute must not be:
|
|
|
|
// * the element type of an array type
|
|
|
|
// * the member type in another structure
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-04-21 16:45:02 +00:00
|
|
|
"A structure type with a [[block]] decoration cannot be used as an "
|
|
|
|
"element of an array",
|
|
|
|
source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateArrayStrideDecoration(const ast::StrideDecoration* deco,
|
|
|
|
uint32_t el_size,
|
|
|
|
uint32_t el_align,
|
|
|
|
const Source& source) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto stride = deco->stride;
|
2021-04-21 16:45:02 +00:00
|
|
|
bool is_valid_stride =
|
|
|
|
(stride >= el_size) && (stride >= el_align) && (stride % el_align == 0);
|
|
|
|
if (!is_valid_stride) {
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#array-layout-rules
|
|
|
|
// Arrays decorated with the stride attribute must have a stride that is
|
|
|
|
// at least the size of the element type, and be a multiple of the
|
|
|
|
// element type's alignment value.
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-04-21 16:45:02 +00:00
|
|
|
"arrays decorated with the stride attribute must have a stride "
|
|
|
|
"that is at least the size of the element type, and be a multiple "
|
|
|
|
"of the element type's alignment value.",
|
|
|
|
source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
bool Resolver::ValidateStructure(const sem::Struct* str) {
|
2021-07-01 08:13:41 +00:00
|
|
|
if (str->Members().empty()) {
|
|
|
|
AddError("structures must have at least one member",
|
2021-10-15 17:33:10 +00:00
|
|
|
str->Declaration()->source);
|
2021-07-01 08:13:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:51:37 +00:00
|
|
|
std::unordered_set<uint32_t> locations;
|
2021-05-07 14:49:34 +00:00
|
|
|
for (auto* member : str->Members()) {
|
2021-05-18 10:28:48 +00:00
|
|
|
if (auto* r = member->Type()->As<sem::Array>()) {
|
2021-05-07 20:58:34 +00:00
|
|
|
if (r->IsRuntimeSized()) {
|
2021-05-07 14:49:34 +00:00
|
|
|
if (member != str->Members().back()) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-03-18 17:59:54 +00:00
|
|
|
"runtime arrays may only appear as the last member of a struct",
|
2021-10-15 17:33:10 +00:00
|
|
|
member->Declaration()->source);
|
2021-03-18 17:59:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-07 14:49:34 +00:00
|
|
|
if (!str->IsBlockDecorated()) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError(
|
|
|
|
"a struct containing a runtime-sized array "
|
|
|
|
"requires the [[block]] attribute: '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(str->Declaration()->name) + "'",
|
|
|
|
member->Declaration()->source);
|
2021-03-18 17:59:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 12:16:35 +00:00
|
|
|
auto has_position = false;
|
2021-10-19 18:38:54 +00:00
|
|
|
const ast::InvariantDecoration* invariant_attribute = nullptr;
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : member->Declaration()->decorations) {
|
2021-07-27 08:17:29 +00:00
|
|
|
if (!deco->IsAnyOf<ast::BuiltinDecoration, //
|
|
|
|
ast::InternalDecoration, //
|
|
|
|
ast::InterpolateDecoration, //
|
|
|
|
ast::InvariantDecoration, //
|
|
|
|
ast::LocationDecoration, //
|
|
|
|
ast::StructMemberOffsetDecoration, //
|
|
|
|
ast::StructMemberSizeDecoration, //
|
|
|
|
ast::StructMemberAlignDecoration>()) {
|
|
|
|
if (deco->Is<ast::StrideDecoration>() &&
|
|
|
|
IsValidationDisabled(
|
2021-10-15 17:33:10 +00:00
|
|
|
member->Declaration()->decorations,
|
2021-07-27 08:17:29 +00:00
|
|
|
ast::DisabledValidation::kIgnoreStrideDecoration)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for structure members", deco->source);
|
2021-03-18 17:59:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-07-28 18:51:37 +00:00
|
|
|
|
2021-07-21 12:16:35 +00:00
|
|
|
if (auto* invariant = deco->As<ast::InvariantDecoration>()) {
|
|
|
|
invariant_attribute = invariant;
|
2021-07-28 18:51:37 +00:00
|
|
|
} else if (auto* location = deco->As<ast::LocationDecoration>()) {
|
|
|
|
if (!ValidateLocationDecoration(location, member->Type(), locations,
|
2021-10-15 17:33:10 +00:00
|
|
|
member->Declaration()->source)) {
|
2021-07-28 18:51:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (auto* builtin = deco->As<ast::BuiltinDecoration>()) {
|
2021-07-23 13:23:20 +00:00
|
|
|
if (!ValidateBuiltinDecoration(builtin, member->Type(),
|
2021-10-05 15:02:17 +00:00
|
|
|
/* is_input */ false)) {
|
2021-06-18 19:58:27 +00:00
|
|
|
return false;
|
2021-06-09 16:01:29 +00:00
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (builtin->builtin == ast::Builtin::kPosition) {
|
2021-07-21 12:16:35 +00:00
|
|
|
has_position = true;
|
|
|
|
}
|
2021-06-28 23:04:43 +00:00
|
|
|
} else if (auto* interpolate = deco->As<ast::InterpolateDecoration>()) {
|
|
|
|
if (!ValidateInterpolateDecoration(interpolate, member->Type())) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-09 16:01:29 +00:00
|
|
|
}
|
2021-03-18 17:59:54 +00:00
|
|
|
}
|
2021-06-18 19:57:57 +00:00
|
|
|
|
2021-07-21 12:16:35 +00:00
|
|
|
if (invariant_attribute && !has_position) {
|
|
|
|
AddError("invariant attribute must only be applied to a position builtin",
|
2021-10-15 17:33:10 +00:00
|
|
|
invariant_attribute->source);
|
2021-07-21 12:16:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-18 19:57:57 +00:00
|
|
|
if (auto* member_struct_type = member->Type()->As<sem::Struct>()) {
|
|
|
|
if (auto* member_struct_type_block_decoration =
|
|
|
|
ast::GetDecoration<ast::StructBlockDecoration>(
|
2021-10-15 17:33:10 +00:00
|
|
|
member_struct_type->Declaration()->decorations)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("structs must not contain [[block]] decorated struct members",
|
2021-10-15 17:33:10 +00:00
|
|
|
member->Declaration()->source);
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("see member's struct decoration here",
|
2021-10-15 17:33:10 +00:00
|
|
|
member_struct_type_block_decoration->source);
|
2021-06-18 19:57:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 17:59:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : str->Declaration()->decorations) {
|
2021-03-18 17:59:54 +00:00
|
|
|
if (!(deco->Is<ast::StructBlockDecoration>())) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("decoration is not valid for struct declarations", deco->source);
|
2021-03-18 17:59:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:51:37 +00:00
|
|
|
bool Resolver::ValidateLocationDecoration(
|
|
|
|
const ast::LocationDecoration* location,
|
|
|
|
const sem::Type* type,
|
|
|
|
std::unordered_set<uint32_t>& locations,
|
|
|
|
const Source& source,
|
|
|
|
const bool is_input) {
|
|
|
|
std::string inputs_or_output = is_input ? "inputs" : "output";
|
2021-10-15 17:33:10 +00:00
|
|
|
if (current_function_ && current_function_->declaration->PipelineStage() ==
|
2021-07-28 18:51:37 +00:00
|
|
|
ast::PipelineStage::kCompute) {
|
|
|
|
AddError("decoration is not valid for compute shader " + inputs_or_output,
|
2021-10-15 17:33:10 +00:00
|
|
|
location->source);
|
2021-07-28 18:51:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!type->is_numeric_scalar_or_vector()) {
|
|
|
|
std::string invalid_type = type->FriendlyName(builder_->Symbols());
|
|
|
|
AddError("cannot apply 'location' attribute to declaration of type '" +
|
|
|
|
invalid_type + "'",
|
|
|
|
source);
|
|
|
|
AddNote(
|
|
|
|
"'location' attribute must only be applied to declarations of "
|
|
|
|
"numeric scalar or numeric vector type",
|
2021-10-15 17:33:10 +00:00
|
|
|
location->source);
|
2021-07-28 18:51:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (locations.count(location->value)) {
|
2021-07-28 18:51:37 +00:00
|
|
|
AddError(deco_to_str(location) + " attribute appears multiple times",
|
2021-10-15 17:33:10 +00:00
|
|
|
location->source);
|
2021-07-28 18:51:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
locations.emplace(location->value);
|
2021-07-28 18:51:37 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Struct* Resolver::Structure(const ast::Struct* str) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(str->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : str->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:07:51 +00:00
|
|
|
sem::StructMemberList sem_members;
|
2021-10-15 17:33:10 +00:00
|
|
|
sem_members.reserve(str->members.size());
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
|
|
// Calculate the effective size and alignment of each field, and the overall
|
|
|
|
// size of the structure.
|
|
|
|
// For size, use the size attribute if provided, otherwise use the default
|
|
|
|
// size for the type.
|
|
|
|
// For alignment, use the alignment attribute if provided, otherwise use the
|
|
|
|
// default alignment for the member type.
|
|
|
|
// Diagnostic errors are raised if a basic rule is violated.
|
|
|
|
// Validation of storage-class rules requires analysing the actual variable
|
|
|
|
// usage of the structure, and so is performed as part of the variable
|
|
|
|
// validation.
|
2021-09-15 17:37:00 +00:00
|
|
|
uint64_t struct_size = 0;
|
|
|
|
uint64_t struct_align = 1;
|
2021-10-19 18:38:54 +00:00
|
|
|
std::unordered_map<Symbol, const ast::StructMember*> member_map;
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* member : str->members) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(member);
|
2021-10-15 17:33:10 +00:00
|
|
|
auto result = member_map.emplace(member->symbol, member);
|
2021-07-19 17:38:20 +00:00
|
|
|
if (!result.second) {
|
|
|
|
AddError("redefinition of '" +
|
2021-10-15 17:33:10 +00:00
|
|
|
builder_->Symbols().NameFor(member->symbol) + "'",
|
|
|
|
member->source);
|
|
|
|
AddNote("previous definition is here", result.first->second->source);
|
2021-07-19 17:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
// Resolve member type
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* type = Type(member->type);
|
2021-05-05 09:09:41 +00:00
|
|
|
if (!type) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-04-28 14:13:13 +00:00
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
// Validate member type
|
2021-06-09 09:12:57 +00:00
|
|
|
if (!IsPlain(type)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(type->FriendlyName(builder_->Symbols()) +
|
|
|
|
" cannot be used as the type of a structure member",
|
2021-10-15 17:33:10 +00:00
|
|
|
member->source);
|
2021-03-15 10:43:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:37:00 +00:00
|
|
|
uint64_t offset = struct_size;
|
|
|
|
uint64_t align = type->Align();
|
|
|
|
uint64_t size = type->Size();
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!ValidateNoDuplicateDecorations(member->decorations)) {
|
2021-06-09 18:53:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:34:22 +00:00
|
|
|
bool has_offset_deco = false;
|
|
|
|
bool has_align_deco = false;
|
|
|
|
bool has_size_deco = false;
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* deco : member->decorations) {
|
2021-04-19 19:16:12 +00:00
|
|
|
Mark(deco);
|
2021-03-15 10:43:11 +00:00
|
|
|
if (auto* o = deco->As<ast::StructMemberOffsetDecoration>()) {
|
2021-05-18 10:28:48 +00:00
|
|
|
// Offset decorations are not part of the WGSL spec, but are emitted
|
|
|
|
// by the SPIR-V reader.
|
2021-10-15 17:33:10 +00:00
|
|
|
if (o->offset < struct_size) {
|
|
|
|
AddError("offsets must be in ascending order", o->source);
|
2021-03-15 10:43:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
offset = o->offset;
|
2021-03-15 10:43:11 +00:00
|
|
|
align = 1;
|
2021-03-15 20:34:22 +00:00
|
|
|
has_offset_deco = true;
|
2021-03-15 10:43:11 +00:00
|
|
|
} else if (auto* a = deco->As<ast::StructMemberAlignDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (a->align <= 0 || !utils::IsPowerOfTwo(a->align)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("align value must be a positive, power-of-two integer",
|
2021-10-15 17:33:10 +00:00
|
|
|
a->source);
|
2021-03-15 10:43:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
align = a->align;
|
2021-03-15 20:34:22 +00:00
|
|
|
has_align_deco = true;
|
2021-03-15 10:43:11 +00:00
|
|
|
} else if (auto* s = deco->As<ast::StructMemberSizeDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (s->size < size) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError("size must be at least as big as the type's size (" +
|
|
|
|
std::to_string(size) + ")",
|
2021-10-15 17:33:10 +00:00
|
|
|
s->source);
|
2021-03-15 10:43:11 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
size = s->size;
|
2021-03-15 20:34:22 +00:00
|
|
|
has_size_deco = true;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:34:22 +00:00
|
|
|
if (has_offset_deco && (has_align_deco || has_size_deco)) {
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(
|
2021-03-15 20:34:22 +00:00
|
|
|
"offset decorations cannot be used with align or size decorations",
|
2021-10-15 17:33:10 +00:00
|
|
|
member->source);
|
2021-03-15 20:34:22 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:37:41 +00:00
|
|
|
offset = utils::RoundUp(align, offset);
|
2021-09-15 17:37:00 +00:00
|
|
|
if (offset > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
std::stringstream msg;
|
|
|
|
msg << "struct member has byte offset 0x" << std::hex << offset
|
|
|
|
<< ", but must not exceed 0x" << std::hex
|
|
|
|
<< std::numeric_limits<uint32_t>::max();
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(msg.str(), member->source);
|
2021-09-15 17:37:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* sem_member = builder_->create<sem::StructMember>(
|
2021-10-19 18:38:54 +00:00
|
|
|
member, member->symbol, type, static_cast<uint32_t>(sem_members.size()),
|
2021-10-14 22:43:21 +00:00
|
|
|
static_cast<uint32_t>(offset), static_cast<uint32_t>(align),
|
|
|
|
static_cast<uint32_t>(size));
|
2021-03-15 10:43:11 +00:00
|
|
|
builder_->Sem().Add(member, sem_member);
|
|
|
|
sem_members.emplace_back(sem_member);
|
|
|
|
|
|
|
|
struct_size = offset + size;
|
|
|
|
struct_align = std::max(struct_align, align);
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:37:00 +00:00
|
|
|
uint64_t size_no_padding = struct_size;
|
2021-03-15 13:37:41 +00:00
|
|
|
struct_size = utils::RoundUp(struct_align, struct_size);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-09-15 17:37:00 +00:00
|
|
|
if (struct_size > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
std::stringstream msg;
|
|
|
|
msg << "struct size in bytes must not exceed 0x" << std::hex
|
|
|
|
<< std::numeric_limits<uint32_t>::max() << ", but is 0x" << std::hex
|
|
|
|
<< struct_size;
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError(msg.str(), str->source);
|
2021-09-15 17:37:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (struct_align > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
<< "calculated struct stride exceeds uint32";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* out = builder_->create<sem::Struct>(
|
2021-10-15 17:33:10 +00:00
|
|
|
str, str->name, sem_members, static_cast<uint32_t>(struct_align),
|
2021-09-15 17:37:00 +00:00
|
|
|
static_cast<uint32_t>(struct_size),
|
|
|
|
static_cast<uint32_t>(size_no_padding));
|
2021-06-17 19:56:14 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < sem_members.size(); i++) {
|
2021-08-05 15:18:29 +00:00
|
|
|
auto* mem_type = sem_members[i]->Type();
|
|
|
|
if (mem_type->Is<sem::Atomic>()) {
|
|
|
|
atomic_composite_info_.emplace(out,
|
2021-10-15 17:33:10 +00:00
|
|
|
sem_members[i]->Declaration()->source);
|
2021-08-05 15:18:29 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
auto found = atomic_composite_info_.find(mem_type);
|
|
|
|
if (found != atomic_composite_info_.end()) {
|
|
|
|
atomic_composite_info_.emplace(out, found->second);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-17 19:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-05 09:09:41 +00:00
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (!ValidateStructure(out)) {
|
2021-05-05 09:09:41 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
return out;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 23:20:17 +00:00
|
|
|
bool Resolver::ValidateReturn(const ast::ReturnStatement* ret) {
|
2021-04-30 19:24:29 +00:00
|
|
|
auto* func_type = current_function_->return_type;
|
2021-03-22 23:20:17 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* ret_type = ret->value ? TypeOf(ret->value)->UnwrapRef()
|
|
|
|
: builder_->create<sem::Void>();
|
2021-03-22 23:20:17 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
if (func_type->UnwrapRef() != ret_type) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError(
|
|
|
|
"return statement type must match its function "
|
|
|
|
"return type, returned '" +
|
|
|
|
ret_type->FriendlyName(builder_->Symbols()) + "', expected '" +
|
|
|
|
current_function_->return_type_name + "'",
|
2021-10-15 17:33:10 +00:00
|
|
|
ret->source);
|
2021-03-22 23:20:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-17 18:09:26 +00:00
|
|
|
auto* sem = builder_->Sem().Get(ret);
|
|
|
|
if (auto* continuing =
|
|
|
|
sem->FindFirstParent<sem::LoopContinuingBlockStatement>()) {
|
|
|
|
AddError("continuing blocks must not contain a return statement",
|
2021-10-15 17:33:10 +00:00
|
|
|
ret->source);
|
2021-07-17 18:09:26 +00:00
|
|
|
if (continuing != sem->Parent()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("see continuing block here", continuing->Declaration()->source);
|
2021-07-17 18:09:26 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-22 23:20:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Return(const ast::ReturnStatement* ret) {
|
2021-03-22 23:20:17 +00:00
|
|
|
current_function_->return_statements.push_back(ret);
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* value = ret->value) {
|
2021-06-15 15:00:57 +00:00
|
|
|
if (!Expression(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
2021-03-22 23:20:17 +00:00
|
|
|
|
2021-06-15 15:00:57 +00:00
|
|
|
// Validate after processing the return value expression so that its type is
|
|
|
|
// available for validation.
|
|
|
|
return ValidateReturn(ret);
|
2021-03-22 23:20:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
bool Resolver::ValidateSwitch(const ast::SwitchStatement* s) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto* cond_type = TypeOf(s->condition)->UnwrapRef();
|
2021-03-25 12:55:27 +00:00
|
|
|
if (!cond_type->is_integer_scalar()) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError(
|
|
|
|
"switch statement selector expression must be of a "
|
|
|
|
"scalar integer type",
|
2021-10-15 17:33:10 +00:00
|
|
|
s->condition->source);
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_default = false;
|
2021-10-14 20:30:29 +00:00
|
|
|
std::unordered_map<uint32_t, Source> selectors;
|
2021-03-25 12:55:27 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* case_stmt : s->body) {
|
2021-03-25 12:55:27 +00:00
|
|
|
if (case_stmt->IsDefault()) {
|
|
|
|
if (has_default) {
|
|
|
|
// More than one default clause
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError("switch statement must have exactly one default clause",
|
2021-10-15 17:33:10 +00:00
|
|
|
case_stmt->source);
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
has_default = true;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* selector : case_stmt->selectors) {
|
2021-04-28 13:50:43 +00:00
|
|
|
if (cond_type != TypeOf(selector)) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError(
|
|
|
|
"the case selector values must have the same "
|
|
|
|
"type as the selector expression.",
|
2021-10-15 17:33:10 +00:00
|
|
|
case_stmt->source);
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
auto v = selector->ValueAsU32();
|
2021-10-14 20:30:29 +00:00
|
|
|
auto it = selectors.find(v);
|
|
|
|
if (it != selectors.end()) {
|
|
|
|
auto val = selector->Is<ast::IntLiteral>()
|
2021-10-15 17:33:10 +00:00
|
|
|
? std::to_string(selector->ValueAsI32())
|
|
|
|
: std::to_string(selector->ValueAsU32());
|
|
|
|
AddError("duplicate switch case '" + val + "'", selector->source);
|
2021-10-14 20:30:29 +00:00
|
|
|
AddNote("previous case declared here", it->second);
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
selectors.emplace(v, selector->source);
|
2021-03-25 12:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_default) {
|
|
|
|
// No default clause
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("switch statement must have a default clause", s->source);
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!s->body.empty()) {
|
|
|
|
auto* last_clause = s->body.back()->As<ast::CaseStatement>();
|
|
|
|
auto* last_stmt = last_clause->body->Last();
|
2021-03-25 12:55:27 +00:00
|
|
|
if (last_stmt && last_stmt->Is<ast::FallthroughStatement>()) {
|
2021-06-29 14:42:19 +00:00
|
|
|
AddError(
|
|
|
|
"a fallthrough statement must not appear as "
|
|
|
|
"the last statement in last clause of a switch",
|
2021-10-15 17:33:10 +00:00
|
|
|
last_stmt->source);
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::SwitchStatement(const ast::SwitchStatement* stmt) {
|
2021-07-14 09:44:41 +00:00
|
|
|
auto* sem =
|
|
|
|
builder_->create<sem::SwitchStatement>(stmt, current_compound_statement_);
|
|
|
|
builder_->Sem().Add(stmt, sem);
|
|
|
|
return Scope(sem, [&] {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!Expression(stmt->condition)) {
|
2021-03-25 12:55:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
for (auto* case_stmt : stmt->body) {
|
2021-07-14 09:44:41 +00:00
|
|
|
Mark(case_stmt);
|
|
|
|
if (!CaseStatement(case_stmt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ValidateSwitch(stmt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2021-03-25 12:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
bool Resolver::Assignment(const ast::AssignmentStatement* a) {
|
2021-10-15 17:33:10 +00:00
|
|
|
if (!Expression(a->lhs) || !Expression(a->rhs)) {
|
2021-03-31 13:26:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-21 23:04:44 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
return ValidateAssignment(a);
|
|
|
|
}
|
2021-03-31 13:26:43 +00:00
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
bool Resolver::ValidateAssignment(const ast::AssignmentStatement* a) {
|
2021-10-21 23:04:44 +00:00
|
|
|
auto const* rhs_type = TypeOf(a->rhs);
|
|
|
|
|
|
|
|
if (a->lhs->Is<ast::PhonyExpression>()) {
|
|
|
|
// https://www.w3.org/TR/WGSL/#phony-assignment-section
|
|
|
|
auto* ty = rhs_type->UnwrapRef();
|
|
|
|
if (!ty->IsConstructible() &&
|
|
|
|
!ty->IsAnyOf<sem::Pointer, sem::Texture, sem::Sampler>()) {
|
|
|
|
AddError(
|
|
|
|
"cannot assign '" + TypeNameOf(a->rhs) +
|
2021-10-25 14:26:55 +00:00
|
|
|
"' to '_'. '_' can only be assigned a constructible, pointer, "
|
2021-10-21 23:04:44 +00:00
|
|
|
"texture or sampler type",
|
|
|
|
a->rhs->source);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true; // RHS can be anything.
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl/#assignment-statement
|
2021-10-15 17:33:10 +00:00
|
|
|
auto const* lhs_type = TypeOf(a->lhs);
|
2021-03-31 13:26:43 +00:00
|
|
|
|
2021-10-15 17:33:10 +00:00
|
|
|
if (auto* ident = a->lhs->As<ast::IdentifierExpression>()) {
|
2021-11-04 22:29:22 +00:00
|
|
|
if (VariableInfo* var = variable_stack_.Get(ident->symbol)) {
|
2021-06-28 20:44:31 +00:00
|
|
|
if (var->kind == VariableKind::kParameter) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot assign to function parameter", a->lhs->source);
|
|
|
|
AddNote("'" + builder_->Symbols().NameFor(ident->symbol) +
|
2021-06-28 20:44:31 +00:00
|
|
|
"' is declared here:",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->declaration->source);
|
2021-06-28 20:44:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-15 17:33:10 +00:00
|
|
|
if (var->declaration->is_const) {
|
|
|
|
AddError("cannot assign to const", a->lhs->source);
|
|
|
|
AddNote("'" + builder_->Symbols().NameFor(ident->symbol) +
|
2021-06-28 20:44:31 +00:00
|
|
|
"' is declared here:",
|
2021-10-15 17:33:10 +00:00
|
|
|
var->declaration->source);
|
2021-06-28 20:44:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* lhs_ref = lhs_type->As<sem::Reference>();
|
|
|
|
if (!lhs_ref) {
|
|
|
|
// LHS is not a reference, so it has no storage.
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot assign to value of type '" + TypeNameOf(a->lhs) + "'",
|
|
|
|
a->lhs->source);
|
2021-03-31 13:26:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:26:40 +00:00
|
|
|
auto* storage_type = lhs_ref->StoreType();
|
2021-05-18 10:28:48 +00:00
|
|
|
auto* value_type = rhs_type->UnwrapRef(); // Implicit load of RHS
|
|
|
|
|
|
|
|
// Value type has to match storage type
|
|
|
|
if (storage_type != value_type) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot assign '" + TypeNameOf(a->rhs) + "' to '" +
|
|
|
|
TypeNameOf(a->lhs) + "'",
|
|
|
|
a->source);
|
2021-05-18 10:28:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-13 15:20:42 +00:00
|
|
|
if (!storage_type->IsConstructible()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("storage type of assignment must be constructible", a->source);
|
2021-08-13 15:20:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-15 22:39:19 +00:00
|
|
|
if (lhs_ref->Access() == ast::Access::kRead) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("cannot store into a read-only type '" + TypeNameOf(a->lhs) + "'",
|
|
|
|
a->source);
|
2021-06-15 22:39:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-18 10:28:48 +00:00
|
|
|
return true;
|
2021-03-31 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 21:30:37 +00:00
|
|
|
bool Resolver::ValidateNoDuplicateDefinition(Symbol sym,
|
|
|
|
const Source& source,
|
|
|
|
bool check_global_scope_only) {
|
|
|
|
if (check_global_scope_only) {
|
2021-11-04 22:29:22 +00:00
|
|
|
if (VariableInfo* var = variable_stack_.Get(sym)) {
|
|
|
|
if (var->kind == VariableKind::kGlobal) {
|
2021-06-29 21:30:37 +00:00
|
|
|
AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'",
|
|
|
|
source);
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("previous definition is here", var->declaration->source);
|
2021-06-29 21:30:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto it = symbol_to_function_.find(sym);
|
|
|
|
if (it != symbol_to_function_.end()) {
|
|
|
|
AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'",
|
|
|
|
source);
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("previous definition is here", it->second->declaration->source);
|
2021-06-29 21:30:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-04 22:29:22 +00:00
|
|
|
if (VariableInfo* var = variable_stack_.Get(sym)) {
|
2021-06-29 21:30:37 +00:00
|
|
|
AddError("redefinition of '" + builder_->Symbols().NameFor(sym) + "'",
|
|
|
|
source);
|
2021-10-15 17:33:10 +00:00
|
|
|
AddNote("previous definition is here", var->declaration->source);
|
2021-06-29 21:30:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:53:57 +00:00
|
|
|
bool Resolver::ValidateNoDuplicateDecorations(
|
|
|
|
const ast::DecorationList& decorations) {
|
|
|
|
std::unordered_map<const TypeInfo*, Source> seen;
|
|
|
|
for (auto* d : decorations) {
|
2021-10-15 17:33:10 +00:00
|
|
|
auto res = seen.emplace(&d->TypeInfo(), d->source);
|
2021-06-15 16:33:57 +00:00
|
|
|
if (!res.second && !d->Is<ast::InternalDecoration>()) {
|
2021-10-15 17:33:10 +00:00
|
|
|
AddError("duplicate " + d->Name() + " decoration", d->source);
|
2021-06-24 11:27:36 +00:00
|
|
|
AddNote("first decoration declared here", res.first->second);
|
2021-06-09 18:53:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:47:33 +00:00
|
|
|
bool Resolver::ApplyStorageClassUsageToType(ast::StorageClass sc,
|
2021-05-07 14:49:34 +00:00
|
|
|
sem::Type* ty,
|
2021-04-16 01:15:43 +00:00
|
|
|
const Source& usage) {
|
2021-05-18 10:28:48 +00:00
|
|
|
ty = const_cast<sem::Type*>(ty->UnwrapRef());
|
2021-03-17 22:47:33 +00:00
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* str = ty->As<sem::Struct>()) {
|
|
|
|
if (str->StorageClassUsage().count(sc)) {
|
2021-03-17 22:47:33 +00:00
|
|
|
return true; // Already applied
|
|
|
|
}
|
2021-05-07 14:49:34 +00:00
|
|
|
|
|
|
|
str->AddUsage(sc);
|
|
|
|
|
|
|
|
for (auto* member : str->Members()) {
|
2021-05-05 09:09:41 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(sc, member->Type(), usage)) {
|
2021-03-17 22:47:33 +00:00
|
|
|
std::stringstream err;
|
2021-03-18 20:46:24 +00:00
|
|
|
err << "while analysing structure member "
|
|
|
|
<< str->FriendlyName(builder_->Symbols()) << "."
|
2021-10-15 17:33:10 +00:00
|
|
|
<< builder_->Symbols().NameFor(member->Declaration()->symbol);
|
|
|
|
AddNote(err.str(), member->Declaration()->source);
|
2021-03-17 22:47:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 20:46:24 +00:00
|
|
|
return true;
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 20:58:34 +00:00
|
|
|
if (auto* arr = ty->As<sem::Array>()) {
|
|
|
|
return ApplyStorageClassUsageToType(
|
|
|
|
sc, const_cast<sem::Type*>(arr->ElemType()), usage);
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 21:03:24 +00:00
|
|
|
if (ast::IsHostShareable(sc) && !IsHostShareable(ty)) {
|
2021-03-18 20:46:24 +00:00
|
|
|
std::stringstream err;
|
|
|
|
err << "Type '" << ty->FriendlyName(builder_->Symbols())
|
|
|
|
<< "' cannot be used in storage class '" << sc
|
2021-03-18 21:03:24 +00:00
|
|
|
<< "' as it is non-host-shareable";
|
2021-06-24 11:27:36 +00:00
|
|
|
AddError(err.str(), usage);
|
2021-03-18 20:46:24 +00:00
|
|
|
return false;
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
template <typename F>
|
2021-07-14 09:44:41 +00:00
|
|
|
bool Resolver::Scope(sem::CompoundStatement* stmt, F&& callback) {
|
|
|
|
auto* prev_current_statement = current_statement_;
|
|
|
|
auto* prev_current_compound_statement = current_compound_statement_;
|
|
|
|
auto* prev_current_block = current_block_;
|
|
|
|
current_statement_ = stmt;
|
|
|
|
current_compound_statement_ = stmt;
|
|
|
|
current_block_ = stmt->As<sem::BlockStatement>();
|
2021-11-04 22:29:22 +00:00
|
|
|
variable_stack_.Push();
|
2021-07-14 09:44:41 +00:00
|
|
|
|
|
|
|
TINT_DEFER({
|
2021-11-04 22:29:22 +00:00
|
|
|
TINT_DEFER(variable_stack_.Pop());
|
2021-07-14 09:44:41 +00:00
|
|
|
current_block_ = prev_current_block;
|
|
|
|
current_compound_statement_ = prev_current_compound_statement;
|
|
|
|
current_statement_ = prev_current_statement;
|
|
|
|
});
|
|
|
|
|
2021-07-08 21:23:33 +00:00
|
|
|
return callback();
|
2021-03-09 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 20:58:34 +00:00
|
|
|
std::string Resolver::VectorPretty(uint32_t size,
|
|
|
|
const sem::Type* element_type) {
|
2021-04-19 22:51:23 +00:00
|
|
|
sem::Vector vec_type(element_type, size);
|
2021-03-18 15:43:14 +00:00
|
|
|
return vec_type.FriendlyName(builder_->Symbols());
|
|
|
|
}
|
|
|
|
|
2021-04-28 12:38:13 +00:00
|
|
|
void Resolver::Mark(const ast::Node* node) {
|
2021-04-19 19:16:12 +00:00
|
|
|
if (node == nullptr) {
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_) << "Resolver::Mark() called with nullptr";
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
|
|
|
if (marked_.emplace(node).second) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-24 11:27:36 +00:00
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
2021-04-19 19:16:12 +00:00
|
|
|
<< "AST node '" << node->TypeInfo().name
|
|
|
|
<< "' was encountered twice in the same AST of a Program\n"
|
2021-10-15 17:33:10 +00:00
|
|
|
<< "At: " << node->source << "\n"
|
2021-04-29 20:57:55 +00:00
|
|
|
<< "Pointer: " << node;
|
2021-04-19 19:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:27:36 +00:00
|
|
|
void Resolver::AddError(const std::string& msg, const Source& source) const {
|
|
|
|
diagnostics_.add_error(diag::System::Resolver, msg, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resolver::AddWarning(const std::string& msg, const Source& source) const {
|
|
|
|
diagnostics_.add_warning(diag::System::Resolver, msg, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resolver::AddNote(const std::string& msg, const Source& source) const {
|
|
|
|
diagnostics_.add_note(diag::System::Resolver, msg, source);
|
|
|
|
}
|
|
|
|
|
2021-04-28 12:38:13 +00:00
|
|
|
Resolver::VariableInfo::VariableInfo(const ast::Variable* decl,
|
2021-05-18 10:28:48 +00:00
|
|
|
sem::Type* ty,
|
2021-05-14 17:51:13 +00:00
|
|
|
const std::string& tn,
|
2021-05-18 10:28:48 +00:00
|
|
|
ast::StorageClass sc,
|
2021-06-09 20:45:09 +00:00
|
|
|
ast::Access ac,
|
2021-07-22 13:24:59 +00:00
|
|
|
VariableKind k,
|
|
|
|
uint32_t idx)
|
2021-03-26 12:47:58 +00:00
|
|
|
: declaration(decl),
|
2021-05-18 10:28:48 +00:00
|
|
|
type(ty),
|
2021-04-28 13:50:43 +00:00
|
|
|
type_name(tn),
|
2021-05-18 10:28:48 +00:00
|
|
|
storage_class(sc),
|
2021-06-09 20:45:09 +00:00
|
|
|
access(ac),
|
2021-07-22 13:24:59 +00:00
|
|
|
kind(k),
|
|
|
|
index(idx) {}
|
2021-02-03 17:51:09 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo::~VariableInfo() = default;
|
2021-02-03 17:51:09 +00:00
|
|
|
|
2021-10-19 18:38:54 +00:00
|
|
|
Resolver::FunctionInfo::FunctionInfo(const ast::Function* decl)
|
|
|
|
: declaration(decl) {}
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::FunctionInfo::~FunctionInfo() = default;
|
2021-02-03 16:43:20 +00:00
|
|
|
|
2021-03-09 15:08:48 +00:00
|
|
|
} // namespace resolver
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|