Search in sources :

Example 1 with OptionType

use of org.graalvm.compiler.options.OptionType in project graal by oracle.

the class OptionProcessor method createOptionsDescriptorsFile.

private void createOptionsDescriptorsFile(OptionsInfo info, String pkg, Name topDeclaringClass, Element[] originatingElements) {
    String optionsClassName = topDeclaringClass + "_" + OptionDescriptors.class.getSimpleName();
    Filer filer = processingEnv.getFiler();
    try (PrintWriter out = createSourceFile(pkg, optionsClassName, filer, originatingElements)) {
        out.println("// CheckStyle: stop header check");
        out.println("// CheckStyle: stop line length check");
        out.println("// GENERATED CONTENT - DO NOT EDIT");
        out.println("// Source: " + topDeclaringClass + ".java");
        out.println("package " + pkg + ";");
        out.println("");
        out.println("import java.util.*;");
        out.println("import " + OptionDescriptors.class.getPackage().getName() + ".*;");
        out.println("import " + OptionType.class.getName() + ";");
        out.println("");
        out.println("public class " + optionsClassName + " implements " + OptionDescriptors.class.getSimpleName() + " {");
        String desc = OptionDescriptor.class.getSimpleName();
        Collections.sort(info.options);
        out.println("    @Override");
        out.println("    public OptionDescriptor get(String value) {");
        out.println("        switch (value) {");
        out.println("        // CheckStyle: stop line length check");
        for (OptionInfo option : info.options) {
            String name = option.name;
            String optionField;
            if (option.field.getModifiers().contains(Modifier.PRIVATE)) {
                throw new InternalError();
            } else {
                optionField = option.declaringClass + "." + option.field.getSimpleName();
            }
            out.println("        case \"" + name + "\": {");
            OptionType optionType = option.optionType;
            String type = option.type;
            String help = option.help;
            String[] extraHelp = option.extraHelp;
            String declaringClass = option.declaringClass;
            Name fieldName = option.field.getSimpleName();
            out.printf("            return " + desc + ".create(\n");
            out.printf("                /*name*/ \"%s\",\n", name);
            out.printf("                /*optionType*/ %s.%s,\n", optionType.getDeclaringClass().getSimpleName(), optionType.name());
            out.printf("                /*optionValueType*/ %s.class,\n", type);
            out.printf("                /*help*/ \"%s\",\n", help);
            if (extraHelp.length != 0) {
                out.printf("                /*extraHelp*/ new String[] {\n");
                for (String line : extraHelp) {
                    out.printf("                         \"%s\",\n", line.replace("\\", "\\\\").replace("\"", "\\\""));
                }
                out.printf("                              },\n");
            }
            out.printf("                /*declaringClass*/ %s.class,\n", declaringClass);
            out.printf("                /*fieldName*/ \"%s\",\n", fieldName);
            out.printf("                /*option*/ %s);\n", optionField);
            out.println("        }");
        }
        out.println("        // CheckStyle: resume line length check");
        out.println("        }");
        out.println("        return null;");
        out.println("    }");
        out.println();
        out.println("    @Override");
        out.println("    public Iterator<" + desc + "> iterator() {");
        out.println("        return new Iterator<OptionDescriptor>() {");
        out.println("            int i = 0;");
        out.println("            @Override");
        out.println("            public boolean hasNext() {");
        out.println("                return i < " + info.options.size() + ";");
        out.println("            }");
        out.println("            @Override");
        out.println("            public OptionDescriptor next() {");
        out.println("                switch (i++) {");
        for (int i = 0; i < info.options.size(); i++) {
            OptionInfo option = info.options.get(i);
            out.println("                    case " + i + ": return get(\"" + option.name + "\");");
        }
        out.println("                }");
        out.println("                throw new NoSuchElementException();");
        out.println("            }");
        out.println("        };");
        out.println("    }");
        out.println("}");
    }
}
Also used : OptionDescriptors(org.graalvm.compiler.options.OptionDescriptors) Filer(javax.annotation.processing.Filer) OptionType(org.graalvm.compiler.options.OptionType) PrintWriter(java.io.PrintWriter) Name(javax.lang.model.element.Name)

Example 2 with OptionType

use of org.graalvm.compiler.options.OptionType in project graal by oracle.

the class SubstrateOptionsParser method parseOption.

