Skip to content

Commit cbcf60c

Browse files
committed
avoid duplicated definition via aliasing
1 parent 8dcd5a3 commit cbcf60c

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

interop/klr/gather.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,26 @@ static void add_global(struct state *st, lean_object *name, PyObject *obj) {
666666
st->defs = def;
667667
}
668668

669+
// Create a global alias that references another name
670+
// Used for import aliases: e.g., "dma_copy = nki.isa.neuron_isa.dma_copy"
671+
static void add_global_alias(struct state *st, const char *alias_name, lean_object *target_name) {
672+
if (!alias_name || !target_name || have_def(st, alias_name))
673+
return;
674+
675+
struct definition *def = region_alloc(st->region, sizeof(*def));
676+
lean_object *pos = curPos(st);
677+
678+
lean_object *ref_expr = Python_Expr_mk(Python_Expr_name(target_name, Python_Ctx_load), pos);
679+
680+
def->str = lean_mk_string(alias_name);
681+
def->name = alias_name;
682+
def->type = GLOBAL;
683+
def->obj = Python_Keyword_mk(mkSome(def->str), ref_expr, pos);
684+
685+
def->next = st->defs;
686+
st->defs = def;
687+
}
688+
669689
// Lookup item `id` in dictionary `name` which should be an attribute of `obj`.
670690
// e.g. f.name['id']
671691
static PyObject* lookup_(PyObject *obj, const char *name, PyObject *id) {
@@ -1626,7 +1646,8 @@ static void definition(struct state *st, PyObject *obj, char* suggested_name) {
16261646
if (st->scope.f) {
16271647
function(st, name, stmt);
16281648
if (suggested_name && strcmp(lean_string_cstr(name), suggested_name) != 0) {
1629-
function(st, lean_mk_string(suggested_name), stmt);
1649+
lean_inc(name);
1650+
add_global_alias(st, suggested_name, name);
16301651
}
16311652
}
16321653
break;
@@ -1635,7 +1656,8 @@ static void definition(struct state *st, PyObject *obj, char* suggested_name) {
16351656
if (st->scope.cls) {
16361657
class(st, name, stmt);
16371658
if (suggested_name && strcmp(lean_string_cstr(name), suggested_name) != 0) {
1638-
class(st, lean_mk_string(suggested_name), stmt);
1659+
lean_inc(name);
1660+
add_global_alias(st, suggested_name, name);
16391661
}
16401662
}
16411663
break;

0 commit comments

Comments
 (0)