lassade / c2z

C++ to Zig bindings and transpiler
102 stars 7 forks source link

Lambda mangling test #5

Open kassane opened 1 year ago

kassane commented 1 year ago

Some lambda test - C++20: https://godbolt.org/z/Gq1h7Ezn9

Lambda 1

input

#include <iostream>
template<typename T> int print_hello = (std::cout << "hello\n", 0);
struct X {
  // Should only print hello once, regardless of how many TUs this appears in.
  static const intptr_t n = (print_hello<decltype([]{})>, 0);
};

output

error: unhandled `VarTemplateDecl`
error: unhandled `VarTemplateSpecializationDecl`
info: transpiled 214903/214939 (99.98 %)
info: formating `x86_64/linux/gnu/cxx.zig`
// auto generated by c2z
const std = @import("std");
//const cpp = @import("cpp");

pub const X = extern struct {
    extern const _ZN1X1nE: isize;
    pub inline fn n() isize {
        return _ZN1X1nE;
    }
};

Lambda 2

input

struct A {
  int f(decltype([]{ static int n; return ++n; }) lambda = {}) {
    return lambda();
  }
};
int a = A().f(), b = A().f();

output

info: binding `cxx.h`
error: unknow type `decltype([] {
    static int n;
    return ++n;
})`, falling back to `*anyopaque`
error: unhandled `RecoveryExpr`
error: syntax errors in `A.f`
info: transpiled 56/58 (96.55 %)
info: formating `x86_64/linux/gnu/cxx.zig`
// auto generated by c2z
const std = @import("std");
//const cpp = @import("cpp");

pub const A = extern struct {

    // syntax errors:
    // pub fn f(self: *A, lambda: *anyopaque) c_int {
    // return ();
    // }
    //
};

extern var a: c_int;
pub inline fn a() *c_int {
    return &a;
}

extern var b: c_int;
pub inline fn b() *c_int {
    return &b;
}

References

kassane commented 1 year ago

C++11/14

Typed

input

int square = [](int num) {
    return num * num;
};

output

info: binding `cxx.h`
info: transpiled 51/51 (100.00 %)
info: formating `x86_64/linux/gnu/cxx.zig`
// auto generated by c2z
const std = @import("std");
//const cpp = @import("cpp");

extern var square: c_int;
pub inline fn square() *c_int {
    return &square;
}

deducted

input

auto square = [](auto num) {
    return num * num;
};

output

info: binding `cxx.h`
error: unknow type `(lambda at cxx.h:2:15)`, falling back to `*anyopaque`
info: transpiled 56/56 (100.00 %)
info: formating `x86_64/linux/gnu/cxx.zig`
// auto generated by c2z
const std = @import("std");
//const cpp = @import("cpp");

extern var square: *anyopaque;
pub inline fn square() **anyopaque {
    return &square;
}

error: redeclaration of 'square'

lassade commented 1 year ago

I don't think lambda is a feature that I wan't to support, but is nice to keep in mind

lassade commented 1 year ago

Just out of curiosity is there any library that shows a real use example of this?

kassane commented 1 year ago

Just out of curiosity is there any library that shows a real use example of this?

No! I don't know of any libraries that have this requirement. It is probably a very recent topic to discuss.

clang tests:

kassane commented 1 year ago

Another test

int foo(){
    return [](){return 6;}();
}

Output:

// auto generated by c2z
const std = @import("std");
//const cpp = @import("cpp");

pub fn foo() c_int {
    return;
}

Suggestion:

fn unammed() c_int { return 6; }
pub fn foo() c_int { return unammed(); }

or try zig callback *const fn( ) T

Reference: