|
| 1 | +package dotty.tools.scaladoc |
| 2 | + |
| 3 | +package cc |
| 4 | + |
| 5 | +import scala.quoted._ |
| 6 | + |
| 7 | +object CaptureDefs: |
| 8 | + // these should become part of the reflect API in the distant future |
| 9 | + def retains(using qctx: Quotes) = |
| 10 | + qctx.reflect.Symbol.requiredClass("scala.annotation.retains") |
| 11 | + def retainsCap(using qctx: Quotes) = |
| 12 | + qctx.reflect.Symbol.requiredClass("scala.annotation.retainsCap") |
| 13 | + def retainsByName(using qctx: Quotes) = |
| 14 | + qctx.reflect.Symbol.requiredClass("scala.annotation.retainsByName") |
| 15 | + def CapsModule(using qctx: Quotes) = |
| 16 | + qctx.reflect.Symbol.requiredPackage("scala.caps") |
| 17 | + def captureRoot(using qctx: Quotes) = |
| 18 | + qctx.reflect.Symbol.requiredPackage("scala.caps.cap") |
| 19 | + def Caps_Capability(using qctx: Quotes) = |
| 20 | + qctx.reflect.Symbol.requiredClass("scala.caps.Capability") |
| 21 | + def Caps_CapSet(using qctx: Quotes) = |
| 22 | + qctx.reflect.Symbol.requiredClass("scala.caps.CapSet") |
| 23 | + def Caps_Mutable(using qctx: Quotes) = |
| 24 | + qctx.reflect.Symbol.requiredClass("scala.caps.Mutable") |
| 25 | + def Caps_SharedCapability(using qctx: Quotes) = |
| 26 | + qctx.reflect.Symbol.requiredClass("scala.caps.SharedCapability") |
| 27 | + def UseAnnot(using qctx: Quotes) = |
| 28 | + qctx.reflect.Symbol.requiredClass("scala.caps.use") |
| 29 | + def ConsumeAnnot(using qctx: Quotes) = |
| 30 | + qctx.reflect.Symbol.requiredClass("scala.caps.consume") |
| 31 | + def ReachCapabilityAnnot(using qctx: Quotes) = |
| 32 | + qctx.reflect.Symbol.requiredClass("scala.annotation.internal.reachCapability") |
| 33 | + def RootCapabilityAnnot(using qctx: Quotes) = |
| 34 | + qctx.reflect.Symbol.requiredClass("scala.caps.internal.rootCapability") |
| 35 | + def ReadOnlyCapabilityAnnot(using qctx: Quotes) = |
| 36 | + qctx.reflect.Symbol.requiredClass("scala.annotation.internal.readOnlyCapability") |
| 37 | + def RequiresCapabilityAnnot(using qctx: Quotes) = |
| 38 | + qctx.reflect.Symbol.requiredClass("scala.annotation.internal.requiresCapability") |
| 39 | + |
| 40 | + def LanguageExperimental(using qctx: Quotes) = |
| 41 | + qctx.reflect.Symbol.requiredPackage("scala.language.experimental") |
| 42 | + |
| 43 | + def ImpureFunction1(using qctx: Quotes) = |
| 44 | + qctx.reflect.Symbol.requiredClass("scala.ImpureFunction1") |
| 45 | + |
| 46 | + def ImpureContextFunction1(using qctx: Quotes) = |
| 47 | + qctx.reflect.Symbol.requiredClass("scala.ImpureContextFunction1") |
| 48 | + |
| 49 | + def Function1(using qctx: Quotes) = |
| 50 | + qctx.reflect.Symbol.requiredClass("scala.Function1") |
| 51 | + |
| 52 | + def ContextFunction1(using qctx: Quotes) = |
| 53 | + qctx.reflect.Symbol.requiredClass("scala.ContextFunction1") |
| 54 | + |
| 55 | + val useAnnotFullName: String = "scala.caps.use.<init>" |
| 56 | + val consumeAnnotFullName: String = "scala.caps.consume.<init>" |
| 57 | + val ccImportSelector = "captureChecking" |
| 58 | +end CaptureDefs |
| 59 | + |
| 60 | +extension (using qctx: Quotes)(ann: qctx.reflect.Symbol) |
| 61 | + /** This symbol is one of `retains` or `retainsCap` */ |
| 62 | + def isRetains: Boolean = |
| 63 | + ann == CaptureDefs.retains || ann == CaptureDefs.retainsCap |
| 64 | + |
| 65 | + /** This symbol is one of `retains`, `retainsCap`, or `retainsByName` */ |
| 66 | + def isRetainsLike: Boolean = |
| 67 | + ann.isRetains || ann == CaptureDefs.retainsByName |
| 68 | + |
| 69 | + def isReachCapabilityAnnot: Boolean = |
| 70 | + ann == CaptureDefs.ReachCapabilityAnnot |
| 71 | + |
| 72 | + def isReadOnlyCapabilityAnnot: Boolean = |
| 73 | + ann == CaptureDefs.ReadOnlyCapabilityAnnot |
| 74 | +end extension |
| 75 | + |
| 76 | +extension (using qctx: Quotes)(tpe: qctx.reflect.TypeRepr) // FIXME clean up and have versions on Symbol for those |
| 77 | + def isCaptureRoot: Boolean = |
| 78 | + import qctx.reflect.* |
| 79 | + tpe match |
| 80 | + case TermRef(ThisType(TypeRef(NoPrefix(), "caps")), "cap") => true |
| 81 | + case TermRef(TermRef(ThisType(TypeRef(NoPrefix(), "scala")), "caps"), "cap") => true |
| 82 | + case TermRef(TermRef(TermRef(TermRef(NoPrefix(), "_root_"), "scala"), "caps"), "cap") => true |
| 83 | + case _ => false |
| 84 | + |
| 85 | + // NOTE: There's something horribly broken with Symbols, and we can't rely on tests like .isContextFunctionType either, |
| 86 | + // so we do these lame string comparisons instead. |
| 87 | + def isImpureFunction1: Boolean = tpe.typeSymbol.fullName == "scala.ImpureFunction1" |
| 88 | + |
| 89 | + def isImpureContextFunction1: Boolean = tpe.typeSymbol.fullName == "scala.ImpureContextFunction1" |
| 90 | + |
| 91 | + def isFunction1: Boolean = tpe.typeSymbol.fullName == "scala.Function1" |
| 92 | + |
| 93 | + def isContextFunction1: Boolean = tpe.typeSymbol.fullName == "scala.ContextFunction1" |
| 94 | + |
| 95 | + def isAnyImpureFunction: Boolean = tpe.typeSymbol.fullName.startsWith("scala.ImpureFunction") |
| 96 | + |
| 97 | + def isAnyImpureContextFunction: Boolean = tpe.typeSymbol.fullName.startsWith("scala.ImpureContextFunction") |
| 98 | + |
| 99 | + def isAnyFunction: Boolean = tpe.typeSymbol.fullName.startsWith("scala.Function") |
| 100 | + |
| 101 | + def isAnyContextFunction: Boolean = tpe.typeSymbol.fullName.startsWith("scala.ContextFunction") |
| 102 | + |
| 103 | + def isCapSet: Boolean = tpe.typeSymbol == CaptureDefs.Caps_CapSet |
| 104 | + |
| 105 | + def isCapSetPure: Boolean = |
| 106 | + tpe.isCapSet && tpe.match |
| 107 | + case CapturingType(_, refs) => refs.isEmpty |
| 108 | + case _ => true |
| 109 | + |
| 110 | + def isCapSetCap: Boolean = |
| 111 | + tpe.isCapSet && tpe.match |
| 112 | + case CapturingType(_, List(ref)) => ref.isCaptureRoot |
| 113 | + case _ => false |
| 114 | +end extension |
| 115 | + |
| 116 | +extension (using qctx: Quotes)(typedef: qctx.reflect.TypeDef) |
| 117 | + def derivesFromCapSet: Boolean = |
| 118 | + import qctx.reflect.* |
| 119 | + typedef.rhs.match |
| 120 | + case t: TypeTree => t.tpe.derivesFrom(CaptureDefs.Caps_CapSet) |
| 121 | + case t: TypeBoundsTree => t.tpe.derivesFrom(CaptureDefs.Caps_CapSet) |
| 122 | + case _ => false |
| 123 | +end extension |
| 124 | + |
| 125 | +/** Matches `import scala.language.experimental.captureChecking` */ |
| 126 | +object CCImport: |
| 127 | + def unapply(using qctx: Quotes)(tree: qctx.reflect.Tree): Boolean = |
| 128 | + import qctx.reflect._ |
| 129 | + tree match |
| 130 | + case imprt: Import if imprt.expr.tpe.termSymbol == CaptureDefs.LanguageExperimental => |
| 131 | + imprt.selectors.exists { |
| 132 | + case SimpleSelector(s) if s == CaptureDefs.ccImportSelector => true |
| 133 | + case _ => false |
| 134 | + } |
| 135 | + case _ => false |
| 136 | + end unapply |
| 137 | +end CCImport |
| 138 | + |
| 139 | +object ReachCapability: |
| 140 | + def unapply(using qctx: Quotes)(ty: qctx.reflect.TypeRepr): Option[qctx.reflect.TypeRepr] = |
| 141 | + import qctx.reflect._ |
| 142 | + ty match |
| 143 | + case AnnotatedType(base, Apply(Select(New(annot), _), Nil)) if annot.symbol.isReachCapabilityAnnot => |
| 144 | + Some(base) |
| 145 | + case _ => None |
| 146 | +end ReachCapability |
| 147 | + |
| 148 | +object ReadOnlyCapability: |
| 149 | + def unapply(using qctx: Quotes)(ty: qctx.reflect.TypeRepr): Option[qctx.reflect.TypeRepr] = |
| 150 | + import qctx.reflect._ |
| 151 | + ty match |
| 152 | + case AnnotatedType(base, Apply(Select(New(annot), _), Nil)) if annot.symbol.isReadOnlyCapabilityAnnot => |
| 153 | + Some(base) |
| 154 | + case _ => None |
| 155 | +end ReadOnlyCapability |
| 156 | + |
| 157 | +/** Decompose capture sets in the union-type-encoding into the sequence of atomic `TypeRepr`s. |
| 158 | + * Returns `None` if the type is not a capture set. |
| 159 | +*/ |
| 160 | +def decomposeCaptureRefs(using qctx: Quotes)(typ0: qctx.reflect.TypeRepr): Option[List[qctx.reflect.TypeRepr]] = |
| 161 | + import qctx.reflect._ |
| 162 | + val buffer = collection.mutable.ListBuffer.empty[TypeRepr] |
| 163 | + def include(t: TypeRepr): Boolean = { buffer += t; true } |
| 164 | + def traverse(typ: TypeRepr): Boolean = |
| 165 | + typ match |
| 166 | + case t if t.typeSymbol == defn.NothingClass => true |
| 167 | + case OrType(t1, t2) => traverse(t1) && traverse(t2) |
| 168 | + case t @ ThisType(_) => include(t) |
| 169 | + case t @ TermRef(_, _) => include(t) |
| 170 | + case t @ ParamRef(_, _) => include(t) |
| 171 | + case t @ ReachCapability(_) => include(t) |
| 172 | + case t @ ReadOnlyCapability(_) => include(t) |
| 173 | + case t : TypeRef => include(t) // FIXME: does this need a more refined check? |
| 174 | + case _ => report.warning(s"Unexpected type tree $typ while trying to extract capture references from $typ0"); false // TODO remove warning eventually |
| 175 | + if traverse(typ0) then Some(buffer.toList) else None |
| 176 | +end decomposeCaptureRefs |
| 177 | + |
| 178 | +object CaptureSetType: |
| 179 | + def unapply(using qctx: Quotes)(tt: qctx.reflect.TypeTree): Option[List[qctx.reflect.TypeRepr]] = decomposeCaptureRefs(tt.tpe) |
| 180 | +end CaptureSetType |
| 181 | + |
| 182 | +object CapturingType: |
| 183 | + def unapply(using qctx: Quotes)(typ: qctx.reflect.TypeRepr): Option[(qctx.reflect.TypeRepr, List[qctx.reflect.TypeRepr])] = |
| 184 | + import qctx.reflect._ |
| 185 | + typ match |
| 186 | + case AnnotatedType(base, Apply(TypeApply(Select(New(annot), _), List(CaptureSetType(refs))), Nil)) if annot.symbol.isRetainsLike => |
| 187 | + Some((base, refs)) |
| 188 | + case AnnotatedType(base, Apply(Select(New(annot), _), Nil)) if annot.symbol == CaptureDefs.retainsCap => |
| 189 | + Some((base, List(CaptureDefs.captureRoot.termRef))) |
| 190 | + case _ => None |
| 191 | +end CapturingType |
0 commit comments