| 1 |
/** |
| 2 |
* Does semantic analysis for 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/attribsem.d, _attrib.d) |
| 21 |
* Documentation: https://dlang.org/phobos/dmd_attribsem.html |
| 22 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/attribsem.d |
| 23 |
*/ |
| 24 |
|
| 25 |
module dmd.attribsem; |
| 26 |
|
| 27 |
import dmd.arraytypes; |
| 28 |
import dmd.attrib; |
| 29 |
import dmd.dscope; |
| 30 |
import dmd.dsymbol; |
| 31 |
import dmd.expression; |
| 32 |
import dmd.expressionsem; |
| 33 |
import dmd.location; |
| 34 |
import dmd.root.array; // for each |
| 35 |
|
| 36 |
|
| 37 |
Expressions* getAttributes(UserAttributeDeclaration a) |
| 38 |
{ |
| 39 |
if (auto sc = a._scope) |
| 40 |
{ |
| 41 |
a._scope = null; |
| 42 |
arrayExpressionSemantic(a.atts.peekSlice(), sc); |
| 43 |
} |
| 44 |
auto exps = new Expressions(); |
| 45 |
if (a.userAttribDecl && a.userAttribDecl !is a) |
| 46 |
exps.push(new TupleExp(Loc.initial, a.userAttribDecl.getAttributes())); |
| 47 |
if (a.atts && a.atts.length) |
| 48 |
exps.push(new TupleExp(Loc.initial, a.atts)); |
| 49 |
return exps; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Iterates the UDAs attached to the given symbol. |
| 54 |
* |
| 55 |
* Params: |
| 56 |
* sym = the symbol to get the UDAs from |
| 57 |
* sc = scope to use for semantic analysis of UDAs |
| 58 |
* dg = called once for each UDA |
| 59 |
* |
| 60 |
* Returns: |
| 61 |
* If `dg` returns `!= 0`, stops the iteration and returns that value. |
| 62 |
* Otherwise, returns 0. |
| 63 |
*/ |
| 64 |
int foreachUda(Dsymbol sym, Scope* sc, int delegate(Expression) dg) |
| 65 |
{ |
| 66 |
if (!sym.userAttribDecl) |
| 67 |
return 0; |
| 68 |
|
| 69 |
auto udas = sym.userAttribDecl.getAttributes(); |
| 70 |
arrayExpressionSemantic(udas.peekSlice(), sc, true); |
| 71 |
|
| 72 |
return udas.each!((uda) { |
| 73 |
if (!uda.isTupleExp()) |
| 74 |
return 0; |
| 75 |
|
| 76 |
auto exps = uda.isTupleExp().exps; |
| 77 |
|
| 78 |
return exps.each!((e) { |
| 79 |
assert(e); |
| 80 |
|
| 81 |
if (auto result = dg(e)) |
| 82 |
return result; |
| 83 |
|
| 84 |
return 0; |
| 85 |
}); |
| 86 |
}); |
| 87 |
} |