diff options
author | Martin Odersky <odersky@gmail.com> | 2013-02-19 16:22:04 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2013-02-19 16:22:04 +0100 |
commit | b72ee12e9eeffe32ffd752c5aaf9ef1713d77286 (patch) | |
tree | 0a6bf27e6251ad4d2f62ca91a470ef57a4faafa0 /src | |
parent | c1ece3bd0bd67e69c13f5529b02061aaa8330b9a (diff) | |
download | dotty-b72ee12e9eeffe32ffd752c5aaf9ef1713d77286.tar.gz dotty-b72ee12e9eeffe32ffd752c5aaf9ef1713d77286.tar.bz2 dotty-b72ee12e9eeffe32ffd752c5aaf9ef1713d77286.zip |
Making TypedTrees a full alternative to Trees
TypedTrees now has the full functionality of Trees. The idea is that a client should inherit from either Trees or TypedTrees, and the rest follows automatically.
Diffstat (limited to 'src')
-rw-r--r-- | src/dotty/tools/dotc/core/Annotations.scala | 34 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/TypedTrees.scala | 408 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/pickling/ClassfileParser.scala | 14 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/pickling/UnPickler.scala | 28 |
4 files changed, 269 insertions, 215 deletions
diff --git a/src/dotty/tools/dotc/core/Annotations.scala b/src/dotty/tools/dotc/core/Annotations.scala index 9cbf5c8e4..c91d0c409 100644 --- a/src/dotty/tools/dotc/core/Annotations.scala +++ b/src/dotty/tools/dotc/core/Annotations.scala @@ -1,53 +1,53 @@ package dotty.tools.dotc.core -import Symbols._, Trees._, Types._, Positions._, Contexts._, Constants._, TypedTrees._ +import Symbols._, Types._, Positions._, Contexts._, Constants._, TypedTrees._ object Annotations { abstract class Annotation { - def tree: TypedTree + def tree: Tree def symbol(implicit ctx: Context): Symbol = tree.tpe.typeSymbol def matches(cls: Symbol)(implicit ctx: Context): Boolean = symbol.isNonBottomSubClass(cls) def appliesToModule: Boolean = ??? } - case class ConcreteAnnotation(val tree: TypedTree) extends Annotation + case class ConcreteAnnotation(val tree: Tree) extends Annotation object Annotation { - def apply(tree: TypedTree) = ConcreteAnnotation(tree) + def apply(tree: Tree) = ConcreteAnnotation(tree) - def apply(cls: ClassSymbol, arg: TypedTree)(implicit ctx: Context): Annotation = + def apply(cls: ClassSymbol, arg: Tree)(implicit ctx: Context): Annotation = apply(cls, arg :: Nil) - def apply(cls: ClassSymbol, arg1: TypedTree, arg2: TypedTree)(implicit ctx: Context): Annotation = + def apply(cls: ClassSymbol, arg1: Tree, arg2: Tree)(implicit ctx: Context): Annotation = apply(cls, arg1 :: arg2 :: Nil) - def apply(cls: ClassSymbol, args: List[TypedTree])(implicit ctx: Context): Annotation = + def apply(cls: ClassSymbol, args: List[Tree])(implicit ctx: Context): Annotation = apply(cls.typeConstructor, args) - def apply(atp: Type, arg: TypedTree)(implicit ctx: Context): Annotation = + def apply(atp: Type, arg: Tree)(implicit ctx: Context): Annotation = apply(atp, arg :: Nil) - def apply(atp: Type, arg1: TypedTree, arg2: TypedTree)(implicit ctx: Context): Annotation = + def apply(atp: Type, arg1: Tree, arg2: Tree)(implicit ctx: Context): Annotation = apply(atp, arg1 :: arg2 :: Nil) - def apply(atp: Type, args: List[TypedTree])(implicit ctx: Context): Annotation = - apply(tpd.New(atp, args)) + def apply(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation = + apply(New(atp, args)) def makeAlias(sym: TermSymbol)(implicit ctx: Context) = - apply(defn.AliasAnnot, List(tpd.Ident(TermRef(sym.owner.thisType, sym.name, sym.signature)))) + apply(defn.AliasAnnot, List(Ident(TermRef(sym.owner.thisType, sym.name, sym.signature)))) def makeChild(sym: Symbol)(implicit ctx: Context) = - apply(defn.ChildAnnot, List(tpd.Ident(NamedType(sym.owner.thisType, sym.name)))) + apply(defn.ChildAnnot, List(Ident(NamedType(sym.owner.thisType, sym.name)))) } - def makeLiteralAnnotArg(const: Constant): TypedTree = ??? + def makeLiteralAnnotArg(const: Constant): Tree = ??? - def makeArrayAnnotArg(elems: Array[TypedTree]): TypedTree = ??? + def makeArrayAnnotArg(elems: Array[Tree]): Tree = ??? - def makeNestedAnnotArg(annot: Annotation): TypedTree = annot.tree + def makeNestedAnnotArg(annot: Annotation): Tree = annot.tree def ThrowsAnnotation(cls: ClassSymbol)(implicit ctx: Context) = - Annotation(defn.ThrowsAnnot, tpd.Ident(TypeRef(cls.owner.thisType, cls.name))) + Annotation(defn.ThrowsAnnot, Ident(TypeRef(cls.owner.thisType, cls.name))) }
\ No newline at end of file diff --git a/src/dotty/tools/dotc/core/TypedTrees.scala b/src/dotty/tools/dotc/core/TypedTrees.scala index 3d5b42a65..c8e468107 100644 --- a/src/dotty/tools/dotc/core/TypedTrees.scala +++ b/src/dotty/tools/dotc/core/TypedTrees.scala @@ -1,232 +1,286 @@ package dotty.tools.dotc package core -import Trees._, Positions._, Types._, Contexts._, Constants._, Names._, Flags._ +import Positions._, Types._, Contexts._, Constants._, Names._, Flags._ import SymDenotations._, Symbols._, StdNames._, Annotations._ object TypedTrees { - class TypeTreeGen { - implicit def pos(implicit ctx: Context): Position = ctx.position + type Modifiers = Trees.Modifiers[Type] + type Tree = Trees.Tree[Type] + type TypTree = Trees.TypTree[Type] + type TermTree = Trees.TermTree[Type] + type SymTree = Trees.SymTree[Type] + type ProxyTree = Trees.ProxyTree[Type] + type NameTree = Trees.NameTree[Type] + type RefTree = Trees.RefTree[Type] + type DefTree = Trees.DefTree[Type] + + type TreeCopier = Trees.TreeCopier[Type] + type TreeAccumulator[T] = Trees.TreeAccumulator[T, Type] + type TreeTransformer[C] = Trees.TreeTransformer[Type, C] + + type Ident = Trees.Ident[Type] + type Select = Trees.Select[Type] + type This = Trees.This[Type] + type Super = Trees.Super[Type] + type Apply = Trees.Apply[Type] + type TypeApply = Trees.TypeApply[Type] + type Literal = Trees.Literal[Type] + type New = Trees.New[Type] + type Pair = Trees.Pair[Type] + type Typed = Trees.Typed[Type] + type NamedArg = Trees.NamedArg[Type] + type Assign = Trees.Assign[Type] + type Function = Trees.Function[Type] + type Block = Trees.Block[Type] + type If = Trees.If[Type] + type Match = Trees.Match[Type] + type CaseDef = Trees.CaseDef[Type] + type Return = Trees.Return[Type] + type Try = Trees.Try[Type] + type Throw = Trees.Throw[Type] + type ArrayValue = Trees.ArrayValue[Type] + type TypeTree = Trees.TypeTree[Type] + type SingletonTypeTree = Trees.SingletonTypeTree[Type] + type SelectFromTypeTree = Trees.SelectFromTypeTree[Type] + type AndTypeTree = Trees.AndTypeTree[Type] + type OrTypeTree = Trees.OrTypeTree[Type] + type RefineTypeTree = Trees.RefineTypeTree[Type] + type AppliedTypeTree = Trees.AppliedTypeTree[Type] + type TypeBoundsTree = Trees.TypeBoundsTree[Type] + type Bind = Trees.Bind[Type] + type Alternative = Trees.Alternative[Type] + type UnApply = Trees.UnApply[Type] + type ValDef = Trees.ValDef[Type] + type DefDef = Trees.DefDef[Type] + type TypeDef = Trees.TypeDef[Type] + type Template = Trees.Template[Type] + type ClassDef = Trees.ClassDef[Type] + type Import = Trees.Import[Type] + type PackageDef = Trees.PackageDef[Type] + type Annotated = Trees.Annotated[Type] + type EmptyTree = Trees.EmptyTree[Type] + type Shared = Trees.Shared[Type] + + private implicit def pos(implicit ctx: Context): Position = ctx.position + + def defPos(sym: Symbol)(implicit ctx: Context) = ctx.position union sym.coord.toPosition + + def Modifiers(sym: Symbol)(implicit ctx: Context): Modifiers = Trees.Modifiers[Type]( + sym.flags & ModifierFlags, + if (sym.privateWithin.exists) sym.privateWithin.asType.name else tpnme.EMPTY, + sym.annotations map (_.tree)) + + val Ident = Trees.Ident + def Ident(tp: NamedType)(implicit ctx: Context): Ident = + Trees.Ident(tp.name).withType(tp) + + def Select(pre: Tree, tp: NamedType)(implicit ctx: Context): Select = + Trees.Select(pre, tp.name).withType(tp) + + def This(cls: ClassSymbol)(implicit ctx: Context): This = + Trees.This(cls.name).withType(cls.thisType) + + def Super(qual: Tree, mixin: Symbol = NoSymbol)(implicit ctx: Context): Super = { + val cls = qual.tpe.typeSymbol + val (owntype, mix) = + if (mixin.exists) (mixin.typeConstructor, mixin.asType.name) + else (ctx.glb(cls.info.parents), tpnme.EMPTY) + Trees.Super(qual, mix).withType(SuperType(qual.tpe, owntype)) + } - def defPos(sym: Symbol)(implicit ctx: Context) = ctx.position union sym.coord.toPosition + def Apply(fn: Tree, args: List[Tree])(implicit ctx: Context): Apply = { + val fntpe @ MethodType(pnames, ptypes) = fn.tpe + assert(sameLength(ptypes, args)) + Trees.Apply(fn, args).withType(fntpe.instantiate(args map (_.tpe))) + } - def Modifiers(sym: Symbol)(implicit ctx: Context): Modifiers[Type] = Trees.Modifiers[Type]( - sym.flags & ModifierFlags, - if (sym.privateWithin.exists) sym.privateWithin.asType.name else tpnme.EMPTY, - sym.annotations map (_.tree)) + def TypeApply(fn: Tree, args: List[Tree])(implicit ctx: Context): TypeApply = { + val fntpe @ PolyType(pnames) = fn.tpe + assert(sameLength(pnames, args)) + Trees.TypeApply(fn, args).withType(fntpe.instantiate(args map (_.tpe))) + } - def Ident(tp: NamedType)(implicit ctx: Context): Ident[Type] = - Trees.Ident(tp.name).withType(tp) + def Literal(const: Constant)(implicit ctx: Context): Literal = + Trees.Literal(const).withType(const.tpe) - def Select(pre: TypedTree, tp: NamedType)(implicit ctx: Context): Select[Type] = - Trees.Select(pre, tp.name).withType(tp) + def New(tp: Type)(implicit ctx: Context): New = + Trees.New(TypeTree(tp)) - def This(cls: ClassSymbol)(implicit ctx: Context): This[Type] = - Trees.This(cls.name).withType(cls.thisType) + def Pair(left: Tree, right: Tree)(implicit ctx: Context): Pair = + Trees.Pair(left, right).withType(defn.PairType.appliedTo(left.tpe, right.tpe)) - def Super(qual: TypedTree, mixin: Symbol = NoSymbol)(implicit ctx: Context): Super[Type] = { - val cls = qual.tpe.typeSymbol - val (owntype, mix) = - if (mixin.exists) (mixin.typeConstructor, mixin.asType.name) - else (ctx.glb(cls.info.parents), tpnme.EMPTY) - Trees.Super(qual, mix).withType(SuperType(qual.tpe, owntype)) - } + def Typed(expr: Tree, tpt: Tree)(implicit ctx: Context): Typed = + Trees.Typed(expr, tpt).withType(tpt.tpe) - def Apply(fn: TypedTree, args: List[TypedTree])(implicit ctx: Context): Apply[Type] = { - val fntpe @ MethodType(pnames, ptypes) = fn.tpe - assert(sameLength(ptypes, args)) - Trees.Apply(fn, args).withType(fntpe.instantiate(args map (_.tpe))) - } + def NamedArg(name: Name, arg: Tree)(implicit ctx: Context) = + Trees.NamedArg(name, arg).withType(arg.tpe) - def TypeApply(fn: TypedTree, args: List[TypedTree])(implicit ctx: Context): TypeApply[Type] = { - val fntpe @ PolyType(pnames) = fn.tpe - assert(sameLength(pnames, args)) - Trees.TypeApply(fn, args).withType(fntpe.instantiate(args map (_.tpe))) - } + def Assign(lhs: Tree, rhs: Tree)(implicit ctx: Context): Assign = + Trees.Assign(lhs, rhs).withType(defn.UnitType) - def Literal(const: Constant)(implicit ctx: Context): Literal[Type] = - Trees.Literal(const).withType(const.tpe) + def Function(vparams: List[ValDef], body: Tree)(implicit ctx: Context): Function = + Trees.Function(vparams, body) + .withType(defn.FunctionType(vparams map (_.tpt.tpe), body.tpe)) - def New(tp: Type)(implicit ctx: Context): New[Type] = - Trees.New(TypeTree(tp)) + def Block(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block = + Trees.Block(stats, expr).withType(expr.tpe) // !!! need to make sure that type does not refer to locals - def Pair(left: TypedTree, right: TypedTree)(implicit ctx: Context): Pair[Type] = - Trees.Pair(left, right).withType(defn.PairType.appliedTo(left.tpe, right.tpe)) + def If(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = + Trees.If(cond, thenp, elsep).withType(thenp.tpe | elsep.tpe) - def Typed(expr: TypedTree, tpt: TypedTree)(implicit ctx: Context): Typed[Type] = - Trees.Typed(expr, tpt).withType(tpt.tpe) + def Match(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match = + Trees.Match(selector, cases).withType(ctx.lub(cases map (_.body.tpe))) - def NamedArg[Type](name: Name, arg: TypedTree)(implicit ctx: Context) = - Trees.NamedArg(name, arg).withType(arg.tpe) + def CaseDef(pat: Tree, guard: Tree, body: Tree)(implicit ctx: Context): CaseDef = + Trees.CaseDef(pat, guard, body).withType(body.tpe) - def Assign(lhs: TypedTree, rhs: TypedTree)(implicit ctx: Context): Assign[Type] = - Trees.Assign(lhs, rhs).withType(defn.UnitType) + def Return(expr: Tree, from: Ident)(implicit ctx: Context): Return = + Trees.Return(expr, from).withType(defn.NothingType) - def Function(vparams: List[ValDef[Type]], body: TypedTree)(implicit ctx: Context): Function[Type] = - Trees.Function(vparams, body) - .withType(defn.FunctionType(vparams map (_.tpt.tpe), body.tpe)) + def Throw(expr: Tree)(implicit ctx: Context): Throw = + Trees.Throw(expr).withType(defn.NothingType) - def Block(stats: List[TypedTree], expr: TypedTree)(implicit ctx: Context): Block[Type] = - Trees.Block(stats, expr).withType(expr.tpe) // !!! need to make sure that type does not refer to locals + def ArrayValue(elemtpt: Tree, elems: List[Tree])(implicit ctx: Context): ArrayValue = + Trees.ArrayValue(elemtpt, elems).withType(defn.ArrayType.appliedTo(elemtpt.tpe)) - def If(cond: TypedTree, thenp: TypedTree, elsep: TypedTree)(implicit ctx: Context): If[Type] = - Trees.If(cond, thenp, elsep).withType(thenp.tpe | elsep.tpe) + def TypeTree(tp: Type, original: Tree = EmptyTree)(implicit ctx: Context): TypeTree = + Trees.TypeTree(original).withType(tp) - def Match(selector: TypedTree, cases: List[CaseDef[Type]])(implicit ctx: Context): Match[Type] = - Trees.Match(selector, cases).withType(ctx.lub(cases map (_.body.tpe))) + def SingletonTypeTree(ref: Tree)(implicit ctx: Context): SingletonTypeTree = + Trees.SingletonTypeTree(ref).withType(ref.tpe) - def CaseDef(pat: TypedTree, guard: TypedTree, body: TypedTree)(implicit ctx: Context): CaseDef[Type] = - Trees.CaseDef(pat, guard, body).withType(body.tpe) + def SelectFromTypeTree(qualifier: Tree, name: TypeName)(implicit ctx: Context): SelectFromTypeTree = + Trees.SelectFromTypeTree(qualifier, name).withType(TypeRef(qualifier.tpe, name)) - def Return(expr: TypedTree, from: Ident[Type])(implicit ctx: Context): Return[Type] = - Trees.Return(expr, from).withType(defn.NothingType) + def AndTypeTree(left: Tree, right: Tree)(implicit ctx: Context): AndTypeTree = + Trees.AndTypeTree(left, right).withType(left.tpe & right.tpe) - def Throw(expr: TypedTree)(implicit ctx: Context): Throw[Type] = - Trees.Throw(expr).withType(defn.NothingType) + def OrTypeTree(left: Tree, right: Tree)(implicit ctx: Context): OrTypeTree = + Trees.OrTypeTree(left, right).withType(left.tpe | right.tpe) - def ArrayValue(elemtpt: TypedTree, elems: List[TypedTree])(implicit ctx: Context): ArrayValue[Type] = - Trees.ArrayValue(elemtpt, elems).withType(defn.ArrayType.appliedTo(elemtpt.tpe)) + def RefineTypeTree(tpt: Tree, refinements: List[DefTree])(implicit ctx: Context): RefineTypeTree = { + def refineType(tp: Type, refinement: Symbol): Type = + RefinedType(tp, refinement.name, refinement.info) + Trees.RefineTypeTree(tpt, refinements) + .withType((tpt.tpe /: (refinements map (_.symbol)))(refineType)) + } - def TypeTree(tp: Type, original: TypedTree = EmptyTree)(implicit ctx: Context): TypeTree[Type] = - Trees.TypeTree(original).withType(tp) + def refineType(tp: Type, refinement: Symbol)(implicit ctx: Context): Type = + RefinedType(tp, refinement.name, refinement.info) - def SingletonTypeTree(ref: TypedTree)(implicit ctx: Context): SingletonTypeTree[Type] = - Trees.SingletonTypeTree(ref).withType(ref.tpe) + def AppliedTypeTree(tpt: Tree, args: List[Tree])(implicit ctx: Context): AppliedTypeTree = + Trees.AppliedTypeTree(tpt, args).withType(tpt.tpe.appliedTo(args map (_.tpe))) - def SelectFromTypeTree(qualifier: TypedTree, name: TypeName)(implicit ctx: Context): SelectFromTypeTree[Type] = - Trees.SelectFromTypeTree(qualifier, name).withType(TypeRef(qualifier.tpe, name)) + def TypeBoundsTree(lo: Tree, hi: Tree)(implicit ctx: Context): TypeBoundsTree = + Trees.TypeBoundsTree(lo, hi).withType(TypeBounds(lo.tpe, hi.tpe)) - def AndTypeTree(left: TypedTree, right: TypedTree)(implicit ctx: Context): AndTypeTree[Type] = - Trees.AndTypeTree(left, right).withType(left.tpe & right.tpe) + def Bind(sym: Symbol, body: Tree)(implicit ctx: Context): Bind = + Trees.Bind(sym.name, body)(defPos(sym)).withType(sym.info) - def OrTypeTree(left: TypedTree, right: TypedTree)(implicit ctx: Context): OrTypeTree[Type] = - Trees.OrTypeTree(left, right).withType(left.tpe | right.tpe) + def Alternative(trees: List[Tree])(implicit ctx: Context): Alternative = + Trees.Alternative(trees).withType(ctx.lub(trees map (_.tpe))) - def RefineTypeTree(tpt: TypedTree, refinements: List[DefTree[Type]])(implicit ctx: Context): RefineTypeTree[Type] = { - def refineType(tp: Type, refinement: Symbol): Type = - RefinedType(tp, refinement.name, refinement.info) - Trees.RefineTypeTree(tpt, refinements) - .withType((tpt.tpe /: (refinements map (_.symbol)))(refineType)) - } + def UnApply(fun: Tree, args: List[Tree])(implicit ctx: Context): UnApply = + Trees.UnApply(fun, args).withType(fun.tpe match { + case MethodType(_, paramTypes) => paramTypes.head + }) - def refineType(tp: Type, refinement: Symbol)(implicit ctx: Context): Type = - RefinedType(tp, refinement.name, refinement.info) + def refType(sym: Symbol)(implicit ctx: Context) = NamedType(sym.owner.thisType, sym) - def AppliedTypeTree(tpt: TypedTree, args: List[TypedTree])(implicit ctx: Context): AppliedTypeTree[Type] = - Trees.AppliedTypeTree(tpt, args).withType(tpt.tpe.appliedTo(args map (_.tpe))) - - def TypeBoundsTree(lo: TypedTree, hi: TypedTree)(implicit ctx: Context): TypeBoundsTree[Type] = - Trees.TypeBoundsTree(lo, hi).withType(TypeBounds(lo.tpe, hi.tpe)) - - def Bind(sym: Symbol, body: TypedTree)(implicit ctx: Context): Bind[Type] = - Trees.Bind(sym.name, body)(defPos(sym)).withType(sym.info) - - def Alternative(trees: List[TypedTree])(implicit ctx: Context): Alternative[Type] = - Trees.Alternative(trees).withType(ctx.lub(trees map (_.tpe))) - - def UnApply(fun: TypedTree, args: List[TypedTree])(implicit ctx: Context): UnApply[Type] = - Trees.UnApply(fun, args).withType(fun.tpe match { - case MethodType(_, paramTypes) => paramTypes.head - }) - - def refType(sym: Symbol)(implicit ctx: Context) = NamedType(sym.owner.thisType, sym) - - def ValDef(sym: TermSymbol, rhs: TypedTree = EmptyTree)(implicit ctx: Context): ValDef[Type] = - Trees.ValDef(Modifiers(sym), sym.name, TypeTree(sym.info), rhs)(defPos(sym)) - .withType(refType(sym)) - - def DefDef(sym: TermSymbol, rhs: TypedTree = EmptyTree)(implicit ctx: Context): DefDef[Type] = { - - val (tparams, mtp) = sym.info match { - case tp: PolyType => - def paramBounds(trefs: List[TypeRef]): List[Type] = - tp.paramBounds map new InstPolyMap(tp, trefs) - val tparams = ctx.newTypeParams(sym, tp.paramNames, EmptyFlags, paramBounds) - (tparams, tp.instantiate(tparams map (_.typeConstructor))) - case tp => (Nil, tp) - } - - def valueParamss(tp: Type): (List[List[TermSymbol]], Type) = tp match { - case tp @ MethodType(paramNames, paramTypes) => - def valueParam(name: TermName, info: Type): TermSymbol = - ctx.newSymbol(sym, name, TermParam, info) - val params = (paramNames, paramTypes).zipped.map(valueParam) - val (paramss, rtp) = valueParamss(tp.instantiate(params map (_.typeConstructor))) - (params :: paramss, rtp) - case tp => (Nil, tp) - } - val (vparamss, rtp) = valueParamss(mtp) - - Trees.DefDef( - Modifiers(sym), sym.name, tparams map TypeDef, - vparamss map (_ map (ValDef(_))), TypeTree(rtp), rhs)(defPos(sym)) - .withType(refType(sym)) + def ValDef(sym: TermSymbol, rhs: Tree = EmptyTree)(implicit ctx: Context): ValDef = + Trees.ValDef(Modifiers(sym), sym.name, TypeTree(sym.info), rhs)(defPos(sym)) + .withType(refType(sym)) + + def DefDef(sym: TermSymbol, rhs: Tree = EmptyTree)(implicit ctx: Context): DefDef = { + + val (tparams, mtp) = sym.info match { + case tp: PolyType => + def paramBounds(trefs: List[TypeRef]): List[Type] = + tp.paramBounds map new InstPolyMap(tp, trefs) + val tparams = ctx.newTypeParams(sym, tp.paramNames, EmptyFlags, paramBounds) + (tparams, tp.instantiate(tparams map (_.typeConstructor))) + case tp => (Nil, tp) } - def TypeDef(sym: TypeSymbol)(implicit ctx: Context): TypeDef[Type] = - Trees.TypeDef(Modifiers(sym), sym.name, TypeTree(sym.info))(defPos(sym)) - .withType(refType(sym)) - - def ClassDef(cls: ClassSymbol, typeParams: List[TypeSymbol], body: List[TypedTree])(implicit ctx: Context): ClassDef[Type] = { - val parents = cls.info.parents map (TypeTree(_)) - val selfType = - if (cls.selfType eq cls.typeConstructor) EmptyValDef - else ValDef(ctx.newSelfSym(cls)) - def isOwnTypeParamAccessor(stat: TypedTree) = - stat.symbol.owner == cls && (stat.symbol is TypeParam) - val (tparamAccessors, rest) = body partition isOwnTypeParamAccessor - val tparams = - (typeParams map TypeDef) ++ + def valueParamss(tp: Type): (List[List[TermSymbol]], Type) = tp match { + case tp @ MethodType(paramNames, paramTypes) => + def valueParam(name: TermName, info: Type): TermSymbol = + ctx.newSymbol(sym, name, TermParam, info) + val params = (paramNames, paramTypes).zipped.map(valueParam) + val (paramss, rtp) = valueParamss(tp.instantiate(params map (_.typeConstructor))) + (params :: paramss, rtp) + case tp => (Nil, tp) + } + val (vparamss, rtp) = valueParamss(mtp) + + Trees.DefDef( + Modifiers(sym), sym.name, tparams map TypeDef, + vparamss map (_ map (ValDef(_))), TypeTree(rtp), rhs)(defPos(sym)) + .withType(refType(sym)) + } + + def TypeDef(sym: TypeSymbol)(implicit ctx: Context): TypeDef = + Trees.TypeDef(Modifiers(sym), sym.name, TypeTree(sym.info))(defPos(sym)) + .withType(refType(sym)) + + def ClassDef(cls: ClassSymbol, typeParams: List[TypeSymbol], body: List[Tree])(implicit ctx: Context): ClassDef = { + val parents = cls.info.parents map (TypeTree(_)) + val selfType = + if (cls.selfType eq cls.typeConstructor) EmptyValDef + else ValDef(ctx.newSelfSym(cls)) + def isOwnTypeParamAccessor(stat: Tree) = + stat.symbol.owner == cls && (stat.symbol is TypeParam) + val (tparamAccessors, rest) = body partition isOwnTypeParamAccessor + val tparams = + (typeParams map TypeDef) ++ (tparamAccessors collect { - case td: TypeDef[_] if !(typeParams contains td.symbol) => td + case td: TypeDef if !(typeParams contains td.symbol) => td }) - val findLocalDummy = new FindLocalDummyAccumulator(cls) - val localDummy = ((NoSymbol: Symbol) /: body)(findLocalDummy) - .orElse(ctx.newLocalDummy(cls)) - val impl = Trees.Template(parents, selfType, rest) - .withType(refType(localDummy)) - Trees.ClassDef(Modifiers(cls), cls.name, tparams, impl)(defPos(cls)) - .withType(refType(cls)) - } + val findLocalDummy = new FindLocalDummyAccumulator(cls) + val localDummy = ((NoSymbol: Symbol) /: body)(findLocalDummy) + .orElse(ctx.newLocalDummy(cls)) + val impl = Trees.Template(parents, selfType, rest) + .withType(refType(localDummy)) + Trees.ClassDef(Modifiers(cls), cls.name, tparams, impl)(defPos(cls)) + .withType(refType(cls)) + } - def Import(expr: TypedTree, selectors: List[UntypedTree])(implicit ctx: Context): Import[Type] = - Trees.Import(expr, selectors).withType(refType(ctx.newImportSymbol(expr))) + def Import(expr: Tree, selectors: List[Trees.UntypedTree])(implicit ctx: Context): Import = + Trees.Import(expr, selectors).withType(refType(ctx.newImportSymbol(expr))) - def PackageDef(pid: RefTree[Type], stats: List[TypedTree])(implicit ctx: Context): PackageDef[Type] = - Trees.PackageDef(pid, stats).withType(refType(pid.symbol)) + def PackageDef(pid: RefTree, stats: List[Tree])(implicit ctx: Context): PackageDef = + Trees.PackageDef(pid, stats).withType(refType(pid.symbol)) - def Annotated(annot: TypedTree, arg: TypedTree)(implicit ctx: Context): Annotated[Type] = - Trees.Annotated(annot, arg).withType(AnnotatedType(List(Annotation(annot)), arg.tpe)) + def Annotated(annot: Tree, arg: Tree)(implicit ctx: Context): Annotated = + Trees.Annotated(annot, arg).withType(AnnotatedType(List(Annotation(annot)), arg.tpe)) - def EmptyTree: TypedTree = Trees.EmptyTree[Type] + val EmptyTree: Tree = Trees.EmptyTree[Type] - def EmptyValDef: ValDef[Type] = Trees.EmptyValDef[Type] + val EmptyValDef: ValDef = Trees.EmptyValDef[Type] - def Shared(tree: TypedTree): Shared[Type] = - Trees.Shared(tree).withType(tree.tpe) + def Shared(tree: Tree): Shared = + Trees.Shared(tree).withType(tree.tpe) - // ----------------------------------------------------------------------------- + // ----------------------------------------------------------------------------- - def New(tp: Type, args: List[TypedTree])(implicit ctx: Context): Apply[Type] = - Apply( - Select( - New(tp), - TermRef(tp.normalizedPrefix, tp.typeSymbol.primaryConstructor.asTerm)), - args) + def New(tp: Type, args: List[Tree])(implicit ctx: Context): Apply = + Apply( + Select( + New(tp), + TermRef(tp.normalizedPrefix, tp.typeSymbol.primaryConstructor.asTerm)), + args) - def ModuleDef(sym: TermSymbol, body: List[TypedTree])(implicit ctx: Context): ValDef[Type] = { - val modcls = sym.moduleClass.asClass - val clsdef = ClassDef(modcls, Nil, body) - val rhs = Block(List(clsdef), New(modcls.typeConstructor)) - ValDef(sym, rhs) - } + def ModuleDef(sym: TermSymbol, body: List[Tree])(implicit ctx: Context): ValDef = { + val modcls = sym.moduleClass.asClass + val clsdef = ClassDef(modcls, Nil, body) + val rhs = Block(List(clsdef), New(modcls.typeConstructor)) + ValDef(sym, rhs) } - object tpd extends TypeTreeGen - - class FindLocalDummyAccumulator(cls: ClassSymbol)(implicit ctx: Context) extends TreeAccumulator[Symbol, Type] { - def apply(sym: Symbol, tree: TypedTree) = + private class FindLocalDummyAccumulator(cls: ClassSymbol)(implicit ctx: Context) extends TreeAccumulator[Symbol] { + def apply(sym: Symbol, tree: Tree) = if (sym.exists) sym else if (tree.isDef) { val owner = tree.symbol.owner diff --git a/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala b/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala index 2507d277b..d131fe8f8 100644 --- a/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala +++ b/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala @@ -4,7 +4,7 @@ package core package pickling import Contexts._, Symbols._, Types._, Names._, StdNames._, NameOps._, Scopes._, Decorators._ -import SymDenotations._, UnPickler._, Constants._, Trees._, Annotations._, Positions._ +import SymDenotations._, UnPickler._, Constants._, Annotations._, Positions._ import TypedTrees._ import java.io.{ File, IOException } import java.lang.Integer.toHexString @@ -348,7 +348,7 @@ class ClassfileParser( if (ownTypeParams.isEmpty) tpe else TempPolyType(ownTypeParams, tpe) } // sigToType - def parseAnnotArg(skip: Boolean = false): Option[TypedTree] = { + def parseAnnotArg(skip: Boolean = false): Option[Tree] = { val tag = in.nextByte.toChar val index = in.nextChar tag match { @@ -366,7 +366,7 @@ class ClassfileParser( assert(s != NoSymbol, t) if (skip) None else Some(makeLiteralAnnotArg(Constant(s))) case ARRAY_TAG => - val arr = new ArrayBuffer[TypedTree]() + val arr = new ArrayBuffer[Tree]() var hasError = false for (i <- 0 until index) parseAnnotArg(skip) match { @@ -386,12 +386,12 @@ class ClassfileParser( def parseAnnotation(attrNameIndex: Char, skip: Boolean = false): Option[Annotation] = try { val attrType = pool.getType(attrNameIndex) val nargs = in.nextChar - val argbuf = new ListBuffer[TypedTree] + val argbuf = new ListBuffer[Tree] var hasError = false for (i <- 0 until nargs) { val name = pool.getName(in.nextChar) parseAnnotArg(skip) match { - case Some(arg) => argbuf += tpd.NamedArg(name, arg) + case Some(arg) => argbuf += NamedArg(name, arg) case None => hasError = !skip } } @@ -436,8 +436,8 @@ class ClassfileParser( case tpnme.BridgeATTR => sym.setFlag(Flags.Bridge) case tpnme.DeprecatedATTR => - val msg = tpd.Literal(Constant("see corresponding Javadoc for more information.")) - val since = tpd.Literal(Constant("")) + val msg = Literal(Constant("see corresponding Javadoc for more information.")) + val since = Literal(Constant("")) sym.addAnnotation(Annotation(defn.DeprecatedAnnot, msg, since)) case tpnme.ConstantValueATTR => val c = pool.getConstant(in.nextChar) diff --git a/src/dotty/tools/dotc/core/pickling/UnPickler.scala b/src/dotty/tools/dotc/core/pickling/UnPickler.scala index ebc45dffe..7a06b098e 100644 --- a/src/dotty/tools/dotc/core/pickling/UnPickler.scala +++ b/src/dotty/tools/dotc/core/pickling/UnPickler.scala @@ -9,7 +9,7 @@ import java.lang.Double.longBitsToDouble import Contexts._, Symbols._, Types._, Scopes._, SymDenotations._, Names._ import StdNames._, Denotations._, NameOps._, Flags._, Constants._, Annotations._ -import Trees._, Positions._, TypedTrees._ +import Positions._, TypedTrees._ import io.AbstractFile import scala.reflect.internal.pickling.PickleFormat._ import scala.collection.{ mutable, immutable } @@ -620,36 +620,36 @@ class UnPickler(bytes: Array[Byte], classRoot: LazyClassDenotation, moduleRoot: protected def readAnnotationRef(): Annotation = at(readNat(), readAnnotation) // protected def readModifiersRef(): Modifiers = at(readNat(), readModifiers) - protected def readTreeRef(): TypedTree = at(readNat(), readTree) + protected def readTreeRef(): Tree = at(readNat(), readTree) - protected def readTree(): TypedTree = ??? + protected def readTree(): Tree = ??? /** Read an annotation argument, which is pickled either * as a Constant or a Tree. */ - protected def readAnnotArg(i: Int): TypedTree = bytes(index(i)) match { + protected def readAnnotArg(i: Int): Tree = bytes(index(i)) match { case TREE => at(i, readTree) - case _ => tpd.Literal(at(i, readConstant)) + case _ => Literal(at(i, readConstant)) } /** Read a ClassfileAnnotArg (argument to a classfile annotation) */ - private def readArrayAnnotArg(): TypedTree = { + private def readArrayAnnotArg(): Tree = { readByte() // skip the `annotargarray` tag val end = readNat() + readIndex // array elements are trees representing instances of scala.annotation.Annotation - tpd.ArrayValue( - tpd.TypeTree(defn.AnnotationClass.typeConstructor), + ArrayValue( + TypeTree(defn.AnnotationClass.typeConstructor), until(end, () => readClassfileAnnotArg(readNat()))) } - private def readAnnotInfoArg(): TypedTree = { + private def readAnnotInfoArg(): Tree = { readByte() // skip the `annotinfo` tag val end = readNat() + readIndex readAnnotationContents(end) } - protected def readClassfileAnnotArg(i: Int): TypedTree = bytes(index(i)) match { + protected def readClassfileAnnotArg(i: Int): Tree = bytes(index(i)) match { case ANNOTINFO => at(i, readAnnotInfoArg) case ANNOTARGARRAY => at(i, readArrayAnnotArg) case _ => readAnnotArg(i) @@ -658,20 +658,20 @@ class UnPickler(bytes: Array[Byte], classRoot: LazyClassDenotation, moduleRoot: /** Read an annotation's contents. Not to be called directly, use * readAnnotation, readSymbolAnnotation, or readAnnotInfoArg */ - protected def readAnnotationContents(end: Int): TypedTree = { + protected def readAnnotationContents(end: Int): Tree = { val atp = readTypeRef() - val args = new ListBuffer[TypedTree] + val args = new ListBuffer[Tree] while (readIndex != end) { val argref = readNat() args += { if (isNameEntry(argref)) { val name = at(argref, readName) val arg = readClassfileAnnotArg(readNat()) - tpd.NamedArg(name, arg) + NamedArg(name, arg) } else readAnnotArg(argref) } } - tpd.New(atp, args.toList) + New(atp, args.toList) } /** Read an annotation and as a side effect store it into |