Skip to content

Commit

Permalink
cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishiking committed May 8, 2023
1 parent 48cbc42 commit cc83f93
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ object CFuncPtr {
% for N in range(0, 23):
% args = ", ".join("arg" + str(i) + ": T" + str(i) for i in range(1, N+1))
% allTps = ", ".join(["T" + str(i) for i in range(1, N+1)] + ["R"])
% wildcards = ", ".join(["_" for i in range(1, N+1)] + ["_"])
% 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1888,27 +1888,28 @@ trait NirGenExpr[G <: nsc.Global with Singleton] { self: NirGenPhase[G] =>
* and boxing result Apply.args can contain different number of arguments
* depending on usage, however they are passed in constant order:
* - 0..N args
* - 0..N+1 type evidences of args (scalanative.Tag)
* - return type evidence
*/
def genCFuncPtrApply(app: Apply, code: Int): Val = {
val Apply(appRec @ Select(receiverp, _), aargs) = app

val paramTypes = app.attachments.get[NonErasedTypes] match {
case None =>
reporter.error(app.pos, "test")
reporter.error(
app.pos,
s"Failed to generate exact NIR types for $app, something is wrong with scala-native internal."
)
Nil
case Some(NonErasedTypes(paramTys)) => paramTys
}

implicit val pos: nir.Position = app.pos
val argsp = aargs

val self = genExpr(receiverp)
val retType = genType(paramTypes.last)
val unboxedRetType = Type.unbox.getOrElse(retType, retType)

val args = argsp
val args = aargs
.zip(paramTypes)
.map {
case (arg, ty) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ abstract class PrepNativeInterop[G <: Global with Singleton](
)
super.transform(tree)

// Attach exact type information to the AST to preserve the type information
// during the type erase phase and refer to it in the NIR generation phase.
case Apply(fun, args) if CFuncPtrApplyMethods.contains(fun.symbol) =>
val paramTypes =
args.map(t => widenDealiasType(t.tpe)) :+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2338,28 +2338,30 @@ trait NirGenExpr(using Context) {
* and boxing result Apply.args can contain different number of arguments
* depending on usage, however they are passed in constant order:
* - 0..N args
* - 0..N+1 type evidences of args (scalanative.Tag)
* - return type evidence
*/
private def genCFuncPtrApply(app: Apply): Val = {
given nir.Position = app.span
val Apply(appRec @ Select(receiverp, _), aargs) = app: @unchecked

val paramTypes = app.getAttachment(NirDefinitions.NonErasedTypes).get
val paramTypes = app.getAttachment(NirDefinitions.NonErasedTypes) match
case None =>
report.error(
s"Failed to generated exact NIR types for $app, something is wrong with scala-native internls.",
app.srcPos
)
Nil
case Some(paramTys) => paramTys

val argsp = aargs
val self = genExpr(receiverp)

val retType = genType(paramTypes.last)
val unboxedRetType = Type.unbox.getOrElse(retType, retType)

val args = argsp
val args = aargs
.zip(paramTypes)
.map {
case (Apply(Select(_, nme.box), List(value)), _) =>
genExpr(value)
case (Apply(Ident(nme.box), List(value)), _) =>
genExpr(value)
case (arg, ty) =>
given nir.Position = arg.span
val tpe = genType(ty)
Expand Down Expand Up @@ -2387,8 +2389,15 @@ trait NirGenExpr(using Context) {

private def genCFuncFromScalaFunction(app: Apply): Val = {
given pos: nir.Position = app.span
val paramTys = app.getAttachment(NirDefinitions.NonErasedTypes).get
val paramTypes: List[SimpleType] = paramTys.map(fromType)
val paramTypes = app.getAttachment(NirDefinitions.NonErasedTypes) match
case None =>
report.error(
s"Failed to generated exact NIR types for $app, something is wrong with scala-native internls.",
app.srcPos
)
Nil
case Some(paramTys) =>
paramTys.map(fromType)

val fn :: _ = app.args: @unchecked

Expand All @@ -2412,7 +2421,6 @@ trait NirGenExpr(using Context) {
fn,
paramTypes
)

fnRef

case ref: RefTree =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PostInlineNativeInterop extends PluginPhase {
val defnNir = this.defnNir
def dealiasTypeMapper = DealiasTypeMapper()

// Attach exact type information to the AST to preserve the type information
// during the type erase phase and refer to it in the NIR generation phase.
tree match
case app @ Apply(TypeApply(fun, tArgs), _)
if defnNir.CFuncPtr_fromScalaFunction.contains(fun.symbol) =>
Expand Down

0 comments on commit cc83f93

Please sign in to comment.