Skip to content
Browse files

MemberCodegen

(move duplicate code into it)
  • Loading branch information...
1 parent 0f8c449 commit 95414078b99644fbf856cd213f5953599bc4b85d @stepancheg stepancheg committed
View
24 compiler/backend/src/org/jetbrains/jet/codegen/ClassBodyCodegen.java
@@ -84,28 +84,10 @@ private void generateClassBody() {
}
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) {
View
70 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);
+ }
+ }
+
+}
View
19 compiler/backend/src/org/jetbrains/jet/codegen/NamespaceCodegen.java
@@ -58,23 +58,10 @@ public void generate(JetFile file) {
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);
View
9 compiler/backend/src/org/jetbrains/jet/di/InjectorForJvmCodegen.java
@@ -31,6 +31,7 @@
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 @@
private ScriptCodegen scriptCodegen;
private IntrinsicMethods intrinsics;
private ClassFileFactory classFileFactory;
+ private MemberCodegen memberCodegen;
private ClosureAnnotator closureAnnotator;
public InjectorForJvmCodegen(
@@ -85,6 +87,7 @@ public 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 InjectorForJvmCodegen(
this.classFileFactory.setBuilderFactory(classBuilderFactory);
this.classFileFactory.setState(generationState);
+ this.memberCodegen.setState(generationState);
+
closureAnnotator.setBindingContext(bindingContext);
closureAnnotator.setFiles(listOfJetFile);
@@ -149,4 +154,8 @@ public ClassFileFactory getClassFileFactory() {
return this.classFileFactory;
}
+ public MemberCodegen getMemberCodegen() {
+ return this.memberCodegen;
+ }
+
}
View
1 injector-generator/src/org/jetbrains/jet/di/AllInjectorsGenerator.java
@@ -171,6 +171,7 @@ private static void generateInjectorForJvmCodegen() throws IOException {
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");
}

0 comments on commit 9541407

Please sign in to comment.
Something went wrong with that request. Please try again.