root/trunk/docsrc/attribute.dd

Revision 2140, 12.3 kB (checked in by walter, 1 year ago)

bugzilla 1351 Discrepancies in the language specification

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