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

Explore higher level macros #43

Open
TheDan64 opened this issue Jun 16, 2018 · 3 comments
Open

Explore higher level macros #43

TheDan64 opened this issue Jun 16, 2018 · 3 comments

Comments

@TheDan64
Copy link
Owner

It'd be pretty neat if you could do something like:

let foo = function! {
    foo(a: u32, b: CustomStruct, c: Ptr<i32>) -> u32 {
        entry:
            a <- 3
            br end
        end:
            return 1;
    }
};

Which would return a function value or Result<FunctionValue, Box<Error>> with that definition. It might need to make some assumptions, like always using the global Context, though. Maybe you could optionally specify a context.

@TheDan64
Copy link
Owner Author

Working with heterogeneous collections is a pain in rust.. We could also provide a macro which simplifies the process for the user. (We may even want this earlier than 0.3.0)

For example,

let values = mixed_value_array!(BasicValue, [
    float_value,
    int_value,
    ...
]);

If we do end up adding this macro, I'd strongly consider switching the current homogeneous function params from say, &[&BasicValue] to &[BasicValueEnum] since the former generally requires an extra heap allocation (unless you happen to have a fixed size of values on the stack, which probably isn't realistic) for the extra trait indirection which could be avoided.

Then, this macro could expand to something like let values: Vec<BasicValueEnum> = array.iter().map(|val| val.into()).collect(). Might need associated type to derive BasicValueEnum from BasicValue, though.

@71
Copy link
Contributor

71 commented Jun 16, 2018

The example function! macro you showed above would require the power brought by proc_macro, but it can be achieved similarly using a Lisp-like syntax (like clap.rs) with the macro_rules system.

Here's an example of doing dynamic code generation using this syntax: primitives.rs (note: it's part of a legacy compiler I wrote last year, the code is ugly in some places). The macro itself is defined here.

@TheDan64
Copy link
Owner Author

Proc macros are probably fine; I don't expect to get to this issue before Macros 2.0 lands in stable(it seems pretty close: rust-lang/rust#38356), but who knows.

@TheDan64 TheDan64 modified the milestones: 0.7.0, 0.8.0 Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants