|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import ast.{Trees, tpd} |
| 5 | +import core._, core.Decorators._ |
| 6 | +import Contexts._, Flags._, Trees._, Types._, StdNames._, Symbols._ |
| 7 | +import Denotations._, SymDenotations._ |
| 8 | +import DenotTransformers._, TreeTransforms._, Phases.Phase |
| 9 | +import ValueClasses._ |
| 10 | + |
| 11 | +/** This phase makes value classes extend VCXPrototype and make their companions extend VCXCompanion. |
| 12 | + * |
| 13 | + * For a value class class V whose erased underlying type is U, X is "U" if U is a primitive |
| 14 | + * type and is "Object" otherwise. |
| 15 | + * |
| 16 | + */ |
| 17 | +class VCParents extends MiniPhaseTransform with DenotTransformer { |
| 18 | + import tpd._ |
| 19 | + |
| 20 | + override def phaseName: String = "vcParents" |
| 21 | + |
| 22 | + override def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation = ref match { |
| 23 | + case moduleClass: ClassDenotation if moduleClass.is(ModuleClass) => |
| 24 | + moduleClass.linkedClass match { |
| 25 | + case valueClass: ClassSymbol if isDerivedValueClass(valueClass) => |
| 26 | + val moduleSym = moduleClass.symbol.asClass |
| 27 | + val cinfo = moduleClass.classInfo |
| 28 | + |
| 29 | + val superType = tpd.ref(defn.vcCompanionOf(valueClass)) |
| 30 | + .select(nme.CONSTRUCTOR) |
| 31 | + .appliedToType(valueClass.typeRef) |
| 32 | + .tpe |
| 33 | + .resultType |
| 34 | + |
| 35 | + moduleClass.copySymDenotation(info = |
| 36 | + cinfo.derivedClassInfo(classParents = |
| 37 | + ctx.normalizeToClassRefs(List(superType), moduleSym, cinfo.decls))) |
| 38 | + case _ => |
| 39 | + moduleClass |
| 40 | + } |
| 41 | + case valueClass: ClassDenotation if isDerivedValueClass(valueClass) => |
| 42 | + val cinfo = valueClass.classInfo |
| 43 | + val superType = defn.vcPrototypeOf(valueClass).typeRef |
| 44 | + |
| 45 | + val (p :: ps) = cinfo.classParents |
| 46 | + assert(p.isRef(defn.AnyValClass)) |
| 47 | + val parents = superType :: ps |
| 48 | + valueClass.copySymDenotation(info = cinfo.derivedClassInfo(classParents = parents)) |
| 49 | + case _ => |
| 50 | + ref |
| 51 | + } |
| 52 | + |
| 53 | + override def transformTemplate(tree: tpd.Template)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = |
| 54 | + ctx.owner.denot match { |
| 55 | + case moduleClass: ClassDenotation if moduleClass.is(ModuleClass) => |
| 56 | + moduleClass.linkedClass match { |
| 57 | + case valueClass: ClassSymbol if isDerivedValueClass(valueClass) => |
| 58 | + val superCall = New(defn.vcCompanionOf(valueClass).typeRef) |
| 59 | + .select(nme.CONSTRUCTOR) |
| 60 | + .appliedToType(valueClass.typeRef) |
| 61 | + .appliedToNone |
| 62 | + |
| 63 | + val (p :: ps) = tree.parents |
| 64 | + // TODO: We shouldn't disallow extending companion objects of value classes |
| 65 | + // with classes other than AnyRef |
| 66 | + assert(p.tpe.isRef(defn.ObjectClass)) |
| 67 | + cpy.Template(tree)(parents = superCall :: ps) |
| 68 | + case _ => |
| 69 | + tree |
| 70 | + } |
| 71 | + case valueClass: ClassDenotation if isDerivedValueClass(valueClass) => |
| 72 | + val prototype = defn.vcPrototypeOf(valueClass).typeRef |
| 73 | + val underlyingSym = valueClassUnbox(valueClass) |
| 74 | + val superCall = New(prototype, List(ref(underlyingSym))) |
| 75 | + // TODO: manually do parameter forwarding: the prototype has a local field |
| 76 | + // so we don't need a field inside the value class |
| 77 | + |
| 78 | + val (p :: ps) = tree.parents |
| 79 | + assert(p.tpe.isRef(defn.AnyValClass)) |
| 80 | + cpy.Template(tree)(parents = superCall :: ps) |
| 81 | + case _ => |
| 82 | + tree |
| 83 | + } |
| 84 | +} |
0 commit comments