Search in sources :

Example 1 with TupleType

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

the class TupleExpr method getType.

@Override
public IType getType() {
    if (this.tupleType != null) {
        return this.tupleType;
    }
    final int arity = this.values.size();
    final TupleType tupleType = new TupleType(arity);
    final TypeList arguments = tupleType.getArguments();
    for (int i = 0; i < arity; i++) {
        arguments.add(this.values.get(i).getType());
    }
    return this.tupleType = tupleType;
}
Also used : TupleType(dyvilx.tools.compiler.ast.type.compound.TupleType) TypeList(dyvilx.tools.compiler.ast.type.TypeList)

Example 2 with TupleType

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

the class CaseClassMetadata method getUnapplyReturnType.

private TupleType getUnapplyReturnType() {
    final TupleType returnType = new TupleType();
    final TypeList typeArgs = returnType.getArguments();
    for (IParameter param : this.theClass.getParameters()) {
        typeArgs.add(param.getType());
    }
    return returnType;
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) TupleType(dyvilx.tools.compiler.ast.type.compound.TupleType) TypeList(dyvilx.tools.compiler.ast.type.TypeList)

Example 3 with TupleType

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

the class TupleSurrogate method getType.

@Override
public IType getType() {
    if (this.tupleType != null) {
        return this.tupleType;
    }
    final TupleType tupleType = new TupleType(this.patternCount);
    final TypeList arguments = tupleType.getArguments();
    for (int i = 0; i < this.patternCount; i++) {
        arguments.add(this.patterns[i].getType());
    }
    return this.tupleType = tupleType;
}
Also used : TupleType(dyvilx.tools.compiler.ast.type.compound.TupleType) TypeList(dyvilx.tools.compiler.ast.type.TypeList)

Example 4 with TupleType

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

the class InternalGenericType method resolveType.

@Override
public IType resolveType(MarkerList markers, IContext context) {
    this.arguments.resolveTypes(markers, context);
    if (this.internalName.startsWith("dyvil/tuple/Tuple$Of")) {
        return new TupleType(this.arguments);
    }
    if (this.internalName.startsWith("dyvil/function/Function$Of")) {
        return new LambdaType(this.arguments);
    }
    switch(this.internalName) {
        case "dyvil/ref/ObjectRef":
            return new ReferenceType(ReferenceType.LazyFields.OBJECT_REF_CLASS, this.arguments.get(0));
        case "dyvil/collection/Map":
            return MapType.base(this.arguments.get(0), this.arguments.get(1));
        case "dyvil/collection/MutableMap":
            return MapType.mutable(this.arguments.get(0), this.arguments.get(1));
        case "dyvil/collection/ImmutableMap":
            return MapType.immutable(this.arguments.get(0), this.arguments.get(1));
    }
    final IClass iclass = Package.rootPackage.resolveInternalClass(this.internalName);
    return new ClassGenericType(iclass, this.arguments);
}
Also used : LambdaType(dyvilx.tools.compiler.ast.type.compound.LambdaType) TupleType(dyvilx.tools.compiler.ast.type.compound.TupleType) IClass(dyvilx.tools.compiler.ast.classes.IClass) ReferenceType(dyvilx.tools.compiler.ast.reference.ReferenceType)

Example 5 with TupleType

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

the class UnapplyPattern method writeJumpOnMismatch.

// Compilation
@Override
public void writeJumpOnMismatch(MethodWriter writer, int varIndex, Label target) throws BytecodeException {
    Pattern.loadVar(writer, varIndex);
    final int lineNumer = this.lineNumber();
    final int tupleVarIndex = writer.localCount();
    this.unapplyCall.writeExpression(writer, null);
    final IType methodType = this.unapplyCall.getType();
    final String internalType = methodType.getInternalName();
    final TupleType tupleType = NullableType.unapply(methodType).extract(TupleType.class);
    final TypeList typeArgs = tupleType.getArguments();
    if (// nullable
    methodType != tupleType) {
        writer.visitInsn(Opcodes.DUP);
        writer.visitVarInsn(Opcodes.ASTORE, tupleVarIndex);
        writer.visitJumpInsn(Opcodes.IFNULL, target);
    } else {
        writer.visitVarInsn(Opcodes.ASTORE, tupleVarIndex);
    }
    for (int i = 0; i < this.patternCount; i++) {
        if (this.patterns[i].isWildcard()) {
            // Skip wildcard patterns
            continue;
        }
        final IType targetType = typeArgs.get(i);
        writer.visitVarInsn(Opcodes.ALOAD, tupleVarIndex);
        // Get and cast the tuple element
        // FIXME not ready for Tuple.OfN
        writer.visitFieldInsn(Opcodes.GETFIELD, internalType, "_" + (i + 1), "Ljava/lang/Object;");
        Types.OBJECT.writeCast(writer, targetType, lineNumer);
        // Check the pattern
        this.patterns[i].writeJumpOnMismatch(writer, -1, target);
    }
}
Also used : TupleType(dyvilx.tools.compiler.ast.type.compound.TupleType) TypeList(dyvilx.tools.compiler.ast.type.TypeList) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

TupleType (dyvilx.tools.compiler.ast.type.compound.TupleType)6 TypeList (dyvilx.tools.compiler.ast.type.TypeList)5 IType (dyvilx.tools.compiler.ast.type.IType)2 IClass (dyvilx.tools.compiler.ast.classes.IClass)1 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)1 AbstractPattern (dyvilx.tools.compiler.ast.pattern.AbstractPattern)1 Pattern (dyvilx.tools.compiler.ast.pattern.Pattern)1 ReferenceType (dyvilx.tools.compiler.ast.reference.ReferenceType)1 LambdaType (dyvilx.tools.compiler.ast.type.compound.LambdaType)1 Marker (dyvilx.tools.parsing.marker.Marker)1