forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.dd
673 lines (538 loc) · 18.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
Ddoc
$(SPEC_S Attributes,
$(GRAMMAR
$(GNAME AttributeSpecifier):
$(GLINK Attribute) $(D :)
$(GLINK Attribute) $(GLINK DeclarationBlock)
$(GNAME Attribute):
$(RELATIVE_LINK2 linkage, $(I LinkageAttribute))
$(RELATIVE_LINK2 align, $(I AlignAttribute))
$(GLINK2 pragma, Pragma)
$(RELATIVE_LINK2 deprecated, $(I DeprecatedAttribute))
$(GLINK ProtectionAttribute)
$(RELATIVE_LINK2 static, $(D static))
$(RELATIVE_LINK2 linkage, $(D extern))
$(D final)
$(D synchronized)
$(RELATIVE_LINK2 override, $(D override))
$(RELATIVE_LINK2 abstract, $(D abstract))
$(RELATIVE_LINK2 auto, $(D auto))
$(RELATIVE_LINK2 scope, $(D scope))
$(RELATIVE_LINK2 const, $(D const))
$(RELATIVE_LINK2 immutable, $(D immutable))
$(RELATIVE_LINK2 inout, $(D inout))
$(RELATIVE_LINK2 shared, $(D shared))
$(RELATIVE_LINK2 gshared, $(D __gshared))
$(RELATIVE_LINK2 disable, $(D @disable))
$(GNAME DeclarationBlock):
$(GLINK2 module, DeclDef)
$(D {) $(GLINK2 module, DeclDefs)$(OPT) $(D })
)
$(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;
...
}
---
$(H3 $(LNAME2 linkage, Linkage Attribute))
$(GRAMMAR
$(GNAME LinkageAttribute):
$(D extern) $(D $(LPAREN)) $(GLINK LinkageType) $(D $(RPAREN))
$(GNAME LinkageType):
$(D C)
$(D C++)
$(D D)
$(D Windows)
$(D Pascal)
$(D 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 ($(D they are not keywords)).
$(D C) and $(D D) must be supplied, the others are what
makes sense for the implementation.
$(D C++) is reserved for future use.
$(D System) is the same as $(D Windows) on Windows platforms,
and $(D C) on other platforms.
$(D Implementation Note:)
for Win32 platforms, $(D Windows) and $(D 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 Windows API conventions are:)
---------------
extern (Windows):
void *VirtualAlloc(
void *lpAddress,
uint dwSize,
uint flAllocationType,
uint flProtect
);
---------------
$(P Note that a lone $(D extern) declaration is used as a
$(XLINK2 declaration.html#extern, storage class).)
$(H3 $(LNAME2 align, Align Attribute))
$(GRAMMAR
$(GNAME AlignAttribute):
$(D align)
$(D align) $(D $(LPAREN)) $(GLINK2 lex, IntegerLiteral) $(D $(RPAREN))
)
$(P Specifies the alignment of:)
$(OL
$(LI variables)
$(LI struct fields)
$(LI union fields)
$(LI class fields)
$(LI struct, union, and class types)
)
$(P $(D align) by itself
sets it to the default, which matches the default member alignment
of the companion C compiler.)
--------
struct S {
align:
byte a; // placed at offset 0
int b; // placed at offset 4
long c; // placed at offset 8
}
auto sz = S.sizeof; // 16
--------
$(P $(I IntegerLiteral) specifies the alignment
which matches the behavior of the companion C compiler when non-default
alignments are used. It must be a positive power of 2.
)
$(P A value of 1 means that no alignment is done;
fields are packed together.
)
--------
struct S {
align (1):
byte a; // placed at offset 0
int b; // placed at offset 1
long c; // placed at offset 5
}
auto sz = S.sizeof; // 16
--------
$(P The alignment for the fields of an aggregate does not affect the alignment
of the aggregate itself - that is affected by the alignment setting outside
of the aggregate.)
--------
align (2) struct S {
align (1):
byte a; // placed at offset 0
int b; // placed at offset 1
long c; // placed at offset 5
}
auto sz = S.sizeof; // 14
--------
$(P Setting the alignment of a field aligns it to that power of 2, regardless
of the size of the field.)
--------
struct S {
align (4):
byte a; // placed at offset 0
byte b; // placed at offset 4
short c; // placed at offset 8
}
auto sz = S.sizeof; // 12
--------
$(P Do not align references or pointers that were allocated
using $(I NewExpression) on boundaries that are not
a multiple of $(D size_t). The garbage collector assumes that pointers
and references to gc allocated objects will be on $(D size_t)
byte boundaries. If they are not, undefined behavior will
result.
)
$(P The $(I AlignAttribute) is reset to the default when
entering a function scope or a non-anonymous struct, union, class, and restored
when exiting that scope.
It is not inherited from a base class.
)
$(H3 $(LNAME2 deprecated, Deprecated Attribute))
$(GRAMMAR
$(GNAME DeprecatedAttribute):
$(D deprecated)
$(D deprecated $(LPAREN)) $(GLINK2 lex, StringLiteral) $(D $(RPAREN))
)
$(P It is often necessary to deprecate a feature in a library,
yet retain it for backwards compatibility. Such
declarations can be marked as $(D deprecated), which means
that the compiler can be instructed to produce an error
if any code refers to deprecated declarations:
)
---------------
deprecated
{
void oldFoo();
}
oldFoo(); // Deprecated: function test.oldFoo is deprecated
---------------
$(P Optional $(GLINK2 lex, StringLiteral) can show additional
information in the deprecation message.
)
---------------
deprecated("Don't use bar") void oldBar();
oldBar(); // Deprecated: function test.oldBar is deprecated - Don't use bar
---------------
$(P $(D Implementation Note:) The compiler should have a switch
specifying if $(D deprecated) should be ignored, cause a warning, or cause an error during compilation.
)
$(H3 Protection Attribute)
$(GRAMMAR
$(GNAME ProtectionAttribute):
$(D private)
$(D package)
$(D protected)
$(D public)
$(D export)
)
$(P Protection is an attribute that is one of
$(D private), $(D package), $(D protected),
$(D public) or $(D 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 $(D 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 can be implicitly cast to the same type as $(SINGLEQUOTE this).
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.
)
$(P Protection does not participate in name lookup.
In particular, if two symbols with the same name are in scope,
and that name is used unqualified then the lookup will be ambiguous,
even if one of the symbols is inaccessible due to protection.
For example:
)
---------------
module A;
private class Foo {}
---------------
---------------
module B;
public class Foo {}
---------------
---------------
import A;
import B;
Foo f1; // error, could be either A.Foo or B.Foo
B.Foo f2; // ok
---------------
$(H3 $(LNAME2 const, $(D const) Attribute))
$(P The $(D const) attribute changes the type of the declared symbol from $(D T) to $(D const(T)),
where $(D T) is the type specified (or inferred) for the introduced symbol in the absence of $(D const).
)
---------------
const int foo = 7;
static assert(is(typeof(foo) == const(int)));
const {
double bar = foo + 6;
}
static assert(is(typeof(bar) == const(double)));
class C {
const void foo();
const {
void bar();
}
void baz() const;
}
pragma(msg, typeof(C.foo)); // const void()
pragma(msg, typeof(C.bar)); // const void()
pragma(msg, typeof(C.baz)); // const void()
static assert(is(typeof(C.foo) == typeof(C.bar)) &&
is(typeof(C.bar) == typeof(C.baz)));
---------------
$(H3 $(LNAME2 immutable, $(D immutable) Attribute))
$(P The $(D immutable) attribute modifies the type from $(D T) to $(D immutable(T)),
the same way as $(D const) does.
)
$(H3 $(LNAME2 inout, $(D inout) Attribute))
$(P The $(D inout) attribute modifies the type from $(D T) to $(D inout(T)),
the same way as $(D const) does.
)
$(H3 $(LNAME2 shared, $(D shared) Attribute))
$(P The $(D shared) attribute modifies the type from $(D T) to $(D shared(T)),
the same way as $(D const) does.
)
$(H3 $(LNAME2 gshared, $(D __gshared) Attribute))
$(P By default, non-immutable global declarations reside in thread local
storage. When a global variable is marked with the $(D __gshared)
attribute, its value is shared across all threads.)
---
int foo; // Each thread has its own exclusive copy of foo.
__gshared int bar; // bar is shared by all threads.
---
$(P $(D __gshared) may also be applied to member variables and local
variables. In these cases, $(D __gshared) is equivalent to $(D static),
except that the variable is shared by all threads rather than being
thread local.)
---
class Foo {
__gshared int bar;
}
int foo() {
__gshared int bar = 0;
return bar++; // Not thread safe.
}
---
$(P Unlike the $(D shared) attribute, $(D __gshared) provides no
safe-guards against data races or other multi-threaded synchronization
issues. It is the responsibility of the programmer to ensure that
access to variables marked $(D __gshared) is synchronized correctly.)
$(P $(D __gshared) is disallowed in safe mode.)
$(H3 $(LNAME2 disable, $(D @disable) Attribute))
$(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.
)
---
@disable void foo() { }
void main() {
foo(); // error, foo is disabled
}
---
$(P $(XLINK2 struct.html#Struct-Constructor, Disabling struct no-arg constructor)
disallows default construction of the struct.
)
$(P $(XLINK2 struct.html#StructPostblit, Disabling struct postblit)
makes the struct not copyable.
)
$(H3 $(LNAME2 override, Override Attribute))
$(P The $(D 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
}
}
---------------
$(H3 $(LNAME2 static, Static Attribute))
$(P The $(D 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 $(D this) reference.
$(D 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 $(D 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
---------------
$(H3 $(LNAME2 auto, Auto Attribute))
$(P The $(D auto) attribute is used when there are no other attributes
and type inference is desired.
)
---
auto i = 6.8; // declare i as a double
---
$(H3 $(LNAME2 scope, Scope Attribute))
$(P
The $(D scope) attribute is used for local variables and for class
declarations. For class declarations, the $(D scope) attribute creates
a $(I scope) class.
For local declarations, $(D 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 $(D scope)
is used to guarantee cleanup.
)
$(P
If there is more than one $(D 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
$(D scope) cannot be applied to globals, statics, data members, ref
or out parameters. Arrays of $(D scope)s are not allowed, and $(D scope)
function return values are not allowed. Assignment to a $(D scope),
other than initialization, is not allowed.
$(D Rationale:) These restrictions may get relaxed in the future
if a compelling reason to appears.
)
$(H3 $(LNAME2 abstract, Abstract Attribute))
$(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.)
)
$(SECTION3 $(LNAME2 uda, User Defined Attributes),
$(P
User Defined Attributes (UDA) are compile time expressions that can be attached
to a declaration. These attributes can then be queried, extracted, and manipulated
at compile time. There is no runtime component to them.
)
$(P
Grammatically, a UDA is a StorageClass:
)
$(GRAMMAR
$(GNAME StorageClass):
$(GLINK UserDefinedAttribute)
$(GNAME UserDefinedAttribute):
@(ArgumentList)
@CallExpression
)
$(P
And looks like:
)
---
@(3) int a;
@("string", 7) int b;
enum Foo;
@Foo int c;
struct Bar
{
int x;
}
@Bar(3) int d;
---
$(P
If there are multiple UDAs in scope for a declaration, they are concatenated:
)
---
@(1) {
@(2) int a; // has UDA's (1, 2)
@("string") int b; // has UDA's (1, "string")
}
---
$(P
UDA's can be extracted into an expression tuple using $(D __traits):
)
---
@('c') string s;
pragma(msg, __traits(getAttributes, s)); // prints tuple('c')
---
)
$(P
If there are no user defined attributes for the symbol, an empty tuple is returned.
The expression tuple can be turned into a manipulatable tuple:
)
---
template Tuple (T...)
{
alias T Tuple;
}
enum EEE = 7;
@("hello") struct SSS { }
@(3) { @(4) @EEE @SSS int foo; }
alias Tuple!(__traits(getAttributes, foo)) TP;
pragma(msg, TP); // prints tuple(3, 4, 7, (SSS))
pragma(msg, TP[2]); // prints 7
---
$(P
Of course the tuple types can be used to declare things:
)
---
TP[3] a; // a is declared as an SSS
---
$(P
The attribute of the type name is not the same as the attribute of the variable:
)
---
pragma(msg, __traits(getAttributes, typeof(a))); // prints tuple("hello")
---
$(P
Of course, the real value of UDA's is to be able to create user defined types with
specific values. Having attribute values of basic types does not scale.
The attribute tuples can be manipulated like any other tuple, and can be passed as
the argument list to a template.
)
$(P
Whether the attributes are values or types is up to the user, and whether later
attributes accumulate or override earlier ones is also up to how the user
interprets them.
)
)
Macros:
TITLE=Attributes
WIKI=Attribute
CATEGORY_SPEC=$0