2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
#include "src/resolver/resolver.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-01 20:01:39 +00:00
|
|
|
#include <algorithm>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
2020-04-07 12:57:42 +00:00
|
|
|
|
2020-04-07 12:47:23 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2020-09-22 22:07:13 +00:00
|
|
|
#include "src/ast/bitcast_expression.h"
|
2020-04-07 12:54:10 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
2020-07-21 13:42:13 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2020-04-07 12:54:29 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/discard_statement.h"
|
|
|
|
#include "src/ast/fallthrough_statement.h"
|
2020-04-07 12:55:25 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2020-04-07 12:55:51 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2020-04-07 12:56:24 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2021-03-18 17:59:54 +00:00
|
|
|
#include "src/ast/struct_block_decoration.h"
|
2020-04-07 12:56:45 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2020-04-07 19:27:21 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2020-04-07 12:57:12 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2021-03-15 10:43:11 +00:00
|
|
|
#include "src/semantic/array.h"
|
2021-02-03 21:02:25 +00:00
|
|
|
#include "src/semantic/call.h"
|
2021-02-03 16:43:20 +00:00
|
|
|
#include "src/semantic/function.h"
|
2021-02-03 23:55:56 +00:00
|
|
|
#include "src/semantic/member_accessor_expression.h"
|
2021-02-16 18:45:45 +00:00
|
|
|
#include "src/semantic/statement.h"
|
2021-03-15 10:43:11 +00:00
|
|
|
#include "src/semantic/struct.h"
|
2021-02-03 17:51:09 +00:00
|
|
|
#include "src/semantic/variable.h"
|
2021-03-15 10:43:11 +00:00
|
|
|
#include "src/type/access_control_type.h"
|
2021-03-15 13:37:41 +00:00
|
|
|
#include "src/utils/math.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
2021-03-09 15:08:48 +00:00
|
|
|
namespace resolver {
|
2021-02-08 19:53:42 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using IntrinsicType = tint::semantic::IntrinsicType;
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
// Helper class that temporarily assigns a value to a reference for the scope of
|
|
|
|
// the object. Once the ScopedAssignment is destructed, the original value is
|
|
|
|
// restored.
|
|
|
|
template <typename T>
|
|
|
|
class ScopedAssignment {
|
|
|
|
public:
|
|
|
|
ScopedAssignment(T& ref, T val) : ref_(ref) {
|
|
|
|
old_value_ = ref;
|
|
|
|
ref = val;
|
|
|
|
}
|
|
|
|
~ScopedAssignment() { ref_ = old_value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T& ref_;
|
|
|
|
T old_value_;
|
|
|
|
};
|
|
|
|
|
2021-03-18 15:43:14 +00:00
|
|
|
// Helper function that returns the range union of two source locations. The
|
|
|
|
// `start` and `end` locations are assumed to refer to the same source file.
|
|
|
|
Source CombineSourceRange(const Source& start, const Source& end) {
|
|
|
|
return Source(Source::Range(start.range.begin, end.range.end),
|
|
|
|
start.file_path, start.file_content);
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:53:42 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::Resolver(ProgramBuilder* builder)
|
2021-02-08 22:42:54 +00:00
|
|
|
: builder_(builder), intrinsic_table_(IntrinsicTable::Create()) {}
|
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
|
|
|
Resolver::BlockInfo::BlockInfo(Resolver::BlockInfo::Type ty,
|
2021-03-09 15:06:37 +00:00
|
|
|
Resolver::BlockInfo* p)
|
|
|
|
: type(ty), parent(p) {}
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::BlockInfo::~BlockInfo() = default;
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::set_referenced_from_function_if_needed(VariableInfo* var,
|
|
|
|
bool local) {
|
2020-06-22 20:52:24 +00:00
|
|
|
if (current_function_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-03 17:51:09 +00:00
|
|
|
if (var->storage_class == ast::StorageClass::kNone ||
|
|
|
|
var->storage_class == ast::StorageClass::kFunction) {
|
2020-06-22 20:52:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
if (local) {
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->local_referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Resolve() {
|
|
|
|
bool result = ResolveInternal();
|
2021-02-03 17:51:09 +00:00
|
|
|
|
|
|
|
// Even if resolving failed, create all the semantic nodes for information we
|
|
|
|
// did generate.
|
|
|
|
CreateSemanticNodes();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:39:54 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#storable-types
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Resolver::IsStorable(type::Type* type) {
|
2021-03-18 16:39:54 +00:00
|
|
|
type = type->UnwrapIfNeeded();
|
2021-03-15 10:43:11 +00:00
|
|
|
if (type->is_scalar() || type->Is<type::Vector>() ||
|
|
|
|
type->Is<type::Matrix>()) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-18 16:39:54 +00:00
|
|
|
if (type::Array* arr = type->As<type::Array>()) {
|
|
|
|
return IsStorable(arr->type());
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
2021-03-18 16:39:54 +00:00
|
|
|
if (type::Struct* str = type->As<type::Struct>()) {
|
|
|
|
for (const auto* member : str->impl()->members()) {
|
2021-03-15 10:43:11 +00:00
|
|
|
if (!IsStorable(member->type())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:46:24 +00:00
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#host-shareable-types
|
2021-03-18 21:03:24 +00:00
|
|
|
bool Resolver::IsHostShareable(type::Type* type) {
|
2021-03-18 20:46:24 +00:00
|
|
|
type = type->UnwrapIfNeeded();
|
|
|
|
if (type->IsAnyOf<type::I32, type::U32, type::F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (auto* vec = type->As<type::Vector>()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
return IsHostShareable(vec->type());
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
|
|
|
if (auto* mat = type->As<type::Matrix>()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
return IsHostShareable(mat->type());
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
|
|
|
if (auto* arr = type->As<type::Array>()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
return IsHostShareable(arr->type());
|
2021-03-18 20:46:24 +00:00
|
|
|
}
|
|
|
|
if (auto* str = type->As<type::Struct>()) {
|
|
|
|
for (auto* member : str->impl()->members()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
if (!IsHostShareable(member->type())) {
|
2021-03-18 20:46:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-18 21:14:44 +00:00
|
|
|
bool Resolver::IsValidAssignment(type::Type* lhs, type::Type* rhs) {
|
|
|
|
// TODO(crbug.com/tint/659): This is a rough approximation, and is missing
|
|
|
|
// checks for writability of pointer storage class, access control, etc.
|
|
|
|
// This will need to be fixed after WGSL agrees the behavior of pointers /
|
|
|
|
// references.
|
|
|
|
// Check:
|
|
|
|
if (lhs->UnwrapIfNeeded() != rhs->UnwrapIfNeeded()) {
|
|
|
|
// Try RHS dereference
|
|
|
|
if (lhs->UnwrapIfNeeded() != rhs->UnwrapAll()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::ResolveInternal() {
|
2021-03-19 18:45:30 +00:00
|
|
|
// Process non-user-defined types (arrays). The rest will be processed in
|
|
|
|
// order of declaration below.
|
2021-03-15 10:43:11 +00:00
|
|
|
for (auto* ty : builder_->Types()) {
|
|
|
|
if (auto* arr = ty->As<type::Array>()) {
|
|
|
|
if (!Array(arr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:45:30 +00:00
|
|
|
// Process everything else in the order they appear in the module. This is
|
|
|
|
// necessary for validation of use-before-declaration.
|
|
|
|
for (auto* decl : builder_->AST().GlobalDeclarations()) {
|
|
|
|
if (decl->Is<type::Type>()) {
|
|
|
|
if (auto* str = decl->As<type::Struct>()) {
|
|
|
|
if (!Structure(str)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (auto* arr = decl->As<type::Array>()) {
|
|
|
|
if (!Array(arr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (auto* func = decl->As<ast::Function>()) {
|
|
|
|
if (!Function(func)) {
|
2020-06-15 20:55:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-19 18:45:30 +00:00
|
|
|
} else if (auto* var = decl->As<ast::Variable>()) {
|
|
|
|
variable_stack_.set_global(var->symbol(), CreateVariableInfo(var));
|
2020-04-06 21:07:41 +00:00
|
|
|
|
2021-03-19 18:45:30 +00:00
|
|
|
if (var->has_constructor()) {
|
|
|
|
if (!Expression(var->constructor())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
|
2021-03-19 18:45:30 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(var->declared_storage_class(),
|
|
|
|
var->type(), var->source())) {
|
|
|
|
diagnostics_.add_note("while instantiating variable " +
|
|
|
|
builder_->Symbols().NameFor(var->symbol()),
|
|
|
|
var->source());
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 18:45:30 +00:00
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-19 14:04:51 +00:00
|
|
|
bool Resolver::ValidateParameter(const ast::Variable* param) {
|
|
|
|
if (auto* r = param->type()->UnwrapAll()->As<type::Array>()) {
|
|
|
|
if (r->IsRuntimeArray()) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0015",
|
|
|
|
"runtime arrays may only appear as the last member of a struct",
|
|
|
|
param->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::ValidateFunction(const ast::Function* func) {
|
|
|
|
if (symbol_to_function_.find(func->symbol()) != symbol_to_function_.end()) {
|
|
|
|
diagnostics_.add_error("v-0016",
|
|
|
|
"function names must be unique '" +
|
|
|
|
builder_->Symbols().NameFor(func->symbol()) +
|
|
|
|
"'",
|
|
|
|
func->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* param : func->params()) {
|
|
|
|
if (!ValidateParameter(param)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func->return_type()->Is<type::Void>()) {
|
|
|
|
if (!func->get_last_statement() ||
|
|
|
|
!func->get_last_statement()->Is<ast::ReturnStatement>()) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0002", "non-void function must end with a return statement",
|
|
|
|
func->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* deco : func->return_type_decorations()) {
|
|
|
|
if (!(deco->Is<ast::BuiltinDecoration>() ||
|
|
|
|
deco->Is<ast::LocationDecoration>())) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"decoration is not valid for function return types",
|
|
|
|
deco->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Function(ast::Function* func) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto* func_info = function_infos_.Create<FunctionInfo>(func);
|
|
|
|
|
2021-03-19 14:04:51 +00:00
|
|
|
if (!ValidateFunction(func)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
ScopedAssignment<FunctionInfo*> sa(current_function_, func_info);
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
variable_stack_.push_scope();
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* param : func->params()) {
|
2021-02-03 17:51:09 +00:00
|
|
|
variable_stack_.set(param->symbol(), CreateVariableInfo(param));
|
2020-06-22 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!BlockStatement(func->body())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
variable_stack_.pop_scope();
|
|
|
|
|
2021-02-24 22:11:34 +00:00
|
|
|
// Register the function information _after_ processing the statements. This
|
|
|
|
// allows us to catch a function calling itself when determining the call
|
|
|
|
// information as this function doesn't exist until it's finished.
|
|
|
|
symbol_to_function_[func->symbol()] = func_info;
|
|
|
|
function_to_info_.emplace(func, func_info);
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::BlockStatement(const ast::BlockStatement* stmt) {
|
2021-03-09 15:06:37 +00:00
|
|
|
return BlockScope(BlockInfo::Type::kGeneric,
|
|
|
|
[&] { return Statements(stmt->list()); });
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Statements(const ast::StatementList& stmts) {
|
2021-03-09 10:26:57 +00:00
|
|
|
for (auto* stmt : stmts) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Statement(stmt)) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Statement(ast::Statement* stmt) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto* sem_statement = builder_->create<semantic::Statement>(stmt);
|
|
|
|
|
|
|
|
ScopedAssignment<semantic::Statement*> sa(current_statement_, sem_statement);
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = stmt->As<ast::AssignmentStatement>()) {
|
2021-03-18 21:14:44 +00:00
|
|
|
if (!Expression(a->lhs()) || !Expression(a->rhs())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// TODO(crbug.com/tint/659): This logic needs updating once pointers are
|
|
|
|
// pinned down in the WGSL spec.
|
|
|
|
auto* lhs_type = TypeOf(a->lhs())->UnwrapAll();
|
|
|
|
auto* rhs_type = TypeOf(a->rhs());
|
|
|
|
if (!IsValidAssignment(lhs_type, rhs_type)) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"invalid assignment: cannot assign value of type '" +
|
|
|
|
rhs_type->FriendlyName(builder_->Symbols()) +
|
|
|
|
"' to a variable of type '" +
|
|
|
|
lhs_type->FriendlyName(builder_->Symbols()) + "'",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-04-07 12:47:23 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = stmt->As<ast::BlockStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return BlockStatement(b);
|
2020-07-27 15:25:00 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::BreakStatement>()) {
|
2021-03-09 15:06:37 +00:00
|
|
|
if (!current_block_->FindFirstParent(BlockInfo::Type::kLoop) &&
|
|
|
|
!current_block_->FindFirstParent(BlockInfo::Type::kSwitchCase)) {
|
|
|
|
diagnostics_.add_error("break statement must be in a loop or switch case",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:10 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = stmt->As<ast::CallStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(c->expr());
|
2020-07-21 13:42:13 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = stmt->As<ast::CaseStatement>()) {
|
2021-03-09 15:06:37 +00:00
|
|
|
return CaseStatement(c);
|
2020-04-07 12:54:20 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::ContinueStatement>()) {
|
2021-03-09 10:26:57 +00:00
|
|
|
// Set if we've hit the first continue statement in our parent loop
|
|
|
|
if (auto* loop_block =
|
2021-03-09 15:06:37 +00:00
|
|
|
current_block_->FindFirstParent(BlockInfo::Type::kLoop)) {
|
2021-03-09 10:26:57 +00:00
|
|
|
if (loop_block->first_continue == size_t(~0)) {
|
|
|
|
loop_block->first_continue = loop_block->decls.size();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diagnostics_.add_error("continue statement must be in a loop",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:29 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::DiscardStatement>()) {
|
2020-07-25 14:33:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* e = stmt->As<ast::ElseStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(e->condition()) && BlockStatement(e->body());
|
2020-04-07 12:54:37 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::FallthroughStatement>()) {
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* i = stmt->As<ast::IfStatement>()) {
|
2021-03-09 15:17:28 +00:00
|
|
|
return IfStatement(i);
|
2020-04-07 12:55:25 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* l = stmt->As<ast::LoopStatement>()) {
|
2021-03-09 10:26:57 +00:00
|
|
|
// We don't call DetermineBlockStatement on the body and continuing block as
|
|
|
|
// these would make their BlockInfo siblings as in the AST, but we want the
|
|
|
|
// body BlockInfo to parent the continuing BlockInfo for semantics and
|
|
|
|
// validation. Also, we need to set their types differently.
|
2021-03-09 15:06:37 +00:00
|
|
|
return BlockScope(BlockInfo::Type::kLoop, [&] {
|
|
|
|
if (!Statements(l->body()->list())) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 15:06:37 +00:00
|
|
|
|
|
|
|
if (l->has_continuing()) {
|
|
|
|
if (!BlockScope(BlockInfo::Type::kLoopContinuing,
|
|
|
|
[&] { return Statements(l->continuing()->list()); })) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2020-04-07 12:55:51 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* r = stmt->As<ast::ReturnStatement>()) {
|
2021-03-22 23:20:17 +00:00
|
|
|
return Return(r);
|
2020-04-07 12:56:24 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* s = stmt->As<ast::SwitchStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(s->condition())) {
|
2020-04-07 12:56:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* case_stmt : s->body()) {
|
2021-03-09 15:06:37 +00:00
|
|
|
if (!CaseStatement(case_stmt)) {
|
2020-04-07 12:56:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
|
2021-03-17 22:47:33 +00:00
|
|
|
return VariableDeclStatement(v);
|
2020-04-07 12:57:12 +00:00
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"unknown statement type for type determination: " + builder_->str(stmt),
|
|
|
|
stmt->source());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
bool Resolver::CaseStatement(ast::CaseStatement* stmt) {
|
|
|
|
return BlockScope(BlockInfo::Type::kSwitchCase,
|
|
|
|
[&] { return Statements(stmt->body()->list()); });
|
2021-03-09 15:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::IfStatement(ast::IfStatement* stmt) {
|
|
|
|
if (!Expression(stmt->condition())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* cond_type = TypeOf(stmt->condition())->UnwrapAll();
|
|
|
|
if (cond_type != builder_->ty.bool_()) {
|
|
|
|
diagnostics_.add_error("if statement condition must be bool, got " +
|
|
|
|
cond_type->FriendlyName(builder_->Symbols()),
|
|
|
|
stmt->condition()->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BlockStatement(stmt->body())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* else_stmt : stmt->else_statements()) {
|
|
|
|
if (!Statement(else_stmt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2021-03-09 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Expressions(const ast::ExpressionList& list) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* expr : list) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(expr)) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Expression(ast::Expression* expr) {
|
2020-04-06 21:07:41 +00:00
|
|
|
// This is blindly called above, so in some cases the expression won't exist.
|
|
|
|
if (!expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
if (TypeOf(expr)) {
|
|
|
|
return true; // Already resolved
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = expr->As<ast::ArrayAccessorExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return ArrayAccessor(a);
|
2020-04-07 12:57:42 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = expr->As<ast::BinaryExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Binary(b);
|
2020-04-07 19:27:41 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = expr->As<ast::BitcastExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Bitcast(b);
|
2020-09-22 22:07:13 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = expr->As<ast::CallExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Call(c);
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = expr->As<ast::ConstructorExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Constructor(c);
|
2020-04-07 12:46:30 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* i = expr->As<ast::IdentifierExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Identifier(i);
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* m = expr->As<ast::MemberAccessorExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return MemberAccessor(m);
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* u = expr->As<ast::UnaryOpExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return UnaryOp(u);
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("unknown expression for type determination",
|
|
|
|
expr->source());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::ArrayAccessor(ast::ArrayAccessorExpression* expr) {
|
|
|
|
if (!Expression(expr->array())) {
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(expr->idx_expr())) {
|
2020-05-01 16:17:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* res = TypeOf(expr->array());
|
2020-10-28 20:32:22 +00:00
|
|
|
auto* parent_type = res->UnwrapAll();
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Type* ret = nullptr;
|
|
|
|
if (auto* arr = parent_type->As<type::Array>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
ret = arr->type();
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* vec = parent_type->As<type::Vector>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
ret = vec->type();
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* mat = parent_type->As<type::Matrix>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Vector>(mat->type(), mat->rows());
|
2020-04-07 12:57:42 +00:00
|
|
|
} else {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("invalid parent type (" + parent_type->type_name() +
|
|
|
|
") in array accessor",
|
|
|
|
expr->source());
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* arr = parent_type->As<type::Array>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
if (!arr->type()->is_scalar()) {
|
|
|
|
// If we extract a non-scalar from an array then we also get a pointer. We
|
2021-03-09 15:06:37 +00:00
|
|
|
// will generate a Function storage class variable to store this into.
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ast::StorageClass::kFunction);
|
2020-12-01 21:07:27 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Bitcast(ast::BitcastExpression* expr) {
|
|
|
|
if (!Expression(expr->expr())) {
|
2020-06-18 18:02:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, expr->type());
|
2020-04-07 12:57:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Call(ast::CallExpression* call) {
|
|
|
|
if (!Expressions(call->params())) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
2020-04-20 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// The expression has to be an identifier as you can't store function pointers
|
|
|
|
// but, if it isn't we'll just use the normal result determination to be on
|
|
|
|
// the safe side.
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* ident = call->func()->As<ast::IdentifierExpression>();
|
|
|
|
if (!ident) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("call target is not an identifier", call->source());
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol());
|
|
|
|
|
2021-03-04 23:00:46 +00:00
|
|
|
auto intrinsic_type = semantic::ParseIntrinsicType(name);
|
2021-02-08 22:31:44 +00:00
|
|
|
if (intrinsic_type != IntrinsicType::kNone) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!IntrinsicCall(call, intrinsic_type)) {
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (current_function_) {
|
|
|
|
auto callee_func_it = symbol_to_function_.find(ident->symbol());
|
|
|
|
if (callee_func_it == symbol_to_function_.end()) {
|
2021-02-24 22:11:34 +00:00
|
|
|
if (current_function_->declaration->symbol() == ident->symbol()) {
|
2021-03-22 17:42:06 +00:00
|
|
|
diagnostics_.add_error("v-0004",
|
|
|
|
"recursion is not permitted. '" + name +
|
2021-02-24 22:11:34 +00:00
|
|
|
"' attempted to call itself.",
|
|
|
|
call->source());
|
|
|
|
} else {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0006: unable to find called function: " + name,
|
|
|
|
call->source());
|
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* callee_func = callee_func_it->second;
|
2020-07-14 20:37:28 +00:00
|
|
|
|
2021-03-03 19:54:44 +00:00
|
|
|
// Note: Requires called functions to be resolved first.
|
|
|
|
// This is currently guaranteed as functions must be declared before use.
|
|
|
|
current_function_->transitive_calls.add(callee_func);
|
|
|
|
for (auto* transitive_call : callee_func->transitive_calls) {
|
|
|
|
current_function_->transitive_calls.add(transitive_call);
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
for (auto* var : callee_func->referenced_module_vars) {
|
|
|
|
set_referenced_from_function_if_needed(var, false);
|
2020-07-14 19:45:47 +00:00
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
|
|
|
auto iter = symbol_to_function_.find(ident->symbol());
|
|
|
|
if (iter == symbol_to_function_.end()) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0005: function must be declared before use: '" + name + "'",
|
|
|
|
call->source());
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-18 02:10:03 +00:00
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* function = iter->second;
|
2021-02-16 18:45:45 +00:00
|
|
|
function_calls_.emplace(call,
|
|
|
|
FunctionCallInfo{function, current_statement_});
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(call, function->declaration->return_type());
|
2020-08-18 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::IntrinsicCall(ast::CallExpression* call,
|
|
|
|
semantic::IntrinsicType intrinsic_type) {
|
2021-02-08 22:31:44 +00:00
|
|
|
std::vector<type::Type*> arg_tys;
|
|
|
|
arg_tys.reserve(call->params().size());
|
|
|
|
for (auto* expr : call->params()) {
|
|
|
|
arg_tys.emplace_back(TypeOf(expr));
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
auto result = intrinsic_table_->Lookup(*builder_, intrinsic_type, arg_tys,
|
|
|
|
call->source());
|
2021-02-08 22:42:54 +00:00
|
|
|
if (!result.intrinsic) {
|
|
|
|
// Intrinsic lookup failed.
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add(result.diagnostics);
|
2021-02-08 22:42:54 +00:00
|
|
|
return false;
|
2020-07-21 17:44:44 +00:00
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
builder_->Sem().Add(call, builder_->create<semantic::Call>(
|
2021-02-16 21:15:01 +00:00
|
|
|
call, result.intrinsic, current_statement_));
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(call, result.intrinsic->ReturnType());
|
2020-09-22 19:42:13 +00:00
|
|
|
return true;
|
2020-06-01 13:43:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Constructor(ast::ConstructorExpression* expr) {
|
2021-03-15 21:21:33 +00:00
|
|
|
if (auto* type_ctor = expr->As<ast::TypeConstructorExpression>()) {
|
|
|
|
for (auto* value : type_ctor->values()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(value)) {
|
2020-04-24 00:41:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 21:21:33 +00:00
|
|
|
SetType(expr, type_ctor->type());
|
|
|
|
|
|
|
|
// Now that the argument types have been determined, make sure that they
|
|
|
|
// obey the constructor type rules laid out in
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#type-constructor-expr.
|
|
|
|
if (auto* vec_type = type_ctor->type()->As<type::Vector>()) {
|
2021-03-18 15:43:14 +00:00
|
|
|
return VectorConstructor(vec_type, type_ctor->values());
|
|
|
|
}
|
|
|
|
if (auto* mat_type = type_ctor->type()->As<type::Matrix>()) {
|
|
|
|
return MatrixConstructor(mat_type, type_ctor->values());
|
2021-03-15 21:21:33 +00:00
|
|
|
}
|
|
|
|
// TODO(crbug.com/tint/634): Validate array constructor
|
|
|
|
} else if (auto* scalar_ctor = expr->As<ast::ScalarConstructorExpression>()) {
|
|
|
|
SetType(expr, scalar_ctor->literal()->type());
|
2020-04-07 12:46:30 +00:00
|
|
|
} else {
|
2021-03-15 21:21:33 +00:00
|
|
|
TINT_ICE(diagnostics_) << "unexpected constructor expression type";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:43:14 +00:00
|
|
|
bool Resolver::VectorConstructor(const type::Vector* vec_type,
|
2021-03-15 21:21:33 +00:00
|
|
|
const ast::ExpressionList& values) {
|
2021-03-18 15:43:14 +00:00
|
|
|
type::Type* elem_type = vec_type->type()->UnwrapAll();
|
2021-03-15 21:21:33 +00:00
|
|
|
size_t value_cardinality_sum = 0;
|
|
|
|
for (auto* value : values) {
|
|
|
|
type::Type* value_type = TypeOf(value)->UnwrapAll();
|
|
|
|
if (value_type->is_scalar()) {
|
|
|
|
if (elem_type != value_type) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"type in vector constructor does not match vector type: "
|
|
|
|
"expected '" +
|
|
|
|
elem_type->FriendlyName(builder_->Symbols()) + "', found '" +
|
|
|
|
value_type->FriendlyName(builder_->Symbols()) + "'",
|
|
|
|
value->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_cardinality_sum++;
|
|
|
|
} else if (auto* value_vec = value_type->As<type::Vector>()) {
|
|
|
|
type::Type* value_elem_type = value_vec->type()->UnwrapAll();
|
|
|
|
// A mismatch of vector type parameter T is only an error if multiple
|
|
|
|
// arguments are present. A single argument constructor constitutes a
|
|
|
|
// type conversion expression.
|
|
|
|
// NOTE: A conversion expression from a vec<bool> to any other vecN<T>
|
|
|
|
// is disallowed (see
|
|
|
|
// https://gpuweb.github.io/gpuweb/wgsl.html#conversion-expr).
|
|
|
|
if (elem_type != value_elem_type &&
|
|
|
|
(values.size() > 1u || value_vec->is_bool_vector())) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"type in vector constructor does not match vector type: "
|
|
|
|
"expected '" +
|
|
|
|
elem_type->FriendlyName(builder_->Symbols()) + "', found '" +
|
|
|
|
value_elem_type->FriendlyName(builder_->Symbols()) + "'",
|
|
|
|
value->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_cardinality_sum += value_vec->size();
|
|
|
|
} else {
|
|
|
|
// A vector constructor can only accept vectors and scalars.
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"expected vector or scalar type in vector constructor; found: " +
|
|
|
|
value_type->FriendlyName(builder_->Symbols()),
|
|
|
|
value->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A correct vector constructor must either be a zero-value expression
|
|
|
|
// or the number of components of all constructor arguments must add up
|
|
|
|
// to the vector cardinality.
|
2021-03-18 15:43:14 +00:00
|
|
|
if (value_cardinality_sum > 0 && value_cardinality_sum != vec_type->size()) {
|
2021-03-15 21:21:33 +00:00
|
|
|
if (values.empty()) {
|
|
|
|
TINT_ICE(diagnostics_)
|
|
|
|
<< "constructor arguments expected to be non-empty!";
|
|
|
|
}
|
|
|
|
const Source& values_start = values[0]->source();
|
|
|
|
const Source& values_end = values[values.size() - 1]->source();
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"attempted to construct '" +
|
2021-03-18 15:43:14 +00:00
|
|
|
vec_type->FriendlyName(builder_->Symbols()) + "' with " +
|
2021-03-15 21:21:33 +00:00
|
|
|
std::to_string(value_cardinality_sum) + " component(s)",
|
2021-03-18 15:43:14 +00:00
|
|
|
CombineSourceRange(values_start, values_end));
|
2021-03-15 21:21:33 +00:00
|
|
|
return false;
|
2020-04-07 12:46:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:43:14 +00:00
|
|
|
bool Resolver::MatrixConstructor(const type::Matrix* matrix_type,
|
|
|
|
const ast::ExpressionList& values) {
|
|
|
|
// Zero Value expression
|
|
|
|
if (values.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
type::Type* elem_type = matrix_type->type()->UnwrapAll();
|
|
|
|
if (matrix_type->columns() != values.size()) {
|
|
|
|
const Source& values_start = values[0]->source();
|
|
|
|
const Source& values_end = values[values.size() - 1]->source();
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"expected " + std::to_string(matrix_type->columns()) + " '" +
|
|
|
|
VectorPretty(matrix_type->rows(), elem_type) + "' arguments in '" +
|
|
|
|
matrix_type->FriendlyName(builder_->Symbols()) +
|
|
|
|
"' constructor, found " + std::to_string(values.size()),
|
|
|
|
CombineSourceRange(values_start, values_end));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* value : values) {
|
|
|
|
type::Type* value_type = TypeOf(value)->UnwrapAll();
|
|
|
|
auto* value_vec = value_type->As<type::Vector>();
|
|
|
|
|
|
|
|
if (!value_vec || value_vec->size() != matrix_type->rows() ||
|
|
|
|
elem_type != value_vec->type()->UnwrapAll()) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"expected argument type '" +
|
|
|
|
VectorPretty(matrix_type->rows(), elem_type) + "' in '" +
|
|
|
|
matrix_type->FriendlyName(builder_->Symbols()) +
|
|
|
|
"' constructor, found '" +
|
|
|
|
value_type->FriendlyName(builder_->Symbols()) + "'",
|
|
|
|
value->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Identifier(ast::IdentifierExpression* expr) {
|
2021-01-11 15:10:19 +00:00
|
|
|
auto symbol = expr->symbol();
|
2021-02-03 17:51:09 +00:00
|
|
|
VariableInfo* var;
|
2021-01-11 15:10:19 +00:00
|
|
|
if (variable_stack_.get(symbol, &var)) {
|
2020-04-23 22:26:52 +00:00
|
|
|
// A constant is the type, but a variable is always a pointer so synthesize
|
|
|
|
// the pointer around the variable type.
|
2021-02-03 17:51:09 +00:00
|
|
|
if (var->declaration->is_const()) {
|
|
|
|
SetType(expr, var->declaration->type());
|
|
|
|
} else if (var->declaration->type()->Is<type::Pointer>()) {
|
|
|
|
SetType(expr, var->declaration->type());
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2021-02-03 17:51:09 +00:00
|
|
|
SetType(expr, builder_->create<type::Pointer>(var->declaration->type(),
|
|
|
|
var->storage_class));
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2021-02-16 21:15:01 +00:00
|
|
|
var->users.push_back(expr);
|
2020-12-08 21:07:24 +00:00
|
|
|
set_referenced_from_function_if_needed(var, true);
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-03-22 17:25:56 +00:00
|
|
|
if (current_block_) {
|
|
|
|
// 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_block_->FindFirstParent(
|
|
|
|
BlockInfo::Type::kLoopContinuing)) {
|
|
|
|
auto* loop_block =
|
|
|
|
continuing_block->FindFirstParent(BlockInfo::Type::kLoop);
|
|
|
|
if (loop_block->first_continue != size_t(~0)) {
|
|
|
|
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->first_continue) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"continue statement bypasses declaration of '" +
|
|
|
|
builder_->Symbols().NameFor(symbol) +
|
|
|
|
"' in continuing block",
|
|
|
|
expr->source());
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-11 22:02:42 +00:00
|
|
|
auto iter = symbol_to_function_.find(symbol);
|
2021-01-11 16:24:32 +00:00
|
|
|
if (iter != symbol_to_function_.end()) {
|
2021-02-24 13:31:22 +00:00
|
|
|
diagnostics_.add_error("missing '(' for function call",
|
|
|
|
expr->source().End());
|
|
|
|
return false;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
std::string name = builder_->Symbols().NameFor(symbol);
|
2021-03-04 23:00:46 +00:00
|
|
|
if (semantic::ParseIntrinsicType(name) != IntrinsicType::kNone) {
|
2021-02-24 13:31:22 +00:00
|
|
|
diagnostics_.add_error("missing '(' for intrinsic call",
|
|
|
|
expr->source().End());
|
|
|
|
return false;
|
2020-10-14 18:26:31 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0006: identifier must be declared before use: " + name,
|
|
|
|
expr->source());
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) {
|
|
|
|
if (!Expression(expr->structure())) {
|
2020-04-07 16:41:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* res = TypeOf(expr->structure());
|
2020-10-28 20:32:22 +00:00
|
|
|
auto* data_type = res->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
|
2020-04-24 00:40:45 +00:00
|
|
|
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Type* ret = nullptr;
|
2021-02-24 14:15:02 +00:00
|
|
|
std::vector<uint32_t> swizzle;
|
2021-02-03 23:55:56 +00:00
|
|
|
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ty = data_type->As<type::Struct>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* strct = ty->impl();
|
2021-01-11 16:24:32 +00:00
|
|
|
auto symbol = expr->member()->symbol();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* member : strct->members()) {
|
2021-01-11 16:24:32 +00:00
|
|
|
if (member->symbol() == symbol) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = member->type();
|
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"struct member " + builder_->Symbols().NameFor(symbol) + " not found",
|
|
|
|
expr->source());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* vec = data_type->As<type::Vector>()) {
|
2021-02-24 14:15:02 +00:00
|
|
|
std::string str = builder_->Symbols().NameFor(expr->member()->symbol());
|
|
|
|
auto size = str.size();
|
|
|
|
swizzle.reserve(str.size());
|
|
|
|
|
|
|
|
for (auto c : str) {
|
|
|
|
switch (c) {
|
|
|
|
case 'x':
|
|
|
|
case 'r':
|
|
|
|
swizzle.emplace_back(0);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
case 'g':
|
|
|
|
swizzle.emplace_back(1);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
case 'b':
|
|
|
|
swizzle.emplace_back(2);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'a':
|
|
|
|
swizzle.emplace_back(3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"invalid vector swizzle character",
|
|
|
|
expr->member()->source().Begin() + swizzle.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < 1 || size > 4) {
|
|
|
|
diagnostics_.add_error("invalid vector swizzle size",
|
|
|
|
expr->member()->source());
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 16:24:32 +00:00
|
|
|
|
2021-03-01 20:01:39 +00:00
|
|
|
// All characters are valid, check if they're being mixed
|
|
|
|
auto is_rgba = [](char c) {
|
|
|
|
return c == 'r' || c == 'g' || c == 'b' || c == 'a';
|
|
|
|
};
|
|
|
|
auto is_xyzw = [](char c) {
|
|
|
|
return c == 'x' || c == 'y' || c == 'z' || c == 'w';
|
|
|
|
};
|
|
|
|
if (!std::all_of(str.begin(), str.end(), is_rgba) &&
|
|
|
|
!std::all_of(str.begin(), str.end(), is_xyzw)) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"invalid mixing of vector swizzle characters rgba with xyzw",
|
|
|
|
expr->member()->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:05:34 +00:00
|
|
|
if (size == 1) {
|
|
|
|
// A single element swizzle is just the type of the vector.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = vec->type();
|
2020-05-01 16:17:03 +00:00
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
2021-02-24 14:15:02 +00:00
|
|
|
// The vector will have a number of components equal to the length of
|
|
|
|
// the swizzle. This assumes the validator will check that the swizzle
|
2020-04-21 13:05:34 +00:00
|
|
|
// is correct.
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Vector>(vec->type(),
|
2021-01-26 16:57:10 +00:00
|
|
|
static_cast<uint32_t>(size));
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
2021-02-24 14:15:02 +00:00
|
|
|
"invalid use of member accessor on a non-vector/non-struct " +
|
2021-02-17 20:13:34 +00:00
|
|
|
data_type->type_name(),
|
|
|
|
expr->source());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
builder_->Sem().Add(expr,
|
|
|
|
builder_->create<semantic::MemberAccessorExpression>(
|
2021-02-24 14:15:02 +00:00
|
|
|
expr, ret, current_statement_, std::move(swizzle)));
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 13:26:03 +00:00
|
|
|
bool Resolver::ValidateBinary(ast::BinaryExpression* expr) {
|
|
|
|
using Bool = type::Bool;
|
|
|
|
using F32 = type::F32;
|
|
|
|
using I32 = type::I32;
|
|
|
|
using U32 = type::U32;
|
|
|
|
using Matrix = type::Matrix;
|
|
|
|
using Vector = type::Vector;
|
|
|
|
|
|
|
|
auto* lhs_type = TypeOf(expr->lhs())->UnwrapPtrIfNeeded();
|
|
|
|
auto* rhs_type = TypeOf(expr->rhs())->UnwrapPtrIfNeeded();
|
|
|
|
|
|
|
|
auto* lhs_vec = lhs_type->As<Vector>();
|
|
|
|
auto* lhs_vec_elem_type = lhs_vec ? lhs_vec->type() : nullptr;
|
|
|
|
auto* rhs_vec = rhs_type->As<Vector>();
|
|
|
|
auto* rhs_vec_elem_type = rhs_vec ? rhs_vec->type() : nullptr;
|
|
|
|
|
|
|
|
const bool matching_types = lhs_type == rhs_type;
|
|
|
|
const bool matching_vec_elem_types = lhs_vec_elem_type && rhs_vec_elem_type &&
|
|
|
|
(lhs_vec_elem_type == rhs_vec_elem_type);
|
|
|
|
|
|
|
|
// Binary logical expressions
|
|
|
|
if (expr->IsLogicalAnd() || expr->IsLogicalOr()) {
|
|
|
|
if (matching_types && lhs_type->Is<Bool>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (expr->IsOr() || expr->IsAnd()) {
|
|
|
|
if (matching_types && lhs_type->Is<Bool>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (matching_types && lhs_vec_elem_type && lhs_vec_elem_type->Is<Bool>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arithmetic expressions
|
|
|
|
if (expr->IsArithmetic()) {
|
|
|
|
// Binary arithmetic expressions over scalars
|
|
|
|
if (matching_types && lhs_type->IsAnyOf<I32, F32, U32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary arithmetic expressions over vectors
|
|
|
|
if (matching_types && lhs_vec_elem_type &&
|
|
|
|
lhs_vec_elem_type->IsAnyOf<I32, F32, U32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary arithmetic expressions with mixed scalar, vector, and matrix
|
|
|
|
// operands
|
|
|
|
if (expr->IsMultiply()) {
|
|
|
|
// Multiplication of a vector and a scalar
|
|
|
|
if (lhs_type->Is<F32>() && rhs_vec_elem_type &&
|
|
|
|
rhs_vec_elem_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (lhs_vec_elem_type && lhs_vec_elem_type->Is<F32>() &&
|
|
|
|
rhs_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* lhs_mat = lhs_type->As<Matrix>();
|
|
|
|
auto* lhs_mat_elem_type = lhs_mat ? lhs_mat->type() : nullptr;
|
|
|
|
auto* rhs_mat = rhs_type->As<Matrix>();
|
|
|
|
auto* rhs_mat_elem_type = rhs_mat ? rhs_mat->type() : nullptr;
|
|
|
|
|
|
|
|
// Multiplication of a matrix and a scalar
|
|
|
|
if (lhs_type->Is<F32>() && rhs_mat_elem_type &&
|
|
|
|
rhs_mat_elem_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() &&
|
|
|
|
rhs_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vector times matrix
|
|
|
|
if (lhs_vec_elem_type && lhs_vec_elem_type->Is<F32>() &&
|
|
|
|
rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix times vector
|
|
|
|
if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() &&
|
|
|
|
rhs_vec_elem_type && rhs_vec_elem_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matrix times matrix
|
|
|
|
if (lhs_mat_elem_type && lhs_mat_elem_type->Is<F32>() &&
|
|
|
|
rhs_mat_elem_type && rhs_mat_elem_type->Is<F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison expressions
|
|
|
|
if (expr->IsComparison()) {
|
|
|
|
if (matching_types) {
|
|
|
|
// Special case for bools: only == and !=
|
|
|
|
if (lhs_type->Is<Bool>() && (expr->IsEqual() || expr->IsNotEqual())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the rest, we can compare i32, u32, and f32
|
|
|
|
if (lhs_type->IsAnyOf<I32, U32, F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for vectors
|
|
|
|
if (matching_vec_elem_types) {
|
|
|
|
if (lhs_vec_elem_type->Is<Bool>() &&
|
|
|
|
(expr->IsEqual() || expr->IsNotEqual())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs_vec_elem_type->IsAnyOf<I32, U32, F32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary bitwise operations
|
|
|
|
if (expr->IsBitwise()) {
|
|
|
|
if (matching_types && lhs_type->IsAnyOf<I32, U32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bit shift expressions
|
|
|
|
if (expr->IsBitshift()) {
|
|
|
|
// Type validation rules are the same for left or right shift, despite
|
|
|
|
// differences in computation rules (i.e. right shift can be arithmetic or
|
|
|
|
// logical depending on lhs type).
|
|
|
|
|
|
|
|
if (lhs_type->IsAnyOf<I32, U32>() && rhs_type->Is<U32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs_vec_elem_type && lhs_vec_elem_type->IsAnyOf<I32, U32>() &&
|
|
|
|
rhs_vec_elem_type && rhs_vec_elem_type->Is<U32>()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"Binary expression operand types are invalid for this operation",
|
|
|
|
expr->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Binary(ast::BinaryExpression* expr) {
|
|
|
|
if (!Expression(expr->lhs()) || !Expression(expr->rhs())) {
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-16 13:26:03 +00:00
|
|
|
if (!ValidateBinary(expr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:26:39 +00:00
|
|
|
// Result type matches first parameter type
|
|
|
|
if (expr->IsAnd() || expr->IsOr() || expr->IsXor() || expr->IsShiftLeft() ||
|
2020-06-03 16:11:44 +00:00
|
|
|
expr->IsShiftRight() || expr->IsAdd() || expr->IsSubtract() ||
|
|
|
|
expr->IsDivide() || expr->IsModulo()) {
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, TypeOf(expr->lhs())->UnwrapPtrIfNeeded());
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Result type is a scalar or vector of boolean type
|
|
|
|
if (expr->IsLogicalAnd() || expr->IsLogicalOr() || expr->IsEqual() ||
|
|
|
|
expr->IsNotEqual() || expr->IsLessThan() || expr->IsGreaterThan() ||
|
|
|
|
expr->IsLessThanEqual() || expr->IsGreaterThanEqual()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* bool_type = builder_->create<type::Bool>();
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* param_type = TypeOf(expr->lhs())->UnwrapPtrIfNeeded();
|
|
|
|
type::Type* result_type = bool_type;
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* vec = param_type->As<type::Vector>()) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = builder_->create<type::Vector>(bool_type, vec->size());
|
2020-04-07 19:26:39 +00:00
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, result_type);
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (expr->IsMultiply()) {
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* lhs_type = TypeOf(expr->lhs())->UnwrapPtrIfNeeded();
|
|
|
|
auto* rhs_type = TypeOf(expr->rhs())->UnwrapPtrIfNeeded();
|
2020-04-07 19:26:39 +00:00
|
|
|
|
|
|
|
// Note, the ordering here matters. The later checks depend on the prior
|
|
|
|
// checks having been done.
|
2021-01-21 15:42:10 +00:00
|
|
|
auto* lhs_mat = lhs_type->As<type::Matrix>();
|
|
|
|
auto* rhs_mat = rhs_type->As<type::Matrix>();
|
|
|
|
auto* lhs_vec = lhs_type->As<type::Vector>();
|
|
|
|
auto* rhs_vec = rhs_type->As<type::Vector>();
|
2021-01-29 16:43:41 +00:00
|
|
|
type::Type* result_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
if (lhs_mat && rhs_mat) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = builder_->create<type::Matrix>(
|
|
|
|
lhs_mat->type(), lhs_mat->rows(), rhs_mat->columns());
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_mat && rhs_vec) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type =
|
|
|
|
builder_->create<type::Vector>(lhs_mat->type(), lhs_mat->rows());
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec && rhs_mat) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type =
|
|
|
|
builder_->create<type::Vector>(rhs_mat->type(), rhs_mat->columns());
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_mat) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// matrix * scalar
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (rhs_mat) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// scalar * matrix
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = rhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec && rhs_vec) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// Vector * scalar
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (rhs_vec) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// Scalar * vector
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = rhs_type;
|
2020-04-07 19:26:39 +00:00
|
|
|
} else {
|
|
|
|
// Scalar * Scalar
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-04-07 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, result_type);
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("Unknown binary expression", expr->source());
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::UnaryOp(ast::UnaryOpExpression* expr) {
|
2020-04-07 19:27:21 +00:00
|
|
|
// Result type matches the parameter type.
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(expr->expr())) {
|
2020-04-07 19:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
|
|
|
|
auto* result_type = TypeOf(expr->expr())->UnwrapPtrIfNeeded();
|
|
|
|
SetType(expr, result_type);
|
2020-04-07 19:27:21 +00:00
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) {
|
2021-03-17 22:47:33 +00:00
|
|
|
if (auto* ctor = stmt->variable()->constructor()) {
|
|
|
|
if (!Expression(ctor)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-18 21:14:44 +00:00
|
|
|
auto* lhs_type = stmt->variable()->type();
|
|
|
|
auto* rhs_type = TypeOf(ctor);
|
|
|
|
if (!IsValidAssignment(lhs_type, rhs_type)) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"variable of type '" + lhs_type->FriendlyName(builder_->Symbols()) +
|
|
|
|
"' cannot be initialized with a value of type '" +
|
|
|
|
rhs_type->FriendlyName(builder_->Symbols()) + "'",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
2021-02-26 18:25:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 22:47:33 +00:00
|
|
|
auto* var = stmt->variable();
|
2021-02-26 18:25:56 +00:00
|
|
|
|
2021-03-17 22:47:33 +00:00
|
|
|
auto* info = CreateVariableInfo(var);
|
|
|
|
variable_to_info_.emplace(var, info);
|
|
|
|
variable_stack_.set(var->symbol(), info);
|
|
|
|
current_block_->decls.push_back(var);
|
|
|
|
|
|
|
|
if (!var->is_const()) {
|
|
|
|
if (info->storage_class != ast::StorageClass::kFunction) {
|
|
|
|
if (info->storage_class != ast::StorageClass::kNone) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"function variable has a non-function storage class",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info->storage_class = ast::StorageClass::kFunction;
|
2021-02-26 18:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 20:46:24 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(info->storage_class, var->type(),
|
|
|
|
var->source())) {
|
|
|
|
diagnostics_.add_note("while instantiating variable " +
|
|
|
|
builder_->Symbols().NameFor(var->symbol()),
|
|
|
|
var->source());
|
2021-03-17 22:47:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:25:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo* Resolver::CreateVariableInfo(ast::Variable* var) {
|
2021-02-03 17:51:09 +00:00
|
|
|
auto* info = variable_infos_.Create(var);
|
|
|
|
variable_to_info_.emplace(var, info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
type::Type* Resolver::TypeOf(ast::Expression* expr) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto it = expr_info_.find(expr);
|
|
|
|
if (it != expr_info_.end()) {
|
|
|
|
return it->second.type;
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::SetType(ast::Expression* expr, type::Type* type) {
|
2021-02-16 18:45:45 +00:00
|
|
|
assert(expr_info_.count(expr) == 0);
|
|
|
|
expr_info_.emplace(expr, ExpressionInfo{type, current_statement_});
|
2021-01-29 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::CreateSemanticNodes() const {
|
2021-02-03 17:51:09 +00:00
|
|
|
auto& sem = builder_->Sem();
|
|
|
|
|
2021-03-03 19:54:44 +00:00
|
|
|
// Collate all the 'ancestor_entry_points' - this is a map of function symbol
|
|
|
|
// to all the entry points that transitively call the function.
|
|
|
|
std::unordered_map<Symbol, std::vector<Symbol>> ancestor_entry_points;
|
|
|
|
for (auto* func : builder_->AST().Functions()) {
|
|
|
|
auto it = function_to_info_.find(func);
|
|
|
|
if (it == function_to_info_.end()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
continue; // Resolver has likely errored. Process what we can.
|
2021-03-03 19:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto* info = it->second;
|
|
|
|
if (!func->IsEntryPoint()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (auto* call : info->transitive_calls) {
|
|
|
|
auto& vec = ancestor_entry_points[call->declaration->symbol()];
|
|
|
|
vec.emplace_back(func->symbol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Variables
|
2021-02-03 17:51:09 +00:00
|
|
|
for (auto it : variable_to_info_) {
|
|
|
|
auto* var = it.first;
|
|
|
|
auto* info = it.second;
|
2021-02-16 21:15:01 +00:00
|
|
|
std::vector<const semantic::Expression*> users;
|
|
|
|
for (auto* user : info->users) {
|
|
|
|
// Create semantic node for the identifier expression if necessary
|
|
|
|
auto* sem_expr = sem.Get(user);
|
|
|
|
if (sem_expr == nullptr) {
|
|
|
|
auto* type = expr_info_.at(user).type;
|
|
|
|
auto* stmt = expr_info_.at(user).statement;
|
|
|
|
sem_expr = builder_->create<semantic::Expression>(user, type, stmt);
|
|
|
|
sem.Add(user, sem_expr);
|
|
|
|
}
|
|
|
|
users.push_back(sem_expr);
|
|
|
|
}
|
|
|
|
sem.Add(var, builder_->create<semantic::Variable>(var, info->storage_class,
|
|
|
|
std::move(users)));
|
2021-02-03 17:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) {
|
|
|
|
std::vector<const semantic::Variable*> out;
|
|
|
|
out.reserve(in.size());
|
|
|
|
for (auto* info : in) {
|
|
|
|
out.emplace_back(sem.Get(info->declaration));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Functions
|
2021-02-08 22:31:44 +00:00
|
|
|
std::unordered_map<FunctionInfo*, semantic::Function*> func_info_to_sem_func;
|
2021-02-03 16:43:20 +00:00
|
|
|
for (auto it : function_to_info_) {
|
|
|
|
auto* func = it.first;
|
|
|
|
auto* info = it.second;
|
2021-03-03 19:54:44 +00:00
|
|
|
|
2021-02-08 22:31:44 +00:00
|
|
|
auto* sem_func = builder_->create<semantic::Function>(
|
|
|
|
info->declaration, remap_vars(info->referenced_module_vars),
|
2021-03-17 14:24:04 +00:00
|
|
|
remap_vars(info->local_referenced_module_vars), info->return_statements,
|
2021-03-03 19:54:44 +00:00
|
|
|
ancestor_entry_points[func->symbol()]);
|
2021-02-08 22:31:44 +00:00
|
|
|
func_info_to_sem_func.emplace(info, sem_func);
|
|
|
|
sem.Add(func, sem_func);
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::CallExpressions
|
2021-02-08 22:31:44 +00:00
|
|
|
for (auto it : function_calls_) {
|
|
|
|
auto* call = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto info = it.second;
|
|
|
|
auto* sem_func = func_info_to_sem_func.at(info.function);
|
2021-02-16 21:15:01 +00:00
|
|
|
sem.Add(call,
|
|
|
|
builder_->create<semantic::Call>(call, sem_func, info.statement));
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create semantic nodes for all remaining expression types
|
2021-02-16 18:45:45 +00:00
|
|
|
for (auto it : expr_info_) {
|
2021-02-09 17:38:05 +00:00
|
|
|
auto* expr = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto& info = it.second;
|
2021-02-09 17:38:05 +00:00
|
|
|
if (sem.Get(expr)) {
|
|
|
|
// Expression has already been assigned a semantic node
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-16 21:15:01 +00:00
|
|
|
sem.Add(expr, builder_->create<semantic::Expression>(expr, info.type,
|
|
|
|
info.statement));
|
2021-02-03 16:43:20 +00:00
|
|
|
}
|
2021-03-17 22:01:53 +00:00
|
|
|
|
|
|
|
// Create semantic nodes for all structs
|
|
|
|
for (auto it : struct_info_) {
|
|
|
|
auto* str = it.first;
|
|
|
|
auto* info = it.second;
|
2021-03-17 22:47:33 +00:00
|
|
|
builder_->Sem().Add(
|
|
|
|
str, builder_->create<semantic::Struct>(
|
|
|
|
str, std::move(info->members), info->align, info->size,
|
|
|
|
info->size_no_padding, info->storage_class_usage));
|
2021-03-17 22:01:53 +00:00
|
|
|
}
|
2021-02-03 16:43:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 10:43:11 +00:00
|
|
|
bool Resolver::DefaultAlignAndSize(type::Type* ty,
|
|
|
|
uint32_t& align,
|
|
|
|
uint32_t& size) {
|
|
|
|
static constexpr uint32_t vector_size[] = {
|
|
|
|
/* padding */ 0,
|
|
|
|
/* padding */ 0,
|
|
|
|
/*vec2*/ 8,
|
|
|
|
/*vec3*/ 12,
|
|
|
|
/*vec4*/ 16,
|
|
|
|
};
|
|
|
|
static constexpr uint32_t vector_align[] = {
|
|
|
|
/* padding */ 0,
|
|
|
|
/* padding */ 0,
|
|
|
|
/*vec2*/ 8,
|
|
|
|
/*vec3*/ 16,
|
|
|
|
/*vec4*/ 16,
|
|
|
|
};
|
|
|
|
|
|
|
|
ty = ty->UnwrapAliasIfNeeded();
|
|
|
|
if (ty->is_scalar()) {
|
2021-03-18 21:03:24 +00:00
|
|
|
// Note: Also captures booleans, but these are not host-shareable.
|
2021-03-15 10:43:11 +00:00
|
|
|
align = 4;
|
|
|
|
size = 4;
|
|
|
|
return true;
|
|
|
|
} else if (auto* vec = ty->As<type::Vector>()) {
|
|
|
|
if (vec->size() < 2 || vec->size() > 4) {
|
|
|
|
TINT_UNREACHABLE(diagnostics_)
|
|
|
|
<< "Invalid vector size: vec" << vec->size();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
align = vector_align[vec->size()];
|
|
|
|
size = vector_size[vec->size()];
|
|
|
|
return true;
|
|
|
|
} else if (auto* mat = ty->As<type::Matrix>()) {
|
|
|
|
if (mat->columns() < 2 || mat->columns() > 4 || mat->rows() < 2 ||
|
|
|
|
mat->rows() > 4) {
|
|
|
|
TINT_UNREACHABLE(diagnostics_)
|
|
|
|
<< "Invalid matrix size: mat" << mat->columns() << "x" << mat->rows();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
align = vector_align[mat->rows()];
|
|
|
|
size = vector_align[mat->rows()] * mat->columns();
|
|
|
|
return true;
|
|
|
|
} else if (auto* s = ty->As<type::Struct>()) {
|
2021-03-17 22:01:53 +00:00
|
|
|
if (auto* si = Structure(s)) {
|
|
|
|
align = si->align;
|
|
|
|
size = si->size;
|
2021-03-15 10:43:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (auto* arr = ty->As<type::Array>()) {
|
|
|
|
if (auto* sem = Array(arr)) {
|
|
|
|
align = sem->Align();
|
|
|
|
size = sem->Size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
TINT_UNREACHABLE(diagnostics_) << "Invalid type " << ty->TypeInfo().name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const semantic::Array* Resolver::Array(type::Array* arr) {
|
|
|
|
if (auto* sem = builder_->Sem().Get(arr)) {
|
|
|
|
// Semantic info already constructed for this array type
|
|
|
|
return sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check the element type is legal
|
|
|
|
auto* el_ty = arr->type();
|
|
|
|
if (!IsStorable(el_ty)) {
|
|
|
|
builder_->Diagnostics().add_error(
|
|
|
|
std::string(el_ty->FriendlyName(builder_->Symbols())) +
|
|
|
|
" cannot be used as an element type of an array");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto create_semantic = [&](uint32_t stride) -> semantic::Array* {
|
|
|
|
uint32_t el_align = 0;
|
|
|
|
uint32_t el_size = 0;
|
|
|
|
if (!DefaultAlignAndSize(arr->type(), el_align, el_size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto align = el_align;
|
|
|
|
// WebGPU requires runtime arrays have at least one element, but the AST
|
|
|
|
// records an element count of 0 for it.
|
|
|
|
auto size = std::max<uint32_t>(arr->size(), 1) * stride;
|
|
|
|
auto* sem = builder_->create<semantic::Array>(arr, align, size, stride);
|
|
|
|
builder_->Sem().Add(arr, sem);
|
|
|
|
return sem;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Look for explicit stride via [[stride(n)]] decoration
|
|
|
|
for (auto* deco : arr->decorations()) {
|
|
|
|
if (auto* stride = deco->As<ast::StrideDecoration>()) {
|
|
|
|
return create_semantic(stride->stride());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate implicit stride
|
|
|
|
uint32_t el_align = 0;
|
|
|
|
uint32_t el_size = 0;
|
|
|
|
if (!DefaultAlignAndSize(el_ty, el_align, el_size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:37:41 +00:00
|
|
|
return create_semantic(utils::RoundUp(el_align, el_size));
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:59:54 +00:00
|
|
|
bool Resolver::ValidateStructure(const type::Struct* st) {
|
|
|
|
for (auto* member : st->impl()->members()) {
|
|
|
|
if (auto* r = member->type()->UnwrapAll()->As<type::Array>()) {
|
|
|
|
if (r->IsRuntimeArray()) {
|
|
|
|
if (member != st->impl()->members().back()) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0015",
|
|
|
|
"runtime arrays may only appear as the last member of a struct",
|
|
|
|
member->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!st->IsBlockDecorated()) {
|
|
|
|
diagnostics_.add_error("v-0015",
|
|
|
|
"a struct containing a runtime-sized array "
|
|
|
|
"requires the [[block]] attribute: '" +
|
|
|
|
builder_->Symbols().NameFor(st->symbol()) +
|
|
|
|
"'",
|
|
|
|
member->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* deco : r->decorations()) {
|
|
|
|
if (!deco->Is<ast::StrideDecoration>()) {
|
|
|
|
diagnostics_.add_error("decoration is not valid for array types",
|
|
|
|
deco->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* deco : member->decorations()) {
|
|
|
|
if (!(deco->Is<ast::BuiltinDecoration>() ||
|
|
|
|
deco->Is<ast::LocationDecoration>() ||
|
|
|
|
deco->Is<ast::StructMemberOffsetDecoration>() ||
|
|
|
|
deco->Is<ast::StructMemberSizeDecoration>() ||
|
|
|
|
deco->Is<ast::StructMemberAlignDecoration>())) {
|
|
|
|
diagnostics_.add_error("decoration is not valid for structure members",
|
|
|
|
deco->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* deco : st->impl()->decorations()) {
|
|
|
|
if (!(deco->Is<ast::StructBlockDecoration>())) {
|
|
|
|
diagnostics_.add_error("decoration is not valid for struct declarations",
|
|
|
|
deco->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:01:53 +00:00
|
|
|
Resolver::StructInfo* Resolver::Structure(type::Struct* str) {
|
|
|
|
auto info_it = struct_info_.find(str);
|
|
|
|
if (info_it != struct_info_.end()) {
|
|
|
|
// StructInfo already resolved for this structure type
|
|
|
|
return info_it->second;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:59:54 +00:00
|
|
|
if (!ValidateStructure(str)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:43:11 +00:00
|
|
|
semantic::StructMemberList sem_members;
|
|
|
|
sem_members.reserve(str->impl()->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.
|
|
|
|
// TODO(crbug.com/tint/628): Actually implement storage-class validation.
|
|
|
|
uint32_t struct_size = 0;
|
|
|
|
uint32_t struct_align = 1;
|
|
|
|
|
|
|
|
for (auto* member : str->impl()->members()) {
|
|
|
|
// First check the member type is legal
|
|
|
|
if (!IsStorable(member->type())) {
|
|
|
|
builder_->Diagnostics().add_error(
|
|
|
|
std::string(member->type()->FriendlyName(builder_->Symbols())) +
|
|
|
|
" cannot be used as the type of a structure member");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = struct_size;
|
|
|
|
uint32_t align = 0;
|
|
|
|
uint32_t size = 0;
|
|
|
|
if (!DefaultAlignAndSize(member->type(), align, size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:34:22 +00:00
|
|
|
bool has_offset_deco = false;
|
|
|
|
bool has_align_deco = false;
|
|
|
|
bool has_size_deco = false;
|
2021-03-15 10:43:11 +00:00
|
|
|
for (auto* deco : member->decorations()) {
|
|
|
|
if (auto* o = deco->As<ast::StructMemberOffsetDecoration>()) {
|
2021-03-15 20:34:22 +00:00
|
|
|
// Offset decorations are not part of the WGSL spec, but are emitted by
|
|
|
|
// the SPIR-V reader.
|
2021-03-15 10:43:11 +00:00
|
|
|
if (o->offset() < struct_size) {
|
|
|
|
diagnostics_.add_error("offsets must be in ascending order",
|
|
|
|
o->source());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
offset = o->offset();
|
|
|
|
align = 1;
|
2021-03-15 20:34:22 +00:00
|
|
|
has_offset_deco = true;
|
2021-03-15 10:43:11 +00:00
|
|
|
} else if (auto* a = deco->As<ast::StructMemberAlignDecoration>()) {
|
2021-03-15 13:37:41 +00:00
|
|
|
if (a->align() <= 0 || !utils::IsPowerOfTwo(a->align())) {
|
2021-03-15 10:43:11 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"align value must be a positive, power-of-two integer",
|
|
|
|
a->source());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
align = a->align();
|
2021-03-15 20:34:22 +00:00
|
|
|
has_align_deco = true;
|
2021-03-15 10:43:11 +00:00
|
|
|
} else if (auto* s = deco->As<ast::StructMemberSizeDecoration>()) {
|
|
|
|
if (s->size() < size) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"size must be at least as big as the type's size (" +
|
|
|
|
std::to_string(size) + ")",
|
|
|
|
s->source());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
size = s->size();
|
2021-03-15 20:34:22 +00:00
|
|
|
has_size_deco = true;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:34:22 +00:00
|
|
|
if (has_offset_deco && (has_align_deco || has_size_deco)) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"offset decorations cannot be used with align or size decorations",
|
|
|
|
member->source());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:37:41 +00:00
|
|
|
offset = utils::RoundUp(align, offset);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
|
|
|
auto* sem_member =
|
2021-03-15 20:25:12 +00:00
|
|
|
builder_->create<semantic::StructMember>(member, offset, align, size);
|
2021-03-15 10:43:11 +00:00
|
|
|
builder_->Sem().Add(member, sem_member);
|
|
|
|
sem_members.emplace_back(sem_member);
|
|
|
|
|
|
|
|
struct_size = offset + size;
|
|
|
|
struct_align = std::max(struct_align, align);
|
|
|
|
}
|
|
|
|
|
2021-03-17 21:54:13 +00:00
|
|
|
auto size_no_padding = struct_size;
|
2021-03-15 13:37:41 +00:00
|
|
|
struct_size = utils::RoundUp(struct_align, struct_size);
|
2021-03-15 10:43:11 +00:00
|
|
|
|
2021-03-17 22:01:53 +00:00
|
|
|
auto* info = struct_infos_.Create();
|
|
|
|
info->members = std::move(sem_members);
|
|
|
|
info->align = struct_align;
|
|
|
|
info->size = struct_size;
|
|
|
|
info->size_no_padding = size_no_padding;
|
|
|
|
struct_info_.emplace(str, info);
|
|
|
|
return info;
|
2021-03-15 10:43:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 23:20:17 +00:00
|
|
|
bool Resolver::ValidateReturn(const ast::ReturnStatement* ret) {
|
|
|
|
type::Type* func_type = current_function_->declaration->return_type();
|
|
|
|
|
|
|
|
auto* ret_type = ret->has_value() ? TypeOf(ret->value())->UnwrapAll()
|
|
|
|
: builder_->ty.void_();
|
|
|
|
|
|
|
|
if (func_type->UnwrapAll() != ret_type) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-000y",
|
|
|
|
"return statement type must match its function "
|
|
|
|
"return type, returned '" +
|
|
|
|
ret_type->FriendlyName(builder_->Symbols()) + "', expected '" +
|
|
|
|
func_type->FriendlyName(builder_->Symbols()) + "'",
|
|
|
|
ret->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::Return(ast::ReturnStatement* ret) {
|
|
|
|
current_function_->return_statements.push_back(ret);
|
|
|
|
|
|
|
|
auto result = Expression(ret->value());
|
|
|
|
|
|
|
|
// Validate after processing the return value expression so that its type is
|
|
|
|
// available for validation
|
|
|
|
return result && ValidateReturn(ret);
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:47:33 +00:00
|
|
|
bool Resolver::ApplyStorageClassUsageToType(ast::StorageClass sc,
|
2021-03-18 20:46:24 +00:00
|
|
|
type::Type* ty,
|
|
|
|
Source usage) {
|
2021-03-18 16:44:24 +00:00
|
|
|
ty = ty->UnwrapIfNeeded();
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
if (auto* str = ty->As<type::Struct>()) {
|
|
|
|
auto* info = Structure(str);
|
|
|
|
if (!info) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (info->storage_class_usage.count(sc)) {
|
|
|
|
return true; // Already applied
|
|
|
|
}
|
|
|
|
info->storage_class_usage.emplace(sc);
|
|
|
|
for (auto* member : str->impl()->members()) {
|
2021-03-18 20:46:24 +00:00
|
|
|
if (!ApplyStorageClassUsageToType(sc, member->type(), usage)) {
|
2021-03-17 22:47:33 +00:00
|
|
|
std::stringstream err;
|
2021-03-18 20:46:24 +00:00
|
|
|
err << "while analysing structure member "
|
|
|
|
<< str->FriendlyName(builder_->Symbols()) << "."
|
|
|
|
<< builder_->Symbols().NameFor(member->symbol());
|
|
|
|
diagnostics_.add_note(err.str(), member->source());
|
2021-03-17 22:47:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 20:46:24 +00:00
|
|
|
return true;
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* arr = ty->As<type::Array>()) {
|
2021-03-18 20:46:24 +00:00
|
|
|
return ApplyStorageClassUsageToType(sc, arr->type(), usage);
|
|
|
|
}
|
|
|
|
|
2021-03-18 21:03:24 +00:00
|
|
|
if (ast::IsHostShareable(sc) && !IsHostShareable(ty)) {
|
2021-03-18 20:46:24 +00:00
|
|
|
std::stringstream err;
|
|
|
|
err << "Type '" << ty->FriendlyName(builder_->Symbols())
|
|
|
|
<< "' cannot be used in storage class '" << sc
|
2021-03-18 21:03:24 +00:00
|
|
|
<< "' as it is non-host-shareable";
|
2021-03-18 20:46:24 +00:00
|
|
|
diagnostics_.add_error(err.str(), usage);
|
|
|
|
return false;
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
template <typename F>
|
|
|
|
bool Resolver::BlockScope(BlockInfo::Type type, F&& callback) {
|
|
|
|
BlockInfo block_info(type, current_block_);
|
|
|
|
ScopedAssignment<BlockInfo*> sa(current_block_, &block_info);
|
2021-03-22 17:38:45 +00:00
|
|
|
variable_stack_.push_scope();
|
|
|
|
bool result = callback();
|
|
|
|
variable_stack_.pop_scope();
|
|
|
|
return result;
|
2021-03-09 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 15:43:14 +00:00
|
|
|
std::string Resolver::VectorPretty(uint32_t size, type::Type* element_type) {
|
|
|
|
type::Vector vec_type(element_type, size);
|
|
|
|
return vec_type.FriendlyName(builder_->Symbols());
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo::VariableInfo(ast::Variable* decl)
|
2021-02-03 17:51:09 +00:00
|
|
|
: declaration(decl), storage_class(decl->declared_storage_class()) {}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo::~VariableInfo() = default;
|
2021-02-03 17:51:09 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::FunctionInfo::FunctionInfo(ast::Function* decl) : declaration(decl) {}
|
|
|
|
Resolver::FunctionInfo::~FunctionInfo() = default;
|
2021-02-03 16:43:20 +00:00
|
|
|
|
2021-03-17 22:01:53 +00:00
|
|
|
Resolver::StructInfo::StructInfo() = default;
|
|
|
|
Resolver::StructInfo::~StructInfo() = default;
|
|
|
|
|
2021-03-09 15:08:48 +00:00
|
|
|
} // namespace resolver
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|