Skip to content

Commit 55b6c8b

Browse files
Clean up InlineParentTrait
1 parent 3e6d2dc commit 55b6c8b

File tree

1 file changed

+34
-60
lines changed

1 file changed

+34
-60
lines changed

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ object Inlines:
474474

475475
def expandDefs(): List[Tree] =
476476
val tpd.Block(stats, _) = Inlines.bodyToInline(parentSym): @unchecked
477-
val stats1 = stats.filterNot(isMemberOverridden)
477+
val stats1 = stats.filterNot(isStatAlreadyOverridden)
478478
val inlinedSymbols = stats1.map(stat => inlinedMember(stat.symbol))
479-
stats1.zip(inlinedSymbols).map(expandStat)//.map(inlined(_)._2)
479+
stats1.zip(inlinedSymbols).map(expandStat)
480480
end expandDefs
481481

482482
protected class InlineTraitTypeMap extends InlinerTypeMap {
@@ -488,27 +488,26 @@ object Inlines:
488488

489489
override protected val inlinerTypeMap: InlinerTypeMap = InlineTraitTypeMap()
490490

491-
private val argsMap: Map[Name, Tree] =
491+
private val paramAccessorsValueOf: Map[Name, Tree] =
492492
def allArgs(tree: Tree): List[List[Tree]] = tree match
493493
case Apply(fun, args) => args :: allArgs(fun)
494-
case TypeApply(fun, targs) => targs :: allArgs(fun)
495-
case AppliedTypeTree(_, targs) => targs :: Nil
494+
case TypeApply(fun, _) => allArgs(fun)
496495
case _ => Nil
497496
def allParams(info: Type): List[List[Name]] = info match
498497
case mt: MethodType => mt.paramNames :: allParams(mt.resultType)
499-
case pt: PolyType => pt.paramNames :: allParams(pt.resultType)
498+
case pt: PolyType => allParams(pt.resultType)
500499
case _ => Nil
501500
val info =
502501
if parent.symbol.isClass then parent.symbol.primaryConstructor.info
503502
else parent.symbol.info
504503
allParams(info).flatten.zip(allArgs(parent).reverse.flatten).toMap
505504

506-
private def isMemberOverridden(stat: Tree): Boolean =
505+
private def isStatAlreadyOverridden(stat: Tree): Boolean =
507506
overriddenDecls.flatMap(_.allOverriddenSymbols).toSet.contains(stat.symbol)
508507

509508
private def expandStat(stat: tpd.Tree, inlinedSym: Symbol): tpd.Tree = stat match
510509
case stat: ValDef if stat.symbol.is(ParamAccessor) =>
511-
tpd.ValDef(inlinedSym.asTerm, argsMap(inlinedSym.name.asTermName)).withSpan(parent.span)
510+
tpd.ValDef(inlinedSym.asTerm, paramAccessorsValueOf(stat.symbol.name)).withSpan(parent.span)
512511
case stat: ValDef =>
513512
inlinedValDef(stat, inlinedSym)
514513
case stat: DefDef if stat.symbol.isSetter =>
@@ -521,19 +520,26 @@ object Inlines:
521520
inlinedTypeDef(stat, inlinedSym)
522521

523522
private def inlinedMember(sym: Symbol)(using Context): Symbol =
524-
if sym.isClass then
525-
val ClassInfo(prefix, cls, declaredParents, scope, selfInfo) = sym.info: @unchecked
526-
val inlinedInfo = ClassInfo(prefix, cls, declaredParents, Scopes.newScope, selfInfo) // TODO adapt parents
527-
sym.copy(owner = ctx.owner, info = inlinedInfo, coord = spanCoord(parent.span)).entered.asClass
528-
else
529-
var flags = sym.flags | Synthetic
530-
if sym.isType || !sym.is(Private) then flags |= Override
531-
if !sym.isType && sym.is(ParamAccessor) then flags &~= ParamAccessor
532-
sym.copy(
533-
owner = ctx.owner,
534-
flags = flags,
535-
info = inlinerTypeMap(sym.info),
536-
coord = spanCoord(parent.span)).entered
523+
sym.info match {
524+
case ClassInfo(prefix, cls, declaredParents, scope, selfInfo) =>
525+
val inlinedInfo = ClassInfo(prefix, cls, declaredParents, Scopes.newScope, selfInfo) // TODO adapt parents
526+
sym.copy(owner = ctx.owner, info = inlinedInfo, coord = spanCoord(parent.span)).entered.asClass
527+
case _ =>
528+
var flags = sym.flags | Synthetic
529+
if sym.isType || !sym.is(Private) then flags |= Override
530+
if !sym.isType && sym.is(ParamAccessor) then flags &~= ParamAccessor
531+
sym.copy(
532+
owner = ctx.owner,
533+
flags = flags,
534+
info = inlinerTypeMap(sym.info),
535+
coord = spanCoord(parent.span)).entered
536+
}
537+
538+
private def inlinedStat(stat: Tree)(using Context): Tree = stat match {
539+
// TODO check if symbols must be copied
540+
case tree: DefDef => inlinedDefDef(tree, tree.symbol)
541+
case tree: ValDef => inlinedValDef(tree, tree.symbol)
542+
}
537543

538544
private def inlinedValDef(vdef: ValDef, inlinedSym: Symbol)(using Context): ValDef =
539545
tpd.ValDef(inlinedSym.asTerm, inlinedRhs(vdef.rhs.changeOwner(vdef.symbol, inlinedSym))).withSpan(parent.span)
@@ -545,55 +551,23 @@ object Inlines:
545551
inlinedRhs(ddef.rhs.subst(oldParamSyms, newParamSyms).changeOwner(ddef.symbol, inlinedSym))
546552
tpd.DefDef(inlinedSym.asTerm, rhsFun).withSpan(parent.span)
547553

554+
private def inlinedPrimaryConstructorDefDef(ddef: DefDef)(using Context): DefDef =
555+
// TODO check if symbol must be copied
556+
val constr = inlinedDefDef(ddef, ddef.symbol)
557+
cpy.DefDef(constr)(tpt = TypeTree(defn.UnitType), rhs = EmptyTree)
558+
548559
private def inlinedClassDef(clDef: TypeDef, impl: Template, inlinedCls: ClassSymbol)(using Context): Tree =
549560
val (constr, body) = inContext(ctx.withOwner(inlinedCls)) {
550-
(clonePrimaryConstructorDefDef(impl.constr), impl.body.map(cloneStat))
561+
(inlinedPrimaryConstructorDefDef(impl.constr), impl.body.map(inlinedStat))
551562
}
552563
inlined(tpd.ClassDefWithParents(inlinedCls, constr, impl.parents, body))._2.withSpan(clDef.span) // TODO adapt parents
553564

554565
private def inlinedTypeDef(tdef: TypeDef, inlinedSym: Symbol)(using Context): TypeDef =
555566
tpd.TypeDef(inlinedSym.asType).withSpan(parent.span)
556567

557568
private def inlinedRhs(rhs: Tree): Inlined =
558-
val (bindings, inlinedRhs) = inlined(rhs)
559-
// assert(bindings.isEmpty)
569+
val inlinedRhs = inlined(rhs)._2
560570
Inlined(tpd.ref(parentSym), Nil, inlinedRhs).withSpan(parent.span)
561571

562-
private def cloneClass(clDef: untpd.TypeDef, impl: Template)(using Context): TypeDef =
563-
val inlinedCls: ClassSymbol =
564-
val ClassInfo(prefix, cls, declaredParents, scope, selfInfo) = clDef.symbol.info: @unchecked
565-
val inlinedInfo = ClassInfo(prefix, cls, declaredParents, Scopes.newScope, selfInfo) // TODO adapt parents
566-
clDef.symbol.copy(owner = ctx.owner, info = inlinedInfo, coord = spanCoord(parent.span)).entered.asClass
567-
val (constr, body) = inContext(ctx.withOwner(inlinedCls)) {
568-
(clonePrimaryConstructorDefDef(impl.constr), impl.body.map(cloneStat))
569-
}
570-
tpd.ClassDefWithParents(inlinedCls, constr, impl.parents, body).withSpan(clDef.span) // TODO adapt parents
571-
572-
private def cloneStat(tree: Tree)(using Context): Tree = tree match
573-
case tree: DefDef => cloneDefDef(tree)
574-
case tree: ValDef => cloneValDef(tree)
575-
// TODO case stat @ TypeDef(_, impl: Template) => cloneClass(stat, impl)
576-
// TODO case tree: TypeDef => cloneTypeDef(tree)
577-
578-
private def cloneDefDef(ddef: DefDef)(using Context): DefDef =
579-
val inlinedSym = ddef.symbol.copy(owner = ctx.owner, info = inlinerTypeMap(ddef.symbol.info), coord = spanCoord(parent.span)).entered
580-
def rhsFun(paramss: List[List[Tree]]): Tree =
581-
val oldParamSyms = ddef.paramss.flatten.map(_.symbol)
582-
val newParamSyms = paramss.flatten.map(_.symbol)
583-
inlinedRhs(ddef.rhs.subst(oldParamSyms, newParamSyms).changeOwner(ddef.symbol, inlinedSym)) // TODO clone local classes?
584-
tpd.DefDef(inlinedSym.asTerm, rhsFun).withSpan(parent.span)
585-
586-
private def clonePrimaryConstructorDefDef(ddef: DefDef)(using Context): DefDef =
587-
val constr = cloneDefDef(ddef)
588-
cpy.DefDef(constr)(tpt = TypeTree(defn.UnitType), rhs = EmptyTree)
589-
590-
private def cloneValDef(vdef: ValDef)(using Context): ValDef =
591-
val inlinedSym = vdef.symbol.copy(owner = ctx.owner, info = inlinerTypeMap(vdef.symbol.info), coord = spanCoord(parent.span)).entered
592-
tpd.ValDef(inlinedSym.asTerm, inlinedRhs(vdef.rhs.changeOwner(vdef.symbol, inlinedSym))).withSpan(parent.span) // TODO clone local classes?
593-
594-
private def cloneTypeDef(tdef: TypeDef)(using Context): TypeDef =
595-
val inlinedSym = tdef.symbol.copy(owner = ctx.owner, coord = spanCoord(parent.span)).entered
596-
tpd.TypeDef(inlinedSym.asType).withSpan(parent.span)
597-
598572
end InlineParentTrait
599573
end Inlines

0 commit comments

Comments
 (0)