|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import MegaPhase._ |
| 5 | +import core.DenotTransformers.{IdentityDenotTransformer} |
| 6 | +import core.Symbols._ |
| 7 | +import core.Contexts._ |
| 8 | +import core.Types._ |
| 9 | +import core.Flags._ |
| 10 | +import core.StdNames.nme |
| 11 | +import core.NameKinds.CacheName |
| 12 | +import core.Constants.Constant |
| 13 | +import core.Decorators._ |
| 14 | +import core.TypeErasure.erasure |
| 15 | +import ast.tpd |
| 16 | + |
| 17 | +object CacheAliasImplicits { |
| 18 | + val name: String = "cacheAliasImplicits" |
| 19 | + |
| 20 | + /** Flags that disable caching */ |
| 21 | + val NoCacheFlags = |
| 22 | + StableRealizable | // It's a simple forwarder, leave it as one |
| 23 | + Exported // Export forwarders are never cached |
| 24 | +} |
| 25 | + |
| 26 | +/** This phase ensures that the right hand side of parameterless alias implicits |
| 27 | + * is cached. It applies to all alias implicits that have neither type parameters |
| 28 | + * not a given clause. Example: The alias |
| 29 | + * |
| 30 | + * implicit a for TC = rhs |
| 31 | + * |
| 32 | + * is expanded before this phase |
| 33 | + * |
| 34 | + * implicit def a: TC = rhs |
| 35 | + * |
| 36 | + * It is then expanded further as follows: |
| 37 | + * |
| 38 | + * 1. If `rhs` is a simple name `x` (possibly with a `this.` prefix), leave the definition as is. |
| 39 | + * 2. Otherwise, if `rhs` is a pure path, replace the definition with |
| 40 | + * |
| 41 | + * implicit val a: TC = rhs |
| 42 | + * |
| 43 | + * 3. Otherwise, if `TC` is a reference type, replace the definition with |
| 44 | + * |
| 45 | + * private[this] var a$_cache: TC = null |
| 46 | + * implicit def a: TC = { if (a$_cache == null) a$_cache = rhs; a$_cache } |
| 47 | + * |
| 48 | + * 4. Otherwise `TC` is a value type. Replace the definition with |
| 49 | + * |
| 50 | + * lazy implicit val a: TC = rhs |
| 51 | + */ |
| 52 | +class CacheAliasImplicits extends MiniPhase with IdentityDenotTransformer { thisPhase => |
| 53 | + import tpd._ |
| 54 | + |
| 55 | + override def phaseName: String = CacheAliasImplicits.name |
| 56 | + |
| 57 | + override def transformDefDef(tree: DefDef)(implicit ctx: Context): Tree = { |
| 58 | + val sym = tree.symbol |
| 59 | + sym.info match { |
| 60 | + case ExprType(rhsType) if sym.is(Implied, butNot = CacheAliasImplicits.NoCacheFlags) => |
| 61 | + // If rhs is a simple TermRef, leave a def. |
| 62 | + tree.rhs.tpe match { |
| 63 | + case TermRef(pre, _) => |
| 64 | + pre match { |
| 65 | + case NoPrefix => return tree |
| 66 | + case pre: ThisType if pre.cls == ctx.owner.enclosingClass => return tree |
| 67 | + case _ => |
| 68 | + } |
| 69 | + case _ => |
| 70 | + } |
| 71 | + def makeVal(additionalFlags: FlagSet) = { |
| 72 | + sym.copySymDenotation( |
| 73 | + initFlags = sym.flags &~ Method | additionalFlags, |
| 74 | + info = rhsType) |
| 75 | + .installAfter(thisPhase) |
| 76 | + cpy.ValDef(tree)(tree.name, tree.tpt, tree.rhs) |
| 77 | + } |
| 78 | + if (isPurePath(tree.rhs)) makeVal(EmptyFlags) |
| 79 | + else if (rhsType.classSymbol.isValueClass || |
| 80 | + !erasure(rhsType).typeSymbol.derivesFrom(defn.ObjectClass)) makeVal(Lazy) |
| 81 | + else { |
| 82 | + val cacheFlags = if (ctx.owner.isClass) Private | Local | Mutable else Mutable |
| 83 | + val cacheSym = |
| 84 | + ctx.newSymbol(ctx.owner, CacheName(tree.name), cacheFlags, rhsType, coord = sym.coord) |
| 85 | + if (ctx.owner.isClass) cacheSym.enteredAfter(thisPhase) |
| 86 | + val cacheDef = ValDef(cacheSym, tpd.defaultValue(rhsType)) |
| 87 | + val cachingDef = cpy.DefDef(tree)(rhs = |
| 88 | + Block( |
| 89 | + If( |
| 90 | + ref(cacheSym).select(defn.Any_==).appliedTo(nullLiteral), |
| 91 | + Assign(ref(cacheSym), tree.rhs), |
| 92 | + unitLiteral) :: Nil, |
| 93 | + ref(cacheSym) |
| 94 | + ) |
| 95 | + ) |
| 96 | + Thicket(cacheDef, cachingDef) |
| 97 | + } |
| 98 | + case _ => tree |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments