Skip to content

Commit 85df508

Browse files
author
Pat Hickey
authored
Change witx unions to be tagged (#220)
* unions are now tagged by an enum * union fields may be empty. TODO: write tests * unions: add more tests. compute layout. * snapshot 0: translate untagged union syntax to tagged union syntax * snapshot: update from untagged union to tagged union syntax * snapshot: update docs * snapshot 0: update docs * ephemeral: translate untagged unions to tagged unions * render: fix union output * bump witx to 0.8.0 - this is definitely semver breaking * snapshot: I forgot to delete the tag field `subscription.type` * regen docs * unionlayout: derive the usual suspects * snapshot 1 and 0: put tag back in event struct, flatten event_u prior to this PR, event_u was a union with only one variant fd_readwrite. the change into tagged union ended up changing the memory layout of the event struct. so, rather than use tagged unions in the event struct, we just use a struct that you ignore in the case of the clock event, which has the same ABI as previously. in ephemeral, we use a tagged union for events, because we dont have any ABI stability guarantees to maintain there. * layout: refactor so we have impls for NamedType and Type too * ast: change tag of UnionDatatype to be a NamedType this is much more convenient for consumers, and it reflects the requirement of the syntax to provide an Id * fixup docs
1 parent 3f054c6 commit 85df508

File tree

16 files changed

+526
-232
lines changed

16 files changed

+526
-232
lines changed

phases/ephemeral/docs.md

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,11 @@ The state of the file descriptor.
688688
The contents of an $event.
689689

690690
### Union variants
691-
- <a href="#event_u.fd_readwrite" name="event_u.fd_readwrite"></a> `fd_readwrite`: [`event_fd_readwrite`](#event_fd_readwrite)
692-
When type is [`eventtype::fd_read`](#eventtype.fd_read) or [`eventtype::fd_write`](#eventtype.fd_write):
691+
- <a href="#event_u.fd_read" name="event_u.fd_read"></a> `fd_read`: [`event_fd_readwrite`](#event_fd_readwrite)
692+
693+
- <a href="#event_u.fd_write" name="event_u.fd_write"></a> `fd_write`: [`event_fd_readwrite`](#event_fd_readwrite)
694+
695+
- <a href="#event_u.clock" name="event_u.clock"></a> `clock`
693696

694697
## <a href="#event" name="event"></a> `event`: Struct
695698
An event that occurred.
@@ -701,11 +704,8 @@ User-provided value that got attached to [`subscription::userdata`](#subscriptio
701704
- <a href="#event.error" name="event.error"></a> `error`: [`errno`](#errno)
702705
If non-zero, an error that occurred while processing the subscription request.
703706

704-
- <a href="#event.type" name="event.type"></a> `type`: [`eventtype`](#eventtype)
705-
The type of the event that occurred.
706-
707707
- <a href="#event.u" name="event.u"></a> `u`: [`event_u`](#event_u)
708-
The contents of the event.
708+
The type of the event that occurred, and the contents of the event
709709

710710
## <a href="#subclockflags" name="subclockflags"></a> `subclockflags`: Flags(`u16`)
711711
Flags determining how to interpret the timestamp provided in
@@ -749,10 +749,10 @@ The contents of a $subscription.
749749

750750
### Union variants
751751
- <a href="#subscription_u.clock" name="subscription_u.clock"></a> `clock`: [`subscription_clock`](#subscription_clock)
752-
When type is [`eventtype::clock`](#eventtype.clock):
753752

754-
- <a href="#subscription_u.fd_readwrite" name="subscription_u.fd_readwrite"></a> `fd_readwrite`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
755-
When type is [`eventtype::fd_read`](#eventtype.fd_read) or [`eventtype::fd_write`](#eventtype.fd_write):
753+
- <a href="#subscription_u.fd_read" name="subscription_u.fd_read"></a> `fd_read`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
754+
755+
- <a href="#subscription_u.fd_write" name="subscription_u.fd_write"></a> `fd_write`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
756756

757757
## <a href="#subscription" name="subscription"></a> `subscription`: Struct
758758
Subscription to an event.
@@ -762,11 +762,8 @@ Subscription to an event.
762762
User-provided value that is attached to the subscription in the
763763
implementation and returned through [`event::userdata`](#event.userdata).
764764

765-
- <a href="#subscription.type" name="subscription.type"></a> `type`: [`eventtype`](#eventtype)
766-
The type of the event to which to subscribe.
767-
768765
- <a href="#subscription.u" name="subscription.u"></a> `u`: [`subscription_u`](#subscription_u)
769-
The contents of the subscription.
766+
The type of the event to which to subscribe, and the contents of the subscription.
770767

771768
## <a href="#exitcode" name="exitcode"></a> `exitcode`: `u32`
772769
Exit code generated by a process when exiting.
@@ -816,22 +813,12 @@ The contents of a $prestat when its $pr_type is [`preopentype::dir`](#preopentyp
816813
- <a href="#prestat_dir.pr_name_len" name="prestat_dir.pr_name_len"></a> `pr_name_len`: [`size`](#size)
817814
The length of the directory name for use with `fd_prestat_dir_name`.
818815

819-
## <a href="#prestat_u" name="prestat_u"></a> `prestat_u`: Union
820-
The contents of an $prestat.
821-
822-
### Union variants
823-
- <a href="#prestat_u.dir" name="prestat_u.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)
824-
When $pr_type of the $prestat is [`preopentype::dir`](#preopentype.dir):
825-
826-
## <a href="#prestat" name="prestat"></a> `prestat`: Struct
816+
## <a href="#prestat" name="prestat"></a> `prestat`: Union
827817
Information about a pre-opened capability.
828818

829-
### Struct members
830-
- <a href="#prestat.pr_type" name="prestat.pr_type"></a> `pr_type`: [`preopentype`](#preopentype)
831-
The type of the pre-opened capability.
832-
833-
- <a href="#prestat.u" name="prestat.u"></a> `u`: [`prestat_u`](#prestat_u)
834-
The contents of the information.
819+
### Union variants
820+
- <a href="#prestat.dir" name="prestat.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)
821+
When type is [`preopentype::dir`](#preopentype.dir):
835822

836823
# Modules
837824
## <a href="#wasi_ephemeral_args" name="wasi_ephemeral_args"></a> wasi_ephemeral_args

phases/ephemeral/witx/typenames.witx

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,10 @@
560560

561561
;;; The contents of an $event.
562562
(typename $event_u
563-
(union
564-
;;; When type is `eventtype::fd_read` or `eventtype::fd_write`:
565-
(field $fd_readwrite $event_fd_readwrite)
563+
(union $eventtype
564+
(field $fd_read $event_fd_readwrite)
565+
(field $fd_write $event_fd_readwrite)
566+
(empty $clock)
566567
)
567568
)
568569

@@ -573,9 +574,7 @@
573574
(field $userdata $userdata)
574575
;;; If non-zero, an error that occurred while processing the subscription request.
575576
(field $error $errno)
576-
;;; The type of the event that occurred.
577-
(field $type $eventtype)
578-
;;; The contents of the event.
577+
;;; The type of the event that occurred, and the contents of the event
579578
(field $u $event_u)
580579
)
581580
)
@@ -619,11 +618,10 @@
619618

620619
;;; The contents of a $subscription.
621620
(typename $subscription_u
622-
(union
623-
;;; When type is `eventtype::clock`:
621+
(union $eventtype
624622
(field $clock $subscription_clock)
625-
;;; When type is `eventtype::fd_read` or `eventtype::fd_write`:
626-
(field $fd_readwrite $subscription_fd_readwrite)
623+
(field $fd_read $subscription_fd_readwrite)
624+
(field $fd_write $subscription_fd_readwrite)
627625
)
628626
)
629627

@@ -633,9 +631,7 @@
633631
;;; User-provided value that is attached to the subscription in the
634632
;;; implementation and returned through `event::userdata`.
635633
(field $userdata $userdata)
636-
;;; The type of the event to which to subscribe.
637-
(field $type $eventtype)
638-
;;; The contents of the subscription.
634+
;;; The type of the event to which to subscribe, and the contents of the subscription.
639635
(field $u $subscription_u)
640636
)
641637
)
@@ -691,20 +687,10 @@
691687
)
692688
)
693689

694-
;;; The contents of an $prestat.
695-
(typename $prestat_u
696-
(union
697-
;;; When $pr_type of the $prestat is `preopentype::dir`:
698-
(field $dir $prestat_dir)
699-
)
700-
)
701-
702690
;;; Information about a pre-opened capability.
703691
(typename $prestat
704-
(struct
705-
;;; The type of the pre-opened capability.
706-
(field $pr_type $preopentype)
707-
;;; The contents of the information.
708-
(field $u $prestat_u)
692+
(union $preopentype
693+
;;; When type is `preopentype::dir`:
694+
(field $dir $prestat_dir)
709695
)
710696
)

phases/old/snapshot_0/docs.md

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ The state of the file descriptor subscribed to with
627627
The peer of this socket has closed or disconnected.
628628

629629
## <a href="#event_fd_readwrite" name="event_fd_readwrite"></a> `event_fd_readwrite`: Struct
630-
The contents of an $event when type is [`eventtype::fd_read`](#eventtype.fd_read) or
631-
[`eventtype::fd_write`](#eventtype.fd_write).
630+
The contents of an $event for the [`eventtype::fd_read`](#eventtype.fd_read) and
631+
[`eventtype::fd_write`](#eventtype.fd_write) variants
632632

633633
### Struct members
634634
- <a href="#event_fd_readwrite.nbytes" name="event_fd_readwrite.nbytes"></a> `nbytes`: [`filesize`](#filesize)
@@ -637,13 +637,6 @@ The number of bytes available for reading or writing.
637637
- <a href="#event_fd_readwrite.flags" name="event_fd_readwrite.flags"></a> `flags`: [`eventrwflags`](#eventrwflags)
638638
The state of the file descriptor.
639639

640-
## <a href="#event_u" name="event_u"></a> `event_u`: Union
641-
The contents of an $event.
642-
643-
### Union variants
644-
- <a href="#event_u.fd_readwrite" name="event_u.fd_readwrite"></a> `fd_readwrite`: [`event_fd_readwrite`](#event_fd_readwrite)
645-
When type is [`eventtype::fd_read`](#eventtype.fd_read) or [`eventtype::fd_write`](#eventtype.fd_write):
646-
647640
## <a href="#event" name="event"></a> `event`: Struct
648641
An event that occurred.
649642

@@ -655,10 +648,11 @@ User-provided value that got attached to [`subscription::userdata`](#subscriptio
655648
If non-zero, an error that occurred while processing the subscription request.
656649

657650
- <a href="#event.type" name="event.type"></a> `type`: [`eventtype`](#eventtype)
658-
The type of the event that occurred.
651+
The type of event that occured
659652

660-
- <a href="#event.u" name="event.u"></a> `u`: [`event_u`](#event_u)
661-
The contents of the event.
653+
- <a href="#event.fd_readwrite" name="event.fd_readwrite"></a> `fd_readwrite`: [`event_fd_readwrite`](#event_fd_readwrite)
654+
The contents of the event, if it is an [`eventtype::fd_read`](#eventtype.fd_read) or
655+
[`eventtype::fd_write`](#eventtype.fd_write). [`eventtype::clock`](#eventtype.clock) events ignore this field.
662656

663657
## <a href="#subclockflags" name="subclockflags"></a> `subclockflags`: Flags(`u16`)
664658
Flags determining how to interpret the timestamp provided in
@@ -693,7 +687,7 @@ to coalesce with other events.
693687
Flags specifying whether the timeout is absolute or relative
694688

695689
## <a href="#subscription_fd_readwrite" name="subscription_fd_readwrite"></a> `subscription_fd_readwrite`: Struct
696-
The contents of a $subscription when type is type is
690+
The contents of a $subscription when the variant is
697691
[`eventtype::fd_read`](#eventtype.fd_read) or [`eventtype::fd_write`](#eventtype.fd_write).
698692

699693
### Struct members
@@ -705,10 +699,10 @@ The contents of a $subscription.
705699

706700
### Union variants
707701
- <a href="#subscription_u.clock" name="subscription_u.clock"></a> `clock`: [`subscription_clock`](#subscription_clock)
708-
When type is [`eventtype::clock`](#eventtype.clock):
709702

710-
- <a href="#subscription_u.fd_readwrite" name="subscription_u.fd_readwrite"></a> `fd_readwrite`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
711-
When type is [`eventtype::fd_read`](#eventtype.fd_read) or [`eventtype::fd_write`](#eventtype.fd_write):
703+
- <a href="#subscription_u.fd_read" name="subscription_u.fd_read"></a> `fd_read`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
704+
705+
- <a href="#subscription_u.fd_write" name="subscription_u.fd_write"></a> `fd_write`: [`subscription_fd_readwrite`](#subscription_fd_readwrite)
712706

713707
## <a href="#subscription" name="subscription"></a> `subscription`: Struct
714708
Subscription to an event.
@@ -718,11 +712,8 @@ Subscription to an event.
718712
User-provided value that is attached to the subscription in the
719713
implementation and returned through [`event::userdata`](#event.userdata).
720714

721-
- <a href="#subscription.type" name="subscription.type"></a> `type`: [`eventtype`](#eventtype)
722-
The type of the event to which to subscribe.
723-
724715
- <a href="#subscription.u" name="subscription.u"></a> `u`: [`subscription_u`](#subscription_u)
725-
The contents of the subscription.
716+
The type of the event to which to subscribe.
726717

727718
## <a href="#exitcode" name="exitcode"></a> `exitcode`: `u32`
728719
Exit code generated by a process when exiting.
@@ -900,22 +891,11 @@ The contents of a $prestat when type is `PREOPENTYPE_DIR`.
900891
- <a href="#prestat_dir.pr_name_len" name="prestat_dir.pr_name_len"></a> `pr_name_len`: [`size`](#size)
901892
The length of the directory name for use with [`fd_prestat_dir_name`](#fd_prestat_dir_name).
902893

903-
## <a href="#prestat_u" name="prestat_u"></a> `prestat_u`: Union
904-
The contents of an $prestat.
905-
906-
### Union variants
907-
- <a href="#prestat_u.dir" name="prestat_u.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)
908-
When type is `PREOPENTYPE_DIR`:
909-
910-
## <a href="#prestat" name="prestat"></a> `prestat`: Struct
894+
## <a href="#prestat" name="prestat"></a> `prestat`: Union
911895
Information about a pre-opened capability.
912896

913-
### Struct members
914-
- <a href="#prestat.pr_type" name="prestat.pr_type"></a> `pr_type`: [`preopentype`](#preopentype)
915-
The type of the pre-opened capability.
916-
917-
- <a href="#prestat.u" name="prestat.u"></a> `u`: [`prestat_u`](#prestat_u)
918-
The contents of the information.
897+
### Union variants
898+
- <a href="#prestat.dir" name="prestat.dir"></a> `dir`: [`prestat_dir`](#prestat_dir)
919899

920900
# Modules
921901
## <a href="#wasi_unstable" name="wasi_unstable"></a> wasi_unstable

phases/old/snapshot_0/witx/typenames.witx

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@
503503
)
504504
)
505505

506-
;;; The contents of an $event when type is `eventtype::fd_read` or
507-
;;; `eventtype::fd_write`.
506+
;;; The contents of an $event for the `eventtype::fd_read` and
507+
;;; `eventtype::fd_write` variants
508508
(typename $event_fd_readwrite
509509
(struct
510510
;;; The number of bytes available for reading or writing.
@@ -514,25 +514,18 @@
514514
)
515515
)
516516

517-
;;; The contents of an $event.
518-
(typename $event_u
519-
(union
520-
;;; When type is `eventtype::fd_read` or `eventtype::fd_write`:
521-
(field $fd_readwrite $event_fd_readwrite)
522-
)
523-
)
524-
525517
;;; An event that occurred.
526518
(typename $event
527519
(struct
528520
;;; User-provided value that got attached to `subscription::userdata`.
529521
(field $userdata $userdata)
530522
;;; If non-zero, an error that occurred while processing the subscription request.
531523
(field $error $errno)
532-
;;; The type of the event that occurred.
524+
;;; The type of event that occured
533525
(field $type $eventtype)
534-
;;; The contents of the event.
535-
(field $u $event_u)
526+
;;; The contents of the event, if it is an `eventtype::fd_read` or
527+
;;; `eventtype::fd_write`. `eventtype::clock` events ignore this field.
528+
(field $fd_readwrite $event_fd_readwrite)
536529
)
537530
)
538531

@@ -566,7 +559,7 @@
566559
)
567560
)
568561

569-
;;; The contents of a $subscription when type is type is
562+
;;; The contents of a $subscription when the variant is
570563
;;; `eventtype::fd_read` or `eventtype::fd_write`.
571564
(typename $subscription_fd_readwrite
572565
(struct
@@ -577,11 +570,10 @@
577570

578571
;;; The contents of a $subscription.
579572
(typename $subscription_u
580-
(union
581-
;;; When type is `eventtype::clock`:
573+
(union $eventtype
582574
(field $clock $subscription_clock)
583-
;;; When type is `eventtype::fd_read` or `eventtype::fd_write`:
584-
(field $fd_readwrite $subscription_fd_readwrite)
575+
(field $fd_read $subscription_fd_readwrite)
576+
(field $fd_write $subscription_fd_readwrite)
585577
)
586578
)
587579

@@ -592,8 +584,6 @@
592584
;;; implementation and returned through `event::userdata`.
593585
(field $userdata $userdata)
594586
;;; The type of the event to which to subscribe.
595-
(field $type $eventtype)
596-
;;; The contents of the subscription.
597587
(field $u $subscription_u)
598588
)
599589
)
@@ -748,20 +738,9 @@
748738
)
749739
)
750740

751-
;;; The contents of an $prestat.
752-
(typename $prestat_u
753-
(union
754-
;;; When type is `PREOPENTYPE_DIR`:
755-
(field $dir $prestat_dir)
756-
)
757-
)
758-
759741
;;; Information about a pre-opened capability.
760742
(typename $prestat
761-
(struct
762-
;;; The type of the pre-opened capability.
763-
(field $pr_type $preopentype)
764-
;;; The contents of the information.
765-
(field $u $prestat_u)
743+
(union $preopentype
744+
(field $dir $prestat_dir)
766745
)
767746
)

0 commit comments

Comments
 (0)