Search in sources :

Example 11 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class TypeCheckerHelper method checkForDuplicateDecls.

public static void checkForDuplicateDecls(ModuleDecl mod, SemanticConditionList errors) {
    Map<KindedName, ResolvedName> duplicateNames = new HashMap<>();
    Map<KindedName, ResolvedName> names = getVisibleNames(mod, duplicateNames);
    for (KindedName n : duplicateNames.keySet()) {
        ResolvedName rn = names.get(n);
        ResolvedName origrn = duplicateNames.get(n);
        ErrorMessage msg = null;
        String location = "";
        Decl decl = null;
        if (origrn instanceof ResolvedDeclName) {
            decl = ((ResolvedDeclName) origrn).getDecl();
        } else if (origrn instanceof ResolvedAmbigiousName) {
            decl = ((AmbiguousDecl) ((ResolvedAmbigiousName) origrn).getDecl()).getAlternative().get(0);
        }
        if (decl != null && !decl.getFileName().equals(Main.UNKNOWN_FILENAME)) {
            location = " at " + decl.getFileName() + ":" + decl.getStartLine() + ":" + decl.getStartColumn();
        }
        switch(n.getKind()) {
            case CLASS:
                msg = ErrorMessage.DUPLICATE_CLASS_NAME;
                break;
            case FUN:
                msg = ErrorMessage.DUPLICATE_FUN_NAME;
                break;
            case PARTIAL_FUN:
                msg = ErrorMessage.DUPLICATE_PARTIAL_FUN_NAME;
                break;
            case DATA_CONSTRUCTOR:
                msg = ErrorMessage.DUPLICATE_CONSTRUCTOR;
                break;
            case TYPE_DECL:
                msg = ErrorMessage.DUPLICATE_TYPE_DECL;
                break;
            case TRAIT_DECL:
                msg = ErrorMessage.DUPLICATE_TRAIT_NAME;
                break;
            case MODULE:
                // doesn't happen, no modules within modules
                assert false;
                break;
            default:
                // detect if we added a new KindedName.Kind
                assert false;
                break;
        }
        errors.add(new TypeError(rn.getDecl(), msg, n.getName(), location));
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError) ErrorMessage(org.abs_models.frontend.analyser.ErrorMessage)

Example 12 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class CostAnnotationChecker method checkStmt.

@Override
public void checkStmt(Stmt s) {
    PureExp cost = AnnotationHelper.getAnnotationValueFromName(s.getAnnotations(), "ABS.DC.Cost");
    if (cost == null)
        return;
    cost.typeCheck(errors);
    if (!cost.getType().isNumericType()) {
        errors.add(new TypeError(s, ErrorMessage.WRONG_COST_ANNOTATION_TYPE, cost.getType().getQualifiedName()));
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError) PureExp(org.abs_models.frontend.ast.PureExp)

Example 13 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class HttpExportChecker method checkDataTypeDecl.

@Override
public void checkDataTypeDecl(DataTypeDecl decl) {
    for (DataConstructor c : decl.getDataConstructors()) {
        Set<String> names = new HashSet<>();
        for (ConstructorArg ca : c.getConstructorArgs()) {
            ASTNode arg = null;
            String key = null;
            List<Annotation> ann = ca.getTypeUse().getAnnotations();
            PureExp keyann = AnnotationHelper.getAnnotationValueFromName(ann, "ABS.StdLib.HTTPName");
            if (keyann != null) {
                if (!(keyann instanceof StringLiteral)) {
                    errors.add(new TypeError(keyann, ErrorMessage.WRONG_HTTPNAME, keyann.getType()));
                } else {
                    key = ((StringLiteral) keyann).getContent();
                    arg = keyann;
                }
            }
            if (ca.hasSelectorName() && key == null) {
                key = ca.getSelectorName().toString();
                arg = ca.getSelectorName();
            }
            if (key != null) {
                if (names.contains(key)) {
                    errors.add(new SemanticWarning(arg, ErrorMessage.DUPLICATE_HTTPNAME, key));
                } else {
                    names.add(key);
                }
            }
        }
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError) SemanticWarning(org.abs_models.frontend.analyser.SemanticWarning) HashSet(java.util.HashSet)

Example 14 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class ProductLineAnalysisHelper method isStronglyUnambiguous.

/*
     * A product line is strongly unambiguous if each set in the partition of
     * the delta modules specified in the product-line declaration is
     * consistent, that is, if one delta module in a set adds or removes a
     * class, no other delta module in the same set may add, remove or modify
     * the same class, and the modifications of the same class in different
     * delta modules in the same set have to be disjoint.
     */
public static boolean isStronglyUnambiguous(ProductLine pl, SemanticConditionList errors) {
    boolean result = true;
    Model model = pl.getModel();
    /*
        System.out.print("Delta partition: ");
        for (Set<String> set : pl.getDeltaPartition()) {
            System.out.print("{");
            for (String el : set)
                System.out.print(" " + el + " ");
            System.out.print("}   ");
        }
        System.out.println();
         */
    assert pl.getDeltaPartition() != null;
    for (Set<String> set : pl.getDeltaPartition()) {
        // Remember the names of classes and methods modified by deltas in
        // current set
        // { Module.Class -> { Method -> Delta } }
        // { Module.Class -> { "CLASS" -> Delta } }
        Map<String, Map<String, String>> cache = new HashMap<>();
        for (String deltaID : set) {
            // assumes the DeltaDecl corresponding to deltaID exists (wellFormedProductLine)
            DeltaDecl delta = model.getDeltaDeclsMap().get(deltaID);
            assert delta.getModuleModifiers() != null;
            for (ModuleModifier moduleModifier : delta.getModuleModifiers()) {
                if (moduleModifier instanceof ModifyClassModifier) {
                    // String methodID;
                    String prefix = ((ClassModifier) moduleModifier).getQualifiedName();
                    for (Modifier mod : ((ModifyClassModifier) moduleModifier).getModifiers()) {
                        if (mod instanceof DeltaTraitModifier) {
                            HashSet<String> methodIDSet = new HashSet<>();
                            ((DeltaTraitModifier) mod).collectMethodIDs(methodIDSet, model);
                            for (String methodID : methodIDSet) {
                                if (cache.containsKey(prefix)) {
                                    if (cache.get(prefix).containsKey(methodID)) {
                                        result = false;
                                        String otherDeltaID = cache.get(prefix).get(methodID);
                                        if (!deltaID.equals(otherDeltaID))
                                            errors.add(new TypeError(pl, ErrorMessage.AMBIGUOUS_PRODUCTLINE, pl.getName(), deltaID, otherDeltaID, prefix + ", method " + methodID));
                                        else
                                            // FIXME also a kind of ambiguity but needs a different error message
                                            ;
                                    } else if (cache.get(prefix).containsKey("CLASS")) {
                                        result = false;
                                        String otherDeltaID = cache.get(prefix).get("CLASS");
                                        if (!deltaID.equals(otherDeltaID))
                                            errors.add(new TypeError(pl, ErrorMessage.AMBIGUOUS_PRODUCTLINE, pl.getName(), deltaID, otherDeltaID, prefix));
                                        else
                                            // FIXME also a kind of ambiguity but needs a different error message
                                            ;
                                    } else {
                                        cache.get(prefix).put(methodID, deltaID);
                                    }
                                } else {
                                    cache.put(prefix, new HashMap<>());
                                    cache.get(prefix).put(methodID, deltaID);
                                }
                            }
                        }
                    }
                } else if (moduleModifier instanceof AddClassModifier || moduleModifier instanceof RemoveClassModifier) {
                    String prefix = ((ClassModifier) moduleModifier).getQualifiedName();
                    if (cache.containsKey(prefix)) {
                        result = false;
                        assert !cache.get(prefix).isEmpty();
                        String otherDeltaID = cache.get(prefix).values().iterator().next();
                        errors.add(new TypeError(pl, ErrorMessage.AMBIGUOUS_PRODUCTLINE, pl.getName(), deltaID, otherDeltaID, prefix));
                        System.out.println("3 ambiguity due to " + deltaID + "<>" + otherDeltaID);
                    } else {
                        cache.put(prefix, new HashMap<>());
                        cache.get(prefix).put("CLASS", deltaID);
                    }
                }
            }
        // TODO apply same reasoning to other elements: fields,
        // functions, ADTs, etc
        }
    }
    // TODO remove boolean result unless needed
    return result;
}
Also used : HashMap(java.util.HashMap) RemoveClassModifier(org.abs_models.frontend.ast.RemoveClassModifier) AddClassModifier(org.abs_models.frontend.ast.AddClassModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) ClassModifier(org.abs_models.frontend.ast.ClassModifier) AddClassModifier(org.abs_models.frontend.ast.AddClassModifier) RemoveClassModifier(org.abs_models.frontend.ast.RemoveClassModifier) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) ModuleModifier(org.abs_models.frontend.ast.ModuleModifier) Model(org.abs_models.frontend.ast.Model) TypeError(org.abs_models.frontend.analyser.TypeError) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) HashMap(java.util.HashMap) Map(java.util.Map) Modifier(org.abs_models.frontend.ast.Modifier) RemoveClassModifier(org.abs_models.frontend.ast.RemoveClassModifier) AddClassModifier(org.abs_models.frontend.ast.AddClassModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) ClassModifier(org.abs_models.frontend.ast.ClassModifier) ModuleModifier(org.abs_models.frontend.ast.ModuleModifier) HashSet(java.util.HashSet)

Example 15 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class TypeCheckerHelper method checkDataTypeUse.

public static void checkDataTypeUse(SemanticConditionList e, DataTypeUse use) {
    Type type = use.getType();
    if (type.getDecl() instanceof ParametricDataTypeDecl) {
        DataTypeType t = (DataTypeType) type;
        int expected = ((ParametricDataTypeDecl) type.getDecl()).getNumTypeParameter();
        if (expected != t.numTypeArgs()) {
            e.add(new TypeError(use, ErrorMessage.WRONG_NUMBER_OF_TYPE_ARGS, type.toString(), "" + expected, "" + t.numTypeArgs()));
        } else if (expected > 0) {
            if (use instanceof ParametricDataTypeUse) {
                for (TypeUse du : ((ParametricDataTypeUse) use).getParams()) {
                    du.typeCheck(e);
                }
            } else if (use.getDecl() instanceof TypeSynDecl) {
            // nothing to check as this is already checked at the TypeSynDecl
            } else {
                e.add(new TypeError(use, ErrorMessage.WRONG_NUMBER_OF_TYPE_ARGS, type.toString(), "" + expected, "0"));
            }
        }
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError)

Aggregations

TypeError (org.abs_models.frontend.analyser.TypeError)25 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)3 Model (org.abs_models.frontend.ast.Model)3 LocationTypeVar (org.abs_models.frontend.typechecker.locationtypes.LocationTypeVar)3 java.util (java.util)2 HashSet (java.util.HashSet)2 FrontendTest (org.abs_models.frontend.FrontendTest)2 ErrorMessage (org.abs_models.frontend.analyser.ErrorMessage)2 SemanticWarning (org.abs_models.frontend.analyser.SemanticWarning)2 org.abs_models.frontend.ast (org.abs_models.frontend.ast)2 FieldDecl (org.abs_models.frontend.ast.FieldDecl)2 PureExp (org.abs_models.frontend.ast.PureExp)2 Type (org.abs_models.frontend.typechecker.Type)2 TypeAnnotation (org.abs_models.frontend.typechecker.TypeAnnotation)2 AdaptDirection (org.abs_models.frontend.typechecker.ext.AdaptDirection)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SemanticError (org.abs_models.frontend.analyser.SemanticError)1