Search in sources :

Example 51 with IType

use of dyvilx.tools.compiler.ast.type.IType in project Dyvil by Dyvil.

the class MemberSorter method compareMethods.

public static int compareMethods(IMethod method1, IMethod method2) {
    // compare names
    final int n = compareMembers(method1, method2);
    if (n != 0) {
        return n;
    }
    if (!method1.overrides(method2, null)) {
        return method1.getSignature().compareTo(method2.getSignature());
    }
    final IType type1 = method1.getType().asParameterType();
    final IType type2 = method2.getType().asParameterType();
    if (Types.isSuperType(type1, type2)) {
        if (Types.isSuperType(type2, type1)) {
            // same type
            return 0;
        }
        // type2 is more specific than type1
        return 1;
    }
    if (Types.isSuperType(type2, type1)) {
        // type2 is less specific than type1
        return -1;
    }
    return compareNames(type1.getName(), type2.getName());
}
Also used : IType(dyvilx.tools.compiler.ast.type.IType)

Example 52 with IType

use of dyvilx.tools.compiler.ast.type.IType in project Dyvil by Dyvil.

the class CaseClasses method writeToString.

public static void writeToString(MethodWriter writer, IValue value) throws BytecodeException {
    final IType type = value.getType();
    // If someValue is a String, write it as is
    if (Types.isSuperType(Types.STRING, type)) {
        value.writeExpression(writer, Types.STRING);
        return;
    }
    // Otherwise, generate an appropriate call to String.valueOf or ObjectArray.toString
    value.writeExpression(writer, type);
    switch(type.getTypecode()) {
        case PrimitiveType.BOOLEAN_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;", false);
            return;
        case PrimitiveType.BYTE_CODE:
        case PrimitiveType.SHORT_CODE:
        case PrimitiveType.CHAR_CODE:
        case PrimitiveType.INT_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false);
            return;
        case PrimitiveType.LONG_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "toString", "(J)Ljava/lang/String;", false);
            return;
        case PrimitiveType.FLOAT_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "toString", "(F)Ljava/lang/String;", false);
            return;
        case PrimitiveType.DOUBLE_CODE:
            writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;", false);
            return;
    }
    final ArrayType arrayType = type.extract(ArrayType.class);
    if (arrayType != null) {
        writeArrayToString(writer, arrayType);
        return;
    }
    writer.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;", false);
}
Also used : ArrayType(dyvilx.tools.compiler.ast.type.compound.ArrayType) IType(dyvilx.tools.compiler.ast.type.IType)

Example 53 with IType

use of dyvilx.tools.compiler.ast.type.IType in project Dyvil by Dyvil.

the class CaseClassMetadata method createUnapplyMethod.

private CodeMethod createUnapplyMethod() {
    // static final func unapply<TypeParams...>(value: This) -> (T...)
    final SourcePosition position = this.theClass.position();
    final AttributeList attributes = AttributeList.of(Modifiers.PUBLIC | Modifiers.STATIC_FINAL | Modifiers.GENERATED);
    final IType type = 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, this.theClass.getThisType());
    unapply.getParameters().add(parameter);
    // = (value.classParams...)
    final TupleExpr tupleExpr = new TupleExpr(position);
    final ArgumentList arguments = tupleExpr.getValues();
    for (IParameter param : this.theClass.getParameters()) {
        // value
        final FieldAccess thisAccess = new FieldAccess(position, null, parameter);
        // value.classParam
        final IValue fieldAccess;
        if (param.isOverride()) {
            // if the class parameter is marked as 'override', we have to resolve it from a super-class
            // the easiest way to do this is by name
            fieldAccess = new FieldAccess(position, thisAccess, param.getName());
        } else {
            fieldAccess = new FieldAccess(position, thisAccess, param);
        }
        arguments.add(fieldAccess);
    }
    unapply.setValue(tupleExpr);
    return unapply;
}
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) SourcePosition(dyvil.source.position.SourcePosition) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) TupleExpr(dyvilx.tools.compiler.ast.expression.TupleExpr) IType(dyvilx.tools.compiler.ast.type.IType)

Example 54 with IType

use of dyvilx.tools.compiler.ast.type.IType 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;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) AttributeList(dyvilx.tools.compiler.ast.attribute.AttributeList) FieldAssignment(dyvilx.tools.compiler.ast.expression.access.FieldAssignment) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) InitializerCall(dyvilx.tools.compiler.ast.expression.access.InitializerCall) IType(dyvilx.tools.compiler.ast.type.IType) IValue(dyvilx.tools.compiler.ast.expression.IValue) CodeConstructor(dyvilx.tools.compiler.ast.constructor.CodeConstructor) SourcePosition(dyvil.source.position.SourcePosition) StatementList(dyvilx.tools.compiler.ast.statement.StatementList) ParameterList(dyvilx.tools.compiler.ast.parameter.ParameterList) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) ThisExpr(dyvilx.tools.compiler.ast.expression.ThisExpr)

Example 55 with IType

use of dyvilx.tools.compiler.ast.type.IType 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;
}
Also used : CodeMethod(dyvilx.tools.compiler.ast.method.CodeMethod) SourcePosition(dyvil.source.position.SourcePosition) ClassOperator(dyvilx.tools.compiler.ast.expression.ClassOperator) ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) CodeParameter(dyvilx.tools.compiler.ast.parameter.CodeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

IType (dyvilx.tools.compiler.ast.type.IType)159 IClass (dyvilx.tools.compiler.ast.classes.IClass)26 Marker (dyvilx.tools.parsing.marker.Marker)23 IValue (dyvilx.tools.compiler.ast.expression.IValue)21 TypeParameterList (dyvilx.tools.compiler.ast.generic.TypeParameterList)13 ParameterList (dyvilx.tools.compiler.ast.parameter.ParameterList)11 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)10 SourcePosition (dyvil.source.position.SourcePosition)9 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)9 ITypeParameter (dyvilx.tools.compiler.ast.generic.ITypeParameter)9 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)9 TypeList (dyvilx.tools.compiler.ast.type.TypeList)7 AttributeList (dyvilx.tools.compiler.ast.attribute.AttributeList)6 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)6 IVariable (dyvilx.tools.compiler.ast.field.IVariable)6 ArrayType (dyvilx.tools.compiler.ast.type.compound.ArrayType)6 MethodWriter (dyvilx.tools.compiler.backend.MethodWriter)6 CodeMethod (dyvilx.tools.compiler.ast.method.CodeMethod)5 CodeParameter (dyvilx.tools.compiler.ast.parameter.CodeParameter)5 Name (dyvil.lang.Name)4