Thread overview
dmd TransitiveVistor read expressions
Oct 08
Andrey
Oct 08
Andrey
October 08
Hello, I'm trying to figure it out how dmd compiler is working and using his parser and tools for my little utility, I learned how to parse a file and working with parsed data via visitor, I wrote this:
> class CollectVisitor : TransitiveVisitor {
>     override void visit(ASTBase.Import imp) {
>         writeln();
>         writeln("visit import");
>     }
> 
>     override void visit(ASTBase.StructDeclaration d) {
>         writeln();
>         writeln("visit struct");
>     }
> 
>     override void visit(ASTBase.UserAttributeDeclaration d) {
>         writeln();
>         writeln("visit attribute");
>         writeln("attributes:");
> 
>         foreach (a; *d.atts) {
>             // How can I here read data from a?
>         }
> 
>         writeln("declarations:");
>         foreach (s; *d.decl) {
>             writeln("  ", s.ident.toString());
>         }
>     }
> }

I'm trying to parse this file:
> @Tag
> @Tag1
> struct A {
>     void greet() {
>         writeln("Hello world!");
>     }
> }
> 
> @Tag
> struct B {
>     void greet() {
>         writeln("Hello world!");
>     }
> }

I can parse it, and read some data from structs (so far only name), but how can I retrieve some info from UDAs? e.g. name? atts is array of expressions, but I don't understand how retrieve something from expression (I need name of attribute and its parameters).
October 08
I understood, I need to read the op field from expression and cast to needed expression type
October 09
On 2017-10-08 10:12, Andrey wrote:

> I can parse it, and read some data from structs (so far only name), but how can I retrieve some info from UDAs? e.g. name? atts is array of expressions, but I don't understand how retrieve something from expression (I need name of attribute and its parameters).

You can have a look here [1]. This looks for the @selector("foo") UDA on a method declaration, extracts the string "foo" and stores it for later processing.

[1] https://github.com/dlang/dmd/blob/master/src/ddmd/objc.d#L199

-- 
/Jacob Carlborg