Skip to content

Commit ed8a84a

Browse files
committed
Remove default methods from the implementation of PartialMethod. These are now implemented in VTable.
1 parent 8ca44a4 commit ed8a84a

File tree

3 files changed

+34
-89
lines changed

3 files changed

+34
-89
lines changed

espresso/src/com.oracle.truffle.espresso.shared/src/com/oracle/truffle/espresso/shared/vtable/PartialMethod.java

-79
Original file line numberDiff line numberDiff line change
@@ -51,85 +51,6 @@ public interface PartialMethod<C extends TypeAccess<C, M, F>, M extends MethodAc
5151
*/
5252
boolean isClassInitializer();
5353

54-
/**
55-
* @return Whether this method appears in a method table.
56-
*/
57-
default boolean isVirtualEntry() {
58-
return !isPrivate() && !isStatic() && !isConstructor() && !isClassInitializer();
59-
}
60-
61-
/**
62-
* Returns whether the current considered {@link PartialMethod method} overrides the method
63-
* {@code parentMethod}.
64-
* <p>
65-
* This check should be done in accordance to the first two cases of the definition of method
66-
* overriding of the JVM Specification (jvms-5.4.5).
67-
* <p>
68-
* In practice, this method simply needs to check whether the declaring class of {@code this}
69-
* can access {@code parentMethod}.
70-
* <p>
71-
* In particular, implementations does not need to check whether there exists a method
72-
* in-between the two methods considered, for which {@code this} can override.
73-
* <p>
74-
* The callers must ensure both {@code this} method and {@code parentMethod}:
75-
* <ul>
76-
* <li>{@link #isVirtualEntry() Appears in method tables}</li>
77-
* <li>Have the same {@link #getSymbolicName() name} and {@link #getSymbolicSignature()
78-
* signature}.</li>
79-
* </ul>
80-
*
81-
* @param declaredType The {@link PartialType} provided to the virtual table builder. Should
82-
* represent the declaring type of this {@link PartialMethod}.
83-
*
84-
* @implNote the {@link ModifiersProvider#isFinalFlagSet() final flag} is irrelevant to the
85-
* definition, and will be checked separately to appropriately throw
86-
* {@link MethodTableException} with the
87-
* {@link MethodTableException.Kind#IllegalClassChangeError} kind set.
88-
*/
89-
default boolean canOverride(PartialType<C, M, F> declaredType, M parentMethod) {
90-
assert isVirtualEntry() && parentMethod.isVirtualEntry();
91-
assert getSymbolicName() == parentMethod.getSymbolicName() && getSymbolicSignature() == parentMethod.getSymbolicSignature();
92-
if (parentMethod.isPublic() || parentMethod.isProtected()) {
93-
return true;
94-
}
95-
return declaredType.sameRuntimePackage(parentMethod.getDeclaringClass());
96-
}
97-
98-
/**
99-
* Returns whether {@link PartialMethod this method} and the given {@code parentMethod} obey the
100-
* same access checks rules.
101-
* <p>
102-
* This method is used to determine whether it is necessary to allocate a new vtable slot for a
103-
* declared method that overrides a method from the super's vtable.
104-
* <p>
105-
* Implementing this method is not necessary if using
106-
* {@link VTable#create(PartialType, boolean, boolean)} with {@code verbose} set to
107-
* {@code true}.
108-
* <p>
109-
* This method should return {@code true} if:
110-
* <ul>
111-
* <li>Both methods are either {@link ModifiersProvider#isPublic() public} or
112-
* {@link ModifiersProvider#isProtected()} protected}</li>
113-
* <li>Both methods are {@link ModifiersProvider#isPackagePrivate()} package-private} and the
114-
* declaring classes of both method are in the same runtime package.</li>
115-
* </ul>
116-
* And returns {@code false} otherwise.
117-
* <p>
118-
* Note that the callers must ensure both methods are not private.
119-
*
120-
* @param declaredType The {@link PartialType} provided to the virtual table builder. Should
121-
* represent the declaring type of this {@link PartialMethod}.
122-
*/
123-
default boolean sameOverrideAccess(PartialType<C, M, F> declaredType, M parentMethod) {
124-
assert isVirtualEntry() && parentMethod.isVirtualEntry();
125-
assert getSymbolicName() == parentMethod.getSymbolicName() && getSymbolicSignature() == parentMethod.getSymbolicSignature();
126-
if (isPublic() || isProtected()) {
127-
return parentMethod.isPublic() || parentMethod.isProtected();
128-
}
129-
assert isPackagePrivate();
130-
return parentMethod.isPackagePrivate() && declaredType.sameRuntimePackage(parentMethod.getDeclaringClass());
131-
}
132-
13354
/**
13455
* This method is not used as part of the vtable creation process, and is provided simply for
13556
* simplifying the translation from {@link PartialMethod} to {@link MethodAccess} once the

espresso/src/com.oracle.truffle.espresso.shared/src/com/oracle/truffle/espresso/shared/vtable/VTable.java

+33-9
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public final class VTable {
6262
*
6363
* @param targetClass The type for which method tables should be created
6464
* @param verbose Whether all declared methods should be unconditionally added to the vtable.
65-
* See {@link PartialMethod#sameOverrideAccess(PartialType, MethodAccess)} for more
66-
* details.
6765
* @param allowInterfaceResolvingToPrivate Whether the runtime allows selection of interface
6866
* invokes to select private methods. Requires implementing
6967
* {@link PartialType#lookupOverrideWithPrivate(Symbol, Symbol)}.
@@ -79,6 +77,13 @@ public static <C extends TypeAccess<C, M, F>, M extends MethodAccess<C, M, F>, F
7977
return new Builder<>(targetClass, verbose, allowInterfaceResolvingToPrivate).build();
8078
}
8179

80+
/**
81+
* Returns whether a given method may appear in a vtable.
82+
*/
83+
public static <C extends TypeAccess<C, M, F>, M extends MethodAccess<C, M, F>, F extends FieldAccess<C, M, F>> boolean isVirtualEntry(PartialMethod<C, M, F> m) {
84+
return !m.isPrivate() && !m.isStatic() && !m.isConstructor() && !m.isClassInitializer();
85+
}
86+
8287
private static final class Builder<C extends TypeAccess<C, M, F>, M extends MethodAccess<C, M, F>, F extends FieldAccess<C, M, F>> {
8388
private final boolean verbose;
8489
private final boolean allowInterfaceResolvingToPrivate;
@@ -124,7 +129,7 @@ private void buildLocations() {
124129

125130
private void assignCandidateTargets() {
126131
for (PartialMethod<C, M, F> impl : targetClass.getDeclaredMethodsList()) {
127-
if (!impl.isVirtualEntry()) {
132+
if (!isVirtualEntry(impl)) {
128133
continue;
129134
}
130135
MethodKey k = MethodKey.of(impl);
@@ -159,7 +164,7 @@ private void resolveVirtual() throws MethodTableException {
159164
MethodTableException.Kind.IllegalClassChangeError);
160165
}
161166
vtable.add(target);
162-
if (!target.sameOverrideAccess(targetClass, m)) {
167+
if (!sameOverrideAccess(target, m)) {
163168
currentLocations.markForPopulation();
164169
}
165170
} else {
@@ -169,7 +174,7 @@ private void resolveVirtual() throws MethodTableException {
169174
}
170175
assert vtable.size() == parentTable.size();
171176
for (PartialMethod<C, M, F> impl : targetClass.getDeclaredMethodsList()) {
172-
if (!impl.isVirtualEntry()) {
177+
if (!isVirtualEntry(impl)) {
173178
continue;
174179
}
175180
if (verbose) {
@@ -191,7 +196,7 @@ private void resolveInterfaces() {
191196
List<PartialMethod<C, M, F>> table = new ArrayList<>(cursor.getValue().size());
192197

193198
for (M m : parentTable) {
194-
if (!m.isVirtualEntry()) {
199+
if (!isVirtualEntry(m)) {
195200
// This should ideally not happen, but we must respect the decisions
196201
// previously made for the tables of our super-interfaces.
197202
table.add(m);
@@ -208,7 +213,7 @@ private void resolveInterfaces() {
208213
private void registerFromTable(List<M> table, LocationKind kind) {
209214
int index = 0;
210215
for (M m : table) {
211-
if (!m.isVirtualEntry()) {
216+
if (!isVirtualEntry(m)) {
212217
continue;
213218
}
214219
MethodKey k = MethodKey.of(m);
@@ -222,21 +227,40 @@ private void registerFromTable(List<M> table, LocationKind kind) {
222227
}
223228

224229
private boolean canOverride(PartialMethod<C, M, F> candidate, M parentMethod, int vtableIndex) {
225-
if (candidate.canOverride(targetClass, parentMethod)) {
230+
if (canOverride(candidate, parentMethod)) {
226231
return true;
227232
}
228233
C parentClass = parentMethod.getDeclaringClass().getSuperClass();
229234
M currentMethod;
230235
while (parentClass != null &&
231236
(currentMethod = parentClass.lookupVTableEntry(vtableIndex)) != null) {
232-
if (candidate.canOverride(targetClass, currentMethod)) {
237+
if (canOverride(candidate, currentMethod)) {
233238
return true;
234239
}
235240
parentClass = parentClass.getSuperClass();
236241
}
237242
return false;
238243
}
239244

245+
private boolean canOverride(PartialMethod<C, M, F> candidate, M parentMethod) {
246+
assert isVirtualEntry(candidate) && isVirtualEntry(parentMethod);
247+
assert candidate.getSymbolicName() == parentMethod.getSymbolicName() && candidate.getSymbolicSignature() == parentMethod.getSymbolicSignature();
248+
if (parentMethod.isPublic() || parentMethod.isProtected()) {
249+
return true;
250+
}
251+
return targetClass.sameRuntimePackage(parentMethod.getDeclaringClass());
252+
}
253+
254+
private boolean sameOverrideAccess(PartialMethod<C, M, F> candidate, M parentMethod) {
255+
assert isVirtualEntry(candidate) && isVirtualEntry(parentMethod);
256+
assert candidate.getSymbolicName() == parentMethod.getSymbolicName() && candidate.getSymbolicSignature() == parentMethod.getSymbolicSignature();
257+
if (candidate.isPublic() || candidate.isProtected()) {
258+
return parentMethod.isPublic() || parentMethod.isProtected();
259+
}
260+
assert candidate.isPackagePrivate();
261+
return parentMethod.isPackagePrivate() && targetClass.sameRuntimePackage(parentMethod.getDeclaringClass());
262+
}
263+
240264
private record MethodKey(Symbol<Name> name, Symbol<Signature> signature) {
241265
static <C extends TypeAccess<C, M, F>, M extends MethodAccess<C, M, F>, F extends FieldAccess<C, M, F>> MethodKey of(PartialMethod<C, M, F> method) {
242266
return new MethodKey(method.getSymbolicName(), method.getSymbolicSignature());

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/impl/EspressoMethodTableBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Method.MethodVersion[] getMirandas() {
144144
private static List<PartialMethod<Klass, Method, Field>> filterInterfaceMethods(Method.MethodVersion[] declaredMethods) {
145145
List<PartialMethod<Klass, Method, Field>> table = new ArrayList<>(declaredMethods.length);
146146
for (Method.MethodVersion m : declaredMethods) {
147-
if (m.getMethod().isVirtualEntry()) {
147+
if (VTable.isVirtualEntry(m.getMethod())) {
148148
table.add(m.getMethod());
149149
}
150150
}

0 commit comments

Comments
 (0)