Commit Graph

73 Commits

Author SHA1 Message Date
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
Mrmaxmeier 161d83c0b1 add lifetime_arg test 2018-10-16 15:50:23 -05:00
Emilio Cobos Álvarez 02c1cbad79 Keep prefixing the body of tagged enums in C since it has no scoping. 2018-10-04 10:08:25 -05:00
Emilio Cobos Álvarez 15d7cb8904 Fix tagged enum's interactions with prefixing. 2018-10-04 10:08:25 -05:00
konstin 8faf7011ae Add no_includes option 2018-10-01 16:53:09 -05:00
IGI-111 110c3e481c Add support for associated constants and struct literals (#170)
* handle associated constants and struct literals

This aims to fix #100 by adding parsing support for associated constants, and
struct literal expressions.

It duplicates some of the parsing for constants, but sadly ImplItemConst and
ImplConst (as well as their children) don't share a common trait
that would allow for genericity.

It also uses the same namespace for both constants and associated
constants, which could cause conflicts in valid Rust with shared const names in
different scopes.

The struct literals use the standard C99 syntax. Do mind that the
limitations on literal expressions still apply.

* added test cases

* fix formatting
2018-10-01 16:37:51 -05:00
Vincent Esche 5e6d58dfe3 Added support for #[repr(transparent)] 2018-08-28 16:35:04 -05:00
Emilio Cobos Álvarez 52270a23af Apply renaming rules to the generated eq/neq impls. 2018-08-17 23:15:50 -05:00
Emilio Cobos Álvarez 863a49f64f Don't compare the tag on tagged structs.
We could assert, I guess, should it be wanted.
2018-08-17 23:15:50 -05:00
Emilio Cobos Álvarez 1e4b90bb1a Honor derive_eq / derive_neq for unions. 2018-08-17 23:15:50 -05:00
Vincent Esche 68a22aac3c Added test mod_attr project
Added impl of `fmt::Display` for `Cfg`

Added  `fn matched_defines(…)` for filtering for matches in `[defines]`

Changes semantic of `has_defines` from `∀` to `∃` (i.e. all -> one or more)

Changed `write_before` to use `matched_defines`

Added logging of warning for omitted `#[cfg(…)]`s

Added expectations for `mod_attr` test project

Renamed function arguments in `mod_attr` test project

Rustfmt

Introduced `Condition` type to ensure correct API usage of `Cfg`

Merged `Condition::Boolean` and `Condition::Named` into `Condition::Define`

Removed `DefineConfig` and `MissingDefineBehavior`.

(Was getting a bit ahead of myself with these.)

Rustfmt
2018-08-17 23:15:16 -05:00
Vincent Esche b07046d28a Fixed missing prefix for named consts in [T; <Const>] types 2018-08-14 00:47:35 -05:00
Basile Clement 33c45a26bb Add ability to specify features to use for macro expansion
Currently, `cbindgen` uses the `--all-features` flag when expanding a
dependent crate. However, this may not be desirable in a number of
cases:

 - Some C APIs may be gated by a feature flag and would not be present
   in the final cdylib depending on the features provided (for instance
   one could want to have the ability to build a "debug" version of the
   library which provides extra unstable hooks). In such cases, a
   programmatic `cbindgen` call in a build script would want to use only
   the features that will get used in the current build.

 - Some features may bring in large dependencies and/or potentially
   increase compilation time without affecting the FFI surface, and it
   would be faster and more efficient to disable them when running
   `cbindgen`.

 - Some features may require external libraries and/or hardware (e.g.
   dependencies on GPU libraries such as CUDA) that may not be available
   on the current machine without affecting the FFI surface.

To alleviate this problem, this PR adds an extended version of the
`parse.expand` configuration key, allowing control over the features
used when expanding in a way similar to the way cargo handles extended
dependencies (although note that there is a single version of each key,
since the features refer to the features of the current crate). So for
instance instead of writing `expand = ["euclid"]` one would write:

```
[parse.expand]
crates = ["euclid"]
```

which is equivalent to:

```
[parse.expand]
crates = ["euclid"]
all_features = false
default_features = true
features = ["feature1", "feature2"]
```

Note that `all_features` is set to `false` by default in order to match
cargo's behavior.

For backwards compatibility, the old syntax `expand = ["euclid"]` is
still supported and is equivalent to:

```
[parse.expand]
crates = ["euclid"]
all_features = true
default_features = true
features = null
```

In this case, `all_features` is set to `true` in order to match the
previous behavior of cbindgen.
2018-07-31 08:31:43 -05:00
Basile Clement ba589a868e Use Cargo.lock from workspace root
cbindgen currently assumes that the `Cargo.lock` is in directly in the
crate directory as a sibling to `Cargo.toml`; however that is usually
not the case inside a workspace.

Instead, this PR extracts the workspace root from the output of `cargo
metadata` [1] (already used to get a list of packages) and uses the
`Cargo.lock` from there.

 1. This is available since Rust 1.24; see rust-lang/cargo#4940
2018-07-30 22:02:56 -05:00
Emilio Cobos Álvarez 5da9ae98d3 Basic item_types test. 2018-07-25 13:54:10 -05:00
Emilio Cobos Álvarez e3e1bb82bf bindings: Don't generate redundant autogen warnings or extern blocks. 2018-07-25 13:54:10 -05:00
Ryan Hunt d192544c7f Add two parsing only tests for #164 and #169 2018-05-31 10:46:28 -05:00
Alexis Beingessner cabe1055c4 Add tests for optional enum convenience constructors 2018-04-25 13:40:58 -05:00
Alexis Beingessner da550f53b2 Implement optional convenience constructors for tagged enums 2018-04-25 13:40:58 -05:00
Johan Anderholm 3a9bb17e8a Modify test to run all three styles for C. 2018-03-27 10:05:45 -05:00
Ryan Hunt 1693519e39 Update test expectations from size_t to uintptr_t changes 2018-03-14 14:40:40 -05:00
Ryan Hunt cf3529fa84 Don't use extern crate aliases when searching for dependencies 2018-03-14 14:40:40 -05:00
Dan Robertson 0c9c32ecdb Improve module path parsing
If a module has a path attribute use this as the module path to parse.
2018-03-14 13:42:18 -05:00
Dan Robertson 22fa8b0824 Ensure that a constants cfg is preserved
When writing out constants the cfg attribute is not written out around
the defined constant.
2018-03-12 12:44:57 -04:00
Ryan Hunt e336f80ff8 Update syn to 0.12.6 2018-01-31 23:28:20 -06: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