www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How does one determine the UDAs of all symbols contained in a given

reply Andrew Edwards <edwards.ac gmail.com> writes:
Given a module (somepackage.somemodule) how does one 
programmatically determine the symbols contained therein and 
associated UDAs?

Where symbol is a variable, function, UDT, etc... is this 
possible?

     foreach (symbol; somepackage.somemodule) {
         writeln(symbol.name, " attributes :")
         foreach(attribute; symbol) {
             writeln("\t", attribute);
         }
     }

I've looked at std.traits but for the most part the functionality 
I saw there only allowed me to grab attributes I already know of 
from symbols I already know... for instance:

      ("bold") string boldText;
     getUDAs!(boldText, "bold");

This also does not allow retrieving attributes from functions 
with void return-type. So this wouldn't work:

      ("bold") void decorateText(ref string data);
     getUDAs!(decorateText, "bold");
     // Error: cannot deduce function from arguments types 
!()(void, string);

What I'd like to do is open a module and evaluate each variable, 
instance variable, member variable, function, member function, 
etc... to see what UDAs they carry.

Please provide code sample or point me toward the appropriate 
documentation. Again, I've looked at std.traits 
(https://dlang.org/phobos/std_traits.html) and the related 
implementation 
(https://github.com/dlang/phobos/blob/master/std/traits.d#L7378-L7760) but
found nothing that would help me toward this end... or maybe I've missed
something?

Thanks,
Andrew
Jul 19 2017
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Wednesday, 19 July 2017 at 07:29:55 UTC, Andrew Edwards wrote:
 Given a module (somepackage.somemodule) how does one 
 programmatically determine the symbols contained therein and 
 associated UDAs?

 Where symbol is a variable, function, UDT, etc... is this 
 possible?

     foreach (symbol; somepackage.somemodule) {
         writeln(symbol.name, " attributes :")
         foreach(attribute; symbol) {
             writeln("\t", attribute);
         }
     }

 I've looked at std.traits but for the most part the 
 functionality I saw there only allowed me to grab attributes I 
 already know of from symbols I already know... for instance:

      ("bold") string boldText;
     getUDAs!(boldText, "bold");

 This also does not allow retrieving attributes from functions 
 with void return-type. So this wouldn't work:

      ("bold") void decorateText(ref string data);
     getUDAs!(decorateText, "bold");
     // Error: cannot deduce function from arguments types 
 !()(void, string);

 What I'd like to do is open a module and evaluate each 
 variable, instance variable, member variable, function, member 
 function, etc... to see what UDAs they carry.

 Please provide code sample or point me toward the appropriate 
 documentation. Again, I've looked at std.traits 
 (https://dlang.org/phobos/std_traits.html) and the related 
 implementation 
 (https://github.com/dlang/phobos/blob/master/std/traits.d#L7378-L7760) but
found nothing that would help me toward this end... or maybe I've missed
something?

 Thanks,
 Andrew
You'll want to use https://dlang.org/spec/traits.html#getMember in conjunction with https://dlang.org/spec/traits.html#getAttributes. Have a look some of the projects on github e.g. https://github.com/kaleidicassociates/excel-d/blob/master/source/xlld/wrap.d#L686
Jul 19 2017
parent reply Jacob Carlborg <doob me.com> writes:
On 2017-07-19 11:25, Nicholas Wilson wrote:

 You'll want to use https://dlang.org/spec/traits.html#getMember in 
 conjunction with https://dlang.org/spec/traits.html#getAttributes.
 
 Have a look some of the projects on github e.g. 
 https://github.com/kaleidicassociates/excel-d/blob/master/sou
ce/xlld/wrap.d#L686 
And https://dlang.org/spec/traits.html#allMembers to iterate all members in a module. -- /Jacob Carlborg
Jul 19 2017
parent reply Andrew Edwards <edwards.ac gmail.com> writes:
On Wednesday, 19 July 2017 at 11:28:30 UTC, Jacob Carlborg wrote:
 On 2017-07-19 11:25, Nicholas Wilson wrote:

 You'll want to use 
 https://dlang.org/spec/traits.html#getMember in conjunction 
 with https://dlang.org/spec/traits.html#getAttributes.
 
 Have a look some of the projects on github e.g. 
 https://github.com/kaleidicassociates/excel-d/blob/master/source/xlld/wrap.d#L686
And https://dlang.org/spec/traits.html#allMembers to iterate all members in a module.
Thanks Jacob and Nicholas... Brian Schott helped me out a bit on IRC earlier. I'm still not getting exactly what I'm looking for though so wanted to experiment a bit more before posting an update here. I'll check out the warp.d examples Nicholas linked to above. Andrew
Jul 19 2017
parent reply Jacob Carlborg <doob me.com> writes:
On 2017-07-19 13:49, Andrew Edwards wrote:

 Thanks Jacob and Nicholas... Brian Schott helped me out a bit on IRC 
 earlier. I'm still not getting exactly what I'm looking for though so 
 wanted to experiment a bit more before posting an update here. I'll 
 check out the warp.d examples Nicholas linked to above.
Here's an example: module mainMod; import std.meta; (1, 2) void foo() {} ("asd") void bar() {} void main() { foreach (m ; __traits(allMembers, mainMod)) { alias udas = AliasSeq!(__traits(getAttributes, mixin(m))); static if (udas.length > 0) pragma(msg, udas); } } And in runnable form [1]. This will iterate all members of the "mainMod" module and print all the UDAs, if the member has any. IF you want to access the UDAs of any classes or structs, then you need to recursively iterate the members. [1] https://is.gd/segmDD -- /Jacob Carlborg
Jul 19 2017
parent reply Andrew Edwards <edwards.ac gmail.com> writes:
On Wednesday, 19 July 2017 at 14:23:25 UTC, Jacob Carlborg wrote:
 Here's an example:
Thanks... Minus the AliasSeq bit, this is pretty much what I've been working with since talking to Brain. The main problem I'm facing is that it fails to compileif any of the symbols in the imported module is marked private. findudas.d-mixin-17(17): Error: ScopeDsymbol findudas.getModuleSymbols!"somepackage.mod1".__anonymous.__anonymous variable somepackage.mod1.workhorse is private findudas.d-mixin-17(17): Deprecation: somepackage.mod1.workhorse is not visible from module findudas findudas.d-mixin-21(21): Error: ScopeDsymbol findudas.getModuleSymbols!"somepackage.mod1".__anonymous.__anonymous variable somepackage.mod1.workhorse is private findudas.d-mixin-21(21): Deprecation: somepackage.mod1.workhorse is not visible from module findudas findudas.d(34): Error: template instance findudas.getModuleSymbols!"somepackage.mod1".getModuleSymbols.printUDAs!(mod1) error instantiating findudas.d(43): instantiated from here: getModuleSymbols!"somepackage.mod1"
Jul 20 2017
parent Jacob Carlborg <doob me.com> writes:
On 2017-07-21 06:16, Andrew Edwards wrote:

 Thanks... Minus the AliasSeq bit, this is pretty much what I've been 
 working with since talking to Brain. The main problem I'm facing is that 
 it fails to compileif any of the symbols in the imported module is 
 marked private.
Ah, yes. That's a known problem. I think it was agreed that everything should be accessible through introspection. -- /Jacob Carlborg
Jul 24 2017