package org.graalvm.compiler.options;
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Formatter;
import java.util.List;
import java.util.ServiceLoader;
import jdk.internal.vm.compiler.collections.EconomicMap;
import jdk.internal.vm.compiler.collections.MapCursor;
public class OptionsParser {
private static volatile List<OptionDescriptors> cachedOptionDescriptors;
public static Iterable<OptionDescriptors> getOptionsLoader() {
if (IS_IN_NATIVE_IMAGE || cachedOptionDescriptors != null) {
return cachedOptionDescriptors;
}
return ModuleSupport.getOptionsLoader();
}
public static void setCachedOptionDescriptors(List<OptionDescriptors> list) {
assert IS_BUILDING_NATIVE_IMAGE : "Used to pre-initialize the option descriptors during native image generation";
OptionsParser.cachedOptionDescriptors = list;
}
public static void parseOptions(EconomicMap<String, String> optionSettings, EconomicMap<OptionKey<?>, Object> values, Iterable<OptionDescriptors> loader) {
if (optionSettings != null && !optionSettings.isEmpty()) {
MapCursor<String, String> cursor = optionSettings.getEntries();
while (cursor.advance()) {
parseOption(cursor.getKey(), cursor.getValue(), values, loader);
}
}
}
public static void parseOptionSettingTo(String optionSetting, EconomicMap<String, String> dst) {
int eqIndex = optionSetting.indexOf('=');
if (eqIndex == -1) {
throw new InternalError("Option setting has does not match the pattern <name>=<value>: " + optionSetting);
}
dst.put(optionSetting.substring(0, eqIndex), optionSetting.substring(eqIndex + 1));
}
private static OptionDescriptor lookup(Iterable<OptionDescriptors> loader, String name) {
for (OptionDescriptors optionDescriptors : loader) {
OptionDescriptor desc = optionDescriptors.get(name);
if (desc != null) {
return desc;
}
}
return null;
}
public static void parseOption(String name, Object uncheckedValue, EconomicMap<OptionKey<?>, Object> values, Iterable<OptionDescriptors> loader) {
OptionDescriptor desc = lookup(loader, name);
if (desc == null) {
List<OptionDescriptor> matches = fuzzyMatch(loader, name);
Formatter msg = new Formatter();
msg.format("Could not find option %s", name);
if (!matches.isEmpty()) {
msg.format("%nDid you mean one of the following?");
for (OptionDescriptor match : matches) {
msg.format("%n %s=<value>", match.getName());
}
}
throw new IllegalArgumentException(msg.toString());
}
Object value = parseOptionValue(desc, uncheckedValue);
desc.getOptionKey().update(values, value);
}
public static Object parseOptionValue(OptionDescriptor desc, Object uncheckedValue) {
Class<?> optionType = desc.getOptionValueType();
Object value;
if (!(uncheckedValue instanceof String)) {
if (optionType != uncheckedValue.getClass()) {
String type = optionType.getSimpleName();
throw new IllegalArgumentException(type + " option '" + desc.getName() + "' must have " + type + " value, not " + uncheckedValue.getClass() + " [toString: " + uncheckedValue + "]");
}
value = uncheckedValue;
} else {
String valueString = (String) uncheckedValue;
if (optionType == Boolean.class) {
if ("true".equals(valueString)) {
value = Boolean.TRUE;
} else if ("false".equals(valueString)) {
value = Boolean.FALSE;
} else {
throw new IllegalArgumentException("Boolean option '" + desc.getName() + "' must have value \"true\" or \"false\", not \"" + uncheckedValue + "\"");
}
} else if (optionType == String.class) {
value = valueString;
} else if (Enum.class.isAssignableFrom(optionType)) {
value = ((EnumOptionKey<?>) desc.getOptionKey()).valueOf(valueString);
} else {
if (valueString.isEmpty()) {
throw new IllegalArgumentException("Non empty value required for option '" + desc.getName() + "'");
}
try {
if (optionType == Float.class) {
value = Float.parseFloat(valueString);
} else if (optionType == Double.class) {
value = Double.parseDouble(valueString);
} else if (optionType == Integer.class) {
value = Integer.valueOf((int) parseLong(valueString));
} else if (optionType == Long.class) {
value = Long.valueOf(parseLong(valueString));
} else {
throw new IllegalArgumentException("Wrong value for option '" + desc.getName() + "'");
}
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Value for option '" + desc.getName() + "' has invalid number format: " + valueString);
}
}
}
return value;
}
private static long parseLong(String v) {
String valueString = v.toLowerCase();
long scale = 1;
if (valueString.endsWith("k")) {
scale = 1024L;
} else if (valueString.endsWith("m")) {
scale = 1024L * 1024L;
} else if (valueString.endsWith("g")) {
scale = 1024L * 1024L * 1024L;
} else if (valueString.endsWith("t")) {
scale = 1024L * 1024L * 1024L * 1024L;
}
if (scale != 1) {
valueString = valueString.substring(0, valueString.length() - 1);
}
return Long.parseLong(valueString) * scale;
}
static float stringSimiliarity(String str1, String str2) {
int hit = 0;
for (int i = 0; i < str1.length() - 1; ++i) {
for (int j = 0; j < str2.length() - 1; ++j) {
if ((str1.charAt(i) == str2.charAt(j)) && (str1.charAt(i + 1) == str2.charAt(j + 1))) {
++hit;
break;
}
}
}
return 2.0f * hit / (str1.length() + str2.length());
}
private static final float FUZZY_MATCH_THRESHOLD = 0.7F;