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;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.codegen.context.MethodContext;
021    import org.jetbrains.kotlin.codegen.state.GenerationState;
022    import org.jetbrains.kotlin.descriptors.CallableDescriptor;
023    import org.jetbrains.kotlin.psi.KtDeclarationWithBody;
024    import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature;
025    import org.jetbrains.org.objectweb.asm.MethodVisitor;
026    
027    public abstract class FunctionGenerationStrategy {
028        public abstract void generateBody(
029                @NotNull MethodVisitor mv,
030                @NotNull FrameMap frameMap,
031                @NotNull JvmMethodSignature signature,
032                @NotNull MethodContext context,
033                @NotNull MemberCodegen<?> parentCodegen
034        );
035    
036        public static class FunctionDefault extends CodegenBased {
037            private final KtDeclarationWithBody declaration;
038    
039            public FunctionDefault(
040                    @NotNull GenerationState state,
041                    @NotNull KtDeclarationWithBody declaration
042            ) {
043                super(state);
044                this.declaration = declaration;
045            }
046    
047            @Override
048            public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
049                codegen.returnExpression(declaration.getBodyExpression());
050            }
051        }
052    
053        public abstract static class CodegenBased extends FunctionGenerationStrategy {
054            protected final GenerationState state;
055    
056            public CodegenBased(@NotNull GenerationState state) {
057                this.state = state;
058            }
059    
060            @Override
061            public final void generateBody(
062                    @NotNull MethodVisitor mv,
063                    @NotNull FrameMap frameMap,
064                    @NotNull JvmMethodSignature signature,
065                    @NotNull MethodContext context,
066                    @NotNull MemberCodegen<?> parentCodegen
067            ) {
068                ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, signature.getReturnType(), context, state, parentCodegen);
069                doGenerateBody(codegen, signature);
070            }
071    
072            public abstract void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature);
073        }
074    }