Skip to content

Commit 5da3715

Browse files
committed
constant: Add support for unary negation.
1 parent 021d09d commit 5da3715

File tree

11 files changed

+17
-0
lines changed

11 files changed

+17
-0
lines changed

src/bindgen/ir/constant.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ impl Literal {
417417
syn::Expr::Unary(syn::ExprUnary {
418418
ref op, ref expr, ..
419419
}) => match *op {
420+
UnOp::Not(_) => {
421+
let val = Self::load(expr)?;
422+
Ok(Literal::PostfixUnaryOp {
423+
op: "~",
424+
value: Box::new(val),
425+
})
426+
}
420427
UnOp::Neg(_) => {
421428
let val = Self::load(expr)?;
422429
Ok(Literal::PostfixUnaryOp {

tests/expectations/bitflags.both.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ typedef struct LargeFlags {
5050
* Flag with a very large shift that usually would be narrowed.
5151
*/
5252
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
53+
#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }
5354

5455
void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags);

tests/expectations/bitflags.both.compat.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef struct LargeFlags {
5050
* Flag with a very large shift that usually would be narrowed.
5151
*/
5252
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
53+
#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }
5354

5455
#ifdef __cplusplus
5556
extern "C" {

tests/expectations/bitflags.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ typedef struct {
5050
* Flag with a very large shift that usually would be narrowed.
5151
*/
5252
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
53+
#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }
5354

5455
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);

tests/expectations/bitflags.compat.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef struct {
5050
* Flag with a very large shift that usually would be narrowed.
5151
*/
5252
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
53+
#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }
5354

5455
#ifdef __cplusplus
5556
extern "C" {

tests/expectations/bitflags.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ struct LargeFlags {
119119
};
120120
/// Flag with a very large shift that usually would be narrowed.
121121
constexpr static const LargeFlags LargeFlags_LARGE_SHIFT = LargeFlags{ /* .bits = */ (uint64_t)(1ull << 44) };
122+
constexpr static const LargeFlags LargeFlags_INVERTED = LargeFlags{ /* .bits = */ (uint64_t)~(LargeFlags_LARGE_SHIFT).bits };
122123

123124
extern "C" {
124125

tests/expectations/bitflags.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ cdef extern from *:
3434
uint64_t bits;
3535
# Flag with a very large shift that usually would be narrowed.
3636
const LargeFlags LargeFlags_LARGE_SHIFT # = <LargeFlags>{ <uint64_t>(1ull << 44) }
37+
const LargeFlags LargeFlags_INVERTED # = <LargeFlags>{ <uint64_t>~(LargeFlags_LARGE_SHIFT).bits }
3738

3839
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);

tests/expectations/bitflags.tag.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ struct LargeFlags {
5050
* Flag with a very large shift that usually would be narrowed.
5151
*/
5252
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
53+
#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }
5354

5455
void root(struct AlignFlags flags, struct DebugFlags bigger_flags, struct LargeFlags largest_flags);

tests/expectations/bitflags.tag.compat.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct LargeFlags {
5050
* Flag with a very large shift that usually would be narrowed.
5151
*/
5252
#define LargeFlags_LARGE_SHIFT (LargeFlags){ .bits = (uint64_t)(1ull << 44) }
53+
#define LargeFlags_INVERTED (LargeFlags){ .bits = (uint64_t)~(LargeFlags_LARGE_SHIFT).bits }
5354

5455
#ifdef __cplusplus
5556
extern "C" {

tests/expectations/bitflags.tag.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ cdef extern from *:
3434
uint64_t bits;
3535
# Flag with a very large shift that usually would be narrowed.
3636
const LargeFlags LargeFlags_LARGE_SHIFT # = <LargeFlags>{ <uint64_t>(1ull << 44) }
37+
const LargeFlags LargeFlags_INVERTED # = <LargeFlags>{ <uint64_t>~(LargeFlags_LARGE_SHIFT).bits }
3738

3839
void root(AlignFlags flags, DebugFlags bigger_flags, LargeFlags largest_flags);

tests/rust/bitflags.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ bitflags! {
3434
pub struct LargeFlags: u64 {
3535
/// Flag with a very large shift that usually would be narrowed.
3636
const LARGE_SHIFT = 1u64 << 44;
37+
const INVERTED = !Self::LARGE_SHIFT.bits;
3738
}
3839
}
3940

0 commit comments

Comments
 (0)