aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/ast/tpd.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2014-07-01 13:51:18 +0200
committerMartin Odersky <odersky@gmail.com>2014-07-17 11:01:59 +0200
commit2e15951a3a7e9dcb877a31ab4a9f32e428d47760 (patch)
treeaa2d14ffe905bba8e1311f14a0d8f39c7e13f5a6 /src/dotty/tools/dotc/ast/tpd.scala
parent8b93f7b4339abb4e376860770eb5b7ca271de71b (diff)
downloaddotty-2e15951a3a7e9dcb877a31ab4a9f32e428d47760.tar.gz
dotty-2e15951a3a7e9dcb877a31ab4a9f32e428d47760.tar.bz2
dotty-2e15951a3a7e9dcb877a31ab4a9f32e428d47760.zip
New utitility methods in tpd.
Added the following utility methods: - polyDefDef: Create a DefDef given a function that takes type and value parameters and yields a body. - appliedToTypeTrees: Apply function to type arguments ion a TypeApply if arguments are nonempty. - mkAsInstanceOf - ensureConforms: generate a cast if expression has non-conforming type.
Diffstat (limited to 'src/dotty/tools/dotc/ast/tpd.scala')
-rw-r--r--src/dotty/tools/dotc/ast/tpd.scala19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/dotty/tools/dotc/ast/tpd.scala b/src/dotty/tools/dotc/ast/tpd.scala
index 3b240ad2c..84e1118f9 100644
--- a/src/dotty/tools/dotc/ast/tpd.scala
+++ b/src/dotty/tools/dotc/ast/tpd.scala
@@ -182,7 +182,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
def DefDef(sym: TermSymbol, rhs: Tree = EmptyTree)(implicit ctx: Context): DefDef =
ta.assignType(DefDef(sym, Function.const(rhs) _), sym)
- def DefDef(sym: TermSymbol, rhsFn: List[List[Tree]] => Tree)(implicit ctx: Context): DefDef = {
+ def DefDef(sym: TermSymbol, rhsFn: List[List[Tree]] => Tree)(implicit ctx: Context): DefDef =
+ polyDefDef(sym, Function.const(rhsFn))
+
+ def polyDefDef(sym: TermSymbol, rhsFn: List[Type] => List[List[Tree]] => Tree)(implicit ctx: Context): DefDef = {
val (tparams, mtp) = sym.info match {
case tp: PolyType =>
val tparams = ctx.newTypeParams(sym, tp.paramNames, EmptyFlags, tp.instantiateBounds)
@@ -200,11 +203,12 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
case tp => (Nil, tp)
}
val (vparamss, rtp) = valueParamss(mtp)
+ val targs = tparams map (_.typeRef)
val argss = vparamss map (_ map (vparam => Ident(vparam.termRef)))
ta.assignType(
untpd.DefDef(
Modifiers(sym), sym.name, tparams map TypeDef,
- vparamss map (_ map (ValDef(_))), TypeTree(rtp), rhsFn(argss)), sym)
+ vparamss map (_ map (ValDef(_))), TypeTree(rtp), rhsFn(targs)(argss)), sym)
}
def TypeDef(sym: TypeSymbol)(implicit ctx: Context): TypeDef =
@@ -381,7 +385,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
new TreeTypeMap(ownerMap = (sym => if (sym == from) to else sym)).apply(tree)
def appliedToTypes(targs: List[Type])(implicit ctx: Context): Tree =
- if (targs.isEmpty) tree else TypeApply(tree, targs map (TypeTree(_)))
+ appliedToTypeTrees(targs map (TypeTree(_)))
+
+ def appliedToTypeTrees(targs: List[Tree])(implicit ctx: Context): Tree =
+ if (targs.isEmpty) tree else TypeApply(tree, targs)
}
implicit class ListOfTreeDecorator(val xs: List[tpd.Tree]) extends AnyVal {
@@ -451,6 +458,12 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
def mkAnd(tree1: Tree, tree2: Tree)(implicit ctx: Context) =
Apply(Select(tree1, defn.Boolean_and), tree2 :: Nil)
+ def mkAsInstanceOf(tree: Tree, pt: Type)(implicit ctx: Context): Tree =
+ TypeApply(Select(tree, defn.Any_asInstanceOf), TypeTree(pt) :: Nil)
+
+ def ensureConforms(tree: Tree, pt: Type)(implicit ctx: Context): Tree =
+ if (tree.tpe <:< pt) tree else mkAsInstanceOf(tree, pt)
+
// ensure that constructors are fully applied?
// ensure that normal methods are fully applied?