Search in sources :

Example 1 with DataTypeType

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

the class SchedulerChecker method checkScheduleExp.

private void checkScheduleExp(PureExp sched, ClassDecl class_decl, ASTNode<?> loc) {
    if (sched == null)
        return;
    if (!(sched instanceof FnApp)) {
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_ANNOTATION_TYPE, sched.getType()));
        return;
    }
    FnApp s = (FnApp) sched;
    Type scheduler_type = s.getType();
    if (s.getDecl().isUnknown()) {
        errors.add(new TypeError(loc, ErrorMessage.FUNCTION_NOT_RESOLVABLE, s.getName()));
        return;
    }
    FunctionDecl sd = (FunctionDecl) s.getDecl();
    // check scheduling function return type
    boolean schedulerTypeCorrect = scheduler_type.isDataType() && ((DataTypeType) scheduler_type).getQualifiedName().equals("ABS.Scheduler.Process");
    // check scheduling function first arg, pt.1: are we a list?
    boolean schedulerFunFirstArgCorrect = sd.getNumParam() > 0 && sd.getParam(0).getType().getQualifiedName().equals("ABS.StdLib.List");
    if (schedulerFunFirstArgCorrect) {
        // check scheduling function first arg, pt.2: are we a list of
        // processes?
        DataTypeType firstArgType = (DataTypeType) sd.getParam(0).getType();
        if (firstArgType.numTypeArgs() != 1) {
            // should not happen since ABS.StdLib.List takes 1 argument
            schedulerFunFirstArgCorrect = false;
        } else {
            schedulerFunFirstArgCorrect = firstArgType.getTypeArg(0).getQualifiedName().equals("ABS.Scheduler.Process");
        }
    }
    if (!schedulerTypeCorrect || !schedulerFunFirstArgCorrect) {
        // emit two messages: one at the annotation location, one for the
        // offending scheduler function
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_ANNOTATION_TYPE, "dummy"));
        errors.add(new TypeError(sd, ErrorMessage.WRONG_SCHEDULER_FUN_TYPE, s.getName()));
    }
    if (s.getNumParam() == 0 || !(s.getParam(0) instanceof VarUse) || !((VarUse) s.getParam(0)).getName().equals("queue")) {
        // first arg to the scheduler expression must be the magic `queue'
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_FIRST_ARGUMENT, "dummy"));
    }
    if (s.getNumParam() != sd.getNumParam()) {
        errors.add(new TypeError(loc, ErrorMessage.WRONG_NUMBER_OF_ARGS, s.getNumParam(), sd.getNumParam()));
    } else {
        // start from 1; magic first parameter `queue' already checked
        for (int i = 1; i < s.getNumParam(); i++) {
            PureExp arg = s.getParam(i);
            String argname = "";
            if (!(arg instanceof VarOrFieldUse)) {
                // argument was not a plain identifier
                errors.add(new TypeError(arg, ErrorMessage.WRONG_SCHEDULER_FIELD_ARGUMENT, Integer.toString(i + 1), class_decl.getName()));
            } else {
                // Check the rest of the parameters against class
                // field/param names and argument types of the scheduling
                // function.  Parts of this could be elided if we verify
                // that `VarUse's in scheduler annotation are rewritten to
                // `FieldUse's -- then we'd just have to check for the
                // presence of `VarUse' in the parameter list to detect
                // invalid args.  But can't hurt to open-code it (and we
                // still have to check the type of all arguments vs the
                // scheduling function parameters).
                VarOrFieldUse vararg = (VarOrFieldUse) arg;
                String name = vararg.getName();
                Type argtype = UnknownType.INSTANCE;
                for (ParamDecl p : class_decl.getParamList()) {
                    if (p.getName().equals(name))
                        argtype = p.getType();
                }
                for (FieldDecl f : class_decl.getFieldList()) {
                    if (f.getName().equals(name))
                        argtype = f.getType();
                }
                if (argtype.isUnknownType()) {
                    // identifier, but unknown in the class
                    errors.add(new TypeError(arg, ErrorMessage.WRONG_SCHEDULER_FIELD_ARGUMENT, "\"" + name + "\"", class_decl.getName()));
                } else {
                    // argtype: field; paramtype: function arg
                    Type paramtype = sd.getParam(i).getType();
                    if (!argtype.isAssignableTo(paramtype)) {
                        errors.add(new TypeError(arg, ErrorMessage.TYPE_MISMATCH, argtype, paramtype));
                    }
                }
            }
        }
    }
    if (class_decl.getType().isDeploymentComponentType()) {
        errors.add(new TypeError(loc, ErrorMessage.SCHEDULER_ON_DC, "dummy"));
    }
}
Also used : DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) PureExp(org.abs_models.frontend.ast.PureExp) VarUse(org.abs_models.frontend.ast.VarUse) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) FieldDecl(org.abs_models.frontend.ast.FieldDecl) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) Type(org.abs_models.frontend.typechecker.Type) UnknownType(org.abs_models.frontend.typechecker.UnknownType) FnApp(org.abs_models.frontend.ast.FnApp) ParamDecl(org.abs_models.frontend.ast.ParamDecl) TypeError(org.abs_models.frontend.analyser.TypeError) VarOrFieldUse(org.abs_models.frontend.ast.VarOrFieldUse)

