Search in sources :

Example 6 with ITypeParameter

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

the class AbstractMethod method checkTypeVarsInferred.

private void checkTypeVarsInferred(MarkerList markers, SourcePosition position, GenericData genericData) {
    if (this.typeParameters == null) {
        return;
    }
    final int count = this.typeParameters.size();
    genericData.lock(count);
    for (int i = 0; i < count; i++) {
        final ITypeParameter typeParameter = this.typeParameters.get(i);
        final IType typeArgument = genericData.resolveType(typeParameter);
        if (typeArgument == null || typeArgument instanceof CovariantTypeVarType) {
            final IType inferredType = typeParameter.getUpperBound();
            markers.add(Markers.semantic(position, "method.typevar.infer", this.name, typeParameter.getName(), inferredType));
            genericData.addMapping(typeParameter, inferredType);
        } else if (!typeParameter.isAssignableFrom(typeArgument, genericData)) {
            final Marker marker = Markers.semanticError(position, "method.typevar.incompatible", this.name, typeParameter.getName());
            marker.addInfo(Markers.getSemantic("type.generic.argument", typeArgument));
            marker.addInfo(Markers.getSemantic("type_parameter.declaration", typeParameter));
            markers.add(marker);
        }
    }
}
Also used : ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter) CovariantTypeVarType(dyvilx.tools.compiler.ast.type.typevar.CovariantTypeVarType) Marker(dyvilx.tools.parsing.marker.Marker) IType(dyvilx.tools.compiler.ast.type.IType)

Example 7 with ITypeParameter

use of dyvilx.tools.compiler.ast.generic.ITypeParameter 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 8 with ITypeParameter

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

the class ReferenceType method inferTypes.

@Override
public void inferTypes(IType concrete, ITypeContext typeContext) {
    if (!this.theClass.isTypeParametric()) {
        return;
    }
    final ITypeParameter typeVariable = this.theClass.getTypeParameters().get(0);
    final IType concreteRefType = concrete.resolveType(typeVariable);
    if (concreteRefType != null) {
        this.type.inferTypes(concreteRefType, typeContext);
    }
}
Also used : ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Example 9 with ITypeParameter

use of dyvilx.tools.compiler.ast.generic.ITypeParameter 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)

Example 10 with ITypeParameter

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

the class ClassGenericType method inferTypes.

@Override
public void inferTypes(IType concrete, ITypeContext typeContext) {
    final TypeParameterList classTypeParams = this.getTheClass().getTypeParameters();
    for (int i = 0, size = this.arguments.size(); i < size; i++) {
        final ITypeParameter typeVar = classTypeParams.get(i);
        final IType concreteType = concrete.resolveType(typeVar);
        if (concreteType != null) {
            this.arguments.get(i).inferTypes(concreteType, typeContext);
        }
    }
}
Also used : TypeParameterList(dyvilx.tools.compiler.ast.generic.TypeParameterList) ITypeParameter(dyvilx.tools.compiler.ast.generic.ITypeParameter) IType(dyvilx.tools.compiler.ast.type.IType)

Aggregations

ITypeParameter (dyvilx.tools.compiler.ast.generic.ITypeParameter)10 IType (dyvilx.tools.compiler.ast.type.IType)8 TypeParameterList (dyvilx.tools.compiler.ast.generic.TypeParameterList)5 IClass (dyvilx.tools.compiler.ast.classes.IClass)3 Marker (dyvilx.tools.parsing.marker.Marker)3 Package (dyvilx.tools.compiler.ast.structure.Package)2 ITypeAlias (dyvilx.tools.compiler.ast.type.alias.ITypeAlias)2 ResolvedTypeVarType (dyvilx.tools.compiler.ast.type.typevar.ResolvedTypeVarType)2 Reified (dyvil.annotation.Reified)1 IContext (dyvilx.tools.compiler.ast.context.IContext)1 GenericData (dyvilx.tools.compiler.ast.generic.GenericData)1 PackageType (dyvilx.tools.compiler.ast.type.raw.PackageType)1 CovariantTypeVarType (dyvilx.tools.compiler.ast.type.typevar.CovariantTypeVarType)1