001 /* 002 * Copyright 2010-2013 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.context; 018 019 import org.jetbrains.annotations.NotNull; 020 import org.jetbrains.annotations.Nullable; 021 import org.jetbrains.jet.codegen.AsmUtil; 022 import org.jetbrains.jet.codegen.JvmCodegenUtil; 023 import org.jetbrains.jet.codegen.OwnerKind; 024 import org.jetbrains.jet.codegen.StackValue; 025 import org.jetbrains.jet.codegen.binding.MutableClosure; 026 import org.jetbrains.jet.codegen.state.GenerationState; 027 import org.jetbrains.jet.codegen.state.JetTypeMapper; 028 import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor; 029 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; 030 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; 031 import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor; 032 import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants; 033 import org.jetbrains.org.objectweb.asm.Label; 034 import org.jetbrains.org.objectweb.asm.Type; 035 036 public class MethodContext extends CodegenContext<CallableMemberDescriptor> { 037 private final boolean isInliningLambda; 038 private Label methodStartLabel; 039 040 protected MethodContext( 041 @NotNull FunctionDescriptor contextDescriptor, 042 @NotNull OwnerKind contextKind, 043 @NotNull CodegenContext parentContext, 044 @Nullable MutableClosure closure, 045 boolean isInliningLambda 046 ) { 047 super(JvmCodegenUtil.getDirectMember(contextDescriptor), contextKind, parentContext, closure, 048 parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null); 049 this.isInliningLambda = isInliningLambda; 050 } 051 052 @NotNull 053 @Override 054 public CodegenContext getParentContext() { 055 //noinspection ConstantConditions 056 return super.getParentContext(); 057 } 058 059 public StackValue getReceiverExpression(JetTypeMapper typeMapper) { 060 assert getCallableDescriptorWithReceiver() != null; 061 @SuppressWarnings("ConstantConditions") 062 Type asmType = typeMapper.mapType(getCallableDescriptorWithReceiver().getReceiverParameter().getType()); 063 return StackValue.local(AsmUtil.getReceiverIndex(this, getContextDescriptor()), asmType); 064 } 065 066 @Override 067 public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) { 068 if (getContextDescriptor() == d) { 069 return result != null ? result : StackValue.local(0, AsmTypeConstants.OBJECT_TYPE); 070 } 071 072 return getParentContext().lookupInContext(d, result, state, ignoreNoOuter); 073 } 074 075 @Override 076 public boolean isStatic() { 077 return getParentContext().isStatic(); 078 } 079 080 @Override 081 public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) { 082 return getParentContext().getOuterExpression(prefix, false); 083 } 084 085 @Nullable 086 public Label getMethodStartLabel() { 087 return methodStartLabel; 088 } 089 090 public void setMethodStartLabel(@NotNull Label methodStartLabel) { 091 this.methodStartLabel = methodStartLabel; 092 } 093 094 @Override 095 public String toString() { 096 return "Method: " + getContextDescriptor(); 097 } 098 099 public boolean isInlineFunction() { 100 DeclarationDescriptor descriptor = getContextDescriptor(); 101 if (descriptor instanceof SimpleFunctionDescriptor) { 102 return ((SimpleFunctionDescriptor) descriptor).getInlineStrategy().isInline(); 103 } 104 return false; 105 } 106 107 public boolean isInliningLambda() { 108 return isInliningLambda; 109 } 110 111 public boolean isSpecialStackValue(StackValue stackValue) { 112 if (isInliningLambda && stackValue instanceof StackValue.Composed) { 113 StackValue prefix = ((StackValue.Composed) stackValue).prefix; 114 StackValue suffix = ((StackValue.Composed) stackValue).suffix; 115 if (prefix instanceof StackValue.Local && ((StackValue.Local) prefix).index == 0) { 116 return suffix instanceof StackValue.Field; 117 } 118 } 119 return false; 120 } 121 }