Skip to content

Commit

Permalink
Implement Union in terms of Variant
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton authored and sunfishcode committed Feb 11, 2021
1 parent 1e48303 commit b07d794
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 498 deletions.
36 changes: 15 additions & 21 deletions phases/ephemeral/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,20 +859,18 @@ The state of the file descriptor.

Offset: 8

## <a href="#event_u" name="event_u"></a> `event_u`: Union
## <a href="#event_u" name="event_u"></a> `event_u`: Variant
The contents of an [`event`](#event).

Size: 24

Alignment: 8

### Union Layout
### Variant Layout
- size: 24
- align: 8
- tag_size: 1
- tag_align: 1
- contents_offset: 8
- contents_size: 16
- contents_align: 8
### Union variants
### Variant cases
- <a href="#event_u.fd_read" name="event_u.fd_read"></a> `fd_read`: [`event_fd_readwrite`](#event_fd_readwrite)

- <a href="#event_u.fd_write" name="event_u.fd_write"></a> `fd_write`: [`event_fd_readwrite`](#event_fd_readwrite)
Expand Down Expand Up @@ -961,20 +959,18 @@ The file descriptor on which to wait for it to become ready for reading or writi

Offset: 0

## <a href="#subscription_u" name="subscription_u"></a> `subscription_u`: Union
## <a href="#subscription_u" name="subscription_u"></a> `subscription_u`: Variant
The contents of a [`subscription`](#subscription).

Size: 40

Alignment: 8

### Union Layout
### Variant Layout
- size: 40
- align: 8
- tag_size: 1
- tag_align: 1
- contents_offset: 8
- contents_size: 32
- contents_align: 8
### Union variants
### Variant cases
- <a href="#subscription_u.clock" name="subscription_u.clock"></a> `clock`: [`subscription_clock`](#subscription_clock)

- <a href="#subscription_u.fd_read" name="subscription_u.fd_read"></a> `fd_read`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
Expand Down Expand Up @@ -1078,20 +1074,18 @@ The length of the directory name for use with `fd_prestat_dir_name`.

Offset: 0

## <a href="#prestat" name="prestat"></a> `prestat`: Union
## <a href="#prestat" name="prestat"></a> `prestat`: Variant
Information about a pre-opened capability.

Size: 8

Alignment: 4

### Union Layout
### Variant Layout
- size: 8
- align: 4
- tag_size: 1
- tag_align: 1
- contents_offset: 4
- contents_size: 4
- contents_align: 4
### Union variants
### Variant cases
- <a href="#prestat.dir" name="prestat.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)
When type is [`preopentype::dir`](#preopentype.dir):

Expand Down
20 changes: 10 additions & 10 deletions phases/ephemeral/witx/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,10 @@

;;; The contents of an `event`.
(typename $event_u
(union $eventtype
(field $fd_read $event_fd_readwrite)
(field $fd_write $event_fd_readwrite)
(empty $clock)
(variant (@witx tag $eventtype)
(case $fd_read $event_fd_readwrite)
(case $fd_write $event_fd_readwrite)
(case $clock)
)
)

Expand Down Expand Up @@ -620,10 +620,10 @@

;;; The contents of a `subscription`.
(typename $subscription_u
(union $eventtype
(field $clock $subscription_clock)
(field $fd_read $subscription_fd_readwrite)
(field $fd_write $subscription_fd_readwrite)
(union (@witx tag $eventtype)
$subscription_clock
$subscription_fd_readwrite
$subscription_fd_readwrite
)
)

Expand Down Expand Up @@ -691,8 +691,8 @@

;;; Information about a pre-opened capability.
(typename $prestat
(union $preopentype
(union (@witx tag $preopentype)
;;; When type is `preopentype::dir`:
(field $dir $prestat_dir)
$prestat_dir
)
)
24 changes: 10 additions & 14 deletions phases/old/snapshot_0/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,20 +899,18 @@ The file descriptor on which to wait for it to become ready for reading or writi

Offset: 0

## <a href="#subscription_u" name="subscription_u"></a> `subscription_u`: Union
## <a href="#subscription_u" name="subscription_u"></a> `subscription_u`: Variant
The contents of a [`subscription`](#subscription).

Size: 48

Alignment: 8

### Union Layout
### Variant Layout
- size: 48
- align: 8
- tag_size: 1
- tag_align: 1
- contents_offset: 8
- contents_size: 40
- contents_align: 8
### Union variants
### Variant cases
- <a href="#subscription_u.clock" name="subscription_u.clock"></a> `clock`: [`subscription_clock`](#subscription_clock)

- <a href="#subscription_u.fd_read" name="subscription_u.fd_read"></a> `fd_read`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
Expand Down Expand Up @@ -1148,20 +1146,18 @@ The length of the directory name for use with [`fd_prestat_dir_name`](#fd_presta

Offset: 0

## <a href="#prestat" name="prestat"></a> `prestat`: Union
## <a href="#prestat" name="prestat"></a> `prestat`: Variant
Information about a pre-opened capability.

Size: 8

Alignment: 4

### Union Layout
### Variant Layout
- size: 8
- align: 4
- tag_size: 1
- tag_align: 1
- contents_offset: 4
- contents_size: 4
- contents_align: 4
### Union variants
### Variant cases
- <a href="#prestat.dir" name="prestat.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)

# Modules
Expand Down
12 changes: 6 additions & 6 deletions phases/old/snapshot_0/witx/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@

;;; The contents of a `subscription`.
(typename $subscription_u
(union $eventtype
(field $clock $subscription_clock)
(field $fd_read $subscription_fd_readwrite)
(field $fd_write $subscription_fd_readwrite)
(union (@witx tag $eventtype)
$subscription_clock
$subscription_fd_readwrite
$subscription_fd_readwrite
)
)

Expand Down Expand Up @@ -740,7 +740,7 @@

;;; Information about a pre-opened capability.
(typename $prestat
(union $preopentype
(field $dir $prestat_dir)
(union (@witx tag $preopentype)
$prestat_dir
)
)
24 changes: 10 additions & 14 deletions phases/snapshot/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -896,20 +896,18 @@ The file descriptor on which to wait for it to become ready for reading or writi

Offset: 0

## <a href="#subscription_u" name="subscription_u"></a> `subscription_u`: Union
## <a href="#subscription_u" name="subscription_u"></a> `subscription_u`: Variant
The contents of a [`subscription`](#subscription).

Size: 40

Alignment: 8

### Union Layout
### Variant Layout
- size: 40
- align: 8
- tag_size: 1
- tag_align: 1
- contents_offset: 8
- contents_size: 32
- contents_align: 8
### Union variants
### Variant cases
- <a href="#subscription_u.clock" name="subscription_u.clock"></a> `clock`: [`subscription_clock`](#subscription_clock)

- <a href="#subscription_u.fd_read" name="subscription_u.fd_read"></a> `fd_read`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
Expand Down Expand Up @@ -1145,20 +1143,18 @@ The length of the directory name for use with [`fd_prestat_dir_name`](#fd_presta

Offset: 0

## <a href="#prestat" name="prestat"></a> `prestat`: Union
## <a href="#prestat" name="prestat"></a> `prestat`: Variant
Information about a pre-opened capability.

Size: 8

Alignment: 4

### Union Layout
### Variant Layout
- size: 8
- align: 4
- tag_size: 1
- tag_align: 1
- contents_offset: 4
- contents_size: 4
- contents_align: 4
### Union variants
### Variant cases
- <a href="#prestat.dir" name="prestat.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)

# Modules
Expand Down
13 changes: 7 additions & 6 deletions phases/snapshot/witx/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,11 @@

;;; The contents of a `subscription`.
(typename $subscription_u
(union $eventtype
(field $clock $subscription_clock)
(field $fd_read $subscription_fd_readwrite)
(field $fd_write $subscription_fd_readwrite)
(union
(@witx tag $eventtype)
$subscription_clock
$subscription_fd_readwrite
$subscription_fd_readwrite
)
)

Expand Down Expand Up @@ -740,8 +741,8 @@

;;; Information about a pre-opened capability.
(typename $prestat
(union $preopentype
(field $dir $prestat_dir)
(union (@witx tag $preopentype)
$prestat_dir
)
)

15 changes: 0 additions & 15 deletions tools/witx/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ impl NamedType {
pub enum Type {
Record(RecordDatatype),
Variant(Variant),
Union(UnionDatatype),
Handle(HandleDatatype),
List(TypeRef),
Pointer(TypeRef),
Expand All @@ -201,7 +200,6 @@ impl Type {
match self {
Record(_) => "record",
Variant(_) => "variant",
Union(_) => "union",
Handle(_) => "handle",
List(_) => "list",
Pointer(_) => "pointer",
Expand Down Expand Up @@ -272,19 +270,6 @@ pub struct Case {
pub docs: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnionDatatype {
pub tag: Rc<NamedType>,
pub variants: Vec<UnionVariant>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnionVariant {
pub name: Id,
pub tref: Option<TypeRef>,
pub docs: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct HandleDatatype {}

Expand Down
2 changes: 1 addition & 1 deletion tools/witx/src/coretypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Type {
},
Type::List { .. } => TypePassedBy::PointerLengthPair,
Type::Pointer { .. } | Type::ConstPointer { .. } => TypePassedBy::Value(AtomType::I32),
Type::Record { .. } | Type::Union { .. } => TypePassedBy::Pointer,
Type::Record { .. } => TypePassedBy::Pointer,
Type::Variant(v) => {
if v.cases.iter().all(|c| c.tref.is_none()) {
TypePassedBy::Value(v.tag_repr.into())
Expand Down
Loading

0 comments on commit b07d794

Please sign in to comment.