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.binding.MutableClosure;
023    import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants;
024    import org.jetbrains.jet.codegen.OwnerKind;
025    import org.jetbrains.jet.codegen.StackValue;
026    import org.jetbrains.jet.codegen.state.GenerationState;
027    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
028    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
029    import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
030    import org.jetbrains.jet.lang.descriptors.PropertyAccessorDescriptor;
031    
032    public class MethodContext extends CodegenContext {
033    
034        private Label methodStartLabel;
035        
036        public MethodContext(
037                @NotNull FunctionDescriptor contextType,
038                @NotNull OwnerKind contextKind,
039                @NotNull CodegenContext parentContext
040        ) {
041            this(contextType, contextKind, parentContext, null);
042        }
043    
044        protected MethodContext(
045                @NotNull FunctionDescriptor contextType,
046                @NotNull OwnerKind contextKind,
047                @NotNull CodegenContext parentContext,
048                @Nullable MutableClosure closure
049        ) {
050            super(contextType instanceof PropertyAccessorDescriptor
051                  ? ((PropertyAccessorDescriptor) contextType).getCorrespondingProperty()
052                  : contextType, contextKind, parentContext, closure,
053                  parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null);
054        }
055    
056        @Override
057        public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) {
058            if (getContextDescriptor() == d) {
059                return result != null ? result : StackValue.local(0, AsmTypeConstants.OBJECT_TYPE);
060            }
061    
062            //noinspection ConstantConditions
063            return getParentContext().lookupInContext(d, result, state, ignoreNoOuter);
064        }
065    
066        @Override
067        public boolean isStatic() {
068            //noinspection ConstantConditions
069            return getParentContext().isStatic();
070        }
071    
072        @Override
073        public StackValue getOuterExpression(StackValue prefix, boolean ignoreNoOuter) {
074            //noinspection ConstantConditions
075            return getParentContext().getOuterExpression(prefix, false);
076        }
077    
078        @Nullable
079        public Label getMethodStartLabel() {
080            return methodStartLabel;
081        }
082    
083        public void setMethodStartLabel(@NotNull Label methodStartLabel) {
084            this.methodStartLabel = methodStartLabel;
085        }
086    
087        @Override
088        public String toString() {
089            return "Method: " + getContextDescriptor();
090        }
091    
092    }