|
| 1 | +package dotty.tools.dotc.qualified_types |
| 2 | + |
| 3 | +import dotty.tools.dotc.core.Contexts.Context |
| 4 | +import dotty.tools.dotc.core.Hashable.Binders |
| 5 | +import dotty.tools.dotc.core.Names.Designator |
| 6 | +import dotty.tools.dotc.core.Names.Name |
| 7 | +import dotty.tools.dotc.core.StdNames.nme |
| 8 | +import dotty.tools.dotc.core.StdNames.tpnme |
| 9 | +import dotty.tools.dotc.core.Symbols.Symbol |
| 10 | +import dotty.tools.dotc.core.Types.{ |
| 11 | + CachedProxyType, |
| 12 | + ConstantType, |
| 13 | + MethodType, |
| 14 | + NamedType, |
| 15 | + NoPrefix, |
| 16 | + SingletonType, |
| 17 | + TermRef, |
| 18 | + ThisType, |
| 19 | + Type |
| 20 | +} |
| 21 | + |
| 22 | +enum ENode: |
| 23 | + import ENode.* |
| 24 | + |
| 25 | + case Atom(tp: SingletonType) |
| 26 | + case New(clazz: ENode) |
| 27 | + case Select(qual: ENode, member: Symbol) |
| 28 | + case Apply(fn: ENode, args: List[ENode]) |
| 29 | + case OpApply(fn: ENode.Op, args: List[ENode]) |
| 30 | + case TypeApply(fn: ENode, args: List[Type]) |
| 31 | + case Lambda(paramTps: List[Type], retTp: Type, body: ENode) |
| 32 | + |
| 33 | + override def toString(): String = |
| 34 | + this match |
| 35 | + case Atom(tp) => typeToString(tp) |
| 36 | + case New(clazz) => s"new $clazz" |
| 37 | + case Select(qual, member) => s"$qual.${designatorToString(member)}" |
| 38 | + case Apply(fn, args) => s"$fn(${args.mkString(", ")})" |
| 39 | + case OpApply(op, args) => s"(${args.mkString(op.operatorString())})" |
| 40 | + case TypeApply(fn, args) => s"$fn[${args.map(typeToString).mkString(", ")}]" |
| 41 | + case Lambda(paramTps, retTp, body) => |
| 42 | + s"(${paramTps.map(typeToString).mkString(", ")}): ${{ typeToString(retTp) }} => $body" |
| 43 | + |
| 44 | +object ENode: |
| 45 | + private def typeToString(tp: Type): String = |
| 46 | + tp match |
| 47 | + case tp: NamedType => |
| 48 | + val prefixString = if isEmptyPrefix(tp.prefix) then "" else typeToString(tp.prefix) + "." |
| 49 | + prefixString + designatorToString(tp.designator) |
| 50 | + case tp: ConstantType => |
| 51 | + tp.value.value.toString |
| 52 | + case tp: ThisType => |
| 53 | + typeToString(tp.tref) + ".this" |
| 54 | + case _ => |
| 55 | + tp.toString |
| 56 | + |
| 57 | + private def isEmptyPrefix(tp: Type): Boolean = |
| 58 | + tp match |
| 59 | + case tp: NoPrefix.type => |
| 60 | + true |
| 61 | + case tp: ThisType => |
| 62 | + tp.tref.designator match |
| 63 | + case d: Symbol => d.lastKnownDenotation.name.toTermName == nme.EMPTY_PACKAGE |
| 64 | + case _ => false |
| 65 | + case _ => false |
| 66 | + |
| 67 | + private def designatorToString(d: Designator): String = |
| 68 | + d match |
| 69 | + case d: Symbol => d.lastKnownDenotation.name.toString |
| 70 | + case _ => d.toString |
| 71 | + |
| 72 | + enum Op: |
| 73 | + case IntSum |
| 74 | + case IntProduct |
| 75 | + case LongSum |
| 76 | + case LongProduct |
| 77 | + case Equal |
| 78 | + case Not |
| 79 | + case And |
| 80 | + case Or |
| 81 | + case LessThan |
| 82 | + |
| 83 | + def operatorString(): String = |
| 84 | + this match |
| 85 | + case IntSum => "+" |
| 86 | + case IntProduct => "*" |
| 87 | + case LongSum => "+" |
| 88 | + case LongProduct => "*" |
| 89 | + case Equal => "==" |
| 90 | + case Not => "!" |
| 91 | + case And => "&&" |
| 92 | + case Or => "||" |
| 93 | + case LessThan => "<" |
| 94 | + |
| 95 | + /** Reference to the argument of an [[ENode.Lambda]]. |
| 96 | + * |
| 97 | + * @param index |
| 98 | + * Debruijn index of the argument, starting from 0 |
| 99 | + * @param underyling |
| 100 | + * Underlying type of the argument |
| 101 | + */ |
| 102 | + final case class ArgRefType(index: Int, underlying: Type) extends CachedProxyType, SingletonType: |
| 103 | + override def underlying(using Context): Type = underlying |
| 104 | + override def computeHash(bs: Binders): Int = doHash(bs, index, underlying) |
0 commit comments