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;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.asm4.MethodVisitor;
022    import org.jetbrains.asm4.Type;
023    import org.jetbrains.jet.codegen.context.MethodContext;
024    import org.jetbrains.jet.codegen.signature.JvmMethodSignature;
025    import org.jetbrains.jet.codegen.state.GenerationState;
026    import org.jetbrains.jet.codegen.state.JetTypeMapper;
027    import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
028    import org.jetbrains.jet.lang.psi.JetDeclarationWithBody;
029    
030    import java.util.ArrayList;
031    import java.util.Collection;
032    
033    public abstract class FunctionGenerationStrategy {
034    
035        private final Collection<String> localVariableNames = new ArrayList<String>();
036    
037        private FrameMap frameMap;
038    
039        public abstract void generateBody(
040                @NotNull MethodVisitor mv,
041                @NotNull JvmMethodSignature signature,
042                @NotNull MethodContext context,
043                @Nullable MemberCodegen parentCodegen
044        );
045    
046        protected void addLocalVariableName(@NotNull String name) {
047            localVariableNames.add(name);
048        }
049    
050        @NotNull
051        public Collection<String> getLocalVariableNames() {
052            return localVariableNames;
053        }
054    
055        @NotNull
056        protected FrameMap createFrameMap(@NotNull JetTypeMapper typeMapper, @NotNull MethodContext context) {
057            return context.prepareFrame(typeMapper);
058        }
059    
060        @NotNull
061        public FrameMap getFrameMap(@NotNull JetTypeMapper typeMapper, @NotNull MethodContext context) {
062            if (frameMap == null) {
063                frameMap = createFrameMap(typeMapper, context);
064            }
065            return frameMap;
066        }
067    
068        public static class FunctionDefault extends CodegenBased<CallableDescriptor> {
069    
070            private final JetDeclarationWithBody declaration;
071    
072            public FunctionDefault(
073                    @NotNull GenerationState state,
074                    @NotNull CallableDescriptor descriptor,
075                    @NotNull JetDeclarationWithBody declaration
076            ) {
077                super(state, descriptor);
078                this.declaration = declaration;
079            }
080    
081            @Override
082            public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
083                codegen.returnExpression(declaration.getBodyExpression());
084            }
085        }
086    
087        public abstract static class CodegenBased<T extends CallableDescriptor> extends FunctionGenerationStrategy {
088    
089            protected final GenerationState state;
090    
091            protected final T callableDescriptor;
092    
093            public CodegenBased(@NotNull GenerationState state, T callableDescriptor) {
094                this.state = state;
095                this.callableDescriptor = callableDescriptor;
096            }
097    
098            @Override
099            public void generateBody(
100                    @NotNull MethodVisitor mv,
101                    @NotNull JvmMethodSignature signature,
102                    @NotNull MethodContext context,
103                    @Nullable MemberCodegen parentCodegen
104            ) {
105                ExpressionCodegen codegen = initializeExpressionCodegen(signature, context, mv, signature.getAsmMethod().getReturnType(), parentCodegen);
106                doGenerateBody(codegen, signature);
107                generateLocalVarNames(codegen);
108            }
109    
110            abstract public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature);
111    
112            @NotNull
113            public ExpressionCodegen initializeExpressionCodegen(
114                    JvmMethodSignature signature,
115                    MethodContext context,
116                    MethodVisitor mv,
117                    Type returnType,
118                    MemberCodegen parentCodegen
119            ) {
120                return new ExpressionCodegen(mv, getFrameMap(state.getTypeMapper(), context), returnType, context, state, parentCodegen);
121            }
122    
123            public void generateLocalVarNames(@NotNull ExpressionCodegen codegen) {
124                for (String name : codegen.getLocalVariableNamesForExpression()) {
125                    addLocalVariableName(name);
126                }
127            }
128        }
129    }