Search in sources :

Example 1 with TypeParameter

use of org.abs_models.frontend.typechecker.TypeParameter in project abstools by abstools.

the class TypingTest method functionTypeArgs5.

@Test
public void functionTypeArgs5() {
    Model m = assertParse("def B nth<B>(List<B> list, Int n) = nth(tail(list), n-1) ; ");
    ParametricFunctionDecl d = getLastParametricFunctionDecl(m);
    TypeParameterDecl typeParameter = d.getTypeParameter(0);
    TypeParameter type = (TypeParameter) ((ExpFunctionDef) d.getFunctionDef()).getRhs().getType();
    assertEquals(typeParameter.getName(), type.getDecl().getName());
}
Also used : TypeParameterDecl(org.abs_models.frontend.ast.TypeParameterDecl) ParametricFunctionDecl(org.abs_models.frontend.ast.ParametricFunctionDecl) TypeParameter(org.abs_models.frontend.typechecker.TypeParameter) Model(org.abs_models.frontend.ast.Model) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 2 with TypeParameter

use of org.abs_models.frontend.typechecker.TypeParameter in project abstools by abstools.

the class TypeExtensionHelper method checkFnApp.

public void checkFnApp(FnApp f) {
    FunctionDecl decl = (FunctionDecl) f.getDecl();
    if (decl instanceof ParametricFunctionDecl) {
        HashMap<TypeParameter, Type> map = new HashMap<>();
        for (int i = 0; i < decl.getNumParam(); i++) {
            Type t = f.getParam(i).getType();
            Type arg = decl.getParam(i).getType();
            checkTypeParameter(map, t, arg, f.getParam(i));
        }
    } else {
        checkAssignable(null, decl, f);
    }
}
Also used : TypeParameter(org.abs_models.frontend.typechecker.TypeParameter) BoundedType(org.abs_models.frontend.typechecker.BoundedType) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) Type(org.abs_models.frontend.typechecker.Type) HashMap(java.util.HashMap)

Example 3 with TypeParameter

use of org.abs_models.frontend.typechecker.TypeParameter in project abstools by abstools.

the class TypeExtensionHelper method checkTypeParameter.

private void checkTypeParameter(HashMap<TypeParameter, Type> map, Type rht, Type lht, ASTNode<?> origin) {
    rht = resolveBoundedType(rht);
    if (rht.isBoundedType())
        return;
    if (lht.isTypeParameter() && rht.isReferenceType()) {
        TypeParameter typeParam = (TypeParameter) lht;
        Type lt = map.get(typeParam);
        if (lt != null) {
            checkEq(lt, rht, origin);
        } else {
            map.put(typeParam, rht);
        }
    } else if (lht.isDataType()) {
        DataTypeType argdt = (DataTypeType) lht;
        if (argdt.hasTypeArgs()) {
            DataTypeType dt = (DataTypeType) rht;
            for (int i = 0; i < dt.numTypeArgs(); i++) {
                checkTypeParameter(map, dt.getTypeArg(i), argdt.getTypeArg(i), origin);
            }
        }
    } else if (lht.isReferenceType()) {
        checkEq(lht, rht, origin);
    }
}
Also used : TypeParameter(org.abs_models.frontend.typechecker.TypeParameter) BoundedType(org.abs_models.frontend.typechecker.BoundedType) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) Type(org.abs_models.frontend.typechecker.Type) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType)

Example 4 with TypeParameter

use of org.abs_models.frontend.typechecker.TypeParameter in project abstools by abstools.

the class JavaBackend method getQualifiedString.

