diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java index 6b860614923..09e5ed096ce 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java @@ -84,28 +84,10 @@ public abstract class ClassBodyCodegen { } protected void generateDeclaration(PropertyCodegen propertyCodegen, JetDeclaration declaration, FunctionCodegen functionCodegen) { - if (declaration instanceof JetProperty) { - propertyCodegen.gen((JetProperty) declaration); + if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) { + state.getInjector().getMemberCodegen().generateFunctionOrProperty( + (JetTypeParameterListOwner) declaration, context, v); } - else if (declaration instanceof JetNamedFunction) { - try { - genNamedFunction((JetNamedFunction) declaration, functionCodegen); - } - catch (CompilationException e) { - throw e; - } - catch (ProcessCanceledException e) { - throw e; - } - catch (RuntimeException e) { - throw new RuntimeException("Error generating method " + myClass.getName() + "." + declaration.getName() + " in " + context, - e); - } - } - } - - protected void genNamedFunction(JetNamedFunction declaration, FunctionCodegen functionCodegen) { - functionCodegen.gen(declaration); } private void generatePrimaryConstructorProperties(PropertyCodegen propertyCodegen, JetClassOrObject origin) { diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/MemberCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/MemberCodegen.java new file mode 100644 index 00000000000..8b2f88cb965 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/MemberCodegen.java @@ -0,0 +1,70 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.codegen; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.psi.JetNamedFunction; +import org.jetbrains.jet.lang.psi.JetProperty; +import org.jetbrains.jet.lang.psi.JetTypeParameterListOwner; + +import javax.inject.Inject; + +/** + * @author Stepan Koltsov + */ +public class MemberCodegen { + + @NotNull + private GenerationState state; + + @Inject + public void setState(@NotNull GenerationState state) { + this.state = state; + } + + + public void generateFunctionOrProperty(@NotNull JetTypeParameterListOwner functionOrProperty, + @NotNull CodegenContext context, @NotNull ClassBuilder classBuilder) { + FunctionCodegen functionCodegen = new FunctionCodegen(context, classBuilder, state); + if (functionOrProperty instanceof JetNamedFunction) { + try { + functionCodegen.gen((JetNamedFunction) functionOrProperty); + } + catch (CompilationException e) { + throw e; + } + catch (Exception e) { + throw new CompilationException("Failed to generate function " + functionOrProperty.getName(), e, functionOrProperty); + } + } + else if (functionOrProperty instanceof JetProperty) { + try { + new PropertyCodegen(context, classBuilder, functionCodegen, state).gen((JetProperty) functionOrProperty); + } + catch (CompilationException e) { + throw e; + } + catch (Exception e) { + throw new CompilationException("Failed to generate property " + functionOrProperty.getName(), e, functionOrProperty); + } + } + else { + throw new IllegalArgumentException("Unknown parameter: " + functionOrProperty); + } + } + +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java index 53348ac9506..f356f9dedb4 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java @@ -58,23 +58,10 @@ public class NamespaceCodegen { NamespaceDescriptor descriptor = state.getBindingContext().get(BindingContext.FILE_TO_NAMESPACE, file); final CodegenContext context = CodegenContexts.STATIC.intoNamespace(descriptor); - final FunctionCodegen functionCodegen = new FunctionCodegen(context, v, state); - final PropertyCodegen propertyCodegen = new PropertyCodegen(context, v, functionCodegen, state); - for (JetDeclaration declaration : file.getDeclarations()) { - if (declaration instanceof JetProperty) { - propertyCodegen.gen((JetProperty) declaration); - } - else if (declaration instanceof JetNamedFunction) { - try { - functionCodegen.gen((JetNamedFunction) declaration); - } - catch (CompilationException e) { - throw e; - } - catch (Exception e) { - throw new CompilationException("Failed to generate function " + declaration.getName(), e, declaration); - } + if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) { + state.getInjector().getMemberCodegen().generateFunctionOrProperty( + (JetTypeParameterListOwner) declaration, context, v); } else if (declaration instanceof JetClassOrObject) { state.getInjector().getClassCodegen().generate(context, (JetClassOrObject) declaration); diff --git a/compiler/backend/src/org/jetbrains/jet/di/InjectorForJvmCodegen.java b/compiler/backend/src/org/jetbrains/jet/di/InjectorForJvmCodegen.java index f9d4533882e..c11820b1afa 100644 --- a/compiler/backend/src/org/jetbrains/jet/di/InjectorForJvmCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/di/InjectorForJvmCodegen.java @@ -31,6 +31,7 @@ import org.jetbrains.jet.codegen.ClassCodegen; import org.jetbrains.jet.codegen.ScriptCodegen; import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods; import org.jetbrains.jet.codegen.ClassFileFactory; +import org.jetbrains.jet.codegen.MemberCodegen; import org.jetbrains.jet.codegen.ClosureAnnotator; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.resolve.BindingContext; @@ -60,6 +61,7 @@ public class InjectorForJvmCodegen { private ScriptCodegen scriptCodegen; private IntrinsicMethods intrinsics; private ClassFileFactory classFileFactory; + private MemberCodegen memberCodegen; private ClosureAnnotator closureAnnotator; public InjectorForJvmCodegen( @@ -85,6 +87,7 @@ public class InjectorForJvmCodegen { this.scriptCodegen = new ScriptCodegen(); this.intrinsics = new IntrinsicMethods(); this.classFileFactory = new ClassFileFactory(); + this.memberCodegen = new MemberCodegen(); this.closureAnnotator = new ClosureAnnotator(); this.jetTypeMapper.setBindingContext(bindingContext); @@ -106,6 +109,8 @@ public class InjectorForJvmCodegen { this.classFileFactory.setBuilderFactory(classBuilderFactory); this.classFileFactory.setState(generationState); + this.memberCodegen.setState(generationState); + closureAnnotator.setBindingContext(bindingContext); closureAnnotator.setFiles(listOfJetFile); @@ -149,4 +154,8 @@ public class InjectorForJvmCodegen { return this.classFileFactory; } + public MemberCodegen getMemberCodegen() { + return this.memberCodegen; + } + } diff --git a/injector-generator/src/org/jetbrains/jet/di/AllInjectorsGenerator.java b/injector-generator/src/org/jetbrains/jet/di/AllInjectorsGenerator.java index ae585619c9c..6a7bbb84610 100644 --- a/injector-generator/src/org/jetbrains/jet/di/AllInjectorsGenerator.java +++ b/injector-generator/src/org/jetbrains/jet/di/AllInjectorsGenerator.java @@ -171,6 +171,7 @@ public class AllInjectorsGenerator { generator.addPublicField(ScriptCodegen.class); generator.addField(true, IntrinsicMethods.class, "intrinsics", null); generator.addPublicField(ClassFileFactory.class); + generator.addPublicField(MemberCodegen.class); generator.generate("compiler/backend/src", "org.jetbrains.jet.di", "InjectorForJvmCodegen"); }