Search in sources :

Example 41 with IValue

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;
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) IField(dyvilx.tools.compiler.ast.field.IField)

Example 42 with IValue

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();
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) AnnotationVisitor(dyvilx.tools.asm.AnnotationVisitor) MethodVisitor(dyvilx.tools.asm.MethodVisitor)

Example 43 with IValue

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;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) VarargsOperator(dyvilx.tools.compiler.ast.expression.intrinsic.VarargsOperator) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ConstructorCall(dyvilx.tools.compiler.ast.expression.access.ConstructorCall) IType(dyvilx.tools.compiler.ast.type.IType)

Example 44 with IValue

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;
}
Also used : AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) CastOperator(dyvilx.tools.compiler.ast.expression.CastOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) MethodCall(dyvilx.tools.compiler.ast.expression.access.MethodCall) IType(dyvilx.tools.compiler.ast.type.IType) InstanceOfOperator(dyvilx.tools.compiler.ast.expression.InstanceOfOperator) IfStatement(dyvilx.tools.compiler.ast.statement.IfStatement) IValue(dyvilx.tools.compiler.ast.expression.IValue) NullValue(dyvilx.tools.compiler.ast.expression.constant.NullValue) CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess)

Example 45 with IValue

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(';');
    }
}
Also used : IValue(dyvilx.tools.compiler.ast.expression.IValue) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall)

Aggregations

IValue (dyvilx.tools.compiler.ast.expression.IValue)79 IType (dyvilx.tools.compiler.ast.type.IType)20 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)17 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)11 SourcePosition (dyvil.source.position.SourcePosition)10 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)9 ArrayExpr (dyvilx.tools.compiler.ast.expression.ArrayExpr)7 Marker (dyvilx.tools.parsing.marker.Marker)7 IMethod (dyvilx.tools.compiler.ast.method.IMethod)6 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)5 IContext (dyvilx.tools.compiler.ast.context.IContext)5 MethodCall (dyvilx.tools.compiler.ast.expression.access.MethodCall)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)4 IClass (dyvilx.tools.compiler.ast.classes.IClass)4 Variable (dyvilx.tools.compiler.ast.field.Variable)4 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)4 CombiningContext (dyvilx.tools.compiler.ast.context.CombiningContext)3 IVariable (dyvilx.tools.compiler.ast.field.IVariable)3 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)3