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

Preliminary support for unions added. #63

wants to merge 3 commits into
base: master


None yet
2 participants
Copy link

harpocrates commented Sep 13, 2016

This is really far from complete, but it starts addressing #22 by filling in everything straightforward. Here are the issues AFAICT

  • We cannot yet derive Copy and Clone for unions, but support for that is pending (incredibly fast given how recent an addition unions are!).

  • We'll need to add #![feature(untagged_unions)] to the top of files (I don't think we currently have anything to do this - if we don't it'd be worth it to make it good).

  • The can of worms that was struct initialization needs to be reopened. I'll take a look at the C99 spec. I'm hoping that when there are multiple (named) initializers in the initializer list for a union in C99, we simply ignore all except the last one (even if the previous ones are bigger), and at least clang seems to agree... I'm also hoping that the rust unions are also initialized with an extra empty space zeroed out. If all that works out as expected, I'll still need to find a nice way of expressing union based initializers - something like

    data Initializer
       = StructInitializer (Maybe Rust.Expr) (IntMap.IntMap Initializer)
       = UnionInitializer Int Initializer

Is there anything else I'm missing here? I think there is something to do with incomplete types that you did for structs that also applies to unions. I'm thinking of this...


This comment has been minimized.

Copy link

petrochenkov commented Sep 13, 2016

We cannot yet derive Copy and Clone for unions

FYI, Copy can already be derived, it was a part of initial union implementation.

Adds support for initialization of unions
Apart from a couple of obvious bug fixes, this focuses on initialization
for unions. The previous initialization code had been designed with only struct
and arrays in mind, so some pretty deep changes had to be made.

   * the data type `Initializer` now has three constructors for regular scalar
     initializers, aggregate initializers (arrays and structs), and union
     initializers. Might want to think about naming `Scalar` differently (see
     section 6.2.5 point 21).
   * `Initializer` is no longer a `Monoid`, since we can't make a `mempty`
     without knowing the type we are initializing. Instead, it is a `Semigroup`,
     with some combinations not valid. This means that either we will need
     to enforce `base >= 4.9` or `semigroups`.

To the best of my current knowledge and testing, this accurately and completely
implements initialization of unions as per C99.

This comment has been minimized.

Copy link

harpocrates commented Sep 17, 2016

This build fails because one of the big changes that comes with this last commit (that adds what I think is complete support for union initialization) is that Initializer no longer is a Monoid but a Semigroup instead. That means we'll need to add either semigroups or base >= 4.9 to the cabal file, and currently I've done neither.

Personally I am in favour of the latter (if we are generating rust that only compiles on rust nightly, it seems fairly reasonably to say we run on GHC 8.0), but I'll leave the choice up to you.

Use `IsIncomplete` for union types too
Add support for incomplete unions in the context of:

  * completing them
  * performing member access on them
  * zero initializing them

Also added missing case in `Eq CType` for unions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment