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-02-17 20:13:34 +00:00
|
|
|
#include "src/diagnostic/formatter.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 23:55:56 +00:00
|
|
|
#include "src/semantic/member_accessor_expression.h"
|
2021-02-16 18:45:45 +00:00
|
|
|
#include "src/semantic/statement.h"
|
2021-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-02-08 19:53:42 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using IntrinsicType = tint::semantic::IntrinsicType;
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
// Helper class that temporarily assigns a value to a reference for the scope of
|
|
|
|
// the object. Once the ScopedAssignment is destructed, the original value is
|
|
|
|
// restored.
|
|
|
|
template <typename T>
|
|
|
|
class ScopedAssignment {
|
|
|
|
public:
|
|
|
|
ScopedAssignment(T& ref, T val) : ref_(ref) {
|
|
|
|
old_value_ = ref;
|
|
|
|
ref = val;
|
|
|
|
}
|
|
|
|
~ScopedAssignment() { ref_ = old_value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T& ref_;
|
|
|
|
T old_value_;
|
|
|
|
};
|
|
|
|
|
2021-02-08 19:53:42 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-02-08 22:42:54 +00:00
|
|
|
TypeDeterminer::TypeDeterminer(ProgramBuilder* builder)
|
|
|
|
: builder_(builder), intrinsic_table_(IntrinsicTable::Create()) {}
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
TypeDeterminer::~TypeDeterminer() = default;
|
|
|
|
|
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-17 13:10:49 +00:00
|
|
|
current_function_->referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
if (local) {
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->local_referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2021-02-17 13:10:49 +00:00
|
|
|
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-16 18:45:45 +00:00
|
|
|
auto* func_info = function_infos_.Create<FunctionInfo>(func);
|
|
|
|
|
|
|
|
ScopedAssignment<FunctionInfo*> sa(current_function_, func_info);
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
variable_stack_.push_scope();
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* param : func->params()) {
|
2021-02-03 17:51:09 +00:00
|
|
|
variable_stack_.set(param->symbol(), CreateVariableInfo(param));
|
2020-06-22 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2021-02-24 22:11:34 +00:00
|
|
|
// Register the function information _after_ processing the statements. This
|
|
|
|
// allows us to catch a function calling itself when determining the call
|
|
|
|
// information as this function doesn't exist until it's finished.
|
|
|
|
symbol_to_function_[func->symbol()] = func_info;
|
|
|
|
function_to_info_.emplace(func, func_info);
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-02-26 18:25:56 +00:00
|
|
|
if (auto* decl = stmt->As<ast::VariableDeclStatement>()) {
|
|
|
|
if (!ValidateVariableDeclStatement(decl)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
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) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("function variable has a non-function storage class",
|
|
|
|
stmt->source());
|
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) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto* sem_statement = builder_->create<semantic::Statement>(stmt);
|
|
|
|
|
|
|
|
ScopedAssignment<semantic::Statement*> sa(current_statement_, sem_statement);
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = stmt->As<ast::AssignmentStatement>()) {
|
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-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"unknown statement type for type determination: " + builder_->str(stmt),
|
|
|
|
stmt->source());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("unknown expression for type determination",
|
|
|
|
expr->source());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("invalid parent type (" + parent_type->type_name() +
|
|
|
|
") in array accessor",
|
|
|
|
expr->source());
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* arr = parent_type->As<type::Array>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
if (!arr->type()->is_scalar()) {
|
|
|
|
// If we extract a non-scalar from an array then we also get a pointer. We
|
|
|
|
// 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->params())) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
2020-04-20 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// The expression has to be an identifier as you can't store function pointers
|
|
|
|
// but, if it isn't we'll just use the normal result determination to be on
|
|
|
|
// the safe side.
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* ident = call->func()->As<ast::IdentifierExpression>();
|
|
|
|
if (!ident) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("call target is not an identifier", call->source());
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol());
|
|
|
|
|
2021-02-08 22:31:44 +00:00
|
|
|
auto intrinsic_type = MatchIntrinsicType(name);
|
|
|
|
if (intrinsic_type != IntrinsicType::kNone) {
|
|
|
|
if (!DetermineIntrinsicCall(call, intrinsic_type)) {
|
2021-02-03 21:02:25 +00:00
|
|
|
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()) {
|
2021-02-24 22:11:34 +00:00
|
|
|
if (current_function_->declaration->symbol() == ident->symbol()) {
|
|
|
|
diagnostics_.add_error("recursion is not permitted. '" + name +
|
|
|
|
"' attempted to call itself.",
|
|
|
|
call->source());
|
|
|
|
} else {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0006: unable to find called function: " + name,
|
|
|
|
call->source());
|
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* callee_func = callee_func_it->second;
|
2020-07-14 20:37:28 +00:00
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
for (auto* var : callee_func->referenced_module_vars) {
|
|
|
|
set_referenced_from_function_if_needed(var, false);
|
2020-07-14 19:45:47 +00:00
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
|
|
|
auto iter = symbol_to_function_.find(ident->symbol());
|
|
|
|
if (iter == symbol_to_function_.end()) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0005: function must be declared before use: '" + name + "'",
|
|
|
|
call->source());
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-18 02:10:03 +00:00
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* function = iter->second;
|
2021-02-16 18:45:45 +00:00
|
|
|
function_calls_.emplace(call,
|
|
|
|
FunctionCallInfo{function, current_statement_});
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(call, function->declaration->return_type());
|
2020-08-18 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:42:54 +00:00
|
|
|
bool TypeDeterminer::DetermineIntrinsicCall(
|
|
|
|
ast::CallExpression* call,
|
|
|
|
semantic::IntrinsicType intrinsic_type) {
|
2021-02-08 22:31:44 +00:00
|
|
|
std::vector<type::Type*> arg_tys;
|
|
|
|
arg_tys.reserve(call->params().size());
|
|
|
|
for (auto* expr : call->params()) {
|
|
|
|
arg_tys.emplace_back(TypeOf(expr));
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
auto result = intrinsic_table_->Lookup(*builder_, intrinsic_type, arg_tys,
|
|
|
|
call->source());
|
2021-02-08 22:42:54 +00:00
|
|
|
if (!result.intrinsic) {
|
|
|
|
// Intrinsic lookup failed.
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add(result.diagnostics);
|
2021-02-08 22:42:54 +00:00
|
|
|
|
|
|
|
// TODO(bclayton): https://crbug.com/tint/487
|
|
|
|
// The Validator expects intrinsic signature mismatches to still produce
|
|
|
|
// type information. The rules for what the Validator expects are rather
|
|
|
|
// bespoke. Try to match what the Validator expects. As the Validator's
|
|
|
|
// checks on intrinsics is now almost entirely covered by the
|
|
|
|
// IntrinsicTable, we should remove the Validator checks on intrinsic
|
|
|
|
// signatures and remove these hacks.
|
2021-02-09 15:37:44 +00:00
|
|
|
semantic::ParameterList parameters;
|
2021-02-08 22:42:54 +00:00
|
|
|
parameters.reserve(arg_tys.size());
|
|
|
|
for (auto* arg : arg_tys) {
|
|
|
|
parameters.emplace_back(semantic::Parameter{arg});
|
2020-09-22 19:42:13 +00:00
|
|
|
}
|
2021-02-08 22:42:54 +00:00
|
|
|
type::Type* ret_ty = nullptr;
|
|
|
|
switch (intrinsic_type) {
|
|
|
|
case IntrinsicType::kCross:
|
|
|
|
ret_ty = builder_->ty.vec3<ProgramBuilder::f32>();
|
2020-09-22 19:42:13 +00:00
|
|
|
break;
|
2021-02-08 22:42:54 +00:00
|
|
|
case IntrinsicType::kDeterminant:
|
|
|
|
ret_ty = builder_->create<type::F32>();
|
2020-09-22 19:42:13 +00:00
|
|
|
break;
|
2021-02-08 22:42:54 +00:00
|
|
|
case IntrinsicType::kArrayLength:
|
|
|
|
ret_ty = builder_->create<type::U32>();
|
2021-02-01 16:03:03 +00:00
|
|
|
break;
|
2021-02-01 15:33:13 +00:00
|
|
|
default:
|
2021-02-08 22:42:54 +00:00
|
|
|
ret_ty = arg_tys.empty() ? builder_->ty.void_() : arg_tys[0];
|
|
|
|
break;
|
2020-07-21 17:44:44 +00:00
|
|
|
}
|
2021-02-08 22:42:54 +00:00
|
|
|
auto* intrinsic = builder_->create<semantic::Intrinsic>(intrinsic_type,
|
|
|
|
ret_ty, parameters);
|
2021-02-16 21:15:01 +00:00
|
|
|
builder_->Sem().Add(call, builder_->create<semantic::Call>(
|
|
|
|
call, intrinsic, current_statement_));
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(call, ret_ty);
|
2021-02-08 22:42:54 +00:00
|
|
|
return false;
|
2020-07-21 17:44:44 +00:00
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
builder_->Sem().Add(call, builder_->create<semantic::Call>(
|
2021-02-16 21:15:01 +00:00
|
|
|
call, result.intrinsic, current_statement_));
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(call, result.intrinsic->ReturnType());
|
2020-09-22 19:42:13 +00:00
|
|
|
return true;
|
2020-06-01 13:43:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-16 21:15:01 +00:00
|
|
|
var->users.push_back(expr);
|
2020-12-08 21:07:24 +00:00
|
|
|
set_referenced_from_function_if_needed(var, true);
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-11 22:02:42 +00:00
|
|
|
auto iter = symbol_to_function_.find(symbol);
|
2021-01-11 16:24:32 +00:00
|
|
|
if (iter != symbol_to_function_.end()) {
|
2021-02-24 13:31:22 +00:00
|
|
|
diagnostics_.add_error("missing '(' for function call",
|
|
|
|
expr->source().End());
|
|
|
|
return false;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
std::string name = builder_->Symbols().NameFor(symbol);
|
2021-02-08 22:31:44 +00:00
|
|
|
if (MatchIntrinsicType(name) != IntrinsicType::kNone) {
|
2021-02-24 13:31:22 +00:00
|
|
|
diagnostics_.add_error("missing '(' for intrinsic call",
|
|
|
|
expr->source().End());
|
|
|
|
return false;
|
2020-10-14 18:26:31 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0006: identifier must be declared before use: " + name,
|
|
|
|
expr->source());
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 22:31:44 +00:00
|
|
|
IntrinsicType TypeDeterminer::MatchIntrinsicType(const std::string& name) {
|
2021-01-11 16:24:32 +00:00
|
|
|
if (name == "abs") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAbs;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "acos") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAcos;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "all") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAll;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "any") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAny;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "arrayLength") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kArrayLength;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "asin") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAsin;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "atan") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAtan;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "atan2") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kAtan2;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "ceil") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kCeil;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "clamp") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kClamp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "cos") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kCos;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "cosh") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kCosh;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "countOneBits") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kCountOneBits;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "cross") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kCross;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "determinant") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDeterminant;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "distance") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDistance;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dot") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDot;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdx") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDpdx;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdxCoarse") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDpdxCoarse;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdxFine") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDpdxFine;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdy") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDpdy;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdyCoarse") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDpdyCoarse;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "dpdyFine") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kDpdyFine;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "exp") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kExp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "exp2") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kExp2;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "faceForward") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFaceForward;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "floor") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFloor;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fma") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFma;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fract") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFract;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "frexp") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFrexp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fwidth") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFwidth;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fwidthCoarse") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFwidthCoarse;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "fwidthFine") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kFwidthFine;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "inverseSqrt") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kInverseSqrt;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isFinite") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kIsFinite;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isInf") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kIsInf;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isNan") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kIsNan;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "isNormal") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kIsNormal;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "ldexp") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kLdexp;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "length") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kLength;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "log") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kLog;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "log2") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kLog2;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "max") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kMax;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "min") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kMin;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "mix") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kMix;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "modf") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kModf;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "normalize") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kNormalize;
|
2021-02-04 16:17:49 +00:00
|
|
|
} else if (name == "pack4x8snorm") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kPack4x8Snorm;
|
2021-02-04 16:17:49 +00:00
|
|
|
} else if (name == "pack4x8unorm") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kPack4x8Unorm;
|
2021-02-04 16:17:49 +00:00
|
|
|
} else if (name == "pack2x16snorm") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kPack2x16Snorm;
|
2021-02-04 16:17:49 +00:00
|
|
|
} else if (name == "pack2x16unorm") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kPack2x16Unorm;
|
2021-02-04 16:17:49 +00:00
|
|
|
} else if (name == "pack2x16float") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kPack2x16Float;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "pow") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kPow;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "reflect") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kReflect;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "reverseBits") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kReverseBits;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "round") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kRound;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "select") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kSelect;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sign") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kSign;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sin") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kSin;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sinh") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kSinh;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "smoothStep") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kSmoothStep;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "sqrt") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kSqrt;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "step") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kStep;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "tan") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTan;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "tanh") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTanh;
|
2021-01-11 21:07:32 +00:00
|
|
|
} else if (name == "textureDimensions") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureDimensions;
|
2021-01-14 16:50:07 +00:00
|
|
|
} else if (name == "textureNumLayers") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureNumLayers;
|
2021-01-14 18:06:57 +00:00
|
|
|
} else if (name == "textureNumLevels") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureNumLevels;
|
2021-01-14 18:11:17 +00:00
|
|
|
} else if (name == "textureNumSamples") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureNumSamples;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureLoad") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureLoad;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureStore") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureStore;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSample") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureSample;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleBias") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureSampleBias;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleCompare") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureSampleCompare;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleGrad") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureSampleGrad;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "textureSampleLevel") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTextureSampleLevel;
|
2021-01-11 16:24:32 +00:00
|
|
|
} else if (name == "trunc") {
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::kTrunc;
|
2021-02-09 21:23:00 +00:00
|
|
|
} else if (name == "unpack4x8snorm") {
|
|
|
|
return IntrinsicType::kUnpack4x8Snorm;
|
|
|
|
} else if (name == "unpack4x8unorm") {
|
|
|
|
return IntrinsicType::kUnpack4x8Unorm;
|
|
|
|
} else if (name == "unpack2x16snorm") {
|
|
|
|
return IntrinsicType::kUnpack2x16Snorm;
|
|
|
|
} else if (name == "unpack2x16unorm") {
|
|
|
|
return IntrinsicType::kUnpack2x16Unorm;
|
|
|
|
} else if (name == "unpack2x16float") {
|
|
|
|
return IntrinsicType::kUnpack2x16Float;
|
2020-09-22 19:42:13 +00:00
|
|
|
}
|
2021-02-08 19:53:42 +00:00
|
|
|
return IntrinsicType::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;
|
2021-02-24 14:15:02 +00:00
|
|
|
std::vector<uint32_t> swizzle;
|
2021-02-03 23:55:56 +00:00
|
|
|
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ty = data_type->As<type::Struct>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* strct = ty->impl();
|
2021-01-11 16:24:32 +00:00
|
|
|
auto symbol = expr->member()->symbol();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* member : strct->members()) {
|
2021-01-11 16:24:32 +00:00
|
|
|
if (member->symbol() == symbol) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = member->type();
|
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"struct member " + builder_->Symbols().NameFor(symbol) + " not found",
|
|
|
|
expr->source());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* vec = data_type->As<type::Vector>()) {
|
2021-02-24 14:15:02 +00:00
|
|
|
std::string str = builder_->Symbols().NameFor(expr->member()->symbol());
|
|
|
|
auto size = str.size();
|
|
|
|
swizzle.reserve(str.size());
|
|
|
|
|
|
|
|
for (auto c : str) {
|
|
|
|
switch (c) {
|
|
|
|
case 'x':
|
|
|
|
case 'r':
|
|
|
|
swizzle.emplace_back(0);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
case 'g':
|
|
|
|
swizzle.emplace_back(1);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
case 'b':
|
|
|
|
swizzle.emplace_back(2);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'a':
|
|
|
|
swizzle.emplace_back(3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"invalid vector swizzle character",
|
|
|
|
expr->member()->source().Begin() + swizzle.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < 1 || size > 4) {
|
|
|
|
diagnostics_.add_error("invalid vector swizzle size",
|
|
|
|
expr->member()->source());
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 16:24:32 +00:00
|
|
|
|
2020-04-21 13:05:34 +00:00
|
|
|
if (size == 1) {
|
|
|
|
// A single element swizzle is just the type of the vector.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = vec->type();
|
2020-05-01 16:17:03 +00:00
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
2021-02-24 14:15:02 +00:00
|
|
|
// The vector will have a number of components equal to the length of
|
|
|
|
// the swizzle. This assumes the validator will check that the swizzle
|
2020-04-21 13:05:34 +00:00
|
|
|
// is correct.
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Vector>(vec->type(),
|
2021-01-26 16:57:10 +00:00
|
|
|
static_cast<uint32_t>(size));
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
2021-02-24 14:15:02 +00:00
|
|
|
"invalid use of member accessor on a non-vector/non-struct " +
|
2021-02-17 20:13:34 +00:00
|
|
|
data_type->type_name(),
|
|
|
|
expr->source());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
builder_->Sem().Add(expr,
|
|
|
|
builder_->create<semantic::MemberAccessorExpression>(
|
2021-02-24 14:15:02 +00:00
|
|
|
expr, ret, current_statement_, std::move(swizzle)));
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("Unknown binary expression", expr->source());
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-26 18:25:56 +00:00
|
|
|
bool TypeDeterminer::ValidateVariableDeclStatement(
|
|
|
|
const ast::VariableDeclStatement* stmt) {
|
|
|
|
auto* ctor = stmt->variable()->constructor();
|
|
|
|
if (!ctor) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* sce = ctor->As<ast::ScalarConstructorExpression>()) {
|
|
|
|
auto* lhs_type = stmt->variable()->type()->UnwrapAliasIfNeeded();
|
|
|
|
auto* rhs_type = sce->literal()->type()->UnwrapAliasIfNeeded();
|
|
|
|
|
|
|
|
if (lhs_type != rhs_type) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"constructor expression type does not match variable type",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-02-09 17:38:05 +00:00
|
|
|
type::Type* TypeDeterminer::TypeOf(ast::Expression* expr) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto it = expr_info_.find(expr);
|
|
|
|
if (it != expr_info_.end()) {
|
|
|
|
return it->second.type;
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TypeDeterminer::SetType(ast::Expression* expr, type::Type* type) {
|
2021-02-16 18:45:45 +00:00
|
|
|
assert(expr_info_.count(expr) == 0);
|
|
|
|
expr_info_.emplace(expr, ExpressionInfo{type, current_statement_});
|
2021-01-29 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 17:51:09 +00:00
|
|
|
void TypeDeterminer::CreateSemanticNodes() const {
|
|
|
|
auto& sem = builder_->Sem();
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Variables
|
2021-02-03 17:51:09 +00:00
|
|
|
for (auto it : variable_to_info_) {
|
|
|
|
auto* var = it.first;
|
|
|
|
auto* info = it.second;
|
2021-02-16 21:15:01 +00:00
|
|
|
std::vector<const semantic::Expression*> users;
|
|
|
|
for (auto* user : info->users) {
|
|
|
|
// Create semantic node for the identifier expression if necessary
|
|
|
|
auto* sem_expr = sem.Get(user);
|
|
|
|
if (sem_expr == nullptr) {
|
|
|
|
auto* type = expr_info_.at(user).type;
|
|
|
|
auto* stmt = expr_info_.at(user).statement;
|
|
|
|
sem_expr = builder_->create<semantic::Expression>(user, type, stmt);
|
|
|
|
sem.Add(user, sem_expr);
|
|
|
|
}
|
|
|
|
users.push_back(sem_expr);
|
|
|
|
}
|
|
|
|
sem.Add(var, builder_->create<semantic::Variable>(var, info->storage_class,
|
|
|
|
std::move(users)));
|
2021-02-03 17:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) {
|
|
|
|
std::vector<const semantic::Variable*> out;
|
|
|
|
out.reserve(in.size());
|
|
|
|
for (auto* info : in) {
|
|
|
|
out.emplace_back(sem.Get(info->declaration));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Functions
|
2021-02-08 22:31:44 +00:00
|
|
|
std::unordered_map<FunctionInfo*, semantic::Function*> func_info_to_sem_func;
|
2021-02-03 16:43:20 +00:00
|
|
|
for (auto it : function_to_info_) {
|
|
|
|
auto* func = it.first;
|
|
|
|
auto* info = it.second;
|
2021-02-08 22:31:44 +00:00
|
|
|
auto* sem_func = builder_->create<semantic::Function>(
|
|
|
|
info->declaration, remap_vars(info->referenced_module_vars),
|
|
|
|
remap_vars(info->local_referenced_module_vars),
|
|
|
|
info->ancestor_entry_points);
|
|
|
|
func_info_to_sem_func.emplace(info, sem_func);
|
|
|
|
sem.Add(func, sem_func);
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::CallExpressions
|
2021-02-08 22:31:44 +00:00
|
|
|
for (auto it : function_calls_) {
|
|
|
|
auto* call = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto info = it.second;
|
|
|
|
auto* sem_func = func_info_to_sem_func.at(info.function);
|
2021-02-16 21:15:01 +00:00
|
|
|
sem.Add(call,
|
|
|
|
builder_->create<semantic::Call>(call, sem_func, info.statement));
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create semantic nodes for all remaining expression types
|
2021-02-16 18:45:45 +00:00
|
|
|
for (auto it : expr_info_) {
|
2021-02-09 17:38:05 +00:00
|
|
|
auto* expr = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto& info = it.second;
|
2021-02-09 17:38:05 +00:00
|
|
|
if (sem.Get(expr)) {
|
|
|
|
// Expression has already been assigned a semantic node
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-16 21:15:01 +00:00
|
|
|
sem.Add(expr, builder_->create<semantic::Expression>(expr, info.type,
|
|
|
|
info.statement));
|
2021-02-03 16:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-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
|