|
| 1 | +package dotty.tools.dotc.qualified_types |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.tpd |
| 4 | +import dotty.tools.dotc.ast.tpd.{ |
| 5 | + Apply, |
| 6 | + Block, |
| 7 | + EmptyTree, |
| 8 | + Ident, |
| 9 | + If, |
| 10 | + Lambda, |
| 11 | + Literal, |
| 12 | + New, |
| 13 | + Select, |
| 14 | + SeqLiteral, |
| 15 | + This, |
| 16 | + Throw, |
| 17 | + Tree, |
| 18 | + TypeApply, |
| 19 | + Typed, |
| 20 | + given |
| 21 | +} |
| 22 | +import dotty.tools.dotc.core.Atoms |
| 23 | +import dotty.tools.dotc.core.Constants.Constant |
| 24 | +import dotty.tools.dotc.core.Contexts.{ctx, Context} |
| 25 | +import dotty.tools.dotc.core.Decorators.{i, em, toTermName} |
| 26 | +import dotty.tools.dotc.core.StdNames.nme |
| 27 | +import dotty.tools.dotc.core.Symbols.{defn, Symbol} |
| 28 | +import dotty.tools.dotc.core.Types.{AndType, ConstantType, SkolemType, ErrorType, MethodType, OrType, TermRef, Type, TypeProxy} |
| 29 | + |
| 30 | +import dotty.tools.dotc.report |
| 31 | +import dotty.tools.dotc.reporting.trace |
| 32 | +import dotty.tools.dotc.config.Printers |
| 33 | + |
| 34 | +object QualifiedTypes: |
| 35 | + /** Does the type `tp1` imply the qualifier `qualifier2`? |
| 36 | + * |
| 37 | + * Used by [[dotty.tools.dotc.core.TypeComparer]] to compare qualified types. |
| 38 | + * |
| 39 | + * Note: the logic here is similar to [[Type#derivesAnnotWith]] but |
| 40 | + * additionally handle comparisons with [[SingletonType]]s. |
| 41 | + */ |
| 42 | + def typeImplies(tp1: Type, qualifier2: Tree)(using Context): Boolean = |
| 43 | + trace(i"typeImplies $tp1 --> $qualifier2", Printers.qualifiedTypes): |
| 44 | + tp1 match |
| 45 | + case QualifiedType(parent1, qualifier1) => |
| 46 | + QualifierSolver().implies(qualifier1, qualifier2) |
| 47 | + case tp1: (ConstantType | TermRef) => |
| 48 | + QualifierSolver().implies(equalToPredicate(tpd.singleton(tp1)), qualifier2) |
| 49 | + || typeImplies(tp1.underlying, qualifier2) |
| 50 | + case tp1: TypeProxy => |
| 51 | + typeImplies(tp1.underlying, qualifier2) |
| 52 | + case AndType(tp11, tp12) => |
| 53 | + typeImplies(tp11, qualifier2) || typeImplies(tp12, qualifier2) |
| 54 | + case OrType(tp11, tp12) => |
| 55 | + typeImplies(tp11, qualifier2) && typeImplies(tp12, qualifier2) |
| 56 | + case _ => |
| 57 | + false |
| 58 | + // QualifierSolver().implies(truePredicate(), qualifier2) |
| 59 | + |
| 60 | + /** Try to adapt the tree to the given type `pt` |
| 61 | + * |
| 62 | + * Returns [[EmptyTree]] if `pt` does not contain qualifiers or if the tree |
| 63 | + * cannot be adapted, or the adapted tree otherwise. |
| 64 | + * |
| 65 | + * Used by [[dotty.tools.dotc.core.Typer]]. |
| 66 | + */ |
| 67 | + def adapt(tree: Tree, pt: Type)(using Context): Tree = |
| 68 | + trace(i"adapt $tree to $pt", Printers.qualifiedTypes): |
| 69 | + if containsQualifier(pt) && isSimple(tree) then |
| 70 | + val selfifiedTp = QualifiedType(tree.tpe, equalToPredicate(tree)) |
| 71 | + if selfifiedTp <:< pt then tree.cast(selfifiedTp) else EmptyTree |
| 72 | + else |
| 73 | + EmptyTree |
| 74 | + |
| 75 | + def isSimple(tree: Tree)(using Context): Boolean = |
| 76 | + tree match |
| 77 | + case Apply(fn, args) => isSimple(fn) && args.forall(isSimple) |
| 78 | + case TypeApply(fn, args) => isSimple(fn) |
| 79 | + case SeqLiteral(elems, _) => elems.forall(isSimple) |
| 80 | + case Typed(expr, _) => isSimple(expr) |
| 81 | + case Block(Nil, expr) => isSimple(expr) |
| 82 | + case _ => tpd.isIdempotentExpr(tree) |
| 83 | + |
| 84 | + def containsQualifier(tp: Type)(using Context): Boolean = |
| 85 | + tp match |
| 86 | + case QualifiedType(_, _) => true |
| 87 | + case tp: TypeProxy => containsQualifier(tp.underlying) |
| 88 | + case AndType(tp1, tp2) => containsQualifier(tp1) || containsQualifier(tp2) |
| 89 | + case OrType(tp1, tp2) => containsQualifier(tp1) || containsQualifier(tp2) |
| 90 | + case _ => false |
| 91 | + |
| 92 | + def checkContainsSkolem(tp: Type)(using Context): Boolean = |
| 93 | + var res = true |
| 94 | + tp.foreachPart: |
| 95 | + case QualifiedType(_, qualifier) => |
| 96 | + qualifier.foreachSubTree: subTree => |
| 97 | + subTree.tpe.foreachPart: |
| 98 | + case tp: SkolemType => |
| 99 | + report.error(em"The qualified type $qualifier cannot be checked at runtime", qualifier.srcPos) |
| 100 | + res = false |
| 101 | + case _ => () |
| 102 | + case _ => () |
| 103 | + res |
| 104 | + |
| 105 | + private def equalToPredicate(tree: Tree)(using Context): Tree = |
| 106 | + Lambda( |
| 107 | + MethodType(List("v".toTermName))(_ => List(tree.tpe), _ => defn.BooleanType), |
| 108 | + (args) => Ident(args(0).symbol.termRef).equal(tree) |
| 109 | + ) |
| 110 | + |
| 111 | + private def truePredicate()(using Context): Tree = |
| 112 | + Lambda( |
| 113 | + MethodType(List("v".toTermName))(_ => List(defn.AnyType), _ => defn.BooleanType), |
| 114 | + (args) => Literal(Constant(true)) |
| 115 | + ) |
0 commit comments