001 /*
002 * Copyright 2010-2014 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.jet.codegen.when;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.codegen.ExpressionCodegen;
021 import org.jetbrains.jet.lang.psi.JetWhenExpression;
022 import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
023 import org.jetbrains.jet.lang.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 JetWhenExpression expression,
032 boolean isStatement,
033 @NotNull ExpressionCodegen codegen,
034 @NotNull WhenByEnumsMapping mapping
035 ) {
036 super(expression, isStatement, codegen);
037 this.mapping = mapping;
038 }
039
040 @Override
041 protected void generateSubject() {
042 codegen.getState().getMappingsClassesForWhenByEnum().generateMappingsClassForExpression(expression);
043
044 super.generateSubject();
045 generateNullCheckIfNeeded();
046
047 v.getstatic(
048 mapping.getMappingsClassInternalName(),
049 mapping.getFieldName(),
050 MappingClassesForWhenByEnumCodegen.MAPPINGS_FIELD_DESCRIPTOR
051 );
052
053 v.swap();
054
055 Type enumType = codegen.getState().getTypeMapper().mapClass(mapping.getEnumClassDescriptor());
056 v.invokevirtual(enumType.getInternalName(), "ordinal", Type.getMethodDescriptor(Type.INT_TYPE), false);
057 v.aload(Type.INT_TYPE);
058 }
059
060 @Override
061 protected void processConstant(@NotNull CompileTimeConstant constant, @NotNull Label entryLabel) {
062 assert constant instanceof EnumValue : "guaranteed by usage contract";
063 putTransitionOnce(mapping.getIndexByEntry((EnumValue) constant), entryLabel);
064 }
065 }