Commit graph

65 commits

Author SHA1 Message Date
Matt Corallo
b7538b62d9 [bindings] Handle unnamed enum variants 2021-02-19 14:02:46 -05:00
Matt Corallo
6c0025439e [bindings] Correctly use access string in to-Rust container conv
`from_c_conversion_container_new_var` should use var_access when
it wishes to access the variable being converted, not `var_name`,
but in a few cases it did not. Note that this has no impact on the
generated bindings as of this commit.
2021-02-19 14:02:33 -05:00
Matt Corallo
1a32e23050 [bindings] Ensure owned objects in Vec conversion are marked mut 2021-02-19 14:00:41 -05:00
Matt Corallo
0a9a7c5d73 [bindings] Silence c-bindings-gen compile warnings 2021-02-18 12:28:25 -05:00
Matt Corallo
6f5f27e901 [bindings] Handle impl Clone like impl ::core::clone::Clone 2021-02-18 12:28:25 -05:00
Matt Corallo
cdbe8333fa [bindings] Drop manual implementations of Features conversion
We now handle features properly in our own resolution, and these
appear to be self-inconsistent in some cases.
2021-02-18 12:28:25 -05:00
Matt Corallo
a387badfe8 [bindings] Handle generic-ized impl blocks by concretizing them
This handles, for example, the `impl<X: Y> for Features<X>` blocks
which are generic across a number of different contexts. We do so
by walking the set of structs which alias Features and then walking
their generic arguments to check that they meet the bounds
specified in the impl block. For each alias which does, we create
a dummy, explicit, `impl XFeatures` block with the same content as
the original and recurse.
2021-02-18 12:28:25 -05:00
Matt Corallo
842d2f6ce7 [bindings] Resolve type aliases mapped as opaque types
We already map type aliases which alias private types as opaque,
but we don't resolve them like we would any other opaque type,
preventing conversion printing or type use.
2021-02-18 12:28:25 -05:00
Matt Corallo
f3ce70660d [bindings] Track structs impling traits, incl priv traits 2021-02-18 12:28:25 -05:00
Matt Corallo
b49781e8d8 [bindings] Allow resolution of private types in some cases
We need this specifically for resolving the
features::sealed::Context trait which is inside the non-pub
`sealed` module.
2021-02-18 12:28:25 -05:00
Matt Corallo
3bbecbef21 [bindings] Use new ImportResolver during first AST pass
This removes some redundant logic and ensures we handle more
import-resolution cases in both contexts.
2021-02-18 12:28:25 -05:00
Matt Corallo
494e0fd8a0 [bindings] Track all declared types in ImportResolver construction
This allows an ImportResolver to handle more than imports, allowing
ident resolution of objects constructed in the same module.
2021-02-18 12:28:25 -05:00
Matt Corallo
dd1ca585b3 [bindings] Extract import resolution logic into a separate struct
This will allow us to reuse the same ident-resolution logic both
during the initial AST pass and during the second conversion pass.
2021-02-18 12:28:25 -05:00
Matt Corallo
be74b366d2 Gen bindings with rustc --pretty=expanded instead of reading files
Instead of walking individual rust files and reading the AST from
those, we instead call
`RUSTC_BOOTSTRAP=1 cargo rustc --profile=check -- -Zunstable-options --pretty=expanded`
and let it create one giant lib.rs which we can parse as a whole.
This allows us to parse a post-macro crate, working with structs
and functions created inside macros just fine. It does require
handling a few things that we didn't previously, most notably Clone
via `impl ::core::clone::Clone` blocks instead of just looking for
`#![derive(Clone)]`.

This ends up resolving a few types slightly differently, resulting
in different bindings, but only in ways which don't impact the
runtime.
2021-02-18 12:28:25 -05:00
Matt Corallo
9e596e2460 [bindings] Use resolved, not local ident in generic mangling
In traits with associated types which are returned in generics (ie
`trait T { type A: B; fn c() -> Result<Self::A, ()> {} }`), we
created a new generic mapping with the local type name (in this
case A) instead of using the real type (in this case B). This is
confusing as it results in generic manglings that don't reference
the real type (eg `LDKCResult_ChanKeySignerDecodeErrorZ`) and
may have multiple generic definitions that are identical.

