Variable reference and variable inizializer

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Sep 7 09:32:29 PDT 2012


Hi Antonio,
Unfortunately, there's no easy way to go back to the variable AST node 
given a symbol - the compiler internally sets a flag on the variable 
symbol (HAS_INIT) if the variable has an initializer, but that's it 
(well, if the initializer is some constant value you will also get some 
extra stuff in the symbol too - see Symbol.getConstantValue). If your 
analysis doesn't care about fields, you could store all variable trees 
in a map when you do the visit, and then access them by the symbol you 
find on the ident.

Maurizio

On 07/09/12 17:17, Antonio Tancredi wrote:
> Good morning,
> I'm trying to create a simple AST visualizer using the OpenJDK 
> compiler and I have one problem.
> I hope is allowed to ask this kind of question in the mailing list. If 
> not, please excuse me.
>
> I'm trying to visit the JCCompilationUnit tree right after the Attr 
> phase. When I see a variable reference (a JCIdent where "sym.kind == 
> Kinds.VAR", if I'm correct), I want to retrieve the initializer (if 
> exists) for that variable.
> For initializer, I mean the JCExpression used for the "init" field of 
> the related JCVariableDecl tree.
>
> For instance, for this snippet
>
> [1] int x = 0;
> [2] int y = x+1;
>
> in the second line, when I find the JCIdent for "x", I also want to 
> find the JCLiteral (the initializer with value zero) from the first line.
>
> Is this possible using the compiler tools?
>
>
> Thank you,
>
>
> Antonio Tancredi




More information about the compiler-dev mailing list