299 Commits

Author SHA1 Message Date
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
Emilio Cobos Álvarez 68b6a7da4f Merge pull request #332 from Gankro/rust-2018-2
First implementation of Rust 2018 modules.
2019-05-02 23:39:40 +02:00
Emilio Cobos Álvarez cec3803350 Merge pull request #333 from emilio/tagged-union-dtor
ir: Add the possibility to auto-generate destructors of tagged unions.
2019-05-02 23:38:43 +02:00
Emilio Cobos Álvarez 49c056aef4 Make it more similar to the other options. 2019-05-02 19:59:02 +02:00
Emilio Cobos Álvarez a4ce6c8105 Test both repr(u8) and repr(C, u8). 2019-05-02 19:43:23 +02:00
Alexis Beingessner 8d24de12a4 Make crate name resolution test case even more complicated (no extern in nested crate case) 2019-05-02 11:52:27 -04:00
Emilio Cobos Álvarez 8c0028e5f1 ir: Add the possibility to auto-generate destructors of tagged unions.
This trades the ABI-safety (making the struct potentially unsafe to pass by
value), thus the opt-in rather than auto-generating it.

The TL;DR is that I need a way to share types that contain boxed slices from the
style system. That's fine when they're inside regular structs, since C++ does
the right thing, but for unions you need to make it explicit like this.

The actual Gecko setup will hook into the leak logging machinery to avoid silly
leaks and such of course.
2019-05-02 16:03:26 +02:00
Alexis Beingessner 6e7dead9d9 Add test for package renaming 2019-04-29 15:05:49 -05:00
Emilio Cobos Álvarez 2bb3e9ba66 Use cargo test instead of test.py.
This makes cargo test test all the stuff in tests/rust, and thus makes tests.py
unnecessary.

Co-authored-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-20 00:30:58 +02:00
Axel Nennker 87007649c8 fix #205 Negative literals (#323)
* fix #205

Signed-off-by: Axel Nennker <axel.nennker@telekom.de>

* remove comment

