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