<commit_before>package com.redhat.ceylon.compiler.typechecker.tree;

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;
import com.redhat.ceylon.compiler.typechecker.model.Unit;


public class Util {
    
    public static final String MISSING_NAME = "program element with missing name";

    public static String name(Tree.Identifier id) {
        if (id==null) {
            return MISSING_NAME;
        }
        else {
            return id.getText();
        }
    }

    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 = 
                        (Tree.BaseMemberExpression) a.getPrimary();
                if (p!=null) {
                    String an = name(p.getIdentifier());
                    String alias = unit==null ? name : //WTF?!
                        unit.getModifiers().get(name); 
                    if (an.equals(alias)) {
                        return a;
                    }
                }
            }
        }
        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);
    }
    
    private static boolean hasUncheckedNulls(Tree.Term term, boolean invoking) {
        if (term instanceof Tree.MemberOrTypeExpression) {
            Declaration d = ((Tree.MemberOrTypeExpression) term).getDeclaration();
            return d instanceof TypedDeclaration 
                    && ((TypedDeclaration) d).hasUncheckedNullType()
                    // only consider method types when invoking them, because java method references can't be null
                    && (d instanceof Method == false || invoking);
        }
        else if (term instanceof Tree.QualifiedMemberOrTypeExpression) {
            return hasUncheckedNulls(((Tree.QualifiedMemberOrTypeExpression)term).getPrimary(), invoking);
        }
        else if (term instanceof Tree.InvocationExpression) {
            return hasUncheckedNulls(((Tree.InvocationExpression) term).getPrimary(), true);
        }
        else if (term instanceof Tree.DefaultOp) {
            return hasUncheckedNulls(((Tree.DefaultOp) term).getRightTerm(), invoking);
        }
        else if (term instanceof Tree.Expression) {
            return hasUncheckedNulls(((Tree.Expression)term).getTerm(), invoking);
        }
        else {<commit_msg>Added helper method `isForBackend()` based on `BackendSupport` and rewrote the existing one to use it (#500)<commit_after>package com.redhat.ceylon.compiler.typechecker.tree;

import java.util.List;

import com.redhat.ceylon.common.Backend;
import com.redhat.ceylon.common.BackendSupport;
import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.model.Method;
import com.redhat.ceylon.compiler.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.compiler.typechecker.model.Unit;
import com.redhat.ceylon.compiler.typechecker.tree.Tree.Annotation;


public class Util {
    
    public static final String MISSING_NAME = "program element with missing name";

    public static String name(Tree.Identifier id) {
        if (id==null) {
            return MISSING_NAME;
        }
        else {
            return id.getText();
        }
    }

    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 = 
                        (Tree.BaseMemberExpression) a.getPrimary();
                if (p!=null) {
                    String an = name(p.getIdentifier());
                    String alias = unit==null ? name : //WTF?!
                        unit.getModifiers().get(name); 
                    if (an.equals(alias)) {
                        return a;
                    }
                }
            }
        }
        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) {
        return isForBackend(al, forBackend.backendSupport, unit);
    }
    
    public static boolean isForBackend(Tree.AnnotationList al, BackendSupport backendSupport, Unit unit) {
        Annotation a = getAnnotation(al, "native", unit);
        if (a != null) {
            Backend backend = Backend.fromAnnotation(getAnnotationArgument(a, ""));
            if (backend == null || !backendSupport.supportsBackend(backend)) {
                return false;
            }
        }
        return true;
    }
    
    public static boolean hasUncheckedNulls(Tree.Term term) {
        return hasUncheckedNulls(term, false);
    }
    
    private static boolean hasUncheckedNulls(Tree.Term term, boolean invoking) {
        if (term instanceof Tree.MemberOrTypeExpression) {
            Declaration d = ((Tree.MemberOrTypeExpression) term).getDeclaration();
            return d instanceof TypedDeclaration 
                    && ((TypedDeclaration) d).hasUncheckedNullType()
                    // only consider method types when invoking them, because java method references can't be null
                    && (d instanceof Method == false || invoking);
        }
        else if (term instanceof Tree.QualifiedMemberOrTypeExpression) {
            return hasUncheckedNulls(((Tree.QualifiedMemberOrTypeExpression)term).getPrimary(), invoking);
        }
        else if (term instanceof Tree.InvocationExpression) {
            return hasUncheckedNulls(((Tree.InvocationExpression) term).getPrimary(), true);
        }
        else if (term instanceof Tree.DefaultOp) {
            return hasUncheckedNulls(((Tree.DefaultOp) term).getRightTerm(), invoking);
        }
        else if (term instanceof Tree.Expression) {
            return hasUncheckedNulls(((Tree.Expression)term).getTerm(), invoking);
        }
        else {
{'fraction_non_alphanumeric': 0.10530877573131094, 'fraction_numerical': 0.0016251354279523294, 'mean_word_length': 2.2967857142857144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '825023', 'n_tokens_mistral': 2394, 'n_tokens_neox': 2337, 'n_words': 542}