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