CodegenAnnotator does not exist anymore
Injectors regenerated to reflect that
This commit is contained in:
@@ -18,6 +18,7 @@ package org.jetbrains.jet.codegen;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.containers.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.asm4.MethodVisitor;
|
||||
import org.jetbrains.asm4.Type;
|
||||
@@ -244,4 +245,8 @@ public class CodegenUtil {
|
||||
v.newField(null, access, field.first, field.second.getDescriptor(), null, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T peekFromStack(com.intellij.util.containers.Stack<T> stack) {
|
||||
return stack.empty() ? null : stack.peek();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1009,7 +1009,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
|
||||
private StackValue genClosure(JetExpression expression) {
|
||||
final FunctionDescriptor descriptor = bindingContext.get(BindingContext.FUNCTION, expression);
|
||||
final CodegenAnnotator codegenAnnotator = state.getInjector().getCodegenAnnotator();
|
||||
final ClassDescriptor classDescriptor =
|
||||
bindingContext.get(CLASS_FOR_FUNCTION, descriptor);
|
||||
//noinspection SuspiciousMethodCalls
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.asm4.commons.Method;
|
||||
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.codegen.context.CalculatedClosure;
|
||||
import org.jetbrains.jet.codegen.context.CodegenBinding;
|
||||
import org.jetbrains.jet.codegen.context.CodegenContext;
|
||||
import org.jetbrains.jet.di.InjectorForJvmCodegen;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
@@ -147,7 +148,8 @@ public class GenerationState {
|
||||
private void beforeCompile() {
|
||||
markUsed();
|
||||
|
||||
injector.getCodegenAnnotator().init();
|
||||
//noinspection unchecked
|
||||
CodegenBinding.initTrace(injector.getBindingTrace(), injector.getListOfJetFile());
|
||||
}
|
||||
|
||||
public void compileCorrectFiles(@NotNull CompilationErrorHandler errorHandler) {
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.asm4.Opcodes;
|
||||
import org.jetbrains.asm4.Type;
|
||||
import org.jetbrains.jet.codegen.context.CalculatedClosure;
|
||||
import org.jetbrains.jet.codegen.context.CodegenAnnotator;
|
||||
import org.jetbrains.jet.codegen.context.EnclosedValueDescriptor;
|
||||
import org.jetbrains.jet.codegen.signature.*;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
@@ -77,7 +76,6 @@ public class JetTypeMapper {
|
||||
public static final Type JET_SHARED_BOOLEAN_TYPE = Type.getObjectType("jet/runtime/SharedVar$Boolean");
|
||||
|
||||
public BindingContext bindingContext;
|
||||
private CodegenAnnotator codegenAnnotator;
|
||||
private boolean mapBuiltinsToJava;
|
||||
private ClassBuilderMode classBuilderMode;
|
||||
|
||||
@@ -86,11 +84,6 @@ public class JetTypeMapper {
|
||||
this.bindingContext = bindingContext;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setCodegenAnnotator(CodegenAnnotator codegenAnnotator) {
|
||||
this.codegenAnnotator = codegenAnnotator;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setBuiltinToJavaTypesMapping(BuiltinToJavaTypesMapping builtinToJavaTypesMapping) {
|
||||
mapBuiltinsToJava = builtinToJavaTypesMapping == BuiltinToJavaTypesMapping.ENABLED;
|
||||
@@ -112,10 +105,6 @@ public class JetTypeMapper {
|
||||
return bindingContext.get(CLOSURE, classDescriptor);
|
||||
}
|
||||
|
||||
public CodegenAnnotator getCodegenAnnotator() {
|
||||
return codegenAnnotator;
|
||||
}
|
||||
|
||||
public static boolean isIntPrimitive(Type type) {
|
||||
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.jetbrains.asm4.MethodVisitor;
|
||||
import org.jetbrains.asm4.Opcodes;
|
||||
import org.jetbrains.asm4.Type;
|
||||
import org.jetbrains.asm4.commons.InstructionAdapter;
|
||||
import org.jetbrains.jet.codegen.context.CodegenAnnotator;
|
||||
import org.jetbrains.jet.codegen.context.CodegenContext;
|
||||
import org.jetbrains.jet.codegen.signature.JvmMethodSignature;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
@@ -56,8 +55,6 @@ public class ScriptCodegen {
|
||||
@NotNull
|
||||
private MemberCodegen memberCodegen;
|
||||
@NotNull
|
||||
private CodegenAnnotator codegenAnnotator;
|
||||
@NotNull
|
||||
private BindingContext bindingContext;
|
||||
|
||||
private List<ScriptDescriptor> earlierScripts;
|
||||
@@ -83,11 +80,6 @@ public class ScriptCodegen {
|
||||
this.memberCodegen = memberCodegen;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setCodegenAnnotator(@NotNull CodegenAnnotator codegenAnnotator) {
|
||||
this.codegenAnnotator = codegenAnnotator;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setBindingContext(@NotNull BindingContext bindingContext) {
|
||||
this.bindingContext = bindingContext;
|
||||
@@ -238,7 +230,7 @@ public class ScriptCodegen {
|
||||
ScriptDescriptor earlierDescriptor = t.first;
|
||||
JvmClassName earlierClassName = t.second;
|
||||
|
||||
registerClassNameForScript(codegenAnnotator.getBindingTrace(), earlierDescriptor, earlierClassName);
|
||||
registerClassNameForScript(state.getInjector().getBindingTrace(), earlierDescriptor, earlierClassName);
|
||||
}
|
||||
|
||||
List<ScriptDescriptor> earlierScriptDescriptors = Lists.newArrayList();
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
* 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.context;
|
||||
|
||||
import com.intellij.util.containers.Stack;
|
||||
import org.jetbrains.jet.codegen.CodegenUtil;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.jet.codegen.context.CodegenBinding.*;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.CLASS;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.DECLARATION_TO_DESCRIPTOR;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.SCRIPT;
|
||||
|
||||
/**
|
||||
* @author alex.tkachman
|
||||
*/
|
||||
class CodegenAnnotatingVisitor extends JetVisitorVoid {
|
||||
private final Map<String, Integer> anonymousSubclassesCount = new HashMap<String, Integer>();
|
||||
|
||||
private final Stack<ClassDescriptor> classStack = new Stack<ClassDescriptor>();
|
||||
private final Stack<String> nameStack = new Stack<String>();
|
||||
private final BindingTrace bindingTrace;
|
||||
private final BindingContext bindingContext;
|
||||
|
||||
public CodegenAnnotatingVisitor(BindingTrace bindingTrace) {
|
||||
this.bindingTrace = bindingTrace;
|
||||
this.bindingContext = bindingTrace.getBindingContext();
|
||||
}
|
||||
|
||||
private ClassDescriptor recordClassForFunction(FunctionDescriptor funDescriptor) {
|
||||
ClassDescriptor classDescriptor;
|
||||
int arity = funDescriptor.getValueParameters().size();
|
||||
|
||||
classDescriptor = new ClassDescriptorImpl(
|
||||
funDescriptor.getContainingDeclaration(),
|
||||
Collections.<AnnotationDescriptor>emptyList(),
|
||||
Modality.FINAL,
|
||||
Name.special("<closure>"));
|
||||
((ClassDescriptorImpl)classDescriptor).initialize(
|
||||
false,
|
||||
Collections.<TypeParameterDescriptor>emptyList(),
|
||||
Collections.singleton((funDescriptor.getReceiverParameter().exists()
|
||||
? JetStandardClasses.getReceiverFunction(arity)
|
||||
: JetStandardClasses.getFunction(arity)).getDefaultType()), JetScope.EMPTY,
|
||||
Collections.<ConstructorDescriptor>emptySet(), null);
|
||||
|
||||
bindingTrace.record(CLASS_FOR_FUNCTION, funDescriptor, classDescriptor);
|
||||
return classDescriptor;
|
||||
}
|
||||
|
||||
private String inventAnonymousClassName(JetElement declaration) {
|
||||
String top = CodegenUtil.peekFromStack(nameStack);
|
||||
Integer cnt = anonymousSubclassesCount.get(top);
|
||||
if (cnt == null) {
|
||||
cnt = 0;
|
||||
}
|
||||
String name = top + "$" + (cnt + 1);
|
||||
ClassDescriptor descriptor = bindingContext.get(CLASS, declaration);
|
||||
if (descriptor == null) {
|
||||
if (declaration instanceof JetFunctionLiteralExpression ||
|
||||
declaration instanceof JetNamedFunction ||
|
||||
declaration instanceof JetObjectLiteralExpression) {
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(
|
||||
"Class-less declaration which is not JetFunctionLiteralExpression|JetNamedFunction|JetObjectLiteralExpression : " +
|
||||
declaration.getClass().getName());
|
||||
}
|
||||
}
|
||||
anonymousSubclassesCount.put(top, cnt + 1);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitJetElement(JetElement element) {
|
||||
super.visitJetElement(element);
|
||||
element.acceptChildren(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitJetFile(JetFile file) {
|
||||
if (file.isScript()) {
|
||||
//noinspection ConstantConditions
|
||||
final ClassDescriptor classDescriptor =
|
||||
bindingContext.get(CLASS_FOR_FUNCTION, bindingContext.get(SCRIPT, file.getScript()));
|
||||
classStack.push(classDescriptor);
|
||||
//noinspection ConstantConditions
|
||||
nameStack.push(classNameForScriptPsi(bindingContext, file.getScript()).getInternalName());
|
||||
}
|
||||
else {
|
||||
nameStack.push(JetPsiUtil.getFQName(file).getFqName().replace('.', '/'));
|
||||
}
|
||||
file.acceptChildren(this);
|
||||
nameStack.pop();
|
||||
if (file.isScript()) {
|
||||
classStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnumEntry(JetEnumEntry enumEntry) {
|
||||
ClassDescriptor descriptor = bindingContext.get(CLASS, enumEntry);
|
||||
assert descriptor != null;
|
||||
|
||||
final boolean trivial = enumEntry.getDeclarations().isEmpty();
|
||||
if (!trivial) {
|
||||
bindingTrace.record(ENUM_ENTRY_CLASS_NEED_SUBCLASS, descriptor);
|
||||
|
||||
classStack.push(descriptor);
|
||||
nameStack.push(CodegenUtil.peekFromStack(nameStack) + "$" + descriptor.getName().getName());
|
||||
super.visitEnumEntry(enumEntry);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
else {
|
||||
super.visitEnumEntry(enumEntry);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClassObject(JetClassObject classObject) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, classObject.getObjectDeclaration());
|
||||
assert classDescriptor != null;
|
||||
|
||||
JvmClassName name = JvmClassName.byInternalName(CodegenUtil.peekFromStack(nameStack) + JvmAbi.CLASS_OBJECT_SUFFIX);
|
||||
recordClosure(bindingTrace, classObject, classDescriptor, CodegenUtil.peekFromStack(classStack), name, false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name.getInternalName());
|
||||
super.visitClassObject(classObject);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitObjectDeclaration(JetObjectDeclaration declaration) {
|
||||
if (declaration.getParent() instanceof JetObjectLiteralExpression || declaration.getParent() instanceof JetClassObject) {
|
||||
super.visitObjectDeclaration(declaration);
|
||||
}
|
||||
else {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, declaration);
|
||||
// working around a problem with shallow analysis
|
||||
if (classDescriptor == null) return;
|
||||
|
||||
String name = getName(classDescriptor);
|
||||
recordClosure(bindingTrace, declaration, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitObjectDeclaration(declaration);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClass(JetClass klass) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, klass);
|
||||
// working around a problem with shallow analysis
|
||||
if (classDescriptor == null) return;
|
||||
|
||||
String name = getName(classDescriptor);
|
||||
recordClosure(bindingTrace, klass, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitClass(klass);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
private String getName(ClassDescriptor classDescriptor) {
|
||||
String base = CodegenUtil.peekFromStack(nameStack);
|
||||
return classDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor ? base.isEmpty() ? classDescriptor.getName()
|
||||
.getName() : base + '/' + classDescriptor.getName() : base + '$' + classDescriptor.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitObjectLiteralExpression(JetObjectLiteralExpression expression) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, expression.getObjectDeclaration());
|
||||
if (classDescriptor == null) {
|
||||
// working around a problem with shallow analysis
|
||||
super.visitObjectLiteralExpression(expression);
|
||||
return;
|
||||
}
|
||||
|
||||
final String name = inventAnonymousClassName(expression.getObjectDeclaration());
|
||||
recordClosure(bindingTrace, expression.getObjectDeclaration(), classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(classNameForClassDescriptor(bindingContext, classDescriptor).getInternalName());
|
||||
super.visitObjectLiteralExpression(expression);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitFunctionLiteralExpression(JetFunctionLiteralExpression expression) {
|
||||
FunctionDescriptor functionDescriptor =
|
||||
(FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, expression);
|
||||
// working around a problem with shallow analysis
|
||||
if (functionDescriptor == null) return;
|
||||
|
||||
String name = inventAnonymousClassName(expression);
|
||||
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
|
||||
recordClosure(bindingTrace, expression, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), true);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitFunctionLiteralExpression(expression);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitProperty(JetProperty property) {
|
||||
nameStack.push(CodegenUtil.peekFromStack(nameStack) + '$' + property.getName());
|
||||
super.visitProperty(property);
|
||||
nameStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNamedFunction(JetNamedFunction function) {
|
||||
FunctionDescriptor functionDescriptor =
|
||||
(FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, function);
|
||||
// working around a problem with shallow analysis
|
||||
if (functionDescriptor == null) return;
|
||||
DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration();
|
||||
if (containingDeclaration instanceof ClassDescriptor) {
|
||||
nameStack.push(CodegenUtil.peekFromStack(nameStack) + '$' + function.getName());
|
||||
super.visitNamedFunction(function);
|
||||
nameStack.pop();
|
||||
}
|
||||
else if (containingDeclaration instanceof NamespaceDescriptor) {
|
||||
String peek = CodegenUtil.peekFromStack(nameStack);
|
||||
if (peek.isEmpty()) {
|
||||
peek = "namespace";
|
||||
}
|
||||
else {
|
||||
peek += "/namespace";
|
||||
}
|
||||
nameStack.push(peek + '$' + function.getName());
|
||||
super.visitNamedFunction(function);
|
||||
nameStack.pop();
|
||||
}
|
||||
else {
|
||||
String name = inventAnonymousClassName(function);
|
||||
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
|
||||
recordClosure(bindingTrace, function, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), true);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitNamedFunction(function);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,307 +0,0 @@
|
||||
/*
|
||||
* 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.context;
|
||||
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
|
||||
import com.intellij.util.containers.Stack;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.*;
|
||||
|
||||
import static org.jetbrains.jet.codegen.context.CodegenBinding.*;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
|
||||
|
||||
/**
|
||||
* @author alex.tkachman
|
||||
*/
|
||||
public class CodegenAnnotator {
|
||||
private BindingTrace bindingTrace;
|
||||
private BindingContext bindingContext;
|
||||
private List<JetFile> files;
|
||||
|
||||
public BindingTrace getBindingTrace() {
|
||||
return bindingTrace;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setBindingTrace(BindingTrace bindingTrace) {
|
||||
this.bindingTrace = bindingTrace;
|
||||
bindingContext = bindingTrace.getBindingContext();
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setFiles(List<JetFile> files) {
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
MyJetVisitorVoid visitor = new MyJetVisitorVoid();
|
||||
for (JetFile file : CodegenBinding.allFilesInNamespaces(bindingContext, files)) {
|
||||
file.accept(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
private ClassDescriptor recordClassForFunction(FunctionDescriptor funDescriptor) {
|
||||
ClassDescriptor classDescriptor;
|
||||
int arity = funDescriptor.getValueParameters().size();
|
||||
|
||||
classDescriptor = new ClassDescriptorImpl(
|
||||
funDescriptor.getContainingDeclaration(),
|
||||
Collections.<AnnotationDescriptor>emptyList(),
|
||||
Modality.FINAL,
|
||||
Name.special("<closure>"));
|
||||
((ClassDescriptorImpl)classDescriptor).initialize(
|
||||
false,
|
||||
Collections.<TypeParameterDescriptor>emptyList(),
|
||||
Collections.singleton((funDescriptor.getReceiverParameter().exists()
|
||||
? JetStandardClasses.getReceiverFunction(arity)
|
||||
: JetStandardClasses.getFunction(arity)).getDefaultType()), JetScope.EMPTY,
|
||||
Collections.<ConstructorDescriptor>emptySet(), null);
|
||||
|
||||
bindingTrace.record(CLASS_FOR_FUNCTION, funDescriptor, classDescriptor);
|
||||
return classDescriptor;
|
||||
}
|
||||
|
||||
static <T> T peekFromStack(Stack<T> stack) {
|
||||
return stack.empty() ? null : stack.peek();
|
||||
}
|
||||
|
||||
private class MyJetVisitorVoid extends JetVisitorVoid {
|
||||
private final Map<String, Integer> anonymousSubclassesCount = new HashMap<String, Integer>();
|
||||
|
||||
private final Stack<ClassDescriptor> classStack = new Stack<ClassDescriptor>();
|
||||
private final Stack<String> nameStack = new Stack<String>();
|
||||
|
||||
private String inventAnonymousClassName(JetElement declaration) {
|
||||
String top = peekFromStack(nameStack);
|
||||
Integer cnt = anonymousSubclassesCount.get(top);
|
||||
if (cnt == null) {
|
||||
cnt = 0;
|
||||
}
|
||||
String name = top + "$" + (cnt + 1);
|
||||
ClassDescriptor descriptor = bindingContext.get(CLASS, declaration);
|
||||
if (descriptor == null) {
|
||||
if (declaration instanceof JetFunctionLiteralExpression ||
|
||||
declaration instanceof JetNamedFunction ||
|
||||
declaration instanceof JetObjectLiteralExpression) {
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(
|
||||
"Class-less declaration which is not JetFunctionLiteralExpression|JetNamedFunction|JetObjectLiteralExpression : " +
|
||||
declaration.getClass().getName());
|
||||
}
|
||||
}
|
||||
anonymousSubclassesCount.put(top, cnt + 1);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitJetElement(JetElement element) {
|
||||
super.visitJetElement(element);
|
||||
element.acceptChildren(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitJetFile(JetFile file) {
|
||||
if (file.isScript()) {
|
||||
//noinspection ConstantConditions
|
||||
final ClassDescriptor classDescriptor =
|
||||
bindingContext.get(CLASS_FOR_FUNCTION, bindingContext.get(SCRIPT, file.getScript()));
|
||||
classStack.push(classDescriptor);
|
||||
//noinspection ConstantConditions
|
||||
nameStack.push(classNameForScriptPsi(bindingContext, file.getScript()).getInternalName());
|
||||
}
|
||||
else {
|
||||
nameStack.push(JetPsiUtil.getFQName(file).getFqName().replace('.', '/'));
|
||||
}
|
||||
file.acceptChildren(this);
|
||||
nameStack.pop();
|
||||
if (file.isScript()) {
|
||||
classStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnumEntry(JetEnumEntry enumEntry) {
|
||||
ClassDescriptor descriptor = bindingContext.get(CLASS, enumEntry);
|
||||
assert descriptor != null;
|
||||
|
||||
final boolean trivial = enumEntry.getDeclarations().isEmpty();
|
||||
if (!trivial) {
|
||||
bindingTrace.record(ENUM_ENTRY_CLASS_NEED_SUBCLASS, descriptor);
|
||||
|
||||
classStack.push(descriptor);
|
||||
nameStack.push(peekFromStack(nameStack) + "$" + descriptor.getName().getName());
|
||||
super.visitEnumEntry(enumEntry);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
else {
|
||||
super.visitEnumEntry(enumEntry);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClassObject(JetClassObject classObject) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, classObject.getObjectDeclaration());
|
||||
assert classDescriptor != null;
|
||||
|
||||
JvmClassName name = JvmClassName.byInternalName(peekFromStack(nameStack) + JvmAbi.CLASS_OBJECT_SUFFIX);
|
||||
recordClosure(bindingTrace, classObject, classDescriptor, peekFromStack(classStack), name, false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name.getInternalName());
|
||||
super.visitClassObject(classObject);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitObjectDeclaration(JetObjectDeclaration declaration) {
|
||||
if (declaration.getParent() instanceof JetObjectLiteralExpression || declaration.getParent() instanceof JetClassObject) {
|
||||
super.visitObjectDeclaration(declaration);
|
||||
}
|
||||
else {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, declaration);
|
||||
// working around a problem with shallow analysis
|
||||
if (classDescriptor == null) return;
|
||||
|
||||
String name = getName(classDescriptor);
|
||||
recordClosure(bindingTrace, declaration, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitObjectDeclaration(declaration);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClass(JetClass klass) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, klass);
|
||||
// working around a problem with shallow analysis
|
||||
if (classDescriptor == null) return;
|
||||
|
||||
String name = getName(classDescriptor);
|
||||
recordClosure(bindingTrace, klass, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitClass(klass);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
private String getName(ClassDescriptor classDescriptor) {
|
||||
String base = peekFromStack(nameStack);
|
||||
return classDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor ? base.isEmpty() ? classDescriptor.getName()
|
||||
.getName() : base + '/' + classDescriptor.getName() : base + '$' + classDescriptor.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitObjectLiteralExpression(JetObjectLiteralExpression expression) {
|
||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, expression.getObjectDeclaration());
|
||||
if (classDescriptor == null) {
|
||||
// working around a problem with shallow analysis
|
||||
super.visitObjectLiteralExpression(expression);
|
||||
return;
|
||||
}
|
||||
|
||||
final String name = inventAnonymousClassName(expression.getObjectDeclaration());
|
||||
recordClosure(bindingTrace, expression.getObjectDeclaration(), classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(classNameForClassDescriptor(bindingContext, classDescriptor).getInternalName());
|
||||
super.visitObjectLiteralExpression(expression);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitFunctionLiteralExpression(JetFunctionLiteralExpression expression) {
|
||||
FunctionDescriptor functionDescriptor =
|
||||
(FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, expression);
|
||||
// working around a problem with shallow analysis
|
||||
if (functionDescriptor == null) return;
|
||||
|
||||
String name = inventAnonymousClassName(expression);
|
||||
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
|
||||
recordClosure(bindingTrace, expression, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), true);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitFunctionLiteralExpression(expression);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitProperty(JetProperty property) {
|
||||
nameStack.push(peekFromStack(nameStack) + '$' + property.getName());
|
||||
super.visitProperty(property);
|
||||
nameStack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNamedFunction(JetNamedFunction function) {
|
||||
FunctionDescriptor functionDescriptor =
|
||||
(FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, function);
|
||||
// working around a problem with shallow analysis
|
||||
if (functionDescriptor == null) return;
|
||||
DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration();
|
||||
if (containingDeclaration instanceof ClassDescriptor) {
|
||||
nameStack.push(peekFromStack(nameStack) + '$' + function.getName());
|
||||
super.visitNamedFunction(function);
|
||||
nameStack.pop();
|
||||
}
|
||||
else if (containingDeclaration instanceof NamespaceDescriptor) {
|
||||
String peek = peekFromStack(nameStack);
|
||||
if (peek.isEmpty()) {
|
||||
peek = "namespace";
|
||||
}
|
||||
else {
|
||||
peek += "/namespace";
|
||||
}
|
||||
nameStack.push(peek + '$' + function.getName());
|
||||
super.visitNamedFunction(function);
|
||||
nameStack.pop();
|
||||
}
|
||||
else {
|
||||
String name = inventAnonymousClassName(function);
|
||||
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
|
||||
recordClosure(bindingTrace, function, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), true);
|
||||
|
||||
classStack.push(classDescriptor);
|
||||
nameStack.push(name);
|
||||
super.visitNamedFunction(function);
|
||||
nameStack.pop();
|
||||
classStack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,13 @@ public class CodegenBinding {
|
||||
private CodegenBinding() {
|
||||
}
|
||||
|
||||
public static void initTrace(BindingTrace bindingTrace, Collection<JetFile> files) {
|
||||
CodegenAnnotatingVisitor visitor = new CodegenAnnotatingVisitor(bindingTrace);
|
||||
for (JetFile file : allFilesInNamespaces(bindingTrace.getBindingContext(), files)) {
|
||||
file.accept(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean enumEntryNeedSubclass(BindingContext bindingContext, JetEnumEntry enumEntry) {
|
||||
return enumEntryNeedSubclass(bindingContext, bindingContext.get(CLASS, enumEntry));
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.BuiltinToJavaTypesMapping;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderMode;
|
||||
import org.jetbrains.jet.codegen.context.CodegenAnnotator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@@ -37,7 +36,6 @@ public class InjectorForJetTypeMapper {
|
||||
private JetTypeMapper jetTypeMapper;
|
||||
private BuiltinToJavaTypesMapping builtinToJavaTypesMapping;
|
||||
private ClassBuilderMode classBuilderMode;
|
||||
private CodegenAnnotator codegenAnnotator;
|
||||
|
||||
public InjectorForJetTypeMapper(
|
||||
@NotNull BindingTrace bindingTrace,
|
||||
@@ -49,15 +47,10 @@ public class InjectorForJetTypeMapper {
|
||||
this.jetTypeMapper = new JetTypeMapper();
|
||||
this.builtinToJavaTypesMapping = BuiltinToJavaTypesMapping.ENABLED;
|
||||
this.classBuilderMode = ClassBuilderMode.FULL;
|
||||
this.codegenAnnotator = new CodegenAnnotator();
|
||||
|
||||
this.jetTypeMapper.setBindingContext(bindingContext);
|
||||
this.jetTypeMapper.setBuiltinToJavaTypesMapping(builtinToJavaTypesMapping);
|
||||
this.jetTypeMapper.setClassBuilderMode(classBuilderMode);
|
||||
this.jetTypeMapper.setCodegenAnnotator(codegenAnnotator);
|
||||
|
||||
codegenAnnotator.setBindingTrace(bindingTrace);
|
||||
codegenAnnotator.setFiles(listOfJetFile);
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +62,10 @@ public class InjectorForJetTypeMapper {
|
||||
return this.bindingTrace;
|
||||
}
|
||||
|
||||
public List getListOfJetFile() {
|
||||
return this.listOfJetFile;
|
||||
}
|
||||
|
||||
public JetTypeMapper getJetTypeMapper() {
|
||||
return this.jetTypeMapper;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ 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.context.CodegenAnnotator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@@ -53,7 +52,6 @@ public class InjectorForJvmCodegen {
|
||||
private IntrinsicMethods intrinsics;
|
||||
private ClassFileFactory classFileFactory;
|
||||
private MemberCodegen memberCodegen;
|
||||
private CodegenAnnotator codegenAnnotator;
|
||||
|
||||
public InjectorForJvmCodegen(
|
||||
@NotNull BindingTrace bindingTrace,
|
||||
@@ -78,19 +76,16 @@ public class InjectorForJvmCodegen {
|
||||
this.intrinsics = new IntrinsicMethods();
|
||||
this.classFileFactory = new ClassFileFactory();
|
||||
this.memberCodegen = new MemberCodegen();
|
||||
this.codegenAnnotator = new CodegenAnnotator();
|
||||
|
||||
this.jetTypeMapper.setBindingContext(bindingContext);
|
||||
this.jetTypeMapper.setBuiltinToJavaTypesMapping(builtinToJavaTypesMapping);
|
||||
this.jetTypeMapper.setClassBuilderMode(classBuilderMode);
|
||||
this.jetTypeMapper.setCodegenAnnotator(codegenAnnotator);
|
||||
|
||||
this.classCodegen.setJetTypeMapper(jetTypeMapper);
|
||||
this.classCodegen.setState(generationState);
|
||||
|
||||
this.scriptCodegen.setBindingContext(bindingContext);
|
||||
this.scriptCodegen.setClassFileFactory(classFileFactory);
|
||||
this.scriptCodegen.setCodegenAnnotator(codegenAnnotator);
|
||||
this.scriptCodegen.setJetTypeMapper(jetTypeMapper);
|
||||
this.scriptCodegen.setMemberCodegen(memberCodegen);
|
||||
this.scriptCodegen.setState(generationState);
|
||||
@@ -100,9 +95,6 @@ public class InjectorForJvmCodegen {
|
||||
|
||||
this.memberCodegen.setState(generationState);
|
||||
|
||||
this.codegenAnnotator.setBindingTrace(bindingTrace);
|
||||
this.codegenAnnotator.setFiles(listOfJetFile);
|
||||
|
||||
intrinsics.init();
|
||||
|
||||
}
|
||||
@@ -115,6 +107,10 @@ public class InjectorForJvmCodegen {
|
||||
return this.bindingTrace;
|
||||
}
|
||||
|
||||
public List getListOfJetFile() {
|
||||
return this.listOfJetFile;
|
||||
}
|
||||
|
||||
public GenerationState getGenerationState() {
|
||||
return this.generationState;
|
||||
}
|
||||
@@ -147,8 +143,4 @@ public class InjectorForJvmCodegen {
|
||||
return this.memberCodegen;
|
||||
}
|
||||
|
||||
public CodegenAnnotator getCodegenAnnotator() {
|
||||
return this.codegenAnnotator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.jetbrains.jet.analyzer.AnalyzeExhaust;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.MapTypeMode;
|
||||
import org.jetbrains.jet.codegen.NamespaceCodegen;
|
||||
import org.jetbrains.jet.codegen.context.CodegenBinding;
|
||||
import org.jetbrains.jet.di.InjectorForJetTypeMapper;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
@@ -204,8 +205,11 @@ public class JetPositionManager implements PositionManager {
|
||||
|
||||
List<JetFile> namespaceFiles = JetFilesProvider.getInstance(file.getProject()).allNamespaceFiles().fun(file);
|
||||
|
||||
JetTypeMapper typeMapper = new InjectorForJetTypeMapper(new DelegatingBindingTrace(analyzeExhaust.getBindingContext()), namespaceFiles).getJetTypeMapper();
|
||||
typeMapper.getCodegenAnnotator().init();
|
||||
final DelegatingBindingTrace bindingTrace = new DelegatingBindingTrace(analyzeExhaust.getBindingContext());
|
||||
final InjectorForJetTypeMapper injector = new InjectorForJetTypeMapper(bindingTrace, namespaceFiles);
|
||||
JetTypeMapper typeMapper = injector.getJetTypeMapper();
|
||||
//noinspection unchecked
|
||||
CodegenBinding.initTrace(bindingTrace, injector.getListOfJetFile());
|
||||
return new Result<JetTypeMapper>(typeMapper, PsiModificationTracker.MODIFICATION_COUNT);
|
||||
}
|
||||
}, false);
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.intellij.debugger.engine.DebugProcess;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.codegen.JetTypeMapper;
|
||||
import org.jetbrains.jet.codegen.context.CodegenBinding;
|
||||
import org.jetbrains.jet.di.InjectorForJetTypeMapper;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace;
|
||||
@@ -45,8 +46,11 @@ public class JetPositionManagerTest extends PositionManagerTestCase {
|
||||
JetPositionManager positionManager = (JetPositionManager) jetPositionManagerFactory.createPositionManager(process);
|
||||
assertNotNull(positionManager);
|
||||
|
||||
JetTypeMapper typeMapper = new InjectorForJetTypeMapper(new DelegatingBindingTrace(state.getBindingContext()), files).getJetTypeMapper();
|
||||
typeMapper.getCodegenAnnotator().init();
|
||||
final DelegatingBindingTrace bindingTrace = new DelegatingBindingTrace(state.getBindingContext());
|
||||
final InjectorForJetTypeMapper injector = new InjectorForJetTypeMapper(bindingTrace, files);
|
||||
JetTypeMapper typeMapper = injector.getJetTypeMapper();
|
||||
//noinspection unchecked
|
||||
CodegenBinding.initTrace(bindingTrace, injector.getListOfJetFile());
|
||||
for (JetFile file : files) {
|
||||
positionManager.addTypeMapper(file, typeMapper);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.jetbrains.jet.di;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.jet.codegen.*;
|
||||
import org.jetbrains.jet.codegen.context.CodegenAnnotator;
|
||||
import org.jetbrains.jet.codegen.intrinsics.IntrinsicMethods;
|
||||
import org.jetbrains.jet.lang.BuiltinsScopeExtensionMode;
|
||||
import org.jetbrains.jet.lang.ModuleConfiguration;
|
||||
@@ -194,7 +193,7 @@ public class AllInjectorsGenerator {
|
||||
generator.addPublicParameter(BindingTrace.class);
|
||||
generator.addField(false, BindingContext.class, "bindingContext",
|
||||
new GivenExpression("bindingTrace.getBindingContext()"));
|
||||
generator.addParameter(DiType.listOf(JetFile.class));
|
||||
generator.addPublicParameter(DiType.listOf(JetFile.class));
|
||||
generator.addParameter(BuiltinToJavaTypesMapping.class);
|
||||
generator.addParameter(ClassBuilderMode.class);
|
||||
generator.addPublicParameter(GenerationState.class);
|
||||
@@ -206,7 +205,6 @@ public class AllInjectorsGenerator {
|
||||
generator.addField(true, IntrinsicMethods.class, "intrinsics", null);
|
||||
generator.addPublicField(ClassFileFactory.class);
|
||||
generator.addPublicField(MemberCodegen.class);
|
||||
generator.addPublicField(CodegenAnnotator.class);
|
||||
generator.generate("compiler/backend/src", "org.jetbrains.jet.di", "InjectorForJvmCodegen");
|
||||
}
|
||||
|
||||
@@ -215,7 +213,7 @@ public class AllInjectorsGenerator {
|
||||
generator.addPublicParameter(BindingTrace.class);
|
||||
generator.addField(false, BindingContext.class, "bindingContext",
|
||||
new GivenExpression("bindingTrace.getBindingContext()"));
|
||||
generator.addParameter(DiType.listOf(JetFile.class));
|
||||
generator.addPublicParameter(DiType.listOf(JetFile.class));
|
||||
generator.addPublicField(JetTypeMapper.class);
|
||||
generator.addField(BuiltinToJavaTypesMapping.ENABLED);
|
||||
generator.addField(ClassBuilderMode.FULL);
|
||||
|
||||
Reference in New Issue
Block a user