Skip to content
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
19 changes: 8 additions & 11 deletions hugr-core/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct Context<'a> {
// that ensures that the `node_to_id` and `id_to_node` maps stay in sync.
}

const NO_VIS: Option<Visibility> = None;

impl<'a> Context<'a> {
pub fn new(hugr: &'a Hugr, bump: &'a Bump) -> Self {
let mut module = table::Module::default();
Expand Down Expand Up @@ -331,7 +333,7 @@ impl<'a> Context<'a> {
OpType::FuncDefn(func) => self.with_local_scope(node_id, |this| {
let symbol = this.export_poly_func_type(
func.func_name(),
func.visibility().clone().into(),
Some(func.visibility().clone().into()),
func.signature(),
);
regions = this.bump.alloc_slice_copy(&[this.export_dfg(
Expand All @@ -345,7 +347,7 @@ impl<'a> Context<'a> {
OpType::FuncDecl(func) => self.with_local_scope(node_id, |this| {
let symbol = this.export_poly_func_type(
func.func_name(),
func.visibility().clone().into(),
Some(func.visibility().clone().into()),
func.signature(),
);
table::Operation::DeclareFunc(symbol)
Expand All @@ -354,10 +356,8 @@ impl<'a> Context<'a> {
OpType::AliasDecl(alias) => self.with_local_scope(node_id, |this| {
// TODO: We should support aliases with different types and with parameters
let signature = this.make_term_apply(model::CORE_TYPE, &[]);
// Visibility is not spec'd in hugr-core
let visibility = this.bump.alloc(Visibility::default()); // good to common up!?
let symbol = this.bump.alloc(table::Symbol {
visibility,
visibility: &NO_VIS, // not spec'd in hugr-core
name: &alias.name,
params: &[],
constraints: &[],
Expand All @@ -370,10 +370,8 @@ impl<'a> Context<'a> {
let value = this.export_type(&alias.definition);
// TODO: We should support aliases with different types and with parameters
let signature = this.make_term_apply(model::CORE_TYPE, &[]);
// Visibility is not spec'd in hugr-core
let visibility = this.bump.alloc(Visibility::default()); // good to common up!?
let symbol = this.bump.alloc(table::Symbol {
visibility,
visibility: &NO_VIS, // not spec'd in hugr-core
name: &alias.name,
params: &[],
constraints: &[],
Expand Down Expand Up @@ -548,8 +546,7 @@ impl<'a> Context<'a> {

let symbol = self.with_local_scope(node, |this| {
let name = this.make_qualified_name(opdef.extension_id(), opdef.name());
// Visibility of OpDef's has no effect
this.export_poly_func_type(name, Visibility::default(), poly_func_type)
this.export_poly_func_type(name, None, poly_func_type)
});

let meta = {
Expand Down Expand Up @@ -800,7 +797,7 @@ impl<'a> Context<'a> {
pub fn export_poly_func_type<RV: MaybeRV>(
&mut self,
name: &'a str,
visibility: Visibility,
visibility: Option<Visibility>,
t: &PolyFuncTypeBase<RV>,
) -> &'a table::Symbol<'a> {
let mut params = BumpVec::with_capacity_in(t.params().len(), self.bump);
Expand Down
20 changes: 10 additions & 10 deletions hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,12 +938,12 @@ impl<'a> Context<'a> {
node_data: &'a table::Node<'a>,
parent: Node,
) -> Result<Node, ImportError> {
let visibility = symbol.visibility.clone().ok_or(ImportErrorInner::Invalid(
"No visibility for FuncDefn".to_string(),
))?;
self.import_poly_func_type(node_id, *symbol, |ctx, signature| {
let optype = OpType::FuncDefn(FuncDefn::new_vis(
symbol.name,
signature,
symbol.visibility.clone().into(),
));
let optype =
OpType::FuncDefn(FuncDefn::new_vis(symbol.name, signature, visibility.into()));

let node = ctx.make_node(node_id, optype, parent)?;

Expand All @@ -967,12 +967,12 @@ impl<'a> Context<'a> {
symbol: &'a table::Symbol<'a>,
parent: Node,
) -> Result<Node, ImportError> {
let visibility = symbol.visibility.clone().ok_or(ImportErrorInner::Invalid(
"No visibility for FuncDecl".to_string(),
))?;
self.import_poly_func_type(node_id, *symbol, |ctx, signature| {
let optype = OpType::FuncDecl(FuncDecl::new_vis(
symbol.name,
signature,
symbol.visibility.clone().into(),
));
let optype =
OpType::FuncDecl(FuncDecl::new_vis(symbol.name, signature, visibility.into()));
let node = ctx.make_node(node_id, optype, parent)?;
Ok(node)
})
Expand Down
1 change: 1 addition & 0 deletions hugr-core/tests/snapshots/model__roundtrip_add.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: ast
"addition modulo 2^N (signed and unsigned versions are the same op)")))

(define-func
public
example.add
(core.fn
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
Expand Down
5 changes: 4 additions & 1 deletion hugr-core/tests/snapshots/model__roundtrip_call.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call.edn\"))"
expression: ast
---
(hugr 0)

Expand All @@ -17,12 +17,14 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call
(import arithmetic.int.types.int)

(declare-func
public
example.callee
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])
(meta (compat.meta_json "description" "\"This is a function declaration.\""))
(meta (compat.meta_json "title" "\"Callee\"")))

(define-func
private
example.caller
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])
(meta
Expand All @@ -41,6 +43,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))))

(define-func
private
example.load
(core.fn [] [(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])])
(dfg [] [%0]
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/tests/snapshots/model__roundtrip_cfg.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ expression: ast

(import core.adt)

(define-func example.cfg_loop (param ?0 core.type) (core.fn [?0] [?0])
(define-func private example.cfg_loop (param ?0 core.type) (core.fn [?0] [?0])
(dfg [%0] [%1]
(signature (core.fn [?0] [?0]))
(cfg [%0] [%1]
Expand All @@ -30,7 +30,7 @@ expression: ast
((core.make_adt 0) [%4] [%5]
(signature (core.fn [?0] [(core.adt [[?0] [?0]])])))))))))

(define-func example.cfg_order (param ?0 core.type) (core.fn [?0] [?0])
(define-func public example.cfg_order (param ?0 core.type) (core.fn [?0] [?0])
(dfg [%0] [%1]
(signature (core.fn [?0] [?0]))
(cfg [%0] [%1]
Expand Down
1 change: 1 addition & 0 deletions hugr-core/tests/snapshots/model__roundtrip_cond.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: ast
"negation modulo 2^N (signed and unsigned versions are the same op)")))

(define-func
private
example.cond
(core.fn
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
Expand Down
13 changes: 10 additions & 3 deletions hugr-core/tests/snapshots/model__roundtrip_const.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-const.edn\"))"
expression: ast
---
(hugr 0)

Expand Down Expand Up @@ -28,7 +28,10 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons

(import core.adt)

(define-func example.bools (core.fn [] [(core.adt [[] []]) (core.adt [[] []])])
(define-func
public
example.bools
(core.fn [] [(core.adt [[] []]) (core.adt [[] []])])
(dfg [] [%0 %1]
(signature (core.fn [] [(core.adt [[] []]) (core.adt [[] []])]))
((core.load_const (core.const.adt [[] []] _ 0 [])) [] [%0]
Expand All @@ -37,6 +40,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
(signature (core.fn [] [(core.adt [[] []])])))))

(define-func
public
example.make-pair
(core.fn
[]
Expand Down Expand Up @@ -73,7 +77,10 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
[[(collections.array.array 5 (arithmetic.int.types.int 6))
arithmetic.float.types.float64]])])))))

(define-func example.f64-json (core.fn [] [arithmetic.float.types.float64])
(define-func
public
example.f64-json
(core.fn [] [arithmetic.float.types.float64])
(dfg [] [%0 %1]
(signature
(core.fn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-constraints.edn\"))"
expression: ast
---
(hugr 0)

Expand All @@ -17,13 +17,15 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
(import core.fn)

(declare-func
private
array.replicate
(param ?0 core.nat)
(param ?1 core.type)
(where (core.nonlinear ?1))
(core.fn [?1] [(collections.array.array ?0 ?1)]))

(declare-func
public
array.copy
(param ?0 core.nat)
(param ?1 core.type)
Expand All @@ -33,6 +35,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
[(collections.array.array ?0 ?1) (collections.array.array ?0 ?1)]))

(define-func
public
util.copy
(param ?0 core.type)
(where (core.nonlinear ?0))
Expand Down
6 changes: 3 additions & 3 deletions hugr-core/tests/snapshots/model__roundtrip_entrypoint.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expression: ast

(import core.entrypoint)

(define-func main (core.fn [] [])
(define-func public main (core.fn [] [])
(dfg (signature (core.fn [] [])) (meta core.entrypoint)))

(mod)
Expand All @@ -19,7 +19,7 @@ expression: ast

(import core.entrypoint)

(define-func wrapper_dfg (core.fn [] [])
(define-func private wrapper_dfg (core.fn [] [])
(dfg (signature (core.fn [] [])) (meta core.entrypoint)))

(mod)
Expand All @@ -34,7 +34,7 @@ expression: ast

(import core.adt)

(define-func wrapper_cfg (core.fn [] [])
(define-func public wrapper_cfg (core.fn [] [])
(dfg
(signature (core.fn [] []))
(cfg
Expand Down
4 changes: 2 additions & 2 deletions hugr-core/tests/snapshots/model__roundtrip_loop.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-loop.edn\"))"
expression: ast
---
(hugr 0)

Expand All @@ -14,7 +14,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-loop

(import core.adt)

(define-func example.loop (param ?0 core.type) (core.fn [?0] [?0])
(define-func private example.loop (param ?0 core.type) (core.fn [?0] [?0])
(dfg [%0] [%1]
(signature (core.fn [?0] [?0]))
(tail-loop [%0] [%1]
Expand Down
1 change: 1 addition & 0 deletions hugr-core/tests/snapshots/model__roundtrip_order.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ expression: ast
"negation modulo 2^N (signed and unsigned versions are the same op)")))

(define-func
public
main
(core.fn
[(arithmetic.int.types.int 6)
Expand Down
6 changes: 4 additions & 2 deletions hugr-core/tests/snapshots/model__roundtrip_params.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: hugr-core/tests/model.rs
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-params.edn\"))"
expression: ast
---
(hugr 0)

Expand All @@ -21,21 +21,23 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-para
(import core.float)

(define-func
public
example.swap
(param ?0 core.type)
(param ?1 core.type)
(core.fn [?0 ?1] [?1 ?0])
(dfg [%0 %1] [%1 %0] (signature (core.fn [?0 ?1] [?1 ?0]))))

(declare-func
public
example.literals
(param ?0 core.str)
(param ?1 core.nat)
(param ?2 core.bytes)
(param ?3 core.float)
(core.fn [] []))

(define-func example.call_literals (core.fn [] [])
(define-func private example.call_literals (core.fn [] [])
(dfg
(signature (core.fn [] []))
((core.call
Expand Down
5 changes: 3 additions & 2 deletions hugr-model/capnp/hugr-v0.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct Param {
}

enum Visibility {
private @0;
public @1;
unspecified @0;
private @1;
public @2;
}
Loading
Loading