use of org.jetbrains.kotlin.resolve.constants.EnumValue in project kotlin by JetBrains.
the class CodegenAnnotatingVisitor method visitWhenExpression.
@Override
public void visitWhenExpression(@NotNull KtWhenExpression expression) {
super.visitWhenExpression(expression);
if (!isWhenWithEnums(expression))
return;
String currentClassName = getCurrentTopLevelClassOrPackagePartInternalName(expression.getContainingKtFile());
if (bindingContext.get(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName) == null) {
bindingTrace.record(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName, new ArrayList<WhenByEnumsMapping>(1));
}
List<WhenByEnumsMapping> mappings = bindingContext.get(MAPPINGS_FOR_WHENS_BY_ENUM_IN_CLASS_FILE, currentClassName);
assert mappings != null : "guaranteed by contract";
int fieldNumber = mappings.size();
assert expression.getSubjectExpression() != null : "subject expression should be not null in a valid when by enums";
KotlinType type = WhenChecker.whenSubjectType(expression, bindingContext);
assert type != null : "should not be null in a valid when by enums";
ClassDescriptor classDescriptor = (ClassDescriptor) type.getConstructor().getDeclarationDescriptor();
assert classDescriptor != null : "because it's enum";
WhenByEnumsMapping mapping = new WhenByEnumsMapping(classDescriptor, currentClassName, fieldNumber);
for (ConstantValue<?> constant : SwitchCodegenUtil.getAllConstants(expression, bindingContext, shouldInlineConstVals)) {
if (constant instanceof NullValue)
continue;
assert constant instanceof EnumValue : "expression in when should be EnumValue";
mapping.putFirstTime((EnumValue) constant, mapping.size() + 1);
}
mappings.add(mapping);
bindingTrace.record(MAPPING_FOR_WHEN_BY_ENUM, expression, mapping);
}
use of org.jetbrains.kotlin.resolve.constants.EnumValue in project kotlin by JetBrains.
the class MappingClassesForWhenByEnumCodegen method generateInitializationForMapping.
private void generateInitializationForMapping(@NotNull ClassBuilder cb, @NotNull InstructionAdapter v, @NotNull WhenByEnumsMapping mapping) {
Type enumType = state.getTypeMapper().mapClass(mapping.getEnumClassDescriptor());
v.invokestatic(enumType.getInternalName(), "values", Type.getMethodDescriptor(Type.getType("[" + enumType.getDescriptor())), false);
v.arraylength();
v.newarray(Type.INT_TYPE);
v.putstatic(cb.getThisName(), mapping.getFieldName(), MAPPINGS_FIELD_DESCRIPTOR);
for (Map.Entry<EnumValue, Integer> item : mapping.enumValuesToIntMapping()) {
EnumValue enumEntry = item.getKey();
int mappedValue = item.getValue();
v.getstatic(cb.getThisName(), mapping.getFieldName(), MAPPINGS_FIELD_DESCRIPTOR);
v.getstatic(enumType.getInternalName(), enumEntry.getValue().getName().asString(), enumType.getDescriptor());
v.invokevirtual(enumType.getInternalName(), "ordinal", Type.getMethodDescriptor(Type.INT_TYPE), false);
v.iconst(mappedValue);
v.astore(Type.INT_TYPE);
}
}
Aggregations