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

Support translation of C macros to Rust #16

Open
copy opened this issue Aug 10, 2018 · 6 comments
Open

Support translation of C macros to Rust #16

copy opened this issue Aug 10, 2018 · 6 comments
Assignees

Comments

@copy
Copy link

copy commented Aug 10, 2018

A feature request that would make it easier to work around the know limitations of C macros and variadic functions: It would be cool if c2rust could take an annotation or similar and turn C macro invocations and variadic function invocations into Rust macro invocations, but leave the Rust macro undefined and to be manually ported by the user.

@harpocrates
Copy link
Contributor

Dealing with macros in a sensible fashion is a long-term goal - but there is still a lot of design work to do.

For instance, it would be necessary to work (at least partially) with the un-preprocessed C source. That poses a pretty big challenge, since most of our current translation works on top of a Clang-produced C AST - which has to run the preprocessor in order to successfully parse and type check.

On possible approach to detecting C macros (and mapping them onto calls to Rust macros) might be to look for subtrees in the expanded C AST that were constructed entirely from tokens that map back to one macro call...

As for variadic functions, we're holding out for Rust language features (like rust-lang/rust#49878).

@thedataking thedataking changed the title C macro / variadic function -> Rust macro to be manually ported Support translation of C macros to Rust Apr 17, 2019
@rinon rinon self-assigned this Apr 18, 2019
@rinon
Copy link
Contributor

rinon commented Apr 18, 2019

I'm going to start taking a look into (simple) macro translation. The current plan is:

  • #define CONSTANT 1 -> Rust const
  • #ifdef CONSTANT -> Rust cfg! macro and attribute
    Constants that we recognize as compiler-provided platform constants should be translated to native rust versions when available, otherwise we emit these constants as feature flags.

@martin-t
Copy link

One possible approach to detecting C macros (and mapping them onto calls to Rust macros) might be to look for subtrees in the expanded C AST that were constructed entirely from tokens that map back to one macro call...

I can confirm that this approach works and can even support nested macros (taking other macros as arguments, calling other macros in the body or both combined). I have a proof of concept (only supports expressions) for a C-like lang here.

One downside is that there's no way to translate macros that are not used, is that acceptable for c2rust? (I am thinking about implementing this properly in c2rust, then forking it and using it as a basis for qc2rust. Not sure if/when i'll have the time so no promises.)

@thedataking
Copy link
Contributor

@martin-t unexpanded macros might be a problem when translating libraries. On the other hand, translating only used macros is better than what we do today, so yes, this is indeed acceptable. If you do get the time to implement this, I'd recommend you feature gate it like we did with const macros.

@XVilka
Copy link

XVilka commented Jun 29, 2021

This issue is quite old and relatively common in the code. Was there any progress with this? Or maybe some unmerged branch/PR somewhere? If not, where in the code it is probably should be implemented? Thank you for the answer.

@nacaclanga
Copy link

One other way to translate macros would be to use some sort of custom preprocessor:
Within the C code, we replace, (after checking if a macro forms a valid expression)

#define MACRONAME macroexpression

by

const _c2rust_macrotype _c2rust_mconst_MACRONAME = macroexpression;
#define MACRONAME _c2rust_mconst_MACRONAME

When translating the AST to rust, we truncate the _c2rust_mconst_ prefex from variables. If we found the const _c2rust_macrotype _c2rust_mconst_MACRONAME declaration, we walk the AST to guess the correct type and emit a Rust const declaration.

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

7 participants