injector for JVM codegen
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
<orderEntry type="library" name="asm" level="project" />
|
||||
<orderEntry type="module" module-name="runtime" />
|
||||
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
||||
<orderEntry type="library" name="javax.inject" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -39,14 +41,27 @@ public class ClosureAnnotator {
|
||||
private final Map<DeclarationDescriptor,ClassDescriptor> enclosing = new HashMap<DeclarationDescriptor, ClassDescriptor>();
|
||||
|
||||
private final MultiMap<FqName, JetFile> namespaceName2Files = MultiMap.create();
|
||||
private final BindingContext bindingContext;
|
||||
|
||||
public ClosureAnnotator(BindingContext bindingContext, Collection<JetFile> files) {
|
||||
private BindingContext bindingContext;
|
||||
private List<JetFile> files;
|
||||
|
||||
@Inject
|
||||
public void setBindingContext(BindingContext bindingContext) {
|
||||
this.bindingContext = bindingContext;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setFiles(List<JetFile> files) {
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
mapFilesToNamespaces(files);
|
||||
prepareAnonymousClasses();
|
||||
}
|
||||
|
||||
|
||||
public ClassDescriptor classDescriptorForFunctionDescriptor(FunctionDescriptor funDescriptor, String name) {
|
||||
ClassDescriptorImpl classDescriptor = classesForFunctions.get(funDescriptor);
|
||||
if(classDescriptor == null) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.containers.Stack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods;
|
||||
import org.jetbrains.jet.di.InjectorForJvmCodegen;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
|
||||
@@ -141,8 +142,8 @@ public class GenerationState {
|
||||
}
|
||||
|
||||
public void compileCorrectFiles(BindingContext bindingContext, List<JetFile> files, @NotNull CompilationErrorHandler errorHandler, boolean annotate) {
|
||||
ClosureAnnotator closureAnnotator = !annotate ? null : new ClosureAnnotator(bindingContext, files);
|
||||
typeMapper = new JetTypeMapper(standardLibrary, bindingContext, closureAnnotator);
|
||||
InjectorForJvmCodegen injector = new InjectorForJvmCodegen(standardLibrary, bindingContext, files);
|
||||
typeMapper = injector.getJetTypeMapper();
|
||||
bindingContexts.push(bindingContext);
|
||||
try {
|
||||
for (JetFile file : files) {
|
||||
|
||||
@@ -37,6 +37,8 @@ import org.jetbrains.jet.lang.types.lang.PrimitiveType;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
@@ -58,8 +60,34 @@ public class JetTypeMapper {
|
||||
|
||||
public static final Type ARRAY_GENERIC_TYPE = Type.getType(Object[].class);
|
||||
|
||||
private final JetStandardLibrary standardLibrary;
|
||||
public final BindingContext bindingContext;
|
||||
private JetStandardLibrary standardLibrary;
|
||||
public BindingContext bindingContext;
|
||||
private ClosureAnnotator closureAnnotator;
|
||||
|
||||
|
||||
@Inject
|
||||
public void setStandardLibrary(JetStandardLibrary standardLibrary) {
|
||||
this.standardLibrary = standardLibrary;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setBindingContext(BindingContext bindingContext) {
|
||||
this.bindingContext = bindingContext;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setClosureAnnotator(ClosureAnnotator closureAnnotator) {
|
||||
this.closureAnnotator = closureAnnotator;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
initKnownTypes();
|
||||
initKnownTypeNames();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean hasThis0(ClassDescriptor classDescriptor) {
|
||||
return closureAnnotator.hasThis0(classDescriptor);
|
||||
@@ -69,8 +97,6 @@ public class JetTypeMapper {
|
||||
return closureAnnotator;
|
||||
}
|
||||
|
||||
private final ClosureAnnotator closureAnnotator;
|
||||
|
||||
private final HashMap<JetType,String> knowTypeNames = new HashMap<JetType, String>();
|
||||
private final HashMap<JetType,Type> knowTypes = new HashMap<JetType, Type>();
|
||||
|
||||
@@ -88,14 +114,6 @@ public class JetTypeMapper {
|
||||
public static final Type TYPE_FUNCTION0 = Type.getObjectType("jet/Function0");
|
||||
public static final Type TYPE_FUNCTION1 = Type.getObjectType("jet/Function1");
|
||||
|
||||
public JetTypeMapper(JetStandardLibrary standardLibrary, BindingContext bindingContext, ClosureAnnotator closureAnnotator) {
|
||||
this.standardLibrary = standardLibrary;
|
||||
this.bindingContext = bindingContext;
|
||||
this.closureAnnotator = closureAnnotator;
|
||||
initKnownTypes();
|
||||
initKnownTypeNames();
|
||||
}
|
||||
|
||||
public static boolean isIntPrimitive(Type type) {
|
||||
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.di;
|
||||
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import java.util.List;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.ClosureAnnotator;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import java.util.List;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/* This file is generated by org.jetbrains.jet.di.AllInjectorsGenerator. DO NOT EDIT! */
|
||||
public class InjectorForJvmCodegen {
|
||||
|
||||
private JetTypeMapper jetTypeMapper;
|
||||
|
||||
public InjectorForJvmCodegen(
|
||||
@NotNull JetStandardLibrary jetStandardLibrary,
|
||||
@NotNull BindingContext bindingContext,
|
||||
@NotNull List<JetFile> listOfJetFile
|
||||
) {
|
||||
this.jetTypeMapper = new JetTypeMapper();
|
||||
ClosureAnnotator closureAnnotator = new ClosureAnnotator();
|
||||
|
||||
this.jetTypeMapper.setBindingContext(bindingContext);
|
||||
this.jetTypeMapper.setClosureAnnotator(closureAnnotator);
|
||||
this.jetTypeMapper.setStandardLibrary(jetStandardLibrary);
|
||||
|
||||
closureAnnotator.setBindingContext(bindingContext);
|
||||
closureAnnotator.setFiles(listOfJetFile);
|
||||
|
||||
jetTypeMapper.init();
|
||||
|
||||
closureAnnotator.init();
|
||||
|
||||
}
|
||||
|
||||
public JetTypeMapper getJetTypeMapper() {
|
||||
return this.jetTypeMapper;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.jet.compiler.CompileSession;
|
||||
import org.jetbrains.jet.compiler.MessageRenderer;
|
||||
import org.jetbrains.jet.di.InjectorForJvmCodegen;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetClass;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
@@ -96,8 +97,9 @@ public class TestlibTest extends CodegenTestCase {
|
||||
new URLClassLoader(new URL[]{ForTestCompileStdlib.stdlibJarForTests().toURI().toURL(), junitJar.toURI().toURL()},
|
||||
TestCase.class.getClassLoader()));
|
||||
|
||||
ClosureAnnotator closureAnnotator = new ClosureAnnotator(session.getMyBindingContext(), session.getSourceFileNamespaces());
|
||||
JetTypeMapper typeMapper = new JetTypeMapper(classFileFactory.state.getStandardLibrary(), session.getMyBindingContext(), closureAnnotator);
|
||||
InjectorForJvmCodegen injector = new InjectorForJvmCodegen(
|
||||
classFileFactory.state.getStandardLibrary(), session.getMyBindingContext(), session.getSourceFileNamespaces());
|
||||
JetTypeMapper typeMapper = injector.getJetTypeMapper();
|
||||
TestSuite suite = new TestSuite("stdlib_test");
|
||||
try {
|
||||
for(JetFile jetFile : session.getSourceFileNamespaces()) {
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.codegen.ClosureAnnotator;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.NamespaceCodegen;
|
||||
import org.jetbrains.jet.di.InjectorForJvmCodegen;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||
@@ -154,8 +155,8 @@ public class JetPositionManager implements PositionManager {
|
||||
}
|
||||
final BindingContext bindingContext = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file);
|
||||
final JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
|
||||
ClosureAnnotator closureAnnotator = new ClosureAnnotator(bindingContext, Collections.singleton(file));
|
||||
final JetTypeMapper typeMapper = new JetTypeMapper(standardLibrary, bindingContext, closureAnnotator);
|
||||
InjectorForJvmCodegen injector = new InjectorForJvmCodegen(standardLibrary, bindingContext, Collections.singletonList(file));
|
||||
final JetTypeMapper typeMapper = injector.getJetTypeMapper();
|
||||
myTypeMappers.put(file, typeMapper);
|
||||
return typeMapper;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<orderEntry type="module" module-name="frontend.java" />
|
||||
<orderEntry type="library" name="intellij-core" level="project" />
|
||||
<orderEntry type="library" name="javax.inject" level="project" />
|
||||
<orderEntry type="module" module-name="backend" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
package org.jetbrains.jet.di;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.jet.codegen.ClosureAnnotator;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.lang.ModuleConfiguration;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.*;
|
||||
import org.jetbrains.jet.lang.resolve.calls.CallResolver;
|
||||
import org.jetbrains.jet.lang.resolve.java.JavaBridgeConfiguration;
|
||||
@@ -45,6 +48,7 @@ public class AllInjectorsGenerator {
|
||||
generateMacroInjector();
|
||||
generateTestInjector();
|
||||
generateInjectorForJavaSemanticServices();
|
||||
generateInjectorForJvmCodegen();
|
||||
}
|
||||
|
||||
private static void generateInjectorForTopDownAnalyzerBasic() throws IOException {
|
||||
@@ -133,4 +137,13 @@ public class AllInjectorsGenerator {
|
||||
generator.generate("compiler/frontend.java/src", "org.jetbrains.jet.di", "InjectorForJavaSemanticServices");
|
||||
}
|
||||
|
||||
private static void generateInjectorForJvmCodegen() throws IOException {
|
||||
DependencyInjectorGenerator generator = new DependencyInjectorGenerator(false);
|
||||
generator.addParameter(JetStandardLibrary.class);
|
||||
generator.addParameter(BindingContext.class);
|
||||
generator.addParameter(DiType.listOf(JetFile.class));
|
||||
generator.addPublicField(JetTypeMapper.class);
|
||||
generator.generate("compiler/backend/src", "org.jetbrains.jet.di", "InjectorForJvmCodegen");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -121,4 +122,24 @@ class DiType {
|
||||
}
|
||||
throw new IllegalArgumentException("unsupported type: " + type);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DiType collectionOf(@NotNull Class<?> type) {
|
||||
return collectionOf(new DiType(type));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DiType collectionOf(@NotNull DiType type) {
|
||||
return new DiType(List.class, Lists.newArrayList(type));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DiType listOf(@NotNull Class<?> type) {
|
||||
return collectionOf(new DiType(type));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static DiType listOf(@NotNull DiType type) {
|
||||
return new DiType(List.class, Lists.newArrayList(type));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user