2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
#include "src/resolver/resolver.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-01 20:01:39 +00:00
|
|
|
#include <algorithm>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
2020-04-07 12:57:42 +00:00
|
|
|
|
2020-04-07 12:47:23 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2020-09-22 22:07:13 +00:00
|
|
|
#include "src/ast/bitcast_expression.h"
|
2020-04-07 12:54:10 +00:00
|
|
|
#include "src/ast/break_statement.h"
|
2020-07-21 13:42:13 +00:00
|
|
|
#include "src/ast/call_statement.h"
|
2020-04-07 12:54:29 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/discard_statement.h"
|
|
|
|
#include "src/ast/fallthrough_statement.h"
|
2020-04-07 12:55:25 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2020-04-07 12:55:51 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2020-04-07 12:56:24 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2020-04-07 12:56:45 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2020-04-07 19:27:21 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2020-04-07 12:57:12 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2021-02-03 21:02:25 +00:00
|
|
|
#include "src/semantic/call.h"
|
2021-02-03 16:43:20 +00:00
|
|
|
#include "src/semantic/function.h"
|
2021-02-03 23:55:56 +00:00
|
|
|
#include "src/semantic/member_accessor_expression.h"
|
2021-02-16 18:45:45 +00:00
|
|
|
#include "src/semantic/statement.h"
|
2021-02-03 17:51:09 +00:00
|
|
|
#include "src/semantic/variable.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
2021-03-09 15:08:48 +00:00
|
|
|
namespace resolver {
|
2021-02-08 19:53:42 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
using IntrinsicType = tint::semantic::IntrinsicType;
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
// Helper class that temporarily assigns a value to a reference for the scope of
|
|
|
|
// the object. Once the ScopedAssignment is destructed, the original value is
|
|
|
|
// restored.
|
|
|
|
template <typename T>
|
|
|
|
class ScopedAssignment {
|
|
|
|
public:
|
|
|
|
ScopedAssignment(T& ref, T val) : ref_(ref) {
|
|
|
|
old_value_ = ref;
|
|
|
|
ref = val;
|
|
|
|
}
|
|
|
|
~ScopedAssignment() { ref_ = old_value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T& ref_;
|
|
|
|
T old_value_;
|
|
|
|
};
|
|
|
|
|
2021-02-08 19:53:42 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::Resolver(ProgramBuilder* builder)
|
2021-02-08 22:42:54 +00:00
|
|
|
: builder_(builder), intrinsic_table_(IntrinsicTable::Create()) {}
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::~Resolver() = default;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::BlockInfo::BlockInfo(Resolver::BlockInfo::Type ty,
|
2021-03-09 15:06:37 +00:00
|
|
|
Resolver::BlockInfo* p)
|
|
|
|
: type(ty), parent(p) {}
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::BlockInfo::~BlockInfo() = default;
|
2021-03-09 10:26:57 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::set_referenced_from_function_if_needed(VariableInfo* var,
|
|
|
|
bool local) {
|
2020-06-22 20:52:24 +00:00
|
|
|
if (current_function_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-03 17:51:09 +00:00
|
|
|
if (var->storage_class == ast::StorageClass::kNone ||
|
|
|
|
var->storage_class == ast::StorageClass::kFunction) {
|
2020-06-22 20:52:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
if (local) {
|
2021-02-17 13:10:49 +00:00
|
|
|
current_function_->local_referenced_module_vars.add(var);
|
2020-12-08 21:07:24 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Resolve() {
|
|
|
|
bool result = ResolveInternal();
|
2021-02-03 17:51:09 +00:00
|
|
|
|
|
|
|
// Even if resolving failed, create all the semantic nodes for information we
|
|
|
|
// did generate.
|
|
|
|
CreateSemanticNodes();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::ResolveInternal() {
|
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()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(var->constructor())) {
|
2020-06-15 20:55:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Functions(builder_->AST().Functions())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-14 19:45:47 +00:00
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Functions(const ast::FunctionList& funcs) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* func : funcs) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Function(func)) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Function(ast::Function* func) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto* func_info = function_infos_.Create<FunctionInfo>(func);
|
|
|
|
|
|
|
|
ScopedAssignment<FunctionInfo*> sa(current_function_, func_info);
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
variable_stack_.push_scope();
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* param : func->params()) {
|
2021-02-03 17:51:09 +00:00
|
|
|
variable_stack_.set(param->symbol(), CreateVariableInfo(param));
|
2020-06-22 20:18:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!BlockStatement(func->body())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
variable_stack_.pop_scope();
|
|
|
|
|
2021-02-24 22:11:34 +00:00
|
|
|
// Register the function information _after_ processing the statements. This
|
|
|
|
// allows us to catch a function calling itself when determining the call
|
|
|
|
// information as this function doesn't exist until it's finished.
|
|
|
|
symbol_to_function_[func->symbol()] = func_info;
|
|
|
|
function_to_info_.emplace(func, func_info);
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::BlockStatement(const ast::BlockStatement* stmt) {
|
2021-03-09 15:06:37 +00:00
|
|
|
return BlockScope(BlockInfo::Type::kGeneric,
|
|
|
|
[&] { return Statements(stmt->list()); });
|
2021-03-09 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Statements(const ast::StatementList& stmts) {
|
2021-03-09 10:26:57 +00:00
|
|
|
for (auto* stmt : stmts) {
|
2021-02-26 18:25:56 +00:00
|
|
|
if (auto* decl = stmt->As<ast::VariableDeclStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!VariableDeclStatement(decl)) {
|
2021-02-26 18:25:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!VariableStorageClass(stmt)) {
|
2020-07-27 15:25:00 +00:00
|
|
|
return false;
|
2020-04-08 19:58:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Statement(stmt)) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::VariableStorageClass(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;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Statement(ast::Statement* stmt) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto* sem_statement = builder_->create<semantic::Statement>(stmt);
|
|
|
|
|
|
|
|
ScopedAssignment<semantic::Statement*> sa(current_statement_, sem_statement);
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = stmt->As<ast::AssignmentStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(a->lhs()) && Expression(a->rhs());
|
2020-04-07 12:47:23 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = stmt->As<ast::BlockStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return BlockStatement(b);
|
2020-07-27 15:25:00 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::BreakStatement>()) {
|
2021-03-09 15:06:37 +00:00
|
|
|
if (!current_block_->FindFirstParent(BlockInfo::Type::kLoop) &&
|
|
|
|
!current_block_->FindFirstParent(BlockInfo::Type::kSwitchCase)) {
|
|
|
|
diagnostics_.add_error("break statement must be in a loop or switch case",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:10 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = stmt->As<ast::CallStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(c->expr());
|
2020-07-21 13:42:13 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = stmt->As<ast::CaseStatement>()) {
|
2021-03-09 15:06:37 +00:00
|
|
|
return CaseStatement(c);
|
2020-04-07 12:54:20 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::ContinueStatement>()) {
|
2021-03-09 10:26:57 +00:00
|
|
|
// Set if we've hit the first continue statement in our parent loop
|
|
|
|
if (auto* loop_block =
|
2021-03-09 15:06:37 +00:00
|
|
|
current_block_->FindFirstParent(BlockInfo::Type::kLoop)) {
|
2021-03-09 10:26:57 +00:00
|
|
|
if (loop_block->first_continue == size_t(~0)) {
|
|
|
|
loop_block->first_continue = loop_block->decls.size();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
diagnostics_.add_error("continue statement must be in a loop",
|
|
|
|
stmt->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:29 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::DiscardStatement>()) {
|
2020-07-25 14:33:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* e = stmt->As<ast::ElseStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(e->condition()) && BlockStatement(e->body());
|
2020-04-07 12:54:37 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (stmt->Is<ast::FallthroughStatement>()) {
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* i = stmt->As<ast::IfStatement>()) {
|
2021-03-09 15:17:28 +00:00
|
|
|
return IfStatement(i);
|
2020-04-07 12:55:25 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* l = stmt->As<ast::LoopStatement>()) {
|
2021-03-09 10:26:57 +00:00
|
|
|
// We don't call DetermineBlockStatement on the body and continuing block as
|
|
|
|
// these would make their BlockInfo siblings as in the AST, but we want the
|
|
|
|
// body BlockInfo to parent the continuing BlockInfo for semantics and
|
|
|
|
// validation. Also, we need to set their types differently.
|
2021-03-09 15:06:37 +00:00
|
|
|
return BlockScope(BlockInfo::Type::kLoop, [&] {
|
|
|
|
if (!Statements(l->body()->list())) {
|
2021-03-09 10:26:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 15:06:37 +00:00
|
|
|
|
|
|
|
if (l->has_continuing()) {
|
|
|
|
if (!BlockScope(BlockInfo::Type::kLoopContinuing,
|
|
|
|
[&] { return Statements(l->continuing()->list()); })) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2020-04-07 12:55:51 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* r = stmt->As<ast::ReturnStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(r->value());
|
2020-04-07 12:56:24 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* s = stmt->As<ast::SwitchStatement>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(s->condition())) {
|
2020-04-07 12:56:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* case_stmt : s->body()) {
|
2021-03-09 15:06:37 +00:00
|
|
|
if (!CaseStatement(case_stmt)) {
|
2020-04-07 12:56:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* v = stmt->As<ast::VariableDeclStatement>()) {
|
2021-02-03 17:51:09 +00:00
|
|
|
variable_stack_.set(v->variable()->symbol(),
|
|
|
|
variable_to_info_.at(v->variable()));
|
2021-03-09 10:26:57 +00:00
|
|
|
current_block_->decls.push_back(v->variable());
|
2021-03-09 10:54:37 +00:00
|
|
|
return Expression(v->variable()->constructor());
|
2020-04-07 12:57:12 +00:00
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"unknown statement type for type determination: " + builder_->str(stmt),
|
|
|
|
stmt->source());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
bool Resolver::CaseStatement(ast::CaseStatement* stmt) {
|
|
|
|
return BlockScope(BlockInfo::Type::kSwitchCase,
|
|
|
|
[&] { return Statements(stmt->body()->list()); });
|
2021-03-09 15:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Resolver::IfStatement(ast::IfStatement* stmt) {
|
|
|
|
if (!Expression(stmt->condition())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* cond_type = TypeOf(stmt->condition())->UnwrapAll();
|
|
|
|
if (cond_type != builder_->ty.bool_()) {
|
|
|
|
diagnostics_.add_error("if statement condition must be bool, got " +
|
|
|
|
cond_type->FriendlyName(builder_->Symbols()),
|
|
|
|
stmt->condition()->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BlockStatement(stmt->body())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto* else_stmt : stmt->else_statements()) {
|
|
|
|
if (!Statement(else_stmt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2021-03-09 15:06:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Expressions(const ast::ExpressionList& list) {
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* expr : list) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(expr)) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Expression(ast::Expression* expr) {
|
2020-04-06 21:07:41 +00:00
|
|
|
// This is blindly called above, so in some cases the expression won't exist.
|
|
|
|
if (!expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
if (TypeOf(expr)) {
|
|
|
|
return true; // Already resolved
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* a = expr->As<ast::ArrayAccessorExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return ArrayAccessor(a);
|
2020-04-07 12:57:42 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = expr->As<ast::BinaryExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Binary(b);
|
2020-04-07 19:27:41 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* b = expr->As<ast::BitcastExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Bitcast(b);
|
2020-09-22 22:07:13 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = expr->As<ast::CallExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Call(c);
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* c = expr->As<ast::ConstructorExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Constructor(c);
|
2020-04-07 12:46:30 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* i = expr->As<ast::IdentifierExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return Identifier(i);
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* m = expr->As<ast::MemberAccessorExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return MemberAccessor(m);
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
2020-11-30 23:30:58 +00:00
|
|
|
if (auto* u = expr->As<ast::UnaryOpExpression>()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
return UnaryOp(u);
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("unknown expression for type determination",
|
|
|
|
expr->source());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::ArrayAccessor(ast::ArrayAccessorExpression* expr) {
|
|
|
|
if (!Expression(expr->array())) {
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(expr->idx_expr())) {
|
2020-05-01 16:17:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* res = TypeOf(expr->array());
|
2020-10-28 20:32:22 +00:00
|
|
|
auto* parent_type = res->UnwrapAll();
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Type* ret = nullptr;
|
|
|
|
if (auto* arr = parent_type->As<type::Array>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
ret = arr->type();
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* vec = parent_type->As<type::Vector>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
ret = vec->type();
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* mat = parent_type->As<type::Matrix>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Vector>(mat->type(), mat->rows());
|
2020-04-07 12:57:42 +00:00
|
|
|
} else {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("invalid parent type (" + parent_type->type_name() +
|
|
|
|
") in array accessor",
|
|
|
|
expr->source());
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* arr = parent_type->As<type::Array>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
if (!arr->type()->is_scalar()) {
|
|
|
|
// If we extract a non-scalar from an array then we also get a pointer. We
|
2021-03-09 15:06:37 +00:00
|
|
|
// will generate a Function storage class variable to store this into.
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ast::StorageClass::kFunction);
|
2020-12-01 21:07:27 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Bitcast(ast::BitcastExpression* expr) {
|
|
|
|
if (!Expression(expr->expr())) {
|
2020-06-18 18:02:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, expr->type());
|
2020-04-07 12:57:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Call(ast::CallExpression* call) {
|
|
|
|
if (!Expressions(call->params())) {
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
2020-04-20 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// The expression has to be an identifier as you can't store function pointers
|
|
|
|
// but, if it isn't we'll just use the normal result determination to be on
|
|
|
|
// the safe side.
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* ident = call->func()->As<ast::IdentifierExpression>();
|
|
|
|
if (!ident) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("call target is not an identifier", call->source());
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto name = builder_->Symbols().NameFor(ident->symbol());
|
|
|
|
|
2021-03-04 23:00:46 +00:00
|
|
|
auto intrinsic_type = semantic::ParseIntrinsicType(name);
|
2021-02-08 22:31:44 +00:00
|
|
|
if (intrinsic_type != IntrinsicType::kNone) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!IntrinsicCall(call, intrinsic_type)) {
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (current_function_) {
|
|
|
|
auto callee_func_it = symbol_to_function_.find(ident->symbol());
|
|
|
|
if (callee_func_it == symbol_to_function_.end()) {
|
2021-02-24 22:11:34 +00:00
|
|
|
if (current_function_->declaration->symbol() == ident->symbol()) {
|
|
|
|
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-03-03 19:54:44 +00:00
|
|
|
// Note: Requires called functions to be resolved first.
|
|
|
|
// This is currently guaranteed as functions must be declared before use.
|
|
|
|
current_function_->transitive_calls.add(callee_func);
|
|
|
|
for (auto* transitive_call : callee_func->transitive_calls) {
|
|
|
|
current_function_->transitive_calls.add(transitive_call);
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
for (auto* var : callee_func->referenced_module_vars) {
|
|
|
|
set_referenced_from_function_if_needed(var, false);
|
2020-07-14 19:45:47 +00:00
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
|
|
|
auto iter = symbol_to_function_.find(ident->symbol());
|
|
|
|
if (iter == symbol_to_function_.end()) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0005: function must be declared before use: '" + name + "'",
|
|
|
|
call->source());
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-18 02:10:03 +00:00
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
auto* function = iter->second;
|
2021-02-16 18:45:45 +00:00
|
|
|
function_calls_.emplace(call,
|
|
|
|
FunctionCallInfo{function, current_statement_});
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(call, function->declaration->return_type());
|
2020-08-18 02:10:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::IntrinsicCall(ast::CallExpression* call,
|
|
|
|
semantic::IntrinsicType intrinsic_type) {
|
2021-02-08 22:31:44 +00:00
|
|
|
std::vector<type::Type*> arg_tys;
|
|
|
|
arg_tys.reserve(call->params().size());
|
|
|
|
for (auto* expr : call->params()) {
|
|
|
|
arg_tys.emplace_back(TypeOf(expr));
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
auto result = intrinsic_table_->Lookup(*builder_, intrinsic_type, arg_tys,
|
|
|
|
call->source());
|
2021-02-08 22:42:54 +00:00
|
|
|
if (!result.intrinsic) {
|
|
|
|
// Intrinsic lookup failed.
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add(result.diagnostics);
|
2021-02-08 22:42:54 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Constructor(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()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(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;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Identifier(ast::IdentifierExpression* expr) {
|
2021-01-11 15:10:19 +00:00
|
|
|
auto symbol = expr->symbol();
|
2021-02-03 17:51:09 +00:00
|
|
|
VariableInfo* var;
|
2021-01-11 15:10:19 +00:00
|
|
|
if (variable_stack_.get(symbol, &var)) {
|
2020-04-23 22:26:52 +00:00
|
|
|
// A constant is the type, but a variable is always a pointer so synthesize
|
|
|
|
// the pointer around the variable type.
|
2021-02-03 17:51:09 +00:00
|
|
|
if (var->declaration->is_const()) {
|
|
|
|
SetType(expr, var->declaration->type());
|
|
|
|
} else if (var->declaration->type()->Is<type::Pointer>()) {
|
|
|
|
SetType(expr, var->declaration->type());
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2021-02-03 17:51:09 +00:00
|
|
|
SetType(expr, builder_->create<type::Pointer>(var->declaration->type(),
|
|
|
|
var->storage_class));
|
2020-04-23 22:26:52 +00:00
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
|
2021-02-16 21:15:01 +00:00
|
|
|
var->users.push_back(expr);
|
2020-12-08 21:07:24 +00:00
|
|
|
set_referenced_from_function_if_needed(var, true);
|
2021-03-09 10:26:57 +00:00
|
|
|
|
|
|
|
// If identifier is part of a loop continuing block, make sure it doesn't
|
|
|
|
// refer to a variable that is bypassed by a continue statement in the
|
|
|
|
// loop's body block.
|
|
|
|
if (auto* continuing_block =
|
2021-03-09 15:06:37 +00:00
|
|
|
current_block_->FindFirstParent(BlockInfo::Type::kLoopContinuing)) {
|
2021-03-09 10:26:57 +00:00
|
|
|
auto* loop_block =
|
2021-03-09 15:06:37 +00:00
|
|
|
continuing_block->FindFirstParent(BlockInfo::Type::kLoop);
|
2021-03-09 10:26:57 +00:00
|
|
|
if (loop_block->first_continue != size_t(~0)) {
|
|
|
|
auto& decls = loop_block->decls;
|
|
|
|
// If our identifier is in loop_block->decls, make sure its index is
|
|
|
|
// less than first_continue
|
2021-03-09 10:54:37 +00:00
|
|
|
auto iter =
|
|
|
|
std::find_if(decls.begin(), decls.end(),
|
|
|
|
[&symbol](auto* v) { return v->symbol() == symbol; });
|
2021-03-09 10:26:57 +00:00
|
|
|
if (iter != decls.end()) {
|
|
|
|
auto var_decl_index =
|
|
|
|
static_cast<size_t>(std::distance(decls.begin(), iter));
|
|
|
|
if (var_decl_index >= loop_block->first_continue) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"continue statement bypasses declaration of '" +
|
|
|
|
builder_->Symbols().NameFor(symbol) +
|
|
|
|
"' in continuing block",
|
|
|
|
expr->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-11 22:02:42 +00:00
|
|
|
auto iter = symbol_to_function_.find(symbol);
|
2021-01-11 16:24:32 +00:00
|
|
|
if (iter != symbol_to_function_.end()) {
|
2021-02-24 13:31:22 +00:00
|
|
|
diagnostics_.add_error("missing '(' for function call",
|
|
|
|
expr->source().End());
|
|
|
|
return false;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:02:25 +00:00
|
|
|
std::string name = builder_->Symbols().NameFor(symbol);
|
2021-03-04 23:00:46 +00:00
|
|
|
if (semantic::ParseIntrinsicType(name) != IntrinsicType::kNone) {
|
2021-02-24 13:31:22 +00:00
|
|
|
diagnostics_.add_error("missing '(' for intrinsic call",
|
|
|
|
expr->source().End());
|
|
|
|
return false;
|
2020-10-14 18:26:31 +00:00
|
|
|
}
|
2021-02-03 21:02:25 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"v-0006: identifier must be declared before use: " + name,
|
|
|
|
expr->source());
|
2021-02-03 21:02:25 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) {
|
|
|
|
if (!Expression(expr->structure())) {
|
2020-04-07 16:41:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* res = TypeOf(expr->structure());
|
2020-10-28 20:32:22 +00:00
|
|
|
auto* data_type = res->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
|
2020-04-24 00:40:45 +00:00
|
|
|
|
2021-01-21 15:42:10 +00:00
|
|
|
type::Type* ret = nullptr;
|
2021-02-24 14:15:02 +00:00
|
|
|
std::vector<uint32_t> swizzle;
|
2021-02-03 23:55:56 +00:00
|
|
|
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ty = data_type->As<type::Struct>()) {
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* strct = ty->impl();
|
2021-01-11 16:24:32 +00:00
|
|
|
auto symbol = expr->member()->symbol();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* member : strct->members()) {
|
2021-01-11 16:24:32 +00:00
|
|
|
if (member->symbol() == symbol) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = member->type();
|
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
|
|
|
"struct member " + builder_->Symbols().NameFor(symbol) + " not found",
|
|
|
|
expr->source());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2021-01-21 15:42:10 +00:00
|
|
|
} else if (auto* vec = data_type->As<type::Vector>()) {
|
2021-02-24 14:15:02 +00:00
|
|
|
std::string str = builder_->Symbols().NameFor(expr->member()->symbol());
|
|
|
|
auto size = str.size();
|
|
|
|
swizzle.reserve(str.size());
|
|
|
|
|
|
|
|
for (auto c : str) {
|
|
|
|
switch (c) {
|
|
|
|
case 'x':
|
|
|
|
case 'r':
|
|
|
|
swizzle.emplace_back(0);
|
|
|
|
break;
|
|
|
|
case 'y':
|
|
|
|
case 'g':
|
|
|
|
swizzle.emplace_back(1);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
case 'b':
|
|
|
|
swizzle.emplace_back(2);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'a':
|
|
|
|
swizzle.emplace_back(3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"invalid vector swizzle character",
|
|
|
|
expr->member()->source().Begin() + swizzle.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < 1 || size > 4) {
|
|
|
|
diagnostics_.add_error("invalid vector swizzle size",
|
|
|
|
expr->member()->source());
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-11 16:24:32 +00:00
|
|
|
|
2021-03-01 20:01:39 +00:00
|
|
|
// All characters are valid, check if they're being mixed
|
|
|
|
auto is_rgba = [](char c) {
|
|
|
|
return c == 'r' || c == 'g' || c == 'b' || c == 'a';
|
|
|
|
};
|
|
|
|
auto is_xyzw = [](char c) {
|
|
|
|
return c == 'x' || c == 'y' || c == 'z' || c == 'w';
|
|
|
|
};
|
|
|
|
if (!std::all_of(str.begin(), str.end(), is_rgba) &&
|
|
|
|
!std::all_of(str.begin(), str.end(), is_xyzw)) {
|
|
|
|
diagnostics_.add_error(
|
|
|
|
"invalid mixing of vector swizzle characters rgba with xyzw",
|
|
|
|
expr->member()->source());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:05:34 +00:00
|
|
|
if (size == 1) {
|
|
|
|
// A single element swizzle is just the type of the vector.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = vec->type();
|
2020-05-01 16:17:03 +00:00
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* ptr = res->As<type::Pointer>()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Pointer>(ret, ptr->storage_class());
|
2020-05-01 16:17:03 +00:00
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
2021-02-24 14:15:02 +00:00
|
|
|
// The vector will have a number of components equal to the length of
|
|
|
|
// the swizzle. This assumes the validator will check that the swizzle
|
2020-04-21 13:05:34 +00:00
|
|
|
// is correct.
|
2021-01-26 16:57:10 +00:00
|
|
|
ret = builder_->create<type::Vector>(vec->type(),
|
2021-01-26 16:57:10 +00:00
|
|
|
static_cast<uint32_t>(size));
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error(
|
2021-02-24 14:15:02 +00:00
|
|
|
"invalid use of member accessor on a non-vector/non-struct " +
|
2021-02-17 20:13:34 +00:00
|
|
|
data_type->type_name(),
|
|
|
|
expr->source());
|
2020-04-23 22:26:52 +00:00
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:45:45 +00:00
|
|
|
builder_->Sem().Add(expr,
|
|
|
|
builder_->create<semantic::MemberAccessorExpression>(
|
2021-02-24 14:15:02 +00:00
|
|
|
expr, ret, current_statement_, std::move(swizzle)));
|
2021-02-09 17:38:05 +00:00
|
|
|
SetType(expr, ret);
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::Binary(ast::BinaryExpression* expr) {
|
|
|
|
if (!Expression(expr->lhs()) || !Expression(expr->rhs())) {
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result type matches first parameter type
|
|
|
|
if (expr->IsAnd() || expr->IsOr() || expr->IsXor() || expr->IsShiftLeft() ||
|
2020-06-03 16:11:44 +00:00
|
|
|
expr->IsShiftRight() || expr->IsAdd() || expr->IsSubtract() ||
|
|
|
|
expr->IsDivide() || expr->IsModulo()) {
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, TypeOf(expr->lhs())->UnwrapPtrIfNeeded());
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Result type is a scalar or vector of boolean type
|
|
|
|
if (expr->IsLogicalAnd() || expr->IsLogicalOr() || expr->IsEqual() ||
|
|
|
|
expr->IsNotEqual() || expr->IsLessThan() || expr->IsGreaterThan() ||
|
|
|
|
expr->IsLessThanEqual() || expr->IsGreaterThanEqual()) {
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* bool_type = builder_->create<type::Bool>();
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* param_type = TypeOf(expr->lhs())->UnwrapPtrIfNeeded();
|
|
|
|
type::Type* result_type = bool_type;
|
2021-01-21 15:42:10 +00:00
|
|
|
if (auto* vec = param_type->As<type::Vector>()) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = builder_->create<type::Vector>(bool_type, vec->size());
|
2020-04-07 19:26:39 +00:00
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, result_type);
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (expr->IsMultiply()) {
|
2021-01-29 16:43:41 +00:00
|
|
|
auto* lhs_type = TypeOf(expr->lhs())->UnwrapPtrIfNeeded();
|
|
|
|
auto* rhs_type = TypeOf(expr->rhs())->UnwrapPtrIfNeeded();
|
2020-04-07 19:26:39 +00:00
|
|
|
|
|
|
|
// Note, the ordering here matters. The later checks depend on the prior
|
|
|
|
// checks having been done.
|
2021-01-21 15:42:10 +00:00
|
|
|
auto* lhs_mat = lhs_type->As<type::Matrix>();
|
|
|
|
auto* rhs_mat = rhs_type->As<type::Matrix>();
|
|
|
|
auto* lhs_vec = lhs_type->As<type::Vector>();
|
|
|
|
auto* rhs_vec = rhs_type->As<type::Vector>();
|
2021-01-29 16:43:41 +00:00
|
|
|
type::Type* result_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
if (lhs_mat && rhs_mat) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = builder_->create<type::Matrix>(
|
|
|
|
lhs_mat->type(), lhs_mat->rows(), rhs_mat->columns());
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_mat && rhs_vec) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type =
|
|
|
|
builder_->create<type::Vector>(lhs_mat->type(), lhs_mat->rows());
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec && rhs_mat) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type =
|
|
|
|
builder_->create<type::Vector>(rhs_mat->type(), rhs_mat->columns());
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_mat) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// matrix * scalar
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (rhs_mat) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// scalar * matrix
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = rhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec && rhs_vec) {
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (lhs_vec) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// Vector * scalar
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-12-01 21:07:27 +00:00
|
|
|
} else if (rhs_vec) {
|
2020-04-07 19:26:39 +00:00
|
|
|
// Scalar * vector
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = rhs_type;
|
2020-04-07 19:26:39 +00:00
|
|
|
} else {
|
|
|
|
// Scalar * Scalar
|
2021-01-29 16:43:41 +00:00
|
|
|
result_type = lhs_type;
|
2020-04-07 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:43:41 +00:00
|
|
|
SetType(expr, result_type);
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diagnostics_.add_error("Unknown binary expression", expr->source());
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::UnaryOp(ast::UnaryOpExpression* expr) {
|
2020-04-07 19:27:21 +00:00
|
|
|
// Result type matches the parameter type.
|
2021-03-09 10:54:37 +00:00
|
|
|
if (!Expression(expr->expr())) {
|
2020-04-07 19:27:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-29 16:43:41 +00:00
|
|
|
|
|
|
|
auto* result_type = TypeOf(expr->expr())->UnwrapPtrIfNeeded();
|
|
|
|
SetType(expr, result_type);
|
2020-04-07 19:27:21 +00:00
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) {
|
2021-02-26 18:25:56 +00:00
|
|
|
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-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo* Resolver::CreateVariableInfo(ast::Variable* var) {
|
2021-02-03 17:51:09 +00:00
|
|
|
auto* info = variable_infos_.Create(var);
|
|
|
|
variable_to_info_.emplace(var, info);
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
type::Type* Resolver::TypeOf(ast::Expression* expr) {
|
2021-02-16 18:45:45 +00:00
|
|
|
auto it = expr_info_.find(expr);
|
|
|
|
if (it != expr_info_.end()) {
|
|
|
|
return it->second.type;
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::SetType(ast::Expression* expr, type::Type* type) {
|
2021-02-16 18:45:45 +00:00
|
|
|
assert(expr_info_.count(expr) == 0);
|
|
|
|
expr_info_.emplace(expr, ExpressionInfo{type, current_statement_});
|
2021-01-29 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
void Resolver::CreateSemanticNodes() const {
|
2021-02-03 17:51:09 +00:00
|
|
|
auto& sem = builder_->Sem();
|
|
|
|
|
2021-03-03 19:54:44 +00:00
|
|
|
// Collate all the 'ancestor_entry_points' - this is a map of function symbol
|
|
|
|
// to all the entry points that transitively call the function.
|
|
|
|
std::unordered_map<Symbol, std::vector<Symbol>> ancestor_entry_points;
|
|
|
|
for (auto* func : builder_->AST().Functions()) {
|
|
|
|
auto it = function_to_info_.find(func);
|
|
|
|
if (it == function_to_info_.end()) {
|
2021-03-09 10:54:37 +00:00
|
|
|
continue; // Resolver has likely errored. Process what we can.
|
2021-03-03 19:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto* info = it->second;
|
|
|
|
if (!func->IsEntryPoint()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (auto* call : info->transitive_calls) {
|
|
|
|
auto& vec = ancestor_entry_points[call->declaration->symbol()];
|
|
|
|
vec.emplace_back(func->symbol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Variables
|
2021-02-03 17:51:09 +00:00
|
|
|
for (auto it : variable_to_info_) {
|
|
|
|
auto* var = it.first;
|
|
|
|
auto* info = it.second;
|
2021-02-16 21:15:01 +00:00
|
|
|
std::vector<const semantic::Expression*> users;
|
|
|
|
for (auto* user : info->users) {
|
|
|
|
// Create semantic node for the identifier expression if necessary
|
|
|
|
auto* sem_expr = sem.Get(user);
|
|
|
|
if (sem_expr == nullptr) {
|
|
|
|
auto* type = expr_info_.at(user).type;
|
|
|
|
auto* stmt = expr_info_.at(user).statement;
|
|
|
|
sem_expr = builder_->create<semantic::Expression>(user, type, stmt);
|
|
|
|
sem.Add(user, sem_expr);
|
|
|
|
}
|
|
|
|
users.push_back(sem_expr);
|
|
|
|
}
|
|
|
|
sem.Add(var, builder_->create<semantic::Variable>(var, info->storage_class,
|
|
|
|
std::move(users)));
|
2021-02-03 17:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto remap_vars = [&sem](const std::vector<VariableInfo*>& in) {
|
|
|
|
std::vector<const semantic::Variable*> out;
|
|
|
|
out.reserve(in.size());
|
|
|
|
for (auto* info : in) {
|
|
|
|
out.emplace_back(sem.Get(info->declaration));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::Functions
|
2021-02-08 22:31:44 +00:00
|
|
|
std::unordered_map<FunctionInfo*, semantic::Function*> func_info_to_sem_func;
|
2021-02-03 16:43:20 +00:00
|
|
|
for (auto it : function_to_info_) {
|
|
|
|
auto* func = it.first;
|
|
|
|
auto* info = it.second;
|
2021-03-03 19:54:44 +00:00
|
|
|
|
2021-02-08 22:31:44 +00:00
|
|
|
auto* sem_func = builder_->create<semantic::Function>(
|
|
|
|
info->declaration, remap_vars(info->referenced_module_vars),
|
|
|
|
remap_vars(info->local_referenced_module_vars),
|
2021-03-03 19:54:44 +00:00
|
|
|
ancestor_entry_points[func->symbol()]);
|
2021-02-08 22:31:44 +00:00
|
|
|
func_info_to_sem_func.emplace(info, sem_func);
|
|
|
|
sem.Add(func, sem_func);
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:38:05 +00:00
|
|
|
// Create semantic nodes for all ast::CallExpressions
|
2021-02-08 22:31:44 +00:00
|
|
|
for (auto it : function_calls_) {
|
|
|
|
auto* call = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto info = it.second;
|
|
|
|
auto* sem_func = func_info_to_sem_func.at(info.function);
|
2021-02-16 21:15:01 +00:00
|
|
|
sem.Add(call,
|
|
|
|
builder_->create<semantic::Call>(call, sem_func, info.statement));
|
2021-02-09 17:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create semantic nodes for all remaining expression types
|
2021-02-16 18:45:45 +00:00
|
|
|
for (auto it : expr_info_) {
|
2021-02-09 17:38:05 +00:00
|
|
|
auto* expr = it.first;
|
2021-02-16 18:45:45 +00:00
|
|
|
auto& info = it.second;
|
2021-02-09 17:38:05 +00:00
|
|
|
if (sem.Get(expr)) {
|
|
|
|
// Expression has already been assigned a semantic node
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-16 21:15:01 +00:00
|
|
|
sem.Add(expr, builder_->create<semantic::Expression>(expr, info.type,
|
|
|
|
info.statement));
|
2021-02-03 16:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:06:37 +00:00
|
|
|
template <typename F>
|
|
|
|
bool Resolver::BlockScope(BlockInfo::Type type, F&& callback) {
|
|
|
|
BlockInfo block_info(type, current_block_);
|
|
|
|
ScopedAssignment<BlockInfo*> sa(current_block_, &block_info);
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo::VariableInfo(ast::Variable* decl)
|
2021-02-03 17:51:09 +00:00
|
|
|
: declaration(decl), storage_class(decl->declared_storage_class()) {}
|
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::VariableInfo::~VariableInfo() = default;
|
2021-02-03 17:51:09 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::FunctionInfo::FunctionInfo(ast::Function* decl) : declaration(decl) {}
|
2021-02-03 16:43:20 +00:00
|
|
|
|
2021-03-09 10:54:37 +00:00
|
|
|
Resolver::FunctionInfo::~FunctionInfo() = default;
|
2021-02-03 16:43:20 +00:00
|
|
|
|
2021-03-09 15:08:48 +00:00
|
|
|
} // namespace resolver
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|