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