Search in sources :

Example 1 with TypeParameterList

use of dyvilx.tools.compiler.ast.generic.TypeParameterList in project Dyvil by Dyvil.

the class ClassGenericType method argumentsMatch.

protected boolean argumentsMatch(IType type) {
    final int count = Math.min(this.arguments.size(), this.getTheClass().typeArity());
    final TypeParameterList classTypeParams = this.getTheClass().getTypeParameters();
    for (int i = 0; i < count; i++) {
        final ITypeParameter typeVar = classTypeParams.get(i);
        final IType thisArgument = this.arguments.get(i);
        final IType thatArgument = type.resolveType(typeVar);
        if (thatArgument != null && !Variance.checkCompatible(typeVar.getVariance(), thisArgument, thatArgument)) {
            return false;
        }
    }
    return true;
}
Also used : TypeParameterList(dyvilx.tools.compiler.ast.generic.TypeParameterList) ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Example 2 with TypeParameterList

use of dyvilx.tools.compiler.ast.generic.TypeParameterList in project Dyvil by Dyvil.

the class ResolvedGenericType method checkType.

@Override
public void checkType(MarkerList markers, IContext context, int position) {
    ModifierUtil.checkVisibility(this.theClass, this.position, markers, context);
    // Check if the Type Variable Bounds accept the supplied Type Arguments
    final int count = Math.min(this.arguments.size(), this.theClass.typeArity());
    final TypeParameterList classTypeParams = this.theClass.getTypeParameters();
    for (int i = 0; i < count; i++) {
        final ITypeParameter typeParameter = classTypeParams.get(i);
        final IType typeArgument = this.resolveType(typeParameter);
        if (typeArgument.isResolved() && !typeParameter.isAssignableFrom(typeArgument, null)) {
            final Marker marker = Markers.semanticError(typeArgument.getPosition(), "type.generic.incompatible", typeParameter.getName().qualified, this.theClass.getFullName());
            marker.addInfo(Markers.getSemantic("type.generic.argument", typeArgument));
            marker.addInfo(Markers.getSemantic("type_parameter.declaration", typeParameter));
            markers.add(marker);
        }
    }
    if ((position & TypePosition.GENERIC_FLAG) == 0) {
        markers.add(Markers.semanticError(this.position, "type.generic.class"));
    }
    super.checkType(markers, context, position);
}
Also used : TypeParameterList(dyvilx.tools.compiler.ast.generic.TypeParameterList) ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter) Marker(dyvilx.tools.parsing.marker.Marker) IType(dyvilx.tools.compiler.ast.type.IType)

Example 3 with TypeParameterList

use of dyvilx.tools.compiler.ast.generic.TypeParameterList in project Dyvil by Dyvil.

the class CodeMethod method writeBridgeTypeParameters.

private void writeBridgeTypeParameters(MethodWriter methodWriter, IMethod overrideMethod) {
    if (this.typeParameters != null) {
        final TypeParameterList overrideTypeParams = overrideMethod.getTypeParameters();
        for (int i = 0, count = this.typeParameters.size(); i < count; i++) {
            final ITypeParameter thisParameter = this.typeParameters.get(i);
            final Reified.Type reifiedType = thisParameter.getReifiedKind();
            if (reifiedType == null) {
                continue;
            }
            final ITypeParameter overrideParameter = overrideTypeParams.get(i);
            this.writeReifyArgument(methodWriter, thisParameter, reifiedType, overrideParameter);
        // Extra type parameters from the overridden method are ignored
        }
    }
}
Also used : TypeParameterList(dyvilx.tools.compiler.ast.generic.TypeParameterList) Reified(dyvil.annotation.Reified) ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter)

Example 4 with TypeParameterList

use of dyvilx.tools.compiler.ast.generic.TypeParameterList in project Dyvil by Dyvil.

the class TupleSurrogate method writeJumpOnMismatch.

