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