Code clean and simplification

This commit is contained in:
Mikhael Bogdanov
2014-02-27 20:15:46 +04:00
parent 1a011c3f9b
commit 286dd50be4
7 changed files with 27 additions and 39 deletions
@@ -1350,7 +1350,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
Type[] argumentTypes = superCallable.getAsmMethod().getArgumentTypes();
ResolvedCall resolvedCall = bindingContext.get(BindingContext.RESOLVED_CALL, superCall.getCalleeExpression());
assert resolvedCall != null;
pushMethodArguments(resolvedCall, Arrays.asList(argumentTypes), null);
pushMethodArguments(resolvedCall, Arrays.asList(argumentTypes), defaulCallGenerator);
}
v.invokespecial(type.getInternalName(), "<init>", constructor.getAsmMethod().getDescriptor());
@@ -1984,7 +1984,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
@NotNull
public StackValue invokeFunction(
@NotNull Call call,
Call call,
StackValue receiver,
ResolvedCall<? extends CallableDescriptor> resolvedCall
) {
@@ -2307,11 +2307,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return false;
}
public int pushMethodArguments(@NotNull ResolvedCall resolvedCall, List<Type> valueParameterTypes, CallGenerator callGenerator) {
public int pushMethodArguments(@NotNull ResolvedCall resolvedCall, List<Type> valueParameterTypes, @NotNull CallGenerator callGenerator) {
return pushMethodArguments(resolvedCall, valueParameterTypes, false, callGenerator);
}
private int pushMethodArguments(@NotNull ResolvedCall resolvedCall, List<Type> valueParameterTypes, boolean skipLast, CallGenerator callGenerator) {
private int pushMethodArguments(@NotNull ResolvedCall resolvedCall, List<Type> valueParameterTypes, boolean skipLast, @NotNull CallGenerator callGenerator) {
@SuppressWarnings("unchecked")
List<ResolvedValueArgument> valueArguments = resolvedCall.getValueArgumentsByIndex();
CallableDescriptor fd = resolvedCall.getResultingDescriptor();
@@ -2321,7 +2321,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
throw new IllegalStateException();
}
callGenerator = callGenerator != null ? callGenerator : defaulCallGenerator;
int mask = 0;
for (Iterator<ValueParameterDescriptor> iterator = valueParameters.iterator(); iterator.hasNext(); ) {
@@ -3409,7 +3408,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (callable instanceof CallableMethod) {
genThisAndReceiverFromResolvedCall(receiver, resolvedCall, (CallableMethod) callable);
boolean skipLast = !isGetter;
pushMethodArguments(resolvedCall, ((CallableMethod) callable).getValueParameterTypes(), skipLast, null);
pushMethodArguments(resolvedCall, ((CallableMethod) callable).getValueParameterTypes(), skipLast, defaulCallGenerator);
}
else {
gen(array, arrayType); // intrinsic method
@@ -180,7 +180,11 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
@NotNull
public MethodContext intoFunction(FunctionDescriptor descriptor) {
return new MethodContext(descriptor, getContextKind(), this, null);
return new MethodContext(descriptor, getContextKind(), this, null, false);
}
public MethodContext intoInlinedLambda(FunctionDescriptor descriptor) {
return new MethodContext(descriptor, getContextKind(), this, null, true);
}
@NotNull
@@ -459,4 +463,4 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
public CodegenContext findChildContext(@NotNull DeclarationDescriptor child) {
return childContexts == null ? null : childContexts.get(child);
}
}
}
@@ -34,7 +34,7 @@ public class ConstructorContext extends MethodContext {
@NotNull CodegenContext parent,
@Nullable MutableClosure closure
) {
super(contextDescriptor, kind, parent, closure);
super(contextDescriptor, kind, parent, closure, false);
}
@Override
@@ -30,26 +30,20 @@ public class MethodContext extends CodegenContext<CallableMemberDescriptor> {
private Label methodStartLabel;
private boolean isInlineClosure;
public MethodContext(
@NotNull FunctionDescriptor contextType,
@NotNull OwnerKind contextKind,
@NotNull CodegenContext parentContext
) {
this(contextType, contextKind, parentContext, null);
}
private final boolean isInliningLambda;
protected MethodContext(
@NotNull FunctionDescriptor contextDescriptor,
@NotNull OwnerKind contextKind,
@NotNull CodegenContext parentContext,
@Nullable MutableClosure closure
@Nullable MutableClosure closure,
boolean isInliningLambda
) {
super(contextDescriptor instanceof PropertyAccessorDescriptor
? ((PropertyAccessorDescriptor) contextDescriptor).getCorrespondingProperty()
: contextDescriptor, contextKind, parentContext, closure,
parentContext.hasThisDescriptor() ? parentContext.getThisDescriptor() : null, null);
this.isInliningLambda = isInliningLambda;
}
@Override
@@ -96,16 +90,12 @@ public class MethodContext extends CodegenContext<CallableMemberDescriptor> {
return false;
}
public void setInlineClosure(boolean isInlineClosure) {
this.isInlineClosure = isInlineClosure;
}
public boolean isInlineClosure() {
return isInlineClosure;
public boolean isInliningLambda() {
return isInliningLambda;
}
public boolean isSpecialStackValue(StackValue stackValue) {
if (isInlineClosure && stackValue instanceof StackValue.Composed) {
if (isInliningLambda && stackValue instanceof StackValue.Composed) {
StackValue prefix = ((StackValue.Composed) stackValue).prefix;
StackValue suffix = ((StackValue.Composed) stackValue).suffix;
if (prefix instanceof StackValue.Local && ((StackValue.Local) prefix).index == 0) {
@@ -213,18 +213,17 @@ public class InlineCodegen implements ParentCodegenAware, CallGenerator {
private void generateClosuresBodies() {
for (LambdaInfo info : expressionMap.values()) {
info.setNode(generateClosureBody(info));
info.setNode(generateLambdaBody(info));
}
}
private MethodNode generateClosureBody(LambdaInfo info) {
private MethodNode generateLambdaBody(LambdaInfo info) {
JetFunctionLiteral declaration = info.getFunctionLiteral();
FunctionDescriptor descriptor = info.getFunctionDescriptor();
MethodContext parentContext = codegen.getContext();
MethodContext context = parentContext.intoClosure(descriptor, codegen, typeMapper).intoFunction(descriptor);
context.setInlineClosure(true);
MethodContext context = parentContext.intoClosure(descriptor, codegen, typeMapper).intoInlinedLambda(descriptor);
JvmMethodSignature jvmMethodSignature = typeMapper.mapSignature(descriptor);
Method asmMethod = jvmMethodSignature.getAsmMethod();
@@ -294,7 +293,7 @@ public class InlineCodegen implements ParentCodegenAware, CallGenerator {
if (stackValue instanceof StackValue.Composed) {
//see: Method.isSpecialStackValue: go through aload 0
if (codegen.getContext().isInlineClosure() && codegen.getContext().getContextDescriptor() instanceof AnonymousFunctionDescriptor) {
if (codegen.getContext().isInliningLambda() && codegen.getContext().getContextDescriptor() instanceof AnonymousFunctionDescriptor) {
if (descriptor != null && !InlineUtil.hasNoinlineAnnotation(descriptor)) {
//TODO: check type of context
return false;
@@ -425,11 +424,11 @@ public class InlineCodegen implements ParentCodegenAware, CallGenerator {
return parent.intoFunction((FunctionDescriptor) descriptor);
}
throw new IllegalStateException("Coudn't build context for " + descriptorName(descriptor));
throw new IllegalStateException("Couldn't build context for " + descriptorName(descriptor));
}
private static boolean isStaticMethod(FunctionDescriptor functionDescriptor, MethodContext context) {
return (getMethodAsmFlags(functionDescriptor, context.getContextKind()) & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
return (getMethodAsmFlags(functionDescriptor, context.getContextKind()) & Opcodes.ACC_STATIC) != 0;
}
@NotNull
@@ -52,7 +52,7 @@ public class StupidSync extends IntrinsicMethod {
assert resolvedCall != null : "Resolved call for " + element.getText() + " should be not null";
codegen.pushMethodArguments(resolvedCall, Arrays.asList(OBJECT_TYPE, FUNCTION0_TYPE), null);
codegen.pushMethodArguments(resolvedCall, Arrays.asList(OBJECT_TYPE, FUNCTION0_TYPE), codegen.defaulCallGenerator);
v.invokestatic("kotlin/jvm/internal/Intrinsics", "stupidSync", Type.getMethodDescriptor(OBJECT_TYPE, OBJECT_TYPE, FUNCTION0_TYPE));
return OBJECT_TYPE;
}
@@ -37,11 +37,6 @@ public class InlineUtil {
public static boolean DEFAULT_INLINE_FLAG_FOR_STUB = false; /*always false*/
@NotNull
public static InlineStrategy getInlineType(@NotNull Annotated annotated) {
return getInlineType(annotated.getAnnotations());
}
public static boolean hasNoinlineAnnotation(@NotNull CallableDescriptor valueParameterDescriptor) {
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
return KotlinBuiltIns.containsAnnotation(valueParameterDescriptor, builtIns.getNoinlineClassAnnotation());
@@ -62,6 +57,7 @@ public class InlineUtil {
else {
assert argument instanceof EnumValue : "Inline annotation parameter should be inline entry but was: " + argument + "!";
ClassDescriptor value = ((EnumValue) argument).getValue();
assert value != null : "Value for enum value should be not null " + argument;
String name = value.getName().asString();
return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION;
}