Skip to content

Commit

Permalink
Support enum types in macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 21, 2024
1 parent 57ef7cf commit 34cdf9f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions bindgen-tests/tests/expectations/tests/macro_enum_retval.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions bindgen-tests/tests/headers/macro_enum_retval.h
@@ -0,0 +1,9 @@
// bindgen-flags: --experimental --generate-fn-macros

typedef enum {
wrong = 0,
right,
} truth;

#define get_wrong() wrong

12 changes: 10 additions & 2 deletions bindgen/ir/context.rs
Expand Up @@ -3238,15 +3238,23 @@ impl cmacro::CodegenContext for BindgenContext {
.last_callback(|c| c.fn_macro_arg_type(name, arg))
}

fn resolve_enum_variant(&self, variant: &str) -> Option<syn::Expr> {
fn resolve_enum_variant(
&self,
variant: &str,
) -> Option<(syn::Type, syn::Expr)> {
let (item_id, enum_variant) = self.enum_variant(variant)?;

let item = self.resolve_item(item_id);
if item.is_blocklisted(self) {
return None;
}

Some(enum_variant.clone())
let enum_ty = match item.kind() {
ItemKind::Type(enum_ty) => enum_ty.to_rust_ty_or_opaque(self, item),
_ => return None,
};

Some((enum_ty, enum_variant.clone()))
}

fn resolve_ty(&self, ty: &str) -> Option<syn::Type> {
Expand Down

0 comments on commit 34cdf9f

Please sign in to comment.