|
| 1 | +package dotty.tools.dotc.quoted |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.Trees._ |
| 4 | +import dotty.tools.dotc.ast.tpd |
| 5 | +import dotty.tools.dotc.core.Contexts._ |
| 6 | +import dotty.tools.dotc.core.Constants._ |
| 7 | +import dotty.tools.dotc.core.Symbols._ |
| 8 | +import dotty.tools.dotc.core.Types._ |
| 9 | + |
| 10 | +/** Clean up quote artifacts from the tree to make it simpler to read. |
| 11 | + * - Flattens block and remove blocks with not statements |
| 12 | + * - Inline type aliases in the tree |
| 13 | + */ |
| 14 | +class TreeCleaner extends tpd.TreeMap { |
| 15 | + import tpd._ |
| 16 | + |
| 17 | + /** List of symbols and their types for type aliases `type T = U` */ |
| 18 | + private[this] var aliasesSyms: List[Symbol] = Nil |
| 19 | + private[this] var aliasesTypes: List[Type] = Nil |
| 20 | + |
| 21 | + override def transform(tree: Tree)(implicit ctx: Context): Tree = { |
| 22 | + val tree0 = tree match { |
| 23 | + case TypeDef(_, TypeBoundsTree(lo, hi)) if lo == hi => |
| 24 | + aliasesSyms = tree.symbol :: aliasesSyms |
| 25 | + aliasesTypes = lo.tpe :: aliasesTypes |
| 26 | + Literal(Constant(())) |
| 27 | + case _ => tree |
| 28 | + } |
| 29 | + |
| 30 | + super.transform(tree0) match { |
| 31 | + case Block(Nil, expr1) => expr1 |
| 32 | + case Block(stats1, expr1) => |
| 33 | + val flatStats = stats1.flatMap { |
| 34 | + case Block(stats2, expr2) => stats2 ::: expr2 :: Nil |
| 35 | + case Literal(Constant(())) => Nil |
| 36 | + case stat => stat :: Nil |
| 37 | + } |
| 38 | + expr1 match { |
| 39 | + case Block(stats3, expr3) => Block(flatStats ::: stats3, expr3) |
| 40 | + case expr3 => Block(flatStats, expr3) |
| 41 | + } |
| 42 | + case tree1: TypeTree => TypeTree(tree1.tpe.subst(aliasesSyms, aliasesTypes)) |
| 43 | + case tree1 => tree1 |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments