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;
}
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;
}
}
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);
}
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);
}
}
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()));
}
}
}
}
Aggregations