Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions interop/klr/gather.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,26 @@ static void add_global(struct state *st, lean_object *name, PyObject *obj) {
st->defs = def;
}

// Create a global alias that references another name
// Used for import aliases: e.g., "dma_copy = nki.isa.neuron_isa.dma_copy"
static void add_global_alias(struct state *st, const char *alias_name, lean_object *target_name) {
if (!alias_name || !target_name || have_def(st, alias_name))
return;

struct definition *def = region_alloc(st->region, sizeof(*def));
lean_object *pos = curPos(st);

lean_object *ref_expr = Python_Expr_mk(Python_Expr_name(target_name, Python_Ctx_load), pos);

def->str = lean_mk_string(alias_name);
def->name = alias_name;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

def->name = lean_string_cstr(def->str);

name and str should refer to the same Lean object.
I think it may not matter, but this is what the comments say is true about this structure.

def->type = GLOBAL;
def->obj = Python_Keyword_mk(mkSome(def->str), ref_expr, pos);

def->next = st->defs;
st->defs = def;
}

// Lookup item `id` in dictionary `name` which should be an attribute of `obj`.
// e.g. f.name['id']
static PyObject* lookup_(PyObject *obj, const char *name, PyObject *id) {
Expand Down Expand Up @@ -1626,7 +1646,8 @@ static void definition(struct state *st, PyObject *obj, char* suggested_name) {
if (st->scope.f) {
function(st, name, stmt);
if (suggested_name && strcmp(lean_string_cstr(name), suggested_name) != 0) {
function(st, lean_mk_string(suggested_name), stmt);
lean_inc(name);
add_global_alias(st, suggested_name, name);
}
}
break;
Expand All @@ -1635,7 +1656,8 @@ static void definition(struct state *st, PyObject *obj, char* suggested_name) {
if (st->scope.cls) {
class(st, name, stmt);
if (suggested_name && strcmp(lean_string_cstr(name), suggested_name) != 0) {
class(st, lean_mk_string(suggested_name), stmt);
lean_inc(name);
add_global_alias(st, suggested_name, name);
}
}
break;
Expand Down
Loading