From cda2639a5f0dc766c6520aa05015e707e17ef5d5 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 25 Dec 2012 16:59:14 +0100 Subject: Added substThis to substituters --- src/dotty/tools/dotc/core/Substituters.scala | 51 ++++++++++++++++++++++++++++ src/dotty/tools/dotc/core/Types.scala | 12 ++++--- 2 files changed, 58 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/core/Substituters.scala b/src/dotty/tools/dotc/core/Substituters.scala index 55e3591a3..539d12310 100644 --- a/src/dotty/tools/dotc/core/Substituters.scala +++ b/src/dotty/tools/dotc/core/Substituters.scala @@ -129,6 +129,49 @@ object Substituters { } } } + + def substThis(from: ClassSymbol, to: Type, map: SubstThisMap)(implicit ctx: Context): Type = + tp match { + case tp @ ThisType(clazz) => + if (clazz eq from) to else tp + case tp: NamedType => + if (tp.symbol.isStatic) tp + else tp.derivedNamedType(tp.prefix.substThis(from, to, map), tp.name) + case MethodParam(_, _) + | PolyParam(_, _) + | NoPrefix => tp + case _ => + val substMap = if (map != null) map else new SubstThisMap(from, to) + tp match { + case tp: AppliedType => + tp.derivedAppliedType( + substMap(tp.tycon), tp.typeArgs mapConserve substMap) + case _ => + substMap mapOver tp + } + } + + def substThis(from: RefinedType, to: Type, map: SubstRefinedThisMap)(implicit ctx: Context): Type = + tp match { + case tp @ RefinedThis(rt) => + if (rt eq from) to else tp + case tp: NamedType => + if (tp.symbol.isStatic) tp + else tp.derivedNamedType(tp.prefix.substThis(from, to, map), tp.name) + case ThisType(_) + | MethodParam(_, _) + | PolyParam(_, _) + | NoPrefix => tp + case _ => + val substMap = if (map != null) map else new SubstRefinedThisMap(from, to) + tp match { + case tp: AppliedType => + tp.derivedAppliedType( + substMap(tp.tycon), tp.typeArgs mapConserve substMap) + case _ => + substMap mapOver tp + } + } } class SubstPolyMap(from: PolyType, to: PolyType)(implicit ctx: Context) extends TypeMap { @@ -150,4 +193,12 @@ object Substituters { class SubstMap(from: List[Symbol], to: List[Type])(implicit ctx: Context) extends TypeMap { def apply(tp: Type): Type = tp.subst(from, to, this) } + + class SubstThisMap(from: ClassSymbol, to: Type)(implicit ctx: Context) extends TypeMap { + def apply(tp: Type): Type = tp.substThis(from, to, this) + } + + class SubstRefinedThisMap(from: RefinedType, to: Type)(implicit ctx: Context) extends TypeMap { + def apply(tp: Type): Type = tp.substThis(from, to, this) + } } \ No newline at end of file diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index fd0318c97..258844467 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -118,6 +118,12 @@ object Types { if (from.isDependent) new SubstOps(this).subst(from, to, null) else this + def substThis(clazz: ClassSymbol, tp: Type)(implicit ctx: Context): Type = + new SubstOps(this).substThis(clazz, tp, null) + + def substThis(from: RefinedType, tp: Type)(implicit ctx: Context): Type = + new SubstOps(this).substThis(from, tp, null) + /** For a class or intersection type, its parents. * For a TypeBounds type, the parents of its hi bound. * inherited by typerefs, singleton types, and refinement types, @@ -167,10 +173,6 @@ object Types { if (this.isTrivial || clazz.isStaticMono) this else new AsSeenFromMap(pre, clazz) apply (this) - def substSym(from: List[Symbol], to: List[Symbol]): Type = ??? - def substThis(clazz: ClassSymbol, tp: Type): Type = ??? - def substThis(from: RefinedType, tp: Type): Type = ??? - def signature: Signature = NullSignature def subSignature: Signature = List() @@ -523,7 +525,7 @@ object Types { lazy val infos = infosExpr(this) - def derivedRefinedType(parent1: Type, names1: List[Name], infos1: List[Type]): RefinedType = + def derivedRefinedType(parent1: Type, names1: List[Name], infos1: List[Type])(implicit ctx: Context): RefinedType = if ((parent1 eq parent) && (names1 eq names) && (infos1 eq infos)) this else RefinedType(parent1, names1) { rt => -- cgit v1.2.3