|
| 1 | +package dotty.tools |
| 2 | +package dotc |
| 3 | +package cc |
| 4 | + |
| 5 | +import core.* |
| 6 | +import Types.*, Symbols.*, Contexts.*, Annotations.* |
| 7 | +import ast.{tpd, untpd} |
| 8 | +import Decorators.* |
| 9 | +import config.Printers.capt |
| 10 | +import util.Property.Key |
| 11 | +import tpd.* |
| 12 | + |
| 13 | +private val Captures: Key[CaptureSet] = Key() |
| 14 | +private val IsBoxed: Key[Unit] = Key() |
| 15 | + |
| 16 | +def retainedElems(tree: Tree)(using Context): List[Tree] = tree match |
| 17 | + case Apply(_, Typed(SeqLiteral(elems, _), _) :: Nil) => elems |
| 18 | + case _ => Nil |
| 19 | + |
| 20 | +extension (tree: Tree) |
| 21 | + |
| 22 | + def toCaptureRef(using Context): CaptureRef = tree.tpe.asInstanceOf[CaptureRef] |
| 23 | + |
| 24 | + def toCaptureSet(using Context): CaptureSet = |
| 25 | + tree.getAttachment(Captures) match |
| 26 | + case Some(refs) => refs |
| 27 | + case None => |
| 28 | + val refs = CaptureSet(retainedElems(tree).map(_.toCaptureRef)*) |
| 29 | + .showing(i"toCaptureSet $tree --> $result", capt) |
| 30 | + tree.putAttachment(Captures, refs) |
| 31 | + refs |
| 32 | + |
| 33 | + def isBoxedCapturing(using Context): Boolean = |
| 34 | + tree.hasAttachment(IsBoxed) |
| 35 | + |
| 36 | + def setBoxedCapturing()(using Context): Unit = |
| 37 | + tree.putAttachment(IsBoxed, ()) |
| 38 | + |
| 39 | +extension (tp: Type) |
| 40 | + |
| 41 | + def derivedCapturingType(parent: Type, refs: CaptureSet)(using Context): Type = tp match |
| 42 | + case CapturingType(p, r, b) => |
| 43 | + if (parent eq p) && (refs eq r) then tp |
| 44 | + else CapturingType(parent, refs, b) |
| 45 | + |
| 46 | + /** If this is type variable instantiated or upper bounded with a capturing type, |
| 47 | + * the capture set associated with that type. Extended to and-or types and |
| 48 | + * type proxies in the obvious way. If a term has a type with a boxed captureset, |
| 49 | + * that captureset counts towards the capture variables of the envirionment. |
| 50 | + */ |
| 51 | + def boxedCaptured(using Context): CaptureSet = |
| 52 | + def getBoxed(tp: Type): CaptureSet = tp match |
| 53 | + case CapturingType(_, refs, boxed) => if boxed then refs else CaptureSet.empty |
| 54 | + case tp: TypeProxy => getBoxed(tp.superType) |
| 55 | + case tp: AndType => getBoxed(tp.tp1) ++ getBoxed(tp.tp2) |
| 56 | + case tp: OrType => getBoxed(tp.tp1) ** getBoxed(tp.tp2) |
| 57 | + case _ => CaptureSet.empty |
| 58 | + getBoxed(tp) |
| 59 | + |
| 60 | + def isBoxedCapturing(using Context) = !tp.boxedCaptured.isAlwaysEmpty |
| 61 | + |
| 62 | + def canHaveInferredCapture(using Context): Boolean = tp match |
| 63 | + case tp: TypeRef if tp.symbol.isClass => |
| 64 | + !tp.symbol.isValueClass && tp.symbol != defn.AnyClass |
| 65 | + case _: TypeVar | _: TypeParamRef => |
| 66 | + false |
| 67 | + case tp: TypeProxy => |
| 68 | + tp.superType.canHaveInferredCapture |
| 69 | + case tp: AndType => |
| 70 | + tp.tp1.canHaveInferredCapture && tp.tp2.canHaveInferredCapture |
| 71 | + case tp: OrType => |
| 72 | + tp.tp1.canHaveInferredCapture || tp.tp2.canHaveInferredCapture |
| 73 | + case _ => |
| 74 | + false |
| 75 | + |
| 76 | + def stripCapturing(using Context): Type = tp.dealiasKeepAnnots match |
| 77 | + case CapturingType(parent, _, _) => |
| 78 | + parent.stripCapturing |
| 79 | + case atd @ AnnotatedType(parent, annot) => |
| 80 | + atd.derivedAnnotatedType(parent.stripCapturing, annot) |
| 81 | + case _ => |
| 82 | + tp |
0 commit comments