Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda mangling test #5

Open
kassane opened this issue Jun 29, 2023 · 5 comments
Open

Lambda mangling test #5

kassane opened this issue Jun 29, 2023 · 5 comments

Comments

@kassane
Copy link
Contributor

kassane commented Jun 29, 2023

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
Copy link
Contributor Author

kassane commented Jun 29, 2023

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
Copy link
Owner

lassade commented Jun 29, 2023

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

@lassade
Copy link
Owner

lassade commented Jun 30, 2023

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

@kassane
Copy link
Contributor Author

kassane commented Jun 30, 2023

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
Copy link
Contributor Author

kassane commented Jul 4, 2023

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants