Cleanup: apply "cascade if..." inspection (+ some others)

This commit is contained in:
Mikhail Glukhikh
2017-06-28 15:19:20 +03:00
committed by Mikhail Glukhikh
parent 9c06739594
commit 1d2017b0fc
80 changed files with 1079 additions and 1190 deletions
@@ -292,14 +292,10 @@ fun calcTypeForIEEE754ArithmeticIfNeeded(expression: KtExpression?, bindingConte
val dataFlow = DataFlowValueFactory.createDataFlowValue(expression!!, ktType, bindingContext, descriptor)
val stableTypes = bindingContext.getDataFlowInfoBefore(expression).getStableTypes(dataFlow)
return stableTypes.firstNotNullResult {
if (KotlinBuiltIns.isDoubleOrNullableDouble(it)) {
TypeAndNullability(Type.DOUBLE_TYPE, TypeUtils.isNullableType(it))
}
else if (KotlinBuiltIns.isFloatOrNullableFloat(it)) {
TypeAndNullability(Type.FLOAT_TYPE, TypeUtils.isNullableType(it))
}
else {
null
when {
KotlinBuiltIns.isDoubleOrNullableDouble(it) -> TypeAndNullability(Type.DOUBLE_TYPE, TypeUtils.isNullableType(it))
KotlinBuiltIns.isFloatOrNullableFloat(it) -> TypeAndNullability(Type.FLOAT_TYPE, TypeUtils.isNullableType(it))
else -> null
}
}
}
@@ -83,18 +83,16 @@ private fun ExpressionCodegen.createOptimizedForLoopGeneratorOrNull(
private fun getLoopRangeResolvedCall(forExpression: KtForExpression, bindingContext: BindingContext): ResolvedCall<out CallableDescriptor>? {
val loopRange = KtPsiUtil.deparenthesize(forExpression.loopRange)
if (loopRange is KtQualifiedExpression) {
val qualifiedExpression = loopRange as KtQualifiedExpression?
val selector = qualifiedExpression!!.selectorExpression
if (selector is KtCallExpression || selector is KtSimpleNameExpression) {
return selector.getResolvedCall(bindingContext)
when (loopRange) {
is KtQualifiedExpression -> {
val qualifiedExpression = loopRange as KtQualifiedExpression?
val selector = qualifiedExpression!!.selectorExpression
if (selector is KtCallExpression || selector is KtSimpleNameExpression) {
return selector.getResolvedCall(bindingContext)
}
}
}
else if (loopRange is KtSimpleNameExpression || loopRange is KtCallExpression) {
return loopRange.getResolvedCall(bindingContext)
}
else if (loopRange is KtBinaryExpression) {
return loopRange.operationReference.getResolvedCall(bindingContext)
is KtSimpleNameExpression, is KtCallExpression -> return loopRange.getResolvedCall(bindingContext)
is KtBinaryExpression -> return loopRange.operationReference.getResolvedCall(bindingContext)
}
return null
@@ -610,14 +610,10 @@ class PsiInlineCodegen(
override fun putClosureParametersOnStack(next: LambdaInfo, functionReferenceReceiver: StackValue?) {
activeLambda = next
if (next is ExpressionLambda) {
codegen.pushClosureOnStack(next.classDescriptor, true, this, functionReferenceReceiver)
}
else if (next is DefaultLambda) {
rememberCapturedForDefaultLambda(next)
}
else {
throw RuntimeException("Unknown lambda: $next")
when (next) {
is ExpressionLambda -> codegen.pushClosureOnStack(next.classDescriptor, true, this, functionReferenceReceiver)
is DefaultLambda -> rememberCapturedForDefaultLambda(next)
else -> throw RuntimeException("Unknown lambda: $next")
}
activeLambda = null
}
@@ -408,23 +408,27 @@ class PsiSourceCompilerForInline(private val codegen: ExpressionCodegen, overrid
sourceFile
)
if (descriptor is ScriptDescriptor) {
val earlierScripts = state.replSpecific.earlierScriptsForReplInterpreter
return parent.intoScript(
descriptor,
earlierScripts ?: emptyList(),
descriptor as ClassDescriptor, state.typeMapper
)
}
else if (descriptor is ClassDescriptor) {
val kind = if (DescriptorUtils.isInterface(descriptor)) OwnerKind.DEFAULT_IMPLS else OwnerKind.IMPLEMENTATION
return parent.intoClass(descriptor, kind, state)
}
else if (descriptor is FunctionDescriptor) {
return parent.intoFunction(descriptor)
return when (descriptor) {
is ScriptDescriptor -> {
val earlierScripts = state.replSpecific.earlierScriptsForReplInterpreter
parent.intoScript(
descriptor,
earlierScripts ?: emptyList(),
descriptor as ClassDescriptor, state.typeMapper
)
}
is ClassDescriptor -> {
val kind = if (DescriptorUtils.isInterface(descriptor)) OwnerKind.DEFAULT_IMPLS else OwnerKind.IMPLEMENTATION
parent.intoClass(descriptor, kind, state)
}
is FunctionDescriptor -> {
parent.intoFunction(descriptor)
}
else -> {
throw IllegalStateException("Couldn't build context for " + descriptor)
}
}
throw IllegalStateException("Couldn't build context for " + descriptor)
}
}
}
@@ -171,33 +171,35 @@ private fun getInlineName(
typeMapper: KotlinTypeMapper,
fileClassesProvider: JvmFileClassesProvider
): String {
if (currentDescriptor is PackageFragmentDescriptor) {
val file = DescriptorToSourceUtils.getContainingFile(codegenContext.contextDescriptor)
when (currentDescriptor) {
is PackageFragmentDescriptor -> {
val file = DescriptorToSourceUtils.getContainingFile(codegenContext.contextDescriptor)
val implementationOwnerType: Type? =
if (file == null) {
CodegenContextUtil.getImplementationOwnerClassType(codegenContext)
}
else fileClassesProvider.getFileClassType(file)
val implementationOwnerType: Type? =
if (file == null) {
CodegenContextUtil.getImplementationOwnerClassType(codegenContext)
}
else fileClassesProvider.getFileClassType(file)
if (implementationOwnerType == null) {
val contextDescriptor = codegenContext.contextDescriptor
throw RuntimeException(
"Couldn't find declaration for " +
contextDescriptor.containingDeclaration!!.name + "." + contextDescriptor.name +
"; context: " + codegenContext
)
if (implementationOwnerType == null) {
val contextDescriptor = codegenContext.contextDescriptor
throw RuntimeException(
"Couldn't find declaration for " +
contextDescriptor.containingDeclaration!!.name + "." + contextDescriptor.name +
"; context: " + codegenContext
)
}
return implementationOwnerType.internalName
}
return implementationOwnerType.internalName
}
else if (currentDescriptor is ClassifierDescriptor) {
return typeMapper.mapType(currentDescriptor).internalName
}
else if (currentDescriptor is FunctionDescriptor) {
val descriptor = typeMapper.bindingContext.get(CodegenBinding.CLASS_FOR_CALLABLE, currentDescriptor)
if (descriptor != null) {
return typeMapper.mapType(descriptor).internalName
is ClassifierDescriptor -> {
return typeMapper.mapType(currentDescriptor).internalName
}
is FunctionDescriptor -> {
val descriptor = typeMapper.bindingContext.get(CodegenBinding.CLASS_FOR_CALLABLE, currentDescriptor)
if (descriptor != null) {
return typeMapper.mapType(descriptor).internalName
}
}
}
@@ -43,21 +43,23 @@ object JavaClassProperty : IntrinsicPropertyGetter() {
fun generateImpl(v: InstructionAdapter, receiver: StackValue): Type {
val type = receiver.type
if (type == Type.VOID_TYPE) {
receiver.put(Type.VOID_TYPE, v)
StackValue.unit().put(UNIT_TYPE, v)
v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;", false)
}
else if (isPrimitive(type)) {
if (!StackValue.couldSkipReceiverOnStaticCall(receiver)) {
receiver.put(type, v)
AsmUtil.pop(v, type)
when {
type == Type.VOID_TYPE -> {
receiver.put(Type.VOID_TYPE, v)
StackValue.unit().put(UNIT_TYPE, v)
v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;", false)
}
isPrimitive(type) -> {
if (!StackValue.couldSkipReceiverOnStaticCall(receiver)) {
receiver.put(type, v)
AsmUtil.pop(v, type)
}
v.getstatic(boxType(type).internalName, "TYPE", "Ljava/lang/Class;")
}
else -> {
receiver.put(type, v)
v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;", false)
}
v.getstatic(boxType(type).internalName, "TYPE", "Ljava/lang/Class;")
}
else {
receiver.put(type, v)
v.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;", false)
}
return getType(Class::class.java)