I'm using APIs to reconstruct the scala AST during compiling.
I want to change an "Apply" AST like
a(1)
into an "Assign and Return" Block
{
val newvalue = a(1)
newvalue
}
And here's my code for generating a new value in AST:
val newVal : Tree = gen.mkPatDef(Typed(Ident(newTermName("newvalue")), returnType), a)(new FreshNameCreator("newvalue"))(0)
val newSymbol = newVal.symbol.newValue(newTermName("newvalue"), a.pos, 0)
newVal.setSymbol(newSymbol)
newVal.setType(a.tpe)
newVal.symbol.setName(newTermName("newvalue"))
newVal.symbol.setInfo(a.tpe)
In the code, "a" is the Apply AST
I do this change after scala compiler phase "packageobjects", the transformation of AST tree can be finished, but it always gets stuck after that. I guess the problem lies in the symbol for the "newvalue", because it works if I use an existing symbol of a existing value, instead of creating a new one by myself.
Thanks.