Skip to content

Commit 0b956a0

Browse files
committed
add bitfields documentation
1 parent b60e972 commit 0b956a0

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
@@ -39,8 +39,14 @@ $(GNAME DeclaratorIdentifierList):
3939

4040
$(GNAME DeclaratorIdentifier):
4141
$(GLINK VarDeclaratorIdentifier)
42+
$(GLINK BitfieldDeclarator)
4243
$(GLINK AltDeclaratorIdentifier)
4344

45+
$(GNAME BitfieldDeclarator):
46+
$(D :) $(GLINK2 expression, AssignExpression)
47+
$(GLINK_LEX Identifier) $(D :) $(GLINK2 expression, AssignExpression)
48+
$(GLINK_LEX Identifier) $(D :) $(GLINK2 expression, AssignExpression) $(D =) $(GLINK Initializer)
49+
4450
$(GNAME VarDeclaratorIdentifier):
4551
$(GLINK_LEX Identifier)
4652
$(GLINK_LEX Identifier) $(GLINK2 template, TemplateParameters)$(OPT) $(D =) $(GLINK Initializer)

spec/struct.dd

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

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

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

0 commit comments

Comments
 (0)