www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21969] New: importC: Error: bit fields are not supported

https://issues.dlang.org/show_bug.cgi?id=21969

          Issue ID: 21969
           Summary: importC: Error: bit fields are not supported
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ibuclaw gdcproject.org

Bitfields can be supported by merging and replacing them with a representative
field - ignoring any field name of the bitfield.
---
struct bitfieldsA
{
  int start;
  unsigned codes : 11;
  int end;
};

struct bitfieldsB
{
  int start;
  unsigned codes : 11;
  unsigned reserved : 5;
  unsigned flags : 16;
  int end;
};

struct bitfieldsC
{
  int start;
  unsigned codes : 11;
  unsigned reserved : 5;
  unsigned : 0;
  unsigned flags : 16;
  int end;
};
---

The above can be translated into the following D representation without
compromising ABI.
---
struct bitfieldsA
{
  int start;
  ushort __bitfield_padding_0;
  int end;
}

struct bitfieldsB
{
  int start;
  uint __bitfield_padding_0;
  int end;
}

struct bitfieldsC
{
  int start;
  ushort __bitfield_padding_0;
  ushort __bitfield_padding_1;
  int end;
}
---

As per the specification:

C99 6.7.2.1-11:
An implementation may allocate any addressable storage unit large enough to
hold a bit-field.  If enough space remains, a bit-field that immediately
follows another bit-field in a structure shall be packed into adjacent bits of
the same unit.  If insufficient space remains,whether a bit-field that does not
fit  is put into the next unit or overlaps adjacent units is
implementation-defined.

C99 6.7.2.1-12:
A bit-field declaration with no declarator, but only a colon and a  width,
indicates an unnamed bit-field. As a special case, a bit-field structure member
with a width of 0 indicates that no further bit-field is to be packed into the
unit in which the previous bit-field, if any, was placed.

--
May 25 2021