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