|
| 1 | +package dotty.tools.dotc.qualified_types |
| 2 | + |
| 3 | +import scala.util.hashing.MurmurHash3 as hashing |
| 4 | + |
| 5 | +import dotty.tools.dotc.ast.tpd.{closureDef, Apply, Block, DefDef, Ident, Literal, New, Select, Tree, TreeOps, TypeApply, Typed, TypeTree} |
| 6 | +import dotty.tools.dotc.core.Contexts.Context |
| 7 | +import dotty.tools.dotc.core.Decorators.i |
| 8 | +import dotty.tools.dotc.core.Symbols.Symbol |
| 9 | +import dotty.tools.dotc.core.Types.{MethodType, TermRef, Type, TypeVar} |
| 10 | +import dotty.tools.dotc.core.Symbols.defn |
| 11 | + |
| 12 | +import dotty.tools.dotc.reporting.trace |
| 13 | +import dotty.tools.dotc.config.Printers |
| 14 | + |
| 15 | +private abstract class QualifierComparer: |
| 16 | + private def typeIso(tp1: Type, tp2: Type) = |
| 17 | + val tp1stripped = stripPermanentTypeVar(tp1) |
| 18 | + val tp2stripped = stripPermanentTypeVar(tp2) |
| 19 | + tp1stripped.equals(tp2stripped) |
| 20 | + |
| 21 | + /** Structural equality for trees. |
| 22 | + * |
| 23 | + * This implementation is _not_ alpha-equivalence aware, which |
| 24 | + * 1. allows it not to rely on a [[Context]] and, |
| 25 | + * 2. allows the corresponding [[hash]] method to reuse [[Type#hashCode]] |
| 26 | + * instead of defining an other hash code for types. |
| 27 | + */ |
| 28 | + def iso(tree1: Tree, tree2: Tree): Boolean = |
| 29 | + (tree1, tree2) match |
| 30 | + case (Literal(_) | Ident(_), _) => |
| 31 | + typeIso(tree1.tpe, tree2.tpe) |
| 32 | + case (Select(qual1, name1), Select(qual2, name2)) => |
| 33 | + name1 == name2 && iso(qual1, qual2) |
| 34 | + case (Apply(fun1, args1), Apply(fun2, args2)) => |
| 35 | + iso(fun1, fun2) && args1.corresponds(args2)(iso) |
| 36 | + case (TypeApply(fun1, args1), TypeApply(fun2, args2)) => |
| 37 | + iso(fun1, fun2) && args1.corresponds(args2)((arg1, arg2) => typeIso(arg1.tpe, arg2.tpe)) |
| 38 | + case (tpt1: TypeTree, tpt2: TypeTree) => |
| 39 | + typeIso(tpt1.tpe, tpt2.tpe) |
| 40 | + case (Typed(expr1, tpt1), Typed(expr2, tpt2)) => |
| 41 | + iso(expr1, expr2) && typeIso(tpt1.tpe, tpt2.tpe) |
| 42 | + case (New(tpt1), New(tpt2)) => |
| 43 | + typeIso(tpt1.tpe, tpt2.tpe) |
| 44 | + case (Block(stats1, expr1), Block(stats2, expr2)) => |
| 45 | + stats1.corresponds(stats2)(iso) && iso(expr1, expr2) |
| 46 | + case _ => |
| 47 | + tree1.equals(tree2) |
| 48 | + |
| 49 | + protected def stripPermanentTypeVar(tp: Type): Type = |
| 50 | + tp match |
| 51 | + case tp: TypeVar if tp.isPermanentlyInstantiated => tp.permanentInst |
| 52 | + case tp => tp |
| 53 | + |
| 54 | +private[qualified_types] object QualifierStructuralComparer extends QualifierComparer: |
| 55 | + /** A hash code for trees that corresponds to `iso(tree1, tree2)`. */ |
| 56 | + def hash(tree: Tree): Int = |
| 57 | + tree match |
| 58 | + case Literal(_) | Ident(_) => |
| 59 | + hashType(tree.tpe) |
| 60 | + case Select(qual, name) => |
| 61 | + hashing.mix(name.hashCode, hash(qual)) |
| 62 | + case Apply(fun, args) => |
| 63 | + hashing.mix(hash(fun), hashList(args)) |
| 64 | + case TypeApply(fun, args) => |
| 65 | + hashing.mix(hash(fun), hashList(args)) |
| 66 | + case tpt: TypeTree => |
| 67 | + hashType(tpt.tpe) |
| 68 | + case Typed(expr, tpt) => |
| 69 | + hashing.mix(hash(expr), hashType(tpt.tpe)) |
| 70 | + case New(tpt1) => |
| 71 | + hashType(tpt1.tpe) |
| 72 | + case Block(stats, expr) => |
| 73 | + hashing.mix(hashList(stats), hash(expr)) |
| 74 | + case _ => |
| 75 | + tree.hashCode |
| 76 | + |
| 77 | + private def hashList(trees: List[Tree]): Int = |
| 78 | + trees.map(hash).foldLeft(0)(hashing.mix) |
| 79 | + |
| 80 | + private def hashType(tp: Type): Int = |
| 81 | + stripPermanentTypeVar(tp).hashCode |
| 82 | + |
| 83 | + /** A box for trees that implements structural equality using [[iso]] and |
| 84 | + * [[hash]]. This enables using trees as keys in hash maps. |
| 85 | + */ |
| 86 | + final class TreeBox(val tree: Tree) extends AnyVal: |
| 87 | + override def equals(that: Any): Boolean = that match |
| 88 | + case that: TreeBox => iso(tree, that.tree) |
| 89 | + case _ => false |
| 90 | + |
| 91 | + override def hashCode: Int = hash(tree) |
| 92 | + |
| 93 | +private[qualified_types] final class QualifierAlphaComparer(using Context) extends QualifierComparer: |
| 94 | + override def iso(tree1: Tree, tree2: Tree): Boolean = |
| 95 | + trace(i"iso $tree1 ; $tree2"): |
| 96 | + (tree1, tree2) match |
| 97 | + case (closureDef(def1), closureDef(def2)) => |
| 98 | + val def2substituted = def2.rhs.subst(def2.symbol.paramSymss.flatten, def1.symbol.paramSymss.flatten) |
| 99 | + val def2normalized = QualifierNormalizer.normalize(def2substituted) |
| 100 | + iso(def1.rhs, def2normalized) |
| 101 | + case _ => |
| 102 | + super.iso(tree1, tree2) |
0 commit comments