Instead, we now use the final ident in the resolved mapping. The
biggest win is `LDKCResult_ChanKeySignerDecodeErrorZ` changing to
`CResult_ChannelKeysDecodeErrorZ`. However, there are several types
where `secp256k1::Error` was imported as `SecpError` and types like
`LDKCResult_SecretKeySecpErrorZ` are now
`LDKCResult_SecretKeyErrorZ` instead. Still, the type of the error
field remains `LDKSecp256k1Error`, which should avoid any confusion.
2021-02-18 12:28:25 -05:00
Matt Corallo
10c45786ab [bindings] Drop some significantly-overly-verbose logging in gen 2021-02-18 12:28:25 -05:00
Matt Corallo
03a6a243ac [bindings] Allow cloning opaque types when inner is NULL
Previously we'd segfault trying to deref the NULL page, but there
is no reason to not simply clone by creating another opaque instance
with a null inner. This comes up specifically when cloning
ChannelSigners as the pubkeys instance is NULL on construction
before get_pubkeys is called.
2021-02-10 22:25:10 -05:00
Matt Corallo
7c8da3ba04 [bindings] Drop some stale code from bindings generic generation 2021-02-10 22:25:10 -05:00
Matt Corallo
faad5124cf [bindings] Move to manual write-out for Tuples, too 2021-02-10 22:25:10 -05:00
Matt Corallo
7dd9d6f40c [bindings] Move to manual write-out for Vec, too 2021-02-10 22:25:10 -05:00
Matt Corallo
c5c2d464ba [bindings] Concretize Result types without type aliasing
While the type aliasing trick works great for cbindgen,
wasm_bindgen doesn't support it and requires fully-concrete types.
In order to better support wasm_bindgen in the future, we do so
here, adding a function which manually writes out almost the exact
thing which was templated previously in concrete form.

As a nice side-effect, we no longer have to allocate and free a u8
for generic parameters which were `()` (though we still do in some
conversion functions, which we can get rid of when we similarly
concretize all generics fully).
2021-02-10 22:24:16 -05:00
Matt Corallo
f5e0e228fd [bindings] Keep track of all types which derive[Clone] 2021-02-10 22:24:16 -05:00
Matt Corallo
e57c225e0f [C++ bindings] Add move-assign operator, require rvalue for move
This adds a move-assignment operator (`A& operator=(A&& o)`) to our
C++ wrapper classes as well as requiring an rvalue for the move
auto-convert operator (`operator CStruct()() &&`).

The second makes the C++ wrapper classes much easier to work with
by requiring an explicit `std::move` when the bindings will
automatically move a C++-wrapper object into a C object.
2021-02-03 10:11:28 -05:00
Matt Corallo
1e06274765 [bindings] Use references in a few places instead of pointers
Previously, references and pointers ended up identical in C, so
there was little reason to differentiate. With the addition of
nullability annotations, there is a (very slight) reason to prefer
references, so use them in a few places where its a trivial change.
2021-02-03 10:11:28 -05:00
Matt Corallo
53f2e25a96 [bindings] Use new non-null annotation feature in cbindgen
This adds a new annotation for objects we take by reference in the
C header indicating the pointers must not be null. We have to
disable some warning clang now dumps that we haven't annotated all
pointers, as cbindgen is not yet able to add a nullable annotation.
2021-02-03 10:11:28 -05:00
Matt Corallo
ac078c103c [bindings] Don't require trait impl for-structs to have no generics
This (finally) exposes `ChannelManager`/`ChannelMonitor` _write
methods, which were (needlessly) excluded as the structs themselves
have generic parameters. Sadly, we also now need to parse
`(C-not exported)` doc comments on impl blocks as we otherwise try
to expose _write methods for `&Vec<RouteHop>`, which doesn't work
(and isn't particularly interesting for users anyway). We add such
doc comments there.
2021-02-03 10:11:28 -05:00
Matt Corallo
56134a2bd1 [bindings] Implement ReadableArgs mapping, try impl mapping for ()s
This is most of the code to expose `ChannelManager`/`ChannelMonitor`
deserialization in our C bindings, using the new infrastructure to
map types in `maybe_convert_trait_impl` and passing generics in
from the callsites.

We also call `maybe_convert_trait_impl` for tuple types, as the
`ChannelManager`/`ChannelMonitor` deserialization returns a
`(BlockHash, T)` to indicate the block hash at which users need to
start resyncing the chain.

The final step to expose them is in the next commit.
2021-02-03 10:11:28 -05:00
Matt Corallo
ef2b321fdb [bindings] Allow write_rust_type to handle leading-colon paths
It just stubs out to `write_rust_path` in this case anyway, which
handles leading-colons just fine, so no need to panic on them.
2021-02-03 10:11:28 -05:00
Matt Corallo
dfef21c666 [bindings] Use common conv in _write impls, drop type restrictions
This expands the manual implementation logic for `*_write` and
`*_read` methods to most types, converting the `*_write` path to
the common type-conversion logic to ensure it works.

Note that `*_write_void` is still only implemented for has-inner
types, as its unclear what the `void*` would point to for others.
2021-02-03 10:11:28 -05:00
Matt Corallo
e36b51400f [bindings] Convert manual _read implementations to return Results
Previously, manual `*_read` implementations were only defined for
types with inner fields, which were set to NULL to indicate read
errors. This prevents exposing `*_read` for several other types,
including tuples (which are needed for `ChannelManager`/
`ChannelMonitors`) and enums (which includes `Event`s, though users
likely never need to call that directly). Further, this means we
don't expose the actual error enum (which is likely no big deal,
but is still nice).

Here, we instead create the `Result<Object, DecodeError>` type and
then pass it through the normal type conversion functions, giving
us access to any types which we can convert normally.
2021-02-02 22:51:52 -05:00
Matt Corallo
0a6c9e85ff [bindings] Pipe errors back from write_template_constructor
We can fail to resolve a part of a tuple, resulting in a panic in
write_template_constructor even if we're calling
`understood_c_type` with the intent of figuring out whether we can
print a type at all. Instead, we should pipe errors back and let
`understood_c_type` return false as a result.
2021-02-02 22:51:52 -05:00
Matt Corallo
1479016331 [bindings] Drop useless #[no_mangle] from pub type definitions
Newer rustc complains that "attribute should be applied to a function or
static"
2021-02-02 17:04:31 -05:00
Matt Corallo
554af1efb3 [bindings] Be explicit with take_ptr calls
If you try to call take_ptr on a pointer to an object which
implements Deref, rustc hits the deref recursion limit.

To avoid this, we can explicitly tell rustc that we want to treat
the pointer as a pointer and call take_ptr on it directly.
2021-02-02 17:04:31 -05:00
Matt Corallo
734c0a6236 [bindings] Separate take_ptr and take_inner
It is confusing to have two utility methods on different classes
of types which do two different, but related, things with the same
name.
2021-02-02 17:04:31 -05:00
Matt Corallo
78c2c48ea3 [bindings] Support exposing bitcoin::OutPoints as our common type 2021-02-02 17:04:31 -05:00
Matt Corallo
ce56152cf5 [bindings] Figure out in-file structs and enums before processing
Previously, types which were declared and used in the same file
would fail if the use was before the declaration. This makes sense
in a few cases where a "parent" class returns a reference to a
"child" class and there's no reason we shouldn't support it.

