Search in sources :

Example 1 with Annotation

use of org.abs_models.frontend.ast.Annotation in project abstools by abstools.

the class AnnotationHelper method getAnnotationsOfType.

public static java.util.List<Annotation> getAnnotationsOfType(List<Annotation> annos, String qualifiedName) {
    ArrayList<Annotation> res = new ArrayList<>();
    for (Annotation a : annos) {
        if (a.getType().getQualifiedName().equals(qualifiedName)) {
            DataConstructorExp de = (DataConstructorExp) a.getValue();
            res.add(a);
        }
    }
    return res;
}
Also used : DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) ArrayList(java.util.ArrayList) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 2 with Annotation

use of org.abs_models.frontend.ast.Annotation in project abstools by abstools.

the class AnnotationUtil method getExpansionId.

/**
 * Gets the expansion ID of a function declaration. If the function declaration is not an expansion, -1 is
 * returned.
 *
 * @param decl a function declaration
 * @return an expansion ID, or -1
 * @throws NullPointerException if decl is null
 */
public static int getExpansionId(FunctionDecl decl) {
    Objects.requireNonNull(decl);
    Annotation annotation = getAnnotation(decl.getAnnotationsNoTransform(), expansionType());
    if (annotation == null) {
        return -1;
    }
    PureExp value = annotation.getValue();
    if (value instanceof IntLiteral) {
        IntLiteral intValue = (IntLiteral) value;
        try {
            int result = Integer.parseInt(intValue.getContent());
            return result < 0 ? -1 : result;
        } catch (NumberFormatException e) {
            return -1;
        }
    } else {
        return -1;
    }
}
Also used : IntLiteral(org.abs_models.frontend.ast.IntLiteral) PureExp(org.abs_models.frontend.ast.PureExp) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 3 with Annotation

use of org.abs_models.frontend.ast.Annotation in project abstools by abstools.

the class AnnotationUtil method annotateExpansion.

public static void annotateExpansion(FunctionDecl expansion, int expansionId) {
    IntLiteral indexLiteral = new IntLiteral(Integer.toString(expansionId));
    Annotation annotation = new TypedAnnotation(indexLiteral, expansionType());
    expansion.addAnnotation(annotation);
}
Also used : IntLiteral(org.abs_models.frontend.ast.IntLiteral) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 4 with Annotation

use of org.abs_models.frontend.ast.Annotation in project abstools by abstools.

the class AnnotationUtil method addToAnnotations.

/**
 * Add an ExpansionCall annotation, or an argument to an existing
 * annotation.  Creates or adds to [ExpansionCall : list[expansionId]]
 * annotation.
 *
 * @param annotations The list to mutate
 * @param annotationType Currently always EXPANSION_CALL
 * @param expansionId An integer to add to the list.
 */
private static void addToAnnotations(List<Annotation> annotations, TypeIdUse annotationType, int expansionId) {
    IntLiteral indexLiteral = new IntLiteral(Integer.toString(expansionId));
    Annotation toAdd = getAnnotation(annotations, annotationType);
    if (toAdd == null) {
        List<PureExp> llist = new List<>(new ListLiteral(new List<PureExp>(indexLiteral)));
        toAdd = new TypedAnnotation(new FnApp("list", llist), annotationType);
        annotations.add(toAdd);
    } else {
        PureExp value = toAdd.getValue();
        if (!(value instanceof FnApp)) {
            throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
        }
        FnApp fvalue = (FnApp) value;
        if (!fvalue.getName().equals("list")) {
            throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
        }
        ListLiteral list = (ListLiteral) fvalue.getParam(0);
        for (PureExp exp : list.getPureExps()) {
            if (exp instanceof IntLiteral) {
                IntLiteral intLiteral = (IntLiteral) exp;
                if (intLiteral.getContent().equals(indexLiteral.getContent())) {
                    return;
                }
            }
        }
        list.addPureExp(indexLiteral);
    }
}
Also used : ListLiteral(org.abs_models.frontend.ast.ListLiteral) FnApp(org.abs_models.frontend.ast.FnApp) IntLiteral(org.abs_models.frontend.ast.IntLiteral) List(org.abs_models.frontend.ast.List) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation) PureExp(org.abs_models.frontend.ast.PureExp) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 5 with Annotation

use of org.abs_models.frontend.ast.Annotation in project abstools by abstools.

the class ClassKindTypeExtension method checkNewExp.

@Override
public void checkNewExp(NewExp e) {
    ClassDecl d = (ClassDecl) e.lookup(new KindedName(Kind.CLASS, e.getClassName()));
    List<Annotation> anns = AnnotationHelper.getAnnotationsOfType(d.getAnnotations(), "ABS.StdLib.ClassKindAnnotation");
    if (!anns.isEmpty()) {
        String name = ((DataConstructorExp) anns.get(0).getValue()).getDecl().getName();
        if (e.hasLocal()) {
            if (name.equals("COG")) {
                errors.add(new TypeError(e, ErrorMessage.CLASSKIND_PLAIN, d.getName()));
            }
        } else {
            if (!name.equals("COG")) {
                errors.add(new TypeError(e, ErrorMessage.CLASSKIND_COG, d.getName()));
            }
        }
    }
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) TypeError(org.abs_models.frontend.analyser.TypeError) KindedName(org.abs_models.frontend.typechecker.KindedName) Annotation(org.abs_models.frontend.ast.Annotation)

Aggregations

Annotation (org.abs_models.frontend.ast.Annotation)7 PureExp (org.abs_models.frontend.ast.PureExp)4 TypedAnnotation (org.abs_models.frontend.ast.TypedAnnotation)4 IntLiteral (org.abs_models.frontend.ast.IntLiteral)3 MethodSig (org.abs_models.frontend.ast.MethodSig)2 ArrayList (java.util.ArrayList)1 TypeError (org.abs_models.frontend.analyser.TypeError)1 ClassDecl (org.abs_models.frontend.ast.ClassDecl)1 DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)1 FnApp (org.abs_models.frontend.ast.FnApp)1 List (org.abs_models.frontend.ast.List)1 ListLiteral (org.abs_models.frontend.ast.ListLiteral)1 KindedName (org.abs_models.frontend.typechecker.KindedName)1