| 1 |
/** |
| 2 |
* This module contains UDA's (User Defined Attributes) either used in |
| 3 |
* the runtime or special UDA's recognized by compiler. |
| 4 |
* |
| 5 |
* $(SCRIPT inhibitQuickIndex = 1;) |
| 6 |
* $(BOOKTABLE Cheat Sheet, |
| 7 |
* $(THEAD Attribute Name, Linkage, Description) |
| 8 |
* $(TROW $(LREF gnuAbiTag), C++, |
| 9 |
* Declares an ABI tag on a C++ symbol.) |
| 10 |
* $(TROW $(LREF mustuse),, |
| 11 |
* Ensures that values of a struct or union type are not discarded.) |
| 12 |
* $(TROW $(LREF optional), Objective-C, |
| 13 |
* Makes an Objective-C interface method optional.) |
| 14 |
* $(TROW $(LREF selector), Objective-C, |
| 15 |
* Attaches an Objective-C selector to a method.) |
| 16 |
* $(TROW $(LREF standalone),, |
| 17 |
* Marks a shared module constructor as not depending on any |
| 18 |
* other module constructor being run first.) |
| 19 |
* $(TROW $(LREF weak),, |
| 20 |
* Specifies that a global symbol should be emitted with weak linkage.) |
| 21 |
* ) |
| 22 |
* |
| 23 |
* Copyright: Copyright Jacob Carlborg 2015. |
| 24 |
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 25 |
* Authors: Jacob Carlborg |
| 26 |
* Source: $(DRUNTIMESRC core/_attribute.d) |
| 27 |
*/ |
| 28 |
|
| 29 |
/* Copyright Jacob Carlborg 2015. |
| 30 |
* Distributed under the Boost Software License, Version 1.0. |
| 31 |
* (See accompanying file LICENSE or copy at |
| 32 |
* http://www.boost.org/LICENSE_1_0.txt) |
| 33 |
*/ |
| 34 |
module core.attribute; |
| 35 |
|
| 36 |
version (GNU) |
| 37 |
public import gcc.attributes; |
| 38 |
|
| 39 |
version (LDC) |
| 40 |
public import ldc.attributes; |
| 41 |
|
| 42 |
version (D_ObjectiveC) |
| 43 |
{ |
| 44 |
version = UdaOptional; |
| 45 |
version = UdaSelector; |
| 46 |
} |
| 47 |
|
| 48 |
version (Posix) |
| 49 |
version = UdaGNUAbiTag; |
| 50 |
|
| 51 |
version (CoreDdoc) |
| 52 |
{ |
| 53 |
version = UdaGNUAbiTag; |
| 54 |
version = UdaOptional; |
| 55 |
version = UdaSelector; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Use this attribute to specify that a global symbol should be emitted with |
| 60 |
* weak linkage. This is primarily useful in defining library functions that |
| 61 |
* can be overridden by user code, though it can also be used with shared and |
| 62 |
* static variables too. |
| 63 |
* |
| 64 |
* The overriding symbol must have the same type as the weak symbol. In |
| 65 |
* addition, if it designates a variable it must also have the same size and |
| 66 |
* alignment as the weak symbol. |
| 67 |
* |
| 68 |
* Quote from the LLVM manual: "Note that weak linkage does not actually allow |
| 69 |
* the optimizer to inline the body of this function into callers because it |
| 70 |
* doesn’t know if this definition of the function is the definitive definition |
| 71 |
* within the program or whether it will be overridden by a stronger |
| 72 |
* definition." |
| 73 |
* |
| 74 |
* This attribute is only meaningful to the GNU and LLVM D compilers. The |
| 75 |
* Digital Mars D compiler emits all symbols with weak linkage by default. |
| 76 |
*/ |
| 77 |
version (DigitalMars) |
| 78 |
{ |
| 79 |
enum weak; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
// GDC and LDC declare this attribute in their own modules. |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Use this attribute to attach an Objective-C selector to a method. |
| 88 |
* |
| 89 |
* This is a special compiler recognized attribute, it has several |
| 90 |
* requirements, which all will be enforced by the compiler: |
| 91 |
* |
| 92 |
* $(UL |
| 93 |
* $(LI |
| 94 |
* The attribute can only be attached to methods or constructors which |
| 95 |
* have Objective-C linkage. That is, a method or a constructor in a |
| 96 |
* class or interface declared as $(D_CODE extern(Objective-C)). |
| 97 |
* ) |
| 98 |
* |
| 99 |
* $(LI It cannot be attached to a method or constructor that is a template) |
| 100 |
* |
| 101 |
* $(LI |
| 102 |
* The number of colons in the string need to match the number of |
| 103 |
* arguments the method accept. |
| 104 |
* ) |
| 105 |
* |
| 106 |
* $(LI It can only be used once in a method declaration) |
| 107 |
* ) |
| 108 |
* |
| 109 |
* Examples: |
| 110 |
* --- |
| 111 |
* extern (Objective-C) |
| 112 |
* class NSObject |
| 113 |
* { |
| 114 |
* this() @selector("init"); |
| 115 |
* static NSObject alloc() @selector("alloc"); |
| 116 |
* NSObject initWithUTF8String(in char* str) @selector("initWithUTF8String:"); |
| 117 |
* ObjcObject copyScriptingValue(ObjcObject value, NSString key, NSDictionary properties) |
| 118 |
* @selector("copyScriptingValue:forKey:withProperties:"); |
| 119 |
* } |
| 120 |
* --- |
| 121 |
*/ |
| 122 |
version (UdaSelector) struct selector |
| 123 |
{ |
| 124 |
string selector; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Use this attribute to make an Objective-C interface method optional. |
| 129 |
* |
| 130 |
* An optional method is a method that does **not** have to be implemented in |
| 131 |
* the class that implements the interface. To safely call an optional method, |
| 132 |
* a runtime check should be performed to make sure the receiver implements the |
| 133 |
* method. |
| 134 |
* |
| 135 |
* This is a special compiler recognized attribute, it has several |
| 136 |
* requirements, which all will be enforced by the compiler: |
| 137 |
* |
| 138 |
* * The attribute can only be attached to methods which have Objective-C |
| 139 |
* linkage. That is, a method inside an interface declared as `extern (Objective-C)` |
| 140 |
* |
| 141 |
* * It can only be used for methods that are declared inside an interface |
| 142 |
* * It can only be used once in a method declaration |
| 143 |
* * It cannot be attached to a method that is a template |
| 144 |
* |
| 145 |
* Examples: |
| 146 |
* --- |
| 147 |
* import core.attribute : optional, selector; |
| 148 |
* |
| 149 |
* extern (Objective-C): |
| 150 |
* |
| 151 |
* struct objc_selector; |
| 152 |
* alias SEL = objc_selector*; |
| 153 |
* |
| 154 |
* SEL sel_registerName(in char* str); |
| 155 |
* |
| 156 |
* extern class NSObject |
| 157 |
* { |
| 158 |
* bool respondsToSelector(SEL sel) @selector("respondsToSelector:"); |
| 159 |
* } |
| 160 |
* |
| 161 |
* interface Foo |
| 162 |
* { |
| 163 |
* @optional void foo() @selector("foo"); |
| 164 |
* @optional void bar() @selector("bar"); |
| 165 |
* } |
| 166 |
* |
| 167 |
* class Bar : NSObject |
| 168 |
* { |
| 169 |
* static Bar alloc() @selector("alloc"); |
| 170 |
* Bar init() @selector("init"); |
| 171 |
* |
| 172 |
* void bar() @selector("bar") |
| 173 |
* { |
| 174 |
* } |
| 175 |
* } |
| 176 |
* |
| 177 |
* extern (D) void main() |
| 178 |
* { |
| 179 |
* auto bar = Bar.alloc.init; |
| 180 |
* |
| 181 |
* if (bar.respondsToSelector(sel_registerName("bar"))) |
| 182 |
* bar.bar(); |
| 183 |
* } |
| 184 |
* --- |
| 185 |
*/ |
| 186 |
version (UdaOptional) |
| 187 |
enum optional; |
| 188 |
|
| 189 |
/** |
| 190 |
* Use this attribute to declare an ABI tag on a C++ symbol. |
| 191 |
* |
| 192 |
* ABI tag is an attribute introduced by the GNU C++ compiler. |
| 193 |
* It modifies the mangled name of the symbol to incorporate the tag name, |
| 194 |
* in order to distinguish from an earlier version with a different ABI. |
| 195 |
* |
| 196 |
* This is a special compiler recognized attribute, it has a few |
| 197 |
* requirements, which all will be enforced by the compiler: |
| 198 |
* |
| 199 |
* $(UL |
| 200 |
* $(LI |
| 201 |
* There can only be one such attribute per symbol. |
| 202 |
* ) |
| 203 |
* $(LI |
| 204 |
* The attribute can only be attached to an `extern(C++)` symbol |
| 205 |
* (`struct`, `class`, `enum`, function, and their templated counterparts). |
| 206 |
* ) |
| 207 |
* $(LI |
| 208 |
* The attribute cannot be applied to C++ namespaces. |
| 209 |
* This is to prevent confusion with the C++ semantic, which allows it to |
| 210 |
* be applied to namespaces. |
| 211 |
* ) |
| 212 |
* $(LI |
| 213 |
* The string arguments must only contain valid characters |
| 214 |
* for C++ name mangling which currently include alphanumerics |
| 215 |
* and the underscore character. |
| 216 |
* ) |
| 217 |
* ) |
| 218 |
* |
| 219 |
* This UDA is not transitive, and inner scope do not inherit outer scopes' |
| 220 |
* ABI tag. See examples below for how to translate a C++ declaration to D. |
| 221 |
* Also note that entries in this UDA will be automatically sorted |
| 222 |
* alphabetically, hence `gnuAbiTag("c", "b", "a")` will appear as |
| 223 |
* `@gnuAbiTag("a", "b", "c")`. |
| 224 |
* |
| 225 |
* See_Also: |
| 226 |
* $(LINK2 https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.abi-tag, Itanium ABI spec) |
| 227 |
* $(LINK2 https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html, GCC attributes documentation). |
| 228 |
* |
| 229 |
* Examples: |
| 230 |
* --- |
| 231 |
* // ---- foo.cpp |
| 232 |
* struct [[gnu::abi_tag ("tag1", "tag2")]] Tagged1_2 |
| 233 |
* { |
| 234 |
* struct [[gnu::abi_tag ("tag3")]] Tagged3 |
| 235 |
* { |
| 236 |
* [[gnu::abi_tag ("tag4")]] |
| 237 |
* int Tagged4 () { return 42; } |
| 238 |
* } |
| 239 |
* } |
| 240 |
* Tagged1_2 inst1; |
| 241 |
* // ---- foo.d |
| 242 |
* @gnuAbiTag("tag1", "tag2") struct Tagged1_2 |
| 243 |
* { |
| 244 |
* // Notice the repetition |
| 245 |
* @gnuAbiTag("tag1", "tag2", "tag3") struct Tagged3 |
| 246 |
* { |
| 247 |
* @gnuAbiTag("tag1", "tag2", "tag3", "tag4") int Tagged4 (); |
| 248 |
* } |
| 249 |
* } |
| 250 |
* extern __gshared Tagged1_2 inst1; |
| 251 |
* --- |
| 252 |
*/ |
| 253 |
version (UdaGNUAbiTag) struct gnuAbiTag |
| 254 |
{ |
| 255 |
string[] tags; |
| 256 |
|
| 257 |
this(string[] tags...) @safe pure nothrow |
| 258 |
{ |
| 259 |
this.tags = tags.dup; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Use this attribute to ensure that values of a `struct` or `union` type are |
| 265 |
* not discarded. |
| 266 |
* |
| 267 |
* The value of an expression is considered to be discarded if |
| 268 |
* |
| 269 |
* $(UL |
| 270 |
* $(LI |
| 271 |
* the expression is the top-level expression in a statement or the |
| 272 |
* left-hand expression in a comma expression, and |
| 273 |
* ) |
| 274 |
* $(LI |
| 275 |
* the expression is not an assignment (`=`, `+=`, etc.), increment |
| 276 |
* (`++`), or decrement (`--`) expression. |
| 277 |
* ) |
| 278 |
* ) |
| 279 |
* |
| 280 |
* If the declaration of a `struct` or `union` type has the `@mustuse` |
| 281 |
* attribute, the compiler will emit an error any time a value of that type |
| 282 |
* would be discarded. |
| 283 |
* |
| 284 |
* Currently, `@mustuse` is only recognized by the compiler when attached to |
| 285 |
* `struct` and `union` declarations. To allow for future expansion, attaching |
| 286 |
* `@mustuse` to a `class`, `interface`, `enum`, or function declaration is |
| 287 |
* currently forbidden, and will result in a compile-time error. All other uses |
| 288 |
* of `@mustuse` are ignored. |
| 289 |
* |
| 290 |
* Examples: |
| 291 |
* --- |
| 292 |
* @mustuse struct ErrorCode { int value; } |
| 293 |
* |
| 294 |
* extern(C) ErrorCode doSomething(); |
| 295 |
* |
| 296 |
* void main() |
| 297 |
* { |
| 298 |
* // error: would discard a value of type ErrorCode |
| 299 |
* //doSomething(); |
| 300 |
* |
| 301 |
* ErrorCode result; |
| 302 |
* // ok: value is assigned to a variable |
| 303 |
* result = doSomething(); |
| 304 |
* |
| 305 |
* // ok: can ignore the value explicitly with a cast |
| 306 |
* cast(void) doSomething(); |
| 307 |
* } |
| 308 |
* --- |
| 309 |
*/ |
| 310 |
enum mustuse; |
| 311 |
|
| 312 |
/** |
| 313 |
* Use this attribute to indicate that a shared module constructor does not depend on any |
| 314 |
* other module constructor being run first. This avoids errors on cyclic module constructors. |
| 315 |
* |
| 316 |
* However, it is now up to the user to enforce safety. |
| 317 |
* The module constructor must be marked `@system` as a result. |
| 318 |
* Prefer to refactor the module constructor causing the cycle so it's in its own module if possible. |
| 319 |
* |
| 320 |
* This is only allowed on `shared` static constructors, not thread-local module constructors. |
| 321 |
*/ |
| 322 |
enum standalone; |