Skip to content

Commit

Permalink
Simplify generic Class<> usage (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Gene Gleyzer <gene@xqiz.it>
  • Loading branch information
ggleyzer and Gene Gleyzer committed Mar 11, 2024
1 parent 0478a4d commit 732aeb2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
22 changes: 22 additions & 0 deletions javatools/src/main/java/org/xvm/asm/ClassStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -4255,6 +4255,28 @@ else if (cFormal > cActual)
m_listActual = listActual = new ArrayList<>(listActual); // clone
List<Map.Entry<StringConstant, TypeConstant>> entries = getTypeParamsAsList();

if (id.equals(pool.clzClass()) && cActual > 0)
{
TypeConstant typePublic = listActual.get(0);
if (typePublic.isSingleUnderlyingClass(false) && !typePublic.isFormalType())
{
// we know a bit more about the relationship between Class formal types;
// prime them accordingly
for (int i = cActual; i < cFormal; i++)
{
listActual.add(pool.ensureAccessTypeConstant(typePublic,
switch (i)
{
case 1 -> Access.PROTECTED;
case 2 -> Access.PRIVATE;
case 3 -> Access.STRUCT;
default -> throw new IllegalStateException();
}));
}
return;
}
}

// fill the missing actual parameters with the canonical constraint types
// Note: since there is a possibility of Tuple self-reference
// (e.g. Tuple<ElementTypes extends Tuple<ElementTypes...>>)
Expand Down
2 changes: 1 addition & 1 deletion javatools/src/main/java/org/xvm/asm/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -3417,7 +3417,7 @@ protected void registerConstants(ConstantPool pool)
TypeConstant type = entry.getValue();

mapNew.put((StringConstant) pool.register(constName),
(TypeConstant) (type == null ? null : pool.register(type)));
(TypeConstant) (type == null ? null : pool.register(type)));
}
m_mapParams = mapNew;
}
Expand Down
6 changes: 4 additions & 2 deletions javatools/src/main/java/org/xvm/asm/ConstantPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,7 @@ public ByteConstant ensureNibbleConstant(int n)
public ClassConstant clzOuter() {ClassConstant c = m_clzOuter; if (c == null) {m_clzOuter = c = (ClassConstant) getImplicitlyImportedIdentity("Outer" );} return c;}
public ClassConstant clzRef() {ClassConstant c = m_clzRef; if (c == null) {m_clzRef = c = (ClassConstant) getImplicitlyImportedIdentity("Ref" );} return c;}
public ClassConstant clzVar() {ClassConstant c = m_clzVar; if (c == null) {m_clzVar = c = (ClassConstant) getImplicitlyImportedIdentity("Var" );} return c;}
public ClassConstant clzClass() {ClassConstant c = m_clzClass; if (c == null) {m_clzClass = c = (ClassConstant) getImplicitlyImportedIdentity("Class" );} return c;}
public ClassConstant clzStruct() {ClassConstant c = m_clzStruct; if (c == null) {m_clzStruct = c = (ClassConstant) getImplicitlyImportedIdentity("Struct" );} return c;}
public ClassConstant clzType() {ClassConstant c = m_clzType; if (c == null) {m_clzType = c = (ClassConstant) getImplicitlyImportedIdentity("Type" );} return c;}
public ClassConstant clzConst() {ClassConstant c = m_clzConst; if (c == null) {m_clzConst = c = (ClassConstant) getImplicitlyImportedIdentity("Const" );} return c;}
Expand Down Expand Up @@ -2463,7 +2464,6 @@ public ByteConstant ensureNibbleConstant(int n)

// ---- internal class helpers -----------------------------------------------------------------

protected ClassConstant clzClass() {return (ClassConstant) getImplicitlyImportedIdentity("Class" );}
protected ClassConstant clzParameter() {return (ClassConstant) getImplicitlyImportedIdentity("Parameter" );}
protected ClassConstant clzBoolean() {return (ClassConstant) getImplicitlyImportedIdentity("Boolean" );}
protected ClassConstant clzFalse() {return (ClassConstant) getImplicitlyImportedIdentity("False" );}
Expand Down Expand Up @@ -3905,8 +3905,9 @@ public static Auto withPool(ConstantPool pool) {
private transient ClassConstant m_clzOuter;
private transient ClassConstant m_clzRef;
private transient ClassConstant m_clzVar;
private transient ClassConstant m_clzType;
private transient ClassConstant m_clzClass;
private transient ClassConstant m_clzStruct;
private transient ClassConstant m_clzType;
private transient ClassConstant m_clzConst;
private transient ClassConstant m_clzService;
private transient ClassConstant m_clzModule;
Expand Down Expand Up @@ -4051,6 +4052,7 @@ private void optimize()
m_clzOuter = null;
m_clzRef = null;
m_clzVar = null;
m_clzClass = null;
m_clzStruct = null;
m_clzType = null;
m_clzCloseable = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ public TypeConstant adoptParameters(ConstantPool pool, TypeConstant[] atypeParam
}
}
}
return m_constType.adoptParameters(pool, atypeParams); }
return m_constType.adoptParameters(pool, atypeParams);
}

@Override
public TypeConstant[] collectGenericParameters()
Expand Down
3 changes: 2 additions & 1 deletion javatools/src/main/java/org/xvm/compiler/ast/AstNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,8 @@ protected MethodConstant findMethod(Context ctx, TypeConstant typeTarget, TypeIn

if (setMethods.isEmpty())
{
if (typeTarget.isSingleDefiningConstant() && typeTarget.getAccess() != Access.PRIVATE)
if (!typeTarget.isFormalType() && typeTarget.isSingleDefiningConstant() &&
typeTarget.getAccess() != Access.PRIVATE)
{
// check if there are any potentially matching private methods
TypeConstant typePrivate = typeTarget.ensureAccess(Access.PRIVATE);
Expand Down
2 changes: 1 addition & 1 deletion manualTests/src/main/x/reflect.x
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ module TestReflection {
for (val constructor : t.constructors) {
switch (constructor.params.size) {
case 1:
Class<public Point, protected Point, private Point, struct Point> clz = Point;
Class<Point> clz = Point;
assert (struct Point) structure := clz.allocate();
structure.x = Int:1;
structure.y = Int:2;
Expand Down
8 changes: 3 additions & 5 deletions tck/src/main/x/tck/constructors/Reflect.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Reflect {
static class Test(Int a1, String a2);

try {
Class<public Test, protected Test, private Test, struct Test> clz = Test;
Class<Test> clz = Test;
assert (struct Test) structure := clz.allocate();
structure.a1 = 4;
// structure.a2 assignment is missing
Expand All @@ -35,8 +35,7 @@ class Reflect {

try {
Class<Test> clz = Test;
assert Struct structure := clz.allocate();
assert structure.is(struct Test);
assert (struct Test) structure := clz.allocate();
structure.a1 = 4;
structure.a2 = "hello";
Test t = clz.instantiate(structure);
Expand Down Expand Up @@ -64,8 +63,7 @@ class Reflect {
}

Class<Derived> clz = Derived;
assert Struct structure := clz.allocate();
assert structure.is(struct Derived);
assert (struct Derived) structure := clz.allocate();
structure.s = "Test";
Derived d = clz.instantiate(structure);
assert d.s == "Test-DA-BA";
Expand Down

0 comments on commit 732aeb2

Please sign in to comment.