Example 2 with DataTypeType

use of org.abs_models.frontend.typechecker.DataTypeType 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 3 with DataTypeType

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

the class TypeExtensionHelper method checkEq.

public void checkEq(Type lht, Type rht, ASTNode<?> origin) {
    if (lht.isDataType() && rht.isDataType()) {
        DataTypeType dtl = (DataTypeType) lht;
        DataTypeType dtr = (DataTypeType) rht;
        if (dtl.hasTypeArgs() && dtr.hasTypeArgs() && dtl.getTypeArgs().size() == dtr.getTypeArgs().size()) {
            for (int i = 0; i < dtl.getTypeArgs().size(); i++) {
                checkEq(dtr.getTypeArg(i), dtl.getTypeArg(i), origin);
            }
        }
    }
    if (lht.isReferenceType() && rht.isReferenceType()) {
        for (TypeSystemExtension tse : obs) {
            tse.checkEq(rht, lht, origin);
        }
    }
}
Also used : DataTypeType(org.abs_models.frontend.typechecker.DataTypeType)

Example 4 with DataTypeType

use of org.abs_models.frontend.typechecker.DataTypeType 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 DataTypeType

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

the class ClassGenerator method generateParameterDescription.

private void generateParameterDescription(Type paramtype) {
    ecs.print("<<\"" + paramtype.getQualifiedName() + "\">>,");
    ecs.print(" {");
    if (paramtype.isDataType()) {
        DataTypeType paramdatatype = (DataTypeType) paramtype;
        if (paramdatatype.hasTypeArgs()) {
            String interp = "";
            for (Type typearg : paramdatatype.getTypeArgs()) {
                ecs.print(interp);
                interp = ", ";
                generateParameterDescription(typearg);
            }
        }
    }
    ecs.print(" }");
}
Also used : DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) Type(org.abs_models.frontend.typechecker.Type) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType)

Aggregations

DataTypeType (org.abs_models.frontend.typechecker.DataTypeType)9 Type (org.abs_models.frontend.typechecker.Type)6 BoundedType (org.abs_models.frontend.typechecker.BoundedType)3 TypeParameter (org.abs_models.frontend.typechecker.TypeParameter)3 FrontendTest (org.abs_models.frontend.FrontendTest)2 Model (org.abs_models.frontend.ast.Model)2 Test (org.junit.Test)2 ABSString (org.abs_models.backend.java.lib.types.ABSString)1 TypeError (org.abs_models.frontend.analyser.TypeError)1 ClassDecl (org.abs_models.frontend.ast.ClassDecl)1 FieldDecl (org.abs_models.frontend.ast.FieldDecl)1 FnApp (org.abs_models.frontend.ast.FnApp)1 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)1 ParamDecl (org.abs_models.frontend.ast.ParamDecl)1 ParametricFunctionDecl (org.abs_models.frontend.ast.ParametricFunctionDecl)1 PureExp (org.abs_models.frontend.ast.PureExp)1 ReturnStmt (org.abs_models.frontend.ast.ReturnStmt)1 Stmt (org.abs_models.frontend.ast.Stmt)1 VarDeclStmt (org.abs_models.frontend.ast.VarDeclStmt)1 VarOrFieldUse (org.abs_models.frontend.ast.VarOrFieldUse)1