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.*; 029 import org.jetbrains.org.objectweb.asm.Label; 030 import org.jetbrains.org.objectweb.asm.Type; 031 032 public class MethodContext extends CodegenContext<CallableMemberDescriptor> { 033 private final boolean isInliningLambda; 034 private Label methodStartLabel; 035 private Label methodEndLabel; 036 037 protected MethodContext( 038 @NotNull FunctionDescriptor contextDescriptor, 039 @NotNull OwnerKind contextKind, 040 @NotNull CodegenContext parentContext, 041 @Nullable MutableClosure closure, 042 boolean isInliningLambda 043 ) { 044 super(JvmCodegenUtil.getDirectMember(contextDescriptor), contextKind, parentContext, closure, 045 parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null); 046 this.isInliningLambda = isInliningLambda; 047 } 048 049 @NotNull 050 @Override 051 public CodegenContext getParentContext() { 052 //noinspection ConstantConditions 053 return super.getParentContext(); 054 } 055 056 public StackValue getReceiverExpression(JetTypeMapper typeMapper) { 057 assert getCallableDescriptorWithReceiver() != null; 058 @SuppressWarnings("ConstantConditions") 059 Type asmType = typeMapper.mapType(getCallableDescriptorWithReceiver().getExtensionReceiverParameter().getType()); 060 return StackValue.local(AsmUtil.getReceiverIndex(this, getContextDescriptor()), asmType); 061 } 062 063 @Override 064 public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) { 065 if (getContextDescriptor() == d) { 066 return result != null ? result : StackValue.LOCAL_0; 067 } 068 069 return getParentContext().lookupInContext(d, result, state, ignoreNoOuter); 070 } 071 072 @Nullable 073 public StackValue generateReceiver(@NotNull CallableDescriptor descriptor, @NotNull GenerationState state, boolean ignoreNoOuter) { 074 if (getCallableDescriptorWithReceiver() == descriptor) { 075 return getReceiverExpression(state.getTypeMapper()); 076 } 077 ReceiverParameterDescriptor parameter = descriptor.getExtensionReceiverParameter(); 078 return lookupInContext(parameter, StackValue.LOCAL_0, state, ignoreNoOuter); 079 } 080 081 @Override 082 public boolean isStatic() { 083 return getParentContext().isStatic(); 084 } 085 086 @Override 087 public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) { 088 return getParentContext().getOuterExpression(prefix, false); 089 } 090 091 @Nullable 092 public Label getMethodStartLabel() { 093 return methodStartLabel; 094 } 095 096 public void setMethodStartLabel(@NotNull Label methodStartLabel) { 097 this.methodStartLabel = methodStartLabel; 098 } 099 100 @Nullable 101 public Label getMethodEndLabel() { 102 return methodEndLabel; 103 } 104 105 public void setMethodEndLabel(@NotNull Label methodEndLabel) { 106 this.methodEndLabel = methodEndLabel; 107 } 108 109 @Override 110 public String toString() { 111 return "Method: " + getContextDescriptor(); 112 } 113 114 public boolean isInlineFunction() { 115 DeclarationDescriptor descriptor = getContextDescriptor(); 116 if (descriptor instanceof SimpleFunctionDescriptor) { 117 return ((SimpleFunctionDescriptor) descriptor).getInlineStrategy().isInline(); 118 } 119 return false; 120 } 121 122 public boolean isInliningLambda() { 123 return isInliningLambda; 124 } 125 126 }