2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/type_determiner.h"
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
#include <memory>
|
2020-06-16 15:02:50 +00:00
|
|
|
#include <vector>
|
2020-04-07 12:57:42 +00:00
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
#include "spirv/unified1/GLSL.std.450.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/array_accessor_expression.h"
|
2020-04-07 12:57:52 +00:00
|
|
|
#include "src/ast/as_expression.h"
|
2020-04-07 12:47:23 +00:00
|
|
|
#include "src/ast/assignment_statement.h"
|
2020-04-07 19:27:41 +00:00
|
|
|
#include "src/ast/binary_expression.h"
|
2020-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 16:41:23 +00:00
|
|
|
#include "src/ast/cast_expression.h"
|
2020-04-07 12:54:29 +00:00
|
|
|
#include "src/ast/continue_statement.h"
|
2020-04-07 12:54:37 +00:00
|
|
|
#include "src/ast/else_statement.h"
|
2020-04-07 12:57:27 +00:00
|
|
|
#include "src/ast/identifier_expression.h"
|
2020-04-07 12:55:25 +00:00
|
|
|
#include "src/ast/if_statement.h"
|
2020-06-04 17:05:35 +00:00
|
|
|
#include "src/ast/intrinsic.h"
|
2020-04-07 12:55:51 +00:00
|
|
|
#include "src/ast/loop_statement.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/member_accessor_expression.h"
|
2020-04-07 12:56:24 +00:00
|
|
|
#include "src/ast/return_statement.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
2020-04-07 12:56:45 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/array_type.h"
|
2020-04-07 19:26:39 +00:00
|
|
|
#include "src/ast/type/bool_type.h"
|
2020-04-07 19:27:00 +00:00
|
|
|
#include "src/ast/type/f32_type.h"
|
2020-08-31 15:11:23 +00:00
|
|
|
#include "src/ast/type/i32_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/matrix_type.h"
|
2020-09-21 15:52:10 +00:00
|
|
|
#include "src/ast/type/multisampled_texture_type.h"
|
2020-04-23 22:26:52 +00:00
|
|
|
#include "src/ast/type/pointer_type.h"
|
2020-08-31 15:24:54 +00:00
|
|
|
#include "src/ast/type/sampled_texture_type.h"
|
|
|
|
#include "src/ast/type/storage_texture_type.h"
|
2020-04-07 16:41:33 +00:00
|
|
|
#include "src/ast/type/struct_type.h"
|
2020-08-31 15:24:54 +00:00
|
|
|
#include "src/ast/type/texture_type.h"
|
2020-08-31 15:11:23 +00:00
|
|
|
#include "src/ast/type/u32_type.h"
|
2020-04-07 12:57:42 +00:00
|
|
|
#include "src/ast/type/vector_type.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
#include "src/ast/type_constructor_expression.h"
|
2020-04-07 19:27:21 +00:00
|
|
|
#include "src/ast/unary_op_expression.h"
|
2020-04-07 12:57:12 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
namespace tint {
|
2020-06-16 15:02:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Most of these are floating-point general except the below which are only
|
|
|
|
// FP16 and FP32. We only have FP32 at this point so the below works, if we
|
|
|
|
// get FP64 support or otherwise we'll need to differentiate.
|
|
|
|
// * radians
|
|
|
|
// * degrees
|
|
|
|
// * sin, cos, tan
|
|
|
|
// * asin, acos, atan
|
|
|
|
// * sinh, cosh, tanh
|
|
|
|
// * asinh, acosh, atanh
|
|
|
|
// * exp, exp2
|
|
|
|
// * log, log2
|
|
|
|
enum class GlslDataType {
|
|
|
|
kFloatScalarOrVector,
|
|
|
|
kIntScalarOrVector,
|
2020-06-17 18:39:17 +00:00
|
|
|
kFloatVector,
|
|
|
|
kMatrix
|
2020-06-16 15:02:50 +00:00
|
|
|
};
|
|
|
|
struct GlslData {
|
|
|
|
const char* name;
|
|
|
|
uint8_t param_count;
|
|
|
|
uint32_t op_id;
|
|
|
|
GlslDataType type;
|
|
|
|
uint8_t vector_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr const GlslData kGlslData[] = {
|
|
|
|
{"acos", 1, GLSLstd450Acos, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"acosh", 1, GLSLstd450Acosh, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"asin", 1, GLSLstd450Asin, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"asinh", 1, GLSLstd450Asinh, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"atan", 1, GLSLstd450Atan, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"atan2", 2, GLSLstd450Atan2, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"atanh", 1, GLSLstd450Atanh, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"ceil", 1, GLSLstd450Ceil, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"cos", 1, GLSLstd450Cos, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"cosh", 1, GLSLstd450Cosh, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"cross", 2, GLSLstd450Cross, GlslDataType::kFloatVector, 3},
|
|
|
|
{"degrees", 1, GLSLstd450Degrees, GlslDataType::kFloatScalarOrVector, 0},
|
2020-06-17 18:39:17 +00:00
|
|
|
{"determinant", 1, GLSLstd450Determinant, GlslDataType::kMatrix, 0},
|
2020-06-16 15:02:50 +00:00
|
|
|
{"distance", 2, GLSLstd450Distance, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"exp", 1, GLSLstd450Exp, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"exp2", 1, GLSLstd450Exp2, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fabs", 1, GLSLstd450FAbs, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"faceforward", 3, GLSLstd450FaceForward,
|
|
|
|
GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fclamp", 3, GLSLstd450FClamp, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"findilsb", 1, GLSLstd450FindILsb, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"findumsb", 1, GLSLstd450FindUMsb, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"findsmsb", 1, GLSLstd450FindSMsb, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"floor", 1, GLSLstd450Floor, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fma", 3, GLSLstd450Fma, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fmax", 2, GLSLstd450FMax, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fmin", 2, GLSLstd450FMin, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fmix", 3, GLSLstd450FMix, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fract", 1, GLSLstd450Fract, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"fsign", 1, GLSLstd450FSign, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"interpolateatcentroid", 1, GLSLstd450InterpolateAtCentroid,
|
|
|
|
GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"inversesqrt", 1, GLSLstd450InverseSqrt,
|
|
|
|
GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"length", 1, GLSLstd450Length, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"log", 1, GLSLstd450Log, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"log2", 1, GLSLstd450Log2, GlslDataType::kFloatScalarOrVector, 0},
|
2020-06-17 20:22:08 +00:00
|
|
|
{"matrixinverse", 1, GLSLstd450MatrixInverse, GlslDataType::kMatrix, 0},
|
2020-06-16 15:02:50 +00:00
|
|
|
{"nclamp", 3, GLSLstd450NClamp, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"nmax", 2, GLSLstd450NMax, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"nmin", 2, GLSLstd450NMin, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"normalize", 1, GLSLstd450Normalize, GlslDataType::kFloatScalarOrVector,
|
|
|
|
0},
|
|
|
|
{"pow", 2, GLSLstd450Pow, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"radians", 1, GLSLstd450Radians, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"reflect", 2, GLSLstd450Reflect, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"round", 1, GLSLstd450Round, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"roundeven", 1, GLSLstd450RoundEven, GlslDataType::kFloatScalarOrVector,
|
|
|
|
0},
|
|
|
|
{"sabs", 1, GLSLstd450SAbs, GlslDataType::kIntScalarOrVector, 0},
|
2020-06-18 18:03:00 +00:00
|
|
|
{"sclamp", 3, GLSLstd450SClamp, GlslDataType::kIntScalarOrVector, 0},
|
2020-06-16 15:02:50 +00:00
|
|
|
{"sin", 1, GLSLstd450Sin, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"sinh", 1, GLSLstd450Sinh, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"smax", 2, GLSLstd450SMax, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"smin", 2, GLSLstd450SMin, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"smoothstep", 3, GLSLstd450SmoothStep, GlslDataType::kFloatScalarOrVector,
|
|
|
|
0},
|
|
|
|
{"sqrt", 1, GLSLstd450Sqrt, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"ssign", 1, GLSLstd450SSign, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"step", 2, GLSLstd450Step, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"tan", 1, GLSLstd450Tan, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"tanh", 1, GLSLstd450Tanh, GlslDataType::kFloatScalarOrVector, 0},
|
|
|
|
{"trunc", 1, GLSLstd450Trunc, GlslDataType::kFloatScalarOrVector, 0},
|
2020-06-18 18:03:00 +00:00
|
|
|
{"uclamp", 3, GLSLstd450UClamp, GlslDataType::kIntScalarOrVector, 0},
|
2020-06-16 15:02:50 +00:00
|
|
|
{"umax", 2, GLSLstd450UMax, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
{"umin", 2, GLSLstd450UMin, GlslDataType::kIntScalarOrVector, 0},
|
|
|
|
};
|
|
|
|
constexpr const uint32_t kGlslDataCount = sizeof(kGlslData) / sizeof(GlslData);
|
|
|
|
|
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
TypeDeterminer::TypeDeterminer(Context* ctx, ast::Module* mod)
|
|
|
|
: ctx_(*ctx), mod_(mod) {}
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
TypeDeterminer::~TypeDeterminer() = default;
|
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
void TypeDeterminer::set_error(const Source& src, const std::string& msg) {
|
|
|
|
error_ = "";
|
|
|
|
if (src.line > 0) {
|
|
|
|
error_ +=
|
|
|
|
std::to_string(src.line) + ":" + std::to_string(src.column) + ": ";
|
|
|
|
}
|
|
|
|
error_ += msg;
|
|
|
|
}
|
|
|
|
|
2020-06-22 20:52:24 +00:00
|
|
|
void TypeDeterminer::set_referenced_from_function_if_needed(
|
|
|
|
ast::Variable* var) {
|
|
|
|
if (current_function_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (var->storage_class() == ast::StorageClass::kNone ||
|
|
|
|
var->storage_class() == ast::StorageClass::kFunction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_function_->add_referenced_module_variable(var);
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
bool TypeDeterminer::Determine() {
|
2020-08-31 15:11:23 +00:00
|
|
|
for (auto& iter : ctx_.type_mgr().types()) {
|
|
|
|
auto& type = iter.second;
|
|
|
|
if (!type->IsTexture() || !type->AsTexture()->IsStorage()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!DetermineStorageTextureSubtype(type->AsTexture()->AsStorage())) {
|
|
|
|
set_error(Source{}, "unable to determine storage texture subtype for: " +
|
|
|
|
type->type_name());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
for (const auto& var : mod_->global_variables()) {
|
2020-04-06 21:07:41 +00:00
|
|
|
variable_stack_.set_global(var->name(), var.get());
|
2020-06-15 20:55:09 +00:00
|
|
|
|
|
|
|
if (var->has_constructor()) {
|
|
|
|
if (!DetermineResultType(var->constructor())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 21:07:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:20:01 +00:00
|
|
|
if (!DetermineFunctions(mod_->functions())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-14 19:45:47 +00:00
|
|
|
|
2020-09-21 17:51:31 +00:00
|
|
|
// Walk over the caller to callee information and update functions with which
|
|
|
|
// entry points call those functions.
|
|
|
|
for (const auto& func : mod_->functions()) {
|
2020-09-21 18:49:01 +00:00
|
|
|
if (!func->IsEntryPoint()) {
|
2020-09-21 17:51:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const auto& callee : caller_to_callee_[func->name()]) {
|
|
|
|
set_entry_points(callee, func->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:45:47 +00:00
|
|
|
void TypeDeterminer::set_entry_points(const std::string& fn_name,
|
|
|
|
const std::string& ep_name) {
|
|
|
|
name_to_function_[fn_name]->add_ancestor_entry_point(ep_name);
|
|
|
|
|
|
|
|
for (const auto& callee : caller_to_callee_[fn_name]) {
|
|
|
|
set_entry_points(callee, ep_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
bool TypeDeterminer::DetermineFunctions(const ast::FunctionList& funcs) {
|
|
|
|
for (const auto& func : funcs) {
|
|
|
|
if (!DetermineFunction(func.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineFunction(ast::Function* func) {
|
2020-06-22 20:18:17 +00:00
|
|
|
name_to_function_[func->name()] = func;
|
|
|
|
|
2020-06-22 20:52:24 +00:00
|
|
|
current_function_ = func;
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
variable_stack_.push_scope();
|
2020-06-22 20:18:17 +00:00
|
|
|
for (const auto& param : func->params()) {
|
|
|
|
variable_stack_.set(param->name(), param.get());
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
if (!DetermineStatements(func->body())) {
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
variable_stack_.pop_scope();
|
|
|
|
|
2020-06-22 20:52:24 +00:00
|
|
|
current_function_ = nullptr;
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
bool TypeDeterminer::DetermineStatements(const ast::BlockStatement* stmts) {
|
|
|
|
for (const auto& stmt : *stmts) {
|
|
|
|
if (!DetermineVariableStorageClass(stmt.get())) {
|
|
|
|
return false;
|
2020-04-08 19:58:20 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
if (!DetermineResultType(stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:58:20 +00:00
|
|
|
bool TypeDeterminer::DetermineVariableStorageClass(ast::Statement* stmt) {
|
|
|
|
if (!stmt->IsVariableDecl()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* var = stmt->AsVariableDecl()->variable();
|
2020-04-08 19:58:20 +00:00
|
|
|
// Nothing to do for const
|
|
|
|
if (var->is_const()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->storage_class() == ast::StorageClass::kFunction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (var->storage_class() != ast::StorageClass::kNone) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(stmt->source(),
|
|
|
|
"function variable has a non-function storage class");
|
2020-04-08 19:58:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->set_storage_class(ast::StorageClass::kFunction);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:47:23 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(ast::Statement* stmt) {
|
|
|
|
if (stmt->IsAssign()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* a = stmt->AsAssign();
|
2020-04-07 12:47:23 +00:00
|
|
|
return DetermineResultType(a->lhs()) && DetermineResultType(a->rhs());
|
|
|
|
}
|
2020-07-27 15:25:00 +00:00
|
|
|
if (stmt->IsBlock()) {
|
|
|
|
return DetermineStatements(stmt->AsBlock());
|
|
|
|
}
|
2020-04-07 12:54:10 +00:00
|
|
|
if (stmt->IsBreak()) {
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:10 +00:00
|
|
|
}
|
2020-07-21 13:42:13 +00:00
|
|
|
if (stmt->IsCall()) {
|
|
|
|
return DetermineResultType(stmt->AsCall()->expr());
|
|
|
|
}
|
2020-04-07 12:54:20 +00:00
|
|
|
if (stmt->IsCase()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* c = stmt->AsCase();
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(c->body());
|
2020-04-07 12:54:20 +00:00
|
|
|
}
|
2020-04-07 12:54:29 +00:00
|
|
|
if (stmt->IsContinue()) {
|
2020-06-03 16:11:28 +00:00
|
|
|
return true;
|
2020-04-07 12:54:29 +00:00
|
|
|
}
|
2020-07-25 14:33:50 +00:00
|
|
|
if (stmt->IsDiscard()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:54:37 +00:00
|
|
|
if (stmt->IsElse()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* e = stmt->AsElse();
|
2020-04-07 12:54:37 +00:00
|
|
|
return DetermineResultType(e->condition()) &&
|
2020-04-08 19:58:20 +00:00
|
|
|
DetermineStatements(e->body());
|
2020-04-07 12:54:37 +00:00
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
if (stmt->IsFallthrough()) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:55:25 +00:00
|
|
|
if (stmt->IsIf()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* i = stmt->AsIf();
|
2020-04-07 12:55:25 +00:00
|
|
|
if (!DetermineResultType(i->condition()) ||
|
2020-04-08 19:58:20 +00:00
|
|
|
!DetermineStatements(i->body())) {
|
2020-04-07 12:55:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& else_stmt : i->else_statements()) {
|
|
|
|
if (!DetermineResultType(else_stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:55:51 +00:00
|
|
|
if (stmt->IsLoop()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* l = stmt->AsLoop();
|
2020-04-08 19:58:20 +00:00
|
|
|
return DetermineStatements(l->body()) &&
|
|
|
|
DetermineStatements(l->continuing());
|
2020-04-07 12:55:51 +00:00
|
|
|
}
|
2020-04-07 12:56:24 +00:00
|
|
|
if (stmt->IsReturn()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* r = stmt->AsReturn();
|
2020-04-07 12:56:24 +00:00
|
|
|
return DetermineResultType(r->value());
|
|
|
|
}
|
2020-04-07 12:56:45 +00:00
|
|
|
if (stmt->IsSwitch()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* s = stmt->AsSwitch();
|
2020-04-07 12:56:45 +00:00
|
|
|
if (!DetermineResultType(s->condition())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto& case_stmt : s->body()) {
|
|
|
|
if (!DetermineResultType(case_stmt.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 12:54:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 12:57:12 +00:00
|
|
|
if (stmt->IsVariableDecl()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* v = stmt->AsVariableDecl();
|
2020-04-07 12:57:12 +00:00
|
|
|
variable_stack_.set(v->variable()->name(), v->variable());
|
|
|
|
return DetermineResultType(v->variable()->constructor());
|
|
|
|
}
|
2020-04-07 12:47:23 +00:00
|
|
|
|
2020-07-27 15:25:00 +00:00
|
|
|
set_error(stmt->source(),
|
|
|
|
"unknown statement type for type determination: " + stmt->str());
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(const ast::ExpressionList& list) {
|
|
|
|
for (const auto& expr : list) {
|
|
|
|
if (!DetermineResultType(expr.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-06 21:07:41 +00:00
|
|
|
bool TypeDeterminer::DetermineResultType(ast::Expression* expr) {
|
|
|
|
// This is blindly called above, so in some cases the expression won't exist.
|
|
|
|
if (!expr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
if (expr->IsArrayAccessor()) {
|
|
|
|
return DetermineArrayAccessor(expr->AsArrayAccessor());
|
|
|
|
}
|
2020-04-07 12:57:52 +00:00
|
|
|
if (expr->IsAs()) {
|
|
|
|
return DetermineAs(expr->AsAs());
|
|
|
|
}
|
2020-04-07 19:27:41 +00:00
|
|
|
if (expr->IsBinary()) {
|
|
|
|
return DetermineBinary(expr->AsBinary());
|
|
|
|
}
|
2020-04-07 16:41:10 +00:00
|
|
|
if (expr->IsCall()) {
|
|
|
|
return DetermineCall(expr->AsCall());
|
|
|
|
}
|
2020-04-07 16:41:23 +00:00
|
|
|
if (expr->IsCast()) {
|
|
|
|
return DetermineCast(expr->AsCast());
|
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
if (expr->IsConstructor()) {
|
|
|
|
return DetermineConstructor(expr->AsConstructor());
|
|
|
|
}
|
2020-04-07 12:57:27 +00:00
|
|
|
if (expr->IsIdentifier()) {
|
|
|
|
return DetermineIdentifier(expr->AsIdentifier());
|
|
|
|
}
|
2020-04-07 16:41:33 +00:00
|
|
|
if (expr->IsMemberAccessor()) {
|
|
|
|
return DetermineMemberAccessor(expr->AsMemberAccessor());
|
|
|
|
}
|
2020-04-07 19:27:11 +00:00
|
|
|
if (expr->IsUnaryOp()) {
|
|
|
|
return DetermineUnaryOp(expr->AsUnaryOp());
|
|
|
|
}
|
2020-04-07 12:46:30 +00:00
|
|
|
|
2020-04-08 19:58:35 +00:00
|
|
|
set_error(expr->source(), "unknown expression for type determination");
|
2020-04-06 21:07:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
bool TypeDeterminer::DetermineArrayAccessor(
|
|
|
|
ast::ArrayAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->array())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
if (!DetermineResultType(expr->idx_expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
auto* res = expr->array()->result_type();
|
2020-06-22 20:33:12 +00:00
|
|
|
auto* parent_type = res->UnwrapAliasPtrAlias();
|
2020-04-23 22:26:52 +00:00
|
|
|
ast::type::Type* ret = nullptr;
|
2020-04-07 12:57:42 +00:00
|
|
|
if (parent_type->IsArray()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = parent_type->AsArray()->type();
|
2020-04-07 12:57:42 +00:00
|
|
|
} else if (parent_type->IsVector()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = parent_type->AsVector()->type();
|
2020-04-07 12:57:42 +00:00
|
|
|
} else if (parent_type->IsMatrix()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* m = parent_type->AsMatrix();
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(m->type(), m->rows()));
|
2020-04-07 12:57:42 +00:00
|
|
|
} else {
|
2020-08-25 21:22:37 +00:00
|
|
|
set_error(expr->source(), "invalid parent type (" +
|
|
|
|
parent_type->type_name() +
|
|
|
|
") in array accessor");
|
2020-04-07 12:57:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
|
|
|
if (res->IsPointer()) {
|
|
|
|
ret = ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
ret, res->AsPointer()->storage_class()));
|
|
|
|
}
|
|
|
|
expr->set_result_type(ret);
|
|
|
|
|
2020-04-07 12:57:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:52 +00:00
|
|
|
bool TypeDeterminer::DetermineAs(ast::AsExpression* expr) {
|
2020-06-18 18:02:46 +00:00
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:52 +00:00
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) {
|
2020-04-20 15:46:18 +00:00
|
|
|
if (!DetermineResultType(expr->params())) {
|
|
|
|
return false;
|
2020-04-20 14:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// The expression has to be an identifier as you can't store function pointers
|
|
|
|
// but, if it isn't we'll just use the normal result determination to be on
|
|
|
|
// the safe side.
|
|
|
|
if (expr->func()->IsIdentifier()) {
|
|
|
|
auto* ident = expr->func()->AsIdentifier();
|
|
|
|
|
2020-06-04 17:05:35 +00:00
|
|
|
if (ast::intrinsic::IsIntrinsic(ident->name())) {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (!DetermineIntrinsic(ident->name(), expr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (ident->has_path()) {
|
2020-04-20 15:46:18 +00:00
|
|
|
auto* imp = mod_->FindImportByName(ident->path());
|
|
|
|
if (imp == nullptr) {
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(expr->source(), "Unable to find import for " + ident->name());
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ext_id = 0;
|
2020-04-21 13:05:42 +00:00
|
|
|
auto* result_type = GetImportData(expr->source(), imp->path(),
|
|
|
|
ident->name(), expr->params(), &ext_id);
|
2020-04-20 15:46:18 +00:00
|
|
|
if (result_type == nullptr) {
|
2020-04-24 00:40:12 +00:00
|
|
|
if (error_.empty()) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"Unable to determine result type for GLSL expression " +
|
|
|
|
ident->name());
|
|
|
|
}
|
2020-04-20 15:46:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
imp->AddMethodId(ident->name(), ext_id);
|
|
|
|
expr->func()->set_result_type(result_type);
|
|
|
|
} else {
|
2020-07-14 19:45:47 +00:00
|
|
|
if (current_function_) {
|
|
|
|
caller_to_callee_[current_function_->name()].push_back(ident->name());
|
2020-07-14 20:37:28 +00:00
|
|
|
|
|
|
|
auto* callee_func = mod_->FindFunctionByName(ident->name());
|
|
|
|
if (callee_func == nullptr) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"unable to find called function: " + ident->name());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We inherit any referenced variables from the callee.
|
|
|
|
for (auto* var : callee_func->referenced_module_variables()) {
|
|
|
|
set_referenced_from_function_if_needed(var);
|
|
|
|
}
|
2020-07-14 19:45:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
// An identifier with a single name is a function call, not an import
|
|
|
|
// lookup which we can handle with the regular identifier lookup.
|
|
|
|
if (!DetermineResultType(ident)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!DetermineResultType(expr->func())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 16:41:10 +00:00
|
|
|
}
|
2020-08-18 02:10:03 +00:00
|
|
|
|
|
|
|
if (!expr->func()->result_type()) {
|
|
|
|
auto func_name = expr->func()->AsIdentifier()->name();
|
|
|
|
set_error(
|
|
|
|
expr->source(),
|
|
|
|
"v-0005: function must be declared before use: '" + func_name + "'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:10 +00:00
|
|
|
expr->set_result_type(expr->func()->result_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:43:22 +00:00
|
|
|
bool TypeDeterminer::DetermineIntrinsic(const std::string& name,
|
|
|
|
ast::CallExpression* expr) {
|
2020-06-04 17:05:35 +00:00
|
|
|
if (ast::intrinsic::IsDerivative(name)) {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (expr->params().size() != 1) {
|
|
|
|
set_error(expr->source(), "incorrect number of parameters for " + name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The result type must be the same as the type of the parameter.
|
|
|
|
auto& param = expr->params()[0];
|
|
|
|
if (!DetermineResultType(param.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expr->func()->set_result_type(param->result_type()->UnwrapPtrIfNeeded());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (name == "any" || name == "all") {
|
|
|
|
expr->func()->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>()));
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-04 17:05:35 +00:00
|
|
|
if (ast::intrinsic::IsFloatClassificationIntrinsic(name)) {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (expr->params().size() != 1) {
|
|
|
|
set_error(expr->source(), "incorrect number of parameters for " + name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* bool_type =
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
|
|
|
|
|
2020-07-21 17:44:44 +00:00
|
|
|
auto& param = expr->params()[0];
|
|
|
|
if (!DetermineResultType(param.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto* param_type = param->result_type()->UnwrapPtrIfNeeded();
|
2020-06-01 13:43:22 +00:00
|
|
|
if (param_type->IsVector()) {
|
|
|
|
expr->func()->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
bool_type, param_type->AsVector()->size())));
|
|
|
|
} else {
|
|
|
|
expr->func()->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-31 15:24:54 +00:00
|
|
|
if (ast::intrinsic::IsTextureOperationIntrinsic(name)) {
|
2020-09-10 14:15:37 +00:00
|
|
|
// TODO: Remove the LOD param from textureLoad on storage textures when
|
|
|
|
// https://github.com/gpuweb/gpuweb/pull/1032 gets merged.
|
2020-08-31 15:24:54 +00:00
|
|
|
uint32_t num_of_params =
|
2020-09-08 16:30:29 +00:00
|
|
|
(name == "textureLoad" || name == "textureSample") ? 3 : 4;
|
2020-08-31 15:24:54 +00:00
|
|
|
if (expr->params().size() != num_of_params) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for " + name + ", got " +
|
|
|
|
std::to_string(expr->params().size()) + " and expected " +
|
|
|
|
std::to_string(num_of_params));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-08 16:30:29 +00:00
|
|
|
if (name == "textureSampleCompare") {
|
2020-08-31 15:24:54 +00:00
|
|
|
expr->func()->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& texture_param = expr->params()[0];
|
|
|
|
if (!DetermineResultType(texture_param.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!texture_param->result_type()->UnwrapPtrIfNeeded()->IsTexture()) {
|
|
|
|
set_error(expr->source(), "invalid first argument for " + name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ast::type::TextureType* texture =
|
|
|
|
texture_param->result_type()->UnwrapPtrIfNeeded()->AsTexture();
|
|
|
|
|
2020-09-21 15:52:10 +00:00
|
|
|
if (!texture->IsStorage() &&
|
|
|
|
!(texture->IsSampled() || texture->IsMultisampled())) {
|
2020-08-31 15:24:54 +00:00
|
|
|
set_error(expr->source(), "invalid texture for " + name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:52:10 +00:00
|
|
|
ast::type::Type* type = nullptr;
|
|
|
|
if (texture->IsStorage()) {
|
|
|
|
type = texture->AsStorage()->type();
|
|
|
|
} else if (texture->IsSampled()) {
|
|
|
|
type = texture->AsSampled()->type();
|
|
|
|
} else if (texture->IsMultisampled()) {
|
|
|
|
type = texture->AsMultisampled()->type();
|
|
|
|
} else {
|
|
|
|
set_error(expr->source(), "unknown texture type for texture sampling");
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-31 15:24:54 +00:00
|
|
|
expr->func()->set_result_type(
|
2020-09-21 15:52:10 +00:00
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(type, 4)));
|
2020-08-31 15:24:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
if (name == "dot") {
|
|
|
|
expr->func()->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()));
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-08 16:30:29 +00:00
|
|
|
if (name == "outerProduct") {
|
2020-06-01 13:43:22 +00:00
|
|
|
if (expr->params().size() != 2) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for outer_product");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-21 17:44:44 +00:00
|
|
|
auto& param0 = expr->params()[0];
|
|
|
|
auto& param1 = expr->params()[1];
|
|
|
|
if (!DetermineResultType(param0.get()) ||
|
|
|
|
!DetermineResultType(param1.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* param0_type = param0->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* param1_type = param1->result_type()->UnwrapPtrIfNeeded();
|
2020-06-01 13:43:22 +00:00
|
|
|
if (!param0_type->IsVector() || !param1_type->IsVector()) {
|
|
|
|
set_error(expr->source(), "invalid parameter type for outer_product");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr->func()->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()),
|
|
|
|
param0_type->AsVector()->size(), param1_type->AsVector()->size())));
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-21 17:44:44 +00:00
|
|
|
if (name == "select") {
|
|
|
|
if (expr->params().size() != 3) {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"incorrect number of parameters for select expected 3 got " +
|
|
|
|
std::to_string(expr->params().size()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The result type must be the same as the type of the parameter.
|
|
|
|
auto& param = expr->params()[0];
|
|
|
|
if (!DetermineResultType(param.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expr->func()->set_result_type(param->result_type()->UnwrapPtrIfNeeded());
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-01 13:43:22 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:23 +00:00
|
|
|
bool TypeDeterminer::DetermineCast(ast::CastExpression* expr) {
|
2020-05-01 19:05:55 +00:00
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:23 +00:00
|
|
|
expr->set_result_type(expr->type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:46:30 +00:00
|
|
|
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
|
|
|
if (expr->IsTypeConstructor()) {
|
2020-04-24 00:41:12 +00:00
|
|
|
auto* ty = expr->AsTypeConstructor();
|
|
|
|
for (const auto& value : ty->values()) {
|
|
|
|
if (!DetermineResultType(value.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expr->set_result_type(ty->type());
|
2020-04-07 12:46:30 +00:00
|
|
|
} else {
|
|
|
|
expr->set_result_type(expr->AsScalarConstructor()->literal()->type());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:57:27 +00:00
|
|
|
bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) {
|
2020-04-20 14:21:00 +00:00
|
|
|
if (expr->has_path()) {
|
2020-04-20 15:46:18 +00:00
|
|
|
set_error(expr->source(),
|
|
|
|
"determine identifier should not be called with imports");
|
2020-04-07 12:57:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:21:00 +00:00
|
|
|
auto name = expr->name();
|
2020-04-07 12:57:27 +00:00
|
|
|
ast::Variable* var;
|
|
|
|
if (variable_stack_.get(name, &var)) {
|
2020-04-23 22:26:52 +00:00
|
|
|
// A constant is the type, but a variable is always a pointer so synthesize
|
|
|
|
// the pointer around the variable type.
|
|
|
|
if (var->is_const()) {
|
|
|
|
expr->set_result_type(var->type());
|
2020-09-16 21:20:36 +00:00
|
|
|
} else if (var->type()->IsPointer()) {
|
|
|
|
expr->set_result_type(var->type());
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
var->type(), var->storage_class())));
|
|
|
|
}
|
2020-06-22 20:52:24 +00:00
|
|
|
|
|
|
|
set_referenced_from_function_if_needed(var);
|
2020-04-07 12:57:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto iter = name_to_function_.find(name);
|
|
|
|
if (iter != name_to_function_.end()) {
|
|
|
|
expr->set_result_type(iter->second->return_type());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:41:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TypeDeterminer::DetermineMemberAccessor(
|
|
|
|
ast::MemberAccessorExpression* expr) {
|
|
|
|
if (!DetermineResultType(expr->structure())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* res = expr->structure()->result_type();
|
2020-06-22 20:30:33 +00:00
|
|
|
auto* data_type = res->UnwrapPtrIfNeeded()->UnwrapAliasesIfNeeded();
|
2020-04-24 00:40:45 +00:00
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
ast::type::Type* ret = nullptr;
|
2020-04-07 16:41:33 +00:00
|
|
|
if (data_type->IsStruct()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* strct = data_type->AsStruct()->impl();
|
2020-04-20 14:21:00 +00:00
|
|
|
auto name = expr->member()->name();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
|
|
|
for (const auto& member : strct->members()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
if (member->name() == name) {
|
|
|
|
ret = member->type();
|
|
|
|
break;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
if (ret == nullptr) {
|
|
|
|
set_error(expr->source(), "struct member " + name + " not found");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-01 16:17:03 +00:00
|
|
|
|
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
|
|
|
if (res->IsPointer()) {
|
|
|
|
ret = ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
ret, res->AsPointer()->storage_class()));
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else if (data_type->IsVector()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* vec = data_type->AsVector();
|
2020-04-07 16:41:33 +00:00
|
|
|
|
2020-04-21 13:05:34 +00:00
|
|
|
auto size = expr->member()->name().size();
|
|
|
|
if (size == 1) {
|
|
|
|
// A single element swizzle is just the type of the vector.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = vec->type();
|
2020-05-01 16:17:03 +00:00
|
|
|
// If we're extracting from a pointer, we return a pointer.
|
|
|
|
if (res->IsPointer()) {
|
|
|
|
ret = ctx_.type_mgr().Get(std::make_unique<ast::type::PointerType>(
|
|
|
|
ret, res->AsPointer()->storage_class()));
|
|
|
|
}
|
2020-04-21 13:05:34 +00:00
|
|
|
} else {
|
|
|
|
// The vector will have a number of components equal to the length of the
|
|
|
|
// swizzle. This assumes the validator will check that the swizzle
|
|
|
|
// is correct.
|
2020-04-23 22:26:52 +00:00
|
|
|
ret = ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(vec->type(), size));
|
2020-04-21 13:05:34 +00:00
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
} else {
|
|
|
|
set_error(expr->source(),
|
|
|
|
"invalid type " + data_type->type_name() + " in member accessor");
|
|
|
|
return false;
|
2020-04-07 16:41:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(ret);
|
|
|
|
|
|
|
|
return true;
|
2020-04-07 12:57:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:41 +00:00
|
|
|
bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) {
|
2020-04-07 19:26:39 +00:00
|
|
|
if (!DetermineResultType(expr->lhs()) || !DetermineResultType(expr->rhs())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result type matches first parameter type
|
|
|
|
if (expr->IsAnd() || expr->IsOr() || expr->IsXor() || expr->IsShiftLeft() ||
|
2020-06-03 16:11:44 +00:00
|
|
|
expr->IsShiftRight() || expr->IsAdd() || expr->IsSubtract() ||
|
|
|
|
expr->IsDivide() || expr->IsModulo()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->lhs()->result_type()->UnwrapPtrIfNeeded());
|
2020-04-07 19:26:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Result type is a scalar or vector of boolean type
|
|
|
|
if (expr->IsLogicalAnd() || expr->IsLogicalOr() || expr->IsEqual() ||
|
|
|
|
expr->IsNotEqual() || expr->IsLessThan() || expr->IsGreaterThan() ||
|
|
|
|
expr->IsLessThanEqual() || expr->IsGreaterThanEqual()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* bool_type =
|
2020-04-07 19:26:39 +00:00
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* param_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded();
|
2020-04-07 19:26:39 +00:00
|
|
|
if (param_type->IsVector()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
bool_type, param_type->AsVector()->size())));
|
|
|
|
} else {
|
|
|
|
expr->set_result_type(bool_type);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (expr->IsMultiply()) {
|
2020-04-23 22:26:52 +00:00
|
|
|
auto* lhs_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded();
|
|
|
|
auto* rhs_type = expr->rhs()->result_type()->UnwrapPtrIfNeeded();
|
2020-04-07 19:26:39 +00:00
|
|
|
|
|
|
|
// Note, the ordering here matters. The later checks depend on the prior
|
|
|
|
// checks having been done.
|
|
|
|
if (lhs_type->IsMatrix() && rhs_type->IsMatrix()) {
|
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
|
|
|
|
lhs_type->AsMatrix()->type(), lhs_type->AsMatrix()->rows(),
|
|
|
|
rhs_type->AsMatrix()->columns())));
|
|
|
|
|
|
|
|
} else if (lhs_type->IsMatrix() && rhs_type->IsVector()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* mat = lhs_type->AsMatrix();
|
2020-04-07 19:27:21 +00:00
|
|
|
expr->set_result_type(ctx_.type_mgr().Get(
|
|
|
|
std::make_unique<ast::type::VectorType>(mat->type(), mat->rows())));
|
2020-04-07 19:26:39 +00:00
|
|
|
} else if (lhs_type->IsVector() && rhs_type->IsMatrix()) {
|
2020-04-09 18:52:06 +00:00
|
|
|
auto* mat = rhs_type->AsMatrix();
|
2020-04-07 19:26:39 +00:00
|
|
|
expr->set_result_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::VectorType>(
|
|
|
|
mat->type(), mat->columns())));
|
|
|
|
} else if (lhs_type->IsMatrix()) {
|
|
|
|
// matrix * scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (rhs_type->IsMatrix()) {
|
|
|
|
// scalar * matrix
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else if (lhs_type->IsVector() && rhs_type->IsVector()) {
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (lhs_type->IsVector()) {
|
|
|
|
// Vector * scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
} else if (rhs_type->IsVector()) {
|
|
|
|
// Scalar * vector
|
|
|
|
expr->set_result_type(rhs_type);
|
|
|
|
} else {
|
|
|
|
// Scalar * Scalar
|
|
|
|
expr->set_result_type(lhs_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:05:42 +00:00
|
|
|
set_error(expr->source(), "Unknown binary expression");
|
2020-04-07 19:26:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:27:11 +00:00
|
|
|
bool TypeDeterminer::DetermineUnaryOp(ast::UnaryOpExpression* expr) {
|
2020-04-07 19:27:21 +00:00
|
|
|
// Result type matches the parameter type.
|
|
|
|
if (!DetermineResultType(expr->expr())) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-23 22:26:52 +00:00
|
|
|
expr->set_result_type(expr->expr()->result_type()->UnwrapPtrIfNeeded());
|
2020-04-07 19:27:21 +00:00
|
|
|
return true;
|
2020-04-07 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:11:23 +00:00
|
|
|
bool TypeDeterminer::DetermineStorageTextureSubtype(
|
|
|
|
ast::type::StorageTextureType* tex) {
|
|
|
|
if (tex->type() != nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tex->image_format()) {
|
|
|
|
case ast::type::ImageFormat::kR8Unorm:
|
|
|
|
case ast::type::ImageFormat::kRg8Unorm:
|
|
|
|
case ast::type::ImageFormat::kRgba8Unorm:
|
|
|
|
case ast::type::ImageFormat::kRgba8UnormSrgb:
|
|
|
|
case ast::type::ImageFormat::kBgra8Unorm:
|
|
|
|
case ast::type::ImageFormat::kBgra8UnormSrgb:
|
|
|
|
case ast::type::ImageFormat::kRgb10A2Unorm:
|
|
|
|
case ast::type::ImageFormat::kR8Uint:
|
|
|
|
case ast::type::ImageFormat::kR16Uint:
|
|
|
|
case ast::type::ImageFormat::kRg8Uint:
|
|
|
|
case ast::type::ImageFormat::kR32Uint:
|
|
|
|
case ast::type::ImageFormat::kRg16Uint:
|
|
|
|
case ast::type::ImageFormat::kRgba8Uint:
|
|
|
|
case ast::type::ImageFormat::kRg32Uint:
|
|
|
|
case ast::type::ImageFormat::kRgba16Uint:
|
|
|
|
case ast::type::ImageFormat::kRgba32Uint: {
|
|
|
|
tex->set_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::U32Type>()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ast::type::ImageFormat::kR8Snorm:
|
|
|
|
case ast::type::ImageFormat::kRg8Snorm:
|
|
|
|
case ast::type::ImageFormat::kRgba8Snorm:
|
|
|
|
case ast::type::ImageFormat::kR8Sint:
|
|
|
|
case ast::type::ImageFormat::kR16Sint:
|
|
|
|
case ast::type::ImageFormat::kRg8Sint:
|
|
|
|
case ast::type::ImageFormat::kR32Sint:
|
|
|
|
case ast::type::ImageFormat::kRg16Sint:
|
|
|
|
case ast::type::ImageFormat::kRgba8Sint:
|
|
|
|
case ast::type::ImageFormat::kRg32Sint:
|
|
|
|
case ast::type::ImageFormat::kRgba16Sint:
|
|
|
|
case ast::type::ImageFormat::kRgba32Sint: {
|
|
|
|
tex->set_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::I32Type>()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ast::type::ImageFormat::kR16Float:
|
|
|
|
case ast::type::ImageFormat::kR32Float:
|
|
|
|
case ast::type::ImageFormat::kRg16Float:
|
|
|
|
case ast::type::ImageFormat::kRg11B10Float:
|
|
|
|
case ast::type::ImageFormat::kRg32Float:
|
|
|
|
case ast::type::ImageFormat::kRgba16Float:
|
|
|
|
case ast::type::ImageFormat::kRgba32Float: {
|
|
|
|
tex->set_type(
|
|
|
|
ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>()));
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-08 16:38:39 +00:00
|
|
|
|
|
|
|
case ast::type::ImageFormat::kNone:
|
|
|
|
break;
|
2020-08-31 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:46:18 +00:00
|
|
|
ast::type::Type* TypeDeterminer::GetImportData(
|
2020-04-21 13:05:42 +00:00
|
|
|
const Source& source,
|
2020-04-20 15:46:18 +00:00
|
|
|
const std::string& path,
|
|
|
|
const std::string& name,
|
|
|
|
const ast::ExpressionList& params,
|
|
|
|
uint32_t* id) {
|
|
|
|
if (path != "GLSL.std.450") {
|
2020-08-26 19:05:46 +00:00
|
|
|
set_error(source, "unknown import path " + path);
|
2020-04-20 15:46:18 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
const GlslData* data = nullptr;
|
|
|
|
for (uint32_t i = 0; i < kGlslDataCount; ++i) {
|
|
|
|
if (name == kGlslData[i].name) {
|
|
|
|
data = &kGlslData[i];
|
|
|
|
break;
|
2020-04-20 21:09:14 +00:00
|
|
|
}
|
2020-06-08 18:49:31 +00:00
|
|
|
}
|
|
|
|
if (data == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-20 21:09:14 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
if (params.size() != data->param_count) {
|
|
|
|
set_error(source, "incorrect number of parameters for " + name +
|
|
|
|
". Expected " + std::to_string(data->param_count) +
|
|
|
|
" got " + std::to_string(params.size()));
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
std::vector<ast::type::Type*> result_types;
|
|
|
|
for (uint32_t i = 0; i < data->param_count; ++i) {
|
|
|
|
result_types.push_back(params[i]->result_type()->UnwrapPtrIfNeeded());
|
2020-04-21 12:55:06 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
switch (data->type) {
|
|
|
|
case GlslDataType::kFloatScalarOrVector:
|
|
|
|
if (!result_types.back()->is_float_scalar_or_vector()) {
|
|
|
|
set_error(source, "incorrect type for " + name + ". " +
|
|
|
|
"Requires float scalar or float vector values");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-04-21 12:55:06 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
break;
|
|
|
|
case GlslDataType::kIntScalarOrVector:
|
2020-06-08 19:01:07 +00:00
|
|
|
if (!result_types.back()->is_integer_scalar_or_vector()) {
|
|
|
|
set_error(source,
|
|
|
|
"incorrect type for " + name + ". " +
|
|
|
|
"Requires integer scalar or integer vector values");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-06-08 18:49:31 +00:00
|
|
|
break;
|
2020-06-08 23:48:15 +00:00
|
|
|
case GlslDataType::kFloatVector:
|
|
|
|
if (!result_types.back()->is_float_vector()) {
|
|
|
|
set_error(source, "incorrect type for " + name + ". " +
|
|
|
|
"Requires float vector values");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (data->vector_count > 0 &&
|
|
|
|
result_types.back()->AsVector()->size() != data->vector_count) {
|
|
|
|
set_error(source,
|
|
|
|
"incorrect vector size for " + name + ". " + "Requires " +
|
|
|
|
std::to_string(data->vector_count) + " elements");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
2020-06-17 18:39:17 +00:00
|
|
|
case GlslDataType::kMatrix:
|
|
|
|
if (!result_types.back()->IsMatrix()) {
|
|
|
|
set_error(source,
|
|
|
|
"incorrect type for " + name + ". Requires matrix value");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
break;
|
2020-04-22 00:23:57 +00:00
|
|
|
}
|
2020-06-08 18:49:31 +00:00
|
|
|
}
|
2020-04-24 00:40:12 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
// Verify all the parameter types match
|
|
|
|
for (size_t i = 1; i < data->param_count; ++i) {
|
|
|
|
if (result_types[0] != result_types[i]) {
|
2020-04-22 00:23:57 +00:00
|
|
|
error_ = "mismatched parameter types for " + name;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-06-08 18:49:31 +00:00
|
|
|
}
|
2020-04-22 00:23:57 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
*id = data->op_id;
|
2020-04-22 00:23:57 +00:00
|
|
|
|
2020-06-08 18:49:31 +00:00
|
|
|
// Handle functions which aways return the type, even if a vector is provided.
|
|
|
|
if (name == "length" || name == "distance") {
|
|
|
|
return result_types[0]->is_float_scalar()
|
|
|
|
? result_types[0]
|
|
|
|
: result_types[0]->AsVector()->type();
|
2020-04-20 15:46:18 +00:00
|
|
|
}
|
2020-06-17 18:39:17 +00:00
|
|
|
// The determinant returns the component type of the columns
|
|
|
|
if (name == "determinant") {
|
|
|
|
return result_types[0]->AsMatrix()->type();
|
|
|
|
}
|
2020-06-08 18:49:31 +00:00
|
|
|
return result_types[0];
|
2020-04-20 15:46:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace tint
|