Signed-off-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-19 15:45:16 +02:00
Ingvar Stepanyan 8ea3b2d25f Respect visibility of constants (#314)
Fixes #123:
 - Adds visibility check for module-level constants.
 - Adds visibility check for associated constants.
 - Fixes bitflags expansion to produce public associated constants (as the real expansion does).
2019-04-16 11:25:23 +02:00
Axel Nennker 7a8b3d9944 check char constant length
Signed-off-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-15 15:56:36 +02:00
Axel Nennker 60b001ebb2 translate Rust unicode representation to C style unicode representation
Signed-off-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-15 15:22:56 +02:00
Axel Nennker 19483c21bc heart char constant in tests
Signed-off-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-15 12:30:05 +02:00
Axel Nennker 58e85439c5 escape chars
Signed-off-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-15 07:57:09 +02:00
Ingvar Stepanyan 944ecb958e Add C99 doc comment style
In modern C (post-C99) it's common to just use `// double-slash comments`, but currently cbindgen provides only `/* block-comment */` and `/// triple-slash comment` variants.
2019-04-14 16:20:07 +02:00
Axel Nennker 19fd975b6f test for char const
Signed-off-by: Axel Nennker <axel.nennker@telekom.de>
2019-04-13 23:46:51 +02:00
Emilio Cobos Álvarez 7619be86bd ir: Add support for #[must_use]. (#307) 2019-03-27 00:47:21 +01:00
Emilio Cobos Álvarez c6131722dc config: Add an option to make explicit renaming override the prefix setting.
This is useful for opaque types or types cbindgen doesn't otherwise understand.
2019-03-26 13:24:19 -05:00
Ralf Biedert 51aff0a119 Add more documentation style options (#305)
* Ignoring IDE files.

* Addresses issue #302, also amends #59 insofar that vanilla C-style now does not prefix individual lines with `*` anymore.

* Removed Javadoc reference.

* Renamed `Doxylight` to `Doxy` and changed C default to that.

* Added documentation.

* Changed enum name and applied `fmt`.

* Fixed comment.

* Fixed match.
2019-03-24 20:56:17 +01:00
Emilio Cobos Álvarez e14f782743 constant: Handle Self in associated constants.
WebRender used to do this and it's not hard to handle.
2019-03-12 10:19:49 -05:00
Emilio Cobos Álvarez 90d26107b8 bitflags: Derive more stuff for bitflags.
The static_cast is needed to avoid -Wnarrowing conversions, but I can also write
it like:

Foo ret;
ret.bits = bits | other.bits;
return ret;

if you prefer.
2019-03-06 14:14:02 -06:00
Emilio Cobos Álvarez 61b3de43a3 constant: Don't generate constants for structs that don't exist or are not safe. 2019-03-06 14:14:02 -06:00
Emilio Cobos Álvarez f35cc28b95 Test the custom assert macro. 2019-03-05 11:06:01 -06:00
Emilio Cobos Álvarez dea102d6c8 bindgen: Allow to auto-generate asserted casts for tagged enums.
This fixes one of my pet-peeves. Without this patch, I need to add them manually
using the raw body stuff, or along the code that accesses the struct member,
none of those being ideal.
2019-03-05 11:06:01 -06:00
Emilio Cobos Álvarez 011b584d7e parser: Make bitflags expansion opt-in.
Otherwise we may break code that does its own bitflags parsing.

In particular, this will prevent Gecko builds from breaking due to:

  https://searchfox.org/mozilla-central/rev/92d11a33250a8e368c8ca3e962e15ca67117f765/gfx/webrender_bindings/webrender_ffi.h#67

(Which should go away once we start opting-in).
2019-03-05 11:02:48 -06:00
Emilio Cobos Álvarez 80da1f59aa Allow to generate associated constants in the body of C++ structs.
Opt-in since it uses a C++17 feature, but this allow exactly the same usage in
C++ and Rust, which I think is nice.

This also fixes constants of transparent structs in general, since the initial
version of this patch broke a test (associated constants in enums), so I added a
test for that too.
2019-02-23 17:58:22 -08:00
Emilio Cobos Álvarez 9761df66c2 tests: Add a test that exercises all the previous patches. 2019-02-23 15:38:34 -08:00
Elichai Turkel b4887febca Added tests for specific includes without default 2019-02-06 10:49:19 -06:00
Emilio Cobos Álvarez bc67e076d1 parser: Properly skip associated constants on unsupported types.
This fixes a crash when running cbindgen 0.7.0 on the style crate.
2019-01-25 18:52:31 -06:00
Emilio Cobos Álvarez 0a6324f4e6 config: Allow to output user-defined stuff in the struct body. 2019-01-22 18:50:12 -06:00
Joshua Groves 8c3e26424d Handle enum associated constants and reduce memory usage 2019-01-11 09:23:02 -06:00
Joshua Groves 0de44edd88 Remove member name from transparent associated constants 2019-01-11 09:23:02 -06:00
Jeff Muizelaar 42b4290c64 Manually copy arrays
C++ arrays can't be assigned. We detect this situation and manually copy
over the elements.
2019-01-03 16:06:33 -06:00
Jeff Muizelaar b718662520 Support references better
This adds better support for references in the type system
and uses them for enumeration helpers instead of string
concatenation.
2019-01-03 14:14:42 -06:00
Jeff Muizelaar 3d5791731c Properly propagate const into arrays.
i.e. 'type I = *const [i32; 16]' should map to 'typedef const int32_t (*I)[16]'
instead of 'typedef int32_t (*I)[16]'
2019-01-03 09:58:06 -06: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
Ryan Hunt 032af08579 Escape reserved C/C++ keywords from struct fields and function arguments 2018-11-26 09:54:03 -06:00
IGI-111 9861755b05 Add tests for nested struct literals 2018-11-05 08:59:44 -06:00
IGI-111 e35ea840d3 Propagate prefixes to struct literals
Fix #238

This ensures that constants of a struct type have their type and the
type of their underlying value expressions renamed in the case of a
prefix.
2018-11-05 08:59:44 -06:00
Mrmaxmeier 49ccb0e3c1 enumerate wildcard arguments 2018-11-05 08:54:07 -06:00
Mrmaxmeier 28698482eb test named fn type arguments 2018-11-05 08:54:07 -06:00
Emilio Cobos Álvarez dad36fef3d Test the interaction between include and prefix configurations.
This broke mozilla-central once, hopefully not twice :)
2018-11-05 08:46:56 -06:00
IGI-111 436eaea8bf Add associated constant prefix (#234)
Fix for #232
This adds a type prefix for associated constants to avoid namespace
collisions. It also adds error invocation in the rare but existing
cases where an collisions still happens in the constant namespace.
2018-10-24 12:10:09 -05:00
IGI-111 d32127e3d7 Fix #229
Place constants back inside namespace and add a test to make sure they
are namespaced correctly.
2018-10-24 11:50:03 -05:00
Vincent Esche 26828c9f3e Replaced Items’ String-based names with proper Paths 2018-10-24 11:48:41 -05:00
Emilio Cobos Álvarez ab9e3b814c ir: Fix generic enums with untagged bodies.
Boris hit this because he used #[repr(C)] instead of #[repr(u8)].

We were incorrectly writing the generic arguments twice for those.

I missed this difference in #219.

Fixes #225.
2018-10-17 14:11:37 -05:00
Emilio Cobos Álvarez 5ac196d3ef ty: Add support for simplifying NonNull<T>.
I want this to move around slices and boxes across the style system, while
preserving the option size optimizations when they're fully in a repr(Rust)
data-structure.

This is sound because NonNull is repr(transparent):

  https://doc.rust-lang.org/src/core/ptr.rs.html#2847

I renamed simplify_option_to_ptr to simplify_standard_types because that's what
it does now.

ABI-wise for NonNull<T> it's guaranteed via repr(transparent). For
Option<NonNull<T>> it is as well, though I've asked in #rustc to confirm.

The LLVM IR of:

```
pub extern "C" fn foo(ptr: Option<::std::ptr::NonNull<i32>>) {}
```

is:

```
define void @foo(i32*) unnamed_addr #0 !dbg !310 {
start:
  %ptr = alloca i32*, align 8
	store i32* %0, i32** %ptr, align 8
	call void @llvm.dbg.declare(metadata i32** %ptr, metadata !327, metadata
	!DIExpression()), !dbg !328
	ret void, !dbg !329
}
```

Which is the same as for:

```
pub extern "C" fn foo(ptr: ::std::ptr::NonNull<i32>) {}
```

Except without the nonnull annotation.

And the same as for:

```
pub extern "C" fn foo(ptr: *mut i32) {}
```
2018-10-17 09:24:22 -05:00
Emilio Cobos Álvarez 43f6f7108e Add support for generic enums.
Going to need this if I ever aim to generate TransformOperation bindings and
remove a bunch of slow and ugly Gecko code:

  https://searchfox.org/mozilla-central/rev/80ac71c1c54af788b32e851192dfd2de2ec18e18/servo/components/style/values/generics/transform.rs#189

With the caveat that I'll need to remove the options, but I can manage to do
that.

This also fixes a bunch of renaming bugs that I found while at it.

This patch has the gotcha that we need to remove the assertion of no-underscores
in mangled names... But I think it should be fine, and I'd rather not do a more
breaking change.

You can generate conflicting names in C using enum variants with the same name
as a struct regardless, for example, so I don't think this is terribly
important.
2018-10-16 16:19:06 -05:00