Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly store names of function definitions/declarations #47

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 2 additions & 6 deletions guppy/hugr/hugr.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,11 @@ def add_def(
self, fun_ty: FunctionType, parent: Optional[Node], name: str
) -> DFContainingVNode:
"""Adds a `Def` node to the graph."""
return self._add_dfg_node(
ops.FuncDefn(), [], [fun_ty], parent, None, meta_data={"name": name}
)
return self._add_dfg_node(ops.FuncDefn(name=name), [], [fun_ty], parent, None)

def add_declare(self, fun_ty: FunctionType, parent: Node, name: str) -> VNode:
"""Adds a `Declare` node to the graph."""
return self.add_node(
ops.FuncDecl(), [], [fun_ty], parent, None, meta_data={"name": name}
)
return self.add_node(ops.FuncDecl(name=name), [], [fun_ty], parent, None)

def add_edge(self, src_port: OutPort, tgt_port: InPort) -> None:
"""Adds an edge between two ports."""
Expand Down
4 changes: 2 additions & 2 deletions guppy/hugr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FuncDefn(BaseOp):

op: Literal["FuncDefn"] = "FuncDefn"

name: str = "main"
name: str
signature: FunctionType = Field(default_factory=FunctionType.empty)

def insert_port_types(self, in_types: TypeRow, out_types: TypeRow) -> None:
Expand All @@ -74,7 +74,7 @@ class FuncDecl(BaseOp):
"""External function declaration, linked at runtime."""

op: Literal["FuncDecl"] = "FuncDecl"
name: str = "main"
name: str
signature: FunctionType = Field(default_factory=FunctionType.empty)

def insert_port_types(self, in_types: TypeRow, out_types: TypeRow) -> None:
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from guppy.compiler import guppy
from guppy.hugr import ops


def test_id(validate):
Expand Down Expand Up @@ -57,3 +58,21 @@ def foo(x: bool) -> bool:
return y

validate(foo)


def test_func_def_name():
@guppy
def func_name() -> None:
return

[def_op] = [n.op for n in func_name.nodes() if isinstance(n.op, ops.FuncDefn)]
assert def_op.name == "func_name"


def test_func_decl_name():
@guppy
def func_name() -> None:
...

[def_op] = [n.op for n in func_name.nodes() if isinstance(n.op, ops.FuncDecl)]
assert def_op.name == "func_name"