|
| 1 | +/* |
| 2 | + * Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com> |
| 3 | + */ |
| 4 | + |
| 5 | +sealed abstract class Type(val code: Char, val prim: String, val ref: String) |
| 6 | +object Type { |
| 7 | + case object Boolean extends Type('Z', "boolean", "Boolean") |
| 8 | + case object Byte extends Type('B', "byte", "Byte") |
| 9 | + case object Char extends Type('C', "char", "Character") |
| 10 | + case object Short extends Type('S', "short", "Short") |
| 11 | + case object Int extends Type('I', "int", "Integer") |
| 12 | + case object Float extends Type('F', "float", "Float") |
| 13 | + case object Double extends Type('D', "double", "Double") |
| 14 | + case object Long extends Type('J', "long", "Long") |
| 15 | + case object Void extends Type('V', "void", "Void") |
| 16 | + case object Object extends Type('L', "Object", "Object") |
| 17 | +} |
| 18 | + |
| 19 | +object CodeGen { |
| 20 | + private val initName = "$init$" |
| 21 | + private val function1ImplClass = "scala.Function1$class" |
| 22 | + private val copyright = """ |
| 23 | +/* |
| 24 | + * Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com> |
| 25 | + */""".trim |
| 26 | + |
| 27 | + private def f0Header = s""" |
| 28 | +$copyright |
| 29 | + |
| 30 | +package scala.runtime; |
| 31 | + |
| 32 | +@FunctionalInterface |
| 33 | +public interface F0<R> extends scala.Function0<R> { |
| 34 | + default void $initName() { |
| 35 | + }; |
| 36 | +""" |
| 37 | + private def f1Header = s""" |
| 38 | +$copyright |
| 39 | + |
| 40 | +package scala.runtime; |
| 41 | + |
| 42 | +@FunctionalInterface |
| 43 | +public interface F1<T1, R> extends scala.Function1<T1, R> { |
| 44 | + default void $initName() { |
| 45 | + }; |
| 46 | + |
| 47 | + @Override |
| 48 | + default <A> scala.Function1<T1, A> andThen(scala.Function1<R, A> g) { |
| 49 | + return $function1ImplClass.andThen(this, g); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + default <A> scala.Function1<A, R> compose(scala.Function1<A, T1> g) { |
| 54 | + return $function1ImplClass.compose(this, g); |
| 55 | + } |
| 56 | + |
| 57 | +""" |
| 58 | + |
| 59 | + private def f2Header = fNHeader(2) |
| 60 | + |
| 61 | + def fNHeader(n: Int) = { |
| 62 | + require(n > 1, n) |
| 63 | + val tparams = (1 to n).map("T" + _).mkString(", ") |
| 64 | + val curriedReturn = (1 to n).reverse.foldLeft("R")((x, y) => s"scala.Function1<T$y, $x>") |
| 65 | + val tupledReturn = s"scala.Function1<scala.Tuple${n}<$tparams>, R>" |
| 66 | + val implClass = s"scala.Function$n" + "$class" |
| 67 | +s""" |
| 68 | +$copyright |
| 69 | + |
| 70 | +package scala.runtime; |
| 71 | + |
| 72 | +@FunctionalInterface |
| 73 | +public interface F$n<$tparams, R> extends scala.Function$n<$tparams, R> { |
| 74 | + default void $initName() { |
| 75 | + }; |
| 76 | + |
| 77 | + default $curriedReturn curried() { |
| 78 | + return $implClass.curried(this); |
| 79 | + } |
| 80 | + |
| 81 | + default $tupledReturn tupled() { |
| 82 | + return $implClass.tupled(this); |
| 83 | + } |
| 84 | + |
| 85 | +""" |
| 86 | +} |
| 87 | + |
| 88 | + private def apply0MethodSpec(r: Type): String = { |
| 89 | + val name = "apply$mc" + s"${r.code}" + "$sp" |
| 90 | + val applyCall = s"apply();" |
| 91 | + def body = if (r == Type.Void) applyCall else s"return (${r.ref}) $applyCall" |
| 92 | + |
| 93 | +s""" |
| 94 | +default ${r.prim} $name() { |
| 95 | + $body |
| 96 | +} |
| 97 | +""".trim |
| 98 | + } |
| 99 | + |
| 100 | + private def apply0SpecMethods = { |
| 101 | + val rs = List(Type.Void, Type.Byte, Type.Short, Type.Int, Type.Long, Type.Char, Type.Float, Type.Double, Type.Boolean) |
| 102 | + val methods = for (r <- rs) yield apply0MethodSpec(r) |
| 103 | + methods.map(indent).mkString("\n\n") |
| 104 | + } |
| 105 | + |
| 106 | + private def apply1MethodSpec(t1: Type, r: Type): String = { |
| 107 | + val name = "apply$mc" + s"${r.code}${t1.code}" + "$sp" |
| 108 | + val applyCall = s"apply((T1) ((${t1.ref}) v1));" |
| 109 | + def body = if (r == Type.Void) applyCall else s"return (${r.ref}) $applyCall" |
| 110 | + |
| 111 | +s""" |
| 112 | +default ${r.prim} $name(${t1.prim} v1) { |
| 113 | + $body |
| 114 | +} |
| 115 | +""".trim |
| 116 | + } |
| 117 | + |
| 118 | + private def apply1SpecMethods = { |
| 119 | + val ts = List(Type.Int, Type.Long, Type.Float, Type.Double) |
| 120 | + val rs = List(Type.Void, Type.Boolean, Type.Int, Type.Float, Type.Long, Type.Double) |
| 121 | + val methods = for (t1 <- ts; r <- rs) yield apply1MethodSpec(t1, r) |
| 122 | + methods.map(indent).mkString("\n\n") |
| 123 | + } |
| 124 | + |
| 125 | + private def apply2MethodSpec(t1: Type, t2: Type, r: Type): String = { |
| 126 | + val name = "apply$mc" + s"${r.code}${t1.code}${t2.code}" + "$sp" |
| 127 | + val applyCall = s"apply((T1) ((${t1.ref}) v1), (T2) ((${t2.ref}) v2));" |
| 128 | + def body = if (r == Type.Void) applyCall else s"return (${r.ref}) $applyCall" |
| 129 | + |
| 130 | +s""" |
| 131 | +default ${r.prim} $name(${t1.prim} v1, ${t2.prim} v2) { |
| 132 | + $body |
| 133 | +} |
| 134 | +""".trim |
| 135 | + } |
| 136 | + |
| 137 | + private def apply2SpecMethods = { |
| 138 | + val ts = List(Type.Int, Type.Long, Type.Double) |
| 139 | + val rs = List(Type.Void, Type.Boolean, Type.Int, Type.Float, Type.Long, Type.Double) |
| 140 | + val methods = for (t1 <- ts; t2 <- ts; r <- rs) yield apply2MethodSpec(t1, t2, r) |
| 141 | + methods.map(indent).mkString("\n\n") |
| 142 | + } |
| 143 | + |
| 144 | + def f0 = f0Header + apply0SpecMethods + "}\n" |
| 145 | + |
| 146 | + def f1 = f1Header + apply1SpecMethods + "}\n" |
| 147 | + |
| 148 | + def f2 = f2Header + apply2SpecMethods + "}\n" |
| 149 | + |
| 150 | + def fN(arity: Int) = arity match { |
| 151 | + case 0 => f0 |
| 152 | + case 1 => f1 |
| 153 | + case 2 => f2 |
| 154 | + case x => fNHeader(arity) + "\n}\n" |
| 155 | + } |
| 156 | + |
| 157 | + def pN(arity: Int) = { |
| 158 | + def csv(f: Int => String): String = |
| 159 | + (1 to arity).map(f).mkString(", ") |
| 160 | + val tparams = (1 to arity).map("T" + _).mkString(", ") |
| 161 | + val vparams = (1 to arity).map(n => s"T$n t$n").mkString(", ") |
| 162 | + val vparamRefs = (1 to arity).map(n => s"t$n").mkString(", ") |
| 163 | + val parent = "F" + arity |
| 164 | +s""" |
| 165 | +$copyright |
| 166 | + |
| 167 | +package scala.runtime; |
| 168 | + |
| 169 | +import scala.runtime.BoxedUnit; |
| 170 | + |
| 171 | +@FunctionalInterface |
| 172 | +public interface P${arity}<${tparams}> extends ${parent}<$tparams, BoxedUnit> { |
| 173 | + default void $initName() { |
| 174 | + } |
| 175 | + |
| 176 | + void applyVoid($vparams); |
| 177 | + default BoxedUnit apply($vparams) { |
| 178 | + applyVoid($vparamRefs); |
| 179 | + return BoxedUnit.UNIT; |
| 180 | + } |
| 181 | +} |
| 182 | +""" |
| 183 | +} |
| 184 | + |
| 185 | + def factory = { |
| 186 | + def factory0(n: Int) = { |
| 187 | + val tparams = (1 to n).map("T" + _).mkString(", ") |
| 188 | +s""" |
| 189 | +public static <$tparams, R> scala.Function$n<$tparams, R> func(F$n<$tparams, R> f) { return f; } |
| 190 | +public static <$tparams> scala.Function$n<$tparams, BoxedUnit> proc(P$n<$tparams> p) { return p; } |
| 191 | +""".trim |
| 192 | + } |
| 193 | + val ms = (1 to 22).map(factory0).mkString("\n") |
| 194 | + |
| 195 | +s""" |
| 196 | +$copyright |
| 197 | + |
| 198 | +package scala.runtime; |
| 199 | + |
| 200 | +import scala.runtime.BoxedUnit; |
| 201 | + |
| 202 | +public final class F { |
| 203 | + private F() {} |
| 204 | + public static <R> scala.Function0<R> f0(F0<R> f) { return f; } |
| 205 | +${indent(ms)} |
| 206 | +} |
| 207 | + |
| 208 | +""" |
| 209 | + } |
| 210 | + |
| 211 | + def accept(n: Int): String = { |
| 212 | + val targs = (1 to n).map(_ => "String").mkString(", ") |
| 213 | + val vargs = (1 to n).map("\"" + _ + "\"").mkString(", ") |
| 214 | +s""" |
| 215 | +static <T> T acceptFunction$n(scala.Function$n<$targs, T> f) { |
| 216 | + return f.apply($vargs); |
| 217 | +} |
| 218 | +static void acceptFunction${n}Unit(scala.Function$n<$targs, scala.runtime.BoxedUnit> f) { |
| 219 | + f.apply($vargs); |
| 220 | +} |
| 221 | +""" |
| 222 | + } |
| 223 | + |
| 224 | + def testApi = { |
| 225 | +s""" |
| 226 | +$copyright |
| 227 | + |
| 228 | +package scala.runtime.test; |
| 229 | + |
| 230 | +final class TestAPI { |
| 231 | +${(1 to 22).map(accept).map(indent).mkString("\n\n")} |
| 232 | +} |
| 233 | +""" |
| 234 | + } |
| 235 | + |
| 236 | + def indent(s: String) = s.linesIterator.map(" " + _).mkString("\n") |
| 237 | +} |
| 238 | + |
0 commit comments