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>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
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-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: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"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program_builder.h"
|
2021-02-03 21:02:25 +00:00
|
|
|
#include "src/semantic/call.h"
|
2021-01-29 16:43:41 +00:00
|
|
|
#include "src/semantic/expression.h"
|
2021-02-03 16:43:20 +00:00
|
|
|
#include "src/semantic/function.h"
|
2021-02-03 21:02:25 +00:00
|
|
|
#include "src/semantic/intrinsic.h"
|
2021-02-03 17:51:09 +00:00
|
|
|
#include "src/semantic/variable.h"
|
2021-01-21 15:42:10 +00:00
|
|
|
#include "src/type/array_type.h"
|
|
|
|
#include "src/type/bool_type.h"
|
|
|
|
#include "src/type/depth_texture_type.h"
|
|
|
|
#include "src/type/f32_type.h"
|
|
|
|
#include "src/type/i32_type.h"
|
|
|
|
#include "src/type/matrix_type.h"
|
|
|
|
#include "src/type/multisampled_texture_type.h"
|
|
|
|
#include "src/type/pointer_type.h"
|
|
|
|
#include "src/type/sampled_texture_type.h"
|
|
|
|
#include "src/type/storage_texture_type.h"
|
|
|
|
#include "src/type/struct_type.h"
|
|
|
|
#include "src/type/texture_type.h"
|
|
|
|
#include "src/type/u32_type.h"
|
|
|
|
#include "src/type/vector_type.h"
|
|
|
|
#include "src/type/void_type.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TypeDeterminer::TypeDeterminer(ProgramBuilder* builder) : builder_(builder) {}
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
TypeDeterminer::~TypeDeterminer() = default;
|
|
|
|
|
2021-01-25 22:09:18 +00:00
|
|
|
diag::List TypeDeterminer::Run(Program* program) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder = program->CloneAsBuilder();
|
|
|
|
TypeDeterminer td(&builder);
|
2021-01-25 22:09:18 +00:00
|
|
|
if (!td.Determine()) {
|
2021-01-26 18:52:11 +00:00
|
|
|
diag::List diagnostics;
|
|
|
|
diagnostics.add_error(td.error());
|
|
|
|
return diagnostics;
|
2021-01-25 22:09:18 +00:00
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
*program = Program(std::move(builder));
|
2021-01-25 22:09:18 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
void TypeDeterminer::set_referenced_from_function_if_needed(VariableInfo* var,
|
2020-12-08 21:07:24 +00:00
|
|
|
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-03 16:43:20 +00:00
|
|
|
current_function_->referenced_module_vars.Add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
if (local) {
|
2021-02-03 16:43:20 +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
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
bool TypeDeterminer::Determine() {
|
2021-02-03 17:51:09 +00:00
|
|
|
bool result = DetermineInternal();
|
|
|
|
|
|
|
|
// Even if resolving failed, create all the semantic nodes for information we
|
|
|
|
// did generate.
|
|
|
|
CreateSemanticNodes();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineInternal() {
|
2021-01-26 16:57:10 +00:00
|
|
|
for (auto* var : builder_->AST().GlobalVariables()) {
|
2021-02-03 17:51:09 +00:00
|
|
|
variable_stack_.set_global(var->symbol(), CreateVariableInfo(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
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
if (!DetermineFunctions(builder_->AST().Functions())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-14 19:45:47 +00:00
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
// Walk over the caller to callee information and update functions with
|
|
|
|
// which entry points call those functions.
|
2021-01-26 16:57:10 +00:00
|
|
|
for (auto* func : builder_->AST().Functions()) {
|
2020-09-21 18:49:01 +00:00
|
|
|
if (!func->IsEntryPoint()) {
|
2020-09-21 17:51:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-11 22:02:42 +00:00
|
|
|
for (const auto& callee : caller_to_callee_[func->symbol()]) {
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-11 16:24:32 +00:00
|
|
|
void TypeDeterminer::set_entry_points(const Symbol& fn_sym, Symbol ep_sym) {
|
2021-02-03 16:43:20 +00:00
|
|
|
auto* info = symbol_to_function_.at(fn_sym);
|
|
|
|
info->ancestor_entry_points.Add(ep_sym);
|
2020-07-14 19:45:47 +00:00
|
|
|
|
2021-01-11 22:02:42 +00:00
|
|
|
for (const auto& callee : caller_to_callee_[fn_sym]) {
|
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) {
|
2021-02-03 17:51:09 +00:00
|
|
|
current_function_ = function_infos_.Create<FunctionInfo>(func);
|
|
|
|
symbol_to_function_[func->symbol()] = current_function_;
|
|
|
|
function_to_info_.emplace(func, current_function_);
|
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
|
|
|
}
|
|
|
|
|
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();
|
2021-02-03 17:51:09 +00:00
|
|
|
|
|
|
|
auto* info = CreateVariableInfo(var);
|
|
|
|
variable_to_info_.emplace(var, info);
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
// Nothing to do for const
|
|
|
|
if (var->is_const()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
if (info->storage_class == ast::StorageClass::kFunction) {
|
2020-04-08 19:58:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
if (info->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;
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
info->storage_class = ast::StorageClass::kFunction;
|
2020-04-08 19:58:20 +00:00
|
|
|
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-02-03 17:51:09 +00:00
|
|
|
variable_stack_.set(v->variable()->symbol(),
|
|
|
|
variable_to_info_.at(v->variable()));
|
2020-04-07 12:57:12 +00:00
|
|
|
return DetermineResultType(v->variable()->constructor());
|
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2021-01-29 10:55:40 +00:00
|
|
|
set_error(stmt->source(), "unknown statement type for type determination: " +
|
2021-01-29 11:22:40 +00:00
|
|
|
builder_->str(stmt));
|
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;
|
|
|
|
}
|
|
|
|
|
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>()) {
|
|
|
|
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
|
|
|
|
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 {
|
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.
|
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
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, expr->type());
|
2020-04-07 12:57:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
bool TypeDeterminer::DetermineCall(ast::CallExpression* call) {
|
|
|
|
if (!DetermineResultType(call->func())) {
|
2020-09-22 19:42:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
if (!DetermineResultType(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) {
|
|
|
|
set_error(call->source(), "call target is not an identifier");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol());
|
|
|
|
|
|
|
|
auto intrinsic = MatchIntrinsic(name);
|
|
|
|
if (intrinsic != semantic::Intrinsic::kNone) {
|
|
|
|
if (!DetermineIntrinsicCall(call, intrinsic)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (current_function_) {
|
|
|
|
caller_to_callee_[current_function_->declaration->symbol()].push_back(
|
|
|
|
ident->symbol());
|
|
|
|
|
|
|
|
auto callee_func_it = symbol_to_function_.find(ident->symbol());
|
|
|
|
if (callee_func_it == symbol_to_function_.end()) {
|
|
|
|
set_error(call->source(), "unable to find called function: " + name);
|
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-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()) {
|
|
|
|
set_error(call->source(),
|
|
|
|
"v-0005: function must be declared before use: '" + name + "'");
|
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;
|
|
|
|
auto* return_ty = function->declaration->return_type();
|
|
|
|
auto* sem = builder_->create<semantic::Call>(return_ty);
|
|
|
|
builder_->Sem().Add(call, sem);
|
2020-08-18 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class IntrinsicDataType {
|
2021-02-01 15:33:13 +00:00
|
|
|
kDependent,
|
|
|
|
kSignedInteger,
|
|
|
|
kUnsignedInteger,
|
|
|
|
kFloat,
|
2021-02-01 16:03:03 +00:00
|
|
|
kBool,
|
2020-09-22 19:42:13 +00:00
|
|
|
};
|
2021-02-01 15:33:13 +00:00
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
struct IntrinsicData {
|
2021-02-03 21:02:25 +00:00
|
|
|
semantic::Intrinsic intrinsic;
|
2021-02-01 15:33:13 +00:00
|
|
|
IntrinsicDataType result_type;
|
|
|
|
uint8_t result_vector_width;
|
|
|
|
uint8_t param_for_result_type;
|
2020-09-22 19:42:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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[] = {
|
2021-02-03 21:02:25 +00:00
|
|
|
{semantic::Intrinsic::kAbs, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kAcos, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kAll, IntrinsicDataType::kBool, 1, 0},
|
|
|
|
{semantic::Intrinsic::kAny, IntrinsicDataType::kBool, 1, 0},
|
|
|
|
{semantic::Intrinsic::kArrayLength, IntrinsicDataType::kUnsignedInteger, 1,
|
|
|
|
0},
|
|
|
|
{semantic::Intrinsic::kAsin, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kAtan, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kAtan2, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kCeil, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kClamp, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kCos, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kCosh, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kCountOneBits, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kCross, IntrinsicDataType::kFloat, 3, 0},
|
|
|
|
{semantic::Intrinsic::kDeterminant, IntrinsicDataType::kFloat, 1, 0},
|
|
|
|
{semantic::Intrinsic::kDistance, IntrinsicDataType::kFloat, 1, 0},
|
|
|
|
{semantic::Intrinsic::kDpdx, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kDpdxCoarse, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kDpdxFine, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kDpdy, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kDpdyCoarse, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kDpdyFine, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kDot, IntrinsicDataType::kFloat, 1, 0},
|
|
|
|
{semantic::Intrinsic::kExp, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kExp2, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFaceForward, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFloor, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFwidth, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFwidthCoarse, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFwidthFine, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFma, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFract, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kFrexp, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kInverseSqrt, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kLdexp, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kLength, IntrinsicDataType::kFloat, 1, 0},
|
|
|
|
{semantic::Intrinsic::kLog, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kLog2, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kMax, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kMin, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kMix, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kModf, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kNormalize, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kPow, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kReflect, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kReverseBits, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kRound, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kSelect, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kSign, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kSin, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kSinh, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kSmoothStep, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kSqrt, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kStep, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kTan, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kTanh, IntrinsicDataType::kDependent, 0, 0},
|
|
|
|
{semantic::Intrinsic::kTrunc, IntrinsicDataType::kDependent, 0, 0},
|
2020-09-22 19:42:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constexpr const uint32_t kIntrinsicDataCount =
|
|
|
|
sizeof(kIntrinsicData) / sizeof(IntrinsicData);
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
bool TypeDeterminer::DetermineIntrinsicCall(ast::CallExpression* call,
|
|
|
|
semantic::Intrinsic intrinsic) {
|
|
|
|
auto create_sem = [&](type::Type* result) {
|
|
|
|
auto* sem = builder_->create<semantic::IntrinsicCall>(result, intrinsic);
|
|
|
|
builder_->Sem().Add(call, sem);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string name = semantic::intrinsic::str(intrinsic);
|
|
|
|
if (semantic::intrinsic::IsFloatClassificationIntrinsic(intrinsic)) {
|
|
|
|
if (call->params().size() != 1) {
|
|
|
|
set_error(call->source(), "incorrect number of parameters for " + name);
|
2020-06-01 13:43:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* bool_type = builder_->create<type::Bool>();
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* param_type = TypeOf(call->params()[0])->UnwrapPtrIfNeeded();
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* vec = param_type->As<type::Vector>()) {
|
2021-02-03 21:02:25 +00:00
|
|
|
create_sem(builder_->create<type::Vector>(bool_type, vec->size()));
|
2020-06-01 13:43:22 +00:00
|
|
|
} else {
|
2021-02-03 21:02:25 +00:00
|
|
|
create_sem(bool_type);
|
2020-06-01 13:43:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
if (semantic::intrinsic::IsTextureIntrinsic(intrinsic)) {
|
|
|
|
semantic::TextureIntrinsicCall::Parameters param;
|
2020-11-18 21:19:22 +00:00
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* texture_param = call->params()[0];
|
2021-01-29 16:43:41 +00:00
|
|
|
if (!TypeOf(texture_param)->UnwrapAll()->Is<type::Texture>()) {
|
2021-02-03 21:02:25 +00:00
|
|
|
set_error(call->source(), "invalid first argument for " + name);
|
2020-11-18 21:19:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Texture* texture =
|
2021-01-29 16:43:41 +00:00
|
|
|
TypeOf(texture_param)->UnwrapAll()->As<type::Texture>();
|
2020-11-18 21:19:22 +00:00
|
|
|
|
2021-01-21 15:42:10 +00:00
|
|
|
bool is_array = type::IsTextureArray(texture->dim());
|
|
|
|
bool is_multisampled = texture->Is<type::MultisampledTexture>();
|
2021-02-03 21:02:25 +00:00
|
|
|
switch (intrinsic) {
|
|
|
|
case semantic::Intrinsic::kTextureDimensions:
|
2021-01-11 21:07:32 +00:00
|
|
|
param.idx.texture = param.count++;
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() > param.count) {
|
2021-01-11 21:07:32 +00:00
|
|
|
param.idx.level = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureNumLayers:
|
|
|
|
case semantic::Intrinsic::kTextureNumLevels:
|
|
|
|
case semantic::Intrinsic::kTextureNumSamples:
|
2021-01-14 16:50:07 +00:00
|
|
|
param.idx.texture = param.count++;
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureLoad:
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->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;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureSample:
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.texture = param.count++;
|
|
|
|
param.idx.sampler = param.count++;
|
|
|
|
param.idx.coords = param.count++;
|
|
|
|
if (is_array) {
|
|
|
|
param.idx.array_index = param.count++;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() > param.count) {
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureSampleBias:
|
2020-11-18 21:19:22 +00:00
|
|
|
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++;
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() > param.count) {
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureSampleLevel:
|
2020-11-18 21:19:22 +00:00
|
|
|
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++;
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() > param.count) {
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureSampleCompare:
|
2020-11-18 21:19:22 +00:00
|
|
|
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++;
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() > param.count) {
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureSampleGrad:
|
2020-11-18 21:19:22 +00:00
|
|
|
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++;
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() > param.count) {
|
2020-11-18 21:19:22 +00:00
|
|
|
param.idx.offset = param.count++;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureStore:
|
2020-12-10 18:39:41 +00:00
|
|
|
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:
|
2021-02-03 21:02:25 +00:00
|
|
|
set_error(call->source(),
|
2020-11-18 21:19:22 +00:00
|
|
|
"Internal compiler error: Unreachable intrinsic " +
|
2021-02-03 21:02:25 +00:00
|
|
|
std::to_string(static_cast<int>(intrinsic)));
|
2020-11-18 21:19:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() != param.count) {
|
|
|
|
set_error(call->source(),
|
|
|
|
"incorrect number of parameters for " + name + ", got " +
|
|
|
|
std::to_string(call->params().size()) + " and expected " +
|
2021-01-11 16:24:32 +00:00
|
|
|
std::to_string(param.count));
|
2020-08-31 15:24:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-10 18:39:41 +00:00
|
|
|
// Set the function return type
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Type* return_type = nullptr;
|
2021-02-03 21:02:25 +00:00
|
|
|
switch (intrinsic) {
|
|
|
|
case semantic::Intrinsic::kTextureDimensions: {
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* i32 = builder_->create<type::I32>();
|
2021-01-14 16:50:07 +00:00
|
|
|
switch (texture->dim()) {
|
|
|
|
default:
|
2021-02-03 21:02:25 +00:00
|
|
|
set_error(call->source(), "invalid texture dimensions");
|
2021-01-14 16:50:07 +00:00
|
|
|
break;
|
2021-01-21 15:42:10 +00:00
|
|
|
case type::TextureDimension::k1d:
|
|
|
|
case type::TextureDimension::k1dArray:
|
2021-01-14 16:50:07 +00:00
|
|
|
return_type = i32;
|
|
|
|
break;
|
2021-01-21 15:42:10 +00:00
|
|
|
case type::TextureDimension::k2d:
|
|
|
|
case type::TextureDimension::k2dArray:
|
2021-01-26 16:57:10 +00:00
|
|
|
return_type = builder_->create<type::Vector>(i32, 2);
|
2021-01-14 16:50:07 +00:00
|
|
|
break;
|
2021-01-21 15:42:10 +00:00
|
|
|
case type::TextureDimension::k3d:
|
|
|
|
case type::TextureDimension::kCube:
|
|
|
|
case type::TextureDimension::kCubeArray:
|
2021-01-26 16:57:10 +00:00
|
|
|
return_type = builder_->create<type::Vector>(i32, 3);
|
2021-01-14 16:50:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2021-01-11 21:07:32 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureNumLayers:
|
|
|
|
case semantic::Intrinsic::kTextureNumLevels:
|
|
|
|
case semantic::Intrinsic::kTextureNumSamples:
|
2021-01-26 16:57:10 +00:00
|
|
|
return_type = builder_->create<type::I32>();
|
2021-01-14 16:50:07 +00:00
|
|
|
break;
|
2021-02-03 21:02:25 +00:00
|
|
|
case semantic::Intrinsic::kTextureStore:
|
2021-01-26 16:57:10 +00:00
|
|
|
return_type = builder_->create<type::Void>();
|
2021-01-14 16:50:07 +00:00
|
|
|
break;
|
|
|
|
default: {
|
2021-01-21 15:42:10 +00:00
|
|
|
if (texture->Is<type::DepthTexture>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
return_type = builder_->create<type::F32>();
|
2020-12-10 18:39:41 +00:00
|
|
|
} else {
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Type* type = nullptr;
|
|
|
|
if (auto* storage = texture->As<type::StorageTexture>()) {
|
2021-01-14 16:50:07 +00:00
|
|
|
type = storage->type();
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* sampled = texture->As<type::SampledTexture>()) {
|
2021-01-14 16:50:07 +00:00
|
|
|
type = sampled->type();
|
|
|
|
} else if (auto* msampled =
|
2021-01-21 15:42:10 +00:00
|
|
|
texture->As<type::MultisampledTexture>()) {
|
2021-01-14 16:50:07 +00:00
|
|
|
type = msampled->type();
|
|
|
|
} else {
|
2021-02-03 21:02:25 +00:00
|
|
|
set_error(call->source(),
|
2021-01-14 16:50:07 +00:00
|
|
|
"unknown texture type for texture sampling");
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-26 16:57:10 +00:00
|
|
|
return_type = builder_->create<type::Vector>(type, 4);
|
2020-12-10 18:39:41 +00:00
|
|
|
}
|
2020-12-09 15:57:00 +00:00
|
|
|
}
|
2020-09-21 15:52:10 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
|
|
|
auto* sem = builder_->create<semantic::TextureIntrinsicCall>(
|
|
|
|
return_type, intrinsic, param);
|
|
|
|
builder_->Sem().Add(call, sem);
|
2020-12-10 18:39:41 +00:00
|
|
|
|
2020-08-31 15:24:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
|
|
|
|
const IntrinsicData* data = nullptr;
|
|
|
|
for (uint32_t i = 0; i < kIntrinsicDataCount; ++i) {
|
2021-02-03 21:02:25 +00:00
|
|
|
if (intrinsic == kIntrinsicData[i].intrinsic) {
|
2020-09-22 19:42:13 +00:00
|
|
|
data = &kIntrinsicData[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (data == nullptr) {
|
2021-02-03 21:02:25 +00:00
|
|
|
error_ = "unable to find intrinsic " + name;
|
2020-09-22 19:42:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-01 15:33:13 +00:00
|
|
|
if (data->result_type == IntrinsicDataType::kDependent) {
|
|
|
|
const auto param_idx = data->param_for_result_type;
|
2021-02-03 21:02:25 +00:00
|
|
|
if (call->params().size() <= param_idx) {
|
|
|
|
set_error(call->source(),
|
2021-02-01 15:33:13 +00:00
|
|
|
"missing parameter " + std::to_string(param_idx) +
|
2021-02-03 21:02:25 +00:00
|
|
|
" required for type determination in builtin " + name);
|
2021-02-01 15:33:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
create_sem(TypeOf(call->params()[param_idx])->UnwrapPtrIfNeeded());
|
2021-02-01 15:33:13 +00:00
|
|
|
} else {
|
|
|
|
// The result type is not dependent on the parameter types.
|
|
|
|
type::Type* type = nullptr;
|
|
|
|
switch (data->result_type) {
|
|
|
|
case IntrinsicDataType::kSignedInteger:
|
|
|
|
type = builder_->create<type::I32>();
|
2020-09-22 19:42:13 +00:00
|
|
|
break;
|
2021-02-01 15:33:13 +00:00
|
|
|
case IntrinsicDataType::kUnsignedInteger:
|
|
|
|
type = builder_->create<type::U32>();
|
2020-09-22 19:42:13 +00:00
|
|
|
break;
|
2021-02-01 15:33:13 +00:00
|
|
|
case IntrinsicDataType::kFloat:
|
|
|
|
type = builder_->create<type::F32>();
|
2020-09-22 19:42:13 +00:00
|
|
|
break;
|
2021-02-01 16:03:03 +00:00
|
|
|
case IntrinsicDataType::kBool:
|
|
|
|
type = builder_->create<type::Bool>();
|
|
|
|
break;
|
2021-02-01 15:33:13 +00:00
|
|
|
default:
|
2021-02-03 21:02:25 +00:00
|
|
|
error_ = "unhandled intrinsic data type for " + name;
|
2021-02-01 15:33:13 +00:00
|
|
|
return false;
|
2020-09-22 19:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 15:33:13 +00:00
|
|
|
if (data->result_vector_width > 1) {
|
|
|
|
type = builder_->create<type::Vector>(type, data->result_vector_width);
|
2020-07-21 17:44:44 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
create_sem(type);
|
2020-07-21 17:44:44 +00:00
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, ty->type());
|
2020-04-07 12:46:30 +00:00
|
|
|
} else {
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr,
|
|
|
|
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) {
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-03 21:02:25 +00:00
|
|
|
// Identifier is to a function, which has no type (currently).
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
std::string name = builder_->Symbols().NameFor(symbol);
|
|
|
|
if (MatchIntrinsic(name) != semantic::Intrinsic::kNone) {
|
|
|
|
// Identifier is to an intrinsic function, which has no type (currently).
|
|
|
|
return true;
|
2020-10-14 18:26:31 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
|
|
|
set_error(expr->source(),
|
|
|
|
"v-0006: identifier must be declared before use: " + name);
|
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
semantic::Intrinsic TypeDeterminer::MatchIntrinsic(const std::string& name) {
|
2021-01-11 16:24:32 +00:00
|
|
|
if (name == "abs") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAbs;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "acos") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAcos;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "all") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAll;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "any") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAny;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "arrayLength") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kArrayLength;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "asin") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAsin;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "atan") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAtan;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "atan2") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kAtan2;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "ceil") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kCeil;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "clamp") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kClamp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "cos") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kCos;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "cosh") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kCosh;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "countOneBits") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kCountOneBits;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "cross") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kCross;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "determinant") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDeterminant;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "distance") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDistance;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dot") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDot;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdx") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDpdx;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdxCoarse") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDpdxCoarse;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdxFine") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDpdxFine;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdy") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDpdy;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdyCoarse") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDpdyCoarse;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdyFine") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kDpdyFine;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "exp") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kExp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "exp2") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kExp2;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "faceForward") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFaceForward;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "floor") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFloor;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fma") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFma;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fract") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFract;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "frexp") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFrexp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fwidth") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFwidth;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fwidthCoarse") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFwidthCoarse;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fwidthFine") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kFwidthFine;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "inverseSqrt") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kInverseSqrt;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isFinite") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kIsFinite;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isInf") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kIsInf;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isNan") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kIsNan;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isNormal") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kIsNormal;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "ldexp") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kLdexp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "length") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kLength;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "log") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kLog;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "log2") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kLog2;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "max") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kMax;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "min") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kMin;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "mix") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kMix;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "modf") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kModf;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "normalize") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kNormalize;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "pow") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kPow;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "reflect") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kReflect;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "reverseBits") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kReverseBits;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "round") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kRound;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "select") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kSelect;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sign") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kSign;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sin") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kSin;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sinh") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kSinh;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "smoothStep") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kSmoothStep;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sqrt") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kSqrt;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "step") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kStep;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "tan") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTan;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "tanh") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTanh;
|
2021-01-11 21:07:32 +00:00
|
|
|
} else if (name == "textureDimensions") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureDimensions;
|
2021-01-14 16:50:07 +00:00
|
|
|
} else if (name == "textureNumLayers") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureNumLayers;
|
2021-01-14 18:06:57 +00:00
|
|
|
} else if (name == "textureNumLevels") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureNumLevels;
|
2021-01-14 18:11:17 +00:00
|
|
|
} else if (name == "textureNumSamples") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureNumSamples;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureLoad") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureLoad;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureStore") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureStore;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSample") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureSample;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleBias") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureSampleBias;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleCompare") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureSampleCompare;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleGrad") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureSampleGrad;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleLevel") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTextureSampleLevel;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "trunc") {
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kTrunc;
|
2020-09-22 19:42:13 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
return semantic::Intrinsic::kNone;
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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-01-26 16:57:10 +00:00
|
|
|
set_error(expr->source(), "struct member " +
|
2021-01-26 16:57:10 +00:00
|
|
|
builder_->Symbols().NameFor(symbol) +
|
2021-01-26 16:57:10 +00:00
|
|
|
" not found");
|
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-03 22:26:36 +00:00
|
|
|
expr->SetIsSwizzle();
|
2021-01-11 16:24:32 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
auto size = builder_->Symbols().NameFor(expr->member()->symbol()).size();
|
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 {
|
|
|
|
// 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.
|
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 {
|
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
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
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()) {
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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-02-03 17:51:09 +00:00
|
|
|
TypeDeterminer::VariableInfo* TypeDeterminer::CreateVariableInfo(
|
|
|
|
ast::Variable* var) {
|
|
|
|
auto* info = variable_infos_.Create(var);
|
|
|
|
variable_to_info_.emplace(var, info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
void TypeDeterminer::SetType(ast::Expression* expr, type::Type* type) const {
|
|
|
|
return builder_->Sem().Add(expr,
|
|
|
|
builder_->create<semantic::Expression>(type));
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
void TypeDeterminer::CreateSemanticNodes() const {
|
|
|
|
auto& sem = builder_->Sem();
|
|
|
|
|
|
|
|
for (auto it : variable_to_info_) {
|
|
|
|
auto* var = it.first;
|
|
|
|
auto* info = it.second;
|
|
|
|
sem.Add(var,
|
|
|
|
builder_->create<semantic::Variable>(var, info->storage_class));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 16:43:20 +00:00
|
|
|
for (auto it : function_to_info_) {
|
|
|
|
auto* func = it.first;
|
|
|
|
auto* info = it.second;
|
2021-02-03 17:51:09 +00:00
|
|
|
sem.Add(func, builder_->create<semantic::Function>(
|
|
|
|
remap_vars(info->referenced_module_vars),
|
|
|
|
remap_vars(info->local_referenced_module_vars),
|
|
|
|
info->ancestor_entry_points));
|
2021-02-03 16:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
TypeDeterminer::VariableInfo::VariableInfo(ast::Variable* decl)
|
|
|
|
: declaration(decl), storage_class(decl->declared_storage_class()) {}
|
|
|
|
|
|
|
|
TypeDeterminer::VariableInfo::~VariableInfo() = default;
|
|
|
|
|
2021-02-03 16:43:20 +00:00
|
|
|
TypeDeterminer::FunctionInfo::FunctionInfo(ast::Function* decl)
|
|
|
|
: declaration(decl) {}
|
|
|
|
|
|
|
|
TypeDeterminer::FunctionInfo::~FunctionInfo() = default;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|