|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import SwiftJava |
| 16 | +import JavaLangReflect |
| 17 | +import SwiftSyntax |
| 18 | +import SwiftJavaConfigurationShared |
| 19 | +import Logging |
| 20 | + |
| 21 | +extension Type { |
| 22 | + /// Adjust the given type to use its bounds, mirroring what we do in |
| 23 | + /// mapping Java types into Swift. |
| 24 | + func adjustToJavaBounds(adjusted: inout Bool) -> Type { |
| 25 | + if let typeVariable = self.as(TypeVariable<GenericDeclaration>.self), |
| 26 | + typeVariable.getBounds().count == 1, |
| 27 | + let bound = typeVariable.getBounds()[0] { |
| 28 | + adjusted = true |
| 29 | + return bound |
| 30 | + } |
| 31 | + |
| 32 | + if let wildcardType = self.as(WildcardType.self), |
| 33 | + wildcardType.getUpperBounds().count == 1, |
| 34 | + let bound = wildcardType.getUpperBounds()[0] { |
| 35 | + adjusted = true |
| 36 | + return bound |
| 37 | + } |
| 38 | + |
| 39 | + return self |
| 40 | + } |
| 41 | + |
| 42 | + /// Determine whether this type is equivalent to or a subtype of the other |
| 43 | + /// type. |
| 44 | + func isEqualTo(_ other: Type, file: String = #file, line: Int = #line, function: String = #function) -> Bool { |
| 45 | + if self.javaHolder.object == other.javaHolder.object { |
| 46 | + return true |
| 47 | + } |
| 48 | + |
| 49 | + // First, adjust types to their bounds, if we need to. |
| 50 | + var anyAdjusted: Bool = false |
| 51 | + let adjustedSelf = self.adjustToJavaBounds(adjusted: &anyAdjusted) |
| 52 | + let adjustedOther = other.adjustToJavaBounds(adjusted: &anyAdjusted) |
| 53 | + if anyAdjusted { |
| 54 | + return adjustedSelf.isEqualTo(adjustedOther) |
| 55 | + } |
| 56 | + |
| 57 | + // If both are classes, check for equivalence. |
| 58 | + if let selfClass = self.as(JavaClass<JavaObject>.self), |
| 59 | + let otherClass = other.as(JavaClass<JavaObject>.self) { |
| 60 | + return selfClass.equals(otherClass.as(JavaObject.self)) |
| 61 | + } |
| 62 | + |
| 63 | + // If both are arrays, check that their component types are equivalent. |
| 64 | + if let selfArray = self.as(GenericArrayType.self), |
| 65 | + let otherArray = other.as(GenericArrayType.self) { |
| 66 | + return selfArray.getGenericComponentType().isEqualTo(otherArray.getGenericComponentType()) |
| 67 | + } |
| 68 | + |
| 69 | + // If both are parameterized types, check their raw type and type |
| 70 | + // arguments for equivalence. |
| 71 | + if let selfParameterizedType = self.as(ParameterizedType.self), |
| 72 | + let otherParameterizedType = other.as(ParameterizedType.self) { |
| 73 | + if !selfParameterizedType.getRawType().isEqualTo(otherParameterizedType.getRawType()) { |
| 74 | + return false |
| 75 | + } |
| 76 | + |
| 77 | + return selfParameterizedType.getActualTypeArguments() |
| 78 | + .allTypesEqual(otherParameterizedType.getActualTypeArguments()) |
| 79 | + } |
| 80 | + |
| 81 | + // If both are type variables, compare their bounds. |
| 82 | + // FIXME: This is a hack. |
| 83 | + if let selfTypeVariable = self.as(TypeVariable<GenericDeclaration>.self), |
| 84 | + let otherTypeVariable = other.as(TypeVariable<GenericDeclaration>.self) { |
| 85 | + return selfTypeVariable.getBounds().allTypesEqual(otherTypeVariable.getBounds()) |
| 86 | + } |
| 87 | + |
| 88 | + // If both are wildcards, compare their upper and lower bounds. |
| 89 | + if let selfWildcard = self.as(WildcardType.self), |
| 90 | + let otherWildcard = other.as(WildcardType.self) { |
| 91 | + return selfWildcard.getUpperBounds().allTypesEqual(otherWildcard.getUpperBounds()) |
| 92 | + && selfWildcard.getLowerBounds().allTypesEqual(otherWildcard.getLowerBounds()) |
| 93 | + } |
| 94 | + |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + /// Determine whether this type is equivalent to or a subtype of the |
| 99 | + /// other type. |
| 100 | + func isEqualToOrSubtypeOf(_ other: Type) -> Bool { |
| 101 | + // First, adjust types to their bounds, if we need to. |
| 102 | + var anyAdjusted: Bool = false |
| 103 | + let adjustedSelf = self.adjustToJavaBounds(adjusted: &anyAdjusted) |
| 104 | + let adjustedOther = other.adjustToJavaBounds(adjusted: &anyAdjusted) |
| 105 | + if anyAdjusted { |
| 106 | + return adjustedSelf.isEqualToOrSubtypeOf(adjustedOther) |
| 107 | + } |
| 108 | + |
| 109 | + if isEqualTo(other) { |
| 110 | + return true |
| 111 | + } |
| 112 | + |
| 113 | + // If both are classes, check for subclassing. |
| 114 | + if let selfClass = self.as(JavaClass<JavaObject>.self), |
| 115 | + let otherClass = other.as(JavaClass<JavaObject>.self) { |
| 116 | + // If either is a Java array, then this cannot be a subtype relationship |
| 117 | + // in Swift. |
| 118 | + if selfClass.isArray() || otherClass.isArray() { |
| 119 | + return false |
| 120 | + } |
| 121 | + |
| 122 | + return selfClass.isSubclass(of: otherClass) |
| 123 | + } |
| 124 | + |
| 125 | + // Anything object-like is a subclass of java.lang.Object |
| 126 | + if let otherClass = other.as(JavaClass<JavaObject>.self), |
| 127 | + otherClass.getName() == "java.lang.Object" { |
| 128 | + if self.is(GenericArrayType.self) || self.is(ParameterizedType.self) || |
| 129 | + self.is(WildcardType.self) || self.is(TypeVariable<GenericDeclaration>.self) { |
| 130 | + return true |
| 131 | + } |
| 132 | + } |
| 133 | + return false |
| 134 | + } |
| 135 | +} |
0 commit comments