Support inline classes as last expression in lambda
This commit is contained in:
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen;
|
||||
@@ -208,7 +197,9 @@ public class ClosureCodegen extends MemberCodegen<KtElement> {
|
||||
|
||||
generateBridge(
|
||||
typeMapper.mapAsmMethod(erasedInterfaceFunction),
|
||||
typeMapper.mapAsmMethod(funDescriptor)
|
||||
erasedInterfaceFunction.getReturnType(),
|
||||
typeMapper.mapAsmMethod(funDescriptor),
|
||||
funDescriptor.getReturnType()
|
||||
);
|
||||
|
||||
//TODO: rewrite cause ugly hack
|
||||
@@ -275,7 +266,12 @@ public class ClosureCodegen extends MemberCodegen<KtElement> {
|
||||
);
|
||||
}
|
||||
|
||||
protected void generateBridge(@NotNull Method bridge, @NotNull Method delegate) {
|
||||
protected void generateBridge(
|
||||
@NotNull Method bridge,
|
||||
@Nullable KotlinType bridgeReturnType,
|
||||
@NotNull Method delegate,
|
||||
@Nullable KotlinType delegateReturnType
|
||||
) {
|
||||
if (bridge.equals(delegate)) return;
|
||||
|
||||
MethodVisitor mv =
|
||||
@@ -306,7 +302,10 @@ public class ClosureCodegen extends MemberCodegen<KtElement> {
|
||||
}
|
||||
|
||||
iv.invokevirtual(asmType.getInternalName(), delegate.getName(), delegate.getDescriptor(), false);
|
||||
StackValue.onStack(delegate.getReturnType()).put(bridge.getReturnType(), iv);
|
||||
|
||||
StackValue
|
||||
.onStack(delegate.getReturnType(), delegateReturnType)
|
||||
.put(bridge.getReturnType(), bridgeReturnType, iv);
|
||||
|
||||
iv.areturn(bridge.getReturnType());
|
||||
|
||||
|
||||
@@ -326,11 +326,15 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
}
|
||||
|
||||
public void gen(KtElement expr, Type type) {
|
||||
StackValue value = Type.VOID_TYPE.equals(type) ? genStatement(expr) : gen(expr);
|
||||
putStackValue(expr, type, value);
|
||||
gen(expr, type, null);
|
||||
}
|
||||
|
||||
private void putStackValue(@Nullable KtElement expr, @NotNull Type type, @NotNull StackValue value) {
|
||||
public void gen(KtElement expr, Type type, KotlinType kotlinType) {
|
||||
StackValue value = Type.VOID_TYPE.equals(type) ? genStatement(expr) : gen(expr);
|
||||
putStackValue(expr, type, kotlinType, value);
|
||||
}
|
||||
|
||||
private void putStackValue(@Nullable KtElement expr, @NotNull Type type, @Nullable KotlinType kotlinType, @NotNull StackValue value) {
|
||||
// for repl store the result of the last line into special field
|
||||
if (value.type != Type.VOID_TYPE && state.getReplSpecific().getShouldGenerateScriptResultValue()) {
|
||||
ScriptContext context = getScriptContext();
|
||||
@@ -342,12 +346,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
}
|
||||
}
|
||||
|
||||
KotlinType ktType = null;
|
||||
if (expr instanceof KtExpression) {
|
||||
ktType = kotlinType((KtExpression) expr);
|
||||
}
|
||||
|
||||
value.put(type, ktType, v);
|
||||
value.put(type, kotlinType, v);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -1511,9 +1510,10 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
}
|
||||
|
||||
Type returnType = isNonLocalReturn ? nonLocalReturn.returnType : this.returnType;
|
||||
KotlinType returnKotlinType = isNonLocalReturn ? null : this.context.getFunctionDescriptor().getReturnType();
|
||||
StackValue valueToReturn = returnedExpression != null ? gen(returnedExpression) : StackValue.none();
|
||||
|
||||
putStackValue(returnedExpression, returnType, valueToReturn);
|
||||
putStackValue(returnedExpression, returnType, returnKotlinType, valueToReturn);
|
||||
|
||||
Label afterReturnLabel = new Label();
|
||||
generateFinallyBlocksIfNeeded(returnType, afterReturnLabel);
|
||||
@@ -1588,7 +1588,10 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
? Type.VOID_TYPE
|
||||
: returnType;
|
||||
|
||||
gen(expr, typeForExpression);
|
||||
KotlinType kotlinTypeForExpression =
|
||||
isBlockedNamedFunction || isVoidCoroutineLambda ? null : context.getFunctionDescriptor().getReturnType();
|
||||
|
||||
gen(expr, typeForExpression, kotlinTypeForExpression);
|
||||
|
||||
// If it does not end with return we should return something
|
||||
// because if we don't there can be VerifyError (specific cases with Nothing-typed expressions)
|
||||
@@ -3389,7 +3392,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
|
||||
v.mark(ifNull);
|
||||
v.pop();
|
||||
gen(expression.getRight(), exprType);
|
||||
gen(expression.getRight(), exprType, exprKotlinType);
|
||||
v.mark(end);
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -380,8 +380,11 @@ public abstract class StackValue {
|
||||
);
|
||||
}
|
||||
|
||||
private static void unboxInlineClass(@NotNull KotlinType kotlinType, @NotNull InstructionAdapter v) {
|
||||
private static void unboxInlineClass(@NotNull Type type, @NotNull KotlinType kotlinType, @NotNull InstructionAdapter v) {
|
||||
Type owner = KotlinTypeMapper.mapInlineClassTypeAsDeclaration(kotlinType);
|
||||
|
||||
coerce(type, owner, v);
|
||||
|
||||
Type resultType = KotlinTypeMapper.mapUnderlyingTypeOfInlineClassType(kotlinType);
|
||||
v.invokevirtual(
|
||||
owner.getInternalName(),
|
||||
@@ -429,7 +432,7 @@ public abstract class StackValue {
|
||||
boxInlineClass(fromKotlinType, v);
|
||||
}
|
||||
else if (!isFromTypeUnboxed) {
|
||||
unboxInlineClass(fromKotlinType, v);
|
||||
unboxInlineClass(fromType, fromKotlinType, v);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -48,7 +48,7 @@ class CoercionValue(
|
||||
class StackValueWithLeaveTask(
|
||||
val stackValue: StackValue,
|
||||
val leaveTasks: (StackValue) -> Unit
|
||||
) : StackValue(stackValue.type) {
|
||||
) : StackValue(stackValue.type, stackValue.kotlinType) {
|
||||
|
||||
override fun putReceiver(v: InstructionAdapter, isRead: Boolean) {
|
||||
stackValue.putReceiver(v, isRead)
|
||||
|
||||
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codegen.coroutines
|
||||
@@ -199,7 +188,7 @@ class CoroutineCodegenForLambda private constructor(
|
||||
val bridgeParameters = (1 until delegate.argumentTypes.size).map { AsmTypes.OBJECT_TYPE } + delegate.argumentTypes.last()
|
||||
val bridge = Method(delegate.name, delegate.returnType, bridgeParameters.toTypedArray())
|
||||
|
||||
generateBridge(bridge, delegate)
|
||||
generateBridge(bridge, createCoroutineDescriptor.returnType, delegate, createCoroutineDescriptor.returnType)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user