001    /*
002     * Copyright 2010-2015 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.kotlin.codegen.context;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.codegen.ExpressionCodegen;
022    import org.jetbrains.kotlin.codegen.StackValue;
023    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
024    import org.jetbrains.org.objectweb.asm.Type;
025    
026    public final class EnclosedValueDescriptor {
027        private final String fieldName;
028        private final DeclarationDescriptor descriptor;
029        private final StackValue.StackValueWithSimpleReceiver innerValue;
030        private final Type type;
031    
032        public EnclosedValueDescriptor(
033                @NotNull String fieldName,
034                @Nullable DeclarationDescriptor descriptor,
035                @NotNull StackValue.StackValueWithSimpleReceiver innerValue,
036                @NotNull Type type
037        ) {
038            this.fieldName = fieldName;
039            this.descriptor = descriptor;
040            this.innerValue = innerValue;
041            this.type = type;
042        }
043    
044        @NotNull
045        public String getFieldName() {
046            return fieldName;
047        }
048    
049        @Nullable
050        public DeclarationDescriptor getDescriptor() {
051            return descriptor;
052        }
053    
054        @NotNull
055        public StackValue.StackValueWithSimpleReceiver getInnerValue() {
056            return innerValue;
057        }
058    
059        @NotNull
060        public Type getType() {
061            return type;
062        }
063    
064        @NotNull
065        public StackValue getOuterValue(@NotNull ExpressionCodegen codegen) {
066            for (LocalLookup.LocalLookupCase aCase : LocalLookup.LocalLookupCase.values()) {
067                if (aCase.isCase(descriptor)) {
068                    return aCase.outerValue(this, codegen);
069                }
070            }
071    
072            throw new IllegalStateException("Can't get outer value in " + codegen + " for " + this);
073        }
074    
075        @Override
076        public String toString() {
077            return fieldName + " " + type + " -> " + descriptor;
078        }
079    }