public static String getQualifiedString(Type absType) {
    String res = null;
    if (absType.isDataType()) {
        DataTypeType dt = (DataTypeType) absType;
        res = dataTypeMap.get(dt.getDecl().getName());
        if (res != null)
            return res;
        StringBuilder sb = new StringBuilder();
        if (dt.hasTypeArgs() && !containsUnboundedType(dt.getTypeArgs())) {
            sb.append("<");
            boolean first = true;
            for (Type t : dt.getTypeArgs()) {
                if (first)
                    first = false;
                else
                    sb.append(',');
                sb.append(getQualifiedString(t));
            }
            sb.append(">");
        }
        return getQualifiedString(dt.getDecl()) + sb.toString();
    /*
             * if (dt.hasTypeArgs() && !containsUnboundedType(dt.getTypeArgs()))
             * {
             *
             * sb.append("<"); boolean first = true; for (Type t :
             * dt.getTypeArgs()) { if (first) first = false; else
             * sb.append(','); sb.append(getQualifiedString(t)); }
             * sb.append(">"); }
             */
    } else if (absType.isInterfaceType()) {
        InterfaceType it = (InterfaceType) absType;
        return getQualifiedString(it.getDecl());
    } else if (absType.isTypeParameter()) {
        TypeParameter tp = (TypeParameter) absType;
        return tp.getDecl().getName();
    } else if (absType.isBoundedType()) {
        BoundedType bt = (BoundedType) absType;
        if (bt.hasBoundType())
            return getQualifiedString(bt.getBoundType());
        return "?";
    } else if (absType.isAnyType()) {
        return "java.lang.Object";
    } else if (absType.isUnionType()) {
        return getQualifiedString(((UnionType) absType).getOriginatingClass());
    }
    throw new RuntimeException("Type " + absType.getClass().getName() + " not yet supported by Java backend");
}
Also used : BoundedType(org.abs_models.frontend.typechecker.BoundedType) UnionType(org.abs_models.frontend.typechecker.UnionType) Type(org.abs_models.frontend.typechecker.Type) InterfaceType(org.abs_models.frontend.typechecker.InterfaceType) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) BoundedType(org.abs_models.frontend.typechecker.BoundedType) UnionType(org.abs_models.frontend.typechecker.UnionType) InterfaceType(org.abs_models.frontend.typechecker.InterfaceType) TypeParameter(org.abs_models.frontend.typechecker.TypeParameter) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) ABSString(org.abs_models.backend.java.lib.types.ABSString)

Example 5 with TypeParameter

use of org.abs_models.frontend.typechecker.TypeParameter in project abstools by abstools.

the class TypeExtensionHelper method checkDataConstructorExp.

public void checkDataConstructorExp(DataConstructorExp e) {
    DataConstructor decl = (DataConstructor) e.getDecl();
    if (decl.getDataTypeDecl() instanceof ParametricDataTypeDecl) {
        HashMap<TypeParameter, Type> map = new HashMap<>();
        for (int i = 0; i < decl.getNumConstructorArg(); i++) {
            Type rht = e.getParam(i).getType();
            Type arg = decl.getConstructorArg(i).getType();
            checkTypeParameter(map, rht, arg, e.getParam(i));
        }
    }
}
Also used : TypeParameter(org.abs_models.frontend.typechecker.TypeParameter) BoundedType(org.abs_models.frontend.typechecker.BoundedType) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) Type(org.abs_models.frontend.typechecker.Type) HashMap(java.util.HashMap)

Aggregations

TypeParameter (org.abs_models.frontend.typechecker.TypeParameter)7 DataTypeType (org.abs_models.frontend.typechecker.DataTypeType)5 BoundedType (org.abs_models.frontend.typechecker.BoundedType)4 Type (org.abs_models.frontend.typechecker.Type)4 FrontendTest (org.abs_models.frontend.FrontendTest)3 Model (org.abs_models.frontend.ast.Model)3 ParametricFunctionDecl (org.abs_models.frontend.ast.ParametricFunctionDecl)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)2 TypeParameterDecl (org.abs_models.frontend.ast.TypeParameterDecl)2 ABSString (org.abs_models.backend.java.lib.types.ABSString)1 InterfaceType (org.abs_models.frontend.typechecker.InterfaceType)1 UnionType (org.abs_models.frontend.typechecker.UnionType)1