From 6080094c4110a5932c700698b2efa32df539235f Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 15 Sep 2020 14:39:31 -0700 Subject: [PATCH] Require mangled names for global variables. --- code/compiler/13/ast.cpp | 12 ++++-------- code/compiler/13/env.cpp | 4 ++-- code/compiler/13/type_env.cpp | 4 ++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/code/compiler/13/ast.cpp b/code/compiler/13/ast.cpp index 01b7413..2305430 100644 --- a/code/compiler/13/ast.cpp +++ b/code/compiler/13/ast.cpp @@ -55,15 +55,11 @@ void ast_lid::translate(global_scope& scope) { } void ast_lid::compile(const env_ptr& env, std::vector& into) const { - auto mangled_name = this->env->get_mangled_name(id); - - // Local names shouldn't need mangling. - assert(!(mangled_name != id && !this->env->is_global(id))); - into.push_back(instruction_ptr( - (env->has_variable(mangled_name) && !this->env->is_global(id)) ? - (instruction*) new instruction_push(env->get_offset(id)) : - (instruction*) new instruction_pushglobal(mangled_name))); + (this->env->is_global(id)) ? + (instruction*) new instruction_pushglobal(id) : + (instruction*) new instruction_push( + env->get_offset(this->env->get_mangled_name(id))))); } void ast_uid::print(int indent, std::ostream& to) const { diff --git a/code/compiler/13/env.cpp b/code/compiler/13/env.cpp index 74b1650..8bf4401 100644 --- a/code/compiler/13/env.cpp +++ b/code/compiler/13/env.cpp @@ -3,7 +3,7 @@ int env_var::get_offset(const std::string& name) const { if(name == this->name) return 0; - assert(parent); + assert(parent != nullptr); return parent->get_offset(name) + 1; } @@ -14,7 +14,7 @@ bool env_var::has_variable(const std::string& name) const { } int env_offset::get_offset(const std::string& name) const { - assert(parent); + assert(parent != nullptr); return parent->get_offset(name) + offset; } diff --git a/code/compiler/13/type_env.cpp b/code/compiler/13/type_env.cpp index 5070f8c..07f9a34 100644 --- a/code/compiler/13/type_env.cpp +++ b/code/compiler/13/type_env.cpp @@ -46,8 +46,8 @@ const std::string& type_env::get_mangled_name(const std::string& name) const { auto it = names.find(name); if(it != names.end()) return (it->second.mangled_name != "") ? it->second.mangled_name : name; - if(parent) return parent->get_mangled_name(name); - return name; + assert(parent != nullptr); + return parent->get_mangled_name(name); } type_ptr type_env::lookup_type(const std::string& name) const {