Skip to content

Commit

Permalink
feat(rivetc): support export annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 3, 2023
1 parent d894d6d commit 159435a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
21 changes: 18 additions & 3 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,20 @@ def gen_decl(self, decl):
)
self.generated_array_returns.append(name)
ret_typ = ir.Type(name)
if decl.is_extern and not decl.has_body:
name = decl.sym.name
elif (not decl.is_method) and decl.annotations.has("export"):
export_annotation = decl.annotations.find("export")
if isinstance(export_annotation.args[0].expr, ast.StringLiteral):
name = export_annotation.args[0].expr.lit
else:
assert False
else:
name = mangle_symbol(decl.sym)
fn_decl = ir.FuncDecl(
False, decl.annotations, decl.is_extern and not decl.has_body,
decl.sym.name if decl.is_extern and not decl.has_body else
mangle_symbol(decl.sym), args, decl.is_variadic
and decl.is_extern, ret_typ, decl.ret_typ == self.comp.never_t
name, args, decl.is_variadic and decl.is_extern, ret_typ,
decl.ret_typ == self.comp.never_t
)
self.cur_fn = fn_decl
self.cur_fn.arr_ret_struct = arr_ret_struct
Expand Down Expand Up @@ -1264,6 +1273,12 @@ def gen_expr(self, expr, custom_tmp = None):
if not is_vtable_call:
if expr.is_closure:
name = self.gen_expr_with_cast(expr.left.typ, expr.left)
elif (not expr.sym.is_method) and expr.sym.annotations.has("export"):
export_annotation = expr.sym.annotations.find("export")
if isinstance(export_annotation.args[0].expr, ast.StringLiteral):
name = export_annotation.args[0].expr.lit
else:
assert False
elif expr.sym.is_extern and expr.sym.abi != sym.ABI.Rivet and not expr.sym.has_body:
name = ir.Name(expr.sym.name)
else:
Expand Down
2 changes: 1 addition & 1 deletion rivetc/src/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def walk_decls(self, decls):
decl.is_unsafe, decl.is_method, decl.is_variadic,
decl.name, decl.args, decl.ret_typ,
decl.has_named_args, decl.has_body, decl.name_pos,
decl.self_is_mut, decl.self_is_ptr
decl.self_is_mut, decl.self_is_ptr, annotations=decl.annotations
)
)
decl.sym.is_main = decl.is_main
Expand Down
3 changes: 2 additions & 1 deletion rivetc/src/sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ class Fn(Sym):
def __init__(
self, abi, is_public, is_extern, is_unsafe, is_method, is_variadic,
name, args, ret_typ, has_named_args, has_body, name_pos, self_is_mut,
self_is_ptr, self_typ = None
self_is_ptr, self_typ = None, annotations = None
):
Sym.__init__(self, is_public, name)
self.is_main = False
Expand All @@ -642,6 +642,7 @@ def __init__(
self.has_named_args = has_named_args
self.has_body = has_body
self.name_pos = name_pos
self.annotations = annotations

def get_arg(self, idx):
arg = self.args[idx]
Expand Down

0 comments on commit 159435a

Please sign in to comment.