From f1d3b30a45f31c217929ec86b010736feed1714c Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Fri, 2 May 2014 12:40:34 -0300 Subject: [PATCH] Fixes to constants in scope --- modules/gdscript/gd_compiler.cpp | 62 ++++++++++++++++++-------------- modules/gdscript/gd_script.cpp | 17 +++++---- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/modules/gdscript/gd_compiler.cpp b/modules/gdscript/gd_compiler.cpp index 90c83c201bdc..48f009282721 100644 --- a/modules/gdscript/gd_compiler.cpp +++ b/modules/gdscript/gd_compiler.cpp @@ -185,51 +185,59 @@ int GDCompiler::_parse_expression(CodeGen& codegen,const GDParser::Node *p_expre //TRY CLASS CONSTANTS - GDScript *scr = codegen.script; - GDNativeClass *nc=NULL; - while(scr) { + GDScript *owner = codegen.script; + while (owner) { - if (scr->constants.has(identifier)) { + GDScript *scr = owner; + GDNativeClass *nc=NULL; + while(scr) { - //int idx=scr->constants[identifier]; - int idx = codegen.get_name_map_pos(identifier); - return idx|(GDFunction::ADDR_TYPE_CLASS_CONSTANT<constants.has(identifier)) { + + //int idx=scr->constants[identifier]; + int idx = codegen.get_name_map_pos(identifier); + return idx|(GDFunction::ADDR_TYPE_CLASS_CONSTANT<native.is_valid()) + nc=scr->native.ptr(); + scr=scr->_base; } - if (scr->native.is_valid()) - nc=scr->native.ptr(); - scr=scr->_base; - } - // CLASS C++ Integer Constant + // CLASS C++ Integer Constant - if (nc) { + if (nc) { - bool success=false; - int constant = ObjectTypeDB::get_integer_constant(nc->get_name(),identifier,&success); - if (success) { - Variant key=constant; - int idx; + bool success=false; + int constant = ObjectTypeDB::get_integer_constant(nc->get_name(),identifier,&success); + if (success) { + Variant key=constant; + int idx; - if (!codegen.constant_map.has(key)) { + if (!codegen.constant_map.has(key)) { - idx=codegen.constant_map.size(); - codegen.constant_map[key]=idx; + idx=codegen.constant_map.size(); + codegen.constant_map[key]=idx; - } else { - idx=codegen.constant_map[key]; + } else { + idx=codegen.constant_map[key]; + } + + return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<_owner; } - if (codegen.script->subclasses.has(identifier)) { + /* + handled in constants now + if (codegen.script->subclasses.has(identifier)) { //same with a subclass, make it a local constant. int idx = codegen.get_constant_pos(codegen.script->subclasses[identifier]); return idx|(GDFunction::ADDR_TYPE_LOCAL_CONSTANT<get_global_map().has(identifier)) { @@ -1457,6 +1465,8 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars Error err = _parse_class(subclass.ptr(),p_script,p_class->subclasses[i]); if (err) return err; + + p_script->constants.insert(name,subclass); //once parsed, goes to the list of constants p_script->subclasses.insert(name,subclass); } diff --git a/modules/gdscript/gd_script.cpp b/modules/gdscript/gd_script.cpp index 299a7d3e54c6..65b10399f89d 100644 --- a/modules/gdscript/gd_script.cpp +++ b/modules/gdscript/gd_script.cpp @@ -76,16 +76,21 @@ Variant *GDFunction::_get_variant(int p_address,GDInstance *p_instance,GDScript case ADDR_TYPE_CLASS_CONSTANT: { //todo change to index! - GDScript *s=p_script; + GDScript *o=p_script; ERR_FAIL_INDEX_V(address,_global_names_count,NULL); const StringName *sn = &_global_names_ptr[address]; - while(s) { - Map::Element *E=s->constants.find(*sn); - if (E) { - return &E->get(); + while(o) { + GDScript *s=o; + while(s) { + + Map::Element *E=s->constants.find(*sn); + if (E) { + return &E->get(); + } + s=s->_base; } - s=s->_base; + o=o->_owner; }