Allow shadowing class members with local variables in GDScript, closes #15896
This commit is contained in:
parent
8f8b032eed
commit
c58891ff4c
|
@ -37,6 +37,9 @@ bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringN
|
|||
if (!codegen.function_node || codegen.function_node->_static)
|
||||
return false;
|
||||
|
||||
if (codegen.stack_identifiers.has(p_name))
|
||||
return false; //shadowed
|
||||
|
||||
return _is_class_member_property(codegen.script, p_name);
|
||||
}
|
||||
|
||||
|
@ -184,6 +187,14 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
|
|||
|
||||
StringName identifier = in->name;
|
||||
|
||||
// TRY STACK!
|
||||
if (!p_initializer && codegen.stack_identifiers.has(identifier)) {
|
||||
|
||||
int pos = codegen.stack_identifiers[identifier];
|
||||
return pos | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS);
|
||||
}
|
||||
|
||||
// TRY CLASS MEMBER
|
||||
if (_is_class_member_property(codegen, identifier)) {
|
||||
//get property
|
||||
codegen.opcodes.push_back(GDScriptFunction::OPCODE_GET_MEMBER); // perform operator
|
||||
|
@ -194,12 +205,6 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
|
|||
return dst_addr;
|
||||
}
|
||||
|
||||
// TRY STACK!
|
||||
if (!p_initializer && codegen.stack_identifiers.has(identifier)) {
|
||||
|
||||
int pos = codegen.stack_identifiers[identifier];
|
||||
return pos | (GDScriptFunction::ADDR_TYPE_STACK_VARIABLE << GDScriptFunction::ADDR_BITS);
|
||||
}
|
||||
//TRY MEMBERS!
|
||||
if (!codegen.function_node || !codegen.function_node->_static) {
|
||||
|
||||
|
@ -1336,10 +1341,12 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
|
|||
|
||||
const GDScriptParser::LocalVarNode *lv = static_cast<const GDScriptParser::LocalVarNode *>(s);
|
||||
|
||||
if (_is_class_member_property(codegen, lv->name)) {
|
||||
_set_error("Name for local variable '" + String(lv->name) + "' can't shadow class property of the same name.", lv);
|
||||
return ERR_ALREADY_EXISTS;
|
||||
}
|
||||
// since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
|
||||
//
|
||||
//if (_is_class_member_property(codegen, lv->name)) {
|
||||
// _set_error("Name for local variable '" + String(lv->name) + "' can't shadow class property of the same name.", lv);
|
||||
// return ERR_ALREADY_EXISTS;
|
||||
//}
|
||||
|
||||
codegen.add_stack_identifier(lv->name, p_stack_level++);
|
||||
codegen.alloc_stack(p_stack_level);
|
||||
|
@ -1376,10 +1383,13 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
|
|||
|
||||
if (p_func) {
|
||||
for (int i = 0; i < p_func->arguments.size(); i++) {
|
||||
if (_is_class_member_property(p_script, p_func->arguments[i])) {
|
||||
_set_error("Name for argument '" + String(p_func->arguments[i]) + "' can't shadow class property of the same name.", p_func);
|
||||
return ERR_ALREADY_EXISTS;
|
||||
}
|
||||
// since we are using properties now for most class access, allow shadowing of class members to make user's life easier.
|
||||
//
|
||||
//if (_is_class_member_property(p_script, p_func->arguments[i])) {
|
||||
// _set_error("Name for argument '" + String(p_func->arguments[i]) + "' can't shadow class property of the same name.", p_func);
|
||||
// return ERR_ALREADY_EXISTS;
|
||||
//}
|
||||
|
||||
codegen.add_stack_identifier(p_func->arguments[i], i);
|
||||
#ifdef TOOLS_ENABLED
|
||||
argnames.push_back(p_func->arguments[i]);
|
||||
|
|
Loading…
Reference in a new issue