From 5da37151874f7e3ca3026353f3f62079bec30aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 7 Jun 2022 16:53:58 +0200 Subject: [PATCH] constant: Add support for unary negation. --- src/bindgen/ir/constant.rs | 7 +++++++ tests/expectations/bitflags.both.c | 1 + tests/expectations/bitflags.both.compat.c | 1 + tests/expectations/bitflags.c | 1 + tests/expectations/bitflags.compat.c | 1 + tests/expectations/bitflags.cpp | 1 + tests/expectations/bitflags.pyx | 1 + tests/expectations/bitflags.tag.c | 1 + tests/expectations/bitflags.tag.compat.c | 1 + tests/expectations/bitflags.tag.pyx | 1 + tests/rust/bitflags.rs | 1 + 11 files changed, 17 insertions(+) diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index c181c3461..42182ab92 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -417,6 +417,13 @@ impl Literal { syn::Expr::Unary(syn::ExprUnary { ref op, ref expr, .. }) => match *op { + UnOp::Not(_) => { + let val = Self::load(expr)?; + Ok(Literal::PostfixUnaryOp { + op: "~", + value: Box::new(val), + }) + } UnOp::Neg(_) => { let val = Self::load(expr)?; Ok(Literal::PostfixUnaryOp { diff --git a/tests/expectations/bitflags.both.c b/tests/expectations/bitflags.both.c index 6003cfdad..b5cb6a7cf 100644 --- a/tests/expectations/bitflags.both.c +++ b/tests/expectations/bitflags.both.c @@ -50,5 +50,6 @@ typedef struct LargeFlags { * Flag with a very large shift that usually would be narrowed. */ #define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) } +#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits } void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags); diff --git a/tests/expectations/bitflags.both.compat.c b/tests/expectations/bitflags.both.compat.c index e5b560bba..35726624c 100644 --- a/tests/expectations/bitflags.both.compat.c +++ b/tests/expectations/bitflags.both.compat.c @@ -50,6 +50,7 @@ typedef struct LargeFlags { * Flag with a very large shift that usually would be narrowed. */ #define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) } +#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits } #ifdef __cplusplus extern "C" { diff --git a/tests/expectations/bitflags.c b/tests/expectations/bitflags.c index f8148db97..9af76af44 100644 --- a/tests/expectations/bitflags.c +++ b/tests/expectations/bitflags.c @@ -50,5 +50,6 @@ typedef struct { * Flag with a very large shift that usually would be narrowed. */ #define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) } +#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits } void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags); diff --git a/tests/expectations/bitflags.compat.c b/tests/expectations/bitflags.compat.c index 84b38e356..863de08b1 100644 --- a/tests/expectations/bitflags.compat.c +++ b/tests/expectations/bitflags.compat.c @@ -50,6 +50,7 @@ typedef struct { * Flag with a very large shift that usually would be narrowed. */ #define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) } +#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits } #ifdef __cplusplus extern "C" { diff --git a/tests/expectations/bitflags.cpp b/tests/expectations/bitflags.cpp index 49dadbadc..9df48b696 100644 --- a/tests/expectations/bitflags.cpp +++ b/tests/expectations/bitflags.cpp @@ -119,6 +119,7 @@ struct LargeFlags { }; /// Flag with a very large shift that usually would be narrowed. constexpr static const LargeFlags LargeFlags_LARGE_SHIFT = LargeFlags{ /* .bits = */ (uint64_t)(1ull << 44) }; +constexpr static const LargeFlags LargeFlags_INVERTED = LargeFlags{ /* .bits = */ (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }; extern "C" { diff --git a/tests/expectations/bitflags.pyx b/tests/expectations/bitflags.pyx index 68639f8b5..e3d104015 100644 --- a/tests/expectations/bitflags.pyx +++ b/tests/expectations/bitflags.pyx @@ -34,5 +34,6 @@ cdef extern from *: uint64_t bits; # Flag with a very large shift that usually would be narrowed. const LargeFlags LargeFlags_LARGE_SHIFT # = { (1ull << 44) } + const LargeFlags LargeFlags_INVERTED # = { ~(LargeFlags_LARGE_SHIFT).bits } void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags); diff --git a/tests/expectations/bitflags.tag.c b/tests/expectations/bitflags.tag.c index 0492d1205..c4be368d0 100644 --- a/tests/expectations/bitflags.tag.c +++ b/tests/expectations/bitflags.tag.c @@ -50,5 +50,6 @@ struct LargeFlags { * Flag with a very large shift that usually would be narrowed. */ #define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) } +#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits } void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags); diff --git a/tests/expectations/bitflags.tag.compat.c b/tests/expectations/bitflags.tag.compat.c index 52c1e8de7..d12ba40f3 100644 --- a/tests/expectations/bitflags.tag.compat.c +++ b/tests/expectations/bitflags.tag.compat.c @@ -50,6 +50,7 @@ struct LargeFlags { * Flag with a very large shift that usually would be narrowed. */ #define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) } +#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits } #ifdef __cplusplus extern "C" { diff --git a/tests/expectations/bitflags.tag.pyx b/tests/expectations/bitflags.tag.pyx index ba2d37f78..d5f2df6d5 100644 --- a/tests/expectations/bitflags.tag.pyx +++ b/tests/expectations/bitflags.tag.pyx @@ -34,5 +34,6 @@ cdef extern from *: uint64_t bits; # Flag with a very large shift that usually would be narrowed. const LargeFlags LargeFlags_LARGE_SHIFT # = { (1ull << 44) } + const LargeFlags LargeFlags_INVERTED # = { ~(LargeFlags_LARGE_SHIFT).bits } void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags); diff --git a/tests/rust/bitflags.rs b/tests/rust/bitflags.rs index 4d05cb08f..7e78bd815 100644 --- a/tests/rust/bitflags.rs +++ b/tests/rust/bitflags.rs @@ -34,6 +34,7 @@ bitflags! { pub struct LargeFlags: u64 { /// Flag with a very large shift that usually would be narrowed. const LARGE_SHIFT = 1u64 << 44; + const INVERTED = !Self::LARGE_SHIFT.bits; } }