use of dyvilx.tools.compiler.ast.constructor.IConstructor 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.constructor.IConstructor 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 dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.
the class ConstructorCall method resolveCall.
@Override
public IValue resolveCall(MarkerList markers, IContext context, boolean report) {
if (!this.type.isResolved()) {
return this;
}
final ArrayType arrayType = this.type.extract(ArrayType.class);
if (arrayType != null) {
this.resolveArrayConstructor(markers, context, arrayType);
return this;
}
final IClass theClass = this.type.getTheClass();
if (theClass != null && theClass.isInterface()) {
if (!report) {
return null;
}
markers.add(Markers.semanticError(this.position, "constructor.access.interface", this.type));
return this;
}
final MatchList<IConstructor> candidates = this.resolveCandidates(context, this.type);
if (candidates.hasCandidate()) {
this.checkArguments(markers, context, candidates.getBestMember());
return this;
}
if (report) {
reportResolve(markers, candidates, this.position, this.type, this.arguments);
return this;
}
return null;
}
use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.
the class InitializerCall method resolveCall.
@Override
public IValue resolveCall(MarkerList markers, IContext context, boolean report) {
final IType targetType = this.getTargetType(context);
if (targetType == null || !targetType.isResolved()) {
return this;
}
final MatchList<IConstructor> matches = IContext.resolveConstructors(context, targetType, this.arguments);
if (matches.hasCandidate()) {
this.constructor = matches.getBestMember();
this.constructor.checkArguments(markers, this.position, context, this.targetType, this.arguments);
return this;
}
if (report) {
ConstructorCall.reportResolve(markers, matches, this.position, targetType, this.arguments);
return this;
}
return null;
}
use of dyvilx.tools.compiler.ast.constructor.IConstructor in project Dyvil by Dyvil.
the class Variable method writeRefInit.
public static void writeRefInit(IVariable variable, MethodWriter writer, IValue value) {
final IType type = variable.getType();
final IType refType = variable.getReferenceType();
final IConstructor constructor = refType.getTheClass().getBody().getConstructor(0);
writer.visitTypeInsn(Opcodes.NEW, refType.getInternalName());
if (value != null) {
// new
writer.visitInsn(Opcodes.DUP);
// new, new
value.writeExpression(writer, type);
// new, new, value
} else {
// value, new
writer.visitInsn(Opcodes.AUTO_DUP_X1);
// new, value, new
writer.visitInsn(Opcodes.AUTO_SWAP);
// new, new, value
}
constructor.writeInvoke(writer, variable.lineNumber());
final int localIndex = writer.localCount();
variable.setLocalIndex(localIndex);
writer.setLocalType(localIndex, refType.getInternalName());
writer.visitVarInsn(Opcodes.ASTORE, localIndex);
}
Aggregations