Skip to content

Commit 81fbea0

Browse files
authored
[spec] Add rationale for not supporting C-style declarations (#3707)
* [spec] Add rationale for not supporting C-Style declarations Also add some links. C-style declarations are illegal, not deprecated. * Clarify a C parser needs to lookup types * Use CPPCODE macro * Update multiple symbols
1 parent 008eff7 commit 81fbea0

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

spec/declaration.dd

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,14 @@ int[3][5] x; // x is a static array of 5 static arrays of 3 ints
128128
int[3]*[5] x; // x is a static array of 5 pointers to static arrays of 3 ints
129129
--------------------
130130

131-
$(H3 $(LNAME2 pointers-to-functions, Pointers to Functions))
131+
$(P See $(DDSUBLINK spec/type, pointers, Pointers), $(DDLINK spec/arrays, Arrays, Arrays)
132+
and $(GLINK2 type, TypeSuffix).)
133+
134+
$(H3 $(LNAME2 pointers-to-functions, Function Pointers))
132135

133136
$(P
134-
Pointers to functions are declared using the $(D function) keyword:
137+
$(DDSUBLINK spec/function, function-pointers, Function Pointers)
138+
are declared using the $(D function) keyword:
135139
)
136140

137141
--------------------
@@ -147,19 +151,31 @@ int function(char)[] x; // x is an array of
147151
$(H3 $(LNAME2 c-style-declarations, C-Style Declarations))
148152

149153
$(P
150-
C-style array, function pointer and pointer to array declarations are deprecated:
154+
C-style array, function pointer and pointer to array declarations are
155+
not supported. The following C declarations are for comparison only:
151156
)
152157

153-
--------------------
154-
int x[3]; // x is a static array of 3 ints
155-
int x[3][5]; // x is a static array of 3 arrays of 5 ints
158+
$(CPPCODE
159+
int x[3]; // C static array of 3 ints
160+
int x[3][5]; // C static array of 3 arrays of 5 ints
156161

157-
int (*x[5])[3]; // x is a static array of 5 pointers to static arrays of 3 ints
158-
int (*x)(char); // x is a pointer to a function taking a char argument
162+
int (\*x[5])[3]; // C static array of 5 pointers to static arrays of 3 ints
163+
int (\*x)(char); // C pointer to a function taking a char argument
159164
// and returning an int
160-
int (*[] x)(char); // x is an array of pointers to functions
165+
int (\*[] x)(char); // C array of pointers to functions
161166
// taking a char argument and returning an int
162-
--------------------
167+
)
168+
169+
$(RATIONALE
170+
* In D types are straightforward to read from right to left,
171+
unlike in C where parentheses are sometimes required and the type is
172+
read iteratively using the 'right-left' rule.
173+
* For a C function pointer declaration `a (*b)(c);` a C parser needs
174+
to attempt a type lookup in order to parse it unambiguously - it
175+
could be a call to a function called `a` which returns a function
176+
pointer, which is immediately called. D function pointer syntax
177+
is unambiguous, avoiding the need for types to be forward declared.
178+
)
163179

164180
$(H3 $(LNAME2 declaring-multiple-symbols, Declaring Multiple Symbols))
165181

@@ -169,14 +185,17 @@ must be of the same type:
169185
)
170186

171187
--------------------
172-
int x,y; // x and y are ints
173-
int* x,y; // x and y are pointers to ints
174-
int[] x,y; // x and y are arrays of ints
188+
int x, y; // x and y are ints
189+
int* x, y; // x and y are pointers to ints
190+
int[] x, y; // x and y are arrays of ints
191+
---
175192

176-
// invalid C-style declarations
177-
int x,*y; // error, multiple types
178-
int x[],y; // error, multiple types
179-
--------------------
193+
$(P This is in contrast to C:)
194+
195+
$(CPPCODE
196+
int x, *y; // x is an int, y is a pointer to int
197+
int x[], y; // x is an array/pointer, y is an int
198+
)
180199

181200
$(H2 $(LEGACY_LNAME2 AutoDeclaration, auto-declaration, Implicit Type Inference))
182201

0 commit comments

Comments
 (0)