Skip to content

Commit 201012b

Browse files
committed
More cleanup in CodeGen
1 parent 113c587 commit 201012b

File tree

1 file changed

+170
-160
lines changed

1 file changed

+170
-160
lines changed

project/CodeGen.scala

Lines changed: 170 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -17,84 +17,144 @@ object Type {
1717
}
1818

1919
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);
20+
case class arity(n: Int) {
21+
val ns = (1 to n).toList
22+
23+
def csv(f: Int => String): String = ns.map(f).mkString(", ")
24+
25+
val tparams = csv("T" + _)
26+
27+
private def f0Header =
28+
s"""
29+
|$copyright
30+
|
31+
|package scala.runtime;
32+
|
33+
|@FunctionalInterface
34+
|public interface F0<R> extends scala.Function0<R> {
35+
| default void $initName() {
36+
| };
37+
|""".stripMargin
38+
private def f1Header =
39+
s"""
40+
|$copyright
41+
|
42+
|package scala.runtime;
43+
|
44+
|@FunctionalInterface
45+
|public interface F1<T1, R> extends scala.Function1<T1, R> {
46+
| default void $initName() {
47+
| };
48+
|
49+
| @Override
50+
| default <A> scala.Function1<T1, A> andThen(scala.Function1<R, A> g) {
51+
| return $function1ImplClass.andThen(this, g);
52+
| }
53+
|
54+
| @Override
55+
| default <A> scala.Function1<A, R> compose(scala.Function1<A, T1> g) {
56+
| return $function1ImplClass.compose(this, g);
57+
| }
58+
|""".stripMargin
59+
60+
private def fNHeader = {
61+
62+
val curriedReturn = (1 to n).reverse.foldLeft("R")((x, y) => s"scala.Function1<T$y, $x>")
63+
val tupledReturn = s"scala.Function1<scala.Tuple${n}<$tparams>, R>"
64+
val implClass = s"scala.Function$n" + "$class"
65+
s"""
66+
|$copyright
67+
|
68+
|package scala.runtime;
69+
|
70+
|@FunctionalInterface
71+
|public interface F$n<$tparams, R> extends scala.Function$n<$tparams, R> {
72+
| default void $initName() {
73+
| };
74+
|
75+
| default $curriedReturn curried() {
76+
| return $implClass.curried(this);
77+
| }
78+
|
79+
| default $tupledReturn tupled() {
80+
| return $implClass.tupled(this);
81+
| }
82+
|
83+
|""".stripMargin
5084
}
5185

52-
@Override
53-
default <A> scala.Function1<A, R> compose(scala.Function1<A, T1> g) {
54-
return $function1ImplClass.compose(this, g);
86+
def fHeader: String =
87+
n match {
88+
case 0 => f0Header
89+
case 1 => f1Header
90+
case _ => fNHeader
91+
}
92+
93+
def pN: String = {
94+
val vparams = csv(n => s"T$n t$n")
95+
val vparamRefs = csv(n => s"t$n")
96+
val parent = "F" + n
97+
s"""
98+
|$copyright
99+
|
100+
|package scala.runtime;
101+
|
102+
|import scala.runtime.BoxedUnit;
103+
|
104+
|@FunctionalInterface
105+
|public interface P${n}<${tparams}> extends ${parent}<$tparams, BoxedUnit> {
106+
| default void $initName() {
107+
| }
108+
|
109+
| void applyVoid($vparams);
110+
|
111+
| default BoxedUnit apply($vparams) {
112+
| applyVoid($vparamRefs);
113+
| return BoxedUnit.UNIT;
114+
| }
115+
|}
116+
|""".stripMargin
55117
}
56118

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);
119+
def factory: String = {
120+
s"""
121+
|public static <$tparams, R> scala.Function$n<$tparams, R> func(F$n<$tparams, R> f) { return f; }
122+
|public static <$tparams> scala.Function$n<$tparams, BoxedUnit> proc(P$n<$tparams> p) { return p; }
123+
|""".stripMargin.trim
79124
}
80125

81-
default $tupledReturn tupled() {
82-
return $implClass.tupled(this);
126+
def accept: String = {
127+
val targs = csv(_ => "String")
128+
val vargs = csv("\"" + _ + "\"")
129+
s"""
130+
|static <T> T acceptFunction$n(scala.Function$n<$targs, T> f) {
131+
| return f.apply($vargs);
132+
|}
133+
|static void acceptFunction${n}Unit(scala.Function$n<$targs, scala.runtime.BoxedUnit> f) {
134+
| f.apply($vargs);
135+
|}
136+
|""".stripMargin
83137
}
138+
}
84139

85-
"""
86-
}
140+
141+
private val initName = "$init$"
142+
private val function1ImplClass = "scala.Function1$class"
143+
private val copyright =
144+
"""
145+
|/*
146+
| * Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
147+
| */""".stripMargin.trim
87148

88149
private def apply0MethodSpec(r: Type): String = {
89150
val name = "apply$mc" + s"${r.code}" + "$sp"
90151
val applyCall = s"apply();"
91152
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
153+
s"""
154+
|default ${r.prim} $name() {
155+
| $body
156+
|}
157+
|""".stripMargin.trim
98158
}
99159

100160
private def apply0SpecMethods = {
@@ -108,11 +168,11 @@ default ${r.prim} $name() {
108168
val applyCall = s"apply((T1) ((${t1.ref}) v1));"
109169
def body = if (r == Type.Void) applyCall else s"return (${r.ref}) $applyCall"
110170

111-
s"""
112-
default ${r.prim} $name(${t1.prim} v1) {
113-
$body
114-
}
115-
""".trim
171+
s"""
172+
|default ${r.prim} $name(${t1.prim} v1) {
173+
| $body
174+
|}
175+
|""".stripMargin.trim
116176
}
117177

118178
private def apply1SpecMethods = {
@@ -127,11 +187,11 @@ default ${r.prim} $name(${t1.prim} v1) {
127187
val applyCall = s"apply((T1) ((${t1.ref}) v1), (T2) ((${t2.ref}) v2));"
128188
def body = if (r == Type.Void) applyCall else s"return (${r.ref}) $applyCall"
129189

130-
s"""
131-
default ${r.prim} $name(${t1.prim} v1, ${t2.prim} v2) {
132-
$body
133-
}
134-
""".trim
190+
s"""
191+
|default ${r.prim} $name(${t1.prim} v1, ${t2.prim} v2) {
192+
| $body
193+
|}
194+
|""".stripMargin.trim
135195
}
136196

137197
private def apply2SpecMethods = {
@@ -141,99 +201,49 @@ default ${r.prim} $name(${t1.prim} v1, ${t2.prim} v2) {
141201
methods.map(indent).mkString("\n\n")
142202
}
143203

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): String = {
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() {
204+
def fN(n: Int) = {
205+
val header = arity(n).fHeader
206+
val applyMethods = n match {
207+
case 0 => apply0SpecMethods
208+
case 1 => apply1SpecMethods
209+
case 2 => apply2SpecMethods
210+
case x => ""
174211
}
212+
val trailer = "}\n"
213+
List(header, applyMethods, trailer).mkString
214+
}
175215

176-
void applyVoid($vparams);
177-
178-
default BoxedUnit apply($vparams) {
179-
applyVoid($vparamRefs);
180-
return BoxedUnit.UNIT;
181-
}
182-
}
183-
"""
184-
}
216+
def pN(n: Int) = arity(n).pN
185217

