use of dyvil.source.position.SourcePosition 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 dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class EnumClassMetadata method createFromNameMethod.
private CodeMethod createFromNameMethod() {
// @BytecodeName("valueOf")
// public static func from(name: String) -> 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("name"), Types.STRING);
method.getParameters().add(parameter);
// = Enum.valueOf(class<EnumType>, name)
final MethodCall valueOfCall = new MethodCall(position, new ClassAccess(Types.ENUM), Name.fromRaw("valueOf"), new ArgumentList(new ClassOperator(this.theClass.getClassType()), new FieldAccess(parameter)));
method.setValue(valueOfCall);
return method;
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class InterfaceMetadata method processProperty.
protected void processProperty(IProperty property, MarkerList markers) {
this.processMember(property, markers);
final IMethod getter = property.getGetter();
if (getter != null) {
this.processMethod(getter, markers);
}
final IMethod setter = property.getSetter();
if (setter != null) {
this.processMethod(setter, markers);
}
final SourcePosition initializerPosition = property.getInitializerPosition();
if (initializerPosition != null) {
this.processPropertyInitializer(initializerPosition, markers);
}
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class StatementList method toString.
@Override
public void toString(String prefix, StringBuilder buffer) {
if (this.valueCount == 0) {
if (Formatting.getBoolean("statement.empty.newline")) {
buffer.append('{').append('\n').append(prefix).append('}');
} else if (Formatting.getBoolean("statement.empty.space_between")) {
buffer.append("{ }");
} else {
buffer.append("{}");
}
return;
}
buffer.append('{').append('\n');
String indentedPrefix = Formatting.getIndent("statement.indent", prefix);
int prevLine = 0;
Label label;
for (int i = 0; i < this.valueCount; i++) {
IValue value = this.values[i];
SourcePosition pos = value.getPosition();
buffer.append(indentedPrefix);
if (pos != null) {
if (pos.startLine() - prevLine > 1 && i > 0) {
buffer.append('\n').append(indentedPrefix);
}
prevLine = pos.endLine();
}
if (this.labels != null && i < this.labels.length && (label = this.labels[i]) != null) {
buffer.append("label ");
buffer.append(label.name);
if (Formatting.getBoolean("label.separator.space_before")) {
buffer.append(' ');
}
buffer.append(':');
if (Formatting.getBoolean("label.separator.newline_after")) {
buffer.append('\n').append(indentedPrefix);
} else if (Formatting.getBoolean("label.separator.newline_after")) {
buffer.append(' ');
}
}
value.toString(indentedPrefix, buffer);
if (Formatting.getBoolean("statement.semicolon")) {
buffer.append(';');
}
buffer.append('\n');
}
buffer.append(prefix).append('}');
}
use of dyvil.source.position.SourcePosition in project Dyvil by Dyvil.
the class CaseClassMetadata method createApplyMethod.
private CodeMethod createApplyMethod() {
// static final func apply<TypeParams...>(classParams...: ClassParamTypes...) -> This
final SourcePosition position = this.theClass.position();
final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
final IType type = this.theClass.getThisType();
final CodeMethod applyMethod = new CodeMethod(this.theClass, Names.apply, type, attributes);
applyMethod.setPosition(position);
applyMethod.getTypeParameters().addAll(this.theClass.getTypeParameters());
this.copyClassParameters(applyMethod);
// = new This<TypeParams...>(classParams...)
final ArgumentList arguments = new ArgumentList();
for (IParameter param : applyMethod.getParameters()) {
// no need to check for override class parameters here, since we are dealing with parameters of the
// apply method
final IValue access;
if (param.isVarargs()) {
access = new VarargsOperator(position, new FieldAccess(param));
} else {
access = new FieldAccess(param);
}
arguments.add(access);
}
// = new This(params...)
applyMethod.setValue(new ConstructorCall(this.theClass.position(), this.theClass.getThisType(), arguments));
return applyMethod;
}
Aggregations