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