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