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.
|
|
|
|
|
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/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
|
|
|
|
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/ast/alias.h"
|
|
|
|
|
#include "src/tint/ast/array.h"
|
|
|
|
|
#include "src/tint/ast/assignment_statement.h"
|
|
|
|
|
#include "src/tint/ast/bitcast_expression.h"
|
|
|
|
|
#include "src/tint/ast/break_statement.h"
|
|
|
|
|
#include "src/tint/ast/call_statement.h"
|
|
|
|
|
#include "src/tint/ast/continue_statement.h"
|
|
|
|
|
#include "src/tint/ast/depth_texture.h"
|
|
|
|
|
#include "src/tint/ast/disable_validation_attribute.h"
|
|
|
|
|
#include "src/tint/ast/discard_statement.h"
|
|
|
|
|
#include "src/tint/ast/fallthrough_statement.h"
|
|
|
|
|
#include "src/tint/ast/for_loop_statement.h"
|
|
|
|
|
#include "src/tint/ast/id_attribute.h"
|
|
|
|
|
#include "src/tint/ast/if_statement.h"
|
|
|
|
|
#include "src/tint/ast/internal_attribute.h"
|
|
|
|
|
#include "src/tint/ast/interpolate_attribute.h"
|
|
|
|
|
#include "src/tint/ast/loop_statement.h"
|
|
|
|
|
#include "src/tint/ast/matrix.h"
|
|
|
|
|
#include "src/tint/ast/pointer.h"
|
|
|
|
|
#include "src/tint/ast/return_statement.h"
|
|
|
|
|
#include "src/tint/ast/sampled_texture.h"
|
|
|
|
|
#include "src/tint/ast/sampler.h"
|
|
|
|
|
#include "src/tint/ast/storage_texture.h"
|
|
|
|
|
#include "src/tint/ast/switch_statement.h"
|
|
|
|
|
#include "src/tint/ast/traverse_expressions.h"
|
|
|
|
|
#include "src/tint/ast/type_name.h"
|
|
|
|
|
#include "src/tint/ast/unary_op_expression.h"
|
|
|
|
|
#include "src/tint/ast/variable_decl_statement.h"
|
|
|
|
|
#include "src/tint/ast/vector.h"
|
2022-06-16 12:01:27 +00:00
|
|
|
|
#include "src/tint/ast/while_statement.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/ast/workgroup_attribute.h"
|
2022-05-11 22:05:15 +00:00
|
|
|
|
#include "src/tint/resolver/uniformity.h"
|
2022-05-20 17:18:50 +00:00
|
|
|
|
#include "src/tint/sem/abstract_float.h"
|
|
|
|
|
#include "src/tint/sem/abstract_int.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/array.h"
|
2022-04-28 18:49:04 +00:00
|
|
|
|
#include "src/tint/sem/atomic.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/call.h"
|
2022-04-28 18:49:04 +00:00
|
|
|
|
#include "src/tint/sem/depth_multisampled_texture.h"
|
|
|
|
|
#include "src/tint/sem/depth_texture.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/for_loop_statement.h"
|
|
|
|
|
#include "src/tint/sem/function.h"
|
|
|
|
|
#include "src/tint/sem/if_statement.h"
|
2022-06-24 20:34:00 +00:00
|
|
|
|
#include "src/tint/sem/index_accessor_expression.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/loop_statement.h"
|
2022-05-24 21:42:03 +00:00
|
|
|
|
#include "src/tint/sem/materialize.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/member_accessor_expression.h"
|
|
|
|
|
#include "src/tint/sem/module.h"
|
2022-04-28 18:49:04 +00:00
|
|
|
|
#include "src/tint/sem/multisampled_texture.h"
|
|
|
|
|
#include "src/tint/sem/pointer.h"
|
|
|
|
|
#include "src/tint/sem/reference.h"
|
|
|
|
|
#include "src/tint/sem/sampled_texture.h"
|
|
|
|
|
#include "src/tint/sem/sampler.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/statement.h"
|
2022-04-28 18:49:04 +00:00
|
|
|
|
#include "src/tint/sem/storage_texture.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/sem/struct.h"
|
|
|
|
|
#include "src/tint/sem/switch_statement.h"
|
|
|
|
|
#include "src/tint/sem/type_constructor.h"
|
|
|
|
|
#include "src/tint/sem/type_conversion.h"
|
|
|
|
|
#include "src/tint/sem/variable.h"
|
2022-06-16 12:01:27 +00:00
|
|
|
|
#include "src/tint/sem/while_statement.h"
|
2022-02-21 15:19:07 +00:00
|
|
|
|
#include "src/tint/utils/defer.h"
|
|
|
|
|
#include "src/tint/utils/math.h"
|
|
|
|
|
#include "src/tint/utils/reverse.h"
|
|
|
|
|
#include "src/tint/utils/scoped_assignment.h"
|
|
|
|
|
#include "src/tint/utils/transform.h"
|
2022-07-26 07:55:24 +00:00
|
|
|
|
#include "src/tint/utils/vector.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
|
2022-04-07 17:45:45 +00:00
|
|
|
|
namespace tint::resolver {
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
2022-06-01 20:34:40 +00:00
|
|
|
|
Resolver::Resolver(ProgramBuilder* builder)
|
2021-05-06 16:04:03 +00:00
|
|
|
|
: builder_(builder),
|
|
|
|
|
diagnostics_(builder->Diagnostics()),
|
2022-07-15 14:14:09 +00:00
|
|
|
|
const_eval_(*builder),
|
2022-05-06 15:13:01 +00:00
|
|
|
|
intrinsic_table_(IntrinsicTable::Create(*builder)),
|
2022-04-21 13:40:16 +00:00
|
|
|
|
sem_(builder, dependencies_),
|
2022-06-01 20:34:40 +00:00
|
|
|
|
validator_(builder, sem_) {}
|
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
|
|
|
|
bool Resolver::Resolve() {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (builder_->Diagnostics().contains_errors()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-05-05 09:09:41 +00:00
|
|
|
|
|
2022-07-18 20:50:02 +00:00
|
|
|
|
builder_->Sem().Reserve(builder_->LastAllocatedNodeID());
|
2022-07-18 16:47:32 +00:00
|
|
|
|
|
2022-07-21 23:46:15 +00:00
|
|
|
|
// Pre-allocate the marked bitset with the total number of AST nodes.
|
|
|
|
|
marked_.Resize(builder_->ASTNodes().Count());
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!DependencyGraph::Build(builder_->AST(), builder_->Symbols(), builder_->Diagnostics(),
|
|
|
|
|
dependencies_)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-11-22 11:44:57 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
bool result = ResolveInternal();
|
2021-02-03 17:51:09 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!result && !diagnostics_.contains_errors()) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "resolving failed, but no error was raised";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-05-05 09:09:41 +00:00
|
|
|
|
|
2022-05-18 22:41:48 +00:00
|
|
|
|
// Create the semantic module
|
|
|
|
|
builder_->Sem().SetModule(builder_->create<sem::Module>(
|
|
|
|
|
std::move(dependencies_.ordered_globals), std::move(enabled_extensions_)));
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return result;
|
2021-02-03 17:51:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
|
bool Resolver::ResolveInternal() {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
Mark(&builder_->AST());
|
|
|
|
|
|
|
|
|
|
// Process all module-scope declarations in dependency order.
|
|
|
|
|
for (auto* decl : dependencies_.ordered_globals) {
|
|
|
|
|
Mark(decl);
|
2022-05-18 22:41:48 +00:00
|
|
|
|
if (!Switch<bool>(
|
2022-05-01 14:40:55 +00:00
|
|
|
|
decl, //
|
2022-05-18 22:41:48 +00:00
|
|
|
|
[&](const ast::Enable* e) { return Enable(e); },
|
2022-05-01 14:40:55 +00:00
|
|
|
|
[&](const ast::TypeDecl* td) { return TypeDecl(td); },
|
|
|
|
|
[&](const ast::Function* func) { return Function(func); },
|
|
|
|
|
[&](const ast::Variable* var) { return GlobalVariable(var); },
|
|
|
|
|
[&](Default) {
|
|
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_)
|
|
|
|
|
<< "unhandled global declaration: " << decl->TypeInfo().name;
|
2022-05-18 22:41:48 +00:00
|
|
|
|
return false;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
})) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
|
}
|
2021-03-19 18:45:30 +00:00
|
|
|
|
|
2022-07-27 20:50:40 +00:00
|
|
|
|
if (!AllocateOverridableConstantIds()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-09-30 17:29:50 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
SetShadows();
|
2021-11-23 20:45:51 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!validator_.PipelineStages(entry_points_)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-06-03 16:07:34 +00:00
|
|
|
|
|
2022-08-02 15:55:35 +00:00
|
|
|
|
if (!validator_.PushConstants(entry_points_)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 22:41:48 +00:00
|
|
|
|
if (!enabled_extensions_.contains(ast::Extension::kChromiumDisableUniformityAnalysis)) {
|
|
|
|
|
if (!AnalyzeUniformity(builder_, dependencies_)) {
|
|
|
|
|
// TODO(jrprice): Reject programs that fail uniformity analysis.
|
|
|
|
|
}
|
2022-05-11 22:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
bool result = true;
|
|
|
|
|
for (auto* node : builder_->ASTNodes().Objects()) {
|
2022-07-21 23:46:15 +00:00
|
|
|
|
if (!marked_[node->node_id.value]) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< "AST node '" << node->TypeInfo().name << "' was not reached by the resolver\n"
|
|
|
|
|
<< "At: " << node->source << "\n"
|
|
|
|
|
<< "Pointer: " << node;
|
|
|
|
|
result = false;
|
|
|
|
|
}
|
2021-04-19 19:16:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +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) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
Mark(ty);
|
|
|
|
|
auto* s = Switch(
|
|
|
|
|
ty, //
|
|
|
|
|
[&](const ast::Void*) { return builder_->create<sem::Void>(); },
|
|
|
|
|
[&](const ast::Bool*) { return builder_->create<sem::Bool>(); },
|
|
|
|
|
[&](const ast::I32*) { return builder_->create<sem::I32>(); },
|
|
|
|
|
[&](const ast::U32*) { return builder_->create<sem::U32>(); },
|
2022-05-13 12:01:11 +00:00
|
|
|
|
[&](const ast::F16* t) -> sem::F16* {
|
|
|
|
|
// Validate if f16 type is allowed.
|
2022-05-18 22:41:48 +00:00
|
|
|
|
if (!enabled_extensions_.contains(ast::Extension::kF16)) {
|
2022-05-13 12:01:11 +00:00
|
|
|
|
AddError("f16 used without 'f16' extension enabled", t->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return builder_->create<sem::F16>();
|
|
|
|
|
},
|
2022-05-01 14:40:55 +00:00
|
|
|
|
[&](const ast::F32*) { return builder_->create<sem::F32>(); },
|
|
|
|
|
[&](const ast::Vector* t) -> sem::Vector* {
|
|
|
|
|
if (!t->type) {
|
|
|
|
|
AddError("missing vector element type", t->source.End());
|
|
|
|
|
return nullptr;
|
2022-02-14 10:05:16 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* el = Type(t->type)) {
|
|
|
|
|
if (auto* vector = builder_->create<sem::Vector>(el, t->width)) {
|
|
|
|
|
if (validator_.Vector(vector, t->source)) {
|
|
|
|
|
return vector;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-21 17:08:05 +00:00
|
|
|
|
}
|
2022-02-14 10:05:16 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
},
|
|
|
|
|
[&](const ast::Matrix* t) -> sem::Matrix* {
|
|
|
|
|
if (!t->type) {
|
|
|
|
|
AddError("missing matrix element type", t->source.End());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (auto* el = Type(t->type)) {
|
|
|
|
|
if (auto* column_type = builder_->create<sem::Vector>(el, t->rows)) {
|
|
|
|
|
if (auto* matrix = builder_->create<sem::Matrix>(column_type, t->columns)) {
|
|
|
|
|
if (validator_.Matrix(matrix, t->source)) {
|
|
|
|
|
return matrix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-14 10:05:16 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
},
|
|
|
|
|
[&](const ast::Array* t) { return Array(t); },
|
|
|
|
|
[&](const ast::Atomic* t) -> sem::Atomic* {
|
|
|
|
|
if (auto* el = Type(t->type)) {
|
|
|
|
|
auto* a = builder_->create<sem::Atomic>(el);
|
|
|
|
|
if (!validator_.Atomic(t, a)) {
|
|
|
|
|
return nullptr;
|
2022-03-22 14:04:41 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::Pointer* t) -> sem::Pointer* {
|
|
|
|
|
if (auto* el = Type(t->type)) {
|
|
|
|
|
auto access = t->access;
|
|
|
|
|
if (access == ast::kUndefined) {
|
|
|
|
|
access = DefaultAccessForStorageClass(t->storage_class);
|
|
|
|
|
}
|
|
|
|
|
return builder_->create<sem::Pointer>(el, t->storage_class, access);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::Sampler* t) { return builder_->create<sem::Sampler>(t->kind); },
|
|
|
|
|
[&](const ast::SampledTexture* t) -> sem::SampledTexture* {
|
|
|
|
|
if (auto* el = Type(t->type)) {
|
2022-06-17 12:52:20 +00:00
|
|
|
|
auto* sem = builder_->create<sem::SampledTexture>(t->dim, el);
|
|
|
|
|
if (!validator_.SampledTexture(sem, t->source)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return sem;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::MultisampledTexture* t) -> sem::MultisampledTexture* {
|
|
|
|
|
if (auto* el = Type(t->type)) {
|
2022-06-17 12:52:20 +00:00
|
|
|
|
auto* sem = builder_->create<sem::MultisampledTexture>(t->dim, el);
|
|
|
|
|
if (!validator_.MultisampledTexture(sem, t->source)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return sem;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::DepthTexture* t) { return builder_->create<sem::DepthTexture>(t->dim); },
|
|
|
|
|
[&](const ast::DepthMultisampledTexture* t) {
|
|
|
|
|
return builder_->create<sem::DepthMultisampledTexture>(t->dim);
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::StorageTexture* t) -> sem::StorageTexture* {
|
|
|
|
|
if (auto* el = Type(t->type)) {
|
|
|
|
|
if (!validator_.StorageTexture(t)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return builder_->create<sem::StorageTexture>(t->dim, t->format, t->access, el);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::ExternalTexture*) { return builder_->create<sem::ExternalTexture>(); },
|
|
|
|
|
[&](Default) {
|
|
|
|
|
auto* resolved = sem_.ResolvedSymbol(ty);
|
|
|
|
|
return Switch(
|
|
|
|
|
resolved, //
|
|
|
|
|
[&](sem::Type* type) { return type; },
|
|
|
|
|
[&](sem::Variable* var) {
|
|
|
|
|
auto name = builder_->Symbols().NameFor(var->Declaration()->symbol);
|
|
|
|
|
AddError("cannot use variable '" + name + "' as type", ty->source);
|
|
|
|
|
AddNote("'" + name + "' declared here", var->Declaration()->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](sem::Function* func) {
|
|
|
|
|
auto name = builder_->Symbols().NameFor(func->Declaration()->symbol);
|
|
|
|
|
AddError("cannot use function '" + name + "' as type", ty->source);
|
|
|
|
|
AddNote("'" + name + "' declared here", func->Declaration()->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](Default) {
|
|
|
|
|
if (auto* tn = ty->As<ast::TypeName>()) {
|
|
|
|
|
if (IsBuiltin(tn->name)) {
|
|
|
|
|
auto name = builder_->Symbols().NameFor(tn->name);
|
|
|
|
|
AddError("cannot use builtin '" + name + "' as type", ty->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_)
|
|
|
|
|
<< "Unhandled resolved type '"
|
|
|
|
|
<< (resolved ? resolved->TypeInfo().name : "<null>")
|
|
|
|
|
<< "' resolved from ast::Type '" << ty->TypeInfo().name << "'";
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-04-30 20:20:19 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (s) {
|
|
|
|
|
builder_->Sem().Add(ty, s);
|
|
|
|
|
}
|
|
|
|
|
return s;
|
2021-04-21 13:47:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 17:26:40 +00:00
|
|
|
|
sem::Variable* Resolver::Variable(const ast::Variable* v, bool is_global) {
|
2022-06-20 19:17:52 +00:00
|
|
|
|
return Switch(
|
|
|
|
|
v, //
|
|
|
|
|
[&](const ast::Var* var) { return Var(var, is_global); },
|
|
|
|
|
[&](const ast::Let* let) { return Let(let, is_global); },
|
|
|
|
|
[&](const ast::Override* override) { return Override(override); },
|
2022-06-25 23:21:39 +00:00
|
|
|
|
[&](const ast::Const* const_) { return Const(const_, is_global); },
|
2022-06-20 19:17:52 +00:00
|
|
|
|
[&](Default) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< "Resolver::GlobalVariable() called with a unknown variable type: "
|
|
|
|
|
<< v->TypeInfo().name;
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Variable* Resolver::Let(const ast::Let* v, bool is_global) {
|
2022-06-20 15:30:41 +00:00
|
|
|
|
const sem::Type* ty = nullptr;
|
2021-05-18 10:28:48 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// If the variable has a declared type, resolve it.
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (v->type) {
|
|
|
|
|
ty = Type(v->type);
|
|
|
|
|
if (!ty) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-05-12 10:41:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 19:17:52 +00:00
|
|
|
|
if (!v->constructor) {
|
|
|
|
|
AddError("'let' declaration must have an initializer", v->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* rhs = Materialize(Expression(v->constructor), ty);
|
|
|
|
|
if (!rhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the variable has no declared type, infer it from the RHS
|
|
|
|
|
if (!ty) {
|
|
|
|
|
ty = rhs->Type()->UnwrapRef(); // Implicit load of RHS
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 11:36:10 +00:00
|
|
|
|
if (rhs && !validator_.VariableInitializer(v, ast::StorageClass::kNone, ty, rhs)) {
|
2022-06-20 19:17:52 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, const_cast<sem::Type*>(ty),
|
|
|
|
|
v->source)) {
|
|
|
|
|
AddNote("while instantiating 'let' " + builder_->Symbols().NameFor(v->symbol), v->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Variable* sem = nullptr;
|
|
|
|
|
if (is_global) {
|
2022-06-29 19:07:30 +00:00
|
|
|
|
sem = builder_->create<sem::GlobalVariable>(
|
2022-07-15 23:46:31 +00:00
|
|
|
|
v, ty, sem::EvaluationStage::kRuntime, ast::StorageClass::kNone,
|
|
|
|
|
ast::Access::kUndefined, /* constant_value */ nullptr, sem::BindingPoint{});
|
2022-06-20 19:17:52 +00:00
|
|
|
|
} else {
|
2022-07-15 23:46:31 +00:00
|
|
|
|
sem = builder_->create<sem::LocalVariable>(v, ty, sem::EvaluationStage::kRuntime,
|
|
|
|
|
ast::StorageClass::kNone,
|
2022-06-20 19:17:52 +00:00
|
|
|
|
ast::Access::kUndefined, current_statement_,
|
2022-06-29 19:07:30 +00:00
|
|
|
|
/* constant_value */ nullptr);
|
2022-06-20 19:17:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem->SetConstructor(rhs);
|
|
|
|
|
builder_->Sem().Add(v, sem);
|
|
|
|
|
return sem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Variable* Resolver::Override(const ast::Override* v) {
|
|
|
|
|
const sem::Type* ty = nullptr;
|
|
|
|
|
|
|
|
|
|
// If the variable has a declared type, resolve it.
|
|
|
|
|
if (v->type) {
|
|
|
|
|
ty = Type(v->type);
|
|
|
|
|
if (!ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-17 12:48:51 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
const sem::Expression* rhs = nullptr;
|
|
|
|
|
|
|
|
|
|
// Does the variable have a constructor?
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (v->constructor) {
|
2022-06-20 15:30:41 +00:00
|
|
|
|
rhs = Materialize(Expression(v->constructor), ty);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!rhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the variable has no declared type, infer it from the RHS
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (!ty) {
|
|
|
|
|
ty = rhs->Type()->UnwrapRef(); // Implicit load of RHS
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-06-20 15:30:41 +00:00
|
|
|
|
} else if (!ty) {
|
2022-06-26 11:36:10 +00:00
|
|
|
|
AddError("override declaration requires a type or initializer", v->source);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
2021-05-12 10:41:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 11:36:10 +00:00
|
|
|
|
if (rhs && !validator_.VariableInitializer(v, ast::StorageClass::kNone, ty, rhs)) {
|
2022-06-20 15:30:41 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, const_cast<sem::Type*>(ty),
|
|
|
|
|
v->source)) {
|
2022-06-20 19:17:52 +00:00
|
|
|
|
AddNote("while instantiating 'override' " + builder_->Symbols().NameFor(v->symbol),
|
2022-06-20 15:30:41 +00:00
|
|
|
|
v->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 19:07:30 +00:00
|
|
|
|
auto* sem = builder_->create<sem::GlobalVariable>(
|
2022-07-15 23:46:31 +00:00
|
|
|
|
v, ty, sem::EvaluationStage::kOverride, ast::StorageClass::kNone, ast::Access::kUndefined,
|
|
|
|
|
/* constant_value */ nullptr, sem::BindingPoint{});
|
2022-06-20 15:30:41 +00:00
|
|
|
|
|
2022-06-20 19:17:52 +00:00
|
|
|
|
if (auto* id = ast::GetAttribute<ast::IdAttribute>(v->attributes)) {
|
2022-07-27 20:50:40 +00:00
|
|
|
|
sem->SetOverrideId(OverrideId{static_cast<decltype(OverrideId::value)>(id->value)});
|
2022-06-20 15:30:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem->SetConstructor(rhs);
|
|
|
|
|
builder_->Sem().Add(v, sem);
|
|
|
|
|
return sem;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-25 23:21:39 +00:00
|
|
|
|
sem::Variable* Resolver::Const(const ast::Const* c, bool is_global) {
|
|
|
|
|
const sem::Type* ty = nullptr;
|
|
|
|
|
|
|
|
|
|
// If the variable has a declared type, resolve it.
|
|
|
|
|
if (c->type) {
|
|
|
|
|
ty = Type(c->type);
|
|
|
|
|
if (!ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!c->constructor) {
|
|
|
|
|
AddError("'const' declaration must have an initializer", c->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto* rhs = Expression(c->constructor);
|
|
|
|
|
if (!rhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ty) {
|
|
|
|
|
// If an explicit type was specified, materialize to that type
|
|
|
|
|
rhs = Materialize(rhs, ty);
|
2022-07-07 16:56:21 +00:00
|
|
|
|
if (!rhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-06-25 23:21:39 +00:00
|
|
|
|
} else {
|
|
|
|
|
// If no type was specified, infer it from the RHS
|
|
|
|
|
ty = rhs->Type();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto value = rhs->ConstantValue();
|
|
|
|
|
if (!value) {
|
|
|
|
|
AddError("'const' initializer must be constant expression", c->constructor->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 11:36:10 +00:00
|
|
|
|
if (!validator_.VariableInitializer(c, ast::StorageClass::kNone, ty, rhs)) {
|
2022-06-25 23:21:39 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, const_cast<sem::Type*>(ty),
|
|
|
|
|
c->source)) {
|
|
|
|
|
AddNote("while instantiating 'const' " + builder_->Symbols().NameFor(c->symbol), c->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* sem = is_global ? static_cast<sem::Variable*>(builder_->create<sem::GlobalVariable>(
|
2022-07-15 23:46:31 +00:00
|
|
|
|
c, ty, sem::EvaluationStage::kConstant, ast::StorageClass::kNone,
|
|
|
|
|
ast::Access::kUndefined, value, sem::BindingPoint{}))
|
2022-06-25 23:21:39 +00:00
|
|
|
|
: static_cast<sem::Variable*>(builder_->create<sem::LocalVariable>(
|
2022-07-15 23:46:31 +00:00
|
|
|
|
c, ty, sem::EvaluationStage::kConstant, ast::StorageClass::kNone,
|
|
|
|
|
ast::Access::kUndefined, current_statement_, value));
|
2022-06-25 23:21:39 +00:00
|
|
|
|
|
|
|
|
|
sem->SetConstructor(rhs);
|
|
|
|
|
builder_->Sem().Add(c, sem);
|
|
|
|
|
return sem;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
sem::Variable* Resolver::Var(const ast::Var* var, bool is_global) {
|
|
|
|
|
const sem::Type* storage_ty = nullptr;
|
|
|
|
|
|
|
|
|
|
// If the variable has a declared type, resolve it.
|
|
|
|
|
if (auto* ty = var->type) {
|
|
|
|
|
storage_ty = Type(ty);
|
|
|
|
|
if (!storage_ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sem::Expression* rhs = nullptr;
|
|
|
|
|
|
|
|
|
|
// Does the variable have a constructor?
|
|
|
|
|
if (var->constructor) {
|
|
|
|
|
rhs = Materialize(Expression(var->constructor), storage_ty);
|
|
|
|
|
if (!rhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
// If the variable has no declared type, infer it from the RHS
|
|
|
|
|
if (!storage_ty) {
|
|
|
|
|
storage_ty = rhs->Type()->UnwrapRef(); // Implicit load of RHS
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 16:51:38 +00:00
|
|
|
|
if (!storage_ty) {
|
2022-06-26 11:36:10 +00:00
|
|
|
|
AddError("var declaration requires a type or initializer", var->source);
|
2021-06-04 15:28:47 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-06-04 15:28:47 +00:00
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
auto storage_class = var->declared_storage_class;
|
|
|
|
|
if (storage_class == ast::StorageClass::kNone) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// No declared storage class. Infer from usage / type.
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (!is_global) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
storage_class = ast::StorageClass::kFunction;
|
|
|
|
|
} else if (storage_ty->UnwrapRef()->is_handle()) {
|
|
|
|
|
// 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 attribute. The
|
|
|
|
|
// storage class will always be handle.
|
|
|
|
|
storage_class = ast::StorageClass::kHandle;
|
|
|
|
|
}
|
2021-05-12 10:41:21 +00:00
|
|
|
|
}
|
2021-04-28 13:50:43 +00:00
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (!is_global && storage_class != ast::StorageClass::kFunction &&
|
|
|
|
|
validator_.IsValidationEnabled(var->attributes,
|
2022-05-01 14:40:55 +00:00
|
|
|
|
ast::DisabledValidation::kIgnoreStorageClass)) {
|
2022-06-20 15:30:41 +00:00
|
|
|
|
AddError("function-scope 'var' declaration must use 'function' storage class", var->source);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
auto access = var->declared_access;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (access == ast::Access::kUndefined) {
|
|
|
|
|
access = DefaultAccessForStorageClass(storage_class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 11:36:10 +00:00
|
|
|
|
if (rhs && !validator_.VariableInitializer(var, storage_class, storage_ty, rhs)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
auto* var_ty = builder_->create<sem::Reference>(storage_ty, storage_class, access);
|
|
|
|
|
|
|
|
|
|
if (!ApplyStorageClassUsageToType(storage_class, var_ty, var->source)) {
|
|
|
|
|
AddNote("while instantiating 'var' " + builder_->Symbols().NameFor(var->symbol),
|
|
|
|
|
var->source);
|
2021-11-05 16:51:38 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
sem::Variable* sem = nullptr;
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (is_global) {
|
|
|
|
|
sem::BindingPoint binding_point;
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (auto bp = var->BindingPoint()) {
|
|
|
|
|
binding_point = {bp.group->value, bp.binding->value};
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-07-15 23:46:31 +00:00
|
|
|
|
sem = builder_->create<sem::GlobalVariable>(var, var_ty, sem::EvaluationStage::kRuntime,
|
|
|
|
|
storage_class, access,
|
2022-06-29 19:07:30 +00:00
|
|
|
|
/* constant_value */ nullptr, binding_point);
|
2022-06-17 12:48:51 +00:00
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
} else {
|
2022-07-15 23:46:31 +00:00
|
|
|
|
sem = builder_->create<sem::LocalVariable>(var, var_ty, sem::EvaluationStage::kRuntime,
|
|
|
|
|
storage_class, access, current_statement_,
|
|
|
|
|
/* constant_value */ nullptr);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
sem->SetConstructor(rhs);
|
|
|
|
|
builder_->Sem().Add(var, sem);
|
|
|
|
|
return sem;
|
2021-04-16 01:15:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 17:26:40 +00:00
|
|
|
|
sem::Parameter* Resolver::Parameter(const ast::Parameter* param, uint32_t index) {
|
|
|
|
|
auto add_note = [&] {
|
|
|
|
|
AddNote("while instantiating parameter " + builder_->Symbols().NameFor(param->symbol),
|
|
|
|
|
param->source);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (auto* attr : param->attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
}
|
|
|
|
|
if (!validator_.NoDuplicateAttributes(param->attributes)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Type* ty = Type(param->type);
|
|
|
|
|
if (!ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, ty, param->source)) {
|
|
|
|
|
add_note();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto* ptr = ty->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)) {
|
|
|
|
|
add_note();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* sem = builder_->create<sem::Parameter>(param, index, ty, ast::StorageClass::kNone,
|
|
|
|
|
ast::Access::kUndefined);
|
|
|
|
|
builder_->Sem().Add(param, sem);
|
|
|
|
|
return sem;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
ast::Access Resolver::DefaultAccessForStorageClass(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::kHandle:
|
|
|
|
|
return ast::Access::kRead;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ast::Access::kReadWrite;
|
2021-06-04 22:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 20:50:40 +00:00
|
|
|
|
bool Resolver::AllocateOverridableConstantIds() {
|
|
|
|
|
constexpr size_t kLimit = std::numeric_limits<decltype(OverrideId::value)>::max();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// The next pipeline constant ID to try to allocate.
|
2022-07-27 20:50:40 +00:00
|
|
|
|
OverrideId next_id;
|
|
|
|
|
bool ids_exhausted = false;
|
|
|
|
|
|
|
|
|
|
auto increment_next_id = [&] {
|
|
|
|
|
if (next_id.value == kLimit) {
|
|
|
|
|
ids_exhausted = true;
|
|
|
|
|
} else {
|
|
|
|
|
next_id.value = next_id.value + 1;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
// 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()) {
|
2022-06-17 12:48:51 +00:00
|
|
|
|
auto* override = decl->As<ast::Override>();
|
|
|
|
|
if (!override) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 20:50:40 +00:00
|
|
|
|
OverrideId id;
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (auto* id_attr = ast::GetAttribute<ast::IdAttribute>(override->attributes)) {
|
2022-07-27 20:50:40 +00:00
|
|
|
|
id = OverrideId{static_cast<decltype(OverrideId::value)>(id_attr->value)};
|
2022-05-01 14:40:55 +00:00
|
|
|
|
} else {
|
|
|
|
|
// No ID was specified, so allocate the next available ID.
|
2022-07-27 20:50:40 +00:00
|
|
|
|
while (!ids_exhausted && override_ids_.count(next_id)) {
|
|
|
|
|
increment_next_id();
|
|
|
|
|
}
|
|
|
|
|
if (ids_exhausted) {
|
|
|
|
|
AddError(
|
|
|
|
|
"number of 'override' variables exceeded limit of " + std::to_string(kLimit),
|
|
|
|
|
decl->source);
|
|
|
|
|
return false;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-07-27 20:50:40 +00:00
|
|
|
|
id = next_id;
|
|
|
|
|
increment_next_id();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 12:48:51 +00:00
|
|
|
|
auto* sem = sem_.Get<sem::GlobalVariable>(override);
|
2022-07-27 20:50:40 +00:00
|
|
|
|
const_cast<sem::GlobalVariable*>(sem)->SetOverrideId(id);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-07-27 20:50:40 +00:00
|
|
|
|
return true;
|
2021-09-30 17:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 20:45:51 +00:00
|
|
|
|
void Resolver::SetShadows() {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto it : dependencies_.shadows) {
|
|
|
|
|
Switch(
|
|
|
|
|
sem_.Get(it.first), //
|
|
|
|
|
[&](sem::LocalVariable* local) { local->SetShadows(sem_.Get(it.second)); },
|
|
|
|
|
[&](sem::Parameter* param) { param->SetShadows(sem_.Get(it.second)); });
|
|
|
|
|
}
|
2022-04-07 17:45:45 +00:00
|
|
|
|
}
|
2021-04-06 20:18:57 +00:00
|
|
|
|
|
2022-06-17 12:48:51 +00:00
|
|
|
|
sem::GlobalVariable* Resolver::GlobalVariable(const ast::Variable* v) {
|
2022-06-20 19:17:52 +00:00
|
|
|
|
auto* sem = As<sem::GlobalVariable>(Variable(v, /* is_global */ true));
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (!sem) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-04-06 20:18:57 +00:00
|
|
|
|
|
2022-06-17 12:48:51 +00:00
|
|
|
|
for (auto* attr : v->attributes) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
Mark(attr);
|
2021-05-13 20:32:32 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* id_attr = attr->As<ast::IdAttribute>()) {
|
|
|
|
|
// Track the constant IDs that are specified in the shader.
|
2022-07-27 20:50:40 +00:00
|
|
|
|
override_ids_.emplace(
|
|
|
|
|
OverrideId{static_cast<decltype(OverrideId::value)>(id_attr->value)}, sem);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-05-13 20:32:32 +00:00
|
|
|
|
}
|
2021-05-10 19:16:46 +00:00
|
|
|
|
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (!validator_.NoDuplicateAttributes(v->attributes)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-06-09 18:53:57 +00:00
|
|
|
|
|
2022-07-27 20:50:40 +00:00
|
|
|
|
if (!validator_.GlobalVariable(sem, override_ids_, atomic_composite_info_)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-04-06 20:18:57 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// TODO(bclayton): Call this at the end of resolve on all uniform and storage
|
|
|
|
|
// referenced structs
|
2022-08-02 15:55:35 +00:00
|
|
|
|
if (!validator_.StorageClassLayout(sem, enabled_extensions_, valid_type_storage_layouts_)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
return sem;
|
2021-07-15 19:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 17:11:56 +00:00
|
|
|
|
sem::Function* Resolver::Function(const ast::Function* decl) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
uint32_t parameter_index = 0;
|
|
|
|
|
std::unordered_map<Symbol, Source> parameter_names;
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::Vector<sem::Parameter*, 8> parameters;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
// Resolve all the parameters
|
|
|
|
|
for (auto* param : decl->params) {
|
|
|
|
|
Mark(param);
|
|
|
|
|
|
|
|
|
|
{ // Check the parameter name is unique for the function
|
|
|
|
|
auto emplaced = parameter_names.emplace(param->symbol, param->source);
|
|
|
|
|
if (!emplaced.second) {
|
|
|
|
|
auto name = builder_->Symbols().NameFor(param->symbol);
|
|
|
|
|
AddError("redefinition of parameter '" + name + "'", param->source);
|
|
|
|
|
AddNote("previous definition is here", emplaced.first->second);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
2022-06-18 17:26:40 +00:00
|
|
|
|
auto* p = Parameter(param, parameter_index++);
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (!p) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
2022-06-18 17:26:40 +00:00
|
|
|
|
if (!validator_.Parameter(decl, p)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 07:55:24 +00:00
|
|
|
|
parameters.Push(p);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-06-17 12:48:51 +00:00
|
|
|
|
auto* p_ty = const_cast<sem::Type*>(p->Type());
|
|
|
|
|
if (auto* str = p_ty->As<sem::Struct>()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
switch (decl->PipelineStage()) {
|
|
|
|
|
case ast::PipelineStage::kVertex:
|
|
|
|
|
str->AddUsage(sem::PipelineStageUsage::kVertexInput);
|
|
|
|
|
break;
|
|
|
|
|
case ast::PipelineStage::kFragment:
|
|
|
|
|
str->AddUsage(sem::PipelineStageUsage::kFragmentInput);
|
|
|
|
|
break;
|
|
|
|
|
case ast::PipelineStage::kCompute:
|
|
|
|
|
str->AddUsage(sem::PipelineStageUsage::kComputeInput);
|
|
|
|
|
break;
|
|
|
|
|
case ast::PipelineStage::kNone:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-15 19:09:25 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// Resolve the return type
|
|
|
|
|
sem::Type* return_type = nullptr;
|
|
|
|
|
if (auto* ty = decl->return_type) {
|
|
|
|
|
return_type = Type(ty);
|
|
|
|
|
if (!return_type) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return_type = builder_->create<sem::Void>();
|
2021-07-15 19:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* str = return_type->As<sem::Struct>()) {
|
|
|
|
|
if (!ApplyStorageClassUsageToType(ast::StorageClass::kNone, str, decl->source)) {
|
|
|
|
|
AddNote(
|
|
|
|
|
"while instantiating return type for " + builder_->Symbols().NameFor(decl->symbol),
|
|
|
|
|
decl->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-11-05 16:51:38 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
switch (decl->PipelineStage()) {
|
|
|
|
|
case ast::PipelineStage::kVertex:
|
|
|
|
|
str->AddUsage(sem::PipelineStageUsage::kVertexOutput);
|
|
|
|
|
break;
|
|
|
|
|
case ast::PipelineStage::kFragment:
|
|
|
|
|
str->AddUsage(sem::PipelineStageUsage::kFragmentOutput);
|
|
|
|
|
break;
|
|
|
|
|
case ast::PipelineStage::kCompute:
|
|
|
|
|
str->AddUsage(sem::PipelineStageUsage::kComputeOutput);
|
|
|
|
|
break;
|
|
|
|
|
case ast::PipelineStage::kNone:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
|
2022-07-26 07:55:24 +00:00
|
|
|
|
auto* func = builder_->create<sem::Function>(decl, return_type, std::move(parameters));
|
2022-05-01 14:40:55 +00:00
|
|
|
|
builder_->Sem().Add(decl, func);
|
2021-05-19 13:40:08 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
TINT_SCOPED_ASSIGNMENT(current_function_, func);
|
|
|
|
|
|
|
|
|
|
if (!WorkgroupSize(decl)) {
|
|
|
|
|
return nullptr;
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (decl->IsEntryPoint()) {
|
|
|
|
|
entry_points_.emplace_back(func);
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (decl->body) {
|
|
|
|
|
Mark(decl->body);
|
|
|
|
|
if (current_compound_statement_) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< "Resolver::Function() called with a current compound statement";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto* body = StatementScope(decl->body, builder_->create<sem::FunctionBlockStatement>(func),
|
|
|
|
|
[&] { return Statements(decl->body->statements); });
|
|
|
|
|
if (!body) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
func->Behaviors() = body->Behaviors();
|
|
|
|
|
if (func->Behaviors().Contains(sem::Behavior::kReturn)) {
|
|
|
|
|
// https://www.w3.org/TR/WGSL/#behaviors-rules
|
|
|
|
|
// We assign a behavior to each function: it is its body’s behavior
|
|
|
|
|
// (treating the body as a regular statement), with any "Return" replaced
|
|
|
|
|
// by "Next".
|
|
|
|
|
func->Behaviors().Remove(sem::Behavior::kReturn);
|
|
|
|
|
func->Behaviors().Add(sem::Behavior::kNext);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-28 21:24:06 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* attr : decl->attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
}
|
|
|
|
|
if (!validator_.NoDuplicateAttributes(decl->attributes)) {
|
|
|
|
|
return nullptr;
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* attr : decl->return_type_attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
}
|
|
|
|
|
if (!validator_.NoDuplicateAttributes(decl->return_type_attributes)) {
|
|
|
|
|
return nullptr;
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2021-05-19 13:40:08 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto stage = current_function_ ? current_function_->Declaration()->PipelineStage()
|
|
|
|
|
: ast::PipelineStage::kNone;
|
|
|
|
|
if (!validator_.Function(func, stage)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-11-11 19:12:36 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// If this is an entry point, mark all transitively called functions as being
|
|
|
|
|
// used by this entry point.
|
|
|
|
|
if (decl->IsEntryPoint()) {
|
|
|
|
|
for (auto* f : func->TransitivelyCalledFunctions()) {
|
|
|
|
|
const_cast<sem::Function*>(f)->AddAncestorEntryPoint(func);
|
|
|
|
|
}
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2021-07-28 21:24:06 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return func;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Resolver::WorkgroupSize(const ast::Function* func) {
|
|
|
|
|
// Set work-group size defaults.
|
|
|
|
|
sem::WorkgroupSize ws;
|
2022-06-29 14:38:15 +00:00
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
ws[i].value = 1;
|
|
|
|
|
ws[i].overridable_const = nullptr;
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
auto* attr = ast::GetAttribute<ast::WorkgroupAttribute>(func->attributes);
|
|
|
|
|
if (!attr) {
|
|
|
|
|
return true;
|
2021-05-19 13:40:08 +00:00
|
|
|
|
}
|
2021-05-19 08:15:18 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto values = attr->Values();
|
2022-07-29 14:15:01 +00:00
|
|
|
|
utils::Vector<const sem::Expression*, 3> args;
|
|
|
|
|
utils::Vector<const sem::Type*, 3> arg_tys;
|
2022-05-28 10:34:06 +00:00
|
|
|
|
|
2022-06-26 09:05:00 +00:00
|
|
|
|
constexpr const char* kErrBadExpr =
|
|
|
|
|
"workgroup_size argument must be either a literal, constant, or overridable of type "
|
|
|
|
|
"abstract-integer, i32 or u32";
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-06-29 14:38:15 +00:00
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
2022-05-28 10:34:06 +00:00
|
|
|
|
// Each argument to this attribute can either be a literal, an identifier for a module-scope
|
|
|
|
|
// constants, or nullptr if not specified.
|
|
|
|
|
auto* value = values[i];
|
|
|
|
|
if (!value) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const auto* expr = Expression(value);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!expr) {
|
2022-05-28 10:34:06 +00:00
|
|
|
|
return false;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-05-28 10:34:06 +00:00
|
|
|
|
auto* ty = expr->Type();
|
|
|
|
|
if (!ty->IsAnyOf<sem::I32, sem::U32, sem::AbstractInt>()) {
|
2022-06-26 09:05:00 +00:00
|
|
|
|
AddError(kErrBadExpr, value->source);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 14:15:01 +00:00
|
|
|
|
args.Push(expr);
|
|
|
|
|
arg_tys.Push(ty);
|
2022-05-28 10:34:06 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-07-29 14:15:01 +00:00
|
|
|
|
auto* common_ty = sem::Type::Common(arg_tys);
|
2022-05-28 10:34:06 +00:00
|
|
|
|
if (!common_ty) {
|
|
|
|
|
AddError("workgroup_size arguments must be of the same type, either i32 or u32",
|
|
|
|
|
attr->source);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If all arguments are abstract-integers, then materialize to i32.
|
|
|
|
|
if (common_ty->Is<sem::AbstractInt>()) {
|
|
|
|
|
common_ty = builder_->create<sem::I32>();
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-07-29 14:15:01 +00:00
|
|
|
|
for (size_t i = 0; i < args.Length(); i++) {
|
2022-05-28 10:34:06 +00:00
|
|
|
|
auto* materialized = Materialize(args[i], common_ty);
|
|
|
|
|
if (!materialized) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 19:07:30 +00:00
|
|
|
|
const sem::Constant* value = nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-28 10:34:06 +00:00
|
|
|
|
if (auto* user = args[i]->As<sem::VariableUser>()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// We have an variable of a module-scope constant.
|
|
|
|
|
auto* decl = user->Variable()->Declaration();
|
2022-06-29 00:55:36 +00:00
|
|
|
|
if (!decl->IsAnyOf<ast::Const, ast::Override>()) {
|
2022-06-26 09:05:00 +00:00
|
|
|
|
AddError(kErrBadExpr, values[i]->source);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Capture the constant if it is pipeline-overridable.
|
2022-06-17 12:48:51 +00:00
|
|
|
|
if (decl->Is<ast::Override>()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
ws[i].overridable_const = decl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (decl->constructor) {
|
|
|
|
|
value = sem_.Get(decl->constructor)->ConstantValue();
|
|
|
|
|
} else {
|
|
|
|
|
// No constructor means this value must be overriden by the user.
|
|
|
|
|
ws[i].value = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-05-28 10:34:06 +00:00
|
|
|
|
} else if (values[i]->Is<ast::LiteralExpression>()) {
|
|
|
|
|
value = materialized->ConstantValue();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
} else {
|
2022-06-26 09:05:00 +00:00
|
|
|
|
AddError(kErrBadExpr, values[i]->source);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< "could not resolve constant workgroup_size constant value";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// validator_.Validate and set the default value for this dimension.
|
2022-06-29 19:07:30 +00:00
|
|
|
|
if (value->As<AInt>() < 1) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
AddError("workgroup_size argument must be at least 1", values[i]->source);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 19:07:30 +00:00
|
|
|
|
ws[i].value = value->As<uint32_t>();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-11-23 21:46:48 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
current_function_->SetWorkgroupSize(std::move(ws));
|
|
|
|
|
return true;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
|
bool Resolver::Statements(const ast::StatementList& stmts) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Behaviors behaviors{sem::Behavior::kNext};
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
bool reachable = true;
|
|
|
|
|
for (auto* stmt : stmts) {
|
|
|
|
|
Mark(stmt);
|
|
|
|
|
auto* sem = Statement(stmt);
|
|
|
|
|
if (!sem) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// s1 s2:(B1∖{Next}) ∪ B2
|
|
|
|
|
sem->SetIsReachable(reachable);
|
|
|
|
|
if (reachable) {
|
|
|
|
|
behaviors = (behaviors - sem::Behavior::kNext) + sem->Behaviors();
|
|
|
|
|
}
|
|
|
|
|
reachable = reachable && sem->Behaviors().Contains(sem::Behavior::kNext);
|
2021-12-03 17:51:48 +00:00
|
|
|
|
}
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
current_statement_->Behaviors() = behaviors;
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!validator_.Statements(stmts)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-06-21 17:53:56 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return true;
|
2021-06-21 17:53:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 16:26:42 +00:00
|
|
|
|
sem::Statement* Resolver::Statement(const ast::Statement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return Switch(
|
|
|
|
|
stmt,
|
|
|
|
|
// Compound statements. These create their own sem::CompoundStatement
|
|
|
|
|
// bindings.
|
|
|
|
|
[&](const ast::BlockStatement* b) { return BlockStatement(b); },
|
|
|
|
|
[&](const ast::ForLoopStatement* l) { return ForLoopStatement(l); },
|
|
|
|
|
[&](const ast::LoopStatement* l) { return LoopStatement(l); },
|
2022-06-16 12:01:27 +00:00
|
|
|
|
[&](const ast::WhileStatement* w) { return WhileStatement(w); },
|
2022-05-01 14:40:55 +00:00
|
|
|
|
[&](const ast::IfStatement* i) { return IfStatement(i); },
|
|
|
|
|
[&](const ast::SwitchStatement* s) { return SwitchStatement(s); },
|
|
|
|
|
|
|
|
|
|
// Non-Compound statements
|
|
|
|
|
[&](const ast::AssignmentStatement* a) { return AssignmentStatement(a); },
|
|
|
|
|
[&](const ast::BreakStatement* b) { return BreakStatement(b); },
|
|
|
|
|
[&](const ast::CallStatement* c) { return CallStatement(c); },
|
|
|
|
|
[&](const ast::CompoundAssignmentStatement* c) { return CompoundAssignmentStatement(c); },
|
|
|
|
|
[&](const ast::ContinueStatement* c) { return ContinueStatement(c); },
|
|
|
|
|
[&](const ast::DiscardStatement* d) { return DiscardStatement(d); },
|
|
|
|
|
[&](const ast::FallthroughStatement* f) { return FallthroughStatement(f); },
|
|
|
|
|
[&](const ast::IncrementDecrementStatement* i) { return IncrementDecrementStatement(i); },
|
|
|
|
|
[&](const ast::ReturnStatement* r) { return ReturnStatement(r); },
|
|
|
|
|
[&](const ast::VariableDeclStatement* v) { return VariableDeclStatement(v); },
|
|
|
|
|
|
|
|
|
|
// Error cases
|
|
|
|
|
[&](const ast::CaseStatement*) {
|
|
|
|
|
AddError("case statement can only be used inside a switch statement", stmt->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](Default) {
|
|
|
|
|
AddError("unknown statement type: " + std::string(stmt->TypeInfo().name), stmt->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
2020-04-06 21:07:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 15:23:52 +00:00
|
|
|
|
sem::CaseStatement* Resolver::CaseStatement(const ast::CaseStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::CaseStatement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
2022-05-20 12:28:00 +00:00
|
|
|
|
sem->Selectors().reserve(stmt->selectors.size());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* sel : stmt->selectors) {
|
2022-05-20 12:28:00 +00:00
|
|
|
|
auto* expr = Expression(sel);
|
|
|
|
|
if (!expr) {
|
2022-05-04 22:18:49 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-05-20 12:28:00 +00:00
|
|
|
|
sem->Selectors().emplace_back(expr);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
Mark(stmt->body);
|
|
|
|
|
auto* body = BlockStatement(stmt->body);
|
|
|
|
|
if (!body) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->SetBlock(body);
|
|
|
|
|
sem->Behaviors() = body->Behaviors();
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2021-03-09 15:17:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 16:26:42 +00:00
|
|
|
|
sem::IfStatement* Resolver::IfStatement(const ast::IfStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::IfStatement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto* cond = Expression(stmt->condition);
|
|
|
|
|
if (!cond) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->SetCondition(cond);
|
|
|
|
|
sem->Behaviors() = cond->Behaviors();
|
|
|
|
|
sem->Behaviors().Remove(sem::Behavior::kNext);
|
|
|
|
|
|
|
|
|
|
Mark(stmt->body);
|
|
|
|
|
auto* body = builder_->create<sem::BlockStatement>(stmt->body, current_compound_statement_,
|
|
|
|
|
current_function_);
|
|
|
|
|
if (!StatementScope(stmt->body, body, [&] { return Statements(stmt->body->statements); })) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->Behaviors().Add(body->Behaviors());
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (stmt->else_statement) {
|
|
|
|
|
Mark(stmt->else_statement);
|
|
|
|
|
auto* else_sem = Statement(stmt->else_statement);
|
|
|
|
|
if (!else_sem) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->Behaviors().Add(else_sem->Behaviors());
|
|
|
|
|
} else {
|
|
|
|
|
// https://www.w3.org/TR/WGSL/#behaviors-rules
|
|
|
|
|
// if statements without an else branch are treated as if they had an
|
|
|
|
|
// empty else branch (which adds Next to their behavior)
|
|
|
|
|
sem->Behaviors().Add(sem::Behavior::kNext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return validator_.IfStatement(sem);
|
|
|
|
|
});
|
2021-07-14 09:44:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 16:26:42 +00:00
|
|
|
|
sem::BlockStatement* Resolver::BlockStatement(const ast::BlockStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem = builder_->create<sem::BlockStatement>(
|
|
|
|
|
stmt->As<ast::BlockStatement>(), current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] { return Statements(stmt->statements); });
|
2021-07-14 09:44:41 +00:00
|
|
|
|
}
|
2021-05-15 14:48:46 +00:00
|
|
|
|
|
2021-11-26 16:26:42 +00:00
|
|
|
|
sem::LoopStatement* Resolver::LoopStatement(const ast::LoopStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::LoopStatement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
Mark(stmt->body);
|
|
|
|
|
|
|
|
|
|
auto* body = builder_->create<sem::LoopBlockStatement>(
|
|
|
|
|
stmt->body, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt->body, body, [&] {
|
|
|
|
|
if (!Statements(stmt->body->statements)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
auto& behaviors = sem->Behaviors();
|
|
|
|
|
behaviors = body->Behaviors();
|
|
|
|
|
|
|
|
|
|
if (stmt->continuing) {
|
|
|
|
|
Mark(stmt->continuing);
|
2022-05-11 13:50:33 +00:00
|
|
|
|
auto* continuing = StatementScope(
|
|
|
|
|
stmt->continuing,
|
|
|
|
|
builder_->create<sem::LoopContinuingBlockStatement>(
|
|
|
|
|
stmt->continuing, current_compound_statement_, current_function_),
|
|
|
|
|
[&] { return Statements(stmt->continuing->statements); });
|
|
|
|
|
if (!continuing) {
|
|
|
|
|
return false;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-05-11 13:50:33 +00:00
|
|
|
|
behaviors.Add(continuing->Behaviors());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (behaviors.Contains(sem::Behavior::kBreak)) { // Does the loop exit?
|
|
|
|
|
behaviors.Add(sem::Behavior::kNext);
|
|
|
|
|
} else {
|
|
|
|
|
behaviors.Remove(sem::Behavior::kNext);
|
|
|
|
|
}
|
|
|
|
|
behaviors.Remove(sem::Behavior::kBreak, sem::Behavior::kContinue);
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.LoopStatement(sem);
|
|
|
|
|
});
|
2021-07-14 09:44:41 +00:00
|
|
|
|
});
|
2021-05-15 14:48:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::ForLoopStatement* Resolver::ForLoopStatement(const ast::ForLoopStatement* stmt) {
|
|
|
|
|
auto* sem = builder_->create<sem::ForLoopStatement>(stmt, current_compound_statement_,
|
|
|
|
|
current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto& behaviors = sem->Behaviors();
|
|
|
|
|
if (auto* initializer = stmt->initializer) {
|
|
|
|
|
Mark(initializer);
|
|
|
|
|
auto* init = Statement(initializer);
|
|
|
|
|
if (!init) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
behaviors.Add(init->Behaviors());
|
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* cond_expr = stmt->condition) {
|
|
|
|
|
auto* cond = Expression(cond_expr);
|
|
|
|
|
if (!cond) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->SetCondition(cond);
|
|
|
|
|
behaviors.Add(cond->Behaviors());
|
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* continuing = stmt->continuing) {
|
|
|
|
|
Mark(continuing);
|
|
|
|
|
auto* cont = Statement(continuing);
|
|
|
|
|
if (!cont) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
behaviors.Add(cont->Behaviors());
|
|
|
|
|
}
|
2021-07-02 19:27:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
Mark(stmt->body);
|
2021-07-14 09:44:41 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* body = builder_->create<sem::LoopBlockStatement>(
|
|
|
|
|
stmt->body, current_compound_statement_, current_function_);
|
|
|
|
|
if (!StatementScope(stmt->body, body, [&] { return Statements(stmt->body->statements); })) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
behaviors.Add(body->Behaviors());
|
|
|
|
|
if (stmt->condition || behaviors.Contains(sem::Behavior::kBreak)) { // Does the loop exit?
|
|
|
|
|
behaviors.Add(sem::Behavior::kNext);
|
|
|
|
|
} else {
|
|
|
|
|
behaviors.Remove(sem::Behavior::kNext);
|
|
|
|
|
}
|
|
|
|
|
behaviors.Remove(sem::Behavior::kBreak, sem::Behavior::kContinue);
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.ForLoopStatement(sem);
|
|
|
|
|
});
|
2021-07-02 19:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 12:01:27 +00:00
|
|
|
|
sem::WhileStatement* Resolver::WhileStatement(const ast::WhileStatement* stmt) {
|
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::WhileStatement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto& behaviors = sem->Behaviors();
|
|
|
|
|
|
|
|
|
|
auto* cond = Expression(stmt->condition);
|
|
|
|
|
if (!cond) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->SetCondition(cond);
|
|
|
|
|
behaviors.Add(cond->Behaviors());
|
|
|
|
|
|
|
|
|
|
Mark(stmt->body);
|
|
|
|
|
|
|
|
|
|
auto* body = builder_->create<sem::LoopBlockStatement>(
|
|
|
|
|
stmt->body, current_compound_statement_, current_function_);
|
|
|
|
|
if (!StatementScope(stmt->body, body, [&] { return Statements(stmt->body->statements); })) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
behaviors.Add(body->Behaviors());
|
|
|
|
|
// Always consider the while as having a 'next' behaviour because it has
|
|
|
|
|
// a condition. We don't check if the condition will terminate but it isn't
|
|
|
|
|
// valid to have an infinite loop in a WGSL program, so a non-terminating
|
|
|
|
|
// condition is already an invalid program.
|
|
|
|
|
behaviors.Add(sem::Behavior::kNext);
|
|
|
|
|
behaviors.Remove(sem::Behavior::kBreak, sem::Behavior::kContinue);
|
|
|
|
|
|
|
|
|
|
return validator_.WhileStatement(sem);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 16:51:38 +00:00
|
|
|
|
sem::Expression* Resolver::Expression(const ast::Expression* root) {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::Vector<const ast::Expression*, 128> sorted;
|
2022-05-17 15:01:42 +00:00
|
|
|
|
constexpr size_t kMaxExpressionDepth = 512U;
|
|
|
|
|
bool failed = false;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!ast::TraverseExpressions<ast::TraverseOrder::RightToLeft>(
|
2022-05-17 15:01:42 +00:00
|
|
|
|
root, diagnostics_, [&](const ast::Expression* expr, size_t depth) {
|
|
|
|
|
if (depth > kMaxExpressionDepth) {
|
|
|
|
|
AddError(
|
|
|
|
|
"reached max expression depth of " + std::to_string(kMaxExpressionDepth),
|
|
|
|
|
expr->source);
|
|
|
|
|
failed = true;
|
|
|
|
|
return ast::TraverseAction::Stop;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!Mark(expr)) {
|
2022-05-17 15:01:42 +00:00
|
|
|
|
failed = true;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return ast::TraverseAction::Stop;
|
|
|
|
|
}
|
2022-07-26 07:55:24 +00:00
|
|
|
|
sorted.Push(expr);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return ast::TraverseAction::Descend;
|
|
|
|
|
})) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 15:01:42 +00:00
|
|
|
|
if (failed) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-09-08 15:18:36 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* expr : utils::Reverse(sorted)) {
|
|
|
|
|
auto* sem_expr = Switch(
|
|
|
|
|
expr,
|
|
|
|
|
[&](const ast::IndexAccessorExpression* array) -> sem::Expression* {
|
|
|
|
|
return IndexAccessor(array);
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::BinaryExpression* bin_op) -> sem::Expression* { return Binary(bin_op); },
|
|
|
|
|
[&](const ast::BitcastExpression* bitcast) -> sem::Expression* {
|
|
|
|
|
return Bitcast(bitcast);
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::CallExpression* call) -> sem::Expression* { return Call(call); },
|
|
|
|
|
[&](const ast::IdentifierExpression* ident) -> sem::Expression* {
|
|
|
|
|
return Identifier(ident);
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::LiteralExpression* literal) -> sem::Expression* {
|
|
|
|
|
return Literal(literal);
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::MemberAccessorExpression* member) -> sem::Expression* {
|
|
|
|
|
return MemberAccessor(member);
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::UnaryOpExpression* unary) -> sem::Expression* { return UnaryOp(unary); },
|
|
|
|
|
[&](const ast::PhonyExpression*) -> sem::Expression* {
|
|
|
|
|
return builder_->create<sem::Expression>(expr, builder_->create<sem::Void>(),
|
2022-07-15 23:46:31 +00:00
|
|
|
|
sem::EvaluationStage::kRuntime,
|
2022-06-29 19:07:30 +00:00
|
|
|
|
current_statement_,
|
|
|
|
|
/* constant_value */ nullptr,
|
2022-05-01 14:40:55 +00:00
|
|
|
|
/* has_side_effects */ false);
|
|
|
|
|
},
|
|
|
|
|
[&](Default) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< "unhandled expression type: " << expr->TypeInfo().name;
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
|
|
|
|
if (!sem_expr) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder_->Sem().Add(expr, sem_expr);
|
|
|
|
|
if (expr == root) {
|
|
|
|
|
return sem_expr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "Expression() did not find root node";
|
2021-11-22 11:44:57 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-11-22 11:44:57 +00:00
|
|
|
|
|
2022-08-01 17:36:54 +00:00
|
|
|
|
const sem::Type* Resolver::ConcreteType(const sem::Type* ty,
|
|
|
|
|
const sem::Type* target_ty,
|
|
|
|
|
const Source& source) {
|
2022-05-24 21:42:03 +00:00
|
|
|
|
auto i32 = [&] { return builder_->create<sem::I32>(); };
|
|
|
|
|
auto f32 = [&] { return builder_->create<sem::F32>(); };
|
|
|
|
|
auto i32v = [&](uint32_t width) { return builder_->create<sem::Vector>(i32(), width); };
|
|
|
|
|
auto f32v = [&](uint32_t width) { return builder_->create<sem::Vector>(f32(), width); };
|
|
|
|
|
auto f32m = [&](uint32_t columns, uint32_t rows) {
|
2022-05-28 05:28:11 +00:00
|
|
|
|
return builder_->create<sem::Matrix>(f32v(rows), columns);
|
2022-05-24 21:42:03 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-07-29 13:19:12 +00:00
|
|
|
|
return Switch(
|
|
|
|
|
ty, //
|
|
|
|
|
[&](const sem::AbstractInt*) { return target_ty ? target_ty : i32(); },
|
|
|
|
|
[&](const sem::AbstractFloat*) { return target_ty ? target_ty : f32(); },
|
2022-05-24 21:42:03 +00:00
|
|
|
|
[&](const sem::Vector* v) {
|
|
|
|
|
return Switch(
|
|
|
|
|
v->type(), //
|
2022-07-29 13:19:12 +00:00
|
|
|
|
[&](const sem::AbstractInt*) { return target_ty ? target_ty : i32v(v->Width()); },
|
2022-05-24 21:42:03 +00:00
|
|
|
|
[&](const sem::AbstractFloat*) {
|
2022-07-29 13:19:12 +00:00
|
|
|
|
return target_ty ? target_ty : f32v(v->Width());
|
|
|
|
|
});
|
2022-05-24 21:42:03 +00:00
|
|
|
|
},
|
|
|
|
|
[&](const sem::Matrix* m) {
|
2022-07-29 13:19:12 +00:00
|
|
|
|
return Switch(m->type(), //
|
|
|
|
|
[&](const sem::AbstractFloat*) {
|
|
|
|
|
return target_ty ? target_ty : f32m(m->columns(), m->rows());
|
|
|
|
|
});
|
2022-08-01 17:36:54 +00:00
|
|
|
|
},
|
|
|
|
|
[&](const sem::Array* a) -> const sem::Type* {
|
|
|
|
|
const sem::Type* target_el_ty = nullptr;
|
|
|
|
|
if (auto* target_arr_ty = As<sem::Array>(target_ty)) {
|
|
|
|
|
target_el_ty = target_arr_ty->ElemType();
|
|
|
|
|
}
|
|
|
|
|
if (auto* el_ty = ConcreteType(a->ElemType(), target_el_ty, source)) {
|
|
|
|
|
return Array(source, el_ty, a->Count(), /* explicit_stride */ 0);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
2022-07-29 13:19:12 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sem::Expression* Resolver::Materialize(const sem::Expression* expr,
|
|
|
|
|
const sem::Type* target_type /* = nullptr */) {
|
|
|
|
|
if (!expr) {
|
|
|
|
|
// Allow for Materialize(Expression(blah)), where failures pass through Materialize()
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* decl = expr->Declaration();
|
|
|
|
|
|
2022-08-01 17:36:54 +00:00
|
|
|
|
auto* concrete_ty = ConcreteType(expr->Type(), target_type, decl->source);
|
2022-07-29 13:19:12 +00:00
|
|
|
|
if (!concrete_ty) {
|
|
|
|
|
return expr; // Does not require materialization
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* src_ty = expr->Type();
|
|
|
|
|
if (!validator_.Materialize(concrete_ty, src_ty, decl->source)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto expr_val = expr->ConstantValue();
|
|
|
|
|
if (!expr_val) {
|
|
|
|
|
TINT_ICE(Resolver, builder_->Diagnostics())
|
|
|
|
|
<< decl->source << "Materialize(" << decl->TypeInfo().name
|
|
|
|
|
<< ") called on expression with no constant value";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto materialized_val = const_eval_.Convert(concrete_ty, expr_val, decl->source);
|
|
|
|
|
if (!materialized_val) {
|
|
|
|
|
// ConvertValue() has already failed and raised an diagnostic error.
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!materialized_val.Get()) {
|
|
|
|
|
TINT_ICE(Resolver, builder_->Diagnostics())
|
|
|
|
|
<< decl->source << "ConvertValue(" << builder_->FriendlyName(expr_val->Type()) << " -> "
|
|
|
|
|
<< builder_->FriendlyName(concrete_ty) << ") returned invalid value";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
auto* m = builder_->create<sem::Materialize>(expr, current_statement_, materialized_val.Get());
|
|
|
|
|
m->Behaviors() = expr->Behaviors();
|
|
|
|
|
builder_->Sem().Replace(decl, m);
|
|
|
|
|
return m;
|
2022-05-24 21:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 07:55:24 +00:00
|
|
|
|
bool Resolver::MaterializeArguments(utils::VectorRef<const sem::Expression*> args,
|
2022-05-24 21:42:03 +00:00
|
|
|
|
const sem::CallTarget* target) {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
for (size_t i = 0, n = std::min(args.Length(), target->Parameters().Length()); i < n; i++) {
|
2022-05-24 21:42:03 +00:00
|
|
|
|
const auto* param_ty = target->Parameters()[i]->Type();
|
|
|
|
|
if (ShouldMaterializeArgument(param_ty)) {
|
|
|
|
|
auto* materialized = Materialize(args[i], param_ty);
|
|
|
|
|
if (!materialized) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
args[i] = materialized;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Resolver::ShouldMaterializeArgument(const sem::Type* parameter_ty) const {
|
|
|
|
|
const auto* param_el_ty = sem::Type::ElementOf(parameter_ty);
|
|
|
|
|
return param_el_ty && !param_el_ty->Is<sem::AbstractNumeric>();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Expression* Resolver::IndexAccessor(const ast::IndexAccessorExpression* expr) {
|
2022-05-31 15:22:21 +00:00
|
|
|
|
auto* idx = Materialize(sem_.Get(expr->index));
|
|
|
|
|
if (!idx) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-07-21 15:25:35 +00:00
|
|
|
|
const auto* obj = sem_.Get(expr->object);
|
|
|
|
|
if (idx->Stage() != sem::EvaluationStage::kConstant) {
|
|
|
|
|
// If the index is non-constant, then the resulting expression is non-constant, so we'll
|
|
|
|
|
// have to materialize the object. For example, consider:
|
|
|
|
|
// vec2(1, 2)[runtime-index]
|
|
|
|
|
obj = Materialize(obj);
|
|
|
|
|
}
|
2022-08-01 18:46:54 +00:00
|
|
|
|
if (!obj) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* obj_raw_ty = obj->Type();
|
|
|
|
|
auto* obj_ty = obj_raw_ty->UnwrapRef();
|
|
|
|
|
auto* ty = Switch(
|
|
|
|
|
obj_ty, //
|
|
|
|
|
[&](const sem::Array* arr) { return arr->ElemType(); },
|
|
|
|
|
[&](const sem::Vector* vec) { return vec->type(); },
|
|
|
|
|
[&](const sem::Matrix* mat) {
|
|
|
|
|
return builder_->create<sem::Vector>(mat->type(), mat->rows());
|
2022-02-14 10:05:16 +00:00
|
|
|
|
},
|
|
|
|
|
[&](Default) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
AddError("cannot index type '" + sem_.TypeNameOf(obj_ty) + "'", expr->source);
|
|
|
|
|
return nullptr;
|
2022-02-14 10:05:16 +00:00
|
|
|
|
});
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (ty == nullptr) {
|
|
|
|
|
return nullptr;
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* idx_ty = idx->Type()->UnwrapRef();
|
|
|
|
|
if (!idx_ty->IsAnyOf<sem::I32, sem::U32>()) {
|
|
|
|
|
AddError("index must be of type 'i32' or 'u32', found: '" + sem_.TypeNameOf(idx_ty) + "'",
|
|
|
|
|
idx->Declaration()->source);
|
|
|
|
|
return nullptr;
|
2021-09-08 15:18:36 +00:00
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// If we're extracting from a reference, we return a reference.
|
|
|
|
|
if (auto* ref = obj_raw_ty->As<sem::Reference>()) {
|
|
|
|
|
ty = builder_->create<sem::Reference>(ty, ref->StorageClass(), ref->Access());
|
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto stage = sem::EarliestStage(obj->Stage(), idx->Stage());
|
2022-07-15 14:14:09 +00:00
|
|
|
|
auto val = const_eval_.Index(obj, idx);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
bool has_side_effects = idx->HasSideEffects() || obj->HasSideEffects();
|
2022-06-24 20:34:00 +00:00
|
|
|
|
auto* sem = builder_->create<sem::IndexAccessorExpression>(
|
2022-07-15 23:46:31 +00:00
|
|
|
|
expr, ty, stage, obj, idx, current_statement_, std::move(val), has_side_effects,
|
2022-06-24 20:34:00 +00:00
|
|
|
|
obj->SourceVariable());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem->Behaviors() = idx->Behaviors() + obj->Behaviors();
|
|
|
|
|
return sem;
|
2020-04-07 12:57:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 16:51:38 +00:00
|
|
|
|
sem::Expression* Resolver::Bitcast(const ast::BitcastExpression* expr) {
|
2022-05-28 07:09:17 +00:00
|
|
|
|
auto* inner = Materialize(sem_.Get(expr->expr));
|
|
|
|
|
if (!inner) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* ty = Type(expr->type);
|
|
|
|
|
if (!ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-11-05 16:51:38 +00:00
|
|
|
|
|
2022-07-15 14:14:09 +00:00
|
|
|
|
auto val = const_eval_.Bitcast(ty, inner);
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto stage = sem::EvaluationStage::kRuntime; // TODO(crbug.com/tint/1581)
|
|
|
|
|
auto* sem = builder_->create<sem::Expression>(expr, ty, stage, current_statement_,
|
|
|
|
|
std::move(val), inner->HasSideEffects());
|
2021-12-03 21:29:13 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem->Behaviors() = inner->Behaviors();
|
2021-12-03 21:29:13 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!validator_.Bitcast(expr, ty)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-12-03 21:29:13 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return sem;
|
2020-04-07 12:57:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:45:50 +00:00
|
|
|
|
sem::Call* Resolver::Call(const ast::CallExpression* expr) {
|
2022-05-16 20:54:42 +00:00
|
|
|
|
// A CallExpression can resolve to one of:
|
|
|
|
|
// * A function call.
|
|
|
|
|
// * A builtin call.
|
|
|
|
|
// * A type constructor.
|
|
|
|
|
// * A type conversion.
|
|
|
|
|
|
|
|
|
|
// Resolve all of the arguments, their types and the set of behaviors.
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::Vector<const sem::Expression*, 8> args;
|
|
|
|
|
args.Reserve(expr->args.size());
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto args_stage = sem::EvaluationStage::kConstant;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Behaviors arg_behaviors;
|
|
|
|
|
for (size_t i = 0; i < expr->args.size(); i++) {
|
|
|
|
|
auto* arg = sem_.Get(expr->args[i]);
|
|
|
|
|
if (!arg) {
|
2021-12-09 14:37:37 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-07-26 07:55:24 +00:00
|
|
|
|
args.Push(arg);
|
2022-07-15 23:46:31 +00:00
|
|
|
|
args_stage = sem::EarliestStage(args_stage, arg->Stage());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
arg_behaviors.Add(arg->Behaviors());
|
|
|
|
|
}
|
|
|
|
|
arg_behaviors.Remove(sem::Behavior::kNext);
|
|
|
|
|
|
2022-05-16 20:54:42 +00:00
|
|
|
|
// Did any arguments have side effects?
|
|
|
|
|
bool has_side_effects =
|
|
|
|
|
std::any_of(args.begin(), args.end(), [](auto* e) { return e->HasSideEffects(); });
|
|
|
|
|
|
|
|
|
|
// ct_ctor_or_conv is a helper for building either a sem::TypeConstructor or sem::TypeConversion
|
|
|
|
|
// call for a CtorConvIntrinsic with an optional template argument type.
|
|
|
|
|
auto ct_ctor_or_conv = [&](CtorConvIntrinsic ty, const sem::Type* template_arg) -> sem::Call* {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
auto arg_tys = utils::Transform(args, [](auto* arg) { return arg->Type(); });
|
2022-07-15 22:27:50 +00:00
|
|
|
|
auto ctor_or_conv = intrinsic_table_->Lookup(ty, template_arg, arg_tys, expr->source);
|
|
|
|
|
if (!ctor_or_conv.target) {
|
2022-05-16 20:54:42 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-07-15 22:27:50 +00:00
|
|
|
|
if (!MaterializeArguments(args, ctor_or_conv.target)) {
|
2022-05-24 21:42:03 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-07-15 22:27:50 +00:00
|
|
|
|
const sem::Constant* value = nullptr;
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto stage = sem::EarliestStage(ctor_or_conv.target->Stage(), args_stage);
|
|
|
|
|
if (stage == sem::EvaluationStage::kConstant) {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
value =
|
|
|
|
|
(const_eval_.*ctor_or_conv.const_eval_fn)(ctor_or_conv.target->ReturnType(), args);
|
2022-07-15 22:27:50 +00:00
|
|
|
|
}
|
2022-07-15 23:46:31 +00:00
|
|
|
|
return builder_->create<sem::Call>(expr, ctor_or_conv.target, stage, std::move(args),
|
2022-07-15 22:27:50 +00:00
|
|
|
|
current_statement_, value, has_side_effects);
|
2022-05-16 20:54:42 +00:00
|
|
|
|
};
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-07-15 23:54:10 +00:00
|
|
|
|
// arr_or_str_ctor is a helper for building a sem::TypeConstructor for an array or structure
|
2022-07-15 23:46:31 +00:00
|
|
|
|
// constructor call target.
|
|
|
|
|
auto arr_or_str_ctor = [&](const sem::Type* ty,
|
|
|
|
|
const sem::CallTarget* call_target) -> sem::Call* {
|
|
|
|
|
if (!MaterializeArguments(args, call_target)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto stage = args_stage; // The evaluation stage of the call
|
|
|
|
|
const sem::Constant* value = nullptr; // The constant value for the call
|
|
|
|
|
if (stage == sem::EvaluationStage::kConstant) {
|
|
|
|
|
value = const_eval_.ArrayOrStructCtor(ty, args);
|
|
|
|
|
if (!value) {
|
|
|
|
|
// Constant evaluation failed.
|
|
|
|
|
// Can happen for expressions that will fail validation (later).
|
2022-07-15 23:54:10 +00:00
|
|
|
|
// Use the kRuntime EvaluationStage, as kConstant will trigger an assertion in the
|
|
|
|
|
// sem::Expression constructor, which checks that kConstant is paired with a
|
|
|
|
|
// constant value.
|
2022-07-15 23:46:31 +00:00
|
|
|
|
stage = sem::EvaluationStage::kRuntime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder_->create<sem::Call>(expr, call_target, stage, std::move(args),
|
2022-07-15 23:54:10 +00:00
|
|
|
|
current_statement_, value, has_side_effects);
|
2022-07-15 23:46:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-07-15 23:54:10 +00:00
|
|
|
|
// ty_ctor_or_conv is a helper for building either a sem::TypeConstructor or sem::TypeConversion
|
2022-05-16 20:54:42 +00:00
|
|
|
|
// call for the given semantic type.
|
|
|
|
|
auto ty_ctor_or_conv = [&](const sem::Type* ty) {
|
|
|
|
|
return Switch(
|
|
|
|
|
ty, //
|
|
|
|
|
[&](const sem::Vector* v) {
|
|
|
|
|
return ct_ctor_or_conv(VectorCtorConvIntrinsic(v->Width()), v->type());
|
|
|
|
|
},
|
|
|
|
|
[&](const sem::Matrix* m) {
|
|
|
|
|
return ct_ctor_or_conv(MatrixCtorConvIntrinsic(m->columns(), m->rows()), m->type());
|
|
|
|
|
},
|
|
|
|
|
[&](const sem::I32*) { return ct_ctor_or_conv(CtorConvIntrinsic::kI32, nullptr); },
|
|
|
|
|
[&](const sem::U32*) { return ct_ctor_or_conv(CtorConvIntrinsic::kU32, nullptr); },
|
2022-06-28 14:03:36 +00:00
|
|
|
|
[&](const sem::F16*) { return ct_ctor_or_conv(CtorConvIntrinsic::kF16, nullptr); },
|
2022-05-16 20:54:42 +00:00
|
|
|
|
[&](const sem::F32*) { return ct_ctor_or_conv(CtorConvIntrinsic::kF32, nullptr); },
|
|
|
|
|
[&](const sem::Bool*) { return ct_ctor_or_conv(CtorConvIntrinsic::kBool, nullptr); },
|
2022-05-19 20:19:49 +00:00
|
|
|
|
[&](const sem::Array* arr) -> sem::Call* {
|
|
|
|
|
auto* call_target = utils::GetOrCreate(
|
2022-07-26 07:55:24 +00:00
|
|
|
|
array_ctors_, ArrayConstructorSig{{arr, args.Length(), args_stage}},
|
2022-05-19 20:19:49 +00:00
|
|
|
|
[&]() -> sem::TypeConstructor* {
|
2022-08-01 17:36:54 +00:00
|
|
|
|
auto params = utils::Transform(args, [&](auto, size_t i) {
|
|
|
|
|
return builder_->create<sem::Parameter>(
|
|
|
|
|
nullptr, // declaration
|
|
|
|
|
static_cast<uint32_t>(i), // index
|
|
|
|
|
arr->ElemType(), // type
|
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
|
ast::Access::kUndefined);
|
|
|
|
|
});
|
2022-07-15 23:46:31 +00:00
|
|
|
|
return builder_->create<sem::TypeConstructor>(arr, std::move(params),
|
|
|
|
|
args_stage);
|
2022-05-19 20:19:49 +00:00
|
|
|
|
});
|
2022-08-01 15:19:54 +00:00
|
|
|
|
|
|
|
|
|
auto* call = arr_or_str_ctor(arr, call_target);
|
|
|
|
|
if (!call) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validation must occur after argument materialization in arr_or_str_ctor().
|
|
|
|
|
if (!validator_.ArrayConstructor(expr, arr)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return call;
|
2022-05-19 20:19:49 +00:00
|
|
|
|
},
|
|
|
|
|
[&](const sem::Struct* str) -> sem::Call* {
|
|
|
|
|
auto* call_target = utils::GetOrCreate(
|
2022-07-26 07:55:24 +00:00
|
|
|
|
struct_ctors_, StructConstructorSig{{str, args.Length(), args_stage}},
|
2022-05-19 20:19:49 +00:00
|
|
|
|
[&]() -> sem::TypeConstructor* {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::Vector<const sem::Parameter*, 8> params;
|
|
|
|
|
params.Resize(std::min(args.Length(), str->Members().size()));
|
|
|
|
|
for (size_t i = 0, n = params.Length(); i < n; i++) {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
params[i] = builder_->create<sem::Parameter>(
|
|
|
|
|
nullptr, // declaration
|
|
|
|
|
static_cast<uint32_t>(i), // index
|
|
|
|
|
str->Members()[i]->Type(), // type
|
|
|
|
|
ast::StorageClass::kNone, // storage_class
|
|
|
|
|
ast::Access::kUndefined); // access
|
|
|
|
|
}
|
2022-07-15 23:46:31 +00:00
|
|
|
|
return builder_->create<sem::TypeConstructor>(str, std::move(params),
|
|
|
|
|
args_stage);
|
2022-05-19 20:19:49 +00:00
|
|
|
|
});
|
2022-08-01 15:19:54 +00:00
|
|
|
|
|
|
|
|
|
auto* call = arr_or_str_ctor(str, call_target);
|
|
|
|
|
if (!call) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validation must occur after argument materialization in arr_or_str_ctor().
|
|
|
|
|
if (!validator_.StructureConstructor(expr, str)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return call;
|
2022-05-19 20:19:49 +00:00
|
|
|
|
},
|
2022-05-16 20:54:42 +00:00
|
|
|
|
[&](Default) {
|
|
|
|
|
AddError("type is not constructible", expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-16 20:54:42 +00:00
|
|
|
|
// ast::CallExpression has a target which is either an ast::Type or an ast::IdentifierExpression
|
|
|
|
|
sem::Call* call = nullptr;
|
|
|
|
|
if (expr->target.type) {
|
|
|
|
|
// ast::CallExpression has an ast::Type as the target.
|
|
|
|
|
// This call is either a type constructor or type conversion.
|
|
|
|
|
call = Switch(
|
|
|
|
|
expr->target.type,
|
|
|
|
|
[&](const ast::Vector* v) -> sem::Call* {
|
|
|
|
|
Mark(v);
|
|
|
|
|
// vector element type must be inferred if it was not specified.
|
|
|
|
|
sem::Type* template_arg = nullptr;
|
|
|
|
|
if (v->type) {
|
|
|
|
|
template_arg = Type(v->type);
|
|
|
|
|
if (!template_arg) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-16 20:54:42 +00:00
|
|
|
|
if (auto* c = ct_ctor_or_conv(VectorCtorConvIntrinsic(v->width), template_arg)) {
|
|
|
|
|
builder_->Sem().Add(expr->target.type, c->Target()->ReturnType());
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](const ast::Matrix* m) -> sem::Call* {
|
|
|
|
|
Mark(m);
|
|
|
|
|
// matrix element type must be inferred if it was not specified.
|
|
|
|
|
sem::Type* template_arg = nullptr;
|
|
|
|
|
if (m->type) {
|
|
|
|
|
template_arg = Type(m->type);
|
|
|
|
|
if (!template_arg) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-16 20:54:42 +00:00
|
|
|
|
if (auto* c = ct_ctor_or_conv(MatrixCtorConvIntrinsic(m->columns, m->rows),
|
|
|
|
|
template_arg)) {
|
|
|
|
|
builder_->Sem().Add(expr->target.type, c->Target()->ReturnType());
|
|
|
|
|
return c;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
2022-05-16 20:54:42 +00:00
|
|
|
|
},
|
2022-08-01 17:36:54 +00:00
|
|
|
|
[&](const ast::Array* a) -> sem::Call* {
|
|
|
|
|
Mark(a);
|
|
|
|
|
// array element type must be inferred if it was not specified.
|
|
|
|
|
auto el_count = static_cast<uint32_t>(args.Length());
|
|
|
|
|
const sem::Type* el_ty = nullptr;
|
|
|
|
|
if (a->type) {
|
|
|
|
|
el_ty = Type(a->type);
|
|
|
|
|
if (!el_ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!a->count) {
|
|
|
|
|
AddError("cannot construct a runtime-sized array", expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (auto count = ArrayCount(a->count)) {
|
|
|
|
|
el_count = count.Get();
|
|
|
|
|
} else {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
// Note: validation later will detect any mismatches between explicit array
|
|
|
|
|
// size and number of constructor expressions.
|
|
|
|
|
} else {
|
|
|
|
|
auto arg_tys =
|
|
|
|
|
utils::Transform(args, [](auto* arg) { return arg->Type()->UnwrapRef(); });
|
|
|
|
|
el_ty = sem::Type::Common(arg_tys);
|
|
|
|
|
if (!el_ty) {
|
|
|
|
|
AddError(
|
|
|
|
|
"cannot infer common array element type from constructor arguments",
|
|
|
|
|
expr->source);
|
|
|
|
|
std::unordered_set<const sem::Type*> types;
|
|
|
|
|
for (size_t i = 0; i < args.Length(); i++) {
|
|
|
|
|
if (types.emplace(args[i]->Type()).second) {
|
|
|
|
|
AddNote("argument " + std::to_string(i) + " is of type '" +
|
|
|
|
|
sem_.TypeNameOf(args[i]->Type()) + "'",
|
|
|
|
|
args[i]->Declaration()->source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint32_t explicit_stride = 0;
|
|
|
|
|
if (!ArrayAttributes(a->attributes, el_ty, explicit_stride)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* arr = Array(a->source, el_ty, el_count, explicit_stride);
|
|
|
|
|
if (!arr) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
builder_->Sem().Add(a, arr);
|
|
|
|
|
|
|
|
|
|
return ty_ctor_or_conv(arr);
|
|
|
|
|
},
|
2022-05-16 20:54:42 +00:00
|
|
|
|
[&](const ast::Type* ast) -> sem::Call* {
|
|
|
|
|
// Handler for AST types that do not have an optional element type.
|
|
|
|
|
if (auto* ty = Type(ast)) {
|
|
|
|
|
return ty_ctor_or_conv(ty);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](Default) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< expr->source << " unhandled CallExpression target:\n"
|
|
|
|
|
<< "type: "
|
|
|
|
|
<< (expr->target.type ? expr->target.type->TypeInfo().name : "<null>");
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// ast::CallExpression has an ast::IdentifierExpression as the target.
|
|
|
|
|
// This call is either a function call, builtin call, type constructor or type conversion.
|
|
|
|
|
auto* ident = expr->target.name;
|
|
|
|
|
Mark(ident);
|
|
|
|
|
auto* resolved = sem_.ResolvedSymbol(ident);
|
|
|
|
|
call = Switch<sem::Call*>(
|
|
|
|
|
resolved, //
|
|
|
|
|
[&](sem::Type* ty) {
|
|
|
|
|
// A type constructor or conversions.
|
2022-05-19 20:19:49 +00:00
|
|
|
|
// Note: Unlike the code path where we're resolving the call target from an
|
2022-05-16 20:54:42 +00:00
|
|
|
|
// ast::Type, all types must already have the element type explicitly specified, so
|
|
|
|
|
// there's no need to infer element types.
|
|
|
|
|
return ty_ctor_or_conv(ty);
|
|
|
|
|
},
|
|
|
|
|
[&](sem::Function* func) {
|
|
|
|
|
return FunctionCall(expr, func, std::move(args), arg_behaviors);
|
|
|
|
|
},
|
|
|
|
|
[&](sem::Variable* var) {
|
|
|
|
|
auto name = builder_->Symbols().NameFor(var->Declaration()->symbol);
|
|
|
|
|
AddError("cannot call variable '" + name + "'", ident->source);
|
|
|
|
|
AddNote("'" + name + "' declared here", var->Declaration()->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
|
|
|
|
[&](Default) -> sem::Call* {
|
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol);
|
|
|
|
|
auto builtin_type = sem::ParseBuiltinType(name);
|
|
|
|
|
if (builtin_type != sem::BuiltinType::kNone) {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
return BuiltinCall(expr, builtin_type, std::move(args));
|
2022-05-16 20:54:42 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-16 20:54:42 +00:00
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< expr->source << " unhandled CallExpression target:\n"
|
|
|
|
|
<< "resolved: " << (resolved ? resolved->TypeInfo().name : "<null>") << "\n"
|
|
|
|
|
<< "name: " << builder_->Symbols().NameFor(ident->symbol);
|
|
|
|
|
return nullptr;
|
|
|
|
|
});
|
2021-11-15 20:45:50 +00:00
|
|
|
|
}
|
2021-12-09 14:37:37 +00:00
|
|
|
|
|
2022-05-16 20:54:42 +00:00
|
|
|
|
if (!call) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-16 20:54:42 +00:00
|
|
|
|
return validator_.Call(call, current_statement_) ? call : nullptr;
|
2021-11-15 20:45:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
|
sem::Call* Resolver::BuiltinCall(const ast::CallExpression* expr,
|
|
|
|
|
sem::BuiltinType builtin_type,
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::VectorRef<const sem::Expression*> args) {
|
2022-06-01 23:57:20 +00:00
|
|
|
|
IntrinsicTable::Builtin builtin;
|
2022-05-19 20:19:49 +00:00
|
|
|
|
{
|
2022-07-26 07:55:24 +00:00
|
|
|
|
auto arg_tys = utils::Transform<8>(args, [](auto* arg) { return arg->Type(); });
|
2022-05-19 20:19:49 +00:00
|
|
|
|
builtin = intrinsic_table_->Lookup(builtin_type, arg_tys, expr->source);
|
2022-06-01 23:57:20 +00:00
|
|
|
|
if (!builtin.sem) {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-11-05 16:51:38 +00:00
|
|
|
|
|
2022-06-01 23:57:20 +00:00
|
|
|
|
if (!MaterializeArguments(args, builtin.sem)) {
|
2022-05-24 21:42:03 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 23:57:20 +00:00
|
|
|
|
if (builtin.sem->IsDeprecated()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
AddWarning("use of deprecated builtin", expr->source);
|
|
|
|
|
}
|
2021-11-23 20:45:51 +00:00
|
|
|
|
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto stage = builtin.sem->Stage();
|
2022-07-15 23:54:10 +00:00
|
|
|
|
if (stage == sem::EvaluationStage::kConstant) { // <-- Optimization
|
|
|
|
|
// If the builtin is not annotated with @const, then it can only be evaluated
|
|
|
|
|
// at runtime, in which case there's no point checking the evaluation stage of the
|
|
|
|
|
// arguments.
|
|
|
|
|
|
|
|
|
|
// The builtin is @const annotated. Check all arguments are also constant.
|
2022-07-15 23:46:31 +00:00
|
|
|
|
for (auto* arg : args) {
|
|
|
|
|
stage = sem::EarliestStage(stage, arg->Stage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 23:57:20 +00:00
|
|
|
|
// If the builtin is @const, and all arguments have constant values, evaluate the builtin now.
|
2022-07-15 14:14:09 +00:00
|
|
|
|
const sem::Constant* value = nullptr;
|
2022-07-15 23:54:10 +00:00
|
|
|
|
if (stage == sem::EvaluationStage::kConstant) {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
value = (const_eval_.*builtin.const_eval_fn)(builtin.sem->ReturnType(), args);
|
2022-06-01 23:57:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
bool has_side_effects =
|
2022-06-01 23:57:20 +00:00
|
|
|
|
builtin.sem->HasSideEffects() ||
|
2022-05-01 14:40:55 +00:00
|
|
|
|
std::any_of(args.begin(), args.end(), [](auto* e) { return e->HasSideEffects(); });
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto* call = builder_->create<sem::Call>(expr, builtin.sem, stage, std::move(args),
|
|
|
|
|
current_statement_, value, has_side_effects);
|
2020-06-01 13:43:22 +00:00
|
|
|
|
|
2022-07-07 17:04:21 +00:00
|
|
|
|
if (current_function_) {
|
|
|
|
|
current_function_->AddDirectlyCalledBuiltin(builtin.sem);
|
|
|
|
|
current_function_->AddDirectCall(call);
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-18 22:41:48 +00:00
|
|
|
|
if (!validator_.RequiredExtensionForBuiltinFunction(call, enabled_extensions_)) {
|
2022-05-10 09:05:54 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (IsTextureBuiltin(builtin_type)) {
|
|
|
|
|
if (!validator_.TextureBuiltinFunction(call)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-06-01 23:57:20 +00:00
|
|
|
|
CollectTextureSamplerPairs(builtin.sem, call->Arguments());
|
2021-06-09 20:17:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!validator_.BuiltinCall(call)) {
|
|
|
|
|
return nullptr;
|
2021-06-09 20:17:59 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
return call;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 07:55:24 +00:00
|
|
|
|
void Resolver::CollectTextureSamplerPairs(
|
|
|
|
|
const sem::Builtin* builtin,
|
|
|
|
|
utils::ConstVectorRef<const sem::Expression*> args) const {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
// Collect a texture/sampler pair for this builtin.
|
|
|
|
|
const auto& signature = builtin->Signature();
|
|
|
|
|
int texture_index = signature.IndexOf(sem::ParameterUsage::kTexture);
|
|
|
|
|
if (texture_index == -1) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "texture builtin without texture parameter";
|
|
|
|
|
}
|
2022-06-29 14:38:15 +00:00
|
|
|
|
auto* texture = args[static_cast<size_t>(texture_index)]->As<sem::VariableUser>()->Variable();
|
2022-05-19 20:19:49 +00:00
|
|
|
|
if (!texture->Type()->UnwrapRef()->Is<sem::StorageTexture>()) {
|
|
|
|
|
int sampler_index = signature.IndexOf(sem::ParameterUsage::kSampler);
|
|
|
|
|
const sem::Variable* sampler =
|
2022-06-29 14:38:15 +00:00
|
|
|
|
sampler_index != -1
|
|
|
|
|
? args[static_cast<size_t>(sampler_index)]->As<sem::VariableUser>()->Variable()
|
|
|
|
|
: nullptr;
|
2022-05-19 20:19:49 +00:00
|
|
|
|
current_function_->AddTextureSamplerPair(texture, sampler);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Call* Resolver::FunctionCall(const ast::CallExpression* expr,
|
|
|
|
|
sem::Function* target,
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::VectorRef<const sem::Expression*> args,
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Behaviors arg_behaviors) {
|
|
|
|
|
auto sym = expr->target.name->symbol;
|
|
|
|
|
auto name = builder_->Symbols().NameFor(sym);
|
|
|
|
|
|
2022-05-24 21:42:03 +00:00
|
|
|
|
if (!MaterializeArguments(args, target)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// TODO(crbug.com/tint/1420): For now, assume all function calls have side
|
|
|
|
|
// effects.
|
|
|
|
|
bool has_side_effects = true;
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto* call = builder_->create<sem::Call>(expr, target, sem::EvaluationStage::kRuntime,
|
|
|
|
|
std::move(args), current_statement_,
|
2022-06-29 19:07:30 +00:00
|
|
|
|
/* constant_value */ nullptr, has_side_effects);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-27 20:16:26 +00:00
|
|
|
|
target->AddCallSite(call);
|
|
|
|
|
|
|
|
|
|
call->Behaviors() = arg_behaviors + target->Behaviors();
|
|
|
|
|
|
|
|
|
|
if (!validator_.FunctionCall(call, current_statement_)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (current_function_) {
|
|
|
|
|
// Note: Requires called functions to be resolved first.
|
|
|
|
|
// This is currently guaranteed as functions must be declared before
|
|
|
|
|
// use.
|
|
|
|
|
current_function_->AddTransitivelyCalledFunction(target);
|
|
|
|
|
current_function_->AddDirectCall(call);
|
|
|
|
|
for (auto* transitive_call : target->TransitivelyCalledFunctions()) {
|
|
|
|
|
current_function_->AddTransitivelyCalledFunction(transitive_call);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
|
for (auto* var : target->TransitivelyReferencedGlobals()) {
|
|
|
|
|
current_function_->AddTransitivelyReferencedGlobal(var);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 20:16:26 +00:00
|
|
|
|
// Note: Validation *must* be performed before calling this method.
|
2022-05-19 20:19:49 +00:00
|
|
|
|
CollectTextureSamplerPairs(target, call->Arguments());
|
2022-01-10 20:46:35 +00:00
|
|
|
|
}
|
2021-06-09 20:17:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return call;
|
2021-11-05 16:51:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 07:55:24 +00:00
|
|
|
|
void Resolver::CollectTextureSamplerPairs(
|
|
|
|
|
sem::Function* func,
|
|
|
|
|
utils::ConstVectorRef<const sem::Expression*> args) const {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
// Map all texture/sampler pairs from the target function to the
|
|
|
|
|
// current function. These can only be global or parameter
|
|
|
|
|
// variables. Resolve any parameter variables to the corresponding
|
|
|
|
|
// argument passed to the current function. Leave global variables
|
|
|
|
|
// as-is. Then add the mapped pair to the current function's list of
|
|
|
|
|
// texture/sampler pairs.
|
|
|
|
|
for (sem::VariablePair pair : func->TextureSamplerPairs()) {
|
|
|
|
|
const sem::Variable* texture = pair.first;
|
|
|
|
|
const sem::Variable* sampler = pair.second;
|
|
|
|
|
if (auto* param = texture->As<sem::Parameter>()) {
|
|
|
|
|
texture = args[param->Index()]->As<sem::VariableUser>()->Variable();
|
|
|
|
|
}
|
|
|
|
|
if (sampler) {
|
|
|
|
|
if (auto* param = sampler->As<sem::Parameter>()) {
|
|
|
|
|
sampler = args[param->Index()]->As<sem::VariableUser>()->Variable();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
current_function_->AddTextureSamplerPair(texture, sampler);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Expression* Resolver::Literal(const ast::LiteralExpression* literal) {
|
2022-05-04 22:18:49 +00:00
|
|
|
|
auto* ty = Switch(
|
|
|
|
|
literal,
|
|
|
|
|
[&](const ast::IntLiteralExpression* i) -> sem::Type* {
|
|
|
|
|
switch (i->suffix) {
|
|
|
|
|
case ast::IntLiteralExpression::Suffix::kNone:
|
2022-06-01 20:34:40 +00:00
|
|
|
|
return builder_->create<sem::AbstractInt>();
|
2022-05-04 22:18:49 +00:00
|
|
|
|
case ast::IntLiteralExpression::Suffix::kI:
|
|
|
|
|
return builder_->create<sem::I32>();
|
|
|
|
|
case ast::IntLiteralExpression::Suffix::kU:
|
|
|
|
|
return builder_->create<sem::U32>();
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
},
|
2022-05-20 17:18:50 +00:00
|
|
|
|
[&](const ast::FloatLiteralExpression* f) -> sem::Type* {
|
2022-06-10 18:18:35 +00:00
|
|
|
|
switch (f->suffix) {
|
|
|
|
|
case ast::FloatLiteralExpression::Suffix::kNone:
|
|
|
|
|
return builder_->create<sem::AbstractFloat>();
|
|
|
|
|
case ast::FloatLiteralExpression::Suffix::kF:
|
|
|
|
|
return builder_->create<sem::F32>();
|
|
|
|
|
case ast::FloatLiteralExpression::Suffix::kH:
|
|
|
|
|
return builder_->create<sem::F16>();
|
2022-05-20 17:18:50 +00:00
|
|
|
|
}
|
2022-06-10 18:18:35 +00:00
|
|
|
|
return nullptr;
|
2022-05-20 17:18:50 +00:00
|
|
|
|
},
|
2022-05-04 22:18:49 +00:00
|
|
|
|
[&](const ast::BoolLiteralExpression*) { return builder_->create<sem::Bool>(); },
|
|
|
|
|
[&](Default) { return nullptr; });
|
|
|
|
|
|
|
|
|
|
if (ty == nullptr) {
|
|
|
|
|
TINT_UNREACHABLE(Resolver, builder_->Diagnostics())
|
|
|
|
|
<< "Unhandled literal type: " << literal->TypeInfo().name;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 18:18:35 +00:00
|
|
|
|
if ((ty->Is<sem::F16>()) && (!enabled_extensions_.contains(tint::ast::Extension::kF16))) {
|
|
|
|
|
AddError("f16 literal used without 'f16' extension enabled", literal->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 14:14:09 +00:00
|
|
|
|
auto val = const_eval_.Literal(ty, literal);
|
2022-07-15 23:46:31 +00:00
|
|
|
|
return builder_->create<sem::Expression>(literal, ty, sem::EvaluationStage::kConstant,
|
|
|
|
|
current_statement_, std::move(val),
|
2022-05-01 14:40:55 +00:00
|
|
|
|
/* has_side_effects */ false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Expression* Resolver::Identifier(const ast::IdentifierExpression* expr) {
|
|
|
|
|
auto symbol = expr->symbol;
|
|
|
|
|
auto* resolved = sem_.ResolvedSymbol(expr);
|
2022-06-26 11:36:10 +00:00
|
|
|
|
if (auto* variable = As<sem::Variable>(resolved)) {
|
|
|
|
|
auto* user = builder_->create<sem::VariableUser>(expr, current_statement_, variable);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
if (current_statement_) {
|
|
|
|
|
// 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.
|
|
|
|
|
if (auto* continuing_block =
|
|
|
|
|
current_statement_->FindFirstParent<sem::LoopContinuingBlockStatement>()) {
|
|
|
|
|
auto* loop_block = continuing_block->FindFirstParent<sem::LoopBlockStatement>();
|
|
|
|
|
if (loop_block->FirstContinue()) {
|
|
|
|
|
auto& decls = loop_block->Decls();
|
|
|
|
|
// If our identifier is in loop_block->decls, make sure its index is
|
|
|
|
|
// less than first_continue
|
|
|
|
|
auto iter = std::find_if(decls.begin(), decls.end(),
|
|
|
|
|
[&symbol](auto* v) { return v->symbol == symbol; });
|
|
|
|
|
if (iter != decls.end()) {
|
|
|
|
|
auto var_decl_index =
|
|
|
|
|
static_cast<size_t>(std::distance(decls.begin(), iter));
|
|
|
|
|
if (var_decl_index >= loop_block->NumDeclsAtFirstContinue()) {
|
|
|
|
|
AddError("continue statement bypasses declaration of '" +
|
|
|
|
|
builder_->Symbols().NameFor(symbol) + "'",
|
|
|
|
|
loop_block->FirstContinue()->source);
|
|
|
|
|
AddNote("identifier '" + builder_->Symbols().NameFor(symbol) +
|
|
|
|
|
"' declared here",
|
|
|
|
|
(*iter)->source);
|
|
|
|
|
AddNote("identifier '" + builder_->Symbols().NameFor(symbol) +
|
|
|
|
|
"' referenced in continuing block here",
|
|
|
|
|
expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (current_function_) {
|
2022-06-26 11:36:10 +00:00
|
|
|
|
if (auto* global = variable->As<sem::GlobalVariable>()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
current_function_->AddDirectlyReferencedGlobal(global);
|
|
|
|
|
}
|
2022-06-26 11:36:10 +00:00
|
|
|
|
} else if (variable->Declaration()->Is<ast::Var>()) {
|
|
|
|
|
// Use of a module-scope 'var' outside of a function.
|
|
|
|
|
// Note: The spec is currently vague around the rules here. See
|
|
|
|
|
// https://github.com/gpuweb/gpuweb/issues/3081. Remove this comment when resolved.
|
|
|
|
|
std::string desc = "var '" + builder_->Symbols().NameFor(symbol) + "' ";
|
|
|
|
|
AddError(desc + "cannot not be referenced at module-scope", expr->source);
|
|
|
|
|
AddNote(desc + "declared here", variable->Declaration()->source);
|
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-26 11:36:10 +00:00
|
|
|
|
variable->AddUser(user);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Is<sem::Function>(resolved)) {
|
|
|
|
|
AddError("missing '(' for function call", expr->source.End());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsBuiltin(symbol)) {
|
|
|
|
|
AddError("missing '(' for builtin call", expr->source.End());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (resolved->Is<sem::Type>()) {
|
|
|
|
|
AddError("missing '(' for type constructor or cast", expr->source.End());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_)
|
|
|
|
|
<< expr->source << " unresolved identifier:\n"
|
|
|
|
|
<< "resolved: " << (resolved ? resolved->TypeInfo().name : "<null>") << "\n"
|
|
|
|
|
<< "name: " << builder_->Symbols().NameFor(symbol);
|
2021-12-03 15:49:34 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Expression* Resolver::MemberAccessor(const ast::MemberAccessorExpression* expr) {
|
|
|
|
|
auto* structure = sem_.TypeOf(expr->structure);
|
|
|
|
|
auto* storage_ty = structure->UnwrapRef();
|
2022-07-14 20:46:39 +00:00
|
|
|
|
auto* object = sem_.Get(expr->structure);
|
|
|
|
|
auto* source_var = object->SourceVariable();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
const sem::Type* ret = nullptr;
|
2022-07-26 07:55:24 +00:00
|
|
|
|
utils::Vector<uint32_t, 4> swizzle;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-06-24 20:34:00 +00:00
|
|
|
|
// Object may be a side-effecting expression (e.g. function call).
|
|
|
|
|
bool has_side_effects = object && object->HasSideEffects();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
if (auto* str = storage_ty->As<sem::Struct>()) {
|
|
|
|
|
Mark(expr->member);
|
|
|
|
|
auto symbol = expr->member->symbol;
|
|
|
|
|
|
|
|
|
|
const sem::StructMember* member = nullptr;
|
|
|
|
|
for (auto* m : str->Members()) {
|
|
|
|
|
if (m->Name() == symbol) {
|
|
|
|
|
ret = m->Type();
|
|
|
|
|
member = m;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret == nullptr) {
|
|
|
|
|
AddError("struct member " + builder_->Symbols().NameFor(symbol) + " not found",
|
|
|
|
|
expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're extracting from a reference, we return a reference.
|
|
|
|
|
if (auto* ref = structure->As<sem::Reference>()) {
|
|
|
|
|
ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), ref->Access());
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 14:14:09 +00:00
|
|
|
|
auto* val = const_eval_.MemberAccess(object, member);
|
2022-07-07 17:49:02 +00:00
|
|
|
|
return builder_->create<sem::StructMemberAccess>(expr, ret, current_statement_, val, object,
|
2022-06-24 20:34:00 +00:00
|
|
|
|
member, has_side_effects, source_var);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto* vec = storage_ty->As<sem::Vector>()) {
|
|
|
|
|
Mark(expr->member);
|
|
|
|
|
std::string s = builder_->Symbols().NameFor(expr->member->symbol);
|
|
|
|
|
auto size = s.size();
|
2022-07-26 07:55:24 +00:00
|
|
|
|
swizzle.Reserve(s.size());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
for (auto c : s) {
|
|
|
|
|
switch (c) {
|
|
|
|
|
case 'x':
|
|
|
|
|
case 'r':
|
2022-07-26 07:55:24 +00:00
|
|
|
|
swizzle.Push(0u);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'y':
|
|
|
|
|
case 'g':
|
2022-07-26 07:55:24 +00:00
|
|
|
|
swizzle.Push(1u);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'z':
|
|
|
|
|
case 'b':
|
2022-07-26 07:55:24 +00:00
|
|
|
|
swizzle.Push(2u);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'w':
|
|
|
|
|
case 'a':
|
2022-07-26 07:55:24 +00:00
|
|
|
|
swizzle.Push(3u);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
AddError("invalid vector swizzle character",
|
2022-07-26 07:55:24 +00:00
|
|
|
|
expr->member->source.Begin() + swizzle.Length());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 07:55:24 +00:00
|
|
|
|
if (swizzle.Back() >= vec->Width()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
AddError("invalid vector swizzle member", expr->member->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size < 1 || size > 4) {
|
|
|
|
|
AddError("invalid vector swizzle size", expr->member->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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'; };
|
|
|
|
|
if (!std::all_of(s.begin(), s.end(), is_rgba) &&
|
|
|
|
|
!std::all_of(s.begin(), s.end(), is_xyzw)) {
|
|
|
|
|
AddError("invalid mixing of vector swizzle characters rgba with xyzw",
|
|
|
|
|
expr->member->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size == 1) {
|
|
|
|
|
// A single element swizzle is just the type of the vector.
|
|
|
|
|
ret = vec->type();
|
|
|
|
|
// If we're extracting from a reference, we return a reference.
|
|
|
|
|
if (auto* ref = structure->As<sem::Reference>()) {
|
|
|
|
|
ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), ref->Access());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// The vector will have a number of components equal to the length of
|
|
|
|
|
// the swizzle.
|
|
|
|
|
ret = builder_->create<sem::Vector>(vec->type(), static_cast<uint32_t>(size));
|
|
|
|
|
}
|
2022-07-15 14:14:09 +00:00
|
|
|
|
auto* val = const_eval_.Swizzle(ret, object, swizzle);
|
2022-07-07 17:49:02 +00:00
|
|
|
|
return builder_->create<sem::Swizzle>(expr, ret, current_statement_, val, object,
|
2022-06-24 20:34:00 +00:00
|
|
|
|
std::move(swizzle), has_side_effects, source_var);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddError("invalid member accessor expression. Expected vector or struct, got '" +
|
|
|
|
|
sem_.TypeNameOf(storage_ty) + "'",
|
|
|
|
|
expr->structure->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Expression* Resolver::Binary(const ast::BinaryExpression* expr) {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
const auto* lhs = sem_.Get(expr->lhs);
|
|
|
|
|
const auto* rhs = sem_.Get(expr->rhs);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* lhs_ty = lhs->Type()->UnwrapRef();
|
|
|
|
|
auto* rhs_ty = rhs->Type()->UnwrapRef();
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto stage = sem::EvaluationStage::kRuntime; // TODO(crbug.com/tint/1581)
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-05-19 20:19:49 +00:00
|
|
|
|
auto op = intrinsic_table_->Lookup(expr->op, lhs_ty, rhs_ty, expr->source, false);
|
|
|
|
|
if (!op.result) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-24 21:42:03 +00:00
|
|
|
|
if (ShouldMaterializeArgument(op.lhs)) {
|
|
|
|
|
lhs = Materialize(lhs, op.lhs);
|
|
|
|
|
if (!lhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ShouldMaterializeArgument(op.rhs)) {
|
|
|
|
|
rhs = Materialize(rhs, op.rhs);
|
|
|
|
|
if (!rhs) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-07-15 14:14:09 +00:00
|
|
|
|
const sem::Constant* value = nullptr;
|
|
|
|
|
if (op.const_eval_fn) {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
value = (const_eval_.*op.const_eval_fn)(op.result, utils::Vector{lhs, rhs});
|
2022-07-15 14:14:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
bool has_side_effects = lhs->HasSideEffects() || rhs->HasSideEffects();
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto* sem = builder_->create<sem::Expression>(expr, op.result, stage, current_statement_, value,
|
2022-07-07 17:30:11 +00:00
|
|
|
|
has_side_effects);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem->Behaviors() = lhs->Behaviors() + rhs->Behaviors();
|
|
|
|
|
|
|
|
|
|
return sem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Expression* Resolver::UnaryOp(const ast::UnaryOpExpression* unary) {
|
2022-05-19 20:19:49 +00:00
|
|
|
|
const auto* expr = sem_.Get(unary->expr);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* expr_ty = expr->Type();
|
|
|
|
|
if (!expr_ty) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-02-03 23:55:56 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
const sem::Type* ty = nullptr;
|
|
|
|
|
const sem::Variable* source_var = nullptr;
|
2022-07-15 14:14:09 +00:00
|
|
|
|
const sem::Constant* value = nullptr;
|
2022-07-15 23:54:10 +00:00
|
|
|
|
auto stage = sem::EvaluationStage::kRuntime;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
|
|
|
|
switch (unary->op) {
|
|
|
|
|
case ast::UnaryOp::kAddressOf:
|
|
|
|
|
if (auto* ref = expr_ty->As<sem::Reference>()) {
|
|
|
|
|
if (ref->StoreType()->UnwrapRef()->is_handle()) {
|
|
|
|
|
AddError("cannot take the address of expression in handle storage class",
|
|
|
|
|
unary->expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-03-15 13:51:17 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* array = unary->expr->As<ast::IndexAccessorExpression>();
|
|
|
|
|
auto* member = unary->expr->As<ast::MemberAccessorExpression>();
|
|
|
|
|
if ((array && sem_.TypeOf(array->object)->UnwrapRef()->Is<sem::Vector>()) ||
|
|
|
|
|
(member && sem_.TypeOf(member->structure)->UnwrapRef()->Is<sem::Vector>())) {
|
|
|
|
|
AddError("cannot take the address of a vector component", unary->expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-04-07 16:41:33 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
ty = builder_->create<sem::Pointer>(ref->StoreType(), ref->StorageClass(),
|
|
|
|
|
ref->Access());
|
2020-04-07 16:41:33 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
source_var = expr->SourceVariable();
|
|
|
|
|
} else {
|
|
|
|
|
AddError("cannot take the address of expression", unary->expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ast::UnaryOp::kIndirection:
|
|
|
|
|
if (auto* ptr = expr_ty->As<sem::Pointer>()) {
|
|
|
|
|
ty = builder_->create<sem::Reference>(ptr->StoreType(), ptr->StorageClass(),
|
|
|
|
|
ptr->Access());
|
|
|
|
|
source_var = expr->SourceVariable();
|
|
|
|
|
} else {
|
|
|
|
|
AddError("cannot dereference expression of type '" + sem_.TypeNameOf(expr_ty) + "'",
|
|
|
|
|
unary->expr->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-05-09 21:22:24 +00:00
|
|
|
|
|
|
|
|
|
default: {
|
2022-05-24 21:42:03 +00:00
|
|
|
|
auto op = intrinsic_table_->Lookup(unary->op, expr_ty, unary->source);
|
|
|
|
|
if (!op.result) {
|
2022-05-09 21:22:24 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-24 21:42:03 +00:00
|
|
|
|
if (ShouldMaterializeArgument(op.parameter)) {
|
|
|
|
|
expr = Materialize(expr, op.parameter);
|
|
|
|
|
if (!expr) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-15 23:54:10 +00:00
|
|
|
|
stage = expr->Stage();
|
|
|
|
|
if (stage == sem::EvaluationStage::kConstant) {
|
|
|
|
|
if (op.const_eval_fn) {
|
2022-07-26 07:55:24 +00:00
|
|
|
|
value = (const_eval_.*op.const_eval_fn)(ty, utils::Vector{expr});
|
2022-07-15 23:54:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
stage = sem::EvaluationStage::kRuntime;
|
|
|
|
|
}
|
2022-07-15 14:14:09 +00:00
|
|
|
|
}
|
2022-07-15 23:54:10 +00:00
|
|
|
|
ty = op.result;
|
2022-05-19 20:19:49 +00:00
|
|
|
|
break;
|
2022-05-09 21:22:24 +00:00
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
2022-07-15 23:46:31 +00:00
|
|
|
|
auto* sem = builder_->create<sem::Expression>(unary, ty, stage, current_statement_, value,
|
2022-05-01 14:40:55 +00:00
|
|
|
|
expr->HasSideEffects(), source_var);
|
|
|
|
|
sem->Behaviors() = expr->Behaviors();
|
|
|
|
|
return sem;
|
|
|
|
|
}
|
2021-06-01 00:37:40 +00:00
|
|
|
|
|
2022-05-18 22:41:48 +00:00
|
|
|
|
bool Resolver::Enable(const ast::Enable* enable) {
|
|
|
|
|
enabled_extensions_.add(enable->extension);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Type* Resolver::TypeDecl(const ast::TypeDecl* named_type) {
|
|
|
|
|
sem::Type* result = nullptr;
|
|
|
|
|
if (auto* alias = named_type->As<ast::Alias>()) {
|
|
|
|
|
result = Alias(alias);
|
|
|
|
|
} else if (auto* str = named_type->As<ast::Struct>()) {
|
|
|
|
|
result = Structure(str);
|
|
|
|
|
} else {
|
|
|
|
|
TINT_UNREACHABLE(Resolver, diagnostics_) << "Unhandled TypeDecl";
|
2021-02-24 14:15:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!result) {
|
|
|
|
|
return nullptr;
|
2021-02-24 14:15:02 +00:00
|
|
|
|
}
|
2021-01-11 16:24:32 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
builder_->Sem().Add(named_type, result);
|
|
|
|
|
return result;
|
2020-04-07 12:57:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Array* Resolver::Array(const ast::Array* arr) {
|
2022-07-31 19:33:04 +00:00
|
|
|
|
if (!arr->type) {
|
|
|
|
|
AddError("missing array element type", arr->source.End());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-03-31 22:30:10 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
auto* el_ty = Type(arr->type);
|
|
|
|
|
if (!el_ty) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-03-31 22:30:10 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
// Look for explicit stride via @stride(n) attribute
|
|
|
|
|
uint32_t explicit_stride = 0;
|
|
|
|
|
if (!ArrayAttributes(arr->attributes, el_ty, explicit_stride)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-03-31 22:30:10 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
uint32_t el_count = 0; // sem::Array uses a size of 0 for a runtime-sized array.
|
2021-03-16 13:26:03 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
// Evaluate the constant array size expression.
|
|
|
|
|
if (auto* count_expr = arr->count) {
|
|
|
|
|
if (auto count = ArrayCount(count_expr)) {
|
|
|
|
|
el_count = count.Get();
|
|
|
|
|
} else {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* out = Array(arr->source, el_ty, el_count, explicit_stride);
|
|
|
|
|
if (out == nullptr) {
|
2021-11-05 16:51:38 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
if (el_ty->Is<sem::Atomic>()) {
|
|
|
|
|
atomic_composite_info_.emplace(out, arr->type->source);
|
|
|
|
|
} else {
|
|
|
|
|
auto found = atomic_composite_info_.find(el_ty);
|
|
|
|
|
if (found != atomic_composite_info_.end()) {
|
|
|
|
|
atomic_composite_info_.emplace(out, found->second);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2022-07-31 19:33:04 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utils::Result<uint32_t> Resolver::ArrayCount(const ast::Expression* count_expr) {
|
|
|
|
|
// Evaluate the constant array size expression.
|
|
|
|
|
const auto* count_sem = Materialize(Expression(count_expr));
|
|
|
|
|
if (!count_sem) {
|
|
|
|
|
return utils::Failure;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2020-04-07 19:27:11 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
auto* count_val = count_sem->ConstantValue();
|
|
|
|
|
if (!count_val) {
|
|
|
|
|
AddError("array size must evaluate to a constant integer expression", count_expr->source);
|
|
|
|
|
return utils::Failure;
|
|
|
|
|
}
|
2021-05-20 08:44:57 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
if (auto* ty = count_val->Type(); !ty->is_integer_scalar()) {
|
|
|
|
|
AddError("array size must evaluate to a constant integer expression, but is type '" +
|
|
|
|
|
builder_->FriendlyName(ty) + "'",
|
|
|
|
|
count_expr->source);
|
|
|
|
|
return utils::Failure;
|
|
|
|
|
}
|
2021-05-20 08:44:57 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
int64_t count = count_val->As<AInt>();
|
|
|
|
|
if (count < 1) {
|
|
|
|
|
AddError("array size (" + std::to_string(count) + ") must be greater than 0",
|
|
|
|
|
count_expr->source);
|
|
|
|
|
return utils::Failure;
|
|
|
|
|
}
|
2022-06-24 23:30:59 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
return static_cast<uint32_t>(count);
|
|
|
|
|
}
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
bool Resolver::ArrayAttributes(const ast::AttributeList& attributes,
|
|
|
|
|
const sem::Type* el_ty,
|
|
|
|
|
uint32_t& explicit_stride) {
|
|
|
|
|
if (!validator_.NoDuplicateAttributes(attributes)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
for (auto* attr : attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
if (auto* sd = attr->As<ast::StrideAttribute>()) {
|
|
|
|
|
explicit_stride = sd->stride;
|
|
|
|
|
if (!validator_.ArrayStrideAttribute(sd, el_ty->Size(), el_ty->Align())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-05-07 20:58:34 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
AddError("attribute is not valid for array types", attr->source);
|
|
|
|
|
return false;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-05-07 20:58:34 +00:00
|
|
|
|
|
2022-07-31 19:33:04 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Array* Resolver::Array(const Source& source,
|
|
|
|
|
const sem::Type* el_ty,
|
|
|
|
|
uint32_t el_count,
|
|
|
|
|
uint32_t explicit_stride) {
|
|
|
|
|
uint32_t el_align = el_ty->Align();
|
|
|
|
|
uint32_t el_size = el_ty->Size();
|
|
|
|
|
uint64_t implicit_stride = el_size ? utils::RoundUp<uint64_t>(el_align, el_size) : 0;
|
|
|
|
|
uint64_t stride = explicit_stride ? explicit_stride : implicit_stride;
|
|
|
|
|
|
|
|
|
|
auto size = std::max<uint64_t>(el_count, 1u) * stride;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (size > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
|
std::stringstream msg;
|
2022-06-24 23:30:59 +00:00
|
|
|
|
msg << "array size (0x" << std::hex << size << ") must not exceed 0xffffffff bytes";
|
2022-07-31 19:33:04 +00:00
|
|
|
|
AddError(msg.str(), source);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-07-31 19:33:04 +00:00
|
|
|
|
auto* out = builder_->create<sem::Array>(el_ty, el_count, el_align, static_cast<uint32_t>(size),
|
|
|
|
|
static_cast<uint32_t>(stride),
|
|
|
|
|
static_cast<uint32_t>(implicit_stride));
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!validator_.Array(out, source)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Type* Resolver::Alias(const ast::Alias* alias) {
|
|
|
|
|
auto* ty = Type(alias->type);
|
|
|
|
|
if (!ty) {
|
2021-09-02 13:49:59 +00:00
|
|
|
|
return nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
if (!validator_.Alias(alias)) {
|
2021-09-02 13:49:59 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return ty;
|
|
|
|
|
}
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Struct* Resolver::Structure(const ast::Struct* str) {
|
|
|
|
|
if (!validator_.NoDuplicateAttributes(str->attributes)) {
|
|
|
|
|
return nullptr;
|
2021-09-02 13:49:59 +00:00
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* attr : str->attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::StructMemberList sem_members;
|
|
|
|
|
sem_members.reserve(str->members.size());
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
uint64_t struct_size = 0;
|
|
|
|
|
uint64_t struct_align = 1;
|
|
|
|
|
std::unordered_map<Symbol, const ast::StructMember*> member_map;
|
|
|
|
|
|
|
|
|
|
for (auto* member : str->members) {
|
|
|
|
|
Mark(member);
|
|
|
|
|
auto result = member_map.emplace(member->symbol, member);
|
|
|
|
|
if (!result.second) {
|
|
|
|
|
AddError("redefinition of '" + builder_->Symbols().NameFor(member->symbol) + "'",
|
|
|
|
|
member->source);
|
|
|
|
|
AddNote("previous definition is here", result.first->second->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// Resolve member type
|
|
|
|
|
auto* type = Type(member->type);
|
|
|
|
|
if (!type) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// validator_.Validate member type
|
|
|
|
|
if (!validator_.IsPlain(type)) {
|
|
|
|
|
AddError(sem_.TypeNameOf(type) + " cannot be used as the type of a structure member",
|
|
|
|
|
member->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-09-02 13:49:59 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
uint64_t offset = struct_size;
|
|
|
|
|
uint64_t align = type->Align();
|
|
|
|
|
uint64_t size = type->Size();
|
2021-05-07 20:58:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!validator_.NoDuplicateAttributes(member->attributes)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool has_offset_attr = false;
|
|
|
|
|
bool has_align_attr = false;
|
|
|
|
|
bool has_size_attr = false;
|
|
|
|
|
for (auto* attr : member->attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
if (auto* o = attr->As<ast::StructMemberOffsetAttribute>()) {
|
|
|
|
|
// Offset attributes are not part of the WGSL spec, but are emitted
|
|
|
|
|
// by the SPIR-V reader.
|
|
|
|
|
if (o->offset < struct_size) {
|
|
|
|
|
AddError("offsets must be in ascending order", o->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
offset = o->offset;
|
|
|
|
|
align = 1;
|
|
|
|
|
has_offset_attr = true;
|
|
|
|
|
} else if (auto* a = attr->As<ast::StructMemberAlignAttribute>()) {
|
|
|
|
|
if (a->align <= 0 || !utils::IsPowerOfTwo(a->align)) {
|
|
|
|
|
AddError("align value must be a positive, power-of-two integer", a->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
align = a->align;
|
|
|
|
|
has_align_attr = true;
|
|
|
|
|
} else if (auto* s = attr->As<ast::StructMemberSizeAttribute>()) {
|
|
|
|
|
if (s->size < size) {
|
|
|
|
|
AddError("size must be at least as big as the type's size (" +
|
|
|
|
|
std::to_string(size) + ")",
|
|
|
|
|
s->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
size = s->size;
|
|
|
|
|
has_size_attr = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (has_offset_attr && (has_align_attr || has_size_attr)) {
|
|
|
|
|
AddError("offset attributes cannot be used with align or size attributes",
|
|
|
|
|
member->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
offset = utils::RoundUp(align, offset);
|
|
|
|
|
if (offset > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
|
std::stringstream msg;
|
2022-06-24 23:30:59 +00:00
|
|
|
|
msg << "struct member offset (0x" << std::hex << offset << ") must not exceed 0x"
|
|
|
|
|
<< std::hex << std::numeric_limits<uint32_t>::max() << " bytes";
|
2022-05-01 14:40:55 +00:00
|
|
|
|
AddError(msg.str(), member->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto* sem_member = builder_->create<sem::StructMember>(
|
|
|
|
|
member, member->symbol, type, static_cast<uint32_t>(sem_members.size()),
|
|
|
|
|
static_cast<uint32_t>(offset), static_cast<uint32_t>(align),
|
|
|
|
|
static_cast<uint32_t>(size));
|
|
|
|
|
builder_->Sem().Add(member, sem_member);
|
|
|
|
|
sem_members.emplace_back(sem_member);
|
2021-05-07 20:58:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
struct_size = offset + size;
|
|
|
|
|
struct_align = std::max(struct_align, align);
|
2021-08-05 15:18:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
uint64_t size_no_padding = struct_size;
|
|
|
|
|
struct_size = utils::RoundUp(struct_align, struct_size);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (struct_size > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
|
std::stringstream msg;
|
2022-06-24 23:30:59 +00:00
|
|
|
|
msg << "struct size (0x" << std::hex << struct_size << ") must not exceed 0xffffffff bytes";
|
2022-05-01 14:40:55 +00:00
|
|
|
|
AddError(msg.str(), str->source);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (struct_align > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "calculated struct stride exceeds uint32";
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-11-26 09:56:19 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* out = builder_->create<sem::Struct>(
|
|
|
|
|
str, str->name, sem_members, static_cast<uint32_t>(struct_align),
|
|
|
|
|
static_cast<uint32_t>(struct_size), static_cast<uint32_t>(size_no_padding));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sem_members.size(); i++) {
|
|
|
|
|
auto* mem_type = sem_members[i]->Type();
|
|
|
|
|
if (mem_type->Is<sem::Atomic>()) {
|
|
|
|
|
atomic_composite_info_.emplace(out, sem_members[i]->Declaration()->source);
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
auto found = atomic_composite_info_.find(mem_type);
|
|
|
|
|
if (found != atomic_composite_info_.end()) {
|
|
|
|
|
atomic_composite_info_.emplace(out, found->second);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-24 20:34:00 +00:00
|
|
|
|
|
|
|
|
|
const_cast<sem::StructMember*>(sem_members[i])->SetStruct(out);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto stage = current_function_ ? current_function_->Declaration()->PipelineStage()
|
|
|
|
|
: ast::PipelineStage::kNone;
|
|
|
|
|
if (!validator_.Structure(out, stage)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-11-26 09:56:19 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return out;
|
2021-11-26 09:56:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 16:26:42 +00:00
|
|
|
|
sem::Statement* Resolver::ReturnStatement(const ast::ReturnStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto& behaviors = current_statement_->Behaviors();
|
|
|
|
|
behaviors = sem::Behavior::kReturn;
|
|
|
|
|
|
2022-05-19 20:19:49 +00:00
|
|
|
|
const sem::Type* value_ty = nullptr;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* value = stmt->value) {
|
2022-06-06 15:11:04 +00:00
|
|
|
|
const auto* expr = Expression(value);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!expr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-06-06 15:11:04 +00:00
|
|
|
|
if (auto* ret_ty = current_function_->ReturnType(); !ret_ty->Is<sem::Void>()) {
|
|
|
|
|
expr = Materialize(expr, ret_ty);
|
|
|
|
|
if (!expr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-01 14:40:55 +00:00
|
|
|
|
behaviors.Add(expr->Behaviors() - sem::Behavior::kNext);
|
2022-05-19 20:19:49 +00:00
|
|
|
|
value_ty = expr->Type()->UnwrapRef();
|
|
|
|
|
} else {
|
|
|
|
|
value_ty = builder_->create<sem::Void>();
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-03-22 23:20:17 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
// Validate after processing the return value expression so that its type
|
|
|
|
|
// is available for validation.
|
2022-05-19 20:19:49 +00:00
|
|
|
|
return validator_.Return(stmt, current_function_->ReturnType(), value_ty,
|
2022-05-01 14:40:55 +00:00
|
|
|
|
current_statement_);
|
|
|
|
|
});
|
2021-03-22 23:20:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::SwitchStatement* Resolver::SwitchStatement(const ast::SwitchStatement* stmt) {
|
|
|
|
|
auto* sem = builder_->create<sem::SwitchStatement>(stmt, current_compound_statement_,
|
|
|
|
|
current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto& behaviors = sem->Behaviors();
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-24 21:42:03 +00:00
|
|
|
|
const auto* cond = Expression(stmt->condition);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!cond) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
behaviors = cond->Behaviors() - sem::Behavior::kNext;
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-24 21:42:03 +00:00
|
|
|
|
auto* cond_ty = cond->Type()->UnwrapRef();
|
|
|
|
|
|
2022-07-29 14:15:01 +00:00
|
|
|
|
utils::Vector<const sem::Type*, 8> types;
|
|
|
|
|
types.Push(cond_ty);
|
2022-05-24 21:42:03 +00:00
|
|
|
|
|
|
|
|
|
std::vector<sem::CaseStatement*> cases;
|
|
|
|
|
cases.reserve(stmt->body.size());
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* case_stmt : stmt->body) {
|
|
|
|
|
Mark(case_stmt);
|
|
|
|
|
auto* c = CaseStatement(case_stmt);
|
|
|
|
|
if (!c) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-05-24 21:42:03 +00:00
|
|
|
|
for (auto* expr : c->Selectors()) {
|
2022-07-29 14:15:01 +00:00
|
|
|
|
types.Push(expr->Type()->UnwrapRef());
|
2022-05-24 21:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
cases.emplace_back(c);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
behaviors.Add(c->Behaviors());
|
2022-05-20 12:28:00 +00:00
|
|
|
|
sem->Cases().emplace_back(c);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-24 21:42:03 +00:00
|
|
|
|
// Determine the common type across all selectors and the switch expression
|
|
|
|
|
// This must materialize to an integer scalar (non-abstract).
|
2022-07-29 14:15:01 +00:00
|
|
|
|
auto* common_ty = sem::Type::Common(types);
|
2022-05-24 21:42:03 +00:00
|
|
|
|
if (!common_ty || !common_ty->is_integer_scalar()) {
|
|
|
|
|
// No common type found or the common type was abstract.
|
|
|
|
|
// Pick i32 and let validation deal with any mismatches.
|
|
|
|
|
common_ty = builder_->create<sem::I32>();
|
|
|
|
|
}
|
|
|
|
|
cond = Materialize(cond, common_ty);
|
|
|
|
|
if (!cond) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (auto* c : cases) {
|
|
|
|
|
for (auto*& sel : c->Selectors()) { // Note: pointer reference
|
|
|
|
|
sel = Materialize(sel, common_ty);
|
|
|
|
|
if (!sel) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (behaviors.Contains(sem::Behavior::kBreak)) {
|
|
|
|
|
behaviors.Add(sem::Behavior::kNext);
|
|
|
|
|
}
|
|
|
|
|
behaviors.Remove(sem::Behavior::kBreak, sem::Behavior::kFallthrough);
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.SwitchStatement(stmt);
|
|
|
|
|
});
|
2021-11-26 16:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Statement* Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) {
|
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
Mark(stmt->variable);
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-06-20 19:17:52 +00:00
|
|
|
|
auto* variable = Variable(stmt->variable, /* is_global */ false);
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (!variable) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
for (auto* attr : stmt->variable->attributes) {
|
|
|
|
|
Mark(attr);
|
|
|
|
|
if (!attr->Is<ast::InternalAttribute>()) {
|
|
|
|
|
AddError("attributes are not valid on local variables", attr->source);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (current_block_) { // Not all statements are inside a block
|
|
|
|
|
current_block_->AddDecl(stmt->variable);
|
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-06-20 15:30:41 +00:00
|
|
|
|
if (auto* ctor = variable->Constructor()) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem->Behaviors() = ctor->Behaviors();
|
|
|
|
|
}
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-07-15 13:01:49 +00:00
|
|
|
|
return validator_.LocalVariable(variable);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
});
|
2021-03-25 12:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Statement* Resolver::AssignmentStatement(const ast::AssignmentStatement* stmt) {
|
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto* lhs = Expression(stmt->lhs);
|
|
|
|
|
if (!lhs) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-31 20:40:59 +00:00
|
|
|
|
const bool is_phony_assignment = stmt->lhs->Is<ast::PhonyExpression>();
|
|
|
|
|
|
|
|
|
|
const auto* rhs = Expression(stmt->rhs);
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!rhs) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-11-26 16:26:42 +00:00
|
|
|
|
|
2022-05-31 20:40:59 +00:00
|
|
|
|
if (!is_phony_assignment) {
|
|
|
|
|
rhs = Materialize(rhs, lhs->Type()->UnwrapRef());
|
|
|
|
|
if (!rhs) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto& behaviors = sem->Behaviors();
|
|
|
|
|
behaviors = rhs->Behaviors();
|
2022-05-31 20:40:59 +00:00
|
|
|
|
if (!is_phony_assignment) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
behaviors.Add(lhs->Behaviors());
|
|
|
|
|
}
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.Assignment(stmt, sem_.TypeOf(stmt->rhs));
|
|
|
|
|
});
|
2021-11-26 16:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Statement* Resolver::BreakStatement(const ast::BreakStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
sem->Behaviors() = sem::Behavior::kBreak;
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.BreakStatement(sem, current_statement_);
|
|
|
|
|
});
|
2021-11-26 16:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Statement* Resolver::CallStatement(const ast::CallStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
if (auto* expr = Expression(stmt->expr)) {
|
|
|
|
|
sem->Behaviors() = expr->Behaviors();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2021-11-26 16:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-31 22:30:10 +00:00
|
|
|
|
sem::Statement* Resolver::CompoundAssignmentStatement(
|
|
|
|
|
const ast::CompoundAssignmentStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto* lhs = Expression(stmt->lhs);
|
|
|
|
|
if (!lhs) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-31 22:30:10 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* rhs = Expression(stmt->rhs);
|
|
|
|
|
if (!rhs) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-31 22:30:10 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem->Behaviors() = rhs->Behaviors() + lhs->Behaviors();
|
2022-03-31 22:30:10 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* lhs_ty = lhs->Type()->UnwrapRef();
|
|
|
|
|
auto* rhs_ty = rhs->Type()->UnwrapRef();
|
2022-05-09 20:00:13 +00:00
|
|
|
|
auto* ty = intrinsic_table_->Lookup(stmt->op, lhs_ty, rhs_ty, stmt->source, true).result;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (!ty) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return validator_.Assignment(stmt, ty);
|
|
|
|
|
});
|
2022-03-31 22:30:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Statement* Resolver::ContinueStatement(const ast::ContinueStatement* stmt) {
|
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
sem->Behaviors() = sem::Behavior::kContinue;
|
|
|
|
|
|
|
|
|
|
// Set if we've hit the first continue statement in our parent loop
|
|
|
|
|
if (auto* block = sem->FindFirstParent<sem::LoopBlockStatement>()) {
|
|
|
|
|
if (!block->FirstContinue()) {
|
|
|
|
|
const_cast<sem::LoopBlockStatement*>(block)->SetFirstContinue(
|
|
|
|
|
stmt, block->Decls().size());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-21 23:04:44 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.ContinueStatement(sem, current_statement_);
|
|
|
|
|
});
|
2021-11-26 16:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sem::Statement* Resolver::DiscardStatement(const ast::DiscardStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
sem->Behaviors() = sem::Behavior::kDiscard;
|
|
|
|
|
current_function_->SetHasDiscard();
|
|
|
|
|
|
|
|
|
|
return validator_.DiscardStatement(sem, current_statement_);
|
|
|
|
|
});
|
2021-11-26 16:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
sem::Statement* Resolver::FallthroughStatement(const ast::FallthroughStatement* stmt) {
|
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
sem->Behaviors() = sem::Behavior::kFallthrough;
|
2021-12-03 15:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.FallthroughStatement(sem);
|
|
|
|
|
});
|
2021-05-18 10:28:48 +00:00
|
|
|
|
}
|
2021-03-31 13:26:43 +00:00
|
|
|
|
|
2022-04-07 13:42:45 +00:00
|
|
|
|
sem::Statement* Resolver::IncrementDecrementStatement(
|
|
|
|
|
const ast::IncrementDecrementStatement* stmt) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
auto* sem =
|
|
|
|
|
builder_->create<sem::Statement>(stmt, current_compound_statement_, current_function_);
|
|
|
|
|
return StatementScope(stmt, sem, [&] {
|
|
|
|
|
auto* lhs = Expression(stmt->lhs);
|
|
|
|
|
if (!lhs) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sem->Behaviors() = lhs->Behaviors();
|
2022-04-07 13:42:45 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return validator_.IncrementDecrementStatement(stmt);
|
|
|
|
|
});
|
2022-04-07 13:42:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
ty = const_cast<sem::Type*>(ty->UnwrapRef());
|
|
|
|
|
|
|
|
|
|
if (auto* str = ty->As<sem::Struct>()) {
|
|
|
|
|
if (str->StorageClassUsage().count(sc)) {
|
|
|
|
|
return true; // Already applied
|
|
|
|
|
}
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
str->AddUsage(sc);
|
|
|
|
|
|
|
|
|
|
for (auto* member : str->Members()) {
|
2022-07-29 14:32:51 +00:00
|
|
|
|
if (!ApplyStorageClassUsageToType(sc, const_cast<sem::Type*>(member->Type()), usage)) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
std::stringstream err;
|
|
|
|
|
err << "while analysing structure member " << sem_.TypeNameOf(str) << "."
|
|
|
|
|
<< builder_->Symbols().NameFor(member->Declaration()->symbol);
|
|
|
|
|
AddNote(err.str(), member->Declaration()->source);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2021-03-17 22:47:33 +00:00
|
|
|
|
}
|
2021-05-07 14:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (auto* arr = ty->As<sem::Array>()) {
|
|
|
|
|
if (arr->IsRuntimeSized() && sc != ast::StorageClass::kStorage) {
|
|
|
|
|
AddError(
|
|
|
|
|
"runtime-sized arrays can only be used in the <storage> storage "
|
|
|
|
|
"class",
|
|
|
|
|
usage);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ApplyStorageClassUsageToType(sc, const_cast<sem::Type*>(arr->ElemType()), usage);
|
|
|
|
|
}
|
2021-05-07 14:49:34 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (ast::IsHostShareable(sc) && !validator_.IsHostShareable(ty)) {
|
2021-03-17 22:47:33 +00:00
|
|
|
|
std::stringstream err;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
err << "Type '" << sem_.TypeNameOf(ty) << "' cannot be used in storage class '" << sc
|
|
|
|
|
<< "' as it is non-host-shareable";
|
|
|
|
|
AddError(err.str(), usage);
|
2021-03-17 22:47:33 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return true;
|
2021-03-17 22:47:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 16:26:42 +00:00
|
|
|
|
template <typename SEM, typename F>
|
2022-05-01 14:40:55 +00:00
|
|
|
|
SEM* Resolver::StatementScope(const ast::Statement* ast, SEM* sem, F&& callback) {
|
|
|
|
|
builder_->Sem().Add(ast, sem);
|
|
|
|
|
|
|
|
|
|
auto* as_compound = As<sem::CompoundStatement, CastFlags::kDontErrorOnImpossibleCast>(sem);
|
|
|
|
|
auto* as_block = As<sem::BlockStatement, CastFlags::kDontErrorOnImpossibleCast>(sem);
|
2021-07-14 09:44:41 +00:00
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
|
TINT_SCOPED_ASSIGNMENT(current_statement_, sem);
|
|
|
|
|
TINT_SCOPED_ASSIGNMENT(current_compound_statement_,
|
|
|
|
|
as_compound ? as_compound : current_compound_statement_);
|
|
|
|
|
TINT_SCOPED_ASSIGNMENT(current_block_, as_block ? as_block : current_block_);
|
|
|
|
|
|
|
|
|
|
if (!callback()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sem;
|
2021-03-09 15:06:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 11:44:57 +00:00
|
|
|
|
bool Resolver::Mark(const ast::Node* node) {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
if (node == nullptr) {
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "Resolver::Mark() called with nullptr";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-07-21 23:46:15 +00:00
|
|
|
|
auto marked_bit_ref = marked_[node->node_id.value];
|
|
|
|
|
if (!marked_bit_ref) {
|
|
|
|
|
marked_bit_ref = true;
|
2022-05-01 14:40:55 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
TINT_ICE(Resolver, diagnostics_) << "AST node '" << node->TypeInfo().name
|
|
|
|
|
<< "' was encountered twice in the same AST of a Program\n"
|
|
|
|
|
<< "At: " << node->source << "\n"
|
|
|
|
|
<< "Pointer: " << node;
|
2021-11-22 11:44:57 +00:00
|
|
|
|
return false;
|
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 {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
diagnostics_.add_error(diag::System::Resolver, msg, source);
|
2021-06-24 11:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resolver::AddWarning(const std::string& msg, const Source& source) const {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
diagnostics_.add_warning(diag::System::Resolver, msg, source);
|
2021-06-24 11:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resolver::AddNote(const std::string& msg, const Source& source) const {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
diagnostics_.add_note(diag::System::Resolver, msg, source);
|
2021-06-24 11:27:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 23:07:11 +00:00
|
|
|
|
bool Resolver::IsBuiltin(Symbol symbol) const {
|
2022-05-01 14:40:55 +00:00
|
|
|
|
std::string name = builder_->Symbols().NameFor(symbol);
|
|
|
|
|
return sem::ParseBuiltinType(name) != sem::BuiltinType::kNone;
|
2021-11-23 20:45:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 17:45:45 +00:00
|
|
|
|
} // namespace tint::resolver
|