-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Bridging: Implement bridges required for ongoing AutoDiff changes #84648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ public class Decl: CustomStringConvertible, Hashable { | |
public class ValueDecl: Decl { | ||
final public var nameLoc: SourceLoc? { SourceLoc(bridged: bridged.Value_getNameLoc()) } | ||
final public var userFacingName: StringRef { StringRef(bridged: bridged.Value_getUserFacingName()) } | ||
final public var baseIdentifier: swift.Identifier { bridged.Value_getBaseIdentifier() } | ||
final public var isObjC: Bool { bridged.Value_isObjC() } | ||
} | ||
|
||
|
@@ -56,6 +57,10 @@ public class NominalTypeDecl: GenericTypeDecl { | |
final public var valueTypeDestructor: DestructorDecl? { | ||
bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self) | ||
} | ||
|
||
public var declaredInterfaceType : AST.`Type` { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to qualify |
||
AST.`Type`(bridged: bridged.NominalType_getDeclaredInterfaceType()) | ||
} | ||
} | ||
|
||
final public class EnumDecl: NominalTypeDecl { | ||
|
@@ -118,6 +123,8 @@ final public class MacroDecl: ValueDecl {} | |
|
||
final public class EnumElementDecl: ValueDecl { | ||
public var hasAssociatedValues: Bool { bridged.EnumElementDecl_hasAssociatedValues() } | ||
public var parameterList: BridgedParameterList { bridged.EnumElementDecl_getParameterList() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't expose bridged data structures in APIs. Create a native swift |
||
public var nameStr: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just call it |
||
} | ||
|
||
final public class ExtensionDecl: Decl {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,18 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren { | |
} | ||
|
||
public var isEmpty: Bool { bridged.impl == nil } | ||
|
||
public var canonicalSignature: CanGenericSignature { CanGenericSignature(bridged: bridged.getCanonicalSignature()) } | ||
} | ||
|
||
public struct CanGenericSignature { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid abbreviations. Better: We should follow the general swift API design guidelines. You can take a look at https://www.swift.org/documentation/api-design-guidelines/ |
||
public let bridged: BridgedCanGenericSignature | ||
|
||
public init(bridged: BridgedCanGenericSignature) { | ||
self.bridged = bridged | ||
} | ||
|
||
public var isEmpty: Bool { bridged.impl == nil } | ||
|
||
public var genericSignature: GenericSignature { GenericSignature(bridged: bridged.getGenericSignature()) } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,30 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre | |
public func subst(with substitutionMap: SubstitutionMap) -> Type { | ||
return Type(bridged: bridged.subst(substitutionMap.bridged)) | ||
} | ||
|
||
public func mapTypeOutOfContext() -> Type { | ||
return Type(bridged: bridged.mapTypeOutOfContext()) | ||
} | ||
|
||
public func getReducedType(sig: GenericSignature) -> CanonicalType { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please copy the comment from the C++ definition here:
Also, a better argument label would be: |
||
CanonicalType(bridged: bridged.getReducedType(sig.bridged)) | ||
} | ||
|
||
public func GenericTypeParam_getName() -> swift.Identifier { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better: |
||
return bridged.GenericTypeParam_getName() | ||
} | ||
|
||
public func GenericTypeParam_getDepth() -> UInt { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better: It's better to return 'Int' instead of 'UInt'. Unlike C/C++, the philosophy of swift is to use plain 'Int' for such counts, indices, etc. |
||
return bridged.GenericTypeParam_getDepth() | ||
} | ||
|
||
public func GenericTypeParam_getIndex() -> UInt { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> |
||
return bridged.GenericTypeParam_getIndex() | ||
} | ||
|
||
public func GenericTypeParam_getParamKind() -> swift.GenericTypeParamKind { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> also: add a type alias |
||
return bridged.GenericTypeParam_getParamKind() | ||
} | ||
} | ||
|
||
/// A Type that is statically known to be canonical. | ||
|
@@ -86,6 +110,10 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti | |
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType { | ||
return rawType.subst(with: substitutionMap).canonical | ||
} | ||
|
||
public func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ->
Also, it make sense to move this to TypeProperties, so that it's also available for |
||
CanGenericSignature(bridged: bridged.SILFunctionType_getSubstGenericSignature()) | ||
} | ||
} | ||
|
||
/// Implements the common members of `AST.Type`, `AST.CanonicalType` and `SIL.Type`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,10 @@ extension CanonicalType { | |
precondition(isBox) | ||
return BoxFieldsArray(boxType: self, function: function) | ||
} | ||
|
||
public func loweredType(in function: Function) -> Type { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function already exists (in TypeProperties). See a few lines above |
||
function.bridged.getLoweredType(bridged).type | ||
} | ||
} | ||
|
||
extension Decl { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,10 @@ public class Argument : Value, Hashable { | |
|
||
public var sourceLoc: SourceLoc? { findVarDecl()?.nameLoc } | ||
|
||
public func replaceAllUsesWith(newArg: Argument) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. This API exists for Operand sequences, e.g.
|
||
bridged.replaceAllUsesWith(newArg.bridged) | ||
} | ||
|
||
public static func ==(lhs: Argument, rhs: Argument) -> Bool { | ||
lhs === rhs | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -662,6 +662,13 @@ public struct Builder { | |
return notifyNew(tuple.getAs(TupleInst.self)) | ||
} | ||
|
||
public func createTuple(elements: [Value]) -> TupleInst { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This API is dangerous because it works for most cases, but fails if the result type must match a tuple type with element labels. People should use the existing We should also remove this API from SILBuilder (not in this PR). |
||
let tuple = elements.withBridgedValues { valuesRef in | ||
return bridged.createTuple(valuesRef) | ||
} | ||
return notifyNew(tuple.getAs(TupleInst.self)) | ||
} | ||
|
||
public func createTupleExtract(tuple: Value, elementIndex: Int) -> TupleExtractInst { | ||
return notifyNew(bridged.createTupleExtract(tuple.bridged, elementIndex).getAs(TupleExtractInst.self)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,20 @@ extension Context { | |
} | ||
} | ||
|
||
public func getTupleType(elements: [AST.`Type`]) -> AST.`Type` { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please modify the existing |
||
let bridgedElements = elements.map { $0.bridged } | ||
return bridgedElements.withBridgedArrayRef { | ||
AST.`Type`(bridged: _bridged.getTupleType($0)) | ||
} | ||
} | ||
|
||
public func getTupleTypeWithLabels(elements: [AST.`Type`], labels: [swift.Identifier]) -> AST.`Type` { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better:
|
||
assert(elements.count == labels.count) | ||
return elements.withBridgedArrayRef{ | ||
eltArr in labels.withBridgedArrayRef{labelsArr in | ||
AST.`Type`(bridged: _bridged.getTupleTypeWithLabels(eltArr, labelsArr))}} | ||
} | ||
|
||
public var swiftArrayDecl: NominalTypeDecl { | ||
_bridged.getSwiftArrayDecl().getAs(NominalTypeDecl.self) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash | |
return Location(bridged: bridged.getLocation()) | ||
} | ||
|
||
public var sourceFile: BridgedNullableSourceFile { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't expose any bridged data structures in APIs. In case Also:
|
||
return bridged.getSourceFile() | ||
} | ||
|
||
final public var description: String { | ||
return String(taking: bridged.getDebugDescription()) | ||
} | ||
|
@@ -88,6 +92,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash | |
return AST.`Type`(bridged: bridged.mapTypeIntoContext(type.bridged)) | ||
} | ||
|
||
public func mapTypeIntoContext(_ type: Type) -> Type { | ||
return Type(bridged: bridged.mapTypeIntoContext(type.bridged)) | ||
} | ||
|
||
/// Returns true if the function is a definition and not only an external declaration. | ||
/// | ||
/// This is the case if the function contains a body, i.e. some basic blocks. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1351,6 +1351,7 @@ final public class PartialApplyInst : SingleValueInstruction, ApplySite { | |
public var hasUnknownResultIsolation: Bool { bridged.PartialApplyInst_hasUnknownResultIsolation() } | ||
public var unappliedArgumentCount: Int { bridged.PartialApply_getCalleeArgIndexOfFirstAppliedArg() } | ||
public var calleeConvention: ArgumentConvention { type.bridged.getCalleeConvention().convention } | ||
public var substitutionMap: SubstitutionMap { SubstitutionMap(bridged: bridged.PartialApplyInst_getSubstitutionMap()) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This already exists as a property of |
||
} | ||
|
||
final public class ApplyInst : SingleValueInstruction, FullApplySite { | ||
|
@@ -1929,6 +1930,7 @@ final public class SwitchValueInst : TermInst { | |
final public class SwitchEnumInst : TermInst { | ||
|
||
public var enumOp: Value { operands[0].value } | ||
public var numCases: Int { bridged.SwitchEnumInst_getNumCases() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. Use |
||
|
||
public struct CaseIndexArray : RandomAccessCollection { | ||
fileprivate let switchEnum: SwitchEnumInst | ||
|
@@ -1953,6 +1955,10 @@ final public class SwitchEnumInst : TermInst { | |
cases.first(where: { $0.0 == forCaseIndex })?.1 | ||
} | ||
|
||
public func getSuccessorForDefault() -> BasicBlock? { | ||
return self.bridged.SwitchEnumInst_getSuccessorForDefault().block | ||
} | ||
|
||
// This does not handle the special case where the default covers exactly | ||
// the "missing" case. | ||
public func getUniqueCase(forSuccessor: BasicBlock) -> Int? { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr | |
|
||
public var isAddress: Bool { bridged.isAddress() } | ||
public var isObject: Bool { !isAddress } | ||
public var category: ValueCategory { ValueCategory(bridged: bridged.getCategory()) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't add this. Use |
||
|
||
public var addressType: Type { bridged.getAddressType().type } | ||
public var objectType: Type { bridged.getObjectType().type } | ||
|
@@ -214,6 +215,16 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr | |
} | ||
return false | ||
} | ||
|
||
public func getEnumCasePayload(caseIdx: Int, function: Function) -> Type { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. Use |
||
bridged.getEnumCasePayload(caseIdx, function.bridged).type | ||
} | ||
|
||
public func mapTypeOutOfContext() -> Type { bridged.mapTypeOutOfContext().type } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please implement this using |
||
|
||
public static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. Use |
||
BridgedType.getPrimitiveType(canType: canType.bridged, silValueCategory: silValueCategory._bridged).type | ||
} | ||
} | ||
|
||
extension Type: Equatable { | ||
|
@@ -266,6 +277,7 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray { | |
} | ||
|
||
public struct EnumCase { | ||
public let enumElementDecl : EnumElementDecl | ||
public let payload: Type? | ||
public let index: Int | ||
} | ||
|
@@ -288,7 +300,8 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol { | |
caseIterator = caseIterator.getNext() | ||
caseIndex += 1 | ||
} | ||
return EnumCase(payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil, | ||
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(caseIterator).getAs(EnumElementDecl.self), | ||
payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil, | ||
index: caseIndex) | ||
} | ||
return nil | ||
|
@@ -304,6 +317,10 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray { | |
public subscript(_ index: Int) -> Type { | ||
type.bridged.getTupleElementType(index).type | ||
} | ||
|
||
public func label(at index: Int) -> swift.Identifier { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
type.bridged.getTupleElementLabel(index) | ||
} | ||
} | ||
|
||
public struct BoxFieldsArray : RandomAccessCollection, FormattedLikeArray { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,27 @@ public protocol Value : AnyObject, CustomStringConvertible { | |
var isLexical: Bool { get } | ||
} | ||
|
||
public enum ValueCategory { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't add this. The boolean isAddress/isObject is sufficient. |
||
case address | ||
case object | ||
|
||
public init(bridged: BridgedValueCategory) { | ||
switch bridged { | ||
case .Address: self = .address | ||
case .Object: self = .object | ||
default: | ||
fatalError("unsupported value category") | ||
} | ||
} | ||
|
||
public var _bridged: BridgedValueCategory { | ||
switch self { | ||
case .address: return BridgedValueCategory.Address | ||
case .object: return BridgedValueCategory.Object | ||
} | ||
} | ||
} | ||
|
||
public enum Ownership { | ||
/// A Value with `unowned` ownership kind is an independent value that | ||
/// has a lifetime that is only guaranteed to last until the next program | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't expose the
swift
namespace in APIs. Instead add a type alias: