This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Quick compiler internals query

4 replies
Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Do we have a mechanism in the compiler to view a Tree node as it existed before being transformed by a previous phase?I've browsed the source, but nothing obvious stands out

Miguel Garcia
Joined: 2009-06-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Quick compiler internals query

Kevin,

Sometimes one needs to compare the pre and the post versions of a Tree after
a transformation. Not sure if that's what you're asking about. I never
streamlined a workflow for that, but some unpolished ideas are:

1) subclass LazyTreeCopier to trace where updates occurred. As a reminder
:-) the typical AST-node-handler in LazyTreeCopier looks as follows:

def ClassDef(tree: Tree, mods: Modifiers, name: Name, tparams:
List[TypeDef], impl: Template) = tree match {
case t @ ClassDef(mods0, name0, tparams0, impl0)
if (mods0 == mods) && (name0 == name) && (tparams0 == tparams) &&
(impl0 == impl) => t
case _ => treeCopy.ClassDef(tree, mods, name, tparams, impl)
}

2) serialize the ASTs pre and post transformation into XML, then use an XML
diff tool

3) the Model-Driven community shares utilities for comparing objects graphs,
for example
http://www.eclipsecon.org/2008/sub/attachments/Comparing_and_Merging_Mod...

Miguel
http://www.sts.tu-harburg.de/people/mi.garcia

Miguel Garcia
Joined: 2009-06-10,
User offline. Last seen 42 years 45 weeks ago.
an easy way to compare AST trees across compilation phases

I've settled on the following procedure, which involves writing zero lines
of code.

1) run the compiler to get a textual representation of the AST just before
the transformation of interest, e.g.
-Xprint:explicitouter -Yshow-trees

2) ditto for the phase afterwards

3) let the IDE compare both text files

Before noticing NodePrinters, I even considered visiting the AST using
reflection and Integer.toHexString(System.identityHashCode(object))

Good that I didn't get carried away with that :-)

Miguel
http://www.sts.tu-harburg.de/people/mi.garcia

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 4 days ago.
Re: an easy way to compare AST trees across compilation phases
Useful stuff :)
Do we have a summary anywhere of all the -X and -Y options?Unfortunately, the descriptions in Settings.scala aren't always as helpful as perhaps they could be.

On Sat, Dec 5, 2009 at 8:39 PM, Miguel Garcia <miguel.garcia@tuhh.de> wrote:

I've settled on the following procedure, which involves writing zero lines of code.

1) run the compiler to get a textual representation of the AST just before the transformation of interest, e.g.
 -Xprint:explicitouter -Yshow-trees

2) ditto for the phase afterwards

3) let the IDE compare both text files

Before noticing NodePrinters, I even considered visiting the AST using reflection and Integer.toHexString(System.identityHashCode(object))

Good that I didn't get carried away with that :-)


Miguel
http://www.sts.tu-harburg.de/people/mi.garcia

Miguel Garcia
Joined: 2009-06-10,
User offline. Last seen 42 years 45 weeks ago.
Re: an easy way to compare AST trees across compilation phases

> Do we have a summary anywhere of all the -X and -Y options?
> Unfortunately, the descriptions in Settings.scala aren't always as helpful
> as perhaps they could be.

Hmmm, besides:

- placing the cursor on a setting in Settings.scala and letting the IDE
find usages for it
- browsing the changesets involving some setting

No, I guess that's all there is :-)

Miguel
http://www.sts.tu-harburg.de/people/mi.garcia

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland