Merge with Alex's refactroings.

This commit is contained in:
Pavel V. Talanov
2012-08-29 16:48:47 +04:00
60 changed files with 747 additions and 637 deletions
+3
View File
@@ -410,6 +410,9 @@
<inspection_tool class="UtilityClassWithPublicConstructor" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="UtilityClassWithoutPrivateConstructor" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreClassesWithOnlyMain" value="false" />
<option name="ignorableAnnotations">
<value />
</option>
</inspection_tool>
<inspection_tool class="VolatileLongOrDoubleField" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="WaitNotInLoop" enabled="true" level="WARNING" enabled_by_default="true" />
@@ -51,7 +51,7 @@ public abstract class AnnotationCodegen {
private AnnotationCodegen(JetTypeMapper mapper) {
typeMapper = mapper;
bindingContext = typeMapper.bindingContext;
bindingContext = typeMapper.getBindingContext();
}
public void genAnnotations(Annotated annotated) {
@@ -0,0 +1,61 @@
/*
* 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.asm4.Type;
import java.lang.annotation.Annotation;
import java.util.Iterator;
/**
* @author alex.tkachman
*/
public class AsmTypeConstants {
public static final Type OBJECT_TYPE = Type.getType(Object.class);
public static final Type JAVA_NUMBER_TYPE = Type.getType(Number.class);
public static final Type JAVA_STRING_BUILDER_TYPE = Type.getType(StringBuilder.class);
public static final Type JAVA_STRING_TYPE = Type.getType(String.class);
public static final Type JAVA_THROWABLE_TYPE = Type.getType(Throwable.class);
public static final Type JAVA_CLASS_TYPE = Type.getType(Class.class);
public static final Type JAVA_BOOLEAN_TYPE = Type.getType(Boolean.class);
public static final Type JAVA_ARRAY_GENERIC_TYPE = Type.getType(Object[].class);
public static final Type JAVA_CHAR_SEQUENCE_TYPE = Type.getType(CharSequence.class);
public static final Type JAVA_COMPARABLE_TYPE = Type.getType(Comparable.class);
public static final Type JAVA_ENUM_TYPE = Type.getType(Enum.class);
public static final Type JAVA_ANNOTATION_TYPE = Type.getType(Annotation.class);
public static final Type JAVA_ITERATOR_TYPE = Type.getType(Iterator.class);
public static final Type JAVA_ITERABLE_TYPE = Type.getType(Iterable.class);
public static final Type JET_NOTHING_TYPE = Type.getObjectType("jet/Nothing");
public static final Type JET_TUPLE0_TYPE = Type.getObjectType("jet/Tuple0");
public static final Type JET_FUNCTION0_TYPE = Type.getObjectType("jet/Function0");
public static final Type JET_FUNCTION1_TYPE = Type.getObjectType("jet/Function1");
public static final Type JET_ITERATOR_TYPE = Type.getObjectType("jet/Iterator");
public static final Type JET_INT_RANGE_TYPE = Type.getObjectType("jet/IntRange");
public static final Type JET_SHARED_VAR_TYPE = Type.getObjectType("jet/runtime/SharedVar$Object");
public static final Type JET_SHARED_INT_TYPE = Type.getObjectType("jet/runtime/SharedVar$Int");
public static final Type JET_SHARED_DOUBLE_TYPE = Type.getObjectType("jet/runtime/SharedVar$Double");
public static final Type JET_SHARED_FLOAT_TYPE = Type.getObjectType("jet/runtime/SharedVar$Float");
public static final Type JET_SHARED_BYTE_TYPE = Type.getObjectType("jet/runtime/SharedVar$Byte");
public static final Type JET_SHARED_SHORT_TYPE = Type.getObjectType("jet/runtime/SharedVar$Short");
public static final Type JET_SHARED_CHAR_TYPE = Type.getObjectType("jet/runtime/SharedVar$Char");
public static final Type JET_SHARED_LONG_TYPE = Type.getObjectType("jet/runtime/SharedVar$Long");
public static final Type JET_SHARED_BOOLEAN_TYPE = Type.getObjectType("jet/runtime/SharedVar$Boolean");
private AsmTypeConstants() {
}
}
@@ -31,7 +31,8 @@ import java.util.Collections;
import java.util.List;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.getVisibilityAccessFlag;
import static org.jetbrains.jet.codegen.CodegenUtil.generateMethodThrow;
import static org.jetbrains.jet.codegen.CodegenUtil.getVisibilityAccessFlag;
/**
* @author max
@@ -145,7 +146,7 @@ public abstract class ClassBodyCodegen {
// generates stub 'remove' function for subclasses of Iterator to be compatible with java.util.Iterator
if (DescriptorUtils.isIteratorWithoutRemoveImpl(descriptor)) {
final MethodVisitor mv = v.getVisitor().visitMethod(ACC_PUBLIC, "remove", "()V", null, null);
CodegenUtil.generateMethodThrow(mv, "java/lang/UnsupportedOperationException", "Mutating method called on a Kotlin Iterator");
generateMethodThrow(mv, "java/lang/UnsupportedOperationException", "Mutating method called on a Kotlin Iterator");
}
}
}
@@ -46,6 +46,8 @@ import java.util.ArrayList;
import java.util.List;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.context.CodegenBinding.classNameForAnonymousClass;
import static org.jetbrains.jet.codegen.context.CodegenBinding.isLocalNamedFun;
@@ -82,7 +84,7 @@ public class ClosureCodegen {
assert funDescriptor != null;
final List<ValueParameterDescriptor> parameters = funDescriptor.getValueParameters();
final JvmClassName funClass = CodegenUtil.getInternalClassName(funDescriptor);
final JvmClassName funClass = getInternalClassName(funDescriptor);
signatureWriter.visitClassType(funClass.getInternalName());
for (ValueParameterDescriptor parameter : parameters) {
appendType(signatureWriter, parameter.getType(), '=');
@@ -107,11 +109,11 @@ public class ClosureCodegen {
constructor = generateConstructor(funClass, fun, cv, closure);
if (CodegenUtil.isConst(closure)) {
if (isConst(closure)) {
generateConstInstance(fun, cv);
}
CodegenUtil.generateClosureFields(closure, cv, state.getInjector().getJetTypeMapper());
generateClosureFields(closure, cv, state.getInjector().getJetTypeMapper());
cv.done();
@@ -168,7 +170,7 @@ public class ClosureCodegen {
JetExpression fun,
ClassBuilder cv
) {
final JvmMethodSignature bridge = CodegenUtil.erasedInvokeSignature(funDescriptor);
final JvmMethodSignature bridge = erasedInvokeSignature(funDescriptor);
final Method delegate = typeMapper.invokeSignature(funDescriptor).getAsmMethod();
if (bridge.getAsmMethod().getDescriptor().equals(delegate.getDescriptor())) {
@@ -191,24 +193,24 @@ public class ClosureCodegen {
final ReceiverDescriptor receiver = funDescriptor.getReceiverParameter();
int count = 1;
if (receiver.exists()) {
StackValue.local(count, JetTypeMapper.OBJECT_TYPE).put(JetTypeMapper.OBJECT_TYPE, iv);
StackValue.onStack(JetTypeMapper.OBJECT_TYPE)
StackValue.local(count, OBJECT_TYPE).put(OBJECT_TYPE, iv);
StackValue.onStack(OBJECT_TYPE)
.upcast(typeMapper.mapType(receiver.getType(), MapTypeMode.VALUE), iv);
count++;
}
final List<ValueParameterDescriptor> params = funDescriptor.getValueParameters();
for (ValueParameterDescriptor param : params) {
StackValue.local(count, JetTypeMapper.OBJECT_TYPE).put(JetTypeMapper.OBJECT_TYPE, iv);
StackValue.onStack(JetTypeMapper.OBJECT_TYPE)
StackValue.local(count, OBJECT_TYPE).put(OBJECT_TYPE, iv);
StackValue.onStack(OBJECT_TYPE)
.upcast(typeMapper.mapType(param.getType(), MapTypeMode.VALUE), iv);
count++;
}
iv.invokevirtual(className, "invoke", delegate.getDescriptor());
StackValue.onStack(delegate.getReturnType()).put(JetTypeMapper.OBJECT_TYPE, iv);
StackValue.onStack(delegate.getReturnType()).put(OBJECT_TYPE, iv);
iv.areturn(JetTypeMapper.OBJECT_TYPE);
iv.areturn(OBJECT_TYPE);
FunctionCodegen.endVisit(mv, "bridge", fun);
}
@@ -239,7 +241,7 @@ public class ClosureCodegen {
int k = 1;
for (int i = 0; i != argTypes.length; ++i) {
StackValue.local(0, JetTypeMapper.OBJECT_TYPE).put(JetTypeMapper.OBJECT_TYPE, iv);
StackValue.local(0, OBJECT_TYPE).put(OBJECT_TYPE, iv);
final Pair<String, Type> nameAndType = args.get(i);
final Type type = nameAndType.second;
StackValue.local(k, type).put(type, iv);
@@ -262,11 +264,11 @@ public class ClosureCodegen {
final ClassDescriptor captureThis = closure.getCaptureThis();
if (captureThis != null) {
final Type type = typeMapper.mapType(captureThis.getDefaultType(), MapTypeMode.VALUE);
args.add(new Pair<String, Type>(CodegenUtil.THIS$0, type));
args.add(new Pair<String, Type>(THIS$0, type));
}
final ClassifierDescriptor captureReceiver = closure.getCaptureReceiver();
if (captureReceiver != null) {
args.add(new Pair<String, Type>(CodegenUtil.RECEIVER$0,
args.add(new Pair<String, Type>(RECEIVER$0,
typeMapper.mapType(captureReceiver.getDefaultType(), MapTypeMode.VALUE)));
}
@@ -16,9 +16,12 @@
package org.jetbrains.jet.codegen;
import com.google.common.collect.ImmutableMap;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.util.containers.Stack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.MethodVisitor;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
@@ -30,7 +33,9 @@ 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.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.JetType;
@@ -39,6 +44,8 @@ import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import java.util.*;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.AsmTypeConstants.OBJECT_TYPE;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isClassObject;
/**
* @author abreslav
@@ -48,6 +55,18 @@ public class CodegenUtil {
public static final String RECEIVER$0 = "receiver$0";
public static final String THIS$0 = "this$0";
private static final int ACC_LOCAL = 0;
private static final int ACC_PACKAGE_PRIVATE = 0;
@NotNull
private static final Map<Visibility, Integer> visibilityToAccessFlag = ImmutableMap.<Visibility, Integer>builder()
.put(Visibilities.PRIVATE, ACC_PRIVATE)
.put(Visibilities.PROTECTED, ACC_PROTECTED)
.put(Visibilities.PUBLIC, ACC_PUBLIC)
.put(Visibilities.INTERNAL, ACC_PUBLIC)
.put(Visibilities.LOCAL, ACC_LOCAL)
.build();
private CodegenUtil() {
}
@@ -160,14 +179,14 @@ public class CodegenUtil {
for (int i = 0; i < paramCount; ++i) {
signatureWriter.writeParameterType(JvmMethodParameterKind.VALUE);
signatureWriter.writeAsmType(JetTypeMapper.OBJECT_TYPE, true);
signatureWriter.writeAsmType(OBJECT_TYPE, true);
signatureWriter.writeParameterTypeEnd();
}
signatureWriter.writeParametersEnd();
signatureWriter.writeReturnType();
signatureWriter.writeAsmType(JetTypeMapper.OBJECT_TYPE, true);
signatureWriter.writeAsmType(OBJECT_TYPE, true);
signatureWriter.writeReturnTypeEnd();
return signatureWriter.makeJvmMethodSignature("invoke");
@@ -223,4 +242,96 @@ public class CodegenUtil {
public static <T> T peekFromStack(Stack<T> stack) {
return stack.empty() ? null : stack.peek();
}
//TODO: move mapping logic to front-end java
public static int getVisibilityAccessFlag(@NotNull MemberDescriptor descriptor) {
Integer specialCase = specialCaseVisibility(descriptor);
if (specialCase != null) {
return specialCase;
}
Integer defaultMapping = visibilityToAccessFlag.get(descriptor.getVisibility());
if (defaultMapping == null) {
throw new IllegalStateException(descriptor.getVisibility() + " is not a valid visibility in backend.");
}
return defaultMapping;
}
@Nullable
private static Integer specialCaseVisibility(@NotNull MemberDescriptor memberDescriptor) {
DeclarationDescriptor containingDeclaration = memberDescriptor.getContainingDeclaration();
if (isInterface(containingDeclaration)) {
return ACC_PUBLIC;
}
Visibility memberVisibility = memberDescriptor.getVisibility();
if (memberVisibility != Visibilities.PRIVATE) {
return null;
}
if (isClassObject(containingDeclaration)) {
return ACC_PACKAGE_PRIVATE;
}
if (memberDescriptor instanceof ConstructorDescriptor) {
ClassKind kind = ((ClassDescriptor) containingDeclaration).getKind();
if (kind == ClassKind.OBJECT) {
//TODO: should be ACC_PACKAGE_PRIVATE
// see http://youtrack.jetbrains.com/issue/KT-2700
return ACC_PUBLIC;
}
else if (kind == ClassKind.ENUM_ENTRY) {
return ACC_PACKAGE_PRIVATE;
}
else if (kind == ClassKind.ENUM_CLASS) {
//TODO: should be ACC_PRIVATE
// see http://youtrack.jetbrains.com/issue/KT-2680
return ACC_PROTECTED;
}
}
if (containingDeclaration instanceof NamespaceDescriptor) {
return ACC_PUBLIC;
}
return null;
}
public static Type unboxType(final Type type) {
JvmPrimitiveType jvmPrimitiveType = JvmPrimitiveType.getByWrapperAsmType(type);
if (jvmPrimitiveType != null) {
return jvmPrimitiveType.getAsmType();
}
else {
throw new UnsupportedOperationException("Unboxing: " + type);
}
}
public static Type boxType(Type asmType) {
JvmPrimitiveType jvmPrimitiveType = JvmPrimitiveType.getByAsmType(asmType);
if (jvmPrimitiveType != null) {
return jvmPrimitiveType.getWrapper().getAsmType();
}
else {
return asmType;
}
}
public static boolean isIntPrimitive(Type type) {
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
}
public static boolean isPrimitive(Type type) {
return type.getSort() != Type.OBJECT && type.getSort() != Type.ARRAY;
}
public static Type correctElementType(Type type) {
String internalName = type.getInternalName();
assert internalName.charAt(0) == '[';
return Type.getType(internalName.substring(1));
}
@Nullable
public static String getLocalNameForObject(JetObjectDeclaration object) {
PsiElement parent = object.getParent();
if (parent instanceof JetClassObject) {
return JvmAbi.CLASS_OBJECT_CLASS_NAME;
}
return null;
}
}
@@ -26,6 +26,8 @@ import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
/**
* @author yole
* @author alex.tkachman
@@ -34,13 +36,13 @@ public class ConstructorFrameMap extends FrameMap {
private int myOuterThisIndex = -1;
public ConstructorFrameMap(CallableMethod callableMethod, @Nullable ConstructorDescriptor descriptor) {
enterTemp(JetTypeMapper.OBJECT_TYPE); // this
enterTemp(OBJECT_TYPE); // this
final List<JvmMethodParameterSignature> parameterTypes = callableMethod.getSignature().getKotlinParameterTypes();
if (parameterTypes != null) {
for (JvmMethodParameterSignature parameterType : parameterTypes) {
if (parameterType.getKind() == JvmMethodParameterKind.OUTER) {
myOuterThisIndex = enterTemp(JetTypeMapper.OBJECT_TYPE); // this0
myOuterThisIndex = enterTemp(OBJECT_TYPE); // this0
}
else if (parameterType.getKind() != JvmMethodParameterKind.VALUE) {
enterTemp(parameterType.getAsmType());
@@ -55,7 +55,8 @@ import org.jetbrains.jet.lexer.JetTokens;
import java.util.*;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.*;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.context.CodegenBinding.*;
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.*;
@@ -153,7 +154,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
assert provided instanceof ClassDescriptor;
if (!CodegenUtil.isInterface(provided) && CodegenUtil.isInterface(required)) {
if (!isInterface(provided) && isInterface(required)) {
inner.put(OBJECT_TYPE, v);
final Type type = asmType(required.getDefaultType());
v.checkcast(type);
@@ -303,7 +304,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (isEmptyExpression(thenExpression)) {
if (isEmptyExpression(elseExpression)) {
if (!asmType.equals(JetTypeMapper.JET_TUPLE0_TYPE)) {
if (!asmType.equals(JET_TUPLE0_TYPE)) {
throw new CompilationException("Completely empty 'if' is expected to have Unit type", null, expression);
}
StackValue.putTuple0Instance(v);
@@ -1019,7 +1020,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
final JvmClassName className = closureCodegen.name;
final Type asmType = className.getAsmType();
final String internalName = className.getInternalName();
if (CodegenUtil.isConst(closure)) {
if (isConst(closure)) {
v.invokestatic(internalName, "$getInstance", "()" + className.getDescriptor());
}
else {
@@ -1041,7 +1042,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
assert constructorDescriptor != null;
CallableMethod constructor = typeMapper.mapToCallableMethod(constructorDescriptor, closure);
final JvmClassName name = closure.getClassName();
final JvmClassName name = bindingContext.get(FQN, constructorDescriptor.getContainingDeclaration());
assert name != null;
Type type = name.getAsmType();
v.anew(type);
v.dup();
@@ -1056,8 +1059,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
.getCalleeExpression()
.getConstructorReferenceExpression());
assert superConstructor != null;
//noinspection SuspiciousMethodCalls
CallableMethod superCallable = typeMapper
.mapToCallableMethod(superConstructor, typeMapper.getCalculatedClosure(superConstructor.getContainingDeclaration()));
.mapToCallableMethod(superConstructor,
bindingContext.get(CLOSURE, superConstructor.getContainingDeclaration()));
Type[] argumentTypes = superCallable.getSignature().getAsmMethod().getArgumentTypes();
ResolvedCall resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, superCall.getCalleeExpression());
assert resolvedCall != null;
@@ -1283,7 +1288,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
VariableAsFunctionResolvedCall call = (VariableAsFunctionResolvedCall) resolvedCall;
resolvedCall = call.getVariableCall();
}
receiver = StackValue.receiver(resolvedCall, receiver, this, null, state);
receiver = StackValue.receiver(resolvedCall, receiver, this, null);
descriptor = resolvedCall.getResultingDescriptor();
}
@@ -1355,8 +1360,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
receiver.put(receiverType != null && !isSuper ? asmType(receiverType) : OBJECT_TYPE, v);
if (receiverType != null) {
ClassDescriptor propReceiverDescriptor = (ClassDescriptor) propertyDescriptor.getContainingDeclaration();
if (!CodegenUtil.isInterface(propReceiverDescriptor) &&
CodegenUtil.isInterface(receiverType.getConstructor().getDeclarationDescriptor())) {
if (!isInterface(propReceiverDescriptor) &&
isInterface(receiverType.getConstructor().getDeclarationDescriptor())) {
v.checkcast(asmType(propReceiverDescriptor.getDefaultType()));
}
}
@@ -1503,7 +1508,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
PsiElement enclosingElement = bindingContext.get(BindingContext.LABEL_TARGET, superExpression.getTargetLabel());
ClassDescriptor enclosed =
(ClassDescriptor) bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, enclosingElement);
if (!CodegenUtil.isInterface(containingDeclaration)) {
if (!isInterface(containingDeclaration)) {
if (enclosed != null && enclosed != context.getThisDescriptor()) {
CodegenContext c = context;
while (c.getContextDescriptor() != enclosed) {
@@ -1563,11 +1568,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
: INVOKEVIRTUAL;
}
else {
isInterface = CodegenUtil.isInterface(containingDeclaration) || overridesTrait;
isInterface = isInterface(containingDeclaration) || overridesTrait;
// TODO ugly
CallableMethod callableMethod = typeMapper.mapToCallableMethod(propertyDescriptor.getGetter(), isSuper, contextKind());
invokeOpcode = callableMethod.getInvokeOpcode();
owner = isFakeOverride && !overridesTrait && !CodegenUtil.isInterface(initialDescriptor.getContainingDeclaration())
owner = isFakeOverride && !overridesTrait && !isInterface(initialDescriptor.getContainingDeclaration())
? JvmClassName.byType(typeMapper.mapType(
((ClassDescriptor) initialDescriptor.getContainingDeclaration()).getDefaultType(), MapTypeMode.IMPL))
: callableMethod.getOwner();
@@ -1605,7 +1610,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (propertyDescriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
final Set<? extends CallableMemberDescriptor> overriddenDescriptors = propertyDescriptor.getOverriddenDescriptors();
for (CallableMemberDescriptor descriptor : overriddenDescriptors) {
if (CodegenUtil.isInterface(descriptor.getContainingDeclaration())) {
if (isInterface(descriptor.getContainingDeclaration())) {
return true;
}
}
@@ -1626,7 +1631,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
DeclarationDescriptor funDescriptor = resolvedCall.getResultingDescriptor();
if (funDescriptor instanceof ConstructorDescriptor) {
receiver = StackValue.receiver(resolvedCall, receiver, this, null, state);
receiver = StackValue.receiver(resolvedCall, receiver, this, null);
return generateConstructorCall(expression, (JetSimpleNameExpression) callee, receiver);
}
else if (funDescriptor instanceof FunctionDescriptor) {
@@ -1661,7 +1666,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
JetSuperExpression superExpression = (JetSuperExpression) receiverExpression;
PsiElement enclosingElement = bindingContext.get(BindingContext.LABEL_TARGET, superExpression.getTargetLabel());
ClassDescriptor enclosed = (ClassDescriptor) bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, enclosingElement);
if (!CodegenUtil.isInterface(fd.getContainingDeclaration())) {
if (!isInterface(fd.getContainingDeclaration())) {
if (enclosed == null) {
enclosed = (ClassDescriptor) fd.getContainingDeclaration();
}
@@ -1706,7 +1711,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return returnValueAsStackValue(fd, callReturnType);
}
else {
receiver = StackValue.receiver(resolvedCall, receiver, this, null, state);
receiver = StackValue.receiver(resolvedCall, receiver, this, null);
IntrinsicMethod intrinsic = (IntrinsicMethod) callable;
List<JetExpression> args = new ArrayList<JetExpression>();
@@ -1747,7 +1752,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
// callableMethod = ClosureCodegen.asCallableMethod((FunctionDescriptor) fd);
//}
if (isCallAsFunctionObject(fd)) {
SimpleFunctionDescriptor invoke = CodegenUtil.createInvoke(fd);
SimpleFunctionDescriptor invoke = createInvoke(fd);
callableMethod = typeMapper.asCallableMethod(invoke);
}
else {
@@ -1802,7 +1807,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
if (!(resolvedCall.getResultingDescriptor() instanceof ConstructorDescriptor)) { // otherwise already
receiver = StackValue.receiver(resolvedCall, receiver, this, callableMethod, state);
receiver = StackValue.receiver(resolvedCall, receiver, this, callableMethod);
receiver.put(receiver.type, v);
if (calleeType != null) {
StackValue.onStack(receiver.type).put(boxType(receiver.type), v);
@@ -1823,7 +1828,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
ResolvedCall<? extends CallableDescriptor> resolvedCall,
CallableMethod callableMethod
) {
receiver = StackValue.receiver(resolvedCall, receiver, this, callableMethod, state);
receiver = StackValue.receiver(resolvedCall, receiver, this, callableMethod);
receiver.put(receiver.type, v);
}
@@ -1934,7 +1939,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
public StackValue generateThisOrOuter(@NotNull final ClassDescriptor calleeContainingClass) {
PsiElement psiElement = classDescriptorToDeclaration(bindingContext, calleeContainingClass);
boolean isObject = psiElement instanceof JetClassOrObject && CodegenUtil.isNonLiteralObject((JetClassOrObject) psiElement);
boolean isObject = psiElement instanceof JetClassOrObject && isNonLiteralObject((JetClassOrObject) psiElement);
CodegenContext cur = context;
Type type = asmType(calleeContainingClass.getDefaultType());
@@ -2057,7 +2062,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
v.dup();
ValueArgument argument = arguments.get(i);
if (argument.getSpreadElement() != null) {
gen(argument.getArgumentExpression(), JetTypeMapper.OBJECT_TYPE);
gen(argument.getArgumentExpression(), OBJECT_TYPE);
v.invokevirtual(owner, "addSpread", "(Ljava/lang/Object;)V");
}
else {
@@ -2109,7 +2114,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
public int indexOfLocal(JetReferenceExpression lhs) {
final DeclarationDescriptor declarationDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, lhs);
if (typeMapper.isVarCapturedInClosure(declarationDescriptor)) {
if (isVarCapturedInClosure(typeMapper.getBindingContext(), declarationDescriptor)) {
return -1;
}
return lookupLocalIndex(declarationDescriptor);
@@ -2538,7 +2543,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
StackValue receiver = StackValue.onStack(lhsType);
if (!(resolvedCall.getResultingDescriptor() instanceof ConstructorDescriptor)) { // otherwise already
receiver = StackValue.receiver(resolvedCall, receiver, this, callable, state);
receiver = StackValue.receiver(resolvedCall, receiver, this, callable);
receiver.put(receiver.type, v);
}
@@ -2566,7 +2571,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
Type leftType = expressionType(left);
Type rightType = expressionType(right);
if (leftType.equals(JetTypeMapper.JAVA_STRING_TYPE) && rightType.equals(JAVA_STRING_TYPE)) {
if (leftType.equals(JAVA_STRING_TYPE) && rightType.equals(JAVA_STRING_TYPE)) {
invokeAppend(left);
invokeAppend(right);
return;
@@ -2639,7 +2644,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
private StackValue invokeOperation(JetOperationExpression expression, FunctionDescriptor op, CallableMethod callable) {
int functionLocalIndex = lookupLocalIndex(op);
if (functionLocalIndex >= 0) {
stackValueForLocal(op, functionLocalIndex).put(CodegenUtil.getInternalClassName(op).getAsmType(), v);
stackValueForLocal(op, functionLocalIndex).put(getInternalClassName(op).getAsmType(), v);
}
ResolvedCall<? extends CallableDescriptor> resolvedCall =
bindingContext.get(BindingContext.RESOLVED_CALL, expression.getOperationReference());
@@ -2881,12 +2886,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
v.dup();
final ClassDescriptor classDescriptor = ((ConstructorDescriptor) constructorDescriptor).getContainingDeclaration();
CallableMethod method = typeMapper
.mapToCallableMethod((ConstructorDescriptor) constructorDescriptor, typeMapper
.getCalculatedClosure(classDescriptor));
.mapToCallableMethod((ConstructorDescriptor) constructorDescriptor,
bindingContext.get(CLOSURE, classDescriptor));
receiver.put(receiver.type, v);
pushClosureOnStack(typeMapper.getCalculatedClosure(classDescriptor), true);
pushClosureOnStack(bindingContext.get(CLOSURE, classDescriptor), true);
invokeMethodWithArguments(method, expression, StackValue.none());
}
}
@@ -3196,7 +3203,7 @@ The "returned" value of try expression with no finally is either the last expres
DeclarationDescriptor descriptor = rightType.getConstructor().getDeclarationDescriptor();
if (descriptor instanceof ClassDescriptor || descriptor instanceof TypeParameterDescriptor) {
StackValue value = genQualified(receiver, left);
value.put(JetTypeMapper.boxType(value.type), v);
value.put(boxType(value.type), v);
assert leftType != null;
if (opToken != JetTokens.AS_SAFE) {
@@ -3264,7 +3271,7 @@ The "returned" value of try expression with no finally is either the last expres
assert condJetType != null;
condType = asmType(condJetType);
if (!(isNumberPrimitive(condType) || condType.getSort() == Type.BOOLEAN)) {
subjectType = JetTypeMapper.boxType(subjectType);
subjectType = boxType(subjectType);
expressionToMatch.coerce(subjectType, v);
}
}
@@ -42,7 +42,9 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import java.util.*;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.getVisibilityAccessFlag;
import static org.jetbrains.jet.codegen.AsmTypeConstants.JAVA_ARRAY_GENERIC_TYPE;
import static org.jetbrains.jet.codegen.AsmTypeConstants.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.context.CodegenBinding.isLocalFun;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.callableDescriptorToDeclaration;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
@@ -132,7 +134,7 @@ public class FunctionCodegen {
boolean isAbstract = (
modality == Modality.ABSTRACT
|| CodegenUtil.isInterface(functionDescriptor.getContainingDeclaration())
|| isInterface(functionDescriptor.getContainingDeclaration())
) && !isStatic && kind != OwnerKind.TRAIT_IMPL;
if (isAbstract) flags |= ACC_ABSTRACT;
@@ -154,8 +156,8 @@ public class FunctionCodegen {
throw new IllegalStateException();
}
JetMethodAnnotationWriter aw = JetMethodAnnotationWriter.visitAnnotation(mv);
BitSet kotlinFlags = CodegenUtil.getFlagsForVisibility(functionDescriptor.getVisibility());
if (CodegenUtil.isInterface(functionDescriptor.getContainingDeclaration()) && modality != Modality.ABSTRACT) {
BitSet kotlinFlags = getFlagsForVisibility(functionDescriptor.getVisibility());
if (isInterface(functionDescriptor.getContainingDeclaration()) && modality != Modality.ABSTRACT) {
kotlinFlags.set(modality == Modality.FINAL
? JvmStdlibNames.FLAG_FORCE_FINAL_BIT
: JvmStdlibNames.FLAG_FORCE_OPEN_BIT);
@@ -249,8 +251,8 @@ public class FunctionCodegen {
else if (kind instanceof OwnerKind.DelegateKind) {
OwnerKind.DelegateKind dk = (OwnerKind.DelegateKind) kind;
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
dk.getDelegate().put(JetTypeMapper.OBJECT_TYPE, iv);
iv.load(0, OBJECT_TYPE);
dk.getDelegate().put(OBJECT_TYPE, iv);
for (int i = 0; i < argTypes.length; i++) {
Type argType = argTypes[i];
iv.load(i + 1, argType);
@@ -325,7 +327,7 @@ public class FunctionCodegen {
Type sharedVarType = state.getInjector().getJetTypeMapper().getSharedVarType(parameter);
mv.visitLocalVariable(parameterName, type.getDescriptor(), null, methodBegin, divideLabel, k);
String nameForSharedVar = CodegenUtil.generateTmpVariableName(localVariableNames);
String nameForSharedVar = generateTmpVariableName(localVariableNames);
localVariableNames.add(nameForSharedVar);
mv.visitLocalVariable(nameForSharedVar, sharedVarType.getDescriptor(), null, divideLabel, methodEnd, k);
@@ -498,7 +500,7 @@ public class FunctionCodegen {
FrameMap frameMap = owner.prepareFrame(state.getInjector().getJetTypeMapper());
if (kind instanceof OwnerKind.StaticDelegateKind) {
frameMap.leaveTemp(JetTypeMapper.OBJECT_TYPE);
frameMap.leaveTemp(OBJECT_TYPE);
}
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, jvmSignature.getReturnType(), owner, state);
@@ -646,15 +648,15 @@ public class FunctionCodegen {
Type[] argTypes = overridden.getArgumentTypes();
Type[] originalArgTypes = jvmSignature.getArgumentTypes();
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
for (int i = 0, reg = 1; i < argTypes.length; i++) {
Type argType = argTypes[i];
iv.load(reg, argType);
if (argType.getSort() == Type.OBJECT) {
StackValue.onStack(JetTypeMapper.OBJECT_TYPE).put(originalArgTypes[i], iv);
StackValue.onStack(OBJECT_TYPE).put(originalArgTypes[i], iv);
}
else if (argType.getSort() == Type.ARRAY) {
StackValue.onStack(JetTypeMapper.JAVA_ARRAY_GENERIC_TYPE).put(originalArgTypes[i], iv);
StackValue.onStack(JAVA_ARRAY_GENERIC_TYPE).put(originalArgTypes[i], iv);
}
//noinspection AssignmentToForLoopParameter
@@ -664,7 +666,7 @@ public class FunctionCodegen {
iv.invokevirtual(state.getInjector().getJetTypeMapper().mapType(
((ClassDescriptor) owner.getContextDescriptor()).getDefaultType(), MapTypeMode.VALUE).getInternalName(),
jvmSignature.getName(), jvmSignature.getDescriptor());
if (JetTypeMapper.isPrimitive(jvmSignature.getReturnType()) && !JetTypeMapper.isPrimitive(overridden.getReturnType())) {
if (isPrimitive(jvmSignature.getReturnType()) && !isPrimitive(overridden.getReturnType())) {
StackValue.valueOf(iv, jvmSignature.getReturnType());
}
if (jvmSignature.getReturnType() == Type.VOID_TYPE) {
@@ -699,22 +701,22 @@ public class FunctionCodegen {
Type[] argTypes = method.getArgumentTypes();
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
for (int i = 0, reg = 1; i < argTypes.length; i++) {
Type argType = argTypes[i];
iv.load(reg, argType);
if (argType.getSort() == Type.OBJECT) {
StackValue.onStack(JetTypeMapper.OBJECT_TYPE).put(method.getArgumentTypes()[i], iv);
StackValue.onStack(OBJECT_TYPE).put(method.getArgumentTypes()[i], iv);
}
else if (argType.getSort() == Type.ARRAY) {
StackValue.onStack(JetTypeMapper.JAVA_ARRAY_GENERIC_TYPE).put(method.getArgumentTypes()[i], iv);
StackValue.onStack(JAVA_ARRAY_GENERIC_TYPE).put(method.getArgumentTypes()[i], iv);
}
//noinspection AssignmentToForLoopParameter
reg += argType.getSize();
}
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
field.put(field.type, iv);
ClassDescriptor classDescriptor = (ClassDescriptor) overriddenDescriptor.getContainingDeclaration();
String internalName =
@@ -27,6 +27,7 @@ import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.asm4.commons.Method;
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.codegen.context.MutableClosure;
import org.jetbrains.jet.codegen.signature.*;
@@ -49,8 +50,9 @@ import org.jetbrains.jet.utils.BitSetUtils;
import java.util.*;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.JetTypeMapper.getVisibilityAccessFlag;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
import static org.jetbrains.jet.codegen.context.CodegenBinding.CLOSURE;
import static org.jetbrains.jet.codegen.context.CodegenBinding.eclosingClassDescriptor;
import static org.jetbrains.jet.codegen.context.CodegenBinding.enumEntryNeedSubclass;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.callableDescriptorToDeclaration;
@@ -203,7 +205,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (signature.getKotlinGenericSignature() != null || descriptor.getVisibility() != Visibilities.PUBLIC) {
AnnotationVisitor annotationVisitor = v.newAnnotation(JvmStdlibNames.JET_CLASS.getDescriptor(), true);
annotationVisitor.visit(JvmStdlibNames.JET_CLASS_SIGNATURE, signature.getKotlinGenericSignature());
BitSet flags = CodegenUtil.getFlagsForVisibility(descriptor.getVisibility());
BitSet flags = getFlagsForVisibility(descriptor.getVisibility());
int flagsValue = BitSetUtils.toInt(flags);
if (JvmStdlibNames.FLAGS_DEFAULT_VALUE != flagsValue) {
annotationVisitor.visit(JvmStdlibNames.JET_CLASS_FLAGS_FIELD, flagsValue);
@@ -258,7 +260,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference());
assert superType != null;
ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
if (CodegenUtil.isInterface(superClassDescriptor)) {
if (isInterface(superClassDescriptor)) {
signatureVisitor.writeInterface();
Type jvmName = typeMapper.mapType(superType, signatureVisitor, MapTypeMode.TYPE_PARAMETER);
signatureVisitor.writeInterfaceEnd();
@@ -302,7 +304,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
assert superType != null;
ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
assert superClassDescriptor != null;
if (!CodegenUtil.isInterface(superClassDescriptor)) {
if (!isInterface(superClassDescriptor)) {
superClassType = superType;
superClass = typeMapper.mapType(superClassDescriptor.getDefaultType(), MapTypeMode.IMPL).getInternalName();
superCall = specifier;
@@ -346,7 +348,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
generateEnumMethods();
CodegenUtil.generateClosureFields(context.closure, v, state.getInjector().getJetTypeMapper());
generateClosureFields(context.closure, v, state.getInjector().getJetTypeMapper());
}
private void generateEnumMethods() {
@@ -407,7 +409,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
for (int i = 1, reg = 1; i < argTypes.length; i++) {
Type argType = argTypes[i];
iv.load(reg, argType);
@@ -444,7 +446,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
if (original.getVisibility() == Visibilities.PRIVATE) {
iv.getfield(typeMapper.getOwner(original, OwnerKind.IMPLEMENTATION).getInternalName(), original.getName().getName(),
originalMethod.getReturnType().getDescriptor());
@@ -479,7 +481,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
Type[] argTypes = method.getArgumentTypes();
for (int i = 1, reg = 1; i < argTypes.length; i++) {
Type argType = argTypes[i];
@@ -507,7 +509,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private void generateFieldForObjectInstance() {
if (CodegenUtil.isNonLiteralObject(myClass)) {
if (isNonLiteralObject(myClass)) {
Type type = typeMapper.mapType(descriptor.getDefaultType(), MapTypeMode.VALUE);
v.newField(myClass, ACC_PUBLIC | ACC_STATIC | ACC_FINAL, "$instance", type.getDescriptor(), null, null);
@@ -576,7 +578,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
AnnotationVisitor jetConstructorVisitor = mv.visitAnnotation(JvmStdlibNames.JET_CONSTRUCTOR.getDescriptor(), true);
int flagsValue = BitSetUtils.toInt(CodegenUtil.getFlagsForVisibility(constructorDescriptor.getVisibility()));
int flagsValue = BitSetUtils.toInt(getFlagsForVisibility(constructorDescriptor.getVisibility()));
if (JvmStdlibNames.FLAGS_DEFAULT_VALUE != flagsValue) {
jetConstructorVisitor.visit(JvmStdlibNames.JET_CLASS_FLAGS_FIELD, flagsValue);
}
@@ -630,11 +632,11 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (hasThis0) {
final Type type = typeMapper
.mapType(eclosingClassDescriptor(typeMapper.bindingContext, descriptor).getDefaultType(), MapTypeMode.VALUE);
.mapType(eclosingClassDescriptor(typeMapper.getBindingContext(), descriptor).getDefaultType(), MapTypeMode.VALUE);
String interfaceDesc = type.getDescriptor();
iv.load(0, classType);
iv.load(frameMap.getOuterThisIndex(), type);
iv.putfield(classname.getInternalName(), CodegenUtil.THIS$0, interfaceDesc);
iv.putfield(classname.getInternalName(), THIS$0, interfaceDesc);
}
if (closure != null) {
@@ -642,10 +644,10 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
final String internalName = typeMapper.mapType(descriptor.getDefaultType(), MapTypeMode.VALUE).getInternalName();
final ClassifierDescriptor captureReceiver = closure.getCaptureReceiver();
if (captureReceiver != null) {
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
final Type asmType = typeMapper.mapType(captureReceiver.getDefaultType(), MapTypeMode.IMPL);
iv.load(1, asmType);
iv.putfield(internalName, CodegenUtil.RECEIVER$0, asmType.getDescriptor());
iv.putfield(internalName, RECEIVER$0, asmType.getDescriptor());
k += asmType.getSize();
}
@@ -655,7 +657,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (sharedVarType == null) {
sharedVarType = typeMapper.mapType(((VariableDescriptor) varDescr).getType(), MapTypeMode.VALUE);
}
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
iv.load(k, StackValue.refType(sharedVarType));
k += StackValue.refType(sharedVarType).getSize();
iv.putfield(internalName,
@@ -704,10 +706,10 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
List<Type> parameterTypes = new ArrayList<Type>();
assert superType != null;
ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
if (typeMapper.hasThis0(superClassDescriptor)) {
iv.load(1, JetTypeMapper.OBJECT_TYPE);
if (CodegenBinding.hasThis0(bindingContext, superClassDescriptor)) {
iv.load(1, OBJECT_TYPE);
parameterTypes.add(typeMapper.mapType(
eclosingClassDescriptor(typeMapper.bindingContext, descriptor).getDefaultType(), MapTypeMode.VALUE));
eclosingClassDescriptor(typeMapper.getBindingContext(), descriptor).getDefaultType(), MapTypeMode.VALUE));
}
Method superCallMethod = new Method("<init>", Type.VOID_TYPE, parameterTypes.toArray(new Type[parameterTypes.size()]));
//noinspection ConstantConditions
@@ -718,7 +720,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
private void genSimpleSuperCall(InstructionAdapter iv) {
iv.load(0, Type.getType("L" + superClass + ";"));
if (descriptor.getKind() == ClassKind.ENUM_CLASS || descriptor.getKind() == ClassKind.ENUM_ENTRY) {
iv.load(1, JetTypeMapper.JAVA_STRING_TYPE);
iv.load(1, AsmTypeConstants.JAVA_STRING_TYPE);
iv.load(2, Type.INT_TYPE);
iv.invokespecial(superClass, "<init>", "(Ljava/lang/String;I)V");
}
@@ -791,46 +793,6 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
generateDelegates(superClass, delegateContext, field);
}
private int addClosureToConstructorParameters(
MutableClosure closure,
List<JvmMethodParameterSignature> consArgTypes
) {
int insert = 0;
if (closure != null) {
if (closure.getCaptureThis() != null) {
consArgTypes.add(insert,
new JvmMethodParameterSignature(Type.getObjectType(context.getThisDescriptor().getName().getName()),
"", JvmMethodParameterKind.OUTER));
insert++;
}
final ClassifierDescriptor captureReceiver = closure.getCaptureReceiver();
if (captureReceiver != null) {
final Type asmType = typeMapper.mapType(captureReceiver.getDefaultType(), MapTypeMode.IMPL);
consArgTypes.add(insert++, new JvmMethodParameterSignature(asmType, "", JvmMethodParameterKind.RECEIVER));
}
for (DeclarationDescriptor descriptor : closure.getCaptureVariables().keySet()) {
if (descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) {
final Type sharedVarType = typeMapper.getSharedVarType(descriptor);
final Type type;
if (sharedVarType != null) {
type = sharedVarType;
}
else {
type = state.getInjector().getJetTypeMapper()
.mapType(((VariableDescriptor) descriptor).getType(), MapTypeMode.VALUE);
}
consArgTypes.add(insert++, new JvmMethodParameterSignature(type, "", JvmMethodParameterKind.SHARED_VAR));
}
else if (descriptor instanceof FunctionDescriptor) {
assert captureReceiver != null;
}
}
}
return insert;
}
private void lookupConstructorExpressionsInClosureIfPresent(final CodegenContext.ConstructorContext constructorContext) {
final JetVisitorVoid visitor = new JetVisitorVoid() {
@Override
@@ -963,7 +925,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
JvmMethodSignature jvmSignature =
typeMapper.mapToCallableMethod(inheritedFun, false, OwnerKind.IMPLEMENTATION).getSignature();
JetMethodAnnotationWriter aw = JetMethodAnnotationWriter.visitAnnotation(mv);
BitSet kotlinFlags = CodegenUtil.getFlagsForVisibility(fun.getVisibility());
BitSet kotlinFlags = getFlagsForVisibility(fun.getVisibility());
if (fun instanceof PropertyAccessorDescriptor) {
kotlinFlags.set(JvmStdlibNames.FLAG_PROPERTY_BIT);
aw.writeTypeParameters(jvmSignature.getKotlinTypeParameter());
@@ -992,7 +954,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
Type[] argTypes = function.getArgumentTypes();
List<Type> originalArgTypes = jvmSignature.getValueParameterTypes();
InstructionAdapter iv = new InstructionAdapter(mv);
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
for (int i = 0, reg = 1; i < argTypes.length; i++) {
Type argType = argTypes[i];
iv.load(reg, argType);
@@ -1046,7 +1008,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
assert resolvedCall != null;
final ConstructorDescriptor superConstructor = (ConstructorDescriptor) resolvedCall.getResultingDescriptor();
final CalculatedClosure closureForSuper = typeMapper.getCalculatedClosure(superConstructor.getContainingDeclaration());
//noinspection SuspiciousMethodCalls
final CalculatedClosure closureForSuper = bindingContext.get(CLOSURE, superConstructor.getContainingDeclaration());
CallableMethod superCallable = typeMapper.mapToCallableMethod(superConstructor,
closureForSuper);
@@ -1153,8 +1116,9 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
ConstructorDescriptor constructorDescriptor = (ConstructorDescriptor) bindingContext
.get(BindingContext.REFERENCE_TARGET, superCall.getCalleeExpression().getConstructorReferenceExpression());
assert constructorDescriptor != null;
CallableMethod method = typeMapper.mapToCallableMethod(constructorDescriptor, typeMapper
.getCalculatedClosure(constructorDescriptor.getContainingDeclaration()));
//noinspection SuspiciousMethodCalls
CallableMethod method = typeMapper.mapToCallableMethod(constructorDescriptor, bindingContext
.get(CLOSURE, constructorDescriptor.getContainingDeclaration()));
codegen.invokeMethodWithArguments(method, superCall, StackValue.none());
}
else {
@@ -1192,7 +1156,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.load(0, OBJECT_TYPE);
Type type = codegen.expressionType(initializer);
if (jetType.isNullable()) {
type = JetTypeMapper.boxType(type);
type = boxType(type);
}
codegen.gen(initializer, type);
// @todo write directly to the field. Fix test excloset.jet::test6
@@ -1210,7 +1174,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
private static boolean skipDefaultValue(PropertyDescriptor propertyDescriptor, Object value, Type type) {
if (JetTypeMapper.isPrimitive(type)) {
if (isPrimitive(type)) {
if (!propertyDescriptor.getType().isNullable() && value instanceof Number) {
if (type == Type.INT_TYPE && ((Number) value).intValue() == 0) {
return true;
@@ -1300,7 +1264,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
OverridingUtil.getOverriddenDeclarations(callableMemberDescriptor);
for (CallableMemberDescriptor overriddenDeclaration : overriddenDeclarations) {
if (overriddenDeclaration.getModality() != Modality.ABSTRACT) {
if (!CodegenUtil.isInterface(overriddenDeclaration.getContainingDeclaration())) {
if (!isInterface(overriddenDeclaration.getContainingDeclaration())) {
continue root;
}
}
@@ -16,7 +16,6 @@
package org.jetbrains.jet.codegen;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -25,11 +24,10 @@ import org.jetbrains.jet.codegen.context.CalculatedClosure;
import org.jetbrains.jet.codegen.context.EnclosedValueDescriptor;
import org.jetbrains.jet.codegen.signature.*;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.JetClassObject;
import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.*;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -45,60 +43,25 @@ import java.util.List;
import java.util.Map;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.codegen.context.CodegenBinding.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isClassObject;
/**
* @author yole
* @author alex.tkachman
*/
public class JetTypeMapper {
public static final Type OBJECT_TYPE = Type.getType(Object.class);
public static final Type JAVA_NUMBER_TYPE = Type.getType(Number.class);
public static final Type JAVA_STRING_BUILDER_TYPE = Type.getType(StringBuilder.class);
public static final Type JAVA_STRING_TYPE = Type.getType(String.class);
public static final Type JAVA_THROWABLE_TYPE = Type.getType(Throwable.class);
public static final Type JAVA_CLASS_TYPE = Type.getType(Class.class);
public static final Type JAVA_BOOLEAN_TYPE = Type.getType(Boolean.class);
public static final Type JAVA_ARRAY_GENERIC_TYPE = Type.getType(Object[].class);
public static final Type JET_NOTHING_TYPE = Type.getObjectType("jet/Nothing");
public static final Type JET_TUPLE0_TYPE = Type.getObjectType("jet/Tuple0");
public static final Type JET_FUNCTION0_TYPE = Type.getObjectType("jet/Function0");
public static final Type JET_FUNCTION1_TYPE = Type.getObjectType("jet/Function1");
public static final Type JET_ITERATOR_TYPE = Type.getObjectType("jet/Iterator");
public static final Type JET_INT_RANGE_TYPE = Type.getObjectType("jet/IntRange");
public static final Type JET_SHARED_VAR_TYPE = Type.getObjectType("jet/runtime/SharedVar$Object");
public static final Type JET_SHARED_INT_TYPE = Type.getObjectType("jet/runtime/SharedVar$Int");
public static final Type JET_SHARED_DOUBLE_TYPE = Type.getObjectType("jet/runtime/SharedVar$Double");
public static final Type JET_SHARED_FLOAT_TYPE = Type.getObjectType("jet/runtime/SharedVar$Float");
public static final Type JET_SHARED_BYTE_TYPE = Type.getObjectType("jet/runtime/SharedVar$Byte");
public static final Type JET_SHARED_SHORT_TYPE = Type.getObjectType("jet/runtime/SharedVar$Short");
public static final Type JET_SHARED_CHAR_TYPE = Type.getObjectType("jet/runtime/SharedVar$Char");
public static final Type JET_SHARED_LONG_TYPE = Type.getObjectType("jet/runtime/SharedVar$Long");
public static final Type JET_SHARED_BOOLEAN_TYPE = Type.getObjectType("jet/runtime/SharedVar$Boolean");
public BindingContext bindingContext;
private BindingContext bindingContext;
private boolean mapBuiltinsToJava;
private ClassBuilderMode classBuilderMode;
private static final int ACC_LOCAL = 0;
private static final int ACC_PACKAGE_PRIVATE = 0;
@NotNull
private static final Map<Visibility, Integer> visibilityToAccessFlag = ImmutableMap.<Visibility, Integer>builder()
.put(Visibilities.PRIVATE, ACC_PRIVATE)
.put(Visibilities.PROTECTED, ACC_PROTECTED)
.put(Visibilities.PUBLIC, ACC_PUBLIC)
.put(Visibilities.INTERNAL, ACC_PUBLIC)
.put(Visibilities.LOCAL, ACC_LOCAL)
.build();
private BindingTrace bindingTrace;
@Inject
public void setBindingContext(BindingContext bindingContext) {
this.bindingContext = bindingContext;
public void setBindingTrace(BindingTrace bindingTrace) {
this.bindingTrace = bindingTrace;
this.bindingContext = bindingTrace.getBindingContext();
}
@Inject
@@ -111,29 +74,12 @@ public class JetTypeMapper {
this.classBuilderMode = classBuilderMode;
}
public boolean hasThis0(ClassDescriptor classDescriptor) {
//noinspection SuspiciousMethodCalls
final CalculatedClosure closure = bindingContext.get(CLOSURE, classDescriptor);
return closure != null && closure.getCaptureThis() != null;
public BindingTrace getBindingTrace() {
return bindingTrace;
}
public CalculatedClosure getCalculatedClosure(ClassDescriptor classDescriptor) {
//noinspection SuspiciousMethodCalls
return bindingContext.get(CLOSURE, classDescriptor);
}
public static boolean isIntPrimitive(Type type) {
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
}
public static boolean isPrimitive(Type type) {
return type.getSort() != Type.OBJECT && type.getSort() != Type.ARRAY;
}
public static Type correctElementType(Type type) {
String internalName = type.getInternalName();
assert internalName.charAt(0) == '[';
return Type.getType(internalName.substring(1));
public BindingContext getBindingContext() {
return bindingContext;
}
@NotNull
@@ -259,85 +205,11 @@ public class JetTypeMapper {
if (signatureVisitor != null) {
signatureVisitor.writeNothing(true);
}
return OBJECT_TYPE;
return AsmTypeConstants.OBJECT_TYPE;
}
return mapType(jetType, signatureVisitor, MapTypeMode.VALUE);
}
@Nullable
public static String getLocalNameForObject(JetObjectDeclaration object) {
PsiElement parent = object.getParent();
if (parent instanceof JetClassObject) {
return JvmAbi.CLASS_OBJECT_CLASS_NAME;
}
return null;
}
public JvmClassName getJvmClassName(ClassDescriptor classDescriptor) {
return JvmClassName.byInternalName(getJvmInternalFQName(classDescriptor));
}
@NotNull
private String getJvmInternalFQName(@NotNull DeclarationDescriptor descriptor) {
descriptor = descriptor.getOriginal();
if (descriptor instanceof FunctionDescriptor) {
throw new IllegalStateException("requested fq name for function: " + descriptor);
}
if (descriptor.getContainingDeclaration() instanceof ModuleDescriptor || descriptor instanceof ScriptDescriptor) {
return "";
}
if (descriptor instanceof ModuleDescriptor) {
throw new IllegalStateException("missed something");
}
if (descriptor instanceof ClassDescriptor) {
ClassDescriptor klass = (ClassDescriptor) descriptor;
if (klass.getKind() == ClassKind.OBJECT || klass.getKind() == ClassKind.CLASS_OBJECT) {
if (klass.getContainingDeclaration() instanceof ClassDescriptor) {
ClassDescriptor containingKlass = (ClassDescriptor) klass.getContainingDeclaration();
if (containingKlass.getKind() == ClassKind.ENUM_CLASS) {
return getJvmInternalFQName(containingKlass);
}
else {
return getJvmInternalFQName(containingKlass) + JvmAbi.CLASS_OBJECT_SUFFIX;
}
}
}
else if (klass.getKind() == ClassKind.ENUM_ENTRY) {
if (enumEntryNeedSubclass(bindingContext, klass)) {
return getJvmInternalFQName(klass.getContainingDeclaration()) + "$" + klass.getName().getName();
}
else {
return getJvmInternalFQName(klass.getContainingDeclaration());
}
}
JvmClassName name = classNameForClassDescriptor(bindingContext, (ClassDescriptor) descriptor);
if (name != null) {
return name.getInternalName();
}
}
DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (container == null) {
throw new IllegalStateException("descriptor has no container: " + descriptor);
}
Name name = descriptor.getName();
String baseName = getJvmInternalFQName(container);
if (!baseName.isEmpty()) {
return baseName + (container instanceof NamespaceDescriptor ? "/" : "$") + name.getIdentifier();
}
return name.getIdentifier();
}
@NotNull
public Type mapType(@NotNull final JetType jetType, @NotNull MapTypeMode kind) {
return mapType(jetType, null, kind);
@@ -418,15 +290,21 @@ public class JetTypeMapper {
r = Type.getType("[" + boxType(mapType(memberType, kind)).getDescriptor());
}
else {
r = JAVA_ARRAY_GENERIC_TYPE;
r = AsmTypeConstants.JAVA_ARRAY_GENERIC_TYPE;
}
checkValidType(r);
return r;
}
if (descriptor instanceof ClassDescriptor) {
JvmClassName name = getJvmClassName((ClassDescriptor) descriptor);
Type asmType = Type.getObjectType(name.getInternalName() + (kind == MapTypeMode.TRAIT_IMPL ? JvmAbi.TRAIT_IMPL_SUFFIX : ""));
JvmClassName name = getJvmClassName(bindingTrace, (ClassDescriptor) descriptor);
Type asmType;
if (kind == MapTypeMode.TRAIT_IMPL) {
asmType = Type.getObjectType(name.getInternalName() + JvmAbi.TRAIT_IMPL_SUFFIX);
}
else {
asmType = name.getAsmType();
}
boolean forceReal = KotlinToJavaTypesMap.getInstance().isForceReal(name);
writeGenericType(jetType, signatureVisitor, asmType, forceReal);
@@ -492,26 +370,6 @@ public class JetTypeMapper {
}
}
public static Type unboxType(final Type type) {
JvmPrimitiveType jvmPrimitiveType = JvmPrimitiveType.getByWrapperAsmType(type);
if (jvmPrimitiveType != null) {
return jvmPrimitiveType.getAsmType();
}
else {
throw new UnsupportedOperationException("Unboxing: " + type);
}
}
public static Type boxType(Type asmType) {
JvmPrimitiveType jvmPrimitiveType = JvmPrimitiveType.getByAsmType(asmType);
if (jvmPrimitiveType != null) {
return jvmPrimitiveType.getWrapper().getAsmType();
}
else {
return asmType;
}
}
public CallableMethod mapToCallableMethod(@NotNull FunctionDescriptor functionDescriptor, boolean superCall, OwnerKind kind) {
final DeclarationDescriptor functionParent = functionDescriptor.getOriginal().getContainingDeclaration();
@@ -552,8 +410,8 @@ public class JetTypeMapper {
ClassDescriptor currentOwner = (ClassDescriptor) functionParent;
ClassDescriptor declarationOwner = (ClassDescriptor) declarationFunctionDescriptor.getContainingDeclaration();
boolean originalIsInterface = CodegenUtil.isInterface(declarationOwner);
boolean currentIsInterface = CodegenUtil.isInterface(currentOwner);
boolean originalIsInterface = isInterface(declarationOwner);
boolean currentIsInterface = isInterface(currentOwner);
boolean isAccessor = isAccessor(functionDescriptor);
@@ -704,7 +562,7 @@ public class JetTypeMapper {
for (JetType jetType : typeParameterDescriptor.getUpperBounds()) {
if (jetType.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) {
if (!CodegenUtil.isInterface(jetType)) {
if (!isInterface(jetType)) {
mapType(jetType, signatureVisitor, MapTypeMode.TYPE_PARAMETER);
break classBound;
}
@@ -720,7 +578,7 @@ public class JetTypeMapper {
for (JetType jetType : typeParameterDescriptor.getUpperBounds()) {
if (jetType.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) {
if (CodegenUtil.isInterface(jetType)) {
if (isInterface(jetType)) {
signatureVisitor.writeInterfaceBound();
mapType(jetType, signatureVisitor, MapTypeMode.TYPE_PARAMETER);
signatureVisitor.writeInterfaceBoundEnd();
@@ -925,8 +783,10 @@ public class JetTypeMapper {
final ConstructorDescriptor superConstructor = (ConstructorDescriptor) superDescriptor;
if (isObjectLiteral(bindingContext, descriptor.getContainingDeclaration())) {
//noinspection SuspiciousMethodCalls
CallableMethod superCallable = mapToCallableMethod(superConstructor,
getCalculatedClosure(superConstructor.getContainingDeclaration()));
bindingContext.get(CLOSURE,
superConstructor.getContainingDeclaration()));
final List<JvmMethodParameterSignature> types = superCallable.getSignature().getKotlinParameterTypes();
if (types != null) {
for (JvmMethodParameterSignature type : types) {
@@ -993,53 +853,6 @@ public class JetTypeMapper {
return new CallableMethod(owner, owner, owner, method, INVOKESPECIAL, null, null, null);
}
//TODO: move mapping logic to front-end java
public static int getVisibilityAccessFlag(@NotNull MemberDescriptor descriptor) {
Integer specialCase = specialCaseVisibility(descriptor);
if (specialCase != null) {
return specialCase;
}
Integer defaultMapping = visibilityToAccessFlag.get(descriptor.getVisibility());
if (defaultMapping == null) {
throw new IllegalStateException(descriptor.getVisibility() + " is not a valid visibility in backend.");
}
return defaultMapping;
}
@Nullable
private static Integer specialCaseVisibility(@NotNull MemberDescriptor memberDescriptor) {
DeclarationDescriptor containingDeclaration = memberDescriptor.getContainingDeclaration();
if (CodegenUtil.isInterface(containingDeclaration)) {
return ACC_PUBLIC;
}
Visibility memberVisibility = memberDescriptor.getVisibility();
if (memberVisibility != Visibilities.PRIVATE) {
return null;
}
if (isClassObject(containingDeclaration)) {
return ACC_PACKAGE_PRIVATE;
}
if (memberDescriptor instanceof ConstructorDescriptor) {
ClassKind kind = ((ClassDescriptor) containingDeclaration).getKind();
if (kind == ClassKind.OBJECT) {
//TODO: should be ACC_PACKAGE_PRIVATE
// see http://youtrack.jetbrains.com/issue/KT-2700
return ACC_PUBLIC;
}
else if (kind == ClassKind.ENUM_ENTRY) {
return ACC_PACKAGE_PRIVATE;
}
else if (kind == ClassKind.ENUM_CLASS) {
//TODO: should be ACC_PRIVATE
// see http://youtrack.jetbrains.com/issue/KT-2680
return ACC_PROTECTED;
}
}
if (containingDeclaration instanceof NamespaceDescriptor) {
return ACC_PUBLIC;
}
return null;
}
private static boolean isGenericsArray(JetType type) {
return JetStandardLibrary.getInstance().isArray(type) &&
@@ -1059,27 +872,20 @@ public class JetTypeMapper {
return StackValue
.sharedTypeForType(mapType(((FunctionDescriptor) descriptor).getReceiverParameter().getType(), MapTypeMode.VALUE));
}
else if (descriptor instanceof VariableDescriptor && isVarCapturedInClosure(descriptor)) {
else if (descriptor instanceof VariableDescriptor && isVarCapturedInClosure(bindingContext, descriptor)) {
JetType outType = ((VariableDescriptor) descriptor).getType();
return StackValue.sharedTypeForType(mapType(outType, MapTypeMode.VALUE));
}
return null;
}
public boolean isVarCapturedInClosure(DeclarationDescriptor descriptor) {
if (!(descriptor instanceof VariableDescriptor) || descriptor instanceof PropertyDescriptor) return false;
VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor;
return Boolean.TRUE.equals(bindingContext.get(BindingContext.CAPTURED_IN_CLOSURE, variableDescriptor)) &&
variableDescriptor.isVar();
}
protected JvmMethodSignature invokeSignature(FunctionDescriptor fd) {
return mapSignature(Name.identifier("invoke"), fd);
}
public CallableMethod asCallableMethod(FunctionDescriptor fd) {
JvmMethodSignature descriptor = CodegenUtil.erasedInvokeSignature(fd);
JvmClassName owner = CodegenUtil.getInternalClassName(fd);
JvmMethodSignature descriptor = erasedInvokeSignature(fd);
JvmClassName owner = getInternalClassName(fd);
Type receiverParameterType;
if (fd.getReceiverParameter().exists()) {
receiverParameterType = mapType(fd.getOriginal().getReceiverParameter().getType(), MapTypeMode.VALUE);
@@ -1089,6 +895,6 @@ public class JetTypeMapper {
}
return new CallableMethod(
owner, null, null, descriptor, INVOKEVIRTUAL,
CodegenUtil.getInternalClassName(fd), receiverParameterType, CodegenUtil.getInternalClassName(fd).getAsmType());
getInternalClassName(fd), receiverParameterType, getInternalClassName(fd).getAsmType());
}
}
@@ -33,11 +33,11 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.lang.types.lang.PrimitiveType;
import org.jetbrains.jet.lang.types.ref.ClassName;
import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
/**
* @author svtk
*/
@@ -64,18 +64,18 @@ public class KotlinToJavaTypesMap {
private void init() {
JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
register(JetStandardClasses.getAny(), Object.class);
register(standardLibrary.getNumber(), Number.class);
register(standardLibrary.getString(), String.class);
register(standardLibrary.getThrowable(), Throwable.class);
register(standardLibrary.getCharSequence(), CharSequence.class);
register(standardLibrary.getComparable(), Comparable.class);
register(standardLibrary.getEnum(), Enum.class);
register(standardLibrary.getAnnotation(), Annotation.class);
register(standardLibrary.getIterable(), Iterable.class);
register(standardLibrary.getIterator(), Iterator.class);
register(standardLibrary.getMutableIterable(), Iterable.class);
register(standardLibrary.getMutableIterator(), Iterator.class);
register(JetStandardClasses.getAny(), OBJECT_TYPE);
register(standardLibrary.getNumber(), JAVA_NUMBER_TYPE);
register(standardLibrary.getString(), JAVA_STRING_TYPE);
register(standardLibrary.getThrowable(), JAVA_THROWABLE_TYPE);
register(standardLibrary.getCharSequence(), JAVA_CHAR_SEQUENCE_TYPE);
register(standardLibrary.getComparable(), JAVA_COMPARABLE_TYPE);
register(standardLibrary.getEnum(), JAVA_ENUM_TYPE);
register(standardLibrary.getAnnotation(), JAVA_ANNOTATION_TYPE);
register(standardLibrary.getIterable(), JAVA_ITERABLE_TYPE);
register(standardLibrary.getMutableIterable(), JAVA_ITERABLE_TYPE);
register(standardLibrary.getIterator(), JAVA_ITERATOR_TYPE);
register(standardLibrary.getMutableIterator(), JAVA_ITERATOR_TYPE);
}
private void initPrimitives() {
@@ -105,10 +105,6 @@ public class KotlinToJavaTypesMap {
return asmTypes.get(className);
}
private void register(@NotNull ClassDescriptor kotlinDescriptor, @NotNull Class<?> javaClass) {
register(kotlinDescriptor, Type.getType(javaClass));
}
private void register(@NotNull ClassDescriptor kotlinDescriptor, @NotNull Type javaType) {
FqNameUnsafe fqName = DescriptorUtils.getFQName(kotlinDescriptor);
ClassName className = new ClassName(fqName.toSafe(), kotlinDescriptor.getDefaultType().getArguments().size());
@@ -39,7 +39,8 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import java.util.BitSet;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.getVisibilityAccessFlag;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
import static org.jetbrains.jet.codegen.CodegenUtil.*;
import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration;
/**
@@ -84,7 +85,7 @@ public class PropertyCodegen {
//noinspection ConstantConditions
if (state.getBindingContext().get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor)) {
DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
if (CodegenUtil.isInterface(containingDeclaration)) {
if (isInterface(containingDeclaration)) {
return;
}
@@ -217,7 +218,7 @@ public class PropertyCodegen {
else {
InstructionAdapter iv = new InstructionAdapter(mv);
if (kind != OwnerKind.NAMESPACE) {
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
}
final Type type = state.getInjector().getJetTypeMapper().mapType(propertyDescriptor.getType(), MapTypeMode.VALUE);
@@ -227,7 +228,7 @@ public class PropertyCodegen {
if (kind instanceof OwnerKind.DelegateKind) {
OwnerKind.DelegateKind dk = (OwnerKind.DelegateKind) kind;
dk.getDelegate().put(JetTypeMapper.OBJECT_TYPE, iv);
dk.getDelegate().put(OBJECT_TYPE, iv);
iv.invokeinterface(dk.getOwnerClass(), getterName, descriptor);
}
else {
@@ -250,9 +251,9 @@ public class PropertyCodegen {
) {
JetMethodAnnotationWriter aw = JetMethodAnnotationWriter.visitAnnotation(mv);
Modality modality = propertyDescriptor.getModality();
BitSet flags = CodegenUtil.getFlagsForVisibility(visibility);
BitSet flags = getFlagsForVisibility(visibility);
flags.set(JvmStdlibNames.FLAG_PROPERTY_BIT);
if (CodegenUtil.isInterface(propertyDescriptor.getContainingDeclaration()) && modality != Modality.ABSTRACT) {
if (isInterface(propertyDescriptor.getContainingDeclaration()) && modality != Modality.ABSTRACT) {
flags.set(modality == Modality.FINAL
? JvmStdlibNames.FLAG_FORCE_FINAL_BIT
: JvmStdlibNames.FLAG_FORCE_OPEN_BIT);
@@ -321,7 +322,7 @@ public class PropertyCodegen {
final Type type = state.getInjector().getJetTypeMapper().mapType(propertyDescriptor.getType(), MapTypeMode.VALUE);
int paramCode = 0;
if (kind != OwnerKind.NAMESPACE) {
iv.load(0, JetTypeMapper.OBJECT_TYPE);
iv.load(0, OBJECT_TYPE);
paramCode = 1;
}
@@ -331,8 +332,8 @@ public class PropertyCodegen {
if (kind instanceof OwnerKind.DelegateKind) {
OwnerKind.DelegateKind dk = (OwnerKind.DelegateKind) kind;
iv.load(0, JetTypeMapper.OBJECT_TYPE);
dk.getDelegate().put(JetTypeMapper.OBJECT_TYPE, iv);
iv.load(0, OBJECT_TYPE);
dk.getDelegate().put(OBJECT_TYPE, iv);
iv.load(paramCode, type);
iv.invokeinterface(dk.getOwnerClass(), setterName(propertyDescriptor.getName()), descriptor);
@@ -38,7 +38,7 @@ import javax.inject.Inject;
import java.util.List;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.AsmTypeConstants.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.context.CodegenBinding.*;
/**
@@ -98,7 +98,8 @@ public class ScriptCodegen {
(CodegenContext.ScriptContext) CodegenContext.STATIC
.intoScript(scriptDescriptor, classDescriptorForScript);
JvmClassName className = classNameForClassDescriptor(bindingContext, classDescriptorForScript);
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
assert className != null;
ClassBuilder classBuilder = classFileFactory.newVisitor(className.getInternalName() + ".class");
classBuilder.defineClass(scriptDeclaration,
@@ -144,7 +145,8 @@ public class ScriptCodegen {
InstructionAdapter instructionAdapter = new InstructionAdapter(mv);
JvmClassName className = classNameForClassDescriptor(bindingContext, classDescriptorForScript);
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
assert className != null;
instructionAdapter.load(0, className.getAsmType());
instructionAdapter.invokespecial("java/lang/Object", "<init>", "()V");
@@ -35,7 +35,7 @@ import org.jetbrains.jet.lexer.JetTokens;
import java.util.List;
import static org.jetbrains.asm4.Opcodes.*;
import static org.jetbrains.jet.codegen.JetTypeMapper.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
/**
* @author yole
@@ -57,7 +57,7 @@ public abstract class StackValue {
instructionAdapter.aconst(null);
}
else {
Type boxed = JetTypeMapper.boxType(type);
Type boxed = CodegenUtil.boxType(type);
instructionAdapter.invokestatic(boxed.getInternalName(), "valueOf", "(" + type.getDescriptor() + ")" + boxed.getDescriptor());
}
}
@@ -167,7 +167,7 @@ public abstract class StackValue {
private static void box(final Type type, final Type toType, InstructionAdapter v) {
// TODO handle toType correctly
if (type == Type.INT_TYPE || (JetTypeMapper.isIntPrimitive(type) && toType.getInternalName().equals("java/lang/Integer"))) {
if (type == Type.INT_TYPE || (CodegenUtil.isIntPrimitive(type) && toType.getInternalName().equals("java/lang/Integer"))) {
v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
}
else if (type == Type.BOOLEAN_TYPE) {
@@ -267,7 +267,7 @@ public abstract class StackValue {
v.iconst(0);
}
}
else if (toType.equals(JetTypeMapper.JET_TUPLE0_TYPE) && !fromType.equals(JetTypeMapper.JET_TUPLE0_TYPE)) {
else if (toType.equals(JET_TUPLE0_TYPE) && !fromType.equals(JET_TUPLE0_TYPE)) {
pop(fromType, v);
putTuple0Instance(v);
}
@@ -291,7 +291,7 @@ public abstract class StackValue {
v.checkcast(JvmPrimitiveType.CHAR.getWrapper().getAsmType());
}
else {
v.checkcast(JetTypeMapper.JAVA_NUMBER_TYPE);
v.checkcast(JAVA_NUMBER_TYPE);
}
}
unbox(toType, v);
@@ -344,11 +344,10 @@ public abstract class StackValue {
ResolvedCall<? extends CallableDescriptor> resolvedCall,
StackValue receiver,
ExpressionCodegen codegen,
@Nullable CallableMethod callableMethod,
GenerationState state
@Nullable CallableMethod callableMethod
) {
if (resolvedCall.getThisObject().exists() || resolvedCall.getReceiverArgument().exists()) {
return new CallReceiver(resolvedCall, receiver, codegen, state, callableMethod);
return new CallReceiver(resolvedCall, receiver, codegen, callableMethod);
}
return receiver;
}
@@ -477,11 +476,11 @@ public abstract class StackValue {
if (type == Type.VOID_TYPE) {
return;
}
if (type.equals(JetTypeMapper.JET_TUPLE0_TYPE)) {
if (type.equals(JET_TUPLE0_TYPE)) {
putTuple0Instance(v);
return;
}
if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(JetTypeMapper.JAVA_BOOLEAN_TYPE)) {
if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(JAVA_BOOLEAN_TYPE)) {
throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type " + type);
}
putAsBoolean(v);
@@ -570,7 +569,7 @@ public abstract class StackValue {
myOperand.put(type, v); // the operand will remove itself from the stack if needed
return;
}
if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(JetTypeMapper.JAVA_BOOLEAN_TYPE)) {
if (type != Type.BOOLEAN_TYPE && !type.equals(OBJECT_TYPE) && !type.equals(JAVA_BOOLEAN_TYPE)) {
throw new UnsupportedOperationException("don't know how to put a compare as a non-boolean type");
}
putAsBoolean(v);
@@ -590,7 +589,7 @@ public abstract class StackValue {
public ArrayElement(Type type, boolean unbox) {
super(type);
this.boxed = unbox ? JetTypeMapper.boxType(type) : type;
this.boxed = unbox ? CodegenUtil.boxType(type) : type;
}
@Override
@@ -677,6 +676,7 @@ public abstract class StackValue {
}
}
else {
//noinspection ConstantConditions
((IntrinsicMethod) setter).generate(codegen, v, null, null, null, null, state);
}
}
@@ -1048,31 +1048,31 @@ public abstract class StackValue {
switch (type.getSort()) {
case Type.OBJECT:
case Type.ARRAY:
return JetTypeMapper.JET_SHARED_VAR_TYPE;
return JET_SHARED_VAR_TYPE;
case Type.BYTE:
return JetTypeMapper.JET_SHARED_BYTE_TYPE;
return JET_SHARED_BYTE_TYPE;
case Type.SHORT:
return JetTypeMapper.JET_SHARED_SHORT_TYPE;
return JET_SHARED_SHORT_TYPE;
case Type.CHAR:
return JetTypeMapper.JET_SHARED_CHAR_TYPE;
return JET_SHARED_CHAR_TYPE;
case Type.INT:
return JetTypeMapper.JET_SHARED_INT_TYPE;
return JET_SHARED_INT_TYPE;
case Type.LONG:
return JetTypeMapper.JET_SHARED_LONG_TYPE;
return JET_SHARED_LONG_TYPE;
case Type.BOOLEAN:
return JetTypeMapper.JET_SHARED_BOOLEAN_TYPE;
return JET_SHARED_BOOLEAN_TYPE;
case Type.FLOAT:
return JetTypeMapper.JET_SHARED_FLOAT_TYPE;
return JET_SHARED_FLOAT_TYPE;
case Type.DOUBLE:
return JetTypeMapper.JET_SHARED_DOUBLE_TYPE;
return JET_SHARED_DOUBLE_TYPE;
default:
throw new UnsupportedOperationException();
@@ -1213,7 +1213,6 @@ public abstract class StackValue {
ResolvedCall<? extends CallableDescriptor> resolvedCall,
StackValue receiver,
ExpressionCodegen codegen,
@NotNull GenerationState state,
CallableMethod callableMethod
) {
super(calcType(resolvedCall, codegen, callableMethod));
@@ -1239,6 +1238,7 @@ public abstract class StackValue {
return callableMethod.getReceiverClass();
}
else {
//noinspection ConstantConditions
return callableMethod.getThisType().getAsmType();
}
}
@@ -18,6 +18,9 @@ package org.jetbrains.jet.codegen;
import org.jetbrains.asm4.MethodVisitor;
import static org.jetbrains.jet.codegen.CodegenUtil.generateMethodThrow;
import static org.jetbrains.jet.codegen.CodegenUtil.generateThrow;
/**
* @author Stepan Koltsov
*/
@@ -29,10 +32,10 @@ public class StubCodegen {
}
public static void generateStubThrow(MethodVisitor mv) {
CodegenUtil.generateThrow(mv, STUB_EXCEPTION, STUB_EXCEPTION_MESSAGE);
generateThrow(mv, STUB_EXCEPTION, STUB_EXCEPTION_MESSAGE);
}
public static void generateStubCode(MethodVisitor mv) {
CodegenUtil.generateMethodThrow(mv, STUB_EXCEPTION, STUB_EXCEPTION_MESSAGE);
generateMethodThrow(mv, STUB_EXCEPTION, STUB_EXCEPTION_MESSAGE);
}
}
@@ -24,7 +24,6 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import java.util.List;
import java.util.Map;
@@ -37,9 +36,6 @@ public interface CalculatedClosure {
@Nullable
ClassDescriptor getEnclosingClass();
@NotNull
JvmClassName getClassName();
@Nullable
JetDelegatorToSuperCall getSuperCall();
@@ -17,7 +17,6 @@
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.*;
@@ -33,6 +32,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.jetbrains.jet.codegen.CodegenUtil.peekFromStack;
import static org.jetbrains.jet.codegen.context.CodegenBinding.*;
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
@@ -74,7 +74,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
}
private String inventAnonymousClassName(JetElement declaration) {
String top = CodegenUtil.peekFromStack(nameStack);
String top = peekFromStack(nameStack);
Integer cnt = anonymousSubclassesCount.get(top);
if (cnt == null) {
cnt = 0;
@@ -131,15 +131,10 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
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);
bindingTrace.record(FQN, descriptor, bindingTrace.get(FQN, peekFromStack(classStack)));
}
}
@@ -148,8 +143,8 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
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);
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());
@@ -169,7 +164,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
if (classDescriptor == null) return;
String name = getName(classDescriptor);
recordClosure(bindingTrace, declaration, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), false);
recordClosure(bindingTrace, declaration, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
classStack.push(classDescriptor);
nameStack.push(name);
@@ -186,7 +181,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
if (classDescriptor == null) return;
String name = getName(classDescriptor);
recordClosure(bindingTrace, klass, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), false);
recordClosure(bindingTrace, klass, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
classStack.push(classDescriptor);
nameStack.push(name);
@@ -196,7 +191,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
}
private String getName(ClassDescriptor classDescriptor) {
String base = CodegenUtil.peekFromStack(nameStack);
String base = peekFromStack(nameStack);
return classDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor ? base.isEmpty() ? classDescriptor.getName()
.getName() : base + '/' + classDescriptor.getName() : base + '$' + classDescriptor.getName();
}
@@ -211,10 +206,11 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
}
final String name = inventAnonymousClassName(expression.getObjectDeclaration());
recordClosure(bindingTrace, expression.getObjectDeclaration(), classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), false);
recordClosure(bindingTrace, expression.getObjectDeclaration(), classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), false);
classStack.push(classDescriptor);
nameStack.push(classNameForClassDescriptor(bindingContext, classDescriptor).getInternalName());
//noinspection ConstantConditions
nameStack.push(bindingContext.get(FQN, classDescriptor).getInternalName());
super.visitObjectLiteralExpression(expression);
nameStack.pop();
classStack.pop();
@@ -229,7 +225,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
String name = inventAnonymousClassName(expression);
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
recordClosure(bindingTrace, expression, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), true);
recordClosure(bindingTrace, expression, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), true);
classStack.push(classDescriptor);
nameStack.push(name);
@@ -240,7 +236,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
@Override
public void visitProperty(JetProperty property) {
nameStack.push(CodegenUtil.peekFromStack(nameStack) + '$' + property.getName());
nameStack.push(peekFromStack(nameStack) + '$' + property.getName());
super.visitProperty(property);
nameStack.pop();
}
@@ -253,12 +249,12 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
if (functionDescriptor == null) return;
DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration();
if (containingDeclaration instanceof ClassDescriptor) {
nameStack.push(CodegenUtil.peekFromStack(nameStack) + '$' + function.getName());
nameStack.push(peekFromStack(nameStack) + '$' + function.getName());
super.visitNamedFunction(function);
nameStack.pop();
}
else if (containingDeclaration instanceof NamespaceDescriptor) {
String peek = CodegenUtil.peekFromStack(nameStack);
String peek = peekFromStack(nameStack);
if (peek.isEmpty()) {
peek = "namespace";
}
@@ -272,7 +268,7 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
else {
String name = inventAnonymousClassName(function);
ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
recordClosure(bindingTrace, function, classDescriptor, CodegenUtil.peekFromStack(classStack), JvmClassName.byInternalName(name), true);
recordClosure(bindingTrace, function, classDescriptor, peekFromStack(classStack), JvmClassName.byInternalName(name), true);
classStack.push(classDescriptor);
nameStack.push(name);
@@ -26,6 +26,7 @@ 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.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -49,6 +50,8 @@ public class CodegenBinding {
public static final WritableSlice<DeclarationDescriptor, ClassDescriptor> CLASS_FOR_FUNCTION = Slices.createSimpleSlice();
public static final WritableSlice<DeclarationDescriptor, JvmClassName> FQN = Slices.createSimpleSlice();
public static final WritableSlice<JvmClassName, Boolean> SCRIPT_NAMES = Slices.createSimpleSetSlice();
public static final WritableSlice<ClassDescriptor, Boolean> ENUM_ENTRY_CLASS_NEED_SUBCLASS = Slices.createSimpleSetSlice();
@@ -74,9 +77,8 @@ public class CodegenBinding {
@NotNull
public static JvmClassName classNameForScriptDescriptor(BindingContext bindingContext, @NotNull ScriptDescriptor scriptDescriptor) {
final ClassDescriptor classDescriptor = bindingContext.get(CLASS_FOR_FUNCTION, scriptDescriptor);
final CalculatedClosure closure = bindingContext.get(CLOSURE, classDescriptor);
assert closure != null;
return closure.getClassName();
//noinspection ConstantConditions
return bindingContext.get(FQN, classDescriptor);
}
@NotNull
@@ -93,11 +95,6 @@ public class CodegenBinding {
return closure == null ? null : closure.getEnclosingClass();
}
public static JvmClassName classNameForClassDescriptor(BindingContext bindingContext, ClassDescriptor descriptor) {
final CalculatedClosure closure = bindingContext.get(CLOSURE, descriptor);
return closure == null ? null : closure.getClassName();
}
public static JvmClassName classNameForAnonymousClass(BindingContext bindingContext, JetElement expression) {
if (expression instanceof JetObjectLiteralExpression) {
JetObjectLiteralExpression jetObjectLiteralExpression = (JetObjectLiteralExpression) expression;
@@ -110,7 +107,7 @@ public class CodegenBinding {
assert functionDescriptor != null;
descriptor = bindingContext.get(CLASS_FOR_FUNCTION, functionDescriptor);
}
return classNameForClassDescriptor(bindingContext, descriptor);
return bindingContext.get(FQN, descriptor);
}
public static void registerClassNameForScript(
@@ -171,8 +168,9 @@ public class CodegenBinding {
}
}
final MutableClosure closure = new MutableClosure(superCall, enclosing, name, enclosingReceiver);
final MutableClosure closure = new MutableClosure(superCall, enclosing, enclosingReceiver);
bindingTrace.record(FQN, classDescriptor, name);
bindingTrace.record(CLOSURE, classDescriptor, closure);
// TODO: this is temporary before we have proper inner classes
@@ -242,4 +240,89 @@ public class CodegenBinding {
}
return false;
}
public static JvmClassName getJvmClassName(BindingTrace bindingTrace, ClassDescriptor classDescriptor) {
classDescriptor = (ClassDescriptor) classDescriptor.getOriginal();
final JvmClassName name = bindingTrace.getBindingContext().get(FQN, classDescriptor);
if(name != null) {
return name;
}
return getJvmInternalName(bindingTrace, classDescriptor);
}
@NotNull
private static JvmClassName getJvmInternalName(BindingTrace bindingTrace, @NotNull DeclarationDescriptor descriptor) {
descriptor = descriptor.getOriginal();
JvmClassName name = bindingTrace.getBindingContext().get(FQN, descriptor);
if(name != null) {
return name;
}
name = JvmClassName.byInternalName(getJvmInternalFQNameImpl(bindingTrace, descriptor));
bindingTrace.record(FQN, descriptor, name);
return name;
}
private static String getJvmInternalFQNameImpl(BindingTrace bindingTrace, DeclarationDescriptor descriptor) {
if (descriptor instanceof FunctionDescriptor) {
throw new IllegalStateException("requested fq name for function: " + descriptor);
}
if (descriptor.getContainingDeclaration() instanceof ModuleDescriptor || descriptor instanceof ScriptDescriptor) {
return "";
}
if (descriptor instanceof ModuleDescriptor) {
throw new IllegalStateException("missed something");
}
if (descriptor instanceof ClassDescriptor) {
ClassDescriptor klass = (ClassDescriptor) descriptor;
if (klass.getKind() == ClassKind.OBJECT || klass.getKind() == ClassKind.CLASS_OBJECT) {
if (klass.getContainingDeclaration() instanceof ClassDescriptor) {
ClassDescriptor containingKlass = (ClassDescriptor) klass.getContainingDeclaration();
if (containingKlass.getKind() == ClassKind.ENUM_CLASS) {
return getJvmInternalName(bindingTrace, containingKlass).getInternalName();
}
else {
return getJvmInternalName(bindingTrace, containingKlass).getInternalName() + JvmAbi.CLASS_OBJECT_SUFFIX;
}
}
}
JvmClassName name = bindingTrace.getBindingContext().get(FQN, descriptor);
if (name != null) {
return name.getInternalName();
}
}
DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (container == null) {
throw new IllegalStateException("descriptor has no container: " + descriptor);
}
Name name = descriptor.getName();
String baseName = getJvmInternalName(bindingTrace, container).getInternalName();
if (!baseName.isEmpty()) {
return baseName + (container instanceof NamespaceDescriptor ? "/" : "$") + name.getIdentifier();
}
return name.getIdentifier();
}
public static boolean isVarCapturedInClosure(BindingContext bindingContext, DeclarationDescriptor descriptor) {
if (!(descriptor instanceof VariableDescriptor) || descriptor instanceof PropertyDescriptor) return false;
VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor;
return Boolean.TRUE.equals(bindingContext.get(CAPTURED_IN_CLOSURE, variableDescriptor)) &&
variableDescriptor.isVar();
}
public static boolean hasThis0(BindingContext bindingContext, ClassDescriptor classDescriptor) {
//noinspection SuspiciousMethodCalls
final CalculatedClosure closure = bindingContext.get(CLOSURE, classDescriptor);
return closure != null && closure.getCaptureThis() != null;
}
}
@@ -30,8 +30,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.codegen.JetTypeMapper.OBJECT_TYPE;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
import static org.jetbrains.jet.codegen.context.CodegenBinding.CLASS_FOR_FUNCTION;
import static org.jetbrains.jet.codegen.context.CodegenBinding.CLOSURE;
import static org.jetbrains.jet.codegen.context.CodegenBinding.FQN;
/*
* @author max
@@ -180,7 +182,7 @@ public abstract class CodegenContext {
) {
final JetTypeMapper typeMapper = expressionCodegen.getState().getInjector().getJetTypeMapper();
return new ClosureContext(typeMapper, funDescriptor,
typeMapper.bindingContext.get(CLASS_FOR_FUNCTION, funDescriptor),
typeMapper.getBindingContext().get(CLASS_FOR_FUNCTION, funDescriptor),
this, expressionCodegen);
}
@@ -258,8 +260,8 @@ public abstract class CodegenContext {
final ClassDescriptor enclosingClass = getEnclosingClass();
outerExpression = enclosingClass != null
? StackValue
.field(typeMapper.mapType(enclosingClass.getDefaultType(), MapTypeMode.VALUE), typeMapper.getJvmClassName(
classDescriptor), CodegenUtil.THIS$0,
.field(typeMapper.mapType(enclosingClass.getDefaultType(), MapTypeMode.VALUE), CodegenBinding.getJvmClassName(
typeMapper.getBindingTrace(), classDescriptor), CodegenUtil.THIS$0,
false)
: null;
}
@@ -275,7 +277,7 @@ public abstract class CodegenContext {
for (LocalLookup.LocalLookupCase aCase : LocalLookup.LocalLookupCase.values()) {
if (aCase.isCase(d, state)) {
StackValue innerValue = aCase.innerValue(d, enclosingLocalLookup, state, closure);
StackValue innerValue = aCase.innerValue(d, enclosingLocalLookup, state, closure, state.getBindingContext().get(FQN, getThisDescriptor()));
if (innerValue == null) {
break;
}
@@ -371,9 +373,9 @@ public abstract class CodegenContext {
}
@Override
public StackValue lookupInContext(DeclarationDescriptor d, StackValue result, GenerationState state, boolean ignoreNoOuter) {
public StackValue lookupInContext(DeclarationDescriptor d, @Nullable StackValue result, GenerationState state, boolean ignoreNoOuter) {
if (getContextDescriptor() == d) {
return StackValue.local(0, JetTypeMapper.OBJECT_TYPE);
return StackValue.local(0, AsmTypeConstants.OBJECT_TYPE);
}
//noinspection ConstantConditions
@@ -456,7 +458,9 @@ public abstract class CodegenContext {
CodegenContext parentContext,
LocalLookup localLookup
) {
super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getCalculatedClosure(contextDescriptor),
//noinspection SuspiciousMethodCalls
super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getBindingContext().get(CLOSURE,
contextDescriptor),
contextDescriptor,
localLookup);
initOuterExpression(typeMapper, contextDescriptor);
@@ -476,7 +480,9 @@ public abstract class CodegenContext {
CodegenContext parentContext,
LocalLookup localLookup
) {
super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getCalculatedClosure(contextDescriptor),
//noinspection SuspiciousMethodCalls
super(contextDescriptor, contextKind, parentContext, (MutableClosure) typeMapper.getBindingContext().get(CLOSURE,
contextDescriptor),
contextDescriptor,
localLookup);
initOuterExpression(typeMapper, contextDescriptor);
@@ -503,8 +509,9 @@ public abstract class CodegenContext {
CodegenContext parentContext,
LocalLookup localLookup
) {
//noinspection SuspiciousMethodCalls
super(contextDescriptor, OwnerKind.IMPLEMENTATION, parentContext,
(MutableClosure) typeMapper.getCalculatedClosure(classDescriptor), classDescriptor, localLookup);
(MutableClosure) typeMapper.getBindingContext().get(CLOSURE, classDescriptor), classDescriptor, localLookup);
this.classDescriptor = classDescriptor;
initOuterExpression(typeMapper, classDescriptor);
@@ -41,7 +41,12 @@ public interface LocalLookup {
}
@Override
public StackValue innerValue(DeclarationDescriptor d, LocalLookup localLookup, GenerationState state, MutableClosure closure) {
public StackValue innerValue(
DeclarationDescriptor d,
LocalLookup localLookup,
GenerationState state,
MutableClosure closure, JvmClassName className
) {
VariableDescriptor vd = (VariableDescriptor) d;
final boolean idx = localLookup != null && localLookup.lookupLocal(vd);
@@ -53,8 +58,8 @@ public interface LocalLookup {
final String fieldName = "$" + vd.getName();
StackValue innerValue = sharedVarType != null
? StackValue.fieldForSharedVar(localType, closure.getClassName(), fieldName)
: StackValue.field(type, closure.getClassName(), fieldName, false);
? StackValue.fieldForSharedVar(localType, className, fieldName)
: StackValue.field(type, className, fieldName, false);
closure.recordField(fieldName, type);
closure.captureVariable(new EnclosedValueDescriptor(d, innerValue, type));
@@ -70,7 +75,13 @@ public interface LocalLookup {
}
@Override
public StackValue innerValue(DeclarationDescriptor d, LocalLookup localLookup, GenerationState state, MutableClosure closure) {
public StackValue innerValue(
DeclarationDescriptor d,
LocalLookup localLookup,
GenerationState state,
MutableClosure closure,
JvmClassName className
) {
FunctionDescriptor vd = (FunctionDescriptor) d;
final boolean idx = localLookup.lookupLocal(vd);
@@ -81,7 +92,7 @@ public interface LocalLookup {
Type localType = cn.getAsmType();
final String fieldName = "$" + vd.getName();
StackValue innerValue = StackValue.field(localType, closure.getClassName(), fieldName, false);
StackValue innerValue = StackValue.field(localType, className, fieldName, false);
closure.recordField(fieldName, localType);
closure.captureVariable(new EnclosedValueDescriptor(d, innerValue, localType));
@@ -101,13 +112,13 @@ public interface LocalLookup {
DeclarationDescriptor d,
LocalLookup enclosingLocalLookup,
GenerationState state,
MutableClosure closure
MutableClosure closure, JvmClassName className
) {
if (closure.getEnclosingReceiverDescriptor() != d) return null;
final JetType receiverType = ((CallableDescriptor) d).getReceiverParameter().getType();
Type type = state.getInjector().getJetTypeMapper().mapType(receiverType, MapTypeMode.VALUE);
StackValue innerValue = StackValue.field(type, closure.getClassName(), CodegenUtil.RECEIVER$0, false);
StackValue innerValue = StackValue.field(type, className, CodegenUtil.RECEIVER$0, false);
closure.setCaptureReceiver();
return innerValue;
@@ -126,7 +137,8 @@ public interface LocalLookup {
DeclarationDescriptor d,
LocalLookup localLookup,
GenerationState state,
MutableClosure closure
MutableClosure closure,
JvmClassName className
);
public StackValue outerValue(EnclosedValueDescriptor d, ExpressionCodegen expressionCodegen) {
@@ -25,7 +25,6 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import java.util.*;
@@ -38,8 +37,6 @@ public final class MutableClosure implements CalculatedClosure {
private final ClassDescriptor enclosingClass;
private final CallableDescriptor enclosingReceiverDescriptor;
private final JvmClassName name;
private boolean captureThis;
private boolean captureReceiver;
@@ -49,13 +46,11 @@ public final class MutableClosure implements CalculatedClosure {
MutableClosure(
JetDelegatorToSuperCall superCall,
ClassDescriptor enclosingClass,
JvmClassName className,
CallableDescriptor enclosingReceiverDescriptor
) {
this.superCall = superCall;
this.enclosingClass = enclosingClass;
this.enclosingReceiverDescriptor = enclosingReceiverDescriptor;
this.name = className;
}
@Nullable
@@ -64,12 +59,6 @@ public final class MutableClosure implements CalculatedClosure {
return enclosingClass;
}
@NotNull
@Override
public JvmClassName getClassName() {
return name;
}
@Override
public JetDelegatorToSuperCall getSuperCall() {
return superCall;
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -42,8 +39,8 @@ public class ArrayGet implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
Type type = JetTypeMapper.correctElementType(receiver.type);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
Type type = CodegenUtil.correctElementType(receiver.type);
codegen.gen(arguments.get(0), Type.INT_TYPE);
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -39,9 +36,9 @@ public class ArrayIndices implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.arraylength();
v.invokestatic("jet/IntRange", "count", "(I)Ljet/IntRange;");
return StackValue.onStack(JetTypeMapper.JET_INT_RANGE_TYPE);
return StackValue.onStack(AsmTypeConstants.JET_INT_RANGE_TYPE);
}
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.psi.JetCallExpression;
@@ -50,7 +47,7 @@ public class ArrayIterator implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
JetCallExpression call = (JetCallExpression) element;
FunctionDescriptor funDescriptor = (FunctionDescriptor) codegen.getBindingContext()
.get(BindingContext.REFERENCE_TARGET, (JetSimpleNameExpression) call.getCalleeExpression());
@@ -58,7 +55,7 @@ public class ArrayIterator implements IntrinsicMethod {
ClassDescriptor containingDeclaration = (ClassDescriptor) funDescriptor.getContainingDeclaration().getOriginal();
if (containingDeclaration.equals(JetStandardLibrary.getInstance().getArray())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([Ljava/lang/Object;)Ljava/util/Iterator;");
return StackValue.onStack(JetTypeMapper.JET_ITERATOR_TYPE);
return StackValue.onStack(AsmTypeConstants.JET_ITERATOR_TYPE);
}
else {
for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -42,8 +39,8 @@ public class ArraySet implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
Type type = JetTypeMapper.correctElementType(receiver.type);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
Type type = CodegenUtil.correctElementType(receiver.type);
codegen.gen(arguments.get(0), Type.INT_TYPE);
codegen.gen(arguments.get(1), type);
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -42,7 +39,7 @@ public class ArraySize implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.arraylength();
return StackValue.onStack(Type.INT_TYPE);
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -52,7 +49,7 @@ public class BinaryOp implements IntrinsicMethod {
) {
boolean nullable = expectedType.getSort() == Type.OBJECT;
if (nullable) {
expectedType = JetTypeMapper.unboxType(expectedType);
expectedType = CodegenUtil.unboxType(expectedType);
}
if (arguments.size() == 1) {
// Intrinsic is called as an ordinary function
@@ -68,7 +65,7 @@ public class BinaryOp implements IntrinsicMethod {
v.visitInsn(expectedType.getOpcode(opcode));
if (nullable) {
StackValue.onStack(expectedType).put(expectedType = JetTypeMapper.boxType(expectedType), v);
StackValue.onStack(expectedType).put(expectedType = CodegenUtil.boxType(expectedType), v);
}
return StackValue.onStack(expectedType);
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -48,7 +45,7 @@ public class Concat implements IntrinsicMethod {
codegen.invokeAppend(arguments.get(1));
}
else { // LHS.plus(RHS)
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
codegen.generateStringBuilderConstructor();
v.swap(); // StringBuilder LHS
codegen.invokeAppendMethod(expectedType); // StringBuilder(LHS)
@@ -56,7 +53,7 @@ public class Concat implements IntrinsicMethod {
}
v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
StackValue.onStack(JetTypeMapper.JAVA_STRING_TYPE).put(expectedType, v);
StackValue.onStack(AsmTypeConstants.JAVA_STRING_TYPE).put(expectedType, v);
return StackValue.onStack(expectedType);
}
}
@@ -21,10 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -40,9 +37,9 @@ public class EnumName implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.invokevirtual("java/lang/Enum", "name", "()Ljava/lang/String;");
StackValue.onStack(JetTypeMapper.JAVA_STRING_TYPE).put(expectedType, v);
StackValue.onStack(AsmTypeConstants.JAVA_STRING_TYPE).put(expectedType, v);
return StackValue.onStack(expectedType);
}
}
@@ -21,10 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -40,7 +37,7 @@ public class EnumOrdinal implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.invokevirtual("java/lang/Enum", "ordinal", "()I");
StackValue.onStack(Type.INT_TYPE).put(expectedType, v);
return StackValue.onStack(expectedType);
@@ -50,7 +50,7 @@ public class EnumValueOf implements IntrinsicMethod {
Type type = state.getInjector().getJetTypeMapper().mapType(
returnType, MapTypeMode.VALUE);
assert arguments != null;
codegen.gen(arguments.get(0), JetTypeMapper.JAVA_STRING_TYPE);
codegen.gen(arguments.get(0), AsmTypeConstants.JAVA_STRING_TYPE);
v.invokestatic(type.getInternalName(), "valueOf", "(Ljava/lang/String;)" + type.getDescriptor());
StackValue.onStack(type).put(expectedType, v);
return StackValue.onStack(expectedType);
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetCallExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.BindingContext;
@@ -50,7 +47,7 @@ public class Equals implements IntrinsicMethod {
boolean leftNullable = true;
JetExpression rightExpr;
if (element instanceof JetCallExpression) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
JetCallExpression jetCallExpression = (JetCallExpression) element;
JetExpression calleeExpression = jetCallExpression.getCalleeExpression();
if (calleeExpression != null) {
@@ -66,16 +63,16 @@ public class Equals implements IntrinsicMethod {
JetType leftType = codegen.getBindingContext().get(BindingContext.EXPRESSION_TYPE, leftExpr);
assert leftType != null;
leftNullable = leftType.isNullable();
codegen.gen(leftExpr).put(JetTypeMapper.OBJECT_TYPE, v);
codegen.gen(leftExpr).put(AsmTypeConstants.OBJECT_TYPE, v);
rightExpr = arguments.get(1);
}
JetType rightType = codegen.getBindingContext().get(BindingContext.EXPRESSION_TYPE, rightExpr);
codegen.gen(rightExpr).put(JetTypeMapper.OBJECT_TYPE, v);
codegen.gen(rightExpr).put(AsmTypeConstants.OBJECT_TYPE, v);
assert rightType != null;
return codegen
.generateEqualsForExpressionsOnStack(JetTokens.EQEQ, JetTypeMapper.OBJECT_TYPE, JetTypeMapper.OBJECT_TYPE, leftNullable,
.generateEqualsForExpressionsOnStack(JetTokens.EQEQ, AsmTypeConstants.OBJECT_TYPE, AsmTypeConstants.OBJECT_TYPE, leftNullable,
rightType.isNullable());
}
}
@@ -22,10 +22,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Opcodes;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -44,7 +41,7 @@ public class HashCode implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I");
return StackValue.onStack(Type.INT_TYPE);
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetBinaryExpression;
import org.jetbrains.jet.lang.psi.JetCallExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
@@ -31,6 +28,8 @@ import org.jetbrains.jet.lexer.JetTokens;
import java.util.List;
import static org.jetbrains.jet.codegen.AsmTypeConstants.*;
/**
* @author alex.tkachman
*/
@@ -46,15 +45,15 @@ public class IdentityEquals implements IntrinsicMethod {
@NotNull GenerationState state
) {
if (element instanceof JetCallExpression) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
codegen.gen(arguments.get(0)).put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(OBJECT_TYPE, v);
codegen.gen(arguments.get(0)).put(OBJECT_TYPE, v);
}
else {
assert element instanceof JetBinaryExpression;
JetBinaryExpression e = (JetBinaryExpression) element;
codegen.gen(e.getLeft()).put(JetTypeMapper.OBJECT_TYPE, v);
codegen.gen(e.getRight()).put(JetTypeMapper.OBJECT_TYPE, v);
codegen.gen(e.getLeft()).put(OBJECT_TYPE, v);
codegen.gen(e.getRight()).put(OBJECT_TYPE, v);
}
return StackValue.cmp(JetTokens.EQEQEQ, JetTypeMapper.OBJECT_TYPE);
return StackValue.cmp(JetTokens.EQEQEQ, OBJECT_TYPE);
}
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetParenthesizedExpression;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
@@ -52,7 +49,7 @@ public class Increment implements IntrinsicMethod {
) {
boolean nullable = expectedType.getSort() == Type.OBJECT;
if (nullable) {
expectedType = JetTypeMapper.unboxType(expectedType);
expectedType = CodegenUtil.unboxType(expectedType);
}
if (arguments.size() > 0) {
JetExpression operand = arguments.get(0);
@@ -61,7 +58,7 @@ public class Increment implements IntrinsicMethod {
}
if (operand instanceof JetReferenceExpression) {
final int index = codegen.indexOfLocal((JetReferenceExpression) operand);
if (index >= 0 && JetTypeMapper.isIntPrimitive(expectedType)) {
if (index >= 0 && CodegenUtil.isIntPrimitive(expectedType)) {
return StackValue.preIncrement(index, myDelta);
}
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -45,7 +42,7 @@ public class Inv implements IntrinsicMethod {
) {
boolean nullable = expectedType.getSort() == Type.OBJECT;
if (nullable) {
expectedType = JetTypeMapper.unboxType(expectedType);
expectedType = CodegenUtil.unboxType(expectedType);
}
receiver.put(expectedType, v);
if (expectedType == Type.LONG_TYPE) {
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -70,7 +67,7 @@ public class IteratorNext implements IntrinsicMethod {
else {
throw new UnsupportedOperationException();
}
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.invokevirtual("jet/" + name + "Iterator", "next" + name, "()" + expectedType.getDescriptor());
return StackValue.onStack(expectedType);
}
@@ -57,6 +57,6 @@ public class JavaClassFunction implements IntrinsicMethod {
else {
v.aconst(type);
}
return StackValue.onStack(JetTypeMapper.JAVA_CLASS_TYPE);
return StackValue.onStack(AsmTypeConstants.JAVA_CLASS_TYPE);
}
}
@@ -21,10 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType;
@@ -53,6 +50,6 @@ public class JavaClassProperty implements IntrinsicMethod {
else {
v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;");
}
return StackValue.onStack(JetTypeMapper.JAVA_CLASS_TYPE);
return StackValue.onStack(AsmTypeConstants.JAVA_CLASS_TYPE);
}
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -43,7 +40,7 @@ public class StringGetChar implements IntrinsicMethod {
@NotNull GenerationState state
) {
if (receiver != null) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
}
if (arguments != null) {
codegen.gen(arguments.get(0)).put(Type.INT_TYPE, v);
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -42,7 +39,7 @@ public class StringLength implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.invokeinterface("java/lang/CharSequence", "length", "()I");
return StackValue.onStack(Type.INT_TYPE);
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -43,14 +40,14 @@ public class StringPlus implements IntrinsicMethod {
@NotNull GenerationState state
) {
if (receiver == null || receiver == StackValue.none()) {
codegen.gen(arguments.get(0)).put(JetTypeMapper.JAVA_STRING_TYPE, v);
codegen.gen(arguments.get(1)).put(JetTypeMapper.OBJECT_TYPE, v);
codegen.gen(arguments.get(0)).put(AsmTypeConstants.JAVA_STRING_TYPE, v);
codegen.gen(arguments.get(1)).put(AsmTypeConstants.OBJECT_TYPE, v);
}
else {
receiver.put(JetTypeMapper.JAVA_STRING_TYPE, v);
codegen.gen(arguments.get(0)).put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.JAVA_STRING_TYPE, v);
codegen.gen(arguments.get(0)).put(AsmTypeConstants.OBJECT_TYPE, v);
}
v.invokestatic("jet/runtime/Intrinsics", "stringPlus", "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;");
return StackValue.onStack(JetTypeMapper.JAVA_STRING_TYPE);
return StackValue.onStack(AsmTypeConstants.JAVA_STRING_TYPE);
}
}
@@ -21,10 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetCallExpression;
import org.jetbrains.jet.lang.psi.JetExpression;
@@ -42,9 +39,9 @@ public class StupidSync implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
codegen.pushMethodArguments((JetCallExpression) element, Arrays.asList(JetTypeMapper.OBJECT_TYPE, JetTypeMapper.JET_FUNCTION0_TYPE));
codegen.pushMethodArguments((JetCallExpression) element, Arrays.asList(AsmTypeConstants.OBJECT_TYPE, AsmTypeConstants.JET_FUNCTION0_TYPE));
v.invokestatic("jet/runtime/Intrinsics", "stupidSync", "(Ljava/lang/Object;Ljet/Function0;)Ljava/lang/Object;");
StackValue.onStack(JetTypeMapper.OBJECT_TYPE).put(expectedType, v);
StackValue.onStack(AsmTypeConstants.OBJECT_TYPE).put(expectedType, v);
return StackValue.onStack(expectedType);
}
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -42,8 +39,8 @@ public class ToString implements IntrinsicMethod {
StackValue receiver,
@NotNull GenerationState state
) {
receiver.put(JetTypeMapper.OBJECT_TYPE, v);
receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
v.invokestatic("java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;");
return StackValue.onStack(JetTypeMapper.JAVA_STRING_TYPE);
return StackValue.onStack(AsmTypeConstants.JAVA_STRING_TYPE);
}
}
@@ -20,10 +20,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -44,7 +41,7 @@ public class UnaryMinus implements IntrinsicMethod {
) {
boolean nullable = expectedType.getSort() == Type.OBJECT;
if (nullable) {
expectedType = JetTypeMapper.unboxType(expectedType);
expectedType = CodegenUtil.unboxType(expectedType);
}
if (arguments.size() == 1) {
codegen.gen(arguments.get(0), expectedType);
@@ -21,10 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.*;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
@@ -45,7 +42,7 @@ public class UnaryPlus implements IntrinsicMethod {
) {
boolean nullable = expectedType.getSort() == Type.OBJECT;
if (nullable) {
expectedType = JetTypeMapper.unboxType(expectedType);
expectedType = CodegenUtil.unboxType(expectedType);
}
if (receiver != null && receiver != StackValue.none()) {
receiver.put(expectedType, v);
@@ -23,7 +23,7 @@ import org.jetbrains.asm4.commons.Method;
import org.jetbrains.asm4.signature.SignatureVisitor;
import org.jetbrains.asm4.signature.SignatureWriter;
import org.jetbrains.asm4.util.CheckSignatureAdapter;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.AsmTypeConstants;
import org.jetbrains.jet.lang.resolve.java.JetSignatureUtils;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.Variance;
@@ -196,7 +196,7 @@ public class BothSignatureWriter {
jetSignatureWriter.visitClassType("jet/Nothing", nullable, false);
jetSignatureWriter.visitEnd();
if (nullable) {
writeAsmType0(JetTypeMapper.OBJECT_TYPE);
writeAsmType0(AsmTypeConstants.OBJECT_TYPE);
}
else {
writeAsmType0(Type.VOID_TYPE);
@@ -48,7 +48,7 @@ public class InjectorForJetTypeMapper {
this.builtinToJavaTypesMapping = BuiltinToJavaTypesMapping.ENABLED;
this.classBuilderMode = ClassBuilderMode.FULL;
this.jetTypeMapper.setBindingContext(bindingContext);
this.jetTypeMapper.setBindingTrace(bindingTrace);
this.jetTypeMapper.setBuiltinToJavaTypesMapping(builtinToJavaTypesMapping);
this.jetTypeMapper.setClassBuilderMode(classBuilderMode);
@@ -77,7 +77,7 @@ public class InjectorForJvmCodegen {
this.classFileFactory = new ClassFileFactory();
this.memberCodegen = new MemberCodegen();
this.jetTypeMapper.setBindingContext(bindingContext);
this.jetTypeMapper.setBindingTrace(bindingTrace);
this.jetTypeMapper.setBuiltinToJavaTypesMapping(builtinToJavaTypesMapping);
this.jetTypeMapper.setClassBuilderMode(classBuilderMode);
@@ -66,6 +66,11 @@ public class CoreExternalAnnotationsManager extends BaseExternalAnnotationsManag
throw new UnsupportedOperationException();
}
public boolean editExternalAnnotation(@NotNull PsiModifierListOwner listOwner, @NotNull String annotationFQN,
@NotNull PsiNameValuePair[] value) {
throw new UnsupportedOperationException();
}
@Override
public AnnotationPlace chooseAnnotationsPlace(@NotNull PsiElement element) {
throw new UnsupportedOperationException();
@@ -1,3 +1,4 @@
/*
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
@@ -18,8 +19,8 @@ package org.jetbrains.jet.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
/**
@@ -33,8 +34,18 @@ public final class QualifiedNamesUtil {
}
public static boolean isSubpackageOf(@NotNull final FqName subpackageName, @NotNull FqName packageName) {
return subpackageName.equals(packageName) ||
(subpackageName.getFqName().startsWith(packageName.getFqName()) && subpackageName.getFqName().charAt(packageName.getFqName().length()) == '.');
if (subpackageName.equals(packageName)) {
return true;
}
if (packageName.isRoot()) {
return true;
}
String subpackageNameStr = subpackageName.getFqName();
String packageNameStr = packageName.getFqName();
return (subpackageNameStr.startsWith(packageNameStr) && subpackageNameStr.charAt(packageNameStr.length()) == '.');
}
public static boolean isOneSegmentFQN(@NotNull final String fqn) {
@@ -84,7 +95,7 @@ public final class QualifiedNamesUtil {
*/
@NotNull
public static String tail(@NotNull FqName headFQN, @NotNull FqName fullFQN) {
if (!isSubpackageOf(fullFQN, headFQN)) {
if (!isSubpackageOf(fullFQN, headFQN) || headFQN.isRoot()) {
return fullFQN.getFqName();
}
@@ -29,7 +29,7 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.codegen.CodegenUtil;
import org.jetbrains.jet.codegen.NamespaceCodegen;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.java.JavaPsiFacadeKotlinHacks;
@@ -136,11 +136,15 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade
if (localName != null) {
FqName fqn = QualifiedNamesUtil.combine(containerFqn, Name.identifier(localName));
if (qualifiedName.equals(fqn)) {
answer.add(new JetLightClass(psiManager, file, qualifiedName));
if (!(declaration instanceof JetEnumEntry)) {
answer.add(new JetLightClass(psiManager, file, qualifiedName));
}
}
else {
for (JetDeclaration child : ((JetClassOrObject) declaration).getDeclarations()) {
scanClasses(answer, child, qualifiedName, fqn, file);
else if (QualifiedNamesUtil.isSubpackageOf(qualifiedName, fqn)) {
if (!(declaration instanceof JetEnumEntry)) {
for (JetDeclaration child : ((JetClassOrObject) declaration).getDeclarations()) {
scanClasses(answer, child, qualifiedName, fqn, file);
}
}
}
}
@@ -156,7 +160,7 @@ public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacade
if (given != null) return given;
if (declaration instanceof JetObjectDeclaration) {
return JetTypeMapper.getLocalNameForObject((JetObjectDeclaration) declaration);
return CodegenUtil.getLocalNameForObject((JetObjectDeclaration) declaration);
}
return null;
@@ -0,0 +1,18 @@
enum class Direction {
NORTH
SOUTH(val x : Int) {
fun again() : String {
return "Hello"
}
class Hello
}
WEST {
class Some {
fun test() : Int {
return 12 + 14
}
}
}
EAST
}
+1
View File
@@ -12,6 +12,7 @@
<orderEntry type="module" module-name="frontend.java" />
<orderEntry type="module" module-name="runtime" />
<orderEntry type="module" module-name="cli" />
<orderEntry type="module" module-name="jet.as.java.psi" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="library" name="asm-addons" level="project" />
<orderEntry type="library" name="dx-android" level="project" />
@@ -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.asJava;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CommonConfigurationKeys;
import org.jetbrains.jet.config.CompilerConfiguration;
/**
* @author Nikolay Krasko
*/
public class JavaElementFinderTest extends JetLiteFixture {
private JavaElementFinder finder;
public JavaElementFinderTest() {
super("asJava/findClasses");
}
@Override
protected JetCoreEnvironment createEnvironment() {
CompilerConfiguration configuration = new CompilerConfiguration();
configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, getTestDataPath() + "/asJava/findClasses/" + getTestName(false) + ".kt");
return new JetCoreEnvironment(getTestRootDisposable(), configuration);
}
@Override
public void setUp() throws Exception {
super.setUp();
finder = new JavaElementFinder(getProject());
}
public void testFromEnumEntry() {
assertClass("Direction");
assertNoClass("Direction.NORTH");
assertNoClass("Direction.SOUTH");
assertNoClass("Direction.WEST");
// TODO: assertClass("Direction.SOUTH.Hello");
// TODO: assertClass("Direction.WEST.Some");
}
private void assertClass(String qualifiedName) {
PsiClass psiClass = finder.findClass(qualifiedName, GlobalSearchScope.allScope(getProject()));
assertNotNull(String.format("Class with fqn='%s' wasn't found.", qualifiedName), psiClass);
assertTrue(String.format("Class with fqn='%s' is not valid.", qualifiedName), psiClass.isValid());
}
private void assertNoClass(String qualifiedName) {
assertNull(String.format("Class with fqn='%s' isn't expected to be found.", qualifiedName),
finder.findClass(qualifiedName, GlobalSearchScope.allScope(getProject())));
}
}
@@ -37,7 +37,15 @@ import java.util.HashSet;
public class JetGotoClassContributor implements GotoClassContributor {
@Override
public String getQualifiedName(NavigationItem item) {
return "Hello";
if (item instanceof JetNamedDeclaration) {
JetNamedDeclaration jetClass = (JetNamedDeclaration) item;
FqName name = JetPsiUtil.getFQName(jetClass);
if (name != null) {
return name.getFqName();
}
}
return null;
}
@Override
@@ -80,7 +88,7 @@ public class JetGotoClassContributor implements GotoClassContributor {
// items.add((JetObjectDeclaration) classOrObject);
}
else if (classOrObject instanceof JetClass) {
items.add((JetClass) classOrObject);
items.add(classOrObject);
}
else {
assert false;
@@ -37,8 +37,8 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.ImportPath;
import org.jetbrains.jet.lang.resolve.QualifiedExpressionResolver;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -90,7 +90,7 @@ public class JetShortNamesCache extends PsiShortNamesCache {
// Quick check for classes from getAllClassNames()
Collection<JetClassOrObject> classOrObjects = JetShortClassNameIndex.getInstance().get(name, project, scope);
if (classOrObjects.isEmpty()) {
return new PsiClass[0];
return PsiClass.EMPTY_ARRAY;
}
List<PsiClass> result = new ArrayList<PsiClass>();
@@ -146,7 +146,7 @@ public class JetPositionManager implements PositionManager {
result.set(getJvmInternalNameForImpl(typeMapper, (JetClassOrObject) element));
}
else if (element instanceof JetFunctionLiteralExpression) {
result.set(classNameForAnonymousClass(typeMapper.bindingContext,
result.set(classNameForAnonymousClass(typeMapper.getBindingContext(),
(JetFunctionLiteralExpression) element).getInternalName());
}
else if (element instanceof JetNamedFunction) {
@@ -155,14 +155,14 @@ public class JetPositionManager implements PositionManager {
result.set(getJvmInternalNameForImpl(typeMapper, (JetClassOrObject) parent));
}
else if (parent instanceof JetFunctionLiteralExpression || parent instanceof JetNamedFunction) {
result.set(classNameForAnonymousClass(typeMapper.bindingContext,
result.set(classNameForAnonymousClass(typeMapper.getBindingContext(),
(JetElement) element).getInternalName());
}
}
if (result.isNull()) {
FqName fqName = JetPsiUtil.getFQName(namespace);
boolean multiFileNamespace = isMultiFileNamespace(typeMapper.bindingContext, fqName);
boolean multiFileNamespace = isMultiFileNamespace(typeMapper.getBindingContext(), fqName);
String namespaceInternalName = NamespaceCodegen.getJVMClassNameForKotlinNs(fqName).getInternalName();
if (multiFileNamespace) {
result.set(NamespaceCodegen.getMultiFileNamespaceInternalName(namespaceInternalName, namespace));
@@ -179,7 +179,7 @@ public class JetPositionManager implements PositionManager {
@Nullable
private static String getJvmInternalNameForImpl(JetTypeMapper typeMapper, JetClassOrObject jetClass) {
final ClassDescriptor classDescriptor = typeMapper.bindingContext.get(BindingContext.CLASS, jetClass);
final ClassDescriptor classDescriptor = typeMapper.getBindingContext().get(BindingContext.CLASS, jetClass);
if (classDescriptor == null) {
return null;
}
@@ -16,8 +16,10 @@
package org.jetbrains.jet.checkers;
import com.intellij.ProjectTopics;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.impl.ModuleRootEventImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
import org.jetbrains.jet.test.generator.SimpleTestClassModel;
@@ -32,6 +34,20 @@ import java.util.Arrays;
*/
public abstract class AbstractJetPsiCheckerTest extends LightDaemonAnalyzerTestCase {
@Override
protected void tearDown() throws Exception {
super.tearDown();
/*
* TODO: remove this when fixed in IDEA
* super.tearDown() calls cleanupForNextTest() on PsiManagerImpl, which invalidates a cache of view providers
* this affects ExternalAnnotationsManagerImpl making XmlFile instances cached in it invalid
* this results in external annotations being not found, unless we invalidate the cache in ExternalAnnotationsManagerImpl.
* Currently, sending this funny event is the only way (apart from reflection) to invalidate that cache.
* The problem will be fixed in IDEA soon (hopefully).
*/
getProject().getMessageBus().syncPublisher(ProjectTopics.PROJECT_ROOTS).rootsChanged(new ModuleRootEventImpl(getProject(), true));
}
public void doTest(@NotNull String filePath) throws Exception {
doTest(filePath, true, false);
}