static OptionParseResult parseOption(SortedMap<String, OptionDescriptor> options, String option, EconomicMap<OptionKey<?>, Object> valuesMap, String optionPrefix, BooleanOptionFormat booleanOptionFormat) {
    if (option.length() == 0) {
        return OptionParseResult.error("Option name must be specified");
    }
    String optionName;
    Object value = null;
    String valueString = null;
    char first = option.charAt(0);
    int eqIndex = option.indexOf('=');
    if (first == '+' || first == '-') {
        if (eqIndex != -1) {
            return OptionParseResult.error("Cannot mix +/- with <name>=<value> format: '" + optionPrefix + option + "'");
        }
        optionName = option.substring(1, eqIndex == -1 ? option.length() : eqIndex);
        if (booleanOptionFormat == BooleanOptionFormat.NAME_VALUE) {
            return OptionParseResult.error("Option '" + optionName + "' must use <name>=<value> format, not +/- prefix");
        }
        value = (first == '+');
    } else {
        if (eqIndex == -1) {
            optionName = option;
            valueString = null;
        } else {
            optionName = option.substring(0, eqIndex);
            valueString = option.substring(eqIndex + 1);
        }
    }
    OptionDescriptor desc = options.get(optionName);
    if (desc == null && value != null) {
        if (eqIndex != -1) {
            optionName = option.substring(1, eqIndex);
            desc = options.get(optionName);
        }
    }
    if (desc == null) {
        List<OptionDescriptor> matches = new ArrayList<>();
        OptionsParser.collectFuzzyMatches(options.values(), optionName, matches);
        StringBuilder msg = new StringBuilder("Could not find option '").append(optionName).append('\'');
        if (!matches.isEmpty()) {
            msg.append(". Did you mean one of these:");
            for (OptionDescriptor match : matches) {
                msg.append(' ').append(match.getName());
            }
        }
        msg.append(". Use " + optionPrefix + SubstrateOptions.PrintFlags.getName() + "= to list all available options.");
        return OptionParseResult.error(msg.toString());
    }
    Class<?> optionType = desc.getOptionValueType();
    if (value == null) {
        if (optionType == Boolean.class && booleanOptionFormat == BooleanOptionFormat.PLUS_MINUS) {
            return OptionParseResult.error("Boolean option '" + optionName + "' must have +/- prefix");
        }
        if (valueString == null) {
            return OptionParseResult.error("Missing value for option '" + optionName + "'");
        }
        try {
            if (optionType == Integer.class) {
                long longValue = parseLong(valueString);
                if ((int) longValue != longValue) {
                    return OptionParseResult.error("Wrong value for option '" + optionName + "': '" + valueString + "' is not a valid number");
                }
                value = (int) longValue;
            } else if (optionType == Long.class) {
                value = parseLong(valueString);
            } else if (optionType == String.class) {
                value = valueString;
            } else if (optionType == Double.class) {
                value = Double.parseDouble(valueString);
            } else if (optionType == Boolean.class) {
                if (valueString.equals("true")) {
                    value = true;
                } else if (valueString.equals("false")) {
                    value = false;
                } else {
                    return OptionParseResult.error("Boolean option '" + optionName + "' must have value 'true' or 'false'");
                }
            } else if (optionType == CompilationWrapper.ExceptionAction.class) {
                value = CompilationWrapper.ExceptionAction.valueOf(valueString);
            } else {
                throw VMError.shouldNotReachHere("Unsupported option value class: " + optionType.getSimpleName());
            }
        } catch (NumberFormatException ex) {
            return OptionParseResult.error("Invalid value for option '" + optionName + "': '" + valueString + "' is not a valid number");
        }
    } else {
        if (optionType != Boolean.class) {
            return OptionParseResult.error("Non-boolean option '" + optionName + "' can not use +/- prefix. Use '" + optionName + "=<value>' format");
        }
    }
    desc.getOptionKey().update(valuesMap, value);
    if (SubstrateOptions.PrintFlags.getName().equals(optionName)) {
        String optionValue = (String) value;
        EnumSet<OptionType> selectedOptionTypes;
        if (optionValue.isEmpty()) {
            selectedOptionTypes = EnumSet.allOf(OptionType.class);
        } else {
            selectedOptionTypes = EnumSet.noneOf(OptionType.class);
            String enumString = null;
            try {
                String[] enumStrings = optionValue.split(",");
                for (int i = 0; i < enumStrings.length; i++) {
                    enumString = enumStrings[i];
                    selectedOptionTypes.add(OptionType.valueOf(enumString));
                }
            } catch (IllegalArgumentException e) {
                String possibleValues = Arrays.stream(OptionType.values()).map(OptionType::name).collect(Collectors.joining(", ", "", ""));
                return OptionParseResult.error("Invalid value for option '" + optionName + ". " + enumString + "' is not one of: " + possibleValues);
            }
        }
        return OptionParseResult.printFlags(selectedOptionTypes);
    }
    return OptionParseResult.correct();
}
Also used : OptionDescriptor(org.graalvm.compiler.options.OptionDescriptor) ArrayList(java.util.ArrayList) OptionType(org.graalvm.compiler.options.OptionType)

Aggregations

OptionType (org.graalvm.compiler.options.OptionType)2 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Filer (javax.annotation.processing.Filer)1 Name (javax.lang.model.element.Name)1 OptionDescriptor (org.graalvm.compiler.options.OptionDescriptor)1 OptionDescriptors (org.graalvm.compiler.options.OptionDescriptors)1