@Override
public void writeJumpOnMismatch(MethodWriter writer, int varIndex, Label target) throws BytecodeException {
    varIndex = Pattern.ensureVar(writer, varIndex);
    final int lineNumber = this.lineNumber();
    final IType tupleType = this.getType();
    final IClass tupleClass = tupleType.getTheClass();
    final TypeParameterList typeParameters = tupleClass.getTypeParameters();
    final String internalTupleClassName = tupleType.getInternalName();
    for (int i = 0; i < this.patternCount; i++) {
        if (this.patterns[i].isWildcard()) {
            // Skip wildcard patterns
            continue;
        }
        writer.visitVarInsn(Opcodes.ALOAD, varIndex);
        writer.visitFieldInsn(Opcodes.GETFIELD, internalTupleClassName, "_" + (i + 1), "Ljava/lang/Object;");
        final IType targetType = Types.resolveTypeSafely(tupleType, typeParameters.get(i));
        Types.OBJECT.writeCast(writer, targetType, lineNumber);
        this.patterns[i].writeJumpOnMismatch(writer, -1, target);
    }
}
Also used : TypeParameterList(dyvilx.tools.compiler.ast.generic.TypeParameterList) IClass(dyvilx.tools.compiler.ast.classes.IClass) IType(dyvilx.tools.compiler.ast.type.IType)

Example 5 with TypeParameterList

use of dyvilx.tools.compiler.ast.generic.TypeParameterList in project Dyvil by Dyvil.

the class AbstractConstructor method checkArguments.

@Override
public IType checkArguments(MarkerList markers, SourcePosition position, IContext context, IType type, ArgumentList arguments) {
    final IClass theClass = this.enclosingClass;
    if (!theClass.isTypeParametric()) {
        for (int i = 0, count = this.parameters.size(); i < count; i++) {
            arguments.checkValue(i, this.parameters.get(i), null, position, markers, context);
        }
        return type;
    }
    final IType classType = theClass.getThisType();
    final GenericData genericData = new GenericData(theClass);
    classType.inferTypes(type, genericData);
    genericData.lockAvailable();
    // Check Values and infer Types
    for (int i = 0, count = this.parameters.size(); i < count; i++) {
        arguments.checkValue(i, this.parameters.get(i), genericData, position, markers, context);
    }
    genericData.lockAvailable();
    // Check Type Var Inference and Compatibility
    final TypeParameterList typeParams = theClass.getTypeParameters();
    for (int i = 0, count = typeParams.size(); i < count; i++) {
        final ITypeParameter typeParameter = typeParams.get(i);
        final IType typeArgument = genericData.resolveType(typeParameter);
        if (typeArgument == null) {
            final IType inferredType = typeParameter.getUpperBound();
            markers.add(Markers.semantic(position, "constructor.typevar.infer", theClass.getName(), typeParameter.getName(), inferredType));
            genericData.addMapping(typeParameter, inferredType);
        } else if (!typeParameter.isAssignableFrom(typeArgument, genericData)) {
            final Marker marker = Markers.semanticError(position, "constructor.typevar.incompatible", theClass.getName(), typeParameter.getName());
            marker.addInfo(Markers.getSemantic("type.generic.argument", typeArgument));
            marker.addInfo(Markers.getSemantic("type_parameter.declaration", typeParameter));
            markers.add(marker);
        }
    }
    return classType.getConcreteType(genericData);
}
Also used : TypeParameterList(dyvilx.tools.compiler.ast.generic.TypeParameterList) ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter) IClass(dyvilx.tools.compiler.ast.classes.IClass) Marker(dyvilx.tools.parsing.marker.Marker) GenericData(dyvilx.tools.compiler.ast.generic.GenericData) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

TypeParameterList (dyvilx.tools.compiler.ast.generic.TypeParameterList)9 IType (dyvilx.tools.compiler.ast.type.IType)8 IClass (dyvilx.tools.compiler.ast.classes.IClass)5 ITypeParameter (dyvilx.tools.compiler.ast.generic.ITypeParameter)5 Marker (dyvilx.tools.parsing.marker.Marker)3 Reified (dyvil.annotation.Reified)1 Annotation (dyvilx.tools.compiler.ast.attribute.annotation.Annotation)1 GenericData (dyvilx.tools.compiler.ast.generic.GenericData)1 AbstractPattern (dyvilx.tools.compiler.ast.pattern.AbstractPattern)1 Pattern (dyvilx.tools.compiler.ast.pattern.Pattern)1 TypeCheckPattern (dyvilx.tools.compiler.ast.pattern.TypeCheckPattern)1