This change adds a second pass to our file processing which gathers
the structs and enums whicha re declared in the file and adds them
to the type resolver first, before doing the real conversion.
2021-02-02 17:04:31 -05:00
Matt Corallo
bec92d3eaa [bindings] Expose secp256k1::Message as ThirtyTwoBytes 2021-02-02 17:04:31 -05:00
Matt Corallo
c6d2697703 [bindings] Add support for mapping Write as a supertrait 2021-02-02 17:04:31 -05:00
Matt Corallo
dcfd95a700 [bindings] Handle MessageSendEventsProvider impl blocks in a util fn
Instead of having manually-written lightning-specific code in a
supertrait walk in the middle of a large function, move it to a
utility function up next to the other manually-written-impl-block
functions.
2021-02-02 17:04:31 -05:00
Matt Corallo
4edf514a05 [bindings] Always resolve supertrait types during supertrait walks
This is a rather trivial cleanup to ensure we always have the full
path when we walk supertraits even if the supertrait is specified
with only a single ident.
2021-02-02 17:04:31 -05:00
Matt Corallo
af3ad4b029 [bindings] Pass GenericTypes through to write_template_generic
With this we support types like `Result<Self::AssociatedType, ()>`.
2021-02-02 17:04:31 -05:00
Matt Corallo
3f5a287ca8 [bindings] Un-special-case returning an associated type
In the case that we return an associated type to C (ie when
implementing a trait which returns an associated type, we had to
convert the Rust-returned concrete Rust type to the C trait struct),
we had code to manually create the neccessary trait struct at the
return site.

This was special-cased in the method-body-writing function instead
of letting the type conversion logic handle it. As a result, we are
unable to do the same conversion when it appears in a different
context, for example inside of a generic like
`Result<Self::AssocType, ErrorType>`.

To solve this, we do the actual work in a
`impl From<nativeType> for CTraitStruct` implementation and then
call `into()` from within the type conversion logic.
2021-02-02 17:04:31 -05:00
Matt Corallo
086434f0c5 [bindings] Replace associated_types HashMaps with common Generics
Instead of handling associated types separately, we can just shove
them into the same generics resolution logic we use for template
types. While we should probably have some precedence logic,
aliasing type names seems like a bad idea anyway so no effort is
made to handle it.

This removes a good chunk of code and, more importantly, tees us up
for supporting `Type<Self::AssociatedType>`-style generics.
2021-02-02 17:04:31 -05:00
Matt Corallo
d169966f76 [bindings] Update eq/clone trait fns to take object, not this_arg
When a trait is required to implement eq/clone (eg in the case of
`SocketDescriptor`), the generated trait struct contains an
eq/clone function which takes a `this_arg` pointer. Since the trait
object can always be read to get the `this_arg` pointer, there is
no loss of generality to pass the trait object itself, and it
provides a bit more flexibility when the trait could be one of
several implementations (which we use in the Java higher-level
bindings).
2020-11-23 11:08:34 -05:00
Matt Corallo
0362972d37 [bindings] Expose a _clone fn for every enum
This somewhat assumes that every public enum implements clone in
some way, but that is currently the case.
2020-11-23 11:08:34 -05:00
Matt Corallo
29b209ceea [bindings] Expose a _clone fn for every struct that derive(Clone)s 2020-11-23 11:08:34 -05:00
Matt Corallo
304471b1c1 [bindings] Support traits with generic arguments (to support #681)
Previously we'd ignored generic arguments in traits, leading to
bogus code generation after the Persister trait was added in #681.

This adds minimal support for it, fixing code generation on latest
upstream.
2020-11-23 11:08:34 -05:00
Matt Corallo
2d0cdbd33e [bindings] Un-Box Tuple mapping
Because the C++ wrappers require being able to memset(0) the C
structs to skip free(), we'd previously mapped tuples with two
pointer indirections. However, because all other types already
support memset(0)'ing to disable free() logic, we can skip the
pointer indirections and the behavior is still correct.
2020-10-21 14:54:51 -04:00
Matt Corallo
65884fffee [bindings] Fix typo in opaque struct docs found by Val 2020-10-21 14:54:51 -04:00
Matt Corallo
e12215ca8a [bindings] Use the same SipHash keys to make C++ header stable 2020-10-21 14:54:51 -04:00