Merge pull request #42943 from vnen/gdscript-local-scope-fix

Fix handling of scope for local GDScript variables
This commit is contained in:
Rémi Verschelde 2020-10-20 21:00:57 +02:00 committed by GitHub
commit ef62e93677
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,12 +51,11 @@ class GDScriptCompiler {
GDScriptCodeGenerator *generator = nullptr; GDScriptCodeGenerator *generator = nullptr;
Map<StringName, GDScriptCodeGenerator::Address> parameters; Map<StringName, GDScriptCodeGenerator::Address> parameters;
Map<StringName, GDScriptCodeGenerator::Address> locals; Map<StringName, GDScriptCodeGenerator::Address> locals;
List<Set<StringName>> locals_in_scope; List<Map<StringName, GDScriptCodeGenerator::Address>> locals_stack;
GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) { GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) {
uint32_t addr = generator->add_local(p_name, p_type); uint32_t addr = generator->add_local(p_name, p_type);
locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type); locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type);
locals_in_scope.back()->get().insert(p_name);
return locals[p_name]; return locals[p_name];
} }
@ -102,17 +101,14 @@ class GDScriptCompiler {
} }
void start_block() { void start_block() {
Set<StringName> scope; Map<StringName, GDScriptCodeGenerator::Address> old_locals = locals;
locals_in_scope.push_back(scope); locals_stack.push_back(old_locals);
generator->start_block(); generator->start_block();
} }
void end_block() { void end_block() {
Set<StringName> &scope = locals_in_scope.back()->get(); locals = locals_stack.back()->get();
for (Set<StringName>::Element *E = scope.front(); E; E = E->next()) { locals_stack.pop_back();
locals.erase(E->get());
}
locals_in_scope.pop_back();
generator->end_block(); generator->end_block();
} }
}; };