forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattribute.dd
511 lines (418 loc) · 13.3 KB
/
attribute.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
Ddoc
$(SPEC_S Attributes,
$(GRAMMAR
$(GNAME AttributeSpecifier):
$(GLINK Attribute) $(B :)
$(GLINK Attribute) $(GLINK DeclarationBlock)
$(GNAME Attribute):
$(LINK2 #linkage, $(I LinkageAttribute))
$(LINK2 #align, $(I AlignAttribute))
$(GLINK2 pragma, Pragma)
$(LINK2 #deprecated, $(B deprecated))
$(GLINK ProtectionAttribute)
$(B static)
$(B extern)
$(B final)
$(B synchronized)
$(LINK2 #override, $(B override))
$(LINK2 #abstract, $(B abstract))
$(LINK2 #const, $(B const))
$(LINK2 #auto, $(B auto))
$(LINK2 #scope, $(B scope))
$(V2 $(LINK2 #gshared, $(B __gshared))
$(LINK2 #shared, $(B shared))
$(LINK2 #immutable, $(B immutable))
$(LINK2 #inout, $(B inout))
$(LINK2 #disable, $(B @disable))
)
$(GNAME DeclarationBlock):
$(GLINK2 module, DeclDef)
$(B {) $(GLINK2 module, DeclDefs)$(OPT) $(B })
)
$(P Attributes are a way to modify one or more declarations.
The general forms are:
)
---
attribute declaration; // affects the declaration
attribute: // affects all declarations until the end of
// the current scope
declaration;
declaration;
...
attribute { // affects all declarations in the block
declaration;
declaration;
...
}
---
$(P For attributes with an optional else clause:)
---
attribute
declaration;
else
declaration;
attribute { // affects all declarations in the block
declaration;
declaration;
...
}
else {
declaration;
declaration;
...
}
---
<h2>$(LNAME2 linkage, Linkage Attribute)</h2>
$(GRAMMAR
$(GNAME LinkageAttribute):
$(B extern) $(B $(LPAREN)) $(GLINK LinkageType) $(B $(RPAREN))
$(GNAME LinkageType):
$(B C)
$(B C++)
$(B D)
$(B Windows)
$(B Pascal)
$(B System)
)
$(P D provides an easy way to call C functions and operating
system API functions, as compatibility with both is essential.
The $(I LinkageType) is case sensitive, and is meant to be
extensible by the implementation ($(B they are not keywords)).
$(B C) and $(B D) must be supplied, the others are what
makes sense for the implementation.
$(B C++) is reserved for future use.
$(B System) is the same as $(B Windows) on Windows platforms,
and $(B C) on other platforms.
$(B Implementation Note:)
for Win32 platforms, $(B Windows) and $(B Pascal) should exist.
)
$(P C function calling conventions are
specified by:
)
---------------
extern (C):
int foo(); // call foo() with C conventions
---------------
$(P D conventions are:)
---------------
extern (D):
---------------
$(P or:)
---------------
extern:
---------------
$(P Windows API conventions are:)
---------------
extern (Windows):
void *VirtualAlloc(
void *lpAddress,
uint dwSize,
uint flAllocationType,
uint flProtect
);
---------------
<h2>$(LNAME2 align, Align Attribute)</h2>
$(GRAMMAR
$(GNAME AlignAttribute):
$(B align)
$(B align) $(B $(LPAREN)) $(GLINK2 lex, IntegerLiteral) $(B $(RPAREN))
)
$(P Specifies the alignment of struct members. $(B align) by itself
sets it to the default, which matches the default member alignment
of the companion C compiler. $(I Integer) specifies the alignment
which matches the behavior of the companion C compiler when non-default
alignments are used.
)
$(P Matching the behavior of the companion C compiler can have some
surprising results, such as the following for Digital Mars C++:
)
---------------
struct S {
align(4) byte a; // placed at offset 0
align(4) byte b; // placed at offset 1
}
---------------
$(P $(I AlignAttribute) is meant for C ABI compatiblity, which is not
the same thing as binary compatibility across diverse platforms.
For that, use packed structs:
)
---------------
align (1) struct S {
byte a; // placed at offset 0
byte[3] filler1;
byte b; // placed at offset 4
byte[3] filler2;
}
---------------
$(P A value of 1 means that no alignment is done;
members are packed together.
)
$(P Do not align references or pointers that were allocated
using $(I NewExpression) on boundaries that are not
a multiple of $(TT size_t). The garbage collector assumes that pointers
and references to gc allocated objects will be on $(TT size_t)
byte boundaries. If they are not, undefined behavior will
result.
)
$(P $(I AlignAttribute) is ignored when applied to declarations
that are not struct members.
Whether it applies to class members or not is implementation defined.
)
$(P The $(I AlignAttribute) is reset to the default when
entering a struct, union, class, or function scope, and restored
when exiting that scope.
It is not inherited from a base class.
)
<h2>$(LNAME2 deprecated, Deprecated Attribute)</h2>
$(P It is often necessary to deprecate a feature in a library,
yet retain it for backwards compatibility. Such
declarations can be marked as deprecated, which means
that the compiler can be set to produce an error
if any code refers to deprecated
declarations:
)
---------------
deprecated
{
void oldFoo();
}
---------------
$(P $(B Implementation Note:) The compiler should have a switch
specifying if deprecated declarations should be compiled with
out complaint or not.
)
<h2>Protection Attribute</h2>
$(GRAMMAR
$(GNAME ProtectionAttribute):
$(B private)
$(B package)
$(B protected)
$(B public)
$(B export)
)
$(P Protection is an attribute that is one of
$(B private), $(B package), $(B protected),
$(B public) or $(B export).
)
$(P Private means that only members of the enclosing class can access
the member, or members and functions in the same module as the
enclosing class.
Private members cannot be overridden.
Private module members are equivalent to $(B static) declarations
in C programs.
)
$(P Package extends private so that package members can be accessed
from code in other modules that are in the same package.
This applies to the innermost package only, if a module is in
nested packages.
)
$(P Protected means that only members of the enclosing class or any
classes derived from that class,
or members and functions in the same module
as the enclosing class, can access the member.
If accessing a protected instance member through a derived class member
function,
that member can only be accessed for the object instance
which is the $(SINGLEQUOTE this) object for the member function call.
Protected module members are illegal.
)
$(P Public means that any code within the executable can access the member.
)
$(P Export means that any code outside the executable can access the
member. Export
is analogous to exporting definitions from a DLL.
)
<h2>$(LNAME2 const, Const Attribute)</h2>
$(P The $(B const) attribute declares constants that can be
evaluated at compile time. For example:
)
---------------
const int foo = 7;
const {
double bar = foo + 6;
}
---------------
$(V1
$(P A const declaration without an initializer must be initialized
in a constructor (for class fields) or in a static constructor
(for static class members, or module variable declarations).
)
---------------
const int x;
const int y;
static this()
{
x = 3; // ok
// error: y not initialized
}
void foo()
{
x = 4; // error, x is const and not in static constructor
}
class C
{
const int a;
const int b;
static const int c;
static const int d;
this() {
a = 3; // ok
a = 4; // ok, multiple initialization allowed
C p = this;
p.a = 4; // error, only members of this instance
c = 5; // error, should initialize in static constructor
// error, b is not initialized
}
this(int x) {
this(); // ok, forwarding constructor
}
static this() {
c = 3; // ok
// error, d is not initialized
}
}
---------------
$(P It is not an error to have const module variable declarations without
initializers if there is no constructor. This is to support the practice
of having modules serve only as declarations that are not linked in,
the implementation of it will be in another module that is linked in.
)
)
$(V2
<h2>$(LNAME2 immutable, immutable Attribute)</h2>
<h2>$(LNAME2 gshared, __gshared Attribute)</h2>
<h2>$(LNAME2 shared, shared Attribute)</h2>
<h2>$(LNAME2 inout, inout Attribute)</h2>
<h2>$(LNAME2 disable, @disable Attribute)</h2>
$(P A reference to a declaration marked with the $(CODE @disable) attribute causes
a compile time error.
This can be used to explicitly disallow certain operations or overloads
at compile time rather than relying on generating a runtime error.
)
---
struct T {
@disable this(this) { } // disabling this makes T not copyable
}
struct S {
T t; // uncopyable member makes S also not copyable
}
@disable void foo() { }
void main() {
S s;
S t = s; // error, S is not copyable
foo(); // error, foo is disabled
}
---
)
<h2>$(LNAME2 override, Override Attribute)</h2>
$(P The $(B override) attribute applies to virtual functions.
It means that the function must override a function with the
same name and parameters in a base class. The override attribute
is useful for catching errors when a base class's member function
gets its parameters changed, and all derived classes need to have
their overriding functions updated.
)
---------------
class Foo {
int bar();
int abc(int x);
}
class Foo2 : Foo {
override {
int bar(char c); // error, no bar(char) in Foo
int abc(int x); // ok
}
}
---------------
<h2>Static Attribute</h2>
$(P The $(B static) attribute applies to functions and data.
It means that the declaration does not apply to a particular
instance of an object, but to the type of the object. In
other words, it means there is no $(B this) reference.
$(B static) is ignored when applied to other declarations.
)
---------------
class Foo {
static int bar() { return 6; }
int foobar() { return 7; }
}
...
Foo f = new Foo;
Foo.bar(); // produces 6
Foo.foobar(); // error, no instance of Foo
f.bar(); // produces 6;
f.foobar(); // produces 7;
---------------
$(P
Static functions are never virtual.
)
$(P
Static data has only one instance for the entire program,
not once per object.
)
$(P
Static does not have the additional C meaning of being local
to a file. Use the $(B private) attribute in D to achieve that.
For example:
)
---------------
module foo;
int x = 3; // x is global
private int y = 4; // y is local to module foo
---------------
<h2>$(LNAME2 auto, Auto Attribute)</h2>
$(P The $(B auto) attribute is used when there are no other attributes
and type inference is desired.
)
---
auto i = 6.8; // declare i as a double
---
<h2>$(LNAME2 scope, Scope Attribute)</h2>
$(P
The $(B scope) attribute is used for local variables and for class
declarations. For class declarations, the $(B scope) attribute creates
a $(I scope) class.
For local declarations, $(B scope) implements the RAII (Resource
Acquisition Is Initialization) protocol. This means that the
destructor for an object is automatically called when the
reference to it goes out of scope. The destructor is called even
if the scope is exited via a thrown exception, thus $(B scope)
is used to guarantee cleanup.
)
$(P
If there is more than one $(B scope) variable going out of scope
at the same point, then the destructors are called in the reverse
order that the variables were constructed.
)
$(P
$(B scope) cannot be applied to globals, statics, data members, ref
or out parameters. Arrays of $(B scope)s are not allowed, and $(B scope)
function return values are not allowed. Assignment to a $(B scope),
other than initialization, is not allowed.
$(B Rationale:) These restrictions may get relaxed in the future
if a compelling reason to appears.
)
<h2>$(LNAME2 abstract, Abstract Attribute)</h2>
$(P
If a class is abstract, it cannot be instantiated
directly. It can only be instantiated as a base class of
another, non-abstract, class.
)
$(P
Classes become abstract if they are defined within an
abstract attribute, or if any of the virtual member functions
within it are declared as abstract.
)
$(P
Non-virtual functions cannot be declared as abstract.
)
$(P
Functions declared as abstract can still have function
bodies. This is so that even though they must be overridden,
they can still provide $(SINGLEQUOTE base class functionality.)
)
)
Macros:
TITLE=Attributes
WIKI=Attribute
CATEGORY_SPEC=$0