Commit Graph

20 Commits

Author SHA1 Message Date
Marc-André Lureau ccea33ecb4 tests: check renaming enum affects variant prefix
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2021-03-29 14:08:02 +02:00
Vadim Petrochenkov 1c1d4754ce tests: Remove Cython-specific configs 2020-11-25 17:30:30 +01:00
Kartikaya Gupta 5f6b223237 Add ostream header to all generated .cpp files.
Ideally we'd just do this when we need it, but the ostream derivation
will be controlled by both global config and per-structure config, so
it's hard to know exactly when we need it and when we don't.
2020-10-01 12:22:51 +02:00
John VanEnk 4cb762ec8f Do not emit enum TagName tag when a sized representation is present.
This affected cases where the style was set to `Stle::Tag`.

In enumerations, do not emut the tag type name with an `enum`
prefix. C treats the `enum` type as an `int` even if the
representation should be something else.

Here's an example non-C-like-enumeration:

    #[repr(C, u8)]
    enum P {
        P1(u8),
        P2(u8, u8),
        P3(u8, u8, u8),
    }

Without this patch, this is what's generated:

    enum P_Tag {
      P1,
      P2,
      P3,
    };
    typedef uint8_t P_Tag;

    typedef struct {
      uint8_t _0;
    } P1_Body;

    typedef struct {
      uint8_t _0;
      uint8_t _1;
    } P2_Body;

    typedef struct {
      uint8_t _0;
      uint8_t _1;
      uint8_t _2;
    } P3_Body;

    typedef struct {
      enum P_Tag tag;
      union {
        P1_Body p1;
        P2_Body p2;
        P3_Body p3;
      };
    } P;

Rust expects the size of the type to be 4 (one for the discriminant,
and 3 for the widest alternative. C, however, treats the `enum P_Tag
tag` field as an `int` rather than a `uint8_t`. This puts the size
(with padding) of of `P` to 8 bytes instead of 4. When passing this
type between Rust and C, they will disagree on the size.

After the patch is applied, the `P` struct is properly defined:

    typedef struct {
      P_Tag tag;
      union {
        P1_Body p1;
        P2_Body p2;
        P3_Body p3;
      };
    } P;
2020-01-26 04:43:27 +01:00
Evan Shaw 6a9066f7cd Add enum_class option
This option allows specifying that a C++ enum should be emitted with
plain `enum` rather than `enum class`. It's true that `enum class` should
generally be preferred, but sometimes there's existing code which is
already using plain `enum`, and porting that code to Rust becomes easier when
`cbindgen` can emit plain `enum`.

This option can be overridden on a per-enum basis using a `cbindgen:` annotation.

It defaults to true for two reasons:

* Backward compatibility.
* `enum class` is probably actually what you want, in general.
2019-12-29 12:55:59 +01:00
Aleksa Sarai 8c66c0c1dc tests: enum: add #[repr(u64)] test
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
2019-12-22 14:06:12 +01:00
Emilio Cobos Álvarez b2e224354b Use placement new for constructing in tagged unions' helper methods.
Using operator= is not quite sound in presence of destructors and operator
overloading.

It's perfectly fine to assume that the left-hand-side of an operator= expression
is valid memory, however we're using uninitialized memory here, that may not be
the case.

Use placement new to properly construct tagged unions. I don't need this with
any urgency, but it's the right thing to do in presence of complex types, and
the current code seems a bomb waiting to explode :)
2019-05-10 18:15:39 +02:00
Dan Robertson 304f752c06 Add support for VaList functions 2019-01-03 09:46:55 -06:00
Emilio Cobos Álvarez e08fb32d74 ir: Add support for negative enum discriminants.
Pretty straight-forward, but it bit me today :)
2018-11-26 09:57:34 -06:00
Johan Anderholm 3a9bb17e8a Modify test to run all three styles for C. 2018-03-27 10:05:45 -05:00
Ingvar Stepanyan 70d8b95f78 Disallow unknown and conflicting repr markers 2018-01-31 09:35:29 -06:00
Ingvar Stepanyan f9b8512dc2 Wrap tag into anonymous struct in C++
Fixes #122
2018-01-31 09:35:29 -06:00
Ingvar Stepanyan 07eae4c171 Implement support for repr(C, Int)
Fixes #119
2018-01-31 09:35:29 -06:00
Ryan Hunt 4e481c1338 Add dependencies for tagged enums 2018-01-29 14:08:02 -06:00
Ingvar Stepanyan bd494d4646 Simplify enum variant handling 2018-01-29 11:36:59 -06:00
Ingvar Stepanyan 3355f2b1ff Rename fields generated by tagged enum 2018-01-29 11:36:59 -06:00
Ingvar Stepanyan 905b9a09aa Add support for repr(C) tagged enums too 2018-01-29 11:36:59 -06:00
Ingvar Stepanyan 3b61c8ead5 Initial support for tagged enums 2018-01-29 11:36:59 -06:00
Ingvar Stepanyan 8fddc5d0a9 Emit generics as native templates in C++ 2017-11-18 14:40:42 -06:00
Ryan Hunt 6a02ec4a7f Commit test expectations 2017-11-09 18:04:22 -05:00