From 5f674e44c5d3dccb55ce080c60d92b8e412d03ac Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 12 Sep 2012 19:32:28 -0700 Subject: Rescued TreeBuilder from the parser. For too long, Tree Builder has ruled over those who would like to build their own trees. Today marks the start of a new era. Trees are for building, not for parsers to hoard. It's in Global now. The particular motivation is exposing makeNew, and I also added makeAnonymousNew, so rather than this... Apply(Select( Block(List(ClassDef( Modifiers(scala.tools.nsc.symtab.Flags.FINAL), tpnme.ANON_CLASS_NAME, Nil, Template(List(Ident(definitions.AnyRefClass)), emptyValDef, List( DefDef(NoMods, nme.CONSTRUCTOR, Nil, List(Nil), TypeTree(), Block( List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), Nil)), Literal(Constant(())) )))))), Apply(Select(New(Ident(tpnme.ANON_CLASS_NAME)), nme.CONSTRUCTOR), Nil)) , sn.GetClass), Nil) We can write this. Apply(Select(makeAnonymousNew(Nil), nme.getClass_), Nil) --- src/compiler/scala/tools/nsc/Global.scala | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/compiler/scala/tools/nsc/Global.scala') diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index d101337087..9e3f47e8fb 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -13,7 +13,7 @@ import scala.collection.{ mutable, immutable } import io.{ SourceReader, AbstractFile, Path } import reporters.{ Reporter, ConsoleReporter } import util.{ Exceptional, ClassPath, MergedClassPath, StatisticsInfo, ScalaClassLoader, returning } -import scala.reflect.internal.util.{ NoPosition, SourceFile, NoSourceFile, BatchSourceFile, ScriptSourceFile } +import scala.reflect.internal.util.{ NoPosition, OffsetPosition, SourceFile, NoSourceFile, BatchSourceFile, ScriptSourceFile } import scala.reflect.internal.pickling.{ PickleBuffer, PickleFormat } import settings.{ AestheticSettings } import symtab.{ Flags, SymbolTable, SymbolLoaders, SymbolTrackers } @@ -96,6 +96,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter) /** Generate ASTs */ type TreeGen = scala.tools.nsc.ast.TreeGen + /** Tree generation, usually based on existing symbols. */ override object gen extends { val global: Global.this.type = Global.this } with TreeGen { @@ -103,6 +104,17 @@ class Global(var currentSettings: Settings, var reporter: Reporter) typer.typed(mkCast(tree, pt)) } + /** Trees fresh from the oven, mostly for use by the parser. */ + object treeBuilder extends { + val global: Global.this.type = Global.this + } with TreeBuilder { + def freshName(prefix: String): Name = freshTermName(prefix) + def freshTermName(prefix: String): TermName = currentUnit.freshTermName(prefix) + def freshTypeName(prefix: String): TypeName = currentUnit.freshTypeName(prefix) + def o2p(offset: Int): Position = new OffsetPosition(currentUnit.source, offset) + def r2p(start: Int, mid: Int, end: Int): Position = rangePos(currentUnit.source, start, mid, end) + } + /** Fold constants */ object constfold extends { val global: Global.this.type = Global.this -- cgit v1.2.3