Skip to content

Commit

Permalink
Remove unnecessary _fromScalaFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishiking committed May 8, 2023
1 parent e296638 commit cc62ca2
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 89 deletions.
46 changes: 0 additions & 46 deletions nativelib/src/main/scala/scala/scalanative/unsafe/CFuncPtr.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ object CFuncPtr {
% evidences = ", ".join(["ev{}: Tag[T{}]".format(i, i) for i in range(1, N+1)] + ["evRet: Tag[R]"])
% CFuncPtrN = "CFuncPtr{}[{}]".format(N, allTps)
% FunctionN = "Function{}[{}]".format(N, allTps)
% CFuncPtrNWildcard = "CFuncPtr{}[{}]".format(N, wildcards)
% FunctionNWildcard = "Function{}[{}]".format(N, wildcards)

final class ${CFuncPtrN} private (rawptr: RawPtr) extends CFuncPtr(rawptr) {
def apply(${args}): R = intrinsic
Expand All @@ -43,8 +41,6 @@ final class ${CFuncPtrN} private (rawptr: RawPtr) extends CFuncPtr(rawptr) {
object CFuncPtr${N} {
implicit def fromScalaFunction[${allTps}](fn: ${FunctionN}): ${CFuncPtrN} = intrinsic

def _fromScalaFunction(fn: ${FunctionNWildcard}): ${CFuncPtrNWildcard} = intrinsic

private[scalanative] def fromRawPtr[${allTps}](ptr: RawPtr): ${CFuncPtrN} = {
new ${CFuncPtrN}(ptr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,6 @@ trait NirDefinitions {
getMember(module, TermName(s"fromScalaFunction"))
}

lazy val _CFuncPtrFromFunctionMethods =
CFuncPtrNModule.zipWithIndex.map {
case (module, n) =>
getMember(module, TermName(s"_fromScalaFunction"))
}

lazy val CastRawSizeToInt =
getMember(IntrinsicsModule, TermName("castRawSizeToInt"))
lazy val CastRawSizeToLong =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ trait NirGenExpr[G <: nsc.Global with Singleton] { self: NirGenPhase[G] =>
val Apply(appRec @ Select(receiverp, _), aargs) = app

val paramTypes = app.attachments.get[NonErasedTypes] match {
case None =>
case None =>
reporter.error(app.pos, "test")
Nil
case Some(NonErasedTypes(paramTys)) => paramTys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ abstract class NirPrimitives {

addPrimitives(CFuncPtrApplyMethods, CFUNCPTR_APPLY)
addPrimitives(CFuncPtrFromFunctionMethods, CFUNCPTR_FROM_FUNCTION)
addPrimitives(_CFuncPtrFromFunctionMethods, CFUNCPTR_FROM_FUNCTION)
addPrimitive(ClassFieldRawPtrMethod, CLASS_FIELD_RAWPTR)
addPrimitives(SizeOfMethods, SIZE_OF)
addPrimitives(AlignmentOfMethods, ALIGNMENT_OF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,10 @@ abstract class PrepNativeInterop[G <: Global with Singleton](
)
super.transform(tree)

case Apply(TypeApply(fun, tArgs), args) if CFuncPtrFromFunctionMethods.contains(fun.symbol) =>
val idx = CFuncPtrFromFunctionMethods.indexOf(fun.symbol)
val transformed = _CFuncPtrFromFunctionMethods(idx)
val tys = tArgs.map(t => widenDealiasType(t.tpe))
typer
.typed {
Apply(transformed, args: _*)
}
.updateAttachment(NonErasedTypes(tys))
.setPos(tree.pos)

case Apply(fun, args) if CFuncPtrApplyMethods.contains(fun.symbol) =>
val paramTypes = args.map(t => widenDealiasType(t.tpe)) :+ widenDealiasType(tree.tpe.finalResultType)
val paramTypes =
args.map(t => widenDealiasType(t.tpe)) :+
widenDealiasType(tree.tpe.finalResultType)
tree.updateAttachment(NonErasedTypes(paramTypes))

case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ final class NirDefinitions()(using ctx: Context) {
@tu lazy val CFuncPtrNModules = CFuncPtrNNames.map(requiredModule)
@tu lazy val CFuncPtr_apply = CFuncPtrNClass.map(_.requiredMethod("apply"))
@tu lazy val CFuncPtr_fromScalaFunction = CFuncPtrNModules.map(_.requiredMethod("fromScalaFunction"))
@tu lazy val _CFuncPtr_fromScalaFunction = CFuncPtrNModules.map(_.requiredMethod("_fromScalaFunction"))

@tu lazy val CStructClasses = (0 to 22).map(n => requiredClass("scala.scalanative.unsafe.CStruct" + n))
@tu lazy val CArrayClass = requiredClass("scala.scalanative.unsafe.CArray")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,7 @@ trait NirGenExpr(using Context) {
fn,
paramTypes
)

fnRef

case ref: RefTree =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class NirPrimitives(using ctx: Context) extends DottyPrimitives(ctx) {
addPrimitive(defnNir.Intrinsics_castLongToRawSize, CAST_LONG_TO_RAWSIZE)
addPrimitives(defnNir.CFuncPtr_apply, CFUNCPTR_APPLY)
addPrimitives(defnNir.CFuncPtr_fromScalaFunction, CFUNCPTR_FROM_FUNCTION)
addPrimitives(defnNir._CFuncPtr_fromScalaFunction, CFUNCPTR_FROM_FUNCTION)
addPrimitive(defnNir.Intrinsics_classFieldRawPtr, CLASS_FIELD_RAWPTR)
addPrimitives(defnNir.Intrinsics_sizeOfAlts, SIZE_OF)
addPrimitives(defnNir.Intrinsics_alignmentOfAlts, ALIGNMENT_OF)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,21 @@ class PostInlineNativeInterop extends PluginPhase {
def dealiasTypeMapper = DealiasTypeMapper()

tree match
// fromScalaFunction[T1, R](fn) -> _fromScalaFunction(fn)
case Apply(TypeApply(fun, tArgs), args) if defnNir.CFuncPtr_fromScalaFunction.contains(fun.symbol) =>
val idx = defnNir.CFuncPtr_fromScalaFunction.indexOf(fun.symbol)
val transformed = defnNir._CFuncPtr_fromScalaFunction(idx)
val cls = defnNir.CFuncPtrNClass(idx)
val tys = tArgs.map(t => dealiasTypeMapper(t.typeOpt))
cpy
.Apply(tree)(
ref(transformed),
args
)
.withAttachment(NirDefinitions.NonErasedTypes, tys)

case app @ Apply(TypeApply(fun, tArgs), _)
if defnNir.CFuncPtr_fromScalaFunction.contains(fun.symbol) =>
val tys = tArgs.map(t => dealiasTypeMapper(t.tpe))
app.withAttachment(NirDefinitions.NonErasedTypes, tys)

case Apply(fun, args) if defnNir.CFuncPtr_apply.contains(fun.symbol) =>
val paramTypes = args.map(a => dealiasTypeMapper(a.tpe)) :+ dealiasTypeMapper(tree.tpe.finalResultType)
val paramTypes =
args.map(a => dealiasTypeMapper(a.tpe)) :+
dealiasTypeMapper(tree.tpe.finalResultType)
tree.withAttachment(NirDefinitions.NonErasedTypes, paramTypes)

case _ => tree

}



override def transformTypeApply(tree: TypeApply)(using Context): Tree = {
val TypeApply(fun, tArgs) = tree
val defnNir = this.defnNir
Expand Down

0 comments on commit cc62ca2

Please sign in to comment.