Debian Codegraph @

gcc/d/dmd/attrib.d

raw
1 /**
2 * Defines declarations of various attributes.
3 *
4 * The term 'attribute' refers to things that can apply to a larger scope than a single declaration.
5 * Among them are:
6 * - Alignment (`align(8)`)
7 * - User defined attributes (`@UDA`)
8 * - Function Attributes (`@safe`)
9 * - Storage classes (`static`, `__gshared`)
10 * - Mixin declarations (`mixin("int x;")`)
11 * - Conditional compilation (`static if`, `static foreach`)
12 * - Linkage (`extern(C)`)
13 * - Anonymous structs / unions
14 * - Protection (`private`, `public`)
15 * - Deprecated declarations (`@deprecated`)
16 *
17 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
18 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
19 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
20 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/attrib.d, _attrib.d)
21 * Documentation: https://dlang.org/phobos/dmd_attrib.html
22 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/attrib.d
23 */
24
25 module dmd.attrib;
26
27 import dmd.aggregate;
28 import dmd.arraytypes;
29 import dmd.astenums;
30 import dmd.cond;
31 import dmd.declaration;
32 import dmd.dmodule;
33 import dmd.dscope;
34 import dmd.dsymbol;
35 import dmd.dsymbolsem : include;
36 import dmd.expression;
37 import dmd.func;
38 import dmd.globals;
39 import dmd.hdrgen : visibilityToBuffer;
40 import dmd.id;
41 import dmd.identifier;
42 import dmd.location;
43 import dmd.mtype;
44 import dmd.objc; // for objc.addSymbols
45 import dmd.common.outbuffer;
46 import dmd.root.array; // for each
47 import dmd.visitor;
48
49 /***********************************************************
50 * Abstract attribute applied to Dsymbol's used as a common
51 * ancestor for storage classes (StorageClassDeclaration),
52 * linkage (LinkageDeclaration) and others.
53 */
54 extern (C++) abstract class AttribDeclaration : Dsymbol
55 {
56 Dsymbols* decl; /// Dsymbol's affected by this AttribDeclaration
57
58 extern (D) this(Dsymbols* decl) @safe
59 {
60 super(DSYM.attribDeclaration);
61 this.decl = decl;
62 }
63
64 extern (D) this(Loc loc, Dsymbols* decl) @safe
65 {
66 super(DSYM.attribDeclaration, loc, null);
67 this.decl = decl;
68 }
69
70 extern (D) this(Loc loc, Identifier ident, Dsymbols* decl) @safe
71 {
72 super(DSYM.attribDeclaration, loc, ident);
73 this.decl = decl;
74 }
75
76 /****************************************
77 * Create a new scope if one or more given attributes
78 * are different from the sc's.
79 * If the returned scope != sc, the caller should pop
80 * the scope after it used.
81 */
82 extern (D) static Scope* createNewScope(Scope* sc, STC stc, LINK linkage,
83 CPPMANGLE cppmangle, Visibility visibility, int explicitVisibility,
84 AlignDeclaration aligndecl, PragmaDeclaration inlining)
85 {
86 Scope* sc2 = sc;
87 if (stc != sc.stc ||
88 linkage != sc.linkage ||
89 cppmangle != sc.cppmangle ||
90 explicitVisibility != sc.explicitVisibility ||
91 visibility != sc.visibility ||
92 aligndecl !is sc.aligndecl ||
93 inlining != sc.inlining)
94 {
95 // create new one for changes
96 sc2 = sc.copy();
97 sc2.stc = stc;
98 sc2.linkage = linkage;
99 sc2.cppmangle = cppmangle;
100 sc2.visibility = visibility;
101 sc2.explicitVisibility = explicitVisibility;
102 sc2.aligndecl = aligndecl;
103 sc2.inlining = inlining;
104 }
105 return sc2;
106 }
107
108 override const(char)* kind() const
109 {
110 return "attribute";
111 }
112
113 override final bool hasPointers()
114 {
115 return this.include(null).foreachDsymbol( (s) { return s.hasPointers(); } ) != 0;
116 }
117
118 /****************************************
119 */
120 override final void addObjcSymbols(ClassDeclarations* classes, ClassDeclarations* categories)
121 {
122 objc.addSymbols(this, classes, categories);
123 }
124
125 override void accept(Visitor v)
126 {
127 v.visit(this);
128 }
129 }
130
131 /***********************************************************
132 * Storage classes applied to Dsymbols, e.g. `const int i;`
133 *
134 * <stc> <decl...>
135 */
136 extern (C++) class StorageClassDeclaration : AttribDeclaration
137 {
138 STC stc;
139
140 extern (D) this(STC stc, Dsymbols* decl) @safe
141 {
142 super(decl);
143 this.stc = stc;
144 this.dsym = DSYM.storageClassDeclaration;
145 }
146
147 extern (D) this(Loc loc, STC stc, Dsymbols* decl) @safe
148 {
149 super(loc, decl);
150 this.stc = stc;
151 this.dsym = DSYM.storageClassDeclaration;
152 }
153
154 override StorageClassDeclaration syntaxCopy(Dsymbol s)
155 {
156 assert(!s);
157 return new StorageClassDeclaration(stc, Dsymbol.arraySyntaxCopy(decl));
158 }
159
160 override void accept(Visitor v)
161 {
162 v.visit(this);
163 }
164 }
165
166 /***********************************************************
167 * Deprecation with an additional message applied to Dsymbols,
168 * e.g. `deprecated("Superseeded by foo") int bar;`.
169 * (Note that `deprecated int bar;` is currently represented as a
170 * StorageClassDeclaration with STC.deprecated_)
171 *
172 * `deprecated(<msg>) <decl...>`
173 */
174 extern (C++) final class DeprecatedDeclaration : StorageClassDeclaration
175 {
176 Expression msg; /// deprecation message
177 const(char)* msgstr; /// cached string representation of msg
178
179 extern (D) this(Expression msg, Dsymbols* decl) @safe
180 {
181 super(STC.deprecated_, decl);
182 this.msg = msg;
183 }
184
185 override DeprecatedDeclaration syntaxCopy(Dsymbol s)
186 {
187 assert(!s);
188 return new DeprecatedDeclaration(msg.syntaxCopy(), Dsymbol.arraySyntaxCopy(decl));
189 }
190
191 override void accept(Visitor v)
192 {
193 v.visit(this);
194 }
195 }
196
197 /***********************************************************
198 * Linkage attribute applied to Dsymbols, e.g.
199 * `extern(C) void foo()`.
200 *
201 * `extern(<linkage>) <decl...>`
202 */
203 extern (C++) final class LinkDeclaration : AttribDeclaration
204 {
205 LINK linkage; /// either explicitly set or `default_`
206
207 extern (D) this(Loc loc, LINK linkage, Dsymbols* decl) @safe
208 {
209 super(loc, null, decl);
210 //printf("LinkDeclaration(linkage = %d, decl = %p)\n", linkage, decl);
211 this.linkage = linkage;
212 this.dsym = DSYM.linkDeclaration;
213 }
214
215 static LinkDeclaration create(Loc loc, LINK p, Dsymbols* decl) @safe
216 {
217 return new LinkDeclaration(loc, p, decl);
218 }
219
220 override LinkDeclaration syntaxCopy(Dsymbol s)
221 {
222 assert(!s);
223 return new LinkDeclaration(loc, linkage, Dsymbol.arraySyntaxCopy(decl));
224 }
225
226 override void accept(Visitor v)
227 {
228 v.visit(this);
229 }
230 }
231
232 /***********************************************************
233 * Attribute declaring whether an external aggregate should be mangled as
234 * a struct or class in C++, e.g. `extern(C++, struct) class C { ... }`.
235 * This is required for correct name mangling on MSVC targets,
236 * see cppmanglewin.d for details.
237 *
238 * `extern(C++, <cppmangle>) <decl...>`
239 */
240 extern (C++) final class CPPMangleDeclaration : AttribDeclaration
241 {
242 CPPMANGLE cppmangle;
243
244 extern (D) this(Loc loc, CPPMANGLE cppmangle, Dsymbols* decl) @safe
245 {
246 super(loc, null, decl);
247 //printf("CPPMangleDeclaration(cppmangle = %d, decl = %p)\n", cppmangle, decl);
248 this.cppmangle = cppmangle;
249 this.dsym = DSYM.cppMangleDeclaration;
250 }
251
252 override CPPMangleDeclaration syntaxCopy(Dsymbol s)
253 {
254 assert(!s);
255 return new CPPMangleDeclaration(loc, cppmangle, Dsymbol.arraySyntaxCopy(decl));
256 }
257
258 override void accept(Visitor v)
259 {
260 v.visit(this);
261 }
262 }
263
264 /**
265 * A node to represent an `extern(C++)` namespace attribute
266 *
267 * There are two ways to declarate a symbol as member of a namespace:
268 * `Nspace` and `CPPNamespaceDeclaration`.
269 * The former creates a scope for the symbol, and inject them in the
270 * parent scope at the same time.
271 * The later, this class, has no semantic implications and is only
272 * used for mangling.
273 * Additionally, this class allows one to use reserved identifiers
274 * (D keywords) in the namespace.
275 *
276 * A `CPPNamespaceDeclaration` can be created from an `Identifier`
277 * (already resolved) or from an `Expression`, which is CTFE-ed
278 * and can be either a `TupleExp`, in which can additional
279 * `CPPNamespaceDeclaration` nodes are created, or a `StringExp`.
280 *
281 * Note that this class, like `Nspace`, matches only one identifier
282 * part of a namespace. For the namespace `"foo::bar"`,
283 * the will be a `CPPNamespaceDeclaration` with its `ident`
284 * set to `"bar"`, and its `namespace` field pointing to another
285 * `CPPNamespaceDeclaration` with its `ident` set to `"foo"`.
286 */
287 extern (C++) final class CPPNamespaceDeclaration : AttribDeclaration
288 {
289 /// CTFE-able expression, resolving to `TupleExp` or `StringExp`
290 Expression exp;
291
292 extern (D) this(Loc loc, Identifier ident, Dsymbols* decl) @safe
293 {
294 super(loc, ident, decl);
295 this.dsym = DSYM.cppNamespaceDeclaration;
296 }
297
298 extern (D) this(Loc loc, Expression exp, Dsymbols* decl) @safe
299 {
300 super(loc, null, decl);
301 this.dsym = DSYM.cppNamespaceDeclaration;
302 this.exp = exp;
303 }
304
305 extern (D) this(Loc loc, Identifier ident, Expression exp, Dsymbols* decl,
306 CPPNamespaceDeclaration parent) @safe
307 {
308 super(loc, ident, decl);
309 this.dsym = DSYM.cppNamespaceDeclaration;
310 this.exp = exp;
311 this.cppnamespace = parent;
312 }
313
314 override CPPNamespaceDeclaration syntaxCopy(Dsymbol s)
315 {
316 assert(!s);
317 return new CPPNamespaceDeclaration(
318 this.loc, this.ident, this.exp, Dsymbol.arraySyntaxCopy(this.decl), this.cppnamespace);
319 }
320
321 override void accept(Visitor v)
322 {
323 v.visit(this);
324 }
325 }
326
327 /***********************************************************
328 * Visibility declaration for Dsymbols, e.g. `public int i;`
329 *
330 * `<visibility> <decl...>` or
331 * `package(<pkg_identifiers>) <decl...>` if `pkg_identifiers !is null`
332 */
333 extern (C++) final class VisibilityDeclaration : AttribDeclaration
334 {
335 Visibility visibility; /// the visibility
336 Identifier[] pkg_identifiers; /// identifiers for `package(foo.bar)` or null
337
338 /**
339 * Params:
340 * loc = source location of attribute token
341 * visibility = visibility attribute data
342 * decl = declarations which are affected by this visibility attribute
343 */
344 extern (D) this(Loc loc, Visibility visibility, Dsymbols* decl) @safe
345 {
346 super(loc, null, decl);
347 this.dsym = DSYM.visibilityDeclaration;
348 this.visibility = visibility;
349 //printf("decl = %p\n", decl);
350 }
351
352 /**
353 * Params:
354 * loc = source location of attribute token
355 * pkg_identifiers = list of identifiers for a qualified package name
356 * decl = declarations which are affected by this visibility attribute
357 */
358 extern (D) this(Loc loc, Identifier[] pkg_identifiers, Dsymbols* decl)
359 {
360 super(loc, null, decl);
361 this.dsym = DSYM.visibilityDeclaration;
362 this.visibility.kind = Visibility.Kind.package_;
363 this.pkg_identifiers = pkg_identifiers;
364 if (pkg_identifiers.length > 0)
365 {
366 Dsymbol tmp;
367 Package.resolve(pkg_identifiers, &tmp, null);
368 visibility.pkg = tmp ? tmp.isPackage() : null;
369 }
370 }
371
372 override VisibilityDeclaration syntaxCopy(Dsymbol s)
373 {
374 assert(!s);
375
376 if (visibility.kind == Visibility.Kind.package_)
377 return new VisibilityDeclaration(this.loc, pkg_identifiers, Dsymbol.arraySyntaxCopy(decl));
378 else
379 return new VisibilityDeclaration(this.loc, visibility, Dsymbol.arraySyntaxCopy(decl));
380 }
381
382 override const(char)* kind() const
383 {
384 return "visibility attribute";
385 }
386
387 override const(char)* toPrettyChars(bool)
388 {
389 assert(visibility.kind > Visibility.Kind.undefined);
390 OutBuffer buf;
391 visibilityToBuffer(buf, visibility);
392 return buf.extractChars();
393 }
394
395 override void accept(Visitor v)
396 {
397 v.visit(this);
398 }
399 }
400
401 /***********************************************************
402 * Alignment attribute for aggregates, members and variables.
403 *
404 * `align(<ealign>) <decl...>` or
405 * `align <decl...>` if `ealign` is null
406 */
407 extern (C++) final class AlignDeclaration : AttribDeclaration
408 {
409 Expressions* exps; /// Expression(s) yielding the desired alignment,
410 /// the largest value wins
411 /// the actual alignment is Unknown until it's either set to the value of `ealign`
412 /// or the default if `ealign` is null ( / an error ocurred)
413 structalign_t salign;
414
415
416 extern (D) this(Loc loc, Expression exp, Dsymbols* decl)
417 {
418 super(loc, null, decl);
419 this.dsym = DSYM.alignDeclaration;
420 if (exp)
421 {
422 exps = new Expressions();
423 exps.push(exp);
424 }
425 }
426
427 extern (D) this(Loc loc, Expressions* exps, Dsymbols* decl) @safe
428 {
429 super(loc, null, decl);
430 this.dsym = DSYM.alignDeclaration;
431 this.exps = exps;
432 }
433
434 extern (D) this(Loc loc, structalign_t salign, Dsymbols* decl) @safe
435 {
436 super(loc, null, decl);
437 this.dsym = DSYM.alignDeclaration;
438 this.salign = salign;
439 }
440
441 override AlignDeclaration syntaxCopy(Dsymbol s)
442 {
443 assert(!s);
444 return new AlignDeclaration(loc,
445 Expression.arraySyntaxCopy(exps),
446 Dsymbol.arraySyntaxCopy(decl));
447 }
448
449 override void accept(Visitor v)
450 {
451 v.visit(this);
452 }
453 }
454
455 /***********************************************************
456 * An anonymous struct/union (defined by `isunion`).
457 */
458 extern (C++) final class AnonDeclaration : AttribDeclaration
459 {
460 bool isunion; /// whether it's a union
461 int sem; /// 1 if successful semantic()
462 uint anonoffset; /// offset of anonymous struct
463 uint anonstructsize; /// size of anonymous struct
464 uint anonalignsize; /// size of anonymous struct for alignment purposes
465
466 extern (D) this(Loc loc, bool isunion, Dsymbols* decl) @safe
467 {
468 super(loc, null, decl);
469 this.dsym = DSYM.anonDeclaration;
470 this.isunion = isunion;
471 }
472
473 override AnonDeclaration syntaxCopy(Dsymbol s)
474 {
475 assert(!s);
476 return new AnonDeclaration(loc, isunion, Dsymbol.arraySyntaxCopy(decl));
477 }
478
479 override const(char)* kind() const
480 {
481 return (isunion ? "anonymous union" : "anonymous struct");
482 }
483
484 override void accept(Visitor v)
485 {
486 v.visit(this);
487 }
488 }
489
490 /***********************************************************
491 * Pragma applied to Dsymbols, e.g. `pragma(inline, true) void foo`,
492 * but not PragmaStatement's like `pragma(msg, "hello");`.
493 *
494 * pragma(<ident>, <args>)
495 */
496 extern (C++) final class PragmaDeclaration : AttribDeclaration
497 {
498 Expressions* args; /// parameters of this pragma
499
500 extern (D) this(Loc loc, Identifier ident, Expressions* args, Dsymbols* decl) @safe
501 {
502 super(loc, ident, decl);
503 this.dsym = DSYM.pragmaDeclaration;
504 this.args = args;
505 }
506
507 override PragmaDeclaration syntaxCopy(Dsymbol s)
508 {
509 //printf("PragmaDeclaration::syntaxCopy(%s)\n", toChars());
510 assert(!s);
511 return new PragmaDeclaration(loc, ident, Expression.arraySyntaxCopy(args), Dsymbol.arraySyntaxCopy(decl));
512 }
513
514 override const(char)* kind() const
515 {
516 return "pragma";
517 }
518
519 override void accept(Visitor v)
520 {
521 v.visit(this);
522 }
523 }
524
525 /***********************************************************
526 * A conditional compilation declaration, used for `version`
527 * / `debug` and specialized for `static if`.
528 *
529 * <condition> { <decl...> } else { <elsedecl> }
530 */
531 extern (C++) class ConditionalDeclaration : AttribDeclaration
532 {
533 Condition condition; /// condition deciding whether decl or elsedecl applies
534 Dsymbols* elsedecl; /// array of Dsymbol's for else block
535
536 extern (D) this(Loc loc, Condition condition, Dsymbols* decl, Dsymbols* elsedecl) @safe
537 {
538 super(loc, null, decl);
539 this.dsym = DSYM.conditionalDeclaration;
540 //printf("ConditionalDeclaration::ConditionalDeclaration()\n");
541 this.condition = condition;
542 this.elsedecl = elsedecl;
543 }
544
545 override ConditionalDeclaration syntaxCopy(Dsymbol s)
546 {
547 assert(!s);
548 return new ConditionalDeclaration(loc, condition.syntaxCopy(), Dsymbol.arraySyntaxCopy(decl), Dsymbol.arraySyntaxCopy(elsedecl));
549 }
550
551 override void accept(Visitor v)
552 {
553 v.visit(this);
554 }
555 }
556
557 /***********************************************************
558 * `<scopesym> {
559 * static if (<condition>) { <decl> } else { <elsedecl> }
560 * }`
561 */
562 extern (C++) final class StaticIfDeclaration : ConditionalDeclaration
563 {
564 ScopeDsymbol scopesym; /// enclosing symbol (e.g. module) where symbols will be inserted
565 bool addisdone = false; /// true if members have been added to scope
566 bool onStack = false; /// true if a call to `include` is currently active
567
568 extern (D) this(Loc loc, Condition condition, Dsymbols* decl, Dsymbols* elsedecl) @safe
569 {
570 super(loc, condition, decl, elsedecl);
571 this.dsym = DSYM.staticIfDeclaration;
572 //printf("StaticIfDeclaration::StaticIfDeclaration()\n");
573 }
574
575 override StaticIfDeclaration syntaxCopy(Dsymbol s)
576 {
577 assert(!s);
578 return new StaticIfDeclaration(loc, condition.syntaxCopy(), Dsymbol.arraySyntaxCopy(decl), Dsymbol.arraySyntaxCopy(elsedecl));
579 }
580
581 override const(char)* kind() const
582 {
583 return "static if";
584 }
585
586 override void accept(Visitor v)
587 {
588 v.visit(this);
589 }
590 }
591
592 /***********************************************************
593 * Static foreach at declaration scope, like:
594 * static foreach (i; [0, 1, 2]){ }
595 */
596
597 extern (C++) final class StaticForeachDeclaration : AttribDeclaration
598 {
599 StaticForeach sfe; /// contains `static foreach` expansion logic
600
601 ScopeDsymbol scopesym; /// cached enclosing scope (mimics `static if` declaration)
602
603 /++
604 `include` can be called multiple times, but a `static foreach`
605 should be expanded at most once. Achieved by caching the result
606 of the first call. We need both `cached` and `cache`, because
607 `null` is a valid value for `cache`.
608 +/
609 bool onStack = false;
610 bool cached = false;
611 Dsymbols* cache = null;
612
613 extern (D) this(StaticForeach sfe, Dsymbols* decl) @safe
614 {
615 super(sfe.loc, null, decl);
616 this.dsym = DSYM.staticForeachDeclaration;
617 this.sfe = sfe;
618 }
619
620 override StaticForeachDeclaration syntaxCopy(Dsymbol s)
621 {
622 assert(!s);
623 return new StaticForeachDeclaration(
624 sfe.syntaxCopy(),
625 Dsymbol.arraySyntaxCopy(decl));
626 }
627
628 override const(char)* kind() const
629 {
630 return "static foreach";
631 }
632
633 override void accept(Visitor v)
634 {
635 v.visit(this);
636 }
637 }
638
639 /***********************************************************
640 * Collection of declarations that stores foreach index variables in a
641 * local symbol table. Other symbols declared within are forwarded to
642 * another scope, like:
643 *
644 * static foreach (i; 0 .. 10) // loop variables for different indices do not conflict.
645 * { // this body is expanded into 10 ForwardingAttribDeclarations, where `i` has storage class STC.local
646 * mixin("enum x" ~ to!string(i) ~ " = i"); // ok, can access current loop variable
647 * }
648 *
649 * static foreach (i; 0.. 10)
650 * {
651 * pragma(msg, mixin("x" ~ to!string(i))); // ok, all 10 symbols are visible as they were forwarded to the global scope
652 * }
653 *
654 * static assert(!is(typeof(i))); // loop index variable is not visible outside of the static foreach loop
655 *
656 * A StaticForeachDeclaration generates one
657 * ForwardingAttribDeclaration for each expansion of its body. The
658 * AST of the ForwardingAttribDeclaration contains both the `static
659 * foreach` variables and the respective copy of the `static foreach`
660 * body. The functionality is achieved by using a
661 * ForwardingScopeDsymbol as the parent symbol for the generated
662 * declarations.
663 */
664
665 extern(C++) final class ForwardingAttribDeclaration : AttribDeclaration
666 {
667 ForwardingScopeDsymbol sym = null;
668
669 this(Dsymbols* decl) @safe
670 {
671 super(decl);
672 this.dsym = DSYM.forwardingAttribDeclaration;
673 sym = new ForwardingScopeDsymbol();
674 sym.symtab = new DsymbolTable();
675 }
676
677 override void accept(Visitor v)
678 {
679 v.visit(this);
680 }
681 }
682
683 /***********************************************************
684 * Mixin declarations, like:
685 * mixin("int x");
686 * https://dlang.org/spec/module.html#mixin-declaration
687 */
688 // Note: was CompileDeclaration
689 extern (C++) final class MixinDeclaration : AttribDeclaration
690 {
691 Expressions* exps;
692 ScopeDsymbol scopesym;
693 bool compiled;
694
695 extern (D) this(Loc loc, Expressions* exps) @safe
696 {
697 super(loc, null, null);
698 //printf("MixinDeclaration(loc = %d)\n", loc.linnum);
699 this.dsym = DSYM.mixinDeclaration;
700 this.exps = exps;
701 }
702
703 override MixinDeclaration syntaxCopy(Dsymbol s)
704 {
705 //printf("MixinDeclaration::syntaxCopy('%s')\n", toChars());
706 return new MixinDeclaration(loc, Expression.arraySyntaxCopy(exps));
707 }
708
709 override const(char)* kind() const
710 {
711 return "mixin";
712 }
713
714 override void accept(Visitor v)
715 {
716 v.visit(this);
717 }
718 }
719
720 /***********************************************************
721 * User defined attributes look like:
722 * @foo(args, ...)
723 * @(args, ...)
724 */
725 extern (C++) final class UserAttributeDeclaration : AttribDeclaration
726 {
727 Expressions* atts;
728
729 extern (D) this(Expressions* atts, Dsymbols* decl) @safe
730 {
731 super(decl);
732 this.dsym = DSYM.userAttributeDeclaration;
733 this.atts = atts;
734 }
735
736 override UserAttributeDeclaration syntaxCopy(Dsymbol s)
737 {
738 //printf("UserAttributeDeclaration::syntaxCopy('%s')\n", toChars());
739 assert(!s);
740 return new UserAttributeDeclaration(Expression.arraySyntaxCopy(this.atts), Dsymbol.arraySyntaxCopy(decl));
741 }
742
743 extern (D) static Expressions* concat(Expressions* udas1, Expressions* udas2)
744 {
745 Expressions* udas;
746 if (!udas1 || udas1.length == 0)
747 udas = udas2;
748 else if (!udas2 || udas2.length == 0)
749 udas = udas1;
750 else
751 {
752 /* Create a new tuple that combines them
753 * (do not append to left operand, as this is a copy-on-write operation)
754 */
755 udas = new Expressions(2);
756 (*udas)[0] = new TupleExp(Loc.initial, udas1);
757 (*udas)[1] = new TupleExp(Loc.initial, udas2);
758 }
759 return udas;
760 }
761
762 override const(char)* kind() const
763 {
764 return "UserAttribute";
765 }
766
767 override void accept(Visitor v)
768 {
769 v.visit(this);
770 }
771 }
772
773 /**
774 * Returns `true` if the given symbol is a symbol declared in
775 * `core.attribute` and has the given identifier.
776 *
777 * This is used to determine if a symbol is a UDA declared in
778 * `core.attribute`.
779 *
780 * Params:
781 * sym = the symbol to check
782 * ident = the name of the expected UDA
783 */
784 bool isCoreUda(Dsymbol sym, Identifier ident)
785 {
786 if (sym.ident != ident || !sym.parent)
787 return false;
788
789 auto _module = sym.parent.isModule();
790 return _module && _module.isCoreModule(Id.attribute);
791 }
792
793 /**
794 * Iterates the UDAs attached to the given symbol, without performing semantic
795 * analysis.
796 *
797 * Use this function instead of `foreachUda` if semantic analysis of `sym` is
798 * still in progress.
799 *
800 * Params:
801 * sym = the symbol to get the UDAs from
802 * dg = called once for each UDA
803 *
804 * Returns:
805 * If `dg` returns `!= 0`, stops the iteration and returns that value.
806 * Otherwise, returns 0.
807 */
808 int foreachUdaNoSemantic(Dsymbol sym, int delegate(Expression) dg)
809 {
810 if (sym.userAttribDecl is null || sym.userAttribDecl.atts is null)
811 return 0;
812
813 foreach (exp; *sym.userAttribDecl.atts)
814 {
815 if (auto result = dg(exp))
816 return result;
817 }
818
819 return 0;
820 }
821
822 /**
823 * Returns: true if the given expression is an enum from `core.attribute` named `id`
824 */
825 bool isEnumAttribute(Expression e, Identifier id)
826 {
827 import dmd.attrib : isCoreUda;
828 import dmd.id : Id;
829
830 // Logic based on dmd.objc.Supported.declaredAsOptionalCount
831 auto typeExp = e.isTypeExp;
832 if (!typeExp)
833 return false;
834
835 auto typeEnum = typeExp.type.isTypeEnum();
836 if (!typeEnum)
837 return false;
838
839 if (isCoreUda(typeEnum.sym, id))
840 return true;
841
842 return false;
843 }