use of dyvilx.tools.compiler.ast.parameter.IParameter in project Dyvil by Dyvil.
the class CaseClassMetadata method getUnapplyReturnType.
private TupleType getUnapplyReturnType() {
final TupleType returnType = new TupleType();
final TypeList typeArgs = returnType.getArguments();
for (IParameter param : this.theClass.getParameters()) {
typeArgs.add(param.getType());
}
return returnType;
}
use of dyvilx.tools.compiler.ast.parameter.IParameter in project Dyvil by Dyvil.
the class CaseClassMetadata method createUnapplyMethod.
private CodeMethod createUnapplyMethod() {
// static final func unapply<TypeParams...>(value: This) -> (T...)
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = 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, this.theClass.getThisType());
unapply.getParameters().add(parameter);
// = (value.classParams...)
final TupleExpr tupleExpr = new TupleExpr(position);
final ArgumentList arguments = tupleExpr.getValues();
for (IParameter param : this.theClass.getParameters()) {
// value
final FieldAccess thisAccess = new FieldAccess(position, null, parameter);
// value.classParam
final IValue fieldAccess;
if (param.isOverride()) {
// if the class parameter is marked as 'override', we have to resolve it from a super-class
// the easiest way to do this is by name
fieldAccess = new FieldAccess(position, thisAccess, param.getName());
} else {
fieldAccess = new FieldAccess(position, thisAccess, param);
}
arguments.add(fieldAccess);
}
unapply.setValue(tupleExpr);
return unapply;
}
use of dyvilx.tools.compiler.ast.parameter.IParameter in project Dyvil by Dyvil.
the class ClassMetadata method createDefaultConstructor.
private CodeConstructor createDefaultConstructor() {
// init(classParams...)
final SourcePosition position = this.theClass.position();
final AttributeList attributes = this.theClass.getConstructorAttributes();
attributes.addFlag(Modifiers.GENERATED);
final CodeConstructor constructor = new CodeConstructor(this.theClass, attributes);
constructor.setPosition(position);
this.copyClassParameters(constructor);
// : super(superParams...)
final IType superType = this.theClass.getSuperType();
if (superType != null) {
// Generate the constructor body
ArgumentList arguments = this.theClass.getSuperConstructorArguments();
if (arguments == null) {
arguments = ArgumentList.empty();
}
final InitializerCall init = new InitializerCall(this.theClass.getPosition(), true, arguments, superType);
constructor.setInitializer(init);
}
// { this.classParams... = classParams... }
final ParameterList classParams = this.theClass.getParameters();
final StatementList ctorBody = new StatementList();
final ParameterList ctorParams = constructor.getParameters();
// j is the counter for class parameters, as there may be leading synthetic constructor parameters
for (int i = 0, j = 0, count = ctorParams.size(); i < count; i++) {
final IParameter ctorParam = ctorParams.get(i);
if (ctorParam.hasModifier(Modifiers.SYNTHETIC)) {
continue;
}
final IParameter classParam = classParams.get(j++);
if (classParam.hasModifier(Modifiers.OVERRIDE)) {
continue;
}
final IValue receiver = new ThisExpr(this.theClass);
final FieldAccess access = new FieldAccess(ctorParam);
final FieldAssignment assignment = new FieldAssignment(position, receiver, classParam, access);
ctorBody.add(assignment);
}
constructor.setValue(ctorBody);
return constructor;
}
use of dyvilx.tools.compiler.ast.parameter.IParameter in project Dyvil by Dyvil.
the class ClassMetadata method copyClassParameters.
protected void copyClassParameters(ICallableMember constructor) {
ParameterList from = this.theClass.getParameters();
final int parameterCount = from.size();
final ParameterList ctrParams = constructor.getParameters();
for (int i = 0; i < parameterCount; i++) {
final IParameter classParameter = from.get(i);
AttributeList attributes = classParameter.getAttributes().filtered(Modifiers.PARAMETER_MODIFIERS);
ctrParams.add(constructor.createParameter(classParameter.getPosition(), classParameter.getName(), classParameter.getType(), attributes));
}
if (ctrParams.isLastVariadic()) {
constructor.getAttributes().addFlag(Modifiers.ACC_VARARGS);
}
}
use of dyvilx.tools.compiler.ast.parameter.IParameter in project Dyvil by Dyvil.
the class AbstractClass method resolveField.
@Override
public IDataMember resolveField(Name name) {
final IParameter parameter = this.resolveClassParameter(name);
if (parameter != null) {
return parameter;
}
IDataMember field;
if (this.body != null) {
// Own fields
field = this.body.getField(name);
if (field != null) {
return field;
}
}
// Inherited Fields
if (this.superType != null) {
field = this.superType.resolveField(name);
if (field != null) {
return field;
}
}
return null;
}
Aggregations