Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Added utility methods for dealing with annotations and native backends (
Browse files Browse the repository at this point in the history
  • Loading branch information
quintesse committed Apr 28, 2015
1 parent 07364c3 commit 9b332f3
Showing 1 changed file with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.redhat.ceylon.common.Backend;
import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.model.Method;
import com.redhat.ceylon.compiler.typechecker.model.TypedDeclaration;
Expand All @@ -22,6 +23,10 @@ public static String name(Tree.Identifier id) {
}

public static boolean hasAnnotation(Tree.AnnotationList al, String name, Unit unit) {
return getAnnotation(al, name, unit) != null;
}

public static Tree.Annotation getAnnotation(Tree.AnnotationList al, String name, Unit unit) {
if (al!=null) {
for (Tree.Annotation a: al.getAnnotations()) {
Tree.BaseMemberExpression p =
Expand All @@ -31,14 +36,47 @@ public static boolean hasAnnotation(Tree.AnnotationList al, String name, Unit un
String alias = unit==null ? name : //WTF?!
unit.getModifiers().get(name);
if (an.equals(alias)) {
return true;
return a;
}
}
}
}
return false;
return null;
}

public static String getAnnotationArgument(Tree.Annotation a, String def) {
String result = def;
Tree.Expression expression = null;
if (a.getPositionalArgumentList() != null && a.getPositionalArgumentList().getPositionalArguments().size() > 0) {
Tree.PositionalArgument arg = a.getPositionalArgumentList().getPositionalArguments().get(0);
if (arg instanceof Tree.ListedArgument) {
expression = ((Tree.ListedArgument) arg).getExpression();
}
} else if (a.getNamedArgumentList() != null && a.getNamedArgumentList().getNamedArguments().size() > 0) {
Tree.SpecifiedArgument arg = (Tree.SpecifiedArgument)a.getNamedArgumentList().getNamedArguments().get(0);
expression = arg.getSpecifierExpression().getExpression();
}
if (expression != null) {
Tree.Literal literal = (Tree.Literal)expression.getTerm();
result = literal.getText();
if (result.startsWith("\"") && result.endsWith("\"")) {
result = result.substring(1, result.length() - 1);
}
}
return result;
}

public static boolean isForBackend(Tree.AnnotationList al, Backend forBackend, Unit unit) {
Tree.Annotation a = getAnnotation(al, "native", unit);
if (a != null) {
String backend = getAnnotationArgument(a, "");
if (!backend.equals(forBackend.nativeAnnotation)) {
return false;
}
}
return true;
}

public static boolean hasUncheckedNulls(Tree.Term term) {
return hasUncheckedNulls(term, false);
}
Expand Down

0 comments on commit 9b332f3

Please sign in to comment.