Skip to content

Commit ccdacb6

Browse files
committed
add bitfields documentation
1 parent 14afc7f commit ccdacb6

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

spec/declaration.dd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ $(GNAME DeclaratorIdentifierList):
5555
$(GNAME DeclaratorIdentifier):$(LEGACY_LNAME2 VarDeclaratorIdentifier)
5656
$(GLINK_LEX Identifier)
5757
$(GLINK_LEX Identifier) $(GLINK2 template, TemplateParameters)$(OPT) $(D =) $(GLINK Initializer)
58+
$(GLINK BitfieldDeclarator)
59+
60+
$(GNAME BitfieldDeclarator):
61+
$(D :) $(GLINK2 expression, AssignExpression)
62+
$(GLINK_LEX Identifier) $(D :) $(GLINK2 expression, AssignExpression)
63+
$(GLINK_LEX Identifier) $(D :) $(GLINK2 expression, AssignExpression) $(D =) $(GLINK Initializer)
5864

5965
$(GNAME Declarator):
6066
$(GLINK VarDeclarator)

spec/struct.dd

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,57 @@ $(H2 $(LNAME2 struct_layout, Struct Layout))
9595
$(LI Avoid using empty structs as parameters or arguments to variadic functions.)
9696
))
9797

98+
$(H2 $(LNAME2 bitfields, Bit Field Declarations))
99+
100+
$(P A bit field is declared with a $(GLINK2 declaration, BitfieldDeclarator). The bit field width
101+
is specified by the $(GLINK2 expression, AssignExpression). It is evaluated at compile time, and
102+
must result in an integer expression that ranges from 0 to the number of bits in type of the
103+
bit field.)
104+
105+
$(P The type of the bit field must be an integral type, signed or unsigned. The values assigned to
106+
bit fields must fit into the specified width. The bit field is placed into a unit of memory at least
107+
as large as needed to hold a value of the type of the bit field.)
108+
109+
$(P Anonymous bit fields do not have a $(GLINK_LEX Identifier) for them.)
110+
111+
$(P A zero width bit field must be anonymous. A zero width bit field causes the next bit field
112+
to be placed into the next unit.)
113+
114+
$(P The address of a bit field cannot be taken.
115+
$(D ref) declarations cannot be initialized from bit fields.
116+
Arrays of bit fields are not allowed.
117+
Bit fields can only be fields in structs, unions and classes.
118+
Bit fields must be non-static.
119+
)
120+
121+
---
122+
struct B
123+
{
124+
int x:3, y:2;
125+
}
126+
127+
static assert(B.sizeof == 4);
128+
129+
int vaporator(B b)
130+
{
131+
b.x = 4;
132+
b.y = 2;
133+
return b.x + b.y; // returns 6
134+
}
135+
---
136+
137+
$(IMPLEMENTATION_DEFINED Reading and writing a bit field may cause reads and writes of other bit
138+
fields in the same unit. Any use of bit fields where asynchronous access is possible will produce
139+
unreliable results.)
140+
141+
$(IMPLEMENTATION_DEFINED the layout of the bit fields is implementation defined. In practice, it is
142+
expected to be identical to that of ImportC.)
143+
144+
$(BEST_PRACTICE
145+
Because the layout of the bit fields is implementation defined, using them to match an externally
146+
defined layout is not a good idea. Use shifts and masks instead for such portability.
147+
)
148+
98149
$(H2 $(LNAME2 POD, Plain Old Data))
99150

100151
$(P A struct or union is $(I Plain Old Data) (POD) if it meets the following criteria:)

0 commit comments

Comments
 (0)