# Is there any way to get ASTs' free vars and bound names when performing macros?

**URL:** <https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747>\
**Category:** General Usage\
**Tags:** package\
**Created:** [August 20, 2018, 5:33am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747 "2018-08-20T05:33:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [August 20, 2018, 5:33am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/1 "2018-08-20T05:33:01Z")

</div>

```julia
@mymacro function (arg1, arg2)
     bound1 = arg1 + arg2
     bound2 = bound1 + closure1
    .... 
end

```

Is there any builtin tool to supply this sort of analysis?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 20, 2018, 5:57am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/2 "2018-08-20T05:57:02Z")

</div>

Check out MacroTools.jl

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 20, 2018, 5:58am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/3 "2018-08-20T05:58:08Z")

</div>

Which sort of analysis?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 20, 2018, 7:08am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/4 "2018-08-20T07:08:17Z")

</div>

No, there is not. Julia figures this type of information (free and bound names) out after the macro expansion pass. All you have is what `dump` shows is in the AST. However, once you parsed the function header (preferably using MacroTools) you can do it yourself.

Good reads: [https://docs.julialang.org/en/stable/devdocs/ast/](https://docs.julialang.org/en/stable/devdocs/ast/), [https://docs.julialang.org/en/stable/devdocs/eval/](https://docs.julialang.org/en/stable/devdocs/eval/).

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [August 20, 2018, 8:54am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/5 "2018-08-20T08:54:21Z")

</div>

At least I want to get the variable names inside an AST and group them into `bound names` and `free vars`. It could be better if any type inference there.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [August 20, 2018, 9:00am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/6 "2018-08-20T09:00:54Z")

</div>

Yes, `postwalk` in `MacroTools.jl` could work but it’s still a heavy task to reach my goal from using MacroTools…

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [August 20, 2018, 9:07am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/7 "2018-08-20T09:07:25Z")

</div>

Did you see [GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for working with Julia code and expressions.](https://github.com/MikeInnes/MacroTools.jl#function-definitions)?

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [August 20, 2018, 9:34am UTC](https://discourse.julialang.org/t/is-there-any-way-to-get-asts-free-vars-and-bound-names-when-performing-macros/13747/8 "2018-08-20T09:34:25Z")

</div>

Thanks, I do have checked it out, but only the function signature is not enough, bound names except arguments and free vars are from function body.  
`splitdef` could extract the body for me and then I have to traverse it to get all of the variable names and group them into bound names and free vars.
