Skip to content

Commit aeaa8c6

Browse files
authored
[spec/struct.dd] Improve struct initialization docs (#3217)
[spec/struct.dd] Improve struct initialization docs Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
1 parent 4c982a4 commit aeaa8c6

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

spec/declaration.dd

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ $(GNAME Initializer):
105105
$(GNAME NonVoidInitializer):
106106
$(GLINK2 expression, AssignExpression)$(LEGACY_LNAME2 ExpInitializer)
107107
$(GLINK ArrayInitializer)
108-
$(GLINK StructInitializer)
108+
$(GLINK2 struct, StructInitializer)$(LEGACY_LNAME2 StructInitializer)
109109

110110
$(GNAME ArrayInitializer):
111111
$(D [) $(GLINK ArrayMemberInitializations)$(OPT) $(D ])
@@ -118,18 +118,6 @@ $(GNAME ArrayMemberInitializations):
118118
$(GNAME ArrayMemberInitialization):
119119
$(GLINK NonVoidInitializer)
120120
$(GLINK2 expression, AssignExpression) $(D :) $(GLINK NonVoidInitializer)
121-
122-
$(GNAME StructInitializer):
123-
$(D {) $(GLINK StructMemberInitializers)$(OPT) $(D })
124-
125-
$(GNAME StructMemberInitializers):
126-
$(GLINK StructMemberInitializer)
127-
$(GLINK StructMemberInitializer) $(D ,)
128-
$(GLINK StructMemberInitializer) $(D ,) $(GSELF StructMemberInitializers)
129-
130-
$(GNAME StructMemberInitializer):
131-
$(GLINK NonVoidInitializer)
132-
$(GLINK_LEX Identifier) $(D :) $(GLINK NonVoidInitializer)
133121
)
134122

135123
$(H2 $(LNAME2 declaration_syntax, Declaration Syntax))

spec/struct.dd

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ S* p; // ok, knowledge of members is not necessary
136136
$(LINK2 https://en.wikipedia.org/wiki/Opaque_pointer, PIMPL idiom).)
137137

138138

139-
$(H2 $(LNAME2 default_struct_init, Default Initialization of Structs))
139+
$(H2 $(LNAME2 initialization, Initialization))
140+
141+
$(H3 $(LNAME2 default_struct_init, Default Initialization of Structs))
140142

141143
$(P Struct fields are by default initialized to whatever the
142144
$(GLINK2 declaration, Initializer) for the field is, and if none is supplied, to
@@ -154,10 +156,24 @@ $(H2 $(LNAME2 default_struct_init, Default Initialization of Structs))
154156

155157
$(P The default initializers may not contain references to mutable data.)
156158

157-
$(H2 $(LNAME2 static_struct_init, Static Initialization of Structs))
159+
$(H3 $(LNAME2 static_struct_init, Static Initialization of Structs))
160+
161+
$(GRAMMAR
162+
$(GNAME StructInitializer):
163+
$(D {) $(I StructMemberInitializers)$(OPT) $(D })
158164

159-
$(P If a $(GLINK2 declaration, StructInitializer) is supplied, the
160-
fields are initialized by the $(GLINK2 declaration, StructMemberInitializer) syntax.
165+
$(GNAME StructMemberInitializers):
166+
$(I StructMemberInitializer)
167+
$(I StructMemberInitializer) $(D ,)
168+
$(I StructMemberInitializer) $(D ,) $(GSELF StructMemberInitializers)
169+
170+
$(GNAME StructMemberInitializer):
171+
$(GLINK2 declaration, NonVoidInitializer)
172+
$(GLINK_LEX Identifier) $(D :) $(GLINK2 declaration, NonVoidInitializer)
173+
)
174+
175+
$(P If a $(I StructInitializer) is supplied, the
176+
fields are initialized by the $(I StructMemberInitializer) syntax.
161177
$(I StructMemberInitializers) with the $(I Identifier : NonVoidInitializer) syntax
162178
may be appear in any order, where $(I Identifier) is the field identifier.
163179
$(I StructMemberInitializer)s with the $(GLINK2 declaration, NonVoidInitializer) syntax
@@ -186,14 +202,12 @@ $(H2 $(LNAME2 static_struct_init, Static Initialization of Structs))
186202
---
187203
)
188204

189-
$(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
205+
$(H3 $(LNAME2 default_union_init, Default Initialization of Unions))
190206

191207
$(P Unions are by default initialized to whatever the
192208
$(GLINK2 declaration, Initializer) for the first field is, and if none is supplied, to
193209
the default initializer for the first field's type.
194-
)
195-
196-
$(P If the union is larger than the first field, the remaining bits
210+
If the union is larger than the first field, the remaining bits
197211
are set to 0.)
198212

199213
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -203,6 +217,8 @@ $(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
203217
---
204218
)
205219

220+
$(P It is an error to supply initializers for members other than the first one.)
221+
206222
$(SPEC_RUNNABLE_EXAMPLE_FAIL
207223
---
208224
union V { int a; long b = 4; } // error: union field `b` with default initialization `4` must be before field `a`
@@ -215,10 +231,10 @@ $(H2 $(LNAME2 default_union_init, Default Initialization of Unions))
215231
$(IMPLEMENTATION_DEFINED The values the fields other than the
216232
default initialized field are set to.)
217233

218-
$(H2 $(LNAME2 static_union_init, Static Initialization of Unions))
234+
$(H3 $(LNAME2 static_union_init, Static Initialization of Unions))
219235

220236
$(P Unions are initialized similarly to structs, except that only
221-
one initializer is allowed.)
237+
one member initializer is allowed.)
222238

223239
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
224240
---
@@ -240,7 +256,7 @@ $(H2 $(LNAME2 static_union_init, Static Initialization of Unions))
240256
$(IMPLEMENTATION_DEFINED The values the fields other than the
241257
initialized field are set to.)
242258

243-
$(H2 $(LNAME2 dynamic_struct_init, Dynamic Initialization of Structs))
259+
$(H3 $(LNAME2 dynamic_struct_init, Dynamic Initialization of Structs))
244260

245261
$(P The $(RELATIVE_LINK2 static_struct_init, static initializer syntax)
246262
can also be used to initialize non-static variables.
@@ -269,7 +285,7 @@ S s = t; // s.a is set to 3
269285
----
270286
)
271287

272-
$(P If the struct has a $(LINK2 #struct-constructor, constructor), and
288+
$(P If the struct has a $(RELATIVE_LINK2 struct-constructor, constructor), and
273289
the struct is initialized with a value that is of a different type,
274290
then the constructor is called:)
275291

@@ -289,7 +305,8 @@ S s = 3; // sets s.a to 3 using S's constructor
289305
----
290306
)
291307

292-
$(P If the struct does not have a constructor but $(D opCall) is
308+
$(P If the struct does not have a constructor but
309+
$(DDSUBLINK spec/operatoroverloading, FunctionCall, `opCall`) is
293310
overridden for the struct, and the struct is initialized with a value
294311
that is of a different type, then the $(D opCall) operator is called:)
295312

0 commit comments

Comments
 (0)