GDScript: Remove some unnecessary booleans

This commit is contained in:
Danil Alexeev 2024-10-10 18:08:50 +03:00
parent 4c4e673344
commit 16116697b6
No known key found for this signature in database
GPG Key ID: 124453E157DA8DC7
7 changed files with 34 additions and 56 deletions

View File

@ -149,7 +149,6 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco
/* STEP 1, CREATE */
GDScriptInstance *instance = memnew(GDScriptInstance);
instance->base_ref_counted = p_is_ref_counted;
instance->members.resize(member_indices.size());
instance->script = Ref<GDScript>(this);
instance->owner = p_owner;
@ -689,7 +688,7 @@ void GDScript::_static_default_init() {
for (const KeyValue<StringName, MemberInfo> &E : static_variables_indices) {
const GDScriptDataType &type = E.value.data_type;
// Only initialize builtin types, which are not expected to be `null`.
if (!type.has_type || type.kind != GDScriptDataType::BUILTIN) {
if (type.kind != GDScriptDataType::BUILTIN) {
continue;
}
if (type.builtin_type == Variant::ARRAY && type.has_container_element_type(0)) {
@ -1007,7 +1006,7 @@ bool GDScript::_set(const StringName &p_name, const Variant &p_value) {
if (E) {
const MemberInfo *member = &E->value;
Variant value = p_value;
if (member->data_type.has_type && !member->data_type.is_type(value)) {
if (!member->data_type.is_type(value)) {
const Variant *args = &p_value;
Callable::CallError err;
Variant::construct(member->data_type.builtin_type, value, &args, 1, err);
@ -1660,7 +1659,7 @@ bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) {
if (E) {
const GDScript::MemberInfo *member = &E->value;
Variant value = p_value;
if (member->data_type.has_type && !member->data_type.is_type(value)) {
if (!member->data_type.is_type(value)) {
const Variant *args = &p_value;
Callable::CallError err;
Variant::construct(member->data_type.builtin_type, value, &args, 1, err);
@ -1687,7 +1686,7 @@ bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) {
if (E) {
const GDScript::MemberInfo *member = &E->value;
Variant value = p_value;
if (member->data_type.has_type && !member->data_type.is_type(value)) {
if (!member->data_type.is_type(value)) {
const Variant *args = &p_value;
Callable::CallError err;
Variant::construct(member->data_type.builtin_type, value, &args, 1, err);
@ -2143,11 +2142,6 @@ void GDScriptInstance::reload_members() {
#endif
}
GDScriptInstance::GDScriptInstance() {
owner = nullptr;
base_ref_counted = false;
}
GDScriptInstance::~GDScriptInstance() {
MutexLock lock(GDScriptLanguage::get_singleton()->mutex);

View File

@ -362,7 +362,6 @@ class GDScriptInstance : public ScriptInstance {
HashMap<StringName, int> member_indices_cache; //used only for hot script reloading
#endif
Vector<Variant> members;
bool base_ref_counted;
SelfList<GDScriptFunctionState>::List pending_func_states;
@ -402,7 +401,7 @@ public:
virtual const Variant get_rpc_config() const;
GDScriptInstance();
GDScriptInstance() {}
~GDScriptInstance();
};

View File

@ -67,7 +67,7 @@ uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) {
uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_type) {
Variant::Type temp_type = Variant::NIL;
if (p_type.has_type && p_type.kind == GDScriptDataType::BUILTIN) {
if (p_type.kind == GDScriptDataType::BUILTIN) {
switch (p_type.builtin_type) {
case Variant::NIL:
case Variant::BOOL:
@ -426,10 +426,10 @@ void GDScriptByteCodeGenerator::set_initial_line(int p_line) {
}
#define HAS_BUILTIN_TYPE(m_var) \
(m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN)
(m_var.type.kind == GDScriptDataType::BUILTIN)
#define IS_BUILTIN_TYPE(m_var, m_type) \
(m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type && m_type != Variant::NIL)
(m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type && m_type != Variant::NIL)
void GDScriptByteCodeGenerator::write_type_adjust(const Address &p_target, Variant::Type p_new_type) {
switch (p_new_type) {
@ -1055,7 +1055,6 @@ GDScriptByteCodeGenerator::CallTarget GDScriptByteCodeGenerator::get_call_target
if (p_target.mode == Address::NIL) {
GDScriptDataType type;
if (p_type != Variant::NIL) {
type.has_type = true;
type.kind = GDScriptDataType::BUILTIN;
type.builtin_type = p_type;
}
@ -1555,7 +1554,7 @@ void GDScriptByteCodeGenerator::write_for(const Address &p_variable, bool p_use_
GDScriptFunction::Opcode begin_opcode = GDScriptFunction::OPCODE_ITERATE_BEGIN;
GDScriptFunction::Opcode iterate_opcode = GDScriptFunction::OPCODE_ITERATE;
if (container.type.has_type) {
if (container.type.has_type()) {
if (container.type.kind == GDScriptDataType::BUILTIN) {
switch (container.type.builtin_type) {
case Variant::INT:
@ -1751,11 +1750,11 @@ void GDScriptByteCodeGenerator::write_newline(int p_line) {
}
void GDScriptByteCodeGenerator::write_return(const Address &p_return_value) {
if (!function->return_type.has_type || p_return_value.type.has_type) {
if (!function->return_type.has_type() || p_return_value.type.has_type()) {
// Either the function is untyped or the return value is also typed.
// If this is a typed function, then we need to check for potential conversions.
if (function->return_type.has_type) {
if (function->return_type.has_type()) {
if (function->return_type.kind == GDScriptDataType::BUILTIN && function->return_type.builtin_type == Variant::ARRAY && function->return_type.has_container_element_type(0)) {
// Typed array.
const GDScriptDataType &element_type = function->return_type.get_container_element_type(0);
@ -1875,7 +1874,7 @@ void GDScriptByteCodeGenerator::clear_address(const Address &p_address) {
// Do not check `is_local_dirty()` here! Always clear the address since the codegen doesn't track the compiler.
// Also, this method is used to initialize local variables of built-in types, since they cannot be `null`.
if (p_address.type.has_type && p_address.type.kind == GDScriptDataType::BUILTIN) {
if (p_address.type.kind == GDScriptDataType::BUILTIN) {
switch (p_address.type.builtin_type) {
case Variant::BOOL:
write_assign_false(p_address);

View File

@ -92,11 +92,10 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
}
GDScriptDataType result;
result.has_type = true;
switch (p_datatype.kind) {
case GDScriptParser::DataType::VARIANT: {
result.has_type = false;
result.kind = GDScriptDataType::VARIANT;
} break;
case GDScriptParser::DataType::BUILTIN: {
result.kind = GDScriptDataType::BUILTIN;
@ -206,7 +205,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
}
static bool _is_exact_type(const PropertyInfo &p_par_type, const GDScriptDataType &p_arg_type) {
if (!p_arg_type.has_type) {
if (!p_arg_type.has_type()) {
return false;
}
if (p_par_type.type == Variant::NIL) {
@ -582,7 +581,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
GDScriptDataType cast_type = _gdtype_from_datatype(cn->get_datatype(), codegen.script, false);
GDScriptCodeGenerator::Address result;
if (cast_type.has_type) {
if (cast_type.has_type()) {
// Create temporary for result first since it will be deleted last.
result = codegen.add_temporary(cast_type);
@ -691,7 +690,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
}
if (is_awaited) {
gen->write_call_async(result, base, call->function_name, arguments);
} else if (base.type.has_type && base.type.kind != GDScriptDataType::BUILTIN) {
} else if (base.type.kind != GDScriptDataType::VARIANT && base.type.kind != GDScriptDataType::BUILTIN) {
// Native method, use faster path.
StringName class_name;
if (base.type.kind == GDScriptDataType::NATIVE) {
@ -711,7 +710,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
} else {
gen->write_call(result, base, call->function_name, arguments);
}
} else if (base.type.has_type && base.type.kind == GDScriptDataType::BUILTIN) {
} else if (base.type.kind == GDScriptDataType::BUILTIN) {
gen->write_call_builtin_type(result, base, base.type.builtin_type, call->function_name, arguments);
} else {
gen->write_call(result, base, call->function_name, arguments);
@ -965,7 +964,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
return GDScriptCodeGenerator::Address();
}
if (test_type.has_type) {
if (test_type.has_type()) {
gen->write_type_test(result, operand, test_type);
} else {
gen->write_assign_true(result);
@ -1065,7 +1064,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
// Get at (potential) root stack pos, so it can be returned.
GDScriptCodeGenerator::Address base = _parse_expression(codegen, r_error, chain.back()->get()->base);
const bool base_known_type = base.type.has_type;
const bool base_known_type = base.type.has_type();
const bool base_is_shared = Variant::is_type_shared(base.type.builtin_type);
if (r_error) {
@ -1169,7 +1168,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
// Set back the values into their bases.
for (const ChainInfo &info : set_chain) {
bool known_type = assigned.type.has_type;
bool known_type = assigned.type.has_type();
bool is_shared = Variant::is_type_shared(assigned.type.builtin_type);
if (!known_type || !is_shared) {
@ -1195,7 +1194,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
assigned = info.base;
}
bool known_type = assigned.type.has_type;
bool known_type = assigned.type.has_type();
bool is_shared = Variant::is_type_shared(assigned.type.builtin_type);
if (!known_type || !is_shared) {
@ -1437,7 +1436,6 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c
// Equality is always a boolean.
GDScriptDataType equality_type;
equality_type.has_type = true;
equality_type.kind = GDScriptDataType::BUILTIN;
equality_type.builtin_type = Variant::BOOL;
@ -1518,7 +1516,6 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c
// Equality is always a boolean.
GDScriptDataType equality_type;
equality_type.has_type = true;
equality_type.kind = GDScriptDataType::BUILTIN;
equality_type.builtin_type = Variant::BOOL;
@ -1603,7 +1600,6 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c
// Equality is always a boolean.
GDScriptDataType temp_type;
temp_type.has_type = true;
temp_type.kind = GDScriptDataType::BUILTIN;
temp_type.builtin_type = Variant::BOOL;
@ -1701,7 +1697,6 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_match_pattern(CodeGen &c
// Equality is always a boolean.
GDScriptDataType temp_type;
temp_type.has_type = true;
temp_type.kind = GDScriptDataType::BUILTIN;
temp_type.builtin_type = Variant::BOOL;
@ -1921,7 +1916,6 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui
// Then, let's save the type of the value in the stack too, so we can reuse for later comparisons.
GDScriptDataType typeof_type;
typeof_type.has_type = true;
typeof_type.kind = GDScriptDataType::BUILTIN;
typeof_type.builtin_type = Variant::INT;
GDScriptCodeGenerator::Address type = codegen.add_local("@match_type", typeof_type);
@ -2186,7 +2180,7 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui
codegen.generator->pop_temporary();
}
initialized = true;
} else if ((local_type.has_type && local_type.kind == GDScriptDataType::BUILTIN) || codegen.generator->is_local_dirty(local)) {
} else if (local_type.kind == GDScriptDataType::BUILTIN || codegen.generator->is_local_dirty(local)) {
// Initialize with default for the type. Built-in types must always be cleared (they cannot be `null`).
// Objects and untyped variables are assigned to `null` only if the stack address has been re-used and not cleared.
codegen.generator->clear_address(local);
@ -2251,7 +2245,6 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
bool is_static = false;
Variant rpc_config;
GDScriptDataType return_type;
return_type.has_type = true;
return_type.kind = GDScriptDataType::BUILTIN;
return_type.builtin_type = Variant::NIL;
@ -2321,7 +2314,7 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
}
GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
if (field_type.has_type) {
if (field_type.has_type()) {
codegen.generator->write_newline(field->start_line);
GDScriptCodeGenerator::Address dst_address(GDScriptCodeGenerator::Address::MEMBER, codegen.script->member_indices[field->identifier->name].index, field_type);
@ -2461,7 +2454,6 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
method_info.return_val = p_func->get_datatype().to_property_info(String());
} else {
gd_function->return_type = GDScriptDataType();
gd_function->return_type.has_type = true;
gd_function->return_type.kind = GDScriptDataType::BUILTIN;
gd_function->return_type.builtin_type = Variant::NIL;
}
@ -2490,7 +2482,6 @@ GDScriptFunction *GDScriptCompiler::_make_static_initializer(Error &r_error, GDS
bool is_static = true;
Variant rpc_config;
GDScriptDataType return_type;
return_type.has_type = true;
return_type.kind = GDScriptDataType::BUILTIN;
return_type.builtin_type = Variant::NIL;
@ -2516,7 +2507,7 @@ GDScriptFunction *GDScriptCompiler::_make_static_initializer(Error &r_error, GDS
}
GDScriptDataType field_type = _gdtype_from_datatype(field->get_datatype(), codegen.script);
if (field_type.has_type) {
if (field_type.has_type()) {
codegen.generator->write_newline(field->start_line);
if (field_type.builtin_type == Variant::ARRAY && field_type.has_container_element_type(0)) {
@ -2802,7 +2793,7 @@ Error GDScriptCompiler::_prepare_compilation(GDScript *p_script, const GDScriptP
PropertyInfo export_info = variable->export_info;
if (variable->exported) {
if (!minfo.data_type.has_type) {
if (!minfo.data_type.has_type()) {
prop_info.type = export_info.type;
prop_info.class_name = export_info.class_name;
}
@ -2997,7 +2988,6 @@ Error GDScriptCompiler::_compile_class(GDScript *p_script, const GDScriptParser:
p_script->placeholders.erase(psi); //remove placeholder
GDScriptInstance *instance = memnew(GDScriptInstance);
instance->base_ref_counted = Object::cast_to<RefCounted>(E->get());
instance->members.resize(p_script->member_indices.size());
instance->script = Ref<GDScript>(p_script);
instance->owner = E->get();

View File

@ -102,7 +102,6 @@ class GDScriptCompiler {
GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) {
GDScriptDataType type;
type.has_type = true;
type.kind = GDScriptDataType::BUILTIN;
type.builtin_type = p_constant.get_type();
if (type.builtin_type == Variant::OBJECT) {

View File

@ -49,29 +49,27 @@ public:
Vector<GDScriptDataType> container_element_types;
enum Kind {
UNINITIALIZED,
VARIANT, // Can be any type.
BUILTIN,
NATIVE,
SCRIPT,
GDSCRIPT,
};
Kind kind = UNINITIALIZED;
Kind kind = VARIANT;
bool has_type = false;
Variant::Type builtin_type = Variant::NIL;
StringName native_type;
Script *script_type = nullptr;
Ref<Script> script_type_ref;
bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
if (!has_type) {
return true; // Can't type check
}
_FORCE_INLINE_ bool has_type() const { return kind != VARIANT; }
bool is_type(const Variant &p_variant, bool p_allow_implicit_conversion = false) const {
switch (kind) {
case UNINITIALIZED:
break;
case VARIANT: {
return true;
} break;
case BUILTIN: {
Variant::Type var_type = p_variant.get_type();
bool valid = builtin_type == var_type;
@ -183,7 +181,7 @@ public:
}
bool can_contain_object() const {
if (has_type && kind == BUILTIN) {
if (kind == BUILTIN) {
switch (builtin_type) {
case Variant::ARRAY:
if (has_container_element_type(0)) {
@ -237,7 +235,6 @@ public:
void operator=(const GDScriptDataType &p_other) {
kind = p_other.kind;
has_type = p_other.has_type;
builtin_type = p_other.builtin_type;
native_type = p_other.native_type;
script_type = p_other.script_type;

View File

@ -553,7 +553,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
stack = (Variant *)aptr;
for (int i = 0; i < p_argcount; i++) {
if (!argument_types[i].has_type) {
if (!argument_types[i].has_type()) {
memnew_placement(&stack[i + 3], Variant(*p_args[i]));
continue;
}