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.context;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.codegen.AsmUtil;
022 import org.jetbrains.kotlin.codegen.JvmCodegenUtil;
023 import org.jetbrains.kotlin.codegen.OwnerKind;
024 import org.jetbrains.kotlin.codegen.StackValue;
025 import org.jetbrains.kotlin.codegen.binding.MutableClosure;
026 import org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenUtilKt;
027 import org.jetbrains.kotlin.codegen.state.GenerationState;
028 import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
029 import org.jetbrains.kotlin.descriptors.*;
030 import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor;
031 import org.jetbrains.kotlin.resolve.inline.InlineUtil;
032 import org.jetbrains.org.objectweb.asm.Label;
033 import org.jetbrains.org.objectweb.asm.Type;
034
035 public class MethodContext extends CodegenContext<CallableMemberDescriptor> {
036 private Label methodStartLabel;
037 private Label methodEndLabel;
038
039 // Note: in case of code inside property accessors, functionDescriptor will be that accessor,
040 // but CodegenContext#contextDescriptor will be the corresponding property
041 private final FunctionDescriptor functionDescriptor;
042 private final boolean isDefaultFunctionContext;
043
044 protected MethodContext(
045 @NotNull FunctionDescriptor functionDescriptor,
046 @NotNull OwnerKind contextKind,
047 @NotNull CodegenContext parentContext,
048 @Nullable MutableClosure closure,
049 boolean isDefaultFunctionContext
050 ) {
051 super(JvmCodegenUtil.getDirectMember(functionDescriptor), contextKind, parentContext, closure,
052 parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null);
053 this.functionDescriptor = functionDescriptor;
054 this.isDefaultFunctionContext = isDefaultFunctionContext;
055 }
056
057 @NotNull
058 @Override
059 public CodegenContext getParentContext() {
060 //noinspection ConstantConditions
061 return super.getParentContext();
062 }
063
064 public StackValue getReceiverExpression(KotlinTypeMapper typeMapper) {
065 assert getCallableDescriptorWithReceiver() != null;
066 @SuppressWarnings("ConstantConditions")
067 Type asmType = typeMapper.mapType(getCallableDescriptorWithReceiver().getExtensionReceiverParameter().getType());
068 return StackValue.local(AsmUtil.getReceiverIndex(this, getContextDescriptor()), asmType);
069 }
070
071 @Override
072 public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) {
073 if (d instanceof SyntheticFieldDescriptor) {
074 SyntheticFieldDescriptor fieldDescriptor = (SyntheticFieldDescriptor) d;
075 d = fieldDescriptor.getPropertyDescriptor();
076 }
077 if (getContextDescriptor() == d) {
078 return result != null ? result : StackValue.LOCAL_0;
079 }
080
081 return getParentContext().lookupInContext(d, result, state, ignoreNoOuter);
082 }
083
084 @Nullable
085 public StackValue generateReceiver(@NotNull CallableDescriptor descriptor, @NotNull GenerationState state, boolean ignoreNoOuter) {
086 // When generating bytecode of some suspend function, we replace the original descriptor with one that reflects how it should look on JVM.
087 // But when we looking for receiver parameter in resolved call, it still references the initial function, so we unwrap it here
088 // before comparision.
089 if (CoroutineCodegenUtilKt.unwrapInitialDescriptorForSuspendFunction(getCallableDescriptorWithReceiver()) == descriptor) {
090 return getReceiverExpression(state.getTypeMapper());
091 }
092 ReceiverParameterDescriptor parameter = descriptor.getExtensionReceiverParameter();
093 return lookupInContext(parameter, StackValue.LOCAL_0, state, ignoreNoOuter);
094 }
095
096 @Override
097 public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) {
098 return getParentContext().getOuterExpression(prefix, false);
099 }
100
101 @Nullable
102 public Label getMethodStartLabel() {
103 return methodStartLabel;
104 }
105
106 public void setMethodStartLabel(@NotNull Label methodStartLabel) {
107 this.methodStartLabel = methodStartLabel;
108 }
109
110 @Nullable
111 public Label getMethodEndLabel() {
112 return methodEndLabel;
113 }
114
115 public void setMethodEndLabel(@NotNull Label methodEndLabel) {
116 this.methodEndLabel = methodEndLabel;
117 }
118
119 @Override
120 public String toString() {
121 return "Method: " + getContextDescriptor();
122 }
123
124 public boolean isInlineMethodContext() {
125 return InlineUtil.isInline(getFunctionDescriptor());
126 }
127
128 @NotNull
129 public FunctionDescriptor getFunctionDescriptor() {
130 return functionDescriptor;
131 }
132
133 public boolean isDefaultFunctionContext() {
134 return isDefaultFunctionContext;
135 }
136 }