diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-11-23 04:31:10 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-23 04:31:10 -0600 |
commit | 7c3584d7bc328a01ae5352e191f1fd41bb7fb1a9 (patch) | |
tree | ace279bfdbd625d446074c01b958d7b4d44c729a /bindgen-integration/cpp | |
parent | e3e6c730393f97daae93c2394d4af7bc9a5183b4 (diff) | |
parent | f0e05310b43e88e541ed011d20994c02fdcc1a3a (diff) |
Auto merge of #1158 - glyn:large-bitfield-units, r=emilio
Support bitfield allocation units larger than 64 bits
Individual bitfields are still limited to at most 64 bits, but this restriction can be weakened when Rust supports `u128`.
This implements issue #816.
Usage notes:
* Since common code is added to each generated binding, a program which uses
more than one binding may need to work around the duplication by including
each binding in its own module.
* The values created by bitfield allocation unit constructors can be assigned
directly to the corresponding struct fields with no need for transmutation.
Implementation notes:
`__BindgenBitfieldUnit` represents a bitfield allocation unit using a `Storage`
type accessible as a slice of `u8`. The alignment of the unit is inherited from
an `Align` type by virtue of the field:
```
align: [Align; 0],
```
The position of this field in the struct is irrelevant.
It is assumed that the alignment of the `Storage` type is no larger than the
alignment of the `Align` type, which will be true if the `Storage` type is, for
example, an array of `u8`. This assumption is checked in a debug assertion.
Although the double underscore (__) prefix is reserved for implementations of
C++, there are precedents for this convention elsewhere in bindgen and so the
convention is adopted here too.
Acknowledgement:
Thanks to @fitzgen for an initial implementation of `__BindgenBitfieldUnit` and
code to integrate it into bindgen.
r? @emilio
Diffstat (limited to 'bindgen-integration/cpp')
-rw-r--r-- | bindgen-integration/cpp/Test.cc | 45 | ||||
-rw-r--r-- | bindgen-integration/cpp/Test.h | 18 |
2 files changed, 49 insertions, 14 deletions
diff --git a/bindgen-integration/cpp/Test.cc b/bindgen-integration/cpp/Test.cc index f125109a..57c2186b 100644 --- a/bindgen-integration/cpp/Test.cc +++ b/bindgen-integration/cpp/Test.cc @@ -69,11 +69,11 @@ Date2::assert(unsigned short nWeekDay, unsigned short nYear, unsigned short byte) { - return this->nWeekDay == nWeekDay && - this->nMonthDay == nMonthDay && - this->nMonth == nMonth && - this->nYear == nYear && - this->byte == byte; + return this->nWeekDay == nWeekDay && + this->nMonthDay == nMonthDay && + this->nMonth == nMonth && + this->nYear == nYear && + this->byte == byte; } bool @@ -83,11 +83,11 @@ Fifth::assert(unsigned short nWeekDay, unsigned short nYear, unsigned char byte) { - return this->nWeekDay == nWeekDay && - this->nMonthDay == nMonthDay && - this->nMonth == nMonth && - this->nYear == nYear && - this->byte == byte; + return this->nWeekDay == nWeekDay && + this->nMonthDay == nMonthDay && + this->nMonth == nMonth && + this->nYear == nYear && + this->byte == byte; } bool @@ -95,10 +95,27 @@ Sixth::assert(unsigned char byte, unsigned char nWeekDay, unsigned char nMonth, unsigned char nMonthDay) { - return this->nWeekDay == nWeekDay && - this->nMonthDay == nMonthDay && - this->nMonth == nMonth && - this->byte == byte; + return this->nWeekDay == nWeekDay && + this->nMonthDay == nMonthDay && + this->nMonth == nMonth && + this->byte == byte; +}; + +bool +Seventh::assert(bool first, + int second, + unsigned short third, + unsigned int fourth, + unsigned short fifth, + bool sixth, + int seventh) { + return this->first_one_bit == first && + this->second_thirty_bits == second && + this->third_two_bits == third && + this->fourth_thirty_bits == fourth && + this->fifth_two_bits == fifth && + this->sixth_one_bit == sixth && + this->seventh_thirty_bits == seventh; }; } // namespace bitfields diff --git a/bindgen-integration/cpp/Test.h b/bindgen-integration/cpp/Test.h index e23a32e6..7ddb98bd 100644 --- a/bindgen-integration/cpp/Test.h +++ b/bindgen-integration/cpp/Test.h @@ -121,6 +121,24 @@ struct Sixth { unsigned char nMonthDay); }; +struct Seventh { + bool first_one_bit : 1; + unsigned int second_thirty_bits : 30; + unsigned short third_two_bits : 2; + unsigned int fourth_thirty_bits : 30; + unsigned short fifth_two_bits : 2; + bool sixth_one_bit : 1; + unsigned int seventh_thirty_bits : 30; + + /// Returns true if the bitfields match the arguments, false otherwise. + bool assert(bool first, + int second, + unsigned short third, + unsigned int fourth, + unsigned short fifth, + bool sixth, + int seventh); +}; } // namespace bitfields |