001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.codegen.when;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.codegen.ExpressionCodegen;
021    import org.jetbrains.kotlin.psi.KtWhenExpression;
022    import org.jetbrains.kotlin.resolve.constants.ConstantValue;
023    import org.jetbrains.kotlin.resolve.constants.EnumValue;
024    import org.jetbrains.org.objectweb.asm.Label;
025    import org.jetbrains.org.objectweb.asm.Type;
026    
027    public class EnumSwitchCodegen extends SwitchCodegen {
028        private final WhenByEnumsMapping mapping;
029    
030        public EnumSwitchCodegen(
031                @NotNull KtWhenExpression expression,
032                boolean isStatement,
033                boolean isExhaustive,
034                @NotNull ExpressionCodegen codegen,
035                @NotNull WhenByEnumsMapping mapping
036        ) {
037            super(expression, isStatement, isExhaustive, codegen);
038            this.mapping = mapping;
039        }
040    
041        @Override
042        protected void generateSubject() {
043            codegen.getState().getMappingsClassesForWhenByEnum().generateMappingsClassForExpression(expression);
044    
045            super.generateSubject();
046            generateNullCheckIfNeeded();
047    
048            v.getstatic(
049                    mapping.getMappingsClassInternalName(),
050                    mapping.getFieldName(),
051                    MappingClassesForWhenByEnumCodegen.MAPPINGS_FIELD_DESCRIPTOR
052            );
053    
054            v.swap();
055    
056            Type enumType = codegen.getState().getTypeMapper().mapClass(mapping.getEnumClassDescriptor());
057            v.invokevirtual(enumType.getInternalName(), "ordinal", Type.getMethodDescriptor(Type.INT_TYPE), false);
058            v.aload(Type.INT_TYPE);
059        }
060    
061        @Override
062        protected void processConstant(@NotNull ConstantValue<?> constant, @NotNull Label entryLabel) {
063            assert constant instanceof EnumValue : "guaranteed by usage contract";
064            putTransitionOnce(mapping.getIndexByEntry((EnumValue) constant), entryLabel);
065        }
066    }