Fix lookup symbol for enum members to search a correct code definition

This commit is contained in:
Chaosus 2024-08-06 17:56:09 +03:00
parent 4c4e673344
commit 54f6a1bf64
2 changed files with 25 additions and 4 deletions

View file

@ -148,6 +148,15 @@ static GDScriptParser::DataType make_enum_type(const StringName &p_enum_name, co
return type; return type;
} }
static GDScriptParser::DataType make_class_enum_type(const StringName &p_enum_name, GDScriptParser::ClassNode *p_class, const String &p_script_path, bool p_meta = true) {
GDScriptParser::DataType type = make_enum_type(p_enum_name, p_class->fqcn, p_meta);
type.class_type = p_class;
type.script_path = p_script_path;
return type;
}
static GDScriptParser::DataType make_native_enum_type(const StringName &p_enum_name, const StringName &p_native_class, bool p_meta = true) { static GDScriptParser::DataType make_native_enum_type(const StringName &p_enum_name, const StringName &p_native_class, bool p_meta = true) {
// Find out which base class declared the enum, so the name is always the same even when coming from other contexts. // Find out which base class declared the enum, so the name is always the same even when coming from other contexts.
StringName native_base = p_native_class; StringName native_base = p_native_class;
@ -1101,7 +1110,7 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class,
check_class_member_name_conflict(p_class, member.m_enum->identifier->name, member.m_enum); check_class_member_name_conflict(p_class, member.m_enum->identifier->name, member.m_enum);
member.m_enum->set_datatype(resolving_datatype); member.m_enum->set_datatype(resolving_datatype);
GDScriptParser::DataType enum_type = make_enum_type(member.m_enum->identifier->name, p_class->fqcn, true); GDScriptParser::DataType enum_type = make_class_enum_type(member.m_enum->identifier->name, p_class, parser->script_path, true);
const GDScriptParser::EnumNode *prev_enum = current_enum; const GDScriptParser::EnumNode *prev_enum = current_enum;
current_enum = member.m_enum; current_enum = member.m_enum;
@ -1194,7 +1203,7 @@ void GDScriptAnalyzer::resolve_class_member(GDScriptParser::ClassNode *p_class,
// Also update the original references. // Also update the original references.
member.enum_value.parent_enum->values.set(member.enum_value.index, member.enum_value); member.enum_value.parent_enum->values.set(member.enum_value.index, member.enum_value);
member.enum_value.identifier->set_datatype(make_enum_type(UNNAMED_ENUM, p_class->fqcn, false)); member.enum_value.identifier->set_datatype(make_class_enum_type(UNNAMED_ENUM, p_class, parser->script_path, false));
} break; } break;
case GDScriptParser::ClassNode::Member::CLASS: case GDScriptParser::ClassNode::Member::CLASS:
check_class_member_name_conflict(p_class, member.m_class->identifier->name, member.m_class); check_class_member_name_conflict(p_class, member.m_class->identifier->name, member.m_class);
@ -4249,7 +4258,7 @@ void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_ident
const GDScriptParser::EnumNode::Value &element = current_enum->values[i]; const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
if (element.identifier->name == p_identifier->name) { if (element.identifier->name == p_identifier->name) {
StringName enum_name = current_enum->identifier ? current_enum->identifier->name : UNNAMED_ENUM; StringName enum_name = current_enum->identifier ? current_enum->identifier->name : UNNAMED_ENUM;
GDScriptParser::DataType type = make_enum_type(enum_name, parser->current_class->fqcn, false); GDScriptParser::DataType type = make_class_enum_type(enum_name, parser->current_class, parser->script_path, false);
if (element.parent_enum->identifier) { if (element.parent_enum->identifier) {
type.enum_type = element.parent_enum->identifier->name; type.enum_type = element.parent_enum->identifier->name;
} }

View file

@ -3782,7 +3782,19 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co
} }
} break; } break;
case GDScriptParser::DataType::ENUM: { case GDScriptParser::DataType::ENUM: {
if (base_type.enum_values.has(p_symbol)) { if (base_type.class_type && base_type.class_type->has_member(base_type.enum_type)) {
GDScriptParser::EnumNode *base_enum = base_type.class_type->get_member(base_type.enum_type).m_enum;
for (const GDScriptParser::EnumNode::Value &value : base_enum->values) {
if (value.identifier && value.identifier->name == p_symbol) {
r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
r_result.class_path = base_type.script_path;
r_result.location = value.line;
Error err = OK;
r_result.script = GDScriptCache::get_shallow_script(r_result.class_path, err);
return err;
}
}
} else if (base_type.enum_values.has(p_symbol)) {
r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT; r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
r_result.class_name = String(base_type.native_type).get_slicec('.', 0); r_result.class_name = String(base_type.native_type).get_slicec('.', 0);
r_result.class_member = p_symbol; r_result.class_member = p_symbol;