use of dyvilx.tools.compiler.ast.parameter.ParameterList 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.ParameterList 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.ParameterList in project Dyvil by Dyvil.
the class AnonymousClassMetadata method write.
@Override
public void write(ClassWriter writer) throws BytecodeException {
final CaptureHelper<CaptureField> captureHelper = this.theClass.captureHelper;
final FieldThis thisField = this.theClass.thisField;
final IConstructor constructor = this.theClass.constructor;
captureHelper.writeCaptureFields(writer);
final MethodWriter initWriter = new MethodWriterImpl(writer, writer.visitMethod(Modifiers.MANDATED, "<init>", this.theClass.getConstructorDesc(), null, null));
final ParameterList parameterList = constructor.getParameters();
final int parameterCount = parameterList.size();
// Signature & Parameter Data
initWriter.setThisType(this.theClass.getInternalName());
parameterList.write(initWriter);
int index = initWriter.localCount();
int thisIndex = index;
if (thisField != null) {
thisField.writeField(writer);
index = initWriter.visitParameter(index, thisField.getName(), thisField.getTargetClass().getThisType(), Modifiers.MANDATED);
}
captureHelper.writeCaptureParameters(initWriter, index);
// Constructor Body
initWriter.visitCode();
initWriter.visitVarInsn(Opcodes.ALOAD, 0);
for (int i = 0; i < parameterCount; i++) {
parameterList.get(i).writeGet(initWriter);
}
constructor.writeInvoke(initWriter, 0);
if (thisField != null) {
initWriter.visitVarInsn(Opcodes.ALOAD, 0);
initWriter.visitVarInsn(Opcodes.ALOAD, thisIndex);
initWriter.visitFieldInsn(Opcodes.PUTFIELD, this.theClass.getInternalName(), thisField.getName(), thisField.getDescriptor());
}
captureHelper.writeFieldAssignments(initWriter);
this.theClass.writeClassInit(initWriter);
initWriter.visitEnd(Types.VOID);
}
use of dyvilx.tools.compiler.ast.parameter.ParameterList in project Dyvil by Dyvil.
the class ClassBody method getConstructor.
public IConstructor getConstructor(ParameterList parameters) {
for (int i = 0; i < this.constructorCount; i++) {
final IConstructor constructor = this.constructors[i];
final ParameterList constructorParameterList = constructor.getParameters();
if (parameters.matches(constructorParameterList)) {
return constructor;
}
}
return null;
}
use of dyvilx.tools.compiler.ast.parameter.ParameterList in project Dyvil by Dyvil.
the class AnnotationExpr method writeExpression.
@Override
public void writeExpression(MethodWriter writer, IType type) throws BytecodeException {
final StringBuilder descBuilder = new StringBuilder().append('(');
final ArgumentList arguments = this.annotation.getArguments();
final IClass iclass = this.annotation.getType().getTheClass();
final ParameterList parameterList = iclass.getParameters();
final int count = parameterList.size();
String[] parameterNames = new String[count];
for (int i = 0; i < count; i++) {
final IParameter parameter = parameterList.get(i);
final IType parameterType = parameter.getType();
parameterNames[i] = parameter.getInternalName();
parameterType.appendExtendedName(descBuilder);
arguments.writeValue(i, parameter, writer);
}
descBuilder.append(')');
descBuilder.append('L').append(iclass.getInternalName()).append(';');
writer.visitInvokeDynamicInsn("_", descBuilder.toString(), ANNOTATION_METAFACTORY, (Object[]) parameterNames);
if (type != null) {
this.annotation.getType().writeCast(writer, type, this.lineNumber());
}
}
Aggregations