186218
def factory: String = {
187-
def factory0(n: Int) = {
188-
val tparams = (1 to n).map("T" + _).mkString(", ")
189-
s"""
190-
public static <$tparams, R> scala.Function$n<$tparams, R> func(F$n<$tparams, R> f) { return f; }
191-
public static <$tparams> scala.Function$n<$tparams, BoxedUnit> proc(P$n<$tparams> p) { return p; }
192-
""".trim
193-
}
194-
val ms = (1 to 22).map(factory0).mkString("\n")
195-
196-
s"""
197-
$copyright
198-
199-
package scala.runtime;
200-
201-
import scala.runtime.BoxedUnit;
202-
203-
public final class F {
204-
private F() {}
205-
public static <R> scala.Function0<R> f0(F0<R> f) { return f; }
206-
${indent(ms)}
207-
}
208-
209-
"""
210-
}
211-
212-
def accept(n: Int): String = {
213-
val targs = (1 to n).map(_ => "String").mkString(", ")
214-
val vargs = (1 to n).map("\"" + _ + "\"").mkString(", ")
215-
s"""
216-
static <T> T acceptFunction$n(scala.Function$n<$targs, T> f) {
217-
return f.apply($vargs);
218-
}
219-
static void acceptFunction${n}Unit(scala.Function$n<$targs, scala.runtime.BoxedUnit> f) {
220-
f.apply($vargs);
221-
}
222-
"""
219+
val ms = (1 to 22).map(n => arity(n).factory).mkString("\n")
220+
s"""
221+
|$copyright
222+
|
223+
|package scala.runtime;
224+
|
225+
|import scala.runtime.BoxedUnit;
226+
|
227+
|public final class F {
228+
| private F() {}
229+
| public static <R> scala.Function0<R> f0(F0<R> f) { return f; }
230+
|${indent(ms)}
231+
|}
232+
|
233+
|""".stripMargin
223234
}
224235

225236
def testApi: String = {
226-
s"""
227-
$copyright
228-
229-
package scala.runtime.test;
230-
231-
final class TestAPI {
232-
${(1 to 22).map(accept).map(indent).mkString("\n\n")}
233-
}
234-
"""
237+
s"""
238+
|$copyright
239+
|
240+
|package scala.runtime.test;
241+
|
242+
|final class TestAPI {
243+
|${(1 to 22).map(n => arity(n).accept).map(indent).mkString("\n\n")}
244+
|}
245+
|""".stripMargin
235246
}
236247

237248
def indent(s: String) = s.linesIterator.map(" " + _).mkString("\n")
238249
}
239-

0 commit comments

Comments
 (0)