mirror of
https://github.com/godotengine/godot.git
synced 2025-10-26 19:24:18 +00:00
GDScript: Treat enum values as int and enum types as dictionary
Since enums resolve to a dictionary at runtime, calling dictionary methods on an enum type is a valid use case. This ensures this is true by adding test cases. This also makes enum values be treated as ints when used in operations.
This commit is contained in:
parent
b013c0d544
commit
ceafdf347e
6 changed files with 127 additions and 31 deletions
|
|
@ -143,7 +143,11 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
|
|||
case GDScriptParser::DataType::ENUM:
|
||||
result.has_type = true;
|
||||
result.kind = GDScriptDataType::BUILTIN;
|
||||
result.builtin_type = Variant::INT;
|
||||
if (p_datatype.is_meta_type) {
|
||||
result.builtin_type = Variant::DICTIONARY;
|
||||
} else {
|
||||
result.builtin_type = Variant::INT;
|
||||
}
|
||||
break;
|
||||
case GDScriptParser::DataType::UNRESOLVED: {
|
||||
ERR_PRINT("Parser bug: converting unresolved type.");
|
||||
|
|
@ -468,7 +472,14 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
|
|||
} break;
|
||||
case GDScriptParser::Node::CAST: {
|
||||
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
|
||||
GDScriptDataType cast_type = _gdtype_from_datatype(cn->cast_type->get_datatype());
|
||||
GDScriptParser::DataType og_cast_type = cn->cast_type->get_datatype();
|
||||
GDScriptDataType cast_type = _gdtype_from_datatype(og_cast_type);
|
||||
|
||||
if (og_cast_type.kind == GDScriptParser::DataType::ENUM) {
|
||||
// Enum types are usually treated as dictionaries, but in this case we want to cast to an integer.
|
||||
cast_type.kind = GDScriptDataType::BUILTIN;
|
||||
cast_type.builtin_type = Variant::INT;
|
||||
}
|
||||
|
||||
// Create temporary for result first since it will be deleted last.
|
||||
GDScriptCodeGenerator::Address result = codegen.add_temporary(cast_type);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue