diff options
author | George Marques <george@gmarqu.es> | 2022-02-02 13:57:24 -0300 |
---|---|---|
committer | George Marques <george@gmarqu.es> | 2022-02-03 13:32:34 -0300 |
commit | ceafdf347e5ecc050629fd4eac93030dabb0d0e9 (patch) | |
tree | aee0a540ef38b6815561697312fad720912b17d6 /modules/gdscript/gdscript_compiler.cpp | |
parent | b013c0d544e784392020aa693fd248fa52f450c2 (diff) | |
download | redot-engine-ceafdf347e5ecc050629fd4eac93030dabb0d0e9.tar.gz |
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.
Diffstat (limited to 'modules/gdscript/gdscript_compiler.cpp')
-rw-r--r-- | modules/gdscript/gdscript_compiler.cpp | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index e0b2c0dae1..108c988add 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -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); |