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.kotlin.codegen.ExpressionCodegen;
021    import org.jetbrains.kotlin.codegen.JvmCodegenUtil;
022    import org.jetbrains.kotlin.codegen.StackValue;
023    import org.jetbrains.kotlin.codegen.binding.MutableClosure;
024    import org.jetbrains.kotlin.codegen.state.GenerationState;
025    import org.jetbrains.kotlin.descriptors.*;
026    import org.jetbrains.kotlin.load.java.JvmAbi;
027    import org.jetbrains.kotlin.resolve.BindingContext;
028    import org.jetbrains.kotlin.types.JetType;
029    import org.jetbrains.org.objectweb.asm.Type;
030    
031    import static org.jetbrains.kotlin.codegen.AsmUtil.CAPTURED_RECEIVER_FIELD;
032    import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.*;
033    import static org.jetbrains.kotlin.resolve.DescriptorUtils.isLocalFunction;
034    
035    public interface LocalLookup {
036        boolean lookupLocal(DeclarationDescriptor descriptor);
037    
038        enum LocalLookupCase {
039            VAR {
040                @Override
041                public boolean isCase(DeclarationDescriptor d) {
042                    return d instanceof VariableDescriptor && !(d instanceof PropertyDescriptor);
043                }
044    
045                @Override
046                public StackValue.StackValueWithSimpleReceiver innerValue(
047                        DeclarationDescriptor d,
048                        LocalLookup localLookup,
049                        GenerationState state,
050                        MutableClosure closure,
051                        Type classType
052                ) {
053                    VariableDescriptor vd = (VariableDescriptor) d;
054    
055                    boolean idx = localLookup != null && localLookup.lookupLocal(vd);
056                    if (!idx) return null;
057    
058                    Type sharedVarType = state.getTypeMapper().getSharedVarType(vd);
059                    Type localType = state.getTypeMapper().mapType(vd);
060                    Type type = sharedVarType != null ? sharedVarType : localType;
061    
062                    String fieldName = "$" + vd.getName();
063                    StackValue.Local thiz = StackValue.LOCAL_0;
064                    StackValue.StackValueWithSimpleReceiver innerValue = sharedVarType != null
065                                            ? StackValue.fieldForSharedVar(localType, classType, fieldName, thiz, vd)
066                                            : StackValue.field(type, classType, fieldName, false, thiz, vd);
067    
068                    closure.recordField(fieldName, type);
069                    closure.captureVariable(new EnclosedValueDescriptor(fieldName, d, innerValue, type));
070    
071                    return innerValue;
072                }
073            },
074    
075            LOCAL_NAMED_FUNCTION {
076                @Override
077                public boolean isCase(DeclarationDescriptor d) {
078                    return isLocalFunction(d);
079                }
080    
081                @Override
082                public StackValue.StackValueWithSimpleReceiver innerValue(
083                        DeclarationDescriptor d,
084                        LocalLookup localLookup,
085                        GenerationState state,
086                        MutableClosure closure,
087                        Type classType
088                ) {
089                    FunctionDescriptor vd = (FunctionDescriptor) d;
090    
091                    boolean idx = localLookup != null && localLookup.lookupLocal(vd);
092                    if (!idx) return null;
093    
094                    BindingContext bindingContext = state.getBindingContext();
095                    Type localType = asmTypeForAnonymousClass(bindingContext, vd);
096    
097                    MutableClosure localFunClosure = bindingContext.get(CLOSURE, bindingContext.get(CLASS_FOR_FUNCTION, vd));
098                    if (localFunClosure != null && JvmCodegenUtil.isConst(localFunClosure)) {
099                        // This is an optimization: we can obtain an instance of a const closure simply by GETSTATIC ...$instance
100                        // (instead of passing this instance to the constructor and storing as a field)
101                        return StackValue.field(localType, localType, JvmAbi.INSTANCE_FIELD, true, StackValue.LOCAL_0, vd);
102                    }
103    
104                    String fieldName = "$" + vd.getName();
105                    StackValue.StackValueWithSimpleReceiver innerValue = StackValue.field(localType, classType, fieldName, false,
106                                                                                          StackValue.LOCAL_0, vd);
107    
108                    closure.recordField(fieldName, localType);
109                    closure.captureVariable(new EnclosedValueDescriptor(fieldName, d, innerValue, localType));
110    
111                    return innerValue;
112                }
113            },
114    
115            RECEIVER {
116                @Override
117                public boolean isCase(DeclarationDescriptor d) {
118                    return d instanceof ReceiverParameterDescriptor;
119                }
120    
121                @Override
122                public StackValue.StackValueWithSimpleReceiver innerValue(
123                        DeclarationDescriptor d,
124                        LocalLookup enclosingLocalLookup,
125                        GenerationState state,
126                        MutableClosure closure,
127                        Type classType
128                ) {
129                    if (closure.getEnclosingReceiverDescriptor() != d) {
130                        return null;
131                    }
132    
133                    JetType receiverType = closure.getEnclosingReceiverDescriptor().getType();
134                    Type type = state.getTypeMapper().mapType(receiverType);
135                    StackValue.StackValueWithSimpleReceiver innerValue = StackValue.field(type, classType, CAPTURED_RECEIVER_FIELD, false,
136                                                                                          StackValue.LOCAL_0, d);
137                    closure.setCaptureReceiver();
138    
139                    return innerValue;
140                }
141    
142                @NotNull
143                @Override
144                public StackValue outerValue(@NotNull EnclosedValueDescriptor d, @NotNull ExpressionCodegen codegen) {
145                    CallableDescriptor descriptor = (CallableDescriptor) d.getDescriptor();
146                    return StackValue.local(descriptor.getDispatchReceiverParameter() != null ? 1 : 0, d.getType());
147                }
148            };
149    
150            public abstract boolean isCase(DeclarationDescriptor d);
151    
152            public abstract StackValue.StackValueWithSimpleReceiver innerValue(
153                    DeclarationDescriptor d,
154                    LocalLookup localLookup,
155                    GenerationState state,
156                    MutableClosure closure,
157                    Type classType
158            );
159    
160            @NotNull
161            public StackValue outerValue(@NotNull EnclosedValueDescriptor d, @NotNull ExpressionCodegen codegen) {
162                int idx = codegen.lookupLocalIndex(d.getDescriptor());
163                assert idx != -1;
164    
165                return StackValue.local(idx, d.getType());
166            }
167        }
168    }