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>
|
|
|
|
|
|
|
|
#include "src/ast/array_accessor_expression.h"
|
2020-04-07 12:57:52 +00:00
|
|
|
#include "src/ast/as_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-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-04-07 12:54:20 +00:00
|
|
|
#include "src/ast/case_statement.h"
|
2020-04-07 16:41:23 +00:00
|
|
|
#include "src/ast/cast_expression.h"
|
2020-04-07 12:54:29 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2020-04-07 12:54:37 +00:00
|
|
|
#include "src/ast/else_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: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-04-07 19:27:00 +00:00
|
|
|
#include "src/ast/type/f32_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/matrix_type.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/type/struct_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/vector_type.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/type_constructor_expression.h"
|
2020-04-07 19:26:49 +00:00
|
|
|
#include "src/ast/unary_derivative_expression.h"
|
2020-04-07 19:27:00 +00:00
|
|
|
#include "src/ast/unary_method_expression.h"
|
2020-04-07 19:27:21 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2020-04-07 12:56:57 +00:00
|
|
|
#include "src/ast/unless_statement.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-04-08 19:58:35 +00:00
|
|
|
TypeDeterminer::TypeDeterminer(Context* ctx) : ctx_(*ctx) {}
|
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_ = "";
|
|
|
|
if (src.line > 0) {
|
|
|
|
error_ +=
|
|
|
|
std::to_string(src.line) + ":" + std::to_string(src.column) + ": ";
|
|
|
|
}
|
|
|
|
error_ += msg;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
bool TypeDeterminer::Determine(ast::Module* mod) {
|
|
|
|
for (const auto& var : mod->global_variables()) {
|
|
|
|
variable_stack_.set_global(var->name(), var.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& func : mod->functions()) {
|
|
|
|
name_to_function_[func->name()] = func.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DetermineFunctions(mod->functions())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineFunctions(const ast::FunctionList& funcs) {
|
|
|
|
for (const auto& func : funcs) {
|
|
|
|
if (!DetermineFunction(func.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineFunction(ast::Function* func) {
|
|
|
|
variable_stack_.push_scope();
|
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-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
bool TypeDeterminer::DetermineStatements(const ast::StatementList& stmts) {
|
2020-04-06 21:07:41 +00:00
|
|
|
for (const auto& stmt : stmts) {
|
2020-04-08 19:58:20 +00:00
|
|
|
if (!DetermineVariableStorageClass(stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
if (!DetermineResultType(stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
|
|
|
|
if (!stmt->IsVariableDecl()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* var = stmt->AsVariableDecl()->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) {
|
|
|
|
error_ = "function variable has a non-function storage class";
|
|
|
|
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) {
|
|
|
|
if (stmt->IsAssign()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* a = stmt->AsAssign();
|
2020-04-07 12:47:23 +00:00
|
|
|
return DetermineResultType(a->lhs()) && DetermineResultType(a->rhs());
|
|
|
|
}
|
2020-04-07 12:54:10 +00:00
|
|
|
if (stmt->IsBreak()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* b = stmt->AsBreak();
|
2020-04-07 12:54:10 +00:00
|
|
|
return DetermineResultType(b->conditional());
|
|
|
|
}
|
2020-04-07 12:54:20 +00:00
|
|
|
if (stmt->IsCase()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* c = stmt->AsCase();
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(c->body());
|
2020-04-07 12:54:20 +00:00
|
|
|
}
|
2020-04-07 12:54:29 +00:00
|
|
|
if (stmt->IsContinue()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* c = stmt->AsContinue();
|
2020-04-07 12:54:29 +00:00
|
|
|
return DetermineResultType(c->conditional());
|
|
|
|
}
|
2020-04-07 12:54:37 +00:00
|
|
|
if (stmt->IsElse()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* e = stmt->AsElse();
|
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-04-07 12:54:59 +00:00
|
|
|
if (stmt->IsFallthrough()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:55:25 +00:00
|
|
|
if (stmt->IsIf()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* i = stmt->AsIf();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& else_stmt : i->else_statements()) {
|
|
|
|
if (!DetermineResultType(else_stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
if (stmt->IsKill()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:55:51 +00:00
|
|
|
if (stmt->IsLoop()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* l = stmt->AsLoop();
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(l->body()) &&
|
|
|
|
DetermineStatements(l->continuing());
|
2020-04-07 12:55:51 +00:00
|
|
|
}
|
2020-04-07 12:56:45 +00:00
|
|
|
if (stmt->IsNop()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:56:24 +00:00
|
|
|
if (stmt->IsReturn()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* r = stmt->AsReturn();
|
2020-04-07 12:56:24 +00:00
|
|
|
return DetermineResultType(r->value());
|
|
|
|
}
|
2020-04-07 12:56:45 +00:00
|
|
|
if (stmt->IsSwitch()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* s = stmt->AsSwitch();
|
2020-04-07 12:56:45 +00:00
|
|
|
if (!DetermineResultType(s->condition())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto& case_stmt : s->body()) {
|
|
|
|
if (!DetermineResultType(case_stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:56:57 +00:00
|
|
|
if (stmt->IsUnless()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* u = stmt->AsUnless();
|
2020-04-07 12:56:57 +00:00
|
|
|
return DetermineResultType(u->condition()) &&
|
2020-04-08 19:58:20 +00:00
|
|
|
DetermineStatements(u->body());
|
2020-04-07 12:56:57 +00:00
|
|
|
}
|
2020-04-07 12:57:12 +00:00
|
|
|
if (stmt->IsVariableDecl()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* v = stmt->AsVariableDecl();
|
2020-04-07 12:57:12 +00:00
|
|
|
variable_stack_.set(v->variable()->name(), v->variable());
|
|
|
|
return DetermineResultType(v->variable()->constructor());
|
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(stmt->source(), "unknown statement type for type determination");
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-07 12:57:42 +00:00
|
|
|
if (expr->IsArrayAccessor()) {
|
|
|
|
return DetermineArrayAccessor(expr->AsArrayAccessor());
|
|
|
|
}
|
2020-04-07 12:57:52 +00:00
|
|
|
if (expr->IsAs()) {
|
|
|
|
return DetermineAs(expr->AsAs());
|
|
|
|
}
|
2020-04-07 19:27:41 +00:00
|
|
|
if (expr->IsBinary()) {
|
|
|
|
return DetermineBinary(expr->AsBinary());
|
|
|
|
}
|
2020-04-07 16:41:10 +00:00
|
|
|
if (expr->IsCall()) {
|
|
|
|
return DetermineCall(expr->AsCall());
|
|
|
|
}
|
2020-04-07 16:41:23 +00:00
|
|
|
if (expr->IsCast()) {
|
|
|
|
return DetermineCast(expr->AsCast());
|
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
if (expr->IsConstructor()) {
|
|
|
|
return DetermineConstructor(expr->AsConstructor());
|
|
|
|
}
|
2020-04-07 12:57:27 +00:00
|
|
|
if (expr->IsIdentifier()) {
|
|
|
|
return DetermineIdentifier(expr->AsIdentifier());
|
|
|
|
}
|
2020-04-07 16:41:33 +00:00
|
|
|
if (expr->IsMemberAccessor()) {
|
|
|
|
return DetermineMemberAccessor(expr->AsMemberAccessor());
|
|
|
|
}
|
2020-04-07 19:26:49 +00:00
|
|
|
if (expr->IsUnaryDerivative()) {
|
|
|
|
return DetermineUnaryDerivative(expr->AsUnaryDerivative());
|
|
|
|
}
|
2020-04-07 19:27:00 +00:00
|
|
|
if (expr->IsUnaryMethod()) {
|
|
|
|
return DetermineUnaryMethod(expr->AsUnaryMethod());
|
|
|
|
}
|
2020-04-07 19:27:11 +00:00
|
|
|
if (expr->IsUnaryOp()) {
|
|
|
|
return DetermineUnaryOp(expr->AsUnaryOp());
|
|
|
|
}
|
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-04-09 18:52:06 +00:00
|
|
|
auto* parent_type = expr->array()->result_type();
|
2020-04-07 12:57:42 +00:00
|
|
|
if (parent_type->IsArray()) {
|
|
|
|
expr->set_result_type(parent_type->AsArray()->type());
|
|
|
|
} else if (parent_type->IsVector()) {
|
|
|
|
expr->set_result_type(parent_type->AsVector()->type());
|
|
|
|
} else if (parent_type->IsMatrix()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* m = parent_type->AsMatrix();
|
2020-04-07 12:57:42 +00:00
|
|
|
expr->set_result_type(ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(m->type(), m->rows())));
|
|
|
|
} else {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "invalid parent type in array accessor");
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:52 +00:00
|
|
|
bool TypeDeterminer::DetermineAs(ast::AsExpression* expr) {
|
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->func())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expr->set_result_type(expr->func()->result_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:23 +00:00
|
|
|
bool TypeDeterminer::DetermineCast(ast::CastExpression* expr) {
|
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:46:30 +00:00
|
|
|
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
|
|
|
if (expr->IsTypeConstructor()) {
|
|
|
|
expr->set_result_type(expr->AsTypeConstructor()->type());
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(expr->AsScalarConstructor()->literal()->type());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) {
|
|
|
|
if (expr->name().size() > 1) {
|
|
|
|
// TODO(dsinclair): Handle imports
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "imports not handled in type determination");
|
2020-04-07 12:57:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto name = expr->name()[0];
|
|
|
|
ast::Variable* var;
|
|
|
|
if (variable_stack_.get(name, &var)) {
|
|
|
|
expr->set_result_type(var->type());
|
|
|
|
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-04-07 16:41:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineMemberAccessor(
|
|
|
|
ast::MemberAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->structure())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* data_type = expr->structure()->result_type();
|
2020-04-07 16:41:33 +00:00
|
|
|
if (data_type->IsStruct()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* strct = data_type->AsStruct()->impl();
|
2020-04-07 16:41:33 +00:00
|
|
|
auto name = expr->member()->name()[0];
|
|
|
|
|
|
|
|
for (const auto& member : strct->members()) {
|
|
|
|
if (member->name() != name) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr->set_result_type(member->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "struct member not found");
|
2020-04-07 16:41:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (data_type->IsVector()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* vec = data_type->AsVector();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
|
|
|
// The vector will have a number of components equal to the length of the
|
|
|
|
// swizzle. This assumes the validator will check that the swizzle
|
|
|
|
// is correct.
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
vec->type(), expr->member()->name()[0].size())));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "invalid type in member accessor");
|
2020-04-07 12:57:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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() ||
|
|
|
|
expr->IsShiftRight() || expr->IsShiftRightArith() || expr->IsAdd() ||
|
|
|
|
expr->IsSubtract() || expr->IsDivide() || expr->IsModulo()) {
|
|
|
|
expr->set_result_type(expr->lhs()->result_type());
|
|
|
|
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-04-09 18:52:06 +00:00
|
|
|
auto* bool_type =
|
2020-04-07 19:26:39 +00:00
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* param_type = expr->lhs()->result_type();
|
2020-04-07 19:26:39 +00:00
|
|
|
if (param_type->IsVector()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
bool_type, param_type->AsVector()->size())));
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (expr->IsMultiply()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* lhs_type = expr->lhs()->result_type();
|
|
|
|
auto* rhs_type = expr->rhs()->result_type();
|
2020-04-07 19:26:39 +00:00
|
|
|
|
|
|
|
// Note, the ordering here matters. The later checks depend on the prior
|
|
|
|
// checks having been done.
|
|
|
|
if (lhs_type->IsMatrix() && rhs_type->IsMatrix()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
|
|
|
|
lhs_type->AsMatrix()->type(), lhs_type->AsMatrix()->rows(),
|
|
|
|
rhs_type->AsMatrix()->columns())));
|
|
|
|
|
|
|
|
} else if (lhs_type->IsMatrix() && rhs_type->IsVector()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* mat = lhs_type->AsMatrix();
|
2020-04-07 19:27:21 +00:00
|
|
|
expr->set_result_type(ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(mat->type(), mat->rows())));
|
2020-04-07 19:26:39 +00:00
|
|
|
} else if (lhs_type->IsVector() && rhs_type->IsMatrix()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* mat = rhs_type->AsMatrix();
|
2020-04-07 19:26:39 +00:00
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
mat->type(), mat->columns())));
|
|
|
|
} else if (lhs_type->IsMatrix()) {
|
|
|
|
// matrix * scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (rhs_type->IsMatrix()) {
|
|
|
|
// scalar * matrix
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else if (lhs_type->IsVector() && rhs_type->IsVector()) {
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (lhs_type->IsVector()) {
|
|
|
|
// Vector * scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (rhs_type->IsVector()) {
|
|
|
|
// Scalar * vector
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else {
|
|
|
|
// Scalar * Scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:26:49 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryDerivative(
|
|
|
|
ast::UnaryDerivativeExpression* expr) {
|
|
|
|
// The result type must be the same as the type of the parameter.
|
|
|
|
if (!DetermineResultType(expr->param())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expr->set_result_type(expr->param()->result_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:00 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryMethod(ast::UnaryMethodExpression* expr) {
|
|
|
|
for (const auto& param : expr->params()) {
|
|
|
|
if (!DetermineResultType(param.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (expr->op()) {
|
|
|
|
case ast::UnaryMethod::kAny:
|
|
|
|
case ast::UnaryMethod::kAll: {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ast::UnaryMethod::kIsNan:
|
|
|
|
case ast::UnaryMethod::kIsInf:
|
|
|
|
case ast::UnaryMethod::kIsFinite:
|
|
|
|
case ast::UnaryMethod::kIsNormal: {
|
|
|
|
if (expr->params().empty()) {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "incorrect number of parameters");
|
2020-04-07 19:27:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* bool_type =
|
2020-04-07 19:27:00 +00:00
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* param_type = expr->params()[0]->result_type();
|
2020-04-07 19:27:00 +00:00
|
|
|
if (param_type->IsVector()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
bool_type, param_type->AsVector()->size())));
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ast::UnaryMethod::kDot: {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ast::UnaryMethod::kOuterProduct: {
|
|
|
|
if (expr->params().size() != 2) {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for outer product");
|
2020-04-07 19:27:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* param0_type = expr->params()[0]->result_type();
|
|
|
|
auto* param1_type = expr->params()[1]->result_type();
|
2020-04-07 19:27:00 +00:00
|
|
|
if (!param0_type->IsVector() || !param1_type->IsVector()) {
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "invalid parameter type for outer product");
|
2020-04-07 19:27:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()),
|
|
|
|
param0_type->AsVector()->size(),
|
|
|
|
param1_type->AsVector()->size())));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
expr->set_result_type(expr->expr()->result_type());
|
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|