Commit Graph

44 Commits

Author SHA1 Message Date
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
Emilio Cobos Álvarez 33a0c8b6c0 parser: Make associated consts not limited to the binding crate.
They're associated to a struct, and we generate the struct, so it makes sense to
generate the associated constants if so.
2019-03-06 14:14:02 -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 c05ce30afd parser: Add a comment re. the bitflags expansion. 2019-02-25 21:38:33 -08:00
Emilio Cobos Álvarez 05df2e85db &Option<Cfg> -> Option<&Cfg>
It was bothering me.
2019-02-24 05:48:59 -08:00
Emilio Cobos Álvarez 47fd0b5f5c parser: Remove unnecessary back and forth during bitflags parsing. 2019-02-23 19:47:40 -08:00
Emilio Cobos Álvarez 7076e0205d Formatting. 2019-02-23 18:37:29 -08: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 38e3f886e2 parser: Special-case bitflags macro.
Fixes #100.

I want to add some more ergonomic improvements when in C++, but that's better in
another PR.
2019-02-23 15:11:17 -08:00
Emilio Cobos Álvarez 4293899cd1 Cleanup matching of items. 2019-02-23 12:29:43 -08: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
Joshua Groves 7dc667ba32 Remove unnecessary clone 2019-01-11 09:23:02 -06:00
Joshua Groves 678e5a8d90 Run cargo-fmt 2019-01-11 09:23:02 -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
Emilio Cobos Álvarez 094cd2bf5d Update syn.
Fixes #244
2018-12-29 14:25:50 +01:00
Ryan Hunt 1ff333e66f Fix bustage from borked merge 2018-10-24 14:51:20 -05: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
Vincent Esche 26828c9f3e Replaced Items’ String-based names with proper Paths 2018-10-24 11:48:41 -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
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 090b8154dd Implemented detection/omission of #[cfg(test)] and #[test] items 2018-08-28 16:11:23 -05:00
Emilio Cobos Álvarez 8b4a271714 Provide a better error when parsing a module fails.
I was hitting this today :)
2018-08-14 00:30:47 -05:00
Basile Clement dc558ecf2c Rustfmt 2018-07-31 08:31:43 -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
Ryan Hunt 52a187c2d1 Use rustfmt from nightly to match CI 2018-05-31 12:07:19 -05:00
Ryan Hunt 155b54004f Use rustfmt 2018-05-31 11:50:03 -05:00
Ryan Hunt e36f1854ca Update to mainline syn 0.14.1 2018-05-31 10:41:49 -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
Ryan Hunt 270491f439 Rustfmt 2018-01-31 23:32:16 -06:00
Ryan Hunt e336f80ff8 Update syn to 0.12.6 2018-01-31 23:28:20 -06:00
Kartikaya Gupta 59104b1efa Increase parse error message to include more context 2018-01-17 15:18:54 -06:00
Ryan Hunt f564f2e686 Add an Error type 2018-01-04 17:07:16 -06:00
Ryan Hunt 21d917b723 Check for no_mangle and pub on functions and static items 2018-01-04 14:52:02 -06:00
Ryan Hunt f0d6d67435 Rustfmt 2017-12-14 11:56:35 -06:00
Ryan Hunt 58d3178dc5 Format with rustfmt-nightly 2017-11-18 21:44:33 -05:00
Ingvar Stepanyan fada89c7f4 Remove Specialization in favour of generic typedef 2017-11-18 14:40:42 -06:00
Ingvar Stepanyan 8fddc5d0a9 Emit generics as native templates in C++ 2017-11-18 14:40:42 -06:00
Ryan Hunt 8c26be584f Limit syn error messages
They tend to just dump the whole file.
2017-11-14 22:34:27 -05:00
Ryan Hunt 864b4a22f9 Use Parser for single source mode 2017-11-14 02:00:04 -05:00
Ryan Hunt 0d8c53ecbc Remove Cargo from public interface 2017-11-14 01:13:54 -05:00
Ryan Hunt bfe3d222d5 Remove rust_lib.rs 2017-11-09 18:04:22 -05:00