diff options
author | Martin Odersky <odersky@gmail.com> | 2017-01-05 16:02:09 +0700 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2017-01-05 18:00:06 +0700 |
commit | aa6ebe938639f07dd6f5612e645f1449f37a86eb (patch) | |
tree | 24d257c1f12d0f5cb9b76102043046e3a904d98a /compiler/src/dotty/tools/dotc/typer/Dynamic.scala | |
parent | 42eb864dc752254fc3b8b0428570fe94aa1dafc7 (diff) | |
download | dotty-aa6ebe938639f07dd6f5612e645f1449f37a86eb.tar.gz dotty-aa6ebe938639f07dd6f5612e645f1449f37a86eb.tar.bz2 dotty-aa6ebe938639f07dd6f5612e645f1449f37a86eb.zip |
Implement structural type member access
New scheme for implementing structural type member access.
Diffstat (limited to 'compiler/src/dotty/tools/dotc/typer/Dynamic.scala')
-rw-r--r-- | compiler/src/dotty/tools/dotc/typer/Dynamic.scala | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/compiler/src/dotty/tools/dotc/typer/Dynamic.scala b/compiler/src/dotty/tools/dotc/typer/Dynamic.scala index 4039c8b81..7cfd6327b 100644 --- a/compiler/src/dotty/tools/dotc/typer/Dynamic.scala +++ b/compiler/src/dotty/tools/dotc/typer/Dynamic.scala @@ -7,10 +7,13 @@ import dotty.tools.dotc.ast.tpd import dotty.tools.dotc.ast.untpd import dotty.tools.dotc.core.Constants.Constant import dotty.tools.dotc.core.Contexts.Context -import dotty.tools.dotc.core.Names.Name +import dotty.tools.dotc.core.Names.{Name, TermName} import dotty.tools.dotc.core.StdNames._ import dotty.tools.dotc.core.Types._ import dotty.tools.dotc.core.Decorators._ +import core.Symbols._ +import core.Definitions +import Inferencing._ import ErrorReporting._ object Dynamic { @@ -18,7 +21,10 @@ object Dynamic { name == nme.applyDynamic || name == nme.selectDynamic || name == nme.updateDynamic || name == nme.applyDynamicNamed } -/** Translates selection that does not typecheck according to the scala.Dynamic rules: +/** Handles programmable member selections of `Dynamic` instances and values + * with structural types. Two functionalities: + * + * 1. Translates selection that does not typecheck according to the scala.Dynamic rules: * foo.bar(baz) = quux ~~> foo.selectDynamic(bar).update(baz, quux) * foo.bar = baz ~~> foo.updateDynamic("bar")(baz) * foo.bar(x = bazX, y = bazY, baz, ...) ~~> foo.applyDynamicNamed("bar")(("x", bazX), ("y", bazY), ("", baz), ...) @@ -26,6 +32,10 @@ object Dynamic { * foo.bar ~~> foo.selectDynamic(bar) * * The first matching rule of is applied. + * + * 2. Translates member seclections on structural types by means of an implicit + * Projector instance. @See handleStructural. + * */ trait Dynamic { self: Typer with Applications => import Dynamic._ @@ -100,4 +110,54 @@ trait Dynamic { self: Typer with Applications => else untpd.TypeApply(select, targs) untpd.Apply(selectWithTypes, Literal(Constant(name.toString))) } + + /** Handle reflection-based dispatch for members of structural types. + * Given `x.a`, where `x` is of (widened) type `T` and `x.a` is of type `U`: + * + * If `U` is a value type, map `x.a` to the equivalent of: + * + * implicitly[Projection[T]].get(x, "a").asInstanceOf[U] + * + * If `U` is a method type (T1,...,Tn)R, map `x.a` to the equivalent of: + * + * implicitly[Projection[T]].getMethod(x, "a")(CT1, ..., CTn).asInstanceOf[(T1,...,Tn) => R] + * + * where CT1,...,CTn are the classtags representing the erasure of T1,...,Tn. + * + * The small print: (1) T is forced to be fully defined. (2) It's an error if + * U is neither a value nor a method type, or a dependent method type, or of too + * large arity (limit is Definitions.MaxStructuralMethodArity). + */ + def handleStructural(tree: Tree)(implicit ctx: Context): Tree = { + val Select(qual, name) = tree + + def issueError(msgFn: String => String): Unit = ctx.error(msgFn("reflective call"), tree.pos) + def implicitArg(tpe: Type) = inferImplicitArg(tpe, issueError, tree.pos.endPos) + val projector = implicitArg(defn.ProjectorType.appliedTo(qual.tpe.widen)) + + def structuralCall(getterName: TermName, formals: List[Tree]) = { + val scall = untpd.Apply( + untpd.TypedSplice(projector.select(getterName)), + (qual :: Literal(Constant(name.toString)) :: formals).map(untpd.TypedSplice(_))) + typed(scall) + } + def fail(reason: String) = + errorTree(tree, em"Structural access not allowed on method $name because it $reason") + fullyDefinedType(tree.tpe.widen, "structural access", tree.pos) match { + case tpe: MethodType => + if (tpe.isDependent) + fail(i"has a dependent method type") + else if (tpe.paramNames.length > Definitions.MaxStructuralMethodArity) + fail(i"takes too many parameters") + val ctags = tpe.paramTypes.map(pt => + implicitArg(defn.ClassTagType.appliedTo(pt :: Nil))) + structuralCall(nme.getMethod, ctags).asInstance(tpe.toFunctionType()) + case tpe: ValueType => + structuralCall(nme.get, Nil).asInstance(tpe) + case tpe: PolyType => + fail("is polymorphic") + case tpe => + fail(i"has an unsupported type: $tpe") + } + } } |