use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.
the class ClassBody method resolveImplicit.
public IValue resolveImplicit(IType type) {
if (type == null) {
return null;
}
IValue candidate = null;
for (int i = 0; i < this.classCount; i++) {
final IClass iclass = this.classes[i];
if (!iclass.isImplicit() || !iclass.isObject() || !Types.isSuperType(type, iclass.getClassType())) {
continue;
}
if (candidate != null) {
// ambiguous
return null;
}
candidate = new FieldAccess(iclass.getMetadata().getInstanceField());
}
for (int i = 0; i < this.fieldCount; i++) {
final IField field = this.fields[i];
if (!field.isImplicit() || !Types.isSuperType(type, field.getType())) {
continue;
}
if (candidate != null) {
// ambiguous
return null;
}
// this<Class> is added automatically later
candidate = new FieldAccess(field);
}
return candidate;
}
use of dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.
the class AnnotationMetadata method write.
@Override
public void write(ClassWriter writer) throws BytecodeException {
for (IParameter parameter : this.theClass.getParameters()) {
final StringBuilder desc = new StringBuilder("()");
parameter.getType().appendExtendedName(desc);
final MethodVisitor methodVisitor = writer.visitMethod(Modifiers.PUBLIC | Modifiers.ABSTRACT, parameter.getInternalName(), desc.toString(), null, null);
final IValue argument = parameter.getValue();
if (argument != null && argument.isAnnotationConstant()) {
final AnnotationVisitor av = methodVisitor.visitAnnotationDefault();
argument.writeAnnotationValue(av, parameter.getInternalName());
av.visitEnd();
}
methodVisitor.visitEnd();
}
}
use of dyvilx.tools.compiler.ast.expression.IValue 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;
}
use of dyvilx.tools.compiler.ast.expression.IValue 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 dyvilx.tools.compiler.ast.expression.IValue in project Dyvil by Dyvil.
the class AbstractConstructor method toString.
@Override
public void toString(@NonNull String indent, @NonNull StringBuilder buffer) {
super.toString(indent, buffer);
buffer.append("init");
this.parameters.toString(indent, buffer);
final InitializerCall init = this.getInitializer();
if (init != null) {
Formatting.appendSeparator(buffer, "initializer.call.colon", ':');
init.toString(indent, buffer);
}
if (this.exceptions != null && this.exceptions.size() > 0) {
String throwsPrefix = indent;
if (Formatting.getBoolean("constructor.throws.newline")) {
throwsPrefix = Formatting.getIndent("constructor.throws.indent", indent);
buffer.append('\n').append(throwsPrefix).append("throws ");
} else {
buffer.append(" throws ");
}
Util.astToString(throwsPrefix, this.exceptions.getTypes(), this.exceptions.size(), Formatting.getSeparator("constructor.throws", ','), buffer);
}
final IValue value = this.getValue();
if (value != null && !Util.formatStatementList(indent, buffer, value)) {
buffer.append(" = ");
value.toString(indent, buffer);
}
if (Formatting.getBoolean("constructor.semicolon")) {
buffer.append(';');
}
}
Aggregations