Skip to content

Commit

Permalink
Complete fix on class fields where the type is inferred from the init…
Browse files Browse the repository at this point in the history
…ializer
  • Loading branch information
stanhebben committed Jan 31, 2025
1 parent c5e3a2f commit 4b64cfb
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package org.openzen.zenscript.codemodel.member;

import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.HighLevelDefinition;
import org.openzen.zenscript.codemodel.Modifiers;
import org.openzen.zenscript.codemodel.*;
import org.openzen.zenscript.codemodel.constant.CompileTimeConstant;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.identifiers.FieldSymbol;
import org.openzen.zenscript.codemodel.identifiers.TypeSymbol;
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
import org.openzen.zenscript.codemodel.statement.ReturnStatement;
import org.openzen.zenscript.codemodel.statement.Statement;
import org.openzen.zenscript.codemodel.type.GenericTypeID;
import org.openzen.zenscript.codemodel.type.TypeID;
import org.openzen.zenscript.codemodel.type.member.MemberSet;
Expand All @@ -25,6 +23,7 @@ public class FieldMember extends PropertyMember implements FieldSymbol {
public final GetterMember autoGetter;
public final SetterMember autoSetter;
public Expression initializer;
private final TypeID thisType;

public FieldMember(
CodePosition position,
Expand All @@ -40,6 +39,7 @@ public FieldMember(
this.name = name;
this.autoGetterAccess = autoGetterAccess;
this.autoSetterAccess = autoSetterAccess;
this.thisType = thisType;

// ToDo: This is never used?
TypeID[] parameters = null;
Expand All @@ -57,30 +57,37 @@ public FieldMember(
private SetterMember constructAutoSetter(TypeID thisType, FieldInstance fieldInstance) {
Modifiers autoSetterModifiers = isStatic() ? autoSetterAccess.withStatic() : autoSetterAccess;
final SetterMember autoSetter = new SetterMember(position, definition, autoSetterModifiers, name, type);
final GetFunctionParameterExpression setValue = new GetFunctionParameterExpression(position, autoSetter.parameter);
autoSetter.setBody(constructAutoSetterBody(thisType, fieldInstance, autoSetter.parameter));
return autoSetter;
}

private Statement constructAutoSetterBody(TypeID thisType, FieldInstance fieldInstance, FunctionParameter parameter) {
final GetFunctionParameterExpression setValue = new GetFunctionParameterExpression(position, parameter);
final Expression setFieldExpression;
if (isStatic()) {
setFieldExpression = new SetStaticFieldExpression(position, fieldInstance, setValue);
} else {
setFieldExpression = new SetFieldExpression(position, new ThisExpression(position, thisType), fieldInstance, setValue);
}
autoSetter.setBody(new ExpressionStatement(position, setFieldExpression));
return autoSetter;
return new ExpressionStatement(position, setFieldExpression);
}

private GetterMember constructAutoGetter(TypeID thisType, FieldInstance fieldInstance) {
Modifiers autoGetterModifiers = isStatic() ? autoGetterAccess.withStatic() : autoGetterAccess;
final GetterMember autoGetter = new GetterMember(position, definition, autoGetterModifiers, name, type);
autoGetter.setBody(constructAutoGetterBody(thisType, fieldInstance));
return autoGetter;
}

private Statement constructAutoGetterBody(TypeID thisType, FieldInstance fieldInstance) {
final Expression getFieldExpression;
if(isStatic()) {
getFieldExpression = new GetStaticFieldExpression(position, fieldInstance);
} else {
getFieldExpression = new GetFieldExpression(position, new ThisExpression(position, thisType), fieldInstance);
}

autoGetter.setBody(new ReturnStatement(position, getFieldExpression));
return autoGetter;
return new ReturnStatement(position, getFieldExpression);
}

public boolean hasAutoGetter() {
Expand Down Expand Up @@ -156,10 +163,15 @@ public TypeID getType() {
public void setType(TypeID type) {
super.setType(type);

if (autoGetter != null)
final FieldInstance fieldInstance = new FieldInstance(this, type);
if (autoGetter != null) {
this.autoGetter.setType(type);
if (autoSetter != null)
this.autoGetter.setBody(constructAutoGetterBody(thisType, fieldInstance));
}
if (autoSetter != null) {
this.autoSetter.setType(type);
this.autoSetter.setBody(constructAutoSetterBody(thisType, fieldInstance, autoSetter.parameter));
}
}

@Override
Expand Down

0 comments on commit 4b64cfb

Please sign in to comment.