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.
|
|
|
|
|
|
|
|
#include "src/type_determiner.h"
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
#include <memory>
|
2020-06-16 15:02:50 +00:00
|
|
|
#include <vector>
|
2020-04-07 12:57:42 +00:00
|
|
|
|
|
|
|
#include "src/ast/array_accessor_expression.h"
|
2020-04-07 12:47:23 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2020-04-07 19:27:41 +00:00
|
|
|
#include "src/ast/binary_expression.h"
|
2020-09-22 22:07:13 +00:00
|
|
|
#include "src/ast/bitcast_expression.h"
|
2020-07-27 15:25:00 +00:00
|
|
|
#include "src/ast/block_statement.h"
|
2020-04-07 12:54:10 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
2020-04-07 16:41:10 +00:00
|
|
|
#include "src/ast/call_expression.h"
|
2020-07-21 13:42:13 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2020-04-07 12:54:20 +00:00
|
|
|
#include "src/ast/case_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"
|
2020-04-07 12:54:37 +00:00
|
|
|
#include "src/ast/else_statement.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/fallthrough_statement.h"
|
2020-04-07 12:57:27 +00:00
|
|
|
#include "src/ast/identifier_expression.h"
|
2020-04-07 12:55:25 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2020-06-04 17:05:35 +00:00
|
|
|
#include "src/ast/intrinsic.h"
|
2020-04-07 12:55:51 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/member_accessor_expression.h"
|
2020-04-07 12:56:24 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
2020-04-07 12:56:45 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/array_type.h"
|
2020-04-07 19:26:39 +00:00
|
|
|
#include "src/ast/type/bool_type.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/type/depth_texture_type.h"
|
2020-04-07 19:27:00 +00:00
|
|
|
#include "src/ast/type/f32_type.h"
|
2020-08-31 15:11:23 +00:00
|
|
|
#include "src/ast/type/i32_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/matrix_type.h"
|
2020-09-21 15:52:10 +00:00
|
|
|
#include "src/ast/type/multisampled_texture_type.h"
|
2020-04-23 22:26:52 +00:00
|
|
|
#include "src/ast/type/pointer_type.h"
|
2020-08-31 15:24:54 +00:00
|
|
|
#include "src/ast/type/sampled_texture_type.h"
|
|
|
|
#include "src/ast/type/storage_texture_type.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/type/struct_type.h"
|
2020-08-31 15:24:54 +00:00
|
|
|
#include "src/ast/type/texture_type.h"
|
2020-08-31 15:11:23 +00:00
|
|
|
#include "src/ast/type/u32_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/vector_type.h"
|
2020-12-10 18:39:41 +00:00
|
|
|
#include "src/ast/type/void_type.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/type_constructor_expression.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"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
|
|
|
|
2020-12-02 21:17:58 +00:00
|
|
|
TypeDeterminer::TypeDeterminer(ast::Module* mod) : mod_(mod) {}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
TypeDeterminer::~TypeDeterminer() = default;
|
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
void TypeDeterminer::set_error(const Source& src, const std::string& msg) {
|
|
|
|
error_ = "";
|
2020-10-30 20:44:53 +00:00
|
|
|
if (src.range.begin.line > 0) {
|
|
|
|
error_ += std::to_string(src.range.begin.line) + ":" +
|
|
|
|
std::to_string(src.range.begin.column) + ": ";
|
2020-04-08 19:58:35 +00:00
|
|
|
}
|
|
|
|
error_ += msg;
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:07:24 +00:00
|
|
|
void TypeDeterminer::set_referenced_from_function_if_needed(ast::Variable* var,
|
|
|
|
bool local) {
|
2020-06-22 20:52:24 +00:00
|
|
|
if (current_function_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (var->storage_class() == ast::StorageClass::kNone ||
|
|
|
|
var->storage_class() == ast::StorageClass::kFunction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_function_->add_referenced_module_variable(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
if (local) {
|
|
|
|
current_function_->add_local_referenced_module_variable(var);
|
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
bool TypeDeterminer::Determine() {
|
2020-11-23 19:58:55 +00:00
|
|
|
for (auto& iter : mod_->types()) {
|
2020-08-31 15:11:23 +00:00
|
|
|
auto& type = iter.second;
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* storage = type->As<ast::type::StorageTexture>()) {
|
|
|
|
if (!DetermineStorageTextureSubtype(storage)) {
|
|
|
|
set_error(Source{},
|
|
|
|
"unable to determine storage texture subtype for: " +
|
|
|
|
type->type_name());
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-31 15:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* var : mod_->global_variables()) {
|
2021-01-11 15:10:19 +00:00
|
|
|
variable_stack_.set_global(var->symbol(), var);
|
2020-06-15 20:55:09 +00:00
|
|
|
|
|
|
|
if (var->has_constructor()) {
|
|
|
|
if (!DetermineResultType(var->constructor())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
if (!DetermineFunctions(mod_->functions())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-14 19:45:47 +00:00
|
|
|
|
2020-09-21 17:51:31 +00:00
|
|
|
// Walk over the caller to callee information and update functions with which
|
|
|
|
// entry points call those functions.
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* func : mod_->functions()) {
|
2020-09-21 18:49:01 +00:00
|
|
|
if (!func->IsEntryPoint()) {
|
2020-09-21 17:51:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const auto& callee : caller_to_callee_[func->name()]) {
|
2020-12-11 18:24:53 +00:00
|
|
|
set_entry_points(callee, func->symbol());
|
2020-09-21 17:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:45:47 +00:00
|
|
|
void TypeDeterminer::set_entry_points(const std::string& fn_name,
|
2020-12-11 18:24:53 +00:00
|
|
|
Symbol ep_sym) {
|
|
|
|
name_to_function_[fn_name]->add_ancestor_entry_point(ep_sym);
|
2020-07-14 19:45:47 +00:00
|
|
|
|
|
|
|
for (const auto& callee : caller_to_callee_[fn_name]) {
|
2020-12-11 18:24:53 +00:00
|
|
|
set_entry_points(callee, ep_sym);
|
2020-07-14 19:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
bool TypeDeterminer::DetermineFunctions(const ast::FunctionList& funcs) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* func : funcs) {
|
|
|
|
if (!DetermineFunction(func)) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineFunction(ast::Function* func) {
|
2020-06-22 20:18:17 +00:00
|
|
|
name_to_function_[func->name()] = func;
|
|
|
|
|
2020-06-22 20:52:24 +00:00
|
|
|
current_function_ = func;
|
|
|
|
|
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-01-11 15:10:19 +00:00
|
|
|
variable_stack_.set(param->symbol(), param);
|
2020-06-22 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
if (!DetermineStatements(func->body())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
variable_stack_.pop_scope();
|
|
|
|
|
2020-06-22 20:52:24 +00:00
|
|
|
current_function_ = nullptr;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
bool TypeDeterminer::DetermineStatements(const ast::BlockStatement* stmts) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* stmt : *stmts) {
|
|
|
|
if (!DetermineVariableStorageClass(stmt)) {
|
2020-07-27 15:25:00 +00:00
|
|
|
return false;
|
2020-04-08 19:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
if (!DetermineResultType(stmt)) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* var_decl = stmt->As<ast::VariableDeclStatement>();
|
|
|
|
if (var_decl == nullptr) {
|
2020-04-08 19:58:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* var = var_decl->variable();
|
2020-04-08 19:58:20 +00:00
|
|
|
// Nothing to do for const
|
|
|
|
if (var->is_const()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->storage_class() == ast::StorageClass::kFunction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->storage_class() != ast::StorageClass::kNone) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(stmt->source(),
|
|
|
|
"function variable has a non-function storage class");
|
2020-04-08 19:58:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->set_storage_class(ast::StorageClass::kFunction);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:47:23 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = stmt->As<ast::AssignmentStatement>()) {
|
2020-04-07 12:47:23 +00:00
|
|
|
return DetermineResultType(a->lhs()) && DetermineResultType(a->rhs());
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = stmt->As<ast::BlockStatement>()) {
|
|
|
|
return DetermineStatements(b);
|
2020-07-27 15:25:00 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::BreakStatement>()) {
|
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>()) {
|
|
|
|
return DetermineResultType(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>()) {
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(c->body());
|
2020-04-07 12:54:20 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::ContinueStatement>()) {
|
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>()) {
|
2020-04-07 12:54:37 +00:00
|
|
|
return DetermineResultType(e->condition()) &&
|
2020-04-08 19:58:20 +00:00
|
|
|
DetermineStatements(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>()) {
|
2020-04-07 12:55:25 +00:00
|
|
|
if (!DetermineResultType(i->condition()) ||
|
2020-04-08 19:58:20 +00:00
|
|
|
!DetermineStatements(i->body())) {
|
2020-04-07 12:55:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* else_stmt : i->else_statements()) {
|
|
|
|
if (!DetermineResultType(else_stmt)) {
|
2020-04-07 12:55:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* l = stmt->As<ast::LoopStatement>()) {
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(l->body()) &&
|
|
|
|
DetermineStatements(l->continuing());
|
2020-04-07 12:55:51 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* r = stmt->As<ast::ReturnStatement>()) {
|
2020-04-07 12:56:24 +00:00
|
|
|
return DetermineResultType(r->value());
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* s = stmt->As<ast::SwitchStatement>()) {
|
2020-04-07 12:56:45 +00:00
|
|
|
if (!DetermineResultType(s->condition())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* case_stmt : s->body()) {
|
|
|
|
if (!DetermineResultType(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-01-11 15:10:19 +00:00
|
|
|
variable_stack_.set(v->variable()->symbol(), v->variable());
|
2020-04-07 12:57:12 +00:00
|
|
|
return DetermineResultType(v->variable()->constructor());
|
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
set_error(stmt->source(),
|
|
|
|
"unknown statement type for type determination: " + stmt->str());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(const ast::ExpressionList& list) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* expr : list) {
|
|
|
|
if (!DetermineResultType(expr)) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(ast::Expression* expr) {
|
|
|
|
// This is blindly called above, so in some cases the expression won't exist.
|
|
|
|
if (!expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = expr->As<ast::ArrayAccessorExpression>()) {
|
|
|
|
return DetermineArrayAccessor(a);
|
2020-04-07 12:57:42 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = expr->As<ast::BinaryExpression>()) {
|
|
|
|
return DetermineBinary(b);
|
2020-04-07 19:27:41 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = expr->As<ast::BitcastExpression>()) {
|
|
|
|
return DetermineBitcast(b);
|
2020-09-22 22:07:13 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = expr->As<ast::CallExpression>()) {
|
|
|
|
return DetermineCall(c);
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = expr->As<ast::ConstructorExpression>()) {
|
|
|
|
return DetermineConstructor(c);
|
2020-04-07 12:46:30 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* i = expr->As<ast::IdentifierExpression>()) {
|
|
|
|
return DetermineIdentifier(i);
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* m = expr->As<ast::MemberAccessorExpression>()) {
|
|
|
|
return DetermineMemberAccessor(m);
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* u = expr->As<ast::UnaryOpExpression>()) {
|
|
|
|
return DetermineUnaryOp(u);
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "unknown expression for type determination");
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
bool TypeDeterminer::DetermineArrayAccessor(
|
|
|
|
ast::ArrayAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->array())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
if (!DetermineResultType(expr->idx_expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
auto* res = expr->array()->result_type();
|
2020-10-28 20:32:22 +00:00
|
|
|
auto* parent_type = res->UnwrapAll();
|
2020-04-23 22:26:52 +00:00
|
|
|
ast::type::Type* ret = nullptr;
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* arr = parent_type->As<ast::type::Array>()) {
|
|
|
|
ret = arr->type();
|
|
|
|
} else if (auto* vec = parent_type->As<ast::type::Vector>()) {
|
|
|
|
ret = vec->type();
|
|
|
|
} else if (auto* mat = parent_type->As<ast::type::Matrix>()) {
|
|
|
|
ret = mod_->create<ast::type::Vector>(mat->type(), mat->rows());
|
2020-04-07 12:57:42 +00:00
|
|
|
} else {
|
2020-08-25 21:22:37 +00:00
|
|
|
set_error(expr->source(), "invalid parent type (" +
|
|
|
|
parent_type->type_name() +
|
|
|
|
") in array accessor");
|
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.
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* ptr = res->As<ast::type::Pointer>()) {
|
|
|
|
ret = mod_->create<ast::type::Pointer>(ret, ptr->storage_class());
|
|
|
|
} else if (auto* arr = parent_type->As<ast::type::Array>()) {
|
|
|
|
if (!arr->type()->is_scalar()) {
|
|
|
|
// If we extract a non-scalar from an array then we also get a pointer. We
|
|
|
|
// will generate a Function storage class variable to store this
|
|
|
|
// into.
|
|
|
|
ret = mod_->create<ast::type::Pointer>(ret, ast::StorageClass::kFunction);
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
|
|
|
expr->set_result_type(ret);
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 22:07:13 +00:00
|
|
|
bool TypeDeterminer::DetermineBitcast(ast::BitcastExpression* expr) {
|
2020-06-18 18:02:46 +00:00
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 12:57:52 +00:00
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) {
|
2020-09-22 19:42:13 +00:00
|
|
|
if (!DetermineResultType(expr->func())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
if (!DetermineResultType(expr->params())) {
|
|
|
|
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.
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* ident = expr->func()->As<ast::IdentifierExpression>()) {
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ident->IsIntrinsic()) {
|
|
|
|
if (!DetermineIntrinsic(ident, expr)) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-14 19:45:47 +00:00
|
|
|
if (current_function_) {
|
|
|
|
caller_to_callee_[current_function_->name()].push_back(ident->name());
|
2020-07-14 20:37:28 +00:00
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
auto* callee_func =
|
|
|
|
mod_->FindFunctionBySymbol(mod_->GetSymbol(ident->name()));
|
2020-07-14 20:37:28 +00:00
|
|
|
if (callee_func == nullptr) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"unable to find called function: " + ident->name());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
for (auto* var : callee_func->referenced_module_variables()) {
|
2020-12-08 21:07:24 +00:00
|
|
|
set_referenced_from_function_if_needed(var, false);
|
2020-07-14 20:37:28 +00:00
|
|
|
}
|
2020-07-14 19:45:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// An identifier with a single name is a function call, not an import
|
|
|
|
// lookup which we can handle with the regular identifier lookup.
|
|
|
|
if (!DetermineResultType(ident)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!DetermineResultType(expr->func())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
2020-08-18 02:10:03 +00:00
|
|
|
|
|
|
|
if (!expr->func()->result_type()) {
|
2020-11-30 23:30:58 +00:00
|
|
|
auto func_name = expr->func()->As<ast::IdentifierExpression>()->name();
|
2020-08-18 02:10:03 +00:00
|
|
|
set_error(
|
|
|
|
expr->source(),
|
|
|
|
"v-0005: function must be declared before use: '" + func_name + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
expr->set_result_type(expr->func()->result_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class IntrinsicDataType {
|
|
|
|
kFloatOrIntScalarOrVector,
|
|
|
|
kFloatScalarOrVector,
|
|
|
|
kIntScalarOrVector,
|
|
|
|
kFloatVector,
|
|
|
|
kMatrix,
|
|
|
|
};
|
|
|
|
struct IntrinsicData {
|
|
|
|
ast::Intrinsic intrinsic;
|
|
|
|
uint8_t param_count;
|
|
|
|
IntrinsicDataType data_type;
|
|
|
|
uint8_t vector_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note, this isn't all the intrinsics. Some are handled specially before
|
|
|
|
// we get to the generic code. See the DetermineIntrinsic code below.
|
|
|
|
constexpr const IntrinsicData kIntrinsicData[] = {
|
|
|
|
{ast::Intrinsic::kAbs, 1, IntrinsicDataType::kFloatOrIntScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kAcos, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kAsin, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kAtan, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kAtan2, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kCeil, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kClamp, 3, IntrinsicDataType::kFloatOrIntScalarOrVector,
|
|
|
|
0},
|
|
|
|
{ast::Intrinsic::kCos, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kCosh, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kCountOneBits, 1, IntrinsicDataType::kIntScalarOrVector,
|
|
|
|
0},
|
|
|
|
{ast::Intrinsic::kCross, 2, IntrinsicDataType::kFloatVector, 3},
|
|
|
|
{ast::Intrinsic::kDeterminant, 1, IntrinsicDataType::kMatrix, 0},
|
|
|
|
{ast::Intrinsic::kDistance, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kExp, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kExp2, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kFaceForward, 3, IntrinsicDataType::kFloatScalarOrVector,
|
|
|
|
0},
|
|
|
|
{ast::Intrinsic::kFloor, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kFma, 3, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kFract, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kFrexp, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kInverseSqrt, 1, IntrinsicDataType::kFloatScalarOrVector,
|
|
|
|
0},
|
|
|
|
{ast::Intrinsic::kLdexp, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kLength, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kLog, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kLog2, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kMax, 2, IntrinsicDataType::kFloatOrIntScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kMin, 2, IntrinsicDataType::kFloatOrIntScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kMix, 3, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kModf, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kNormalize, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kPow, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kReflect, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kReverseBits, 1, IntrinsicDataType::kIntScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kRound, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kSign, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kSin, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kSinh, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kSmoothStep, 3, IntrinsicDataType::kFloatScalarOrVector,
|
|
|
|
0},
|
|
|
|
{ast::Intrinsic::kSqrt, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kStep, 2, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kTan, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kTanh, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
{ast::Intrinsic::kTrunc, 1, IntrinsicDataType::kFloatScalarOrVector, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr const uint32_t kIntrinsicDataCount =
|
|
|
|
sizeof(kIntrinsicData) / sizeof(IntrinsicData);
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
|
2020-06-01 13:43:22 +00:00
|
|
|
ast::CallExpression* expr) {
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ast::intrinsic::IsDerivative(ident->intrinsic())) {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (expr->params().size() != 1) {
|
2020-09-22 19:42:13 +00:00
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for " + ident->name());
|
2020-06-01 13:43:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The result type must be the same as the type of the parameter.
|
2020-09-22 19:42:13 +00:00
|
|
|
auto* param_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
expr->func()->set_result_type(param_type);
|
2020-06-01 13:43:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kAny ||
|
|
|
|
ident->intrinsic() == ast::Intrinsic::kAll) {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->func()->set_result_type(mod_->create<ast::type::Bool>());
|
2020-06-01 13:43:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-10-08 17:01:55 +00:00
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kArrayLength) {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->func()->set_result_type(mod_->create<ast::type::U32>());
|
2020-10-08 17:01:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ast::intrinsic::IsFloatClassificationIntrinsic(ident->intrinsic())) {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (expr->params().size() != 1) {
|
2020-09-22 19:42:13 +00:00
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for " + ident->name());
|
2020-06-01 13:43:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
auto* bool_type = mod_->create<ast::type::Bool>();
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
auto* param_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded();
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* vec = param_type->As<ast::type::Vector>()) {
|
|
|
|
expr->func()->set_result_type(
|
|
|
|
mod_->create<ast::type::Vector>(bool_type, vec->size()));
|
2020-06-01 13:43:22 +00:00
|
|
|
} else {
|
|
|
|
expr->func()->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ast::intrinsic::IsTextureIntrinsic(ident->intrinsic())) {
|
2020-11-18 21:19:22 +00:00
|
|
|
ast::intrinsic::TextureSignature::Parameters param;
|
|
|
|
|
|
|
|
auto* texture_param = expr->params()[0];
|
2020-11-30 23:30:58 +00:00
|
|
|
if (!texture_param->result_type()
|
|
|
|
->UnwrapPtrIfNeeded()
|
2020-11-30 23:30:58 +00:00
|
|
|
->Is<ast::type::Texture>()) {
|
2020-11-18 21:19:22 +00:00
|
|
|
set_error(expr->source(), "invalid first argument for " + ident->name());
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::Texture* texture = texture_param->result_type()
|
|
|
|
->UnwrapPtrIfNeeded()
|
|
|
|
->As<ast::type::Texture>();
|
2020-11-18 21:19:22 +00:00
|
|
|
|
|
|
|
bool is_array = false;
|
2020-12-09 15:57:00 +00:00
|
|
|
bool is_multisampled = texture->Is<ast::type::MultisampledTexture>();
|
2020-11-18 21:19:22 +00:00
|
|
|
switch (texture->dim()) {
|
|
|
|
case ast::type::TextureDimension::k1dArray:
|
|
|
|
case ast::type::TextureDimension::k2dArray:
|
|
|
|
case ast::type::TextureDimension::kCubeArray:
|
|
|
|
is_array = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (ident->intrinsic()) {
|
|
|
|
case ast::Intrinsic::kTextureLoad:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
if (expr->params().size() > param.count) {
|
2020-12-09 15:57:00 +00:00
|
|
|
if (is_multisampled) {
|
|
|
|
param.idx.sample_index = param.count++;
|
|
|
|
} else {
|
|
|
|
param.idx.level = param.count++;
|
|
|
|
}
|
2020-11-18 21:19:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Intrinsic::kTextureSample:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.sampler = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
if (expr->params().size() > param.count) {
|
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Intrinsic::kTextureSampleBias:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.sampler = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
param.idx.bias = param.count++;
|
|
|
|
if (expr->params().size() > param.count) {
|
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Intrinsic::kTextureSampleLevel:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.sampler = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
param.idx.level = param.count++;
|
|
|
|
if (expr->params().size() > param.count) {
|
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Intrinsic::kTextureSampleCompare:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.sampler = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
param.idx.depth_ref = param.count++;
|
|
|
|
if (expr->params().size() > param.count) {
|
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ast::Intrinsic::kTextureSampleGrad:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.sampler = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
param.idx.ddx = param.count++;
|
|
|
|
param.idx.ddy = param.count++;
|
|
|
|
if (expr->params().size() > param.count) {
|
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2020-12-10 18:39:41 +00:00
|
|
|
case ast::Intrinsic::kTextureStore:
|
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
|
|
|
param.idx.value = param.count++;
|
|
|
|
break;
|
2020-11-18 21:19:22 +00:00
|
|
|
default:
|
|
|
|
set_error(expr->source(),
|
|
|
|
"Internal compiler error: Unreachable intrinsic " +
|
|
|
|
std::to_string(static_cast<int>(ident->intrinsic())));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr->params().size() != param.count) {
|
2020-08-31 15:24:54 +00:00
|
|
|
set_error(expr->source(),
|
2020-09-22 19:42:13 +00:00
|
|
|
"incorrect number of parameters for " + ident->name() +
|
|
|
|
", got " + std::to_string(expr->params().size()) +
|
2020-11-18 21:19:22 +00:00
|
|
|
" and expected " + std::to_string(param.count));
|
2020-08-31 15:24:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-18 21:19:22 +00:00
|
|
|
ident->set_intrinsic_signature(
|
|
|
|
std::make_unique<ast::intrinsic::TextureSignature>(param));
|
|
|
|
|
2020-12-10 18:39:41 +00:00
|
|
|
// Set the function return type
|
|
|
|
ast::type::Type* return_type = nullptr;
|
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kTextureStore) {
|
|
|
|
return_type = mod_->create<ast::type::Void>();
|
2020-09-21 15:52:10 +00:00
|
|
|
} else {
|
2020-12-10 18:39:41 +00:00
|
|
|
if (texture->Is<ast::type::DepthTexture>()) {
|
|
|
|
return_type = mod_->create<ast::type::F32>();
|
2020-12-09 15:57:00 +00:00
|
|
|
} else {
|
2020-12-10 18:39:41 +00:00
|
|
|
ast::type::Type* type = nullptr;
|
|
|
|
if (auto* storage = texture->As<ast::type::StorageTexture>()) {
|
|
|
|
type = storage->type();
|
|
|
|
} else if (auto* sampled = texture->As<ast::type::SampledTexture>()) {
|
|
|
|
type = sampled->type();
|
|
|
|
} else if (auto* msampled =
|
|
|
|
texture->As<ast::type::MultisampledTexture>()) {
|
|
|
|
type = msampled->type();
|
|
|
|
} else {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"unknown texture type for texture sampling");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return_type = mod_->create<ast::type::Vector>(type, 4);
|
2020-12-09 15:57:00 +00:00
|
|
|
}
|
2020-09-21 15:52:10 +00:00
|
|
|
}
|
2020-12-10 18:39:41 +00:00
|
|
|
expr->func()->set_result_type(return_type);
|
|
|
|
|
2020-08-31 15:24:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kDot) {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->func()->set_result_type(mod_->create<ast::type::F32>());
|
2020-06-01 13:43:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kOuterProduct) {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (expr->params().size() != 2) {
|
|
|
|
set_error(expr->source(),
|
2020-09-22 19:42:13 +00:00
|
|
|
"incorrect number of parameters for " + ident->name());
|
2020-06-01 13:43:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
auto* param0_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* param1_type = expr->params()[1]->result_type()->UnwrapPtrIfNeeded();
|
2020-11-30 23:30:58 +00:00
|
|
|
if (!param0_type->Is<ast::type::Vector>() ||
|
|
|
|
!param1_type->Is<ast::type::Vector>()) {
|
2020-09-22 19:42:13 +00:00
|
|
|
set_error(expr->source(), "invalid parameter type for " + ident->name());
|
2020-06-01 13:43:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->func()->set_result_type(mod_->create<ast::type::Matrix>(
|
|
|
|
mod_->create<ast::type::F32>(),
|
|
|
|
param0_type->As<ast::type::Vector>()->size(),
|
|
|
|
param1_type->As<ast::type::Vector>()->size()));
|
2020-06-01 13:43:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kSelect) {
|
2020-07-21 17:44:44 +00:00
|
|
|
if (expr->params().size() != 3) {
|
2020-09-22 19:42:13 +00:00
|
|
|
set_error(expr->source(), "incorrect number of parameters for " +
|
|
|
|
ident->name() + " expected 3 got " +
|
|
|
|
std::to_string(expr->params().size()));
|
2020-07-21 17:44:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The result type must be the same as the type of the parameter.
|
2020-09-22 19:42:13 +00:00
|
|
|
auto* param_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
expr->func()->set_result_type(param_type);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IntrinsicData* data = nullptr;
|
|
|
|
for (uint32_t i = 0; i < kIntrinsicDataCount; ++i) {
|
|
|
|
if (ident->intrinsic() == kIntrinsicData[i].intrinsic) {
|
|
|
|
data = &kIntrinsicData[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data == nullptr) {
|
2020-10-08 17:01:55 +00:00
|
|
|
error_ = "unable to find intrinsic " + ident->name();
|
2020-09-22 19:42:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr->params().size() != data->param_count) {
|
|
|
|
set_error(expr->source(), "incorrect number of parameters for " +
|
|
|
|
ident->name() + ". Expected " +
|
|
|
|
std::to_string(data->param_count) + " got " +
|
|
|
|
std::to_string(expr->params().size()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ast::type::Type*> result_types;
|
|
|
|
for (uint32_t i = 0; i < data->param_count; ++i) {
|
|
|
|
result_types.push_back(
|
|
|
|
expr->params()[i]->result_type()->UnwrapPtrIfNeeded());
|
|
|
|
|
|
|
|
switch (data->data_type) {
|
|
|
|
case IntrinsicDataType::kFloatOrIntScalarOrVector:
|
|
|
|
if (!result_types.back()->is_float_scalar_or_vector() &&
|
|
|
|
!result_types.back()->is_integer_scalar_or_vector()) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect type for " + ident->name() + ". " +
|
|
|
|
"Requires float or int, scalar or vector values");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IntrinsicDataType::kFloatScalarOrVector:
|
|
|
|
if (!result_types.back()->is_float_scalar_or_vector()) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect type for " + ident->name() + ". " +
|
|
|
|
"Requires float scalar or float vector values");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case IntrinsicDataType::kIntScalarOrVector:
|
|
|
|
if (!result_types.back()->is_integer_scalar_or_vector()) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect type for " + ident->name() + ". " +
|
|
|
|
"Requires integer scalar or integer vector values");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IntrinsicDataType::kFloatVector:
|
|
|
|
if (!result_types.back()->is_float_vector()) {
|
|
|
|
set_error(expr->source(), "incorrect type for " + ident->name() +
|
|
|
|
". " + "Requires float vector values");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (data->vector_size > 0 &&
|
2020-11-30 23:30:58 +00:00
|
|
|
result_types.back()->As<ast::type::Vector>()->size() !=
|
2020-11-30 23:30:58 +00:00
|
|
|
data->vector_size) {
|
2020-09-22 19:42:13 +00:00
|
|
|
set_error(expr->source(), "incorrect vector size for " +
|
|
|
|
ident->name() + ". " + "Requires " +
|
|
|
|
std::to_string(data->vector_size) +
|
|
|
|
" elements");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IntrinsicDataType::kMatrix:
|
2020-11-30 23:30:58 +00:00
|
|
|
if (!result_types.back()->Is<ast::type::Matrix>()) {
|
2020-09-22 19:42:13 +00:00
|
|
|
set_error(expr->source(), "incorrect type for " + ident->name() +
|
|
|
|
". Requires matrix value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify all the parameter types match
|
|
|
|
for (size_t i = 1; i < data->param_count; ++i) {
|
|
|
|
if (result_types[0] != result_types[i]) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"mismatched parameter types for " + ident->name());
|
2020-07-21 17:44:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
// Handle functions which aways return the type, even if a vector is
|
|
|
|
// provided.
|
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kLength ||
|
|
|
|
ident->intrinsic() == ast::Intrinsic::kDistance) {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->func()->set_result_type(
|
|
|
|
result_types[0]->is_float_scalar()
|
|
|
|
? result_types[0]
|
2020-11-30 23:30:58 +00:00
|
|
|
: result_types[0]->As<ast::type::Vector>()->type());
|
2020-09-22 19:42:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// The determinant returns the component type of the columns
|
|
|
|
if (ident->intrinsic() == ast::Intrinsic::kDeterminant) {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->func()->set_result_type(
|
2020-11-30 23:30:58 +00:00
|
|
|
result_types[0]->As<ast::type::Matrix>()->type());
|
2020-09-22 19:42:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
expr->func()->set_result_type(result_types[0]);
|
|
|
|
return true;
|
2020-06-01 13:43:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 12:46:30 +00:00
|
|
|
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* ty = expr->As<ast::TypeConstructorExpression>()) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* value : ty->values()) {
|
|
|
|
if (!DetermineResultType(value)) {
|
2020-04-24 00:41:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expr->set_result_type(ty->type());
|
2020-04-07 12:46:30 +00:00
|
|
|
} else {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->set_result_type(
|
|
|
|
expr->As<ast::ScalarConstructorExpression>()->literal()->type());
|
2020-04-07 12:46:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) {
|
2020-04-20 14:21:00 +00:00
|
|
|
auto name = expr->name();
|
2021-01-11 15:10:19 +00:00
|
|
|
auto symbol = expr->symbol();
|
2020-04-07 12:57:27 +00:00
|
|
|
ast::Variable* 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.
|
|
|
|
if (var->is_const()) {
|
|
|
|
expr->set_result_type(var->type());
|
2020-11-30 23:30:58 +00:00
|
|
|
} else if (var->type()->Is<ast::type::Pointer>()) {
|
2020-09-16 21:20:36 +00:00
|
|
|
expr->set_result_type(var->type());
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->set_result_type(
|
|
|
|
mod_->create<ast::type::Pointer>(var->type(), var->storage_class()));
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2020-12-08 21:07:24 +00:00
|
|
|
set_referenced_from_function_if_needed(var, true);
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = name_to_function_.find(name);
|
|
|
|
if (iter != name_to_function_.end()) {
|
|
|
|
expr->set_result_type(iter->second->return_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-14 18:26:31 +00:00
|
|
|
if (!SetIntrinsicIfNeeded(expr)) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"v-0006: identifier must be declared before use: " + name);
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 16:41:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-14 18:26:31 +00:00
|
|
|
bool TypeDeterminer::SetIntrinsicIfNeeded(ast::IdentifierExpression* ident) {
|
2020-09-22 19:42:13 +00:00
|
|
|
if (ident->name() == "abs") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAbs);
|
|
|
|
} else if (ident->name() == "acos") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAcos);
|
|
|
|
} else if (ident->name() == "all") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAll);
|
|
|
|
} else if (ident->name() == "any") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAny);
|
2020-10-08 17:01:55 +00:00
|
|
|
} else if (ident->name() == "arrayLength") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kArrayLength);
|
2020-09-22 19:42:13 +00:00
|
|
|
} else if (ident->name() == "asin") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAsin);
|
|
|
|
} else if (ident->name() == "atan") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAtan);
|
|
|
|
} else if (ident->name() == "atan2") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kAtan2);
|
|
|
|
} else if (ident->name() == "ceil") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kCeil);
|
|
|
|
} else if (ident->name() == "clamp") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kClamp);
|
|
|
|
} else if (ident->name() == "cos") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kCos);
|
|
|
|
} else if (ident->name() == "cosh") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kCosh);
|
|
|
|
} else if (ident->name() == "countOneBits") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kCountOneBits);
|
|
|
|
} else if (ident->name() == "cross") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kCross);
|
|
|
|
} else if (ident->name() == "determinant") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDeterminant);
|
|
|
|
} else if (ident->name() == "distance") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDistance);
|
|
|
|
} else if (ident->name() == "dot") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDot);
|
|
|
|
} else if (ident->name() == "dpdx") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDpdx);
|
|
|
|
} else if (ident->name() == "dpdxCoarse") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDpdxCoarse);
|
|
|
|
} else if (ident->name() == "dpdxFine") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDpdxFine);
|
|
|
|
} else if (ident->name() == "dpdy") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDpdy);
|
|
|
|
} else if (ident->name() == "dpdyCoarse") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDpdyCoarse);
|
|
|
|
} else if (ident->name() == "dpdyFine") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kDpdyFine);
|
|
|
|
} else if (ident->name() == "exp") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kExp);
|
|
|
|
} else if (ident->name() == "exp2") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kExp2);
|
|
|
|
} else if (ident->name() == "faceForward") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFaceForward);
|
|
|
|
} else if (ident->name() == "floor") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFloor);
|
|
|
|
} else if (ident->name() == "fma") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFma);
|
|
|
|
} else if (ident->name() == "fract") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFract);
|
|
|
|
} else if (ident->name() == "frexp") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFrexp);
|
|
|
|
} else if (ident->name() == "fwidth") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFwidth);
|
|
|
|
} else if (ident->name() == "fwidthCoarse") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFwidthCoarse);
|
|
|
|
} else if (ident->name() == "fwidthFine") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kFwidthFine);
|
|
|
|
} else if (ident->name() == "inverseSqrt") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kInverseSqrt);
|
|
|
|
} else if (ident->name() == "isFinite") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kIsFinite);
|
|
|
|
} else if (ident->name() == "isInf") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kIsInf);
|
|
|
|
} else if (ident->name() == "isNan") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kIsNan);
|
|
|
|
} else if (ident->name() == "isNormal") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kIsNormal);
|
|
|
|
} else if (ident->name() == "ldexp") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kLdexp);
|
|
|
|
} else if (ident->name() == "length") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kLength);
|
|
|
|
} else if (ident->name() == "log") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kLog);
|
|
|
|
} else if (ident->name() == "log2") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kLog2);
|
|
|
|
} else if (ident->name() == "max") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kMax);
|
|
|
|
} else if (ident->name() == "min") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kMin);
|
|
|
|
} else if (ident->name() == "mix") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kMix);
|
|
|
|
} else if (ident->name() == "modf") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kModf);
|
|
|
|
} else if (ident->name() == "normalize") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kNormalize);
|
|
|
|
} else if (ident->name() == "outerProduct") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kOuterProduct);
|
|
|
|
} else if (ident->name() == "pow") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kPow);
|
|
|
|
} else if (ident->name() == "reflect") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kReflect);
|
|
|
|
} else if (ident->name() == "reverseBits") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kReverseBits);
|
|
|
|
} else if (ident->name() == "round") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kRound);
|
|
|
|
} else if (ident->name() == "select") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kSelect);
|
|
|
|
} else if (ident->name() == "sign") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kSign);
|
|
|
|
} else if (ident->name() == "sin") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kSin);
|
|
|
|
} else if (ident->name() == "sinh") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kSinh);
|
|
|
|
} else if (ident->name() == "smoothStep") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kSmoothStep);
|
|
|
|
} else if (ident->name() == "sqrt") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kSqrt);
|
|
|
|
} else if (ident->name() == "step") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kStep);
|
|
|
|
} else if (ident->name() == "tan") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTan);
|
|
|
|
} else if (ident->name() == "tanh") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTanh);
|
|
|
|
} else if (ident->name() == "textureLoad") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureLoad);
|
2020-12-10 18:39:41 +00:00
|
|
|
} else if (ident->name() == "textureStore") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureStore);
|
2020-09-22 19:42:13 +00:00
|
|
|
} else if (ident->name() == "textureSample") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureSample);
|
|
|
|
} else if (ident->name() == "textureSampleBias") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureSampleBias);
|
|
|
|
} else if (ident->name() == "textureSampleCompare") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureSampleCompare);
|
2020-11-18 21:19:22 +00:00
|
|
|
} else if (ident->name() == "textureSampleGrad") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureSampleGrad);
|
2020-09-22 19:42:13 +00:00
|
|
|
} else if (ident->name() == "textureSampleLevel") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTextureSampleLevel);
|
|
|
|
} else if (ident->name() == "trunc") {
|
|
|
|
ident->set_intrinsic(ast::Intrinsic::kTrunc);
|
2020-10-14 18:26:31 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2020-09-22 19:42:13 +00:00
|
|
|
}
|
2020-10-14 18:26:31 +00:00
|
|
|
return true;
|
2020-09-22 19:42:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:33 +00:00
|
|
|
bool TypeDeterminer::DetermineMemberAccessor(
|
|
|
|
ast::MemberAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->structure())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* res = expr->structure()->result_type();
|
2020-10-28 20:32:22 +00:00
|
|
|
auto* data_type = res->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
|
2020-04-24 00:40:45 +00:00
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
ast::type::Type* ret = nullptr;
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* ty = data_type->As<ast::type::Struct>()) {
|
|
|
|
auto* strct = ty->impl();
|
2020-04-20 14:21:00 +00:00
|
|
|
auto name = expr->member()->name();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* member : strct->members()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
if (member->name() == name) {
|
|
|
|
ret = member->type();
|
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
|
|
|
set_error(expr->source(), "struct member " + name + " not found");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* ptr = res->As<ast::type::Pointer>()) {
|
|
|
|
ret = mod_->create<ast::type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (auto* vec = data_type->As<ast::type::Vector>()) {
|
2020-04-21 13:05:34 +00:00
|
|
|
auto size = expr->member()->name().size();
|
|
|
|
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.
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* ptr = res->As<ast::type::Pointer>()) {
|
|
|
|
ret = mod_->create<ast::type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
|
|
|
// The vector will have a number of components equal to the length of the
|
|
|
|
// swizzle. This assumes the validator will check that the swizzle
|
|
|
|
// is correct.
|
2020-11-30 23:30:58 +00:00
|
|
|
ret = mod_->create<ast::type::Vector>(vec->type(), size);
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2020-11-16 14:35:17 +00:00
|
|
|
set_error(
|
|
|
|
expr->source(),
|
|
|
|
"v-0007: invalid use of member accessor on a non-vector/non-struct " +
|
|
|
|
data_type->type_name());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(ret);
|
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:41 +00:00
|
|
|
bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) {
|
2020-04-07 19:26:39 +00:00
|
|
|
if (!DetermineResultType(expr->lhs()) || !DetermineResultType(expr->rhs())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->lhs()->result_type()->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()) {
|
2020-11-30 23:30:58 +00:00
|
|
|
auto* bool_type = mod_->create<ast::type::Bool>();
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* param_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded();
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* vec = param_type->As<ast::type::Vector>()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
mod_->create<ast::type::Vector>(bool_type, vec->size()));
|
2020-04-07 19:26:39 +00:00
|
|
|
} else {
|
|
|
|
expr->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (expr->IsMultiply()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* lhs_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* rhs_type = expr->rhs()->result_type()->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.
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* lhs_mat = lhs_type->As<ast::type::Matrix>();
|
|
|
|
auto* rhs_mat = rhs_type->As<ast::type::Matrix>();
|
|
|
|
auto* lhs_vec = lhs_type->As<ast::type::Vector>();
|
|
|
|
auto* rhs_vec = rhs_type->As<ast::type::Vector>();
|
|
|
|
if (lhs_mat && rhs_mat) {
|
2020-11-30 23:30:58 +00:00
|
|
|
expr->set_result_type(mod_->create<ast::type::Matrix>(
|
2020-12-01 21:07:27 +00:00
|
|
|
lhs_mat->type(), lhs_mat->rows(), rhs_mat->columns()));
|
|
|
|
} else if (lhs_mat && rhs_vec) {
|
2020-11-23 19:58:55 +00:00
|
|
|
expr->set_result_type(
|
2020-12-01 21:07:27 +00:00
|
|
|
mod_->create<ast::type::Vector>(lhs_mat->type(), lhs_mat->rows()));
|
|
|
|
} else if (lhs_vec && rhs_mat) {
|
2020-04-07 19:26:39 +00:00
|
|
|
expr->set_result_type(
|
2020-12-01 21:07:27 +00:00
|
|
|
mod_->create<ast::type::Vector>(rhs_mat->type(), rhs_mat->columns()));
|
|
|
|
} else if (lhs_mat) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// matrix * scalar
|
|
|
|
expr->set_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
|
|
|
|
expr->set_result_type(rhs_type);
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec && rhs_vec) {
|
2020-04-07 19:26:39 +00:00
|
|
|
expr->set_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
|
|
|
|
expr->set_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
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else {
|
|
|
|
// Scalar * Scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(expr->source(), "Unknown binary expression");
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:11 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryOp(ast::UnaryOpExpression* expr) {
|
2020-04-07 19:27:21 +00:00
|
|
|
// Result type matches the parameter type.
|
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->expr()->result_type()->UnwrapPtrIfNeeded());
|
2020-04-07 19:27:21 +00:00
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:11:23 +00:00
|
|
|
bool TypeDeterminer::DetermineStorageTextureSubtype(
|
2020-11-30 23:30:58 +00:00
|
|
|
ast::type::StorageTexture* tex) {
|
2020-08-31 15:11:23 +00:00
|
|
|
if (tex->type() != nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tex->image_format()) {
|
|
|
|
case ast::type::ImageFormat::kR8Uint:
|
|
|
|
case ast::type::ImageFormat::kR16Uint:
|
|
|
|
case ast::type::ImageFormat::kRg8Uint:
|
|
|
|
case ast::type::ImageFormat::kR32Uint:
|
|
|
|
case ast::type::ImageFormat::kRg16Uint:
|
|
|
|
case ast::type::ImageFormat::kRgba8Uint:
|
|
|
|
case ast::type::ImageFormat::kRg32Uint:
|
|
|
|
case ast::type::ImageFormat::kRgba16Uint:
|
|
|
|
case ast::type::ImageFormat::kRgba32Uint: {
|
2020-11-30 23:30:58 +00:00
|
|
|
tex->set_type(mod_->create<ast::type::U32>());
|
2020-08-31 15:11:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ast::type::ImageFormat::kR8Sint:
|
|
|
|
case ast::type::ImageFormat::kR16Sint:
|
|
|
|
case ast::type::ImageFormat::kRg8Sint:
|
|
|
|
case ast::type::ImageFormat::kR32Sint:
|
|
|
|
case ast::type::ImageFormat::kRg16Sint:
|
|
|
|
case ast::type::ImageFormat::kRgba8Sint:
|
|
|
|
case ast::type::ImageFormat::kRg32Sint:
|
|
|
|
case ast::type::ImageFormat::kRgba16Sint:
|
|
|
|
case ast::type::ImageFormat::kRgba32Sint: {
|
2020-11-30 23:30:58 +00:00
|
|
|
tex->set_type(mod_->create<ast::type::I32>());
|
2020-08-31 15:11:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:58:47 +00:00
|
|
|
case ast::type::ImageFormat::kR8Unorm:
|
|
|
|
case ast::type::ImageFormat::kRg8Unorm:
|
|
|
|
case ast::type::ImageFormat::kRgba8Unorm:
|
|
|
|
case ast::type::ImageFormat::kRgba8UnormSrgb:
|
|
|
|
case ast::type::ImageFormat::kBgra8Unorm:
|
|
|
|
case ast::type::ImageFormat::kBgra8UnormSrgb:
|
|
|
|
case ast::type::ImageFormat::kRgb10A2Unorm:
|
|
|
|
case ast::type::ImageFormat::kR8Snorm:
|
|
|
|
case ast::type::ImageFormat::kRg8Snorm:
|
|
|
|
case ast::type::ImageFormat::kRgba8Snorm:
|
2020-08-31 15:11:23 +00:00
|
|
|
case ast::type::ImageFormat::kR16Float:
|
|
|
|
case ast::type::ImageFormat::kR32Float:
|
|
|
|
case ast::type::ImageFormat::kRg16Float:
|
|
|
|
case ast::type::ImageFormat::kRg11B10Float:
|
|
|
|
case ast::type::ImageFormat::kRg32Float:
|
|
|
|
case ast::type::ImageFormat::kRgba16Float:
|
|
|
|
case ast::type::ImageFormat::kRgba32Float: {
|
2020-11-30 23:30:58 +00:00
|
|
|
tex->set_type(mod_->create<ast::type::F32>());
|
2020-08-31 15:11:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-08 16:38:39 +00:00
|
|
|
|
|
|
|
case ast::type::ImageFormat::kNone:
|
|
|
|
break;
|
2020-08-31 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|