use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class CaseClassMetadata method createUnapplyAnyMethod.
private CodeMethod createUnapplyAnyMethod() {
// static final func unapply<TypeParams...>(value: any) -> (T...)?
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = NullableType.apply(this.getUnapplyReturnType());
final CodeMethod unapply = new CodeMethod(this.theClass, Names.unapply, type, attributes);
unapply.setPosition(position);
unapply.getTypeParameters().addAll(this.theClass.getTypeParameters());
final CodeParameter parameter = new CodeParameter(unapply, position, Names.value, Types.NULLABLE_ANY);
unapply.getParameters().add(parameter);
// = (param is This) ? unapply(param as This) : null
final InstanceOfOperator isOperator = new InstanceOfOperator(new FieldAccess(parameter), this.theClass.getClassType());
final CastOperator castOperator = new CastOperator(new FieldAccess(parameter), this.theClass.getThisType());
final IValue call = new MethodCall(position, null, Names.unapply, new ArgumentList(castOperator));
final IfStatement ifStatement = new IfStatement(isOperator, call, new NullValue());
unapply.setValue(ifStatement);
return unapply;
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class EnumClassMetadata method updateConstructor.
private void updateConstructor(IConstructor constructor) {
constructor.getAttributes().addFlag(Modifiers.PRIVATE_PROTECTED);
// Prepend parameters and set super initializer
final CodeParameter nameParam = new CodeParameter(constructor, null, Names.name, new ImplicitNullableType(Types.STRING), AttributeList.of(Modifiers.SYNTHETIC));
final CodeParameter ordParam = new CodeParameter(constructor, null, Names.ordinal, Types.INT, AttributeList.of(Modifiers.SYNTHETIC));
constructor.getParameters().insert(0, nameParam);
constructor.getParameters().insert(1, ordParam);
final IConstructor enumConstructor = Types.ENUM_CLASS.getBody().getConstructor(0);
final InitializerCall initializer = constructor.getInitializer();
if (initializer == null) {
final ArgumentList arguments = new ArgumentList(new FieldAccess(nameParam), new FieldAccess(ordParam));
final SourcePosition position = constructor.position();
final InitializerCall init = new InitializerCall(position, true, arguments, this.theClass.getSuperType(), enumConstructor);
constructor.setInitializer(init);
return;
}
// Prepend access to new parameters
final ArgumentList arguments = initializer.getArguments();
arguments.insert(0, new FieldAccess(nameParam));
arguments.insert(1, new FieldAccess(ordParam));
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class EnumClassMetadata method createFromOrdMethod.
private CodeMethod createFromOrdMethod() {
// @BytecodeName("valueOf")
// public static func from(ordinal: int) -> EnumType
final SourcePosition position = this.theClass.position();
final IType type = this.theClass.getClassType();
final CodeMethod method = new CodeMethod(this.theClass, Name.fromRaw("from"), type, AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC));
method.setPosition(position);
method.setInternalName("valueOf");
final CodeParameter parameter = new CodeParameter(Name.fromRaw("ordinal"), Types.INT);
method.getParameters().add(parameter);
// = $VALUES[ordinal]
final FieldAccess valuesField = new FieldAccess(position, null, Names.$VALUES);
final SubscriptAccess getCall = new SubscriptAccess(position, valuesField, new ArgumentList(new FieldAccess(parameter)));
method.setValue(getCall);
return method;
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class EnumClassMetadata method createValuesField.
private Field createValuesField() {
final SourcePosition position = this.theClass.position();
final ArrayType type = new ArrayType(this.theClass.getClassType(), Mutability.IMMUTABLE);
final AttributeList attributes = AttributeList.of(Modifiers.PRIVATE | Modifiers.CONST | Modifiers.SYNTHETIC);
final Field field = new Field(this.theClass, position, Names.$VALUES, type, attributes);
field.setValue(this.createValuesArray());
return field;
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class EnumClassMetadata method createValuesMethod.
private CodeMethod createValuesMethod() {
// public static func values() -> [final EnumType]
final SourcePosition position = this.theClass.position();
final ArrayType type = new ArrayType(this.theClass.getClassType());
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC);
final CodeMethod method = new CodeMethod(this.theClass, Names.values, type, attributes);
method.setPosition(position);
// = $VALUES.copy()
final FieldAccess valuesField = new FieldAccess(position, null, Names.$VALUES);
final MethodCall cloneCall = new MethodCall(position, valuesField, Name.fromRaw("copy"), ArgumentList.EMPTY);
method.setValue(cloneCall);
return method;
}
Aggregations