On Thursday, 22 February 2018 at 13:21:04 UTC, joe wrote:
On Monday, 12 February 2018 at 08:47:58 UTC, RazvanN wrote:
On Tuesday, 6 February 2018 at 12:03:06 UTC, joe wrote:
The FuncDeclaration node contains all the information for that.
For example, you can access fd.parent to see if the function is
declared at top-level (in which case, the parent is going to
be a module
declaration ) or if it is a nested function (in a class, in a
struct, in a function).
Every AST node contains information about the position in the
AST, all you
have to do is find how to get that information: which field to
access or which
member function to call.
Follow up question...
Why is *.parent always null?
extern(C++) class MyVisitor(AST): ParseTimeTransitiveVisitor!AST
override void visit(AST.Import i)
assert(i.parent is null); // always true
override void visitFuncBody(AST.FuncDeclaration f)
assert(f.parent is null); // always true
Indeed, @Stefan is right. The ParseTimeVisitor only contains
information available at parse time. If you are interested in the
parent you have 2 options: either (1) use the ParseTimeVisitor
and implement the AST traversal logic yourself or (2) you can use
the SemanticTimeTransitiveVisitor in which case the parent is not
going to be null. In the case of (2) you need to also do some
semantic analysis (so you need the whole dmd library, not just
the parsing one). Here's an example on using the dmd library
(including semantic) . You can copy paste that example and add
a few lines of code where you instantiate your visitor (which
will inherit SemanticTimeTransitiveVisitor).