Skip to content

Commit 6f1ade0

Browse files
committed
Extracting ApplyOverloaded to be reused in UnPickler and ClassfileParser
1 parent abe786e commit 6f1ade0

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools
22
package dotc
33
package ast
44

5+
import dotty.tools.dotc.typer.ProtoTypes.FunProtoTyped
56
import transform.SymUtils._
67
import core._
78
import util.Positions._, Types._, Contexts._, Constants._, Names._, Flags._
@@ -646,6 +647,23 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
646647
Throw(New(defn.ClassCastExceptionClass.typeRef, Nil)) withPos tree.pos
647648
}
648649
}
650+
651+
def applyOverloaded(receiver: Tree, method: TermName, args: List[Tree], targs: List[Type], expectedType: Type)(implicit ctx: Context): Tree = {
652+
val typer = ctx.typer
653+
val proto = new FunProtoTyped(args, expectedType, typer)
654+
val alts = receiver.tpe.member(method).alternatives.map(_.termRef)
655+
656+
val alternatives = ctx.typer.resolveOverloaded(alts, proto, Nil)
657+
assert(alternatives.size == 1) // this is parsed from bytecode tree. there's nothing user can do about it
658+
659+
val selected = alternatives.head
660+
val fun = receiver
661+
.select(TermRef.withSig(receiver.tpe.normalizedPrefix, selected.termSymbol.asTerm))
662+
.appliedToTypes(targs)
663+
val apply = untpd.Apply(fun, args)
664+
665+
new typer.ApplyToTyped(apply, fun, selected, args, expectedType).result.asInstanceOf[Tree] // needed to handle varargs
666+
}
649667

650668
@tailrec
651669
def sameTypes(trees: List[tpd.Tree], trees1: List[tpd.Tree]): Boolean = {

src/dotty/tools/dotc/core/Annotations.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package core
33

44
import Symbols._, Types._, util.Positions._, Contexts._, Constants._, ast.tpd._
55
import config.ScalaVersion
6+
import StdNames._
7+
import dotty.tools.dotc.ast.{tpd, untpd}
8+
import dotty.tools.dotc.typer.ProtoTypes.FunProtoTyped
69

710
object Annotations {
811

@@ -61,12 +64,24 @@ object Annotations {
6164
def apply(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
6265
apply(New(atp, args))
6366

67+
private def resolveConstructor(atp: Type, args:List[Tree])(implicit ctx: Context): Tree = {
68+
val targs = atp.argTypes
69+
tpd.applyOverloaded(New(atp withoutArgs targs), nme.CONSTRUCTOR, args, targs, atp)
70+
}
71+
72+
def applyResolve(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation = {
73+
apply(resolveConstructor(atp, args))
74+
}
75+
6476
def deferred(sym: Symbol, treeFn: Context => Tree)(implicit ctx: Context): Annotation =
6577
new LazyAnnotation(sym)(treeFn)
6678

6779
def deferred(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
6880
deferred(atp.classSymbol, implicit ctx => New(atp, args))
6981

82+
def deferredResolve(atp: Type, args: List[Tree])(implicit ctx: Context): Annotation =
83+
deferred(atp.classSymbol, implicit ctx => resolveConstructor(atp, args))
84+
7085
def makeAlias(sym: TermSymbol)(implicit ctx: Context) =
7186
apply(defn.AliasAnnot, List(
7287
ref(TermRef.withSigAndDenot(sym.owner.thisType, sym.name, sym.signature, sym))))

src/dotty/tools/dotc/core/pickling/ClassfileParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class ClassfileParser(
440440
}
441441
}
442442
if (hasError || skip) None
443-
else Some(Annotation.deferred(attrType, argbuf.toList))
443+
else Some(Annotation.deferredResolve(attrType, argbuf.toList))
444444
} catch {
445445
case f: FatalError => throw f // don't eat fatal errors, they mean a class was not found
446446
case ex: Throwable =>

src/dotty/tools/dotc/core/pickling/UnPickler.scala

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -832,22 +832,10 @@ class UnPickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClassRoot:
832832
t.toList
833833
}
834834
// println(atp)
835-
val typer = ctx.typer
836-
val proto = new FunProtoTyped(args, atp, typer)
837-
val alts = atp.member(nme.CONSTRUCTOR).alternatives.map(_.termRef)
838-
839-
val constructors = ctx.typer.resolveOverloaded(alts, proto, Nil)
840-
assert(constructors.size == 1) // this is parsed from bytecode tree. there's nothing user can do about it
841-
842-
val constr = constructors.head
843835
val targs = atp.argTypes
844-
val fun = tpd.New(atp withoutArgs targs)
845-
.select(TermRef.withSig(atp.normalizedPrefix, constr.termSymbol.asTerm))
846-
.appliedToTypes(targs)
847-
val apply = untpd.Apply(fun, args)
848-
new typer.ApplyToTyped(apply, fun, constr, args, atp).result.asInstanceOf[tpd.Tree] // needed to handle varargs
849-
// Dotty deviation, for scalac the last cast wouldn't be required
850-
}
836+
837+
tpd.applyOverloaded(tpd.New(atp withoutArgs targs), nme.CONSTRUCTOR, args, targs, atp)
838+
}
851839

852840
/** Read an annotation and as a side effect store it into
853841
* the symbol it requests. Called at top-level, for all

0 commit comments

Comments
 (0)