Generate not-null assertions on extension receiver parameters

This commit is contained in:
Alexander Udalov
2015-02-02 15:53:04 +03:00
parent 39c9216edc
commit 5c2d0c6173
6 changed files with 83 additions and 15 deletions
@@ -597,18 +597,33 @@ public class AsmUtil {
// Private method is not accessible from other classes, no assertions needed
if (getVisibilityAccessFlag(descriptor) == ACC_PRIVATE) return;
for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) {
JetType type = parameter.getReturnType();
if (type == null || isNullableType(type)) continue;
ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
if (receiverParameter != null) {
genParamAssertion(v, state.getTypeMapper(), frameMap, receiverParameter, "$receiver");
}
int index = frameMap.getIndex(parameter);
Type asmType = state.getTypeMapper().mapType(type);
if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
v.load(index, asmType);
v.visitLdcInsn(parameter.getName().asString());
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkParameterIsNotNull",
"(Ljava/lang/Object;Ljava/lang/String;)V", false);
}
for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) {
genParamAssertion(v, state.getTypeMapper(), frameMap, parameter, parameter.getName().asString());
}
}
private static void genParamAssertion(
@NotNull InstructionAdapter v,
@NotNull JetTypeMapper typeMapper,
@NotNull FrameMap frameMap,
@NotNull CallableDescriptor parameter,
@NotNull String name
) {
JetType type = parameter.getReturnType();
if (type == null || isNullableType(type)) return;
int index = frameMap.getIndex(parameter);
Type asmType = typeMapper.mapType(type);
if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
v.load(index, asmType);
v.visitLdcInsn(name);
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkParameterIsNotNull",
"(Ljava/lang/Object;Ljava/lang/String;)V", false);
}
}
@@ -731,7 +731,13 @@ public class FunctionCodegen {
}
for (JvmMethodParameterSignature parameter : signature.getValueParameters()) {
if (parameter.getKind() != JvmMethodParameterKind.VALUE) {
if (parameter.getKind() == JvmMethodParameterKind.RECEIVER) {
ReceiverParameterDescriptor receiverParameter = function.getExtensionReceiverParameter();
if (receiverParameter != null) {
frameMap.enter(receiverParameter, state.getTypeMapper().mapType(receiverParameter));
}
}
else if (parameter.getKind() != JvmMethodParameterKind.VALUE) {
frameMap.enterTemp(parameter.getAsmType());
}
}