Skip to content

Commit 88dc38e

Browse files
authored
merge: Free java (#1271)
2 parents 9adb900 + eda2127 commit 88dc38e

35 files changed

+996
-179
lines changed

cli-impl/src/main/java/org/aya/cli/library/incremental/DiskCompilerAdvisor.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
1+
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
22
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33
package org.aya.cli.library.incremental;
44

@@ -8,7 +8,6 @@
88
import org.aya.cli.library.source.LibrarySource;
99
import org.aya.cli.utils.CompilerUtil;
1010
import org.aya.compiler.CompiledModule;
11-
import org.aya.compiler.free.morphism.SourceFreeJavaBuilder;
1211
import org.aya.compiler.serializers.ModuleSerializer;
1312
import org.aya.compiler.serializers.NameSerializer;
1413
import org.aya.prelude.GeneratedVersion;
@@ -129,8 +128,8 @@ public void addURL(Path url) throws MalformedURLException {
129128
@NotNull ImmutableSeq<TyckDef> defs,
130129
@NotNull ModuleLoader recurseLoader
131130
) throws IOException, ClassNotFoundException {
132-
var javaCode = new ModuleSerializer<String>(resolveInfo.shapeFactory())
133-
.serialize(SourceFreeJavaBuilder.create(), new ModuleSerializer.ModuleResult(
131+
var javaCode = new ModuleSerializer(resolveInfo.shapeFactory())
132+
.serializeWithBestBuilder(new ModuleSerializer.ModuleResult(
134133
QPath.fileLevel(file.moduleName()),
135134
defs.filterIsInstance(TopLevelDef.class)));
136135
var libraryRoot = file.owner().outDir();

jit-compiler/src/main/java/module-info.java

-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
exports org.aya.compiler;
99
exports org.aya.compiler.free;
1010
exports org.aya.compiler.free.data;
11-
exports org.aya.compiler.free.morphism;
1211
exports org.aya.compiler.serializers;
1312
}

jit-compiler/src/main/java/org/aya/compiler/free/ArgumentProvider.java

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.aya.compiler.free.data.LocalVariable;
66
import org.jetbrains.annotations.NotNull;
77

8+
/**
9+
* The implementation should be pure (at least, same input same output, some kind of side effect is acceptable)
10+
*/
811
public interface ArgumentProvider {
912
interface Lambda extends ArgumentProvider {
1013
@NotNull FreeJavaExpr capture(int nth);

jit-compiler/src/main/java/org/aya/compiler/free/Constants.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import kala.collection.immutable.ImmutableTreeSeq;
88
import kala.collection.mutable.MutableSeq;
99
import kala.control.Result;
10-
import org.aya.compiler.free.data.FieldRef;
1110
import org.aya.compiler.free.data.MethodRef;
11+
import org.aya.compiler.free.data.FieldRef;
1212
import org.aya.syntax.compile.JitClass;
1313
import org.aya.syntax.compile.JitCon;
1414
import org.aya.syntax.compile.JitData;
@@ -41,30 +41,30 @@ private Constants() { }
4141
public static final @NotNull ClassDesc CD_Result = FreeUtil.fromClass(Result.class);
4242

4343
// Term -> Term
44-
public static final @NotNull MethodRef CLOSURE = new MethodRef.Default(
44+
public static final @NotNull MethodRef CLOSURE = new MethodRef(
4545
FreeUtil.fromClass(UnaryOperator.class),
4646
"apply",
4747
ConstantDescs.CD_Object, ImmutableSeq.of(ConstantDescs.CD_Object),
4848
true
4949
);
5050

5151
// () -> Term
52-
public static final @NotNull MethodRef THUNK = new MethodRef.Default(
52+
public static final @NotNull MethodRef THUNK = new MethodRef(
5353
FreeUtil.fromClass(Supplier.class),
5454
"get",
5555
ConstantDescs.CD_Object, ImmutableSeq.empty(),
5656
true
5757
);
5858

59-
public static final @NotNull MethodRef FUNCTION = new MethodRef.Default(
59+
public static final @NotNull MethodRef FUNCTION = new MethodRef(
6060
FreeUtil.fromClass(Function.class),
6161
"apply",
6262
ConstantDescs.CD_Object, ImmutableSeq.of(ConstantDescs.CD_Object),
6363
true
6464
);
6565

6666
// ImmutableSeq from(Object[])
67-
public static final @NotNull MethodRef IMMSEQ = new MethodRef.Default(
67+
public static final @NotNull MethodRef IMMSEQ = new MethodRef(
6868
CD_ImmutableSeq,
6969
"from",
7070
CD_ImmutableSeq, ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()),
@@ -74,7 +74,7 @@ private Constants() { }
7474
/**
7575
* @see MutableSeq#fill(int, Object)
7676
*/
77-
public static final @NotNull MethodRef MUTSEQ = new MethodRef.Default(
77+
public static final @NotNull MethodRef MUTSEQ = new MethodRef(
7878
CD_MutableSeq,
7979
"fill",
8080
CD_MutableSeq, ImmutableSeq.of(ConstantDescs.CD_int, ConstantDescs.CD_Object),
@@ -84,7 +84,7 @@ private Constants() { }
8484
/**
8585
* @see MutableSeq#set(int, Object)
8686
*/
87-
public static final @NotNull MethodRef MUTSEQ_SET = new MethodRef.Default(
87+
public static final @NotNull MethodRef MUTSEQ_SET = new MethodRef(
8888
CD_MutableSeq, "set", ConstantDescs.CD_void,
8989
ImmutableSeq.of(ConstantDescs.CD_int, ConstantDescs.CD_Object),
9090
true
@@ -95,7 +95,7 @@ private Constants() { }
9595
*
9696
* @see Seq#get(int)
9797
*/
98-
public static final @NotNull MethodRef SEQ_GET = new MethodRef.Default(
98+
public static final @NotNull MethodRef SEQ_GET = new MethodRef(
9999
CD_Seq, "get", ConstantDescs.CD_Object,
100100
ImmutableSeq.of(ConstantDescs.CD_int),
101101
true
@@ -104,19 +104,19 @@ private Constants() { }
104104
/**
105105
* @see Seq#toImmutableSeq()
106106
*/
107-
public static final @NotNull MethodRef SEQ_TOIMMSEQ = new MethodRef.Default(
107+
public static final @NotNull MethodRef SEQ_TOIMMSEQ = new MethodRef(
108108
CD_Seq, "toImmutableSeq", CD_ImmutableSeq, ImmutableSeq.empty(), true
109109
);
110110

111-
public static final @NotNull MethodRef IMMTREESEQ = new MethodRef.Default(
111+
public static final @NotNull MethodRef IMMTREESEQ = new MethodRef(
112112
FreeUtil.fromClass(ImmutableTreeSeq.class),
113113
"from",
114114
FreeUtil.fromClass(ImmutableTreeSeq.class),
115115
ImmutableSeq.of(ConstantDescs.CD_Object.arrayType()),
116116
false
117117
);
118118

119-
public static final @NotNull MethodRef BETAMAKE = new MethodRef.Default(
119+
public static final @NotNull MethodRef BETAMAKE = new MethodRef(
120120
FreeUtil.fromClass(BetaRedex.class),
121121
"make",
122122
CD_Term, ImmutableSeq.empty(),
@@ -126,14 +126,14 @@ private Constants() { }
126126
/**
127127
* @see Term#elevate(int)
128128
*/
129-
public static final @NotNull MethodRef ELEVATE = new MethodRef.Default(
129+
public static final @NotNull MethodRef ELEVATE = new MethodRef(
130130
CD_Term, "elevate", CD_Term, ImmutableSeq.of(ConstantDescs.CD_int), true
131131
);
132132

133133
/**
134134
* @see RuleReducer#make()
135135
*/
136-
public static final @NotNull MethodRef RULEREDUCER_MAKE = new MethodRef.Default(
136+
public static final @NotNull MethodRef RULEREDUCER_MAKE = new MethodRef(
137137
FreeUtil.fromClass(RuleReducer.class),
138138
"make",
139139
CD_Term, ImmutableSeq.empty(),
@@ -143,7 +143,7 @@ private Constants() { }
143143
/**
144144
* @see Closure#mkConst(Term)
145145
*/
146-
public static final @NotNull MethodRef CLOSURE_MKCONST = new MethodRef.Default(
146+
public static final @NotNull MethodRef CLOSURE_MKCONST = new MethodRef(
147147
FreeUtil.fromClass(Closure.class),
148148
"mkConst",
149149
FreeUtil.fromClass(Closure.class),
@@ -154,15 +154,15 @@ private Constants() { }
154154
/**
155155
* @see Panic#unreachable()
156156
*/
157-
public static final @NotNull MethodRef PANIC = new MethodRef.Default(
157+
public static final @NotNull MethodRef PANIC = new MethodRef(
158158
FreeUtil.fromClass(Panic.class),
159159
"unreachable",
160160
ConstantDescs.CD_Object,
161161
ImmutableSeq.empty(),
162162
true
163163
);
164164

165-
public static final @NotNull MethodRef INT_REPR = new MethodRef.Default(
165+
public static final @NotNull MethodRef INT_REPR = new MethodRef(
166166
FreeUtil.fromClass(IntegerTerm.class),
167167
"repr",
168168
ConstantDescs.CD_int,
@@ -173,7 +173,7 @@ private Constants() { }
173173
/**
174174
* @see ConCallLike#conArgs()
175175
*/
176-
public static final @NotNull MethodRef CONARGS = new MethodRef.Default(
176+
public static final @NotNull MethodRef CONARGS = new MethodRef(
177177
FreeUtil.fromClass(ConCallLike.class),
178178
"conArgs",
179179
CD_ImmutableSeq,
@@ -184,7 +184,7 @@ private Constants() { }
184184
/**
185185
* @see TupTerm#lhs()
186186
*/
187-
public static final @NotNull MethodRef TUP_LHS = new MethodRef.Default(
187+
public static final @NotNull MethodRef TUP_LHS = new MethodRef(
188188
FreeUtil.fromClass(TupTerm.class),
189189
"lhs",
190190
CD_Term,
@@ -195,7 +195,7 @@ private Constants() { }
195195
/**
196196
* @see TupTerm#rhs()
197197
*/
198-
public static final @NotNull MethodRef TUP_RHS = new MethodRef.Default(
198+
public static final @NotNull MethodRef TUP_RHS = new MethodRef(
199199
FreeUtil.fromClass(TupTerm.class),
200200
"rhs",
201201
CD_Term,
@@ -206,13 +206,13 @@ private Constants() { }
206206
/**
207207
* @see Result#ok(Object)
208208
*/
209-
public static final @NotNull MethodRef RESULT_OK = new MethodRef.Default(
209+
public static final @NotNull MethodRef RESULT_OK = new MethodRef(
210210
CD_Result, "ok",
211211
CD_Result, ImmutableSeq.of(ConstantDescs.CD_Object),
212212
true
213213
);
214214

215-
public static final @NotNull MethodRef RESULT_ERR = new MethodRef.Default(
215+
public static final @NotNull MethodRef RESULT_ERR = new MethodRef(
216216
CD_Result, "err",
217217
CD_Result, ImmutableSeq.of(ConstantDescs.CD_Object),
218218
true
@@ -225,13 +225,13 @@ private Constants() { }
225225
ConstantDescs.CD_int, ConstantDescs.CD_boolean.arrayType(), ConstantDescs.CD_String.arrayType()
226226
);
227227

228-
public static final @NotNull FieldRef JITDATA_CONS = new FieldRef.Default(
228+
public static final @NotNull FieldRef JITDATA_CONS = new FieldRef(
229229
FreeUtil.fromClass(JitData.class),
230230
FreeUtil.fromClass(JitCon.class).arrayType(),
231231
"constructors"
232232
);
233233

234-
public static final @NotNull FieldRef JITCLASS_MEMS = new FieldRef.Default(
234+
public static final @NotNull FieldRef JITCLASS_MEMS = new FieldRef(
235235
FreeUtil.fromClass(JitClass.class),
236236
FreeUtil.fromClass(JitMember.class).arrayType(),
237237
"members"
@@ -240,7 +240,7 @@ private Constants() { }
240240
/**
241241
* @see UnaryOperator#identity()
242242
*/
243-
public static final @NotNull MethodRef CLOSURE_ID = new MethodRef.Default(
243+
public static final @NotNull MethodRef CLOSURE_ID = new MethodRef(
244244
FreeUtil.fromClass(UnaryOperator.class),
245245
"identity",
246246
FreeUtil.fromClass(UnaryOperator.class),
@@ -251,7 +251,7 @@ private Constants() { }
251251
/**
252252
* @see PatMatcher#apply(ImmutableSeq, ImmutableSeq)
253253
*/
254-
public static final @NotNull MethodRef PATMATCHER_APPLY = new MethodRef.Default(
254+
public static final @NotNull MethodRef PATMATCHER_APPLY = new MethodRef(
255255
FreeUtil.fromClass(PatMatcher.class), "apply",
256256
CD_Result, ImmutableSeq.of(CD_ImmutableSeq, CD_ImmutableSeq), false
257257
);

jit-compiler/src/main/java/org/aya/compiler/free/FreeClassBuilder.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
public interface FreeClassBuilder {
1818
static @NotNull MethodRef makeConstructorRef(@NotNull ClassDesc owner, @NotNull ImmutableSeq<ClassDesc> parameterTypes) {
19-
return new MethodRef.Default(owner, ConstantDescs.INIT_NAME, ConstantDescs.CD_void, parameterTypes, false);
19+
return new MethodRef(owner, ConstantDescs.INIT_NAME, ConstantDescs.CD_void, parameterTypes, false);
2020
}
2121

22-
void buildMetadata(@NotNull CompiledAya compiledAya);
2322
void buildNestedClass(
2423
@NotNull CompiledAya compiledAya,
2524
@NotNull String name,
@@ -44,6 +43,4 @@ void buildNestedClass(
4443
@NotNull String name,
4544
@NotNull Function<FreeExprBuilder, FreeJavaExpr> initializer
4645
);
47-
48-
@NotNull FreeJavaResolver resolver();
4946
}

jit-compiler/src/main/java/org/aya/compiler/free/FreeCodeBuilder.java

-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import kala.collection.immutable.ImmutableSeq;
66
import kala.collection.immutable.primitive.ImmutableIntSeq;
7-
import org.aya.compiler.free.data.FieldRef;
87
import org.aya.compiler.free.data.LocalVariable;
98
import org.jetbrains.annotations.NotNull;
109
import org.jetbrains.annotations.Nullable;
@@ -27,10 +26,6 @@ default LocalVariable makeVar(@NotNull Class<?> type, @Nullable FreeJavaExpr ini
2726

2827
void updateArray(@NotNull FreeJavaExpr array, int idx, @NotNull FreeJavaExpr update);
2928

30-
void updateField(@NotNull FieldRef field, @NotNull FreeJavaExpr update);
31-
32-
void updateField(@NotNull FieldRef field, @NotNull FreeJavaExpr owner, @NotNull FreeJavaExpr update);
33-
3429
void ifNotTrue(@NotNull LocalVariable notTrue, @NotNull Consumer<FreeCodeBuilder> thenBlock, @Nullable Consumer<FreeCodeBuilder> elseBlock);
3530

3631
void ifTrue(@NotNull LocalVariable theTrue, @NotNull Consumer<FreeCodeBuilder> thenBlock, @Nullable Consumer<FreeCodeBuilder> elseBlock);

jit-compiler/src/main/java/org/aya/compiler/free/FreeExprBuilder.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
1+
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
22
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33
package org.aya.compiler.free;
44

@@ -7,6 +7,7 @@
77
import org.aya.compiler.free.data.LocalVariable;
88
import org.aya.compiler.free.data.MethodRef;
99
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
1011

1112
import java.lang.constant.ClassDesc;
1213
import java.util.Arrays;
@@ -16,8 +17,6 @@
1617
* the result only depends on the {@link FreeCodeBuilder} that this builder derived from
1718
*/
1819
public interface FreeExprBuilder {
19-
@NotNull FreeJavaResolver resolver();
20-
2120
/**
2221
* A {@code new} expression on specified constructor.
2322
*/
@@ -39,7 +38,9 @@ public interface FreeExprBuilder {
3938
return mkNew(conRef, args);
4039
}
4140

42-
@NotNull FreeJavaExpr refVar(@NotNull LocalVariable name);
41+
default @NotNull FreeJavaExpr refVar(@NotNull LocalVariable name) {
42+
return name.ref();
43+
}
4344

4445
/**
4546
* Invoke a (non-interface) method on {@param owner}.
@@ -62,6 +63,12 @@ public interface FreeExprBuilder {
6263
return refEnum(cd, name);
6364
}
6465

66+
/**
67+
* Make a lambda expression
68+
*
69+
* @param builder the builder for building the lambda body, you should use local variable comes from this and the
70+
* {@link ArgumentProvider.Lambda} ONLY, other variables introduced outside of the lambda is unavailable.
71+
*/
6572
@NotNull FreeJavaExpr mkLambda(
6673
@NotNull ImmutableSeq<FreeJavaExpr> captures,
6774
@NotNull MethodRef method,
@@ -85,7 +92,7 @@ public interface FreeExprBuilder {
8592
*/
8693
@NotNull FreeJavaExpr mkArray(
8794
@NotNull ClassDesc type, int length,
88-
@NotNull ImmutableSeq<FreeJavaExpr> initializer
95+
@Nullable ImmutableSeq<FreeJavaExpr> initializer
8996
);
9097

9198
@NotNull FreeJavaExpr getArray(@NotNull FreeJavaExpr array, int index);

0 commit comments

Comments
 (0)