From b3d31c7d512c16bc266ee73444df000e0790dcca Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Mon, 6 Oct 2025 14:45:09 +0300 Subject: [PATCH] Bridging: Implement bridges required for ongoing AutoDiff changes In #83926, part of the changes resolving #68944 is submitted. The AutoDiff closure specialization optimizer pass relies on several bridges not implemented yet. This patch introduces these missing bridges. See the list of the changes below. **AST:** * Define `.asValueDecl` on each BridgedXXXDecl type that's also a ValueDecl. * Define `.asNominalTypeDecl` on each BridgedXXXDecl type that's also a NominalTypeDecl. * Define `.asGenericContext` on each BridgedXXXDecl type that's also a GenericContext. * `class BridgedSourceFile`: - Make nullable - `func addTopLevelDecl(_ decl: BridgedDecl)` * `class BridgedFileUnit`: - `func castToSourceFile() -> BridgedNullableSourceFile` * `class BridgedDecl`: - `func getDeclContext() -> BridgedDeclContext` * `class BridgedParamDecl`: - `func cloneWithoutType() -> BridgedParamDecl` - `func setInterfaceType(_ type : BridgedASTType)` * Define `BridgedDecl.setImplicit()` instead of `BridgedParamDecl.setImplicit()` * `class BridgedGenericContext`: - `setGenericSignature(_ genSig: BridgedGenericSignature)` * Change return type of `BridgedEnumDecl.createParsed(/*...*/)` from `BridgedNominalTypeDecl` to `BridgedEnumDecl` * `class BridgedValueDecl`: - `func setAccess(_ accessLevel: swift.AccessLevel)` * `class BridgedNominalTypeDecl`: - `func addMember(_ member: BridgedDecl)` * `class BridgedGenericTypeParamDecl`: - `func createImplicit(declContext: BridgedDeclContext, name: swift.Identifier, depth: UInt, index: UInt, paramKind: swift.GenericTypeParamKind)` * `class ValueDecl`: - `var baseIdentifier: swift.Identifier` * `class NominalTypeDecl`: - `var declaredInterfaceType: Type` * `class EnumElementDecl`: - `var parameterList: BridgedParameterList` - `var nameStr: StringRef` * `struct GenericSignature`: - `var canonicalSignature: CanGenericSignature` * `struct CanGenericSignature`: - `var isEmpty: Bool` - `var genericSignature: GenericSignature` * `struct Type`: - `func mapTypeOutOfContext() -> Type` - `func getReducedType(sig: GenericSignature) -> CanonicalType` - `func GenericTypeParam_getName() -> swift.Identifier` - `func GenericTypeParam_getDepth() -> UInt` - `func GenericTypeParam_getIndex() -> UInt` - `func GenericTypeParam_getParamKind() -> swift.GenericTypeParamKind` * `struct CanonicalType`: - `func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature` - `func loweredType(in function: SIL.Function) -> SIL.Type` **SIL:** * `class Argument`: - `func replaceAllUsesWith(newArg: Argument)` * `class BasicBlock`: - `func insertPhiArgument(atPosition: Int, type: Type, ownership: Ownership, _ context: some MutatingContext) -> Argument` * `struct Builder`: - `func createTuple(elements: [Value]) -> TupleInst` * `protocol Context`: - `func getTupleType(elements: [AST.Type]) -> AST.Type` - `func getTupleTypeWithLabels(elements: [AST.Type], labels: [swift.Identifier]) -> AST.Type` * `class Function`: - `var sourceFile: BridgedNullableSourceFile` - `func mapTypeIntoContext(_ type: Type) -> Type` * `class PartialApplyInst`: - `var substitutionMap: SubstitutionMap` * `class SwitchEnumInst`: - `var numCases: Int` - `public func getSuccessorForDefault() -> BasicBlock?` * `Type`: - `var category: ValueCategory` - `func getEnumCasePayload(caseIdx: Int, function: Function) -> Type` - `func mapTypeOutOfContext() -> Type` - `static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type` * `struct EnumCase`: - `let enumElementDecl: EnumElementDecl` * `struct TupleElementArray`: - `func label(at index: Int) -> swift.Identifier` * Define `enum ValueCategory` with `address` and `object` elements --- .../Sources/AST/Declarations.swift | 7 + .../Sources/AST/GenericSignature.swift | 14 ++ SwiftCompilerSources/Sources/AST/Type.swift | 28 ++++ .../Sources/SIL/ASTExtensions.swift | 4 + .../Sources/SIL/Argument.swift | 4 + .../Sources/SIL/BasicBlock.swift | 5 + .../Sources/SIL/Builder.swift | 7 + .../Sources/SIL/Context.swift | 14 ++ .../Sources/SIL/Function.swift | 8 + .../Sources/SIL/Instruction.swift | 6 + SwiftCompilerSources/Sources/SIL/Type.swift | 19 ++- SwiftCompilerSources/Sources/SIL/Value.swift | 21 +++ include/swift/AST/ASTBridging.h | 100 ++++++++++++- include/swift/AST/ASTBridgingImpl.h | 101 ++++++++++++- include/swift/AST/ASTBridgingWrappers.def | 4 +- include/swift/SIL/SILBridging.h | 35 +++++ include/swift/SIL/SILBridgingImpl.h | 137 ++++++++++++++++++ lib/AST/Bridging/DeclBridging.cpp | 43 +++++- lib/AST/Bridging/GenericsBridging.cpp | 9 ++ lib/ASTGen/Sources/ASTGen/Decls.swift | 4 +- lib/ASTGen/Sources/ASTGen/Exprs.swift | 2 +- 21 files changed, 561 insertions(+), 11 deletions(-) diff --git a/SwiftCompilerSources/Sources/AST/Declarations.swift b/SwiftCompilerSources/Sources/AST/Declarations.swift index 2041d32fd22de..6724cf2f16448 100644 --- a/SwiftCompilerSources/Sources/AST/Declarations.swift +++ b/SwiftCompilerSources/Sources/AST/Declarations.swift @@ -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` { + 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() } + public var nameStr: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) } } final public class ExtensionDecl: Decl {} diff --git a/SwiftCompilerSources/Sources/AST/GenericSignature.swift b/SwiftCompilerSources/Sources/AST/GenericSignature.swift index 64b858e9b4bd2..f91ad0504ba00 100644 --- a/SwiftCompilerSources/Sources/AST/GenericSignature.swift +++ b/SwiftCompilerSources/Sources/AST/GenericSignature.swift @@ -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 { + 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()) } } diff --git a/SwiftCompilerSources/Sources/AST/Type.swift b/SwiftCompilerSources/Sources/AST/Type.swift index c193387800f1a..008fc55776bc0 100644 --- a/SwiftCompilerSources/Sources/AST/Type.swift +++ b/SwiftCompilerSources/Sources/AST/Type.swift @@ -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 { + CanonicalType(bridged: bridged.getReducedType(sig.bridged)) + } + + public func GenericTypeParam_getName() -> swift.Identifier { + return bridged.GenericTypeParam_getName() + } + + public func GenericTypeParam_getDepth() -> UInt { + return bridged.GenericTypeParam_getDepth() + } + + public func GenericTypeParam_getIndex() -> UInt { + return bridged.GenericTypeParam_getIndex() + } + + public func GenericTypeParam_getParamKind() -> swift.GenericTypeParamKind { + 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 { + CanGenericSignature(bridged: bridged.SILFunctionType_getSubstGenericSignature()) + } } /// Implements the common members of `AST.Type`, `AST.CanonicalType` and `SIL.Type`. diff --git a/SwiftCompilerSources/Sources/SIL/ASTExtensions.swift b/SwiftCompilerSources/Sources/SIL/ASTExtensions.swift index f02ab08bef4a8..76b47a4cda938 100644 --- a/SwiftCompilerSources/Sources/SIL/ASTExtensions.swift +++ b/SwiftCompilerSources/Sources/SIL/ASTExtensions.swift @@ -36,6 +36,10 @@ extension CanonicalType { precondition(isBox) return BoxFieldsArray(boxType: self, function: function) } + + public func loweredType(in function: Function) -> Type { + function.bridged.getLoweredType(bridged).type + } } extension Decl { diff --git a/SwiftCompilerSources/Sources/SIL/Argument.swift b/SwiftCompilerSources/Sources/SIL/Argument.swift index 8a9c861366501..88b3662761057 100644 --- a/SwiftCompilerSources/Sources/SIL/Argument.swift +++ b/SwiftCompilerSources/Sources/SIL/Argument.swift @@ -50,6 +50,10 @@ public class Argument : Value, Hashable { public var sourceLoc: SourceLoc? { findVarDecl()?.nameLoc } + public func replaceAllUsesWith(newArg: Argument) { + bridged.replaceAllUsesWith(newArg.bridged) + } + public static func ==(lhs: Argument, rhs: Argument) -> Bool { lhs === rhs } diff --git a/SwiftCompilerSources/Sources/SIL/BasicBlock.swift b/SwiftCompilerSources/Sources/SIL/BasicBlock.swift index c543559c24489..cdd548f5dc8fe 100644 --- a/SwiftCompilerSources/Sources/SIL/BasicBlock.swift +++ b/SwiftCompilerSources/Sources/SIL/BasicBlock.swift @@ -68,6 +68,11 @@ final public class BasicBlock : CustomStringConvertible, HasShortDescription, Ha (decl as Decl?).bridged).argument as! FunctionArgument } + public func insertPhiArgument(atPosition: Int, type: Type, ownership: Ownership, _ context: some MutatingContext) -> Argument { + context.notifyInstructionsChanged() + return bridged.insertPhiArgument(atPosition, type.bridged, ownership._bridged).argument + } + public func eraseArgument(at index: Int, _ context: some MutatingContext) { context.notifyInstructionsChanged() bridged.eraseArgument(index) diff --git a/SwiftCompilerSources/Sources/SIL/Builder.swift b/SwiftCompilerSources/Sources/SIL/Builder.swift index 5072bf4759393..43fcb73ce27a5 100644 --- a/SwiftCompilerSources/Sources/SIL/Builder.swift +++ b/SwiftCompilerSources/Sources/SIL/Builder.swift @@ -662,6 +662,13 @@ public struct Builder { return notifyNew(tuple.getAs(TupleInst.self)) } + public func createTuple(elements: [Value]) -> TupleInst { + 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)) } diff --git a/SwiftCompilerSources/Sources/SIL/Context.swift b/SwiftCompilerSources/Sources/SIL/Context.swift index 1db07567efaeb..3384573b6a29d 100644 --- a/SwiftCompilerSources/Sources/SIL/Context.swift +++ b/SwiftCompilerSources/Sources/SIL/Context.swift @@ -55,6 +55,20 @@ extension Context { } } + public func getTupleType(elements: [AST.`Type`]) -> AST.`Type` { + 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` { + 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) } diff --git a/SwiftCompilerSources/Sources/SIL/Function.swift b/SwiftCompilerSources/Sources/SIL/Function.swift index a802984455223..548b08b69da9b 100644 --- a/SwiftCompilerSources/Sources/SIL/Function.swift +++ b/SwiftCompilerSources/Sources/SIL/Function.swift @@ -26,6 +26,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash return Location(bridged: bridged.getLocation()) } + public var sourceFile: BridgedNullableSourceFile { + 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. diff --git a/SwiftCompilerSources/Sources/SIL/Instruction.swift b/SwiftCompilerSources/Sources/SIL/Instruction.swift index 6786cb516f2f2..c33ae1772f725 100644 --- a/SwiftCompilerSources/Sources/SIL/Instruction.swift +++ b/SwiftCompilerSources/Sources/SIL/Instruction.swift @@ -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()) } } 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() } 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? { diff --git a/SwiftCompilerSources/Sources/SIL/Type.swift b/SwiftCompilerSources/Sources/SIL/Type.swift index ea1767fccb7b7..397c4b4198e0d 100644 --- a/SwiftCompilerSources/Sources/SIL/Type.swift +++ b/SwiftCompilerSources/Sources/SIL/Type.swift @@ -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()) } 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 { + bridged.getEnumCasePayload(caseIdx, function.bridged).type + } + + public func mapTypeOutOfContext() -> Type { bridged.mapTypeOutOfContext().type } + + public static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type { + 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 { + type.bridged.getTupleElementLabel(index) + } } public struct BoxFieldsArray : RandomAccessCollection, FormattedLikeArray { diff --git a/SwiftCompilerSources/Sources/SIL/Value.swift b/SwiftCompilerSources/Sources/SIL/Value.swift index a346cb621ba80..213a176d72832 100644 --- a/SwiftCompilerSources/Sources/SIL/Value.swift +++ b/SwiftCompilerSources/Sources/SIL/Value.swift @@ -41,6 +41,27 @@ public protocol Value : AnyObject, CustomStringConvertible { var isLexical: Bool { get } } +public enum ValueCategory { + 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 diff --git a/include/swift/AST/ASTBridging.h b/include/swift/AST/ASTBridging.h index ec88cb660a4f0..860ce0dfe5629 100644 --- a/include/swift/AST/ASTBridging.h +++ b/include/swift/AST/ASTBridging.h @@ -86,6 +86,7 @@ class BridgedASTContext; class BridgedLangOptions; struct BridgedSubstitutionMap; struct BridgedGenericSignature; +struct BridgedCanGenericSignature; struct BridgedConformance; class BridgedParameterList; @@ -315,11 +316,15 @@ struct BridgedDeclObj { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Type_getName() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Value_getUserFacingName() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::SourceLoc Value_getNameLoc() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::Identifier + Value_getBaseIdentifier() const; BRIDGED_INLINE bool hasClangNode() const; BRIDGED_INLINE bool Value_isObjC() const; BRIDGED_INLINE bool AbstractStorage_isConst() const; BRIDGED_INLINE bool GenericType_isGenericAtAnyLevel() const; BRIDGED_INLINE bool NominalType_isGlobalActor() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType + NominalType_getDeclaredInterfaceType() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj NominalType_getValueTypeDestructor() const; BRIDGED_INLINE bool Enum_hasRawType() const; BRIDGED_INLINE bool Struct_hasUnreferenceableStorage() const; @@ -329,6 +334,10 @@ struct BridgedDeclObj { BRIDGED_INLINE bool AbstractFunction_isOverridden() const; BRIDGED_INLINE bool Destructor_isIsolated() const; BRIDGED_INLINE bool EnumElementDecl_hasAssociatedValues() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParameterList + EnumElementDecl_getParameterList() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef + EnumElementDecl_getNameStr() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef AccessorDecl_getKindName() const; }; @@ -382,6 +391,23 @@ class BridgedASTNode { #define ABSTRACT_DECL(Id, Parent) DECL(Id, Parent) #include "swift/AST/DeclNodes.def" +// Declare `.asValueDecl` on each BridgedXXXDecl type that's also a +// ValueDecl. +#define DECL(Id, Parent) +#define VALUE_DECL(Id, Parent) \ + SWIFT_NAME("getter:Bridged" #Id "Decl.asValueDecl(self:)") \ + BridgedValueDecl Bridged##Id##Decl_asValueDecl(Bridged##Id##Decl decl); +#include "swift/AST/DeclNodes.def" + +// Declare `.asNominalTypeDecl` on each BridgedXXXDecl type that's also a +// NominalTypeDecl. +#define DECL(Id, Parent) +#define NOMINAL_TYPE_DECL(Id, Parent) \ + SWIFT_NAME("getter:Bridged" #Id "Decl.asNominalTypeDecl(self:)") \ + BridgedNominalTypeDecl Bridged##Id##Decl_asNominalTypeDecl( \ + Bridged##Id##Decl decl); +#include "swift/AST/DeclNodes.def" + // Declare `.asDeclContext` on each BridgedXXXDecl type that's also a // DeclContext. #define DECL(Id, Parent) @@ -391,6 +417,16 @@ class BridgedASTNode { #define ABSTRACT_CONTEXT_DECL(Id, Parent) CONTEXT_DECL(Id, Parent) #include "swift/AST/DeclNodes.def" +// Declare `.asGenericContext` on each BridgedXXXDecl type that's also a +// GenericContext. +#define DECL(Id, Parent) +#define GENERIC_DECL(Id, Parent) \ + SWIFT_NAME("getter:Bridged" #Id "Decl.asGenericContext(self:)") \ + BridgedGenericContext Bridged##Id##Decl_asGenericContext( \ + Bridged##Id##Decl decl); +#define ITERABLE_GENERIC_DECL(Id, Parent) GENERIC_DECL(Id, Parent) +#include "swift/AST/DeclNodes.def" + // Declare `.asStmt` on each BridgedXXXStmt type, which upcasts a wrapper for // a Stmt subclass to a BridgedStmt. #define STMT(Id, Parent) \ @@ -568,6 +604,14 @@ BridgedDeclContext_getParentSourceFile(BridgedDeclContext dc); SWIFT_NAME("getter:BridgedSourceFile.isScriptMode(self:)") BRIDGED_INLINE bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf); +SWIFT_NAME("BridgedSourceFile.addTopLevelDecl(self:_:)") +BRIDGED_INLINE void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf, + BridgedDecl decl); + +SWIFT_NAME("BridgedFileUnit.castToSourceFile(self:)") +BRIDGED_INLINE BridgedNullableSourceFile +BridgedFileUnit_castToSourceFile(BridgedFileUnit fileUnit); + SWIFT_NAME("BridgedPatternBindingInitializer.create(declContext:)") BridgedPatternBindingInitializer BridgedPatternBindingInitializer_create(BridgedDeclContext cDeclContext); @@ -1288,6 +1332,9 @@ SWIFT_NAME("BridgedDecl.forEachDeclToHoist(self:_:)") void BridgedDecl_forEachDeclToHoist(BridgedDecl decl, BridgedSwiftClosure closure); +SWIFT_NAME("BridgedDecl.getDeclContext(self:)") +BridgedDeclContext BridgedDecl_getDeclContext(BridgedDecl decl); + enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling { BridgedStaticSpellingNone, BridgedStaticSpellingStatic, @@ -1335,10 +1382,18 @@ BridgedParamDecl BridgedParamDecl_createParsed( swift::SourceLoc paramNameLoc, BridgedNullableExpr defaultValue, BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext); +SWIFT_NAME("BridgedParamDecl.cloneWithoutType(self:)") +BRIDGED_INLINE BridgedParamDecl +BridgedParamDecl_cloneWithoutType(BridgedParamDecl cDecl); + SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)") BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl, BridgedTypeRepr cType); +SWIFT_NAME("BridgedParamDecl.setInterfaceType(self:_:)") +BRIDGED_INLINE void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl, + BridgedASTType cType); + /// The various spellings of ownership modifier that can be used in source. enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier { BridgedParamSpecifierDefault, @@ -1357,8 +1412,13 @@ BRIDGED_INLINE void BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl, BridgedParamSpecifier cSpecifier); -SWIFT_NAME("BridgedParamDecl.setImplicit(self:)") -BRIDGED_INLINE void BridgedParamDecl_setImplicit(BridgedParamDecl cDecl); +SWIFT_NAME("BridgedDecl.setImplicit(self:)") +BRIDGED_INLINE void BridgedDecl_setImplicit(BridgedDecl cDecl); + +SWIFT_NAME("BridgedGenericContext.setGenericSignature(self:_:)") +BRIDGED_INLINE void +BridgedGenericContext_setGenericSignature(BridgedGenericContext cDecl, + BridgedGenericSignature cGenSig); SWIFT_NAME("BridgedConstructorDecl.setParsedBody(self:_:)") void BridgedConstructorDecl_setParsedBody(BridgedConstructorDecl decl, @@ -1422,7 +1482,7 @@ void BridgedExtensionDecl_setParsedMembers(BridgedExtensionDecl decl, SWIFT_NAME( "BridgedEnumDecl.createParsed(_:declContext:enumKeywordLoc:name:nameLoc:" "genericParamList:inheritedTypes:genericWhereClause:braceRange:)") -BridgedNominalTypeDecl BridgedEnumDecl_createParsed( +BridgedEnumDecl BridgedEnumDecl_createParsed( BridgedASTContext cContext, BridgedDeclContext cDeclContext, swift::SourceLoc enumKeywordLoc, swift::Identifier name, swift::SourceLoc nameLoc, BridgedNullableGenericParamList genericParamList, @@ -1613,6 +1673,10 @@ void BridgedTopLevelCodeDecl_dump(BridgedTopLevelCodeDecl decl); SWIFT_NAME("BridgedDecl.dump(self:)") void BridgedDecl_dump(BridgedDecl decl); +SWIFT_NAME("BridgedValueDecl.setAccess(self:_:)") +void BridgedValueDecl_setAccess(BridgedValueDecl decl, + swift::AccessLevel accessLevel); + //===----------------------------------------------------------------------===// // MARK: AbstractStorageDecl //===----------------------------------------------------------------------===// @@ -1662,6 +1726,11 @@ SWIFT_NAME("BridgedNominalTypeDecl.getSourceLocation(self:)") BRIDGED_INLINE swift::SourceLoc BridgedNominalTypeDecl_getSourceLocation(BridgedNominalTypeDecl decl); +SWIFT_NAME("BridgedNominalTypeDecl.addMember(self:_:)") +BRIDGED_INLINE void +BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl, + BridgedDecl member); + //===----------------------------------------------------------------------===// // MARK: SubscriptDecl //===----------------------------------------------------------------------===// @@ -2851,6 +2920,12 @@ BridgedGenericTypeParamDecl BridgedGenericTypeParamDecl_createParsed( swift::SourceLoc nameLoc, BridgedNullableTypeRepr opaqueInheritedType, size_t index, swift::GenericTypeParamKind paramKind); +SWIFT_NAME("BridgedGenericTypeParamDecl.createImplicit(declContext:" + "name:depth:index:paramKind:)") +BridgedGenericTypeParamDecl BridgedGenericTypeParamDecl_createImplicit( + BridgedDeclContext cDeclContext, swift::Identifier name, SwiftUInt depth, + SwiftUInt index, swift::GenericTypeParamKind paramKind); + SWIFT_NAME( "BridgedTrailingWhereClause.createParsed(_:whereKeywordLoc:requirements:)") BridgedTrailingWhereClause @@ -2968,6 +3043,15 @@ struct BridgedASTType { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeOutOfContext() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType + getReducedType(BridgedGenericSignature sig) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::Identifier + GenericTypeParam_getName() const; + BRIDGED_INLINE SwiftUInt GenericTypeParam_getDepth() const; + BRIDGED_INLINE SwiftUInt GenericTypeParam_getIndex() const; + BRIDGED_INLINE swift::GenericTypeParamKind + GenericTypeParam_getParamKind() const; }; class BridgedCanType { @@ -2978,6 +3062,8 @@ class BridgedCanType { BRIDGED_INLINE BridgedCanType(swift::CanType ty); BRIDGED_INLINE swift::CanType unbridged() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getRawType() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanGenericSignature + SILFunctionType_getSubstGenericSignature() const; }; struct BridgedASTTypeArray { @@ -3042,6 +3128,14 @@ struct BridgedGenericSignature { BridgedOwnedString getDebugDescription() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getGenericParams() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeIntoContext(BridgedASTType type) const; + BRIDGED_INLINE BridgedCanGenericSignature getCanonicalSignature() const; +}; + +struct BridgedCanGenericSignature { + const swift::GenericSignatureImpl *_Nullable impl; + + BRIDGED_INLINE swift::CanGenericSignature unbridged() const; + BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const; }; struct BridgedFingerprint { diff --git a/include/swift/AST/ASTBridgingImpl.h b/include/swift/AST/ASTBridgingImpl.h index b71a98e3b140d..e3b4c4a78e69c 100644 --- a/include/swift/AST/ASTBridgingImpl.h +++ b/include/swift/AST/ASTBridgingImpl.h @@ -148,6 +148,17 @@ bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) { return sf.unbridged()->isScriptMode(); } +void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf, BridgedDecl decl) { + auto &file = sf.unbridged()->getOrCreateSynthesizedFile(); + file.addTopLevelDecl(decl.unbridged()); + file.getParentModule()->clearLookupCache(); +} + +BridgedNullableSourceFile +BridgedFileUnit_castToSourceFile(BridgedFileUnit fileUnit) { + return llvm::dyn_cast(fileUnit.unbridged()); +} + //===----------------------------------------------------------------------===// // MARK: BridgedDeclObj //===----------------------------------------------------------------------===// @@ -176,6 +187,10 @@ swift::SourceLoc BridgedDeclObj::Value_getNameLoc() const { return getAs()->getNameLoc(); } +swift::Identifier BridgedDeclObj::Value_getBaseIdentifier() const { + return getAs()->getBaseIdentifier(); +} + bool BridgedDeclObj::hasClangNode() const { return unbridged()->hasClangNode(); } @@ -234,6 +249,19 @@ bool BridgedDeclObj::EnumElementDecl_hasAssociatedValues() const { return getAs()->hasAssociatedValues(); } +BridgedParameterList BridgedDeclObj::EnumElementDecl_getParameterList() const { + return getAs()->getParameterList(); +} + +BridgedStringRef BridgedDeclObj::EnumElementDecl_getNameStr() const { + return getAs()->getNameStr(); +} + +BridgedASTType BridgedDeclObj::NominalType_getDeclaredInterfaceType() const { + return { + getAs()->getDeclaredInterfaceType().getPointer()}; +} + //===----------------------------------------------------------------------===// // MARK: BridgedASTNode //===----------------------------------------------------------------------===// @@ -337,20 +365,44 @@ swift::ParamSpecifier unbridge(BridgedParamSpecifier specifier) { } } +BridgedParamDecl BridgedParamDecl_cloneWithoutType(BridgedParamDecl cDecl) { + return swift::ParamDecl::cloneWithoutType(cDecl.unbridged()->getASTContext(), + cDecl.unbridged()); +} + void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl, BridgedTypeRepr cType) { cDecl.unbridged()->setTypeRepr(cType.unbridged()); } +void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl, + BridgedASTType cType) { + cDecl.unbridged()->setInterfaceType(cType.unbridged()); +} + void BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl, BridgedParamSpecifier cSpecifier) { cDecl.unbridged()->setSpecifier(unbridge(cSpecifier)); } -void BridgedParamDecl_setImplicit(BridgedParamDecl cDecl) { +void BridgedDecl_setImplicit(BridgedDecl cDecl) { cDecl.unbridged()->setImplicit(); } +void BridgedGenericContext_setGenericSignature( + BridgedGenericContext cDecl, BridgedGenericSignature cGenSig) { + cDecl.unbridged()->setGenericSignature(cGenSig.unbridged()); +} + +//===----------------------------------------------------------------------===// +// MARK: BridgedNominalTypeDecl +//===----------------------------------------------------------------------===// + +void BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl, + BridgedDecl member) { + cDecl.unbridged()->addMember(member.unbridged()); +} + //===----------------------------------------------------------------------===// // MARK: BridgedSubscriptDecl //===----------------------------------------------------------------------===// @@ -618,6 +670,32 @@ BridgedConformance BridgedASTType::checkConformance(BridgedDeclObj proto) const return swift::checkConformance(unbridged(), proto.getAs(), /*allowMissing=*/ false); } +BridgedASTType BridgedASTType::mapTypeOutOfContext() const { + return {unbridged()->mapTypeOutOfContext().getPointer()}; +} + +BridgedCanType +BridgedASTType::getReducedType(BridgedGenericSignature sig) const { + return {unbridged()->getReducedType(sig.unbridged())}; +} + +swift::Identifier BridgedASTType::GenericTypeParam_getName() const { + return llvm::cast(type)->getName(); +} + +SwiftUInt BridgedASTType::GenericTypeParam_getDepth() const { + return llvm::cast(type)->getDepth(); +} + +SwiftUInt BridgedASTType::GenericTypeParam_getIndex() const { + return llvm::cast(type)->getIndex(); +} + +swift::GenericTypeParamKind +BridgedASTType::GenericTypeParam_getParamKind() const { + return llvm::cast(type)->getParamKind(); +} + static_assert((int)BridgedASTType::TraitResult::IsNot == (int)swift::TypeTraitResult::IsNot); static_assert((int)BridgedASTType::TraitResult::CanBe == (int)swift::TypeTraitResult::CanBe); static_assert((int)BridgedASTType::TraitResult::Is == (int)swift::TypeTraitResult::Is); @@ -644,6 +722,13 @@ BridgedASTType BridgedCanType::getRawType() const { return {type}; } +BridgedCanGenericSignature +BridgedCanType::SILFunctionType_getSubstGenericSignature() const { + return {swift::CanSILFunctionType(llvm::cast(type)) + ->getSubstGenericSignature() + .getPointer()}; +} + //===----------------------------------------------------------------------===// // MARK: BridgedASTTypeArray //===----------------------------------------------------------------------===// @@ -835,6 +920,20 @@ BridgedASTType BridgedGenericSignature::mapTypeIntoContext(BridgedASTType type) return {unbridged().getGenericEnvironment()->mapTypeIntoContext(type.unbridged()).getPointer()}; } +BridgedCanGenericSignature +BridgedGenericSignature::getCanonicalSignature() const { + return BridgedCanGenericSignature{impl}; +} + +swift::CanGenericSignature BridgedCanGenericSignature::unbridged() const { + return swift::GenericSignature(impl).getCanonicalSignature(); +} + +BridgedGenericSignature +BridgedCanGenericSignature::getGenericSignature() const { + return BridgedGenericSignature{impl}; +} + //===----------------------------------------------------------------------===// // MARK: BridgedFingerprint //===----------------------------------------------------------------------===// diff --git a/include/swift/AST/ASTBridgingWrappers.def b/include/swift/AST/ASTBridgingWrappers.def index 19af5d314740c..619f70fb033ed 100644 --- a/include/swift/AST/ASTBridgingWrappers.def +++ b/include/swift/AST/ASTBridgingWrappers.def @@ -95,7 +95,9 @@ // optional parameters. AST_BRIDGING_WRAPPER_NULLABLE(Decl) AST_BRIDGING_WRAPPER_NONNULL(DeclContext) -AST_BRIDGING_WRAPPER_NONNULL(SourceFile) +AST_BRIDGING_WRAPPER_NONNULL(GenericContext) +AST_BRIDGING_WRAPPER_NONNULL(FileUnit) +AST_BRIDGING_WRAPPER_NULLABLE(SourceFile) AST_BRIDGING_WRAPPER_NULLABLE(Stmt) AST_BRIDGING_WRAPPER_NULLABLE(Expr) AST_BRIDGING_WRAPPER_NULLABLE(Pattern) diff --git a/include/swift/SIL/SILBridging.h b/include/swift/SIL/SILBridging.h index 4384c1132fcff..77a2a7613d13e 100644 --- a/include/swift/SIL/SILBridging.h +++ b/include/swift/SIL/SILBridging.h @@ -251,6 +251,7 @@ BridgedYieldInfoArray SILFunctionType_getYields(BridgedCanType); SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLifetimeDependenceInfoArray SILFunctionType_getLifetimeDependencies(BridgedCanType); +enum class BridgedValueCategory { Address, Object }; struct BridgedType { void * _Nullable opaqueValue; @@ -270,6 +271,7 @@ struct BridgedType { BRIDGED_INLINE BridgedOwnedString getDebugDescription() const; BRIDGED_INLINE bool isNull() const; BRIDGED_INLINE bool isAddress() const; + BRIDGED_INLINE BridgedValueCategory getCategory() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getAddressType() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getObjectType() const; BRIDGED_INLINE bool isTrivial(BridgedFunction f) const; @@ -283,6 +285,8 @@ struct BridgedType { BRIDGED_INLINE bool isExactSuperclassOf(BridgedType t) const; BRIDGED_INLINE bool isMarkedAsImmortal() const; BRIDGED_INLINE bool isAddressableForDeps(BridgedFunction f) const; + SWIFT_IMPORT_UNSAFE bool + isAutodiffBranchTracingEnumInVJP(BridgedFunction vjp) const; BRIDGED_INLINE SWIFT_IMPORT_UNSAFE BridgedASTType getRawLayoutSubstitutedLikeType() const; BRIDGED_INLINE SWIFT_IMPORT_UNSAFE BridgedASTType getRawLayoutSubstitutedCountType() const; BRIDGED_INLINE SwiftInt getCaseIdxOfEnumType(BridgedStringRef name) const; @@ -297,13 +301,24 @@ struct BridgedType { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE EnumElementIterator getFirstEnumCaseIterator() const; BRIDGED_INLINE bool isEndCaseIterator(EnumElementIterator i) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getEnumCasePayload(EnumElementIterator i, BridgedFunction f) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType + getEnumCasePayload(SwiftInt caseIndex, BridgedFunction f) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj + getEnumElementDecl(EnumElementIterator i) const; BRIDGED_INLINE SwiftInt getNumTupleElements() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getTupleElementType(SwiftInt idx) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::Identifier + getTupleElementLabel(SwiftInt idx) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getFunctionTypeWithNoEscape(bool withNoEscape) const; BRIDGED_INLINE BridgedArgumentConvention getCalleeConvention() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType mapTypeOutOfContext() const; }; +SWIFT_NAME("BridgedType.getPrimitiveType(canType:silValueCategory:)") +BRIDGED_INLINE BridgedType BridgedType_getPrimitiveType( + BridgedCanType canType, BridgedValueCategory silValueCategory); + // SIL Bridging struct BridgedValue { @@ -508,6 +523,9 @@ struct BridgedFunction { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const; BridgedOwnedString getDebugDescription() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getLocation() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableSourceFile + getSourceFile() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArrayRef getFilesForModule() const; BRIDGED_INLINE bool isAccessor() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getAccessorName() const; BRIDGED_INLINE bool hasOwnership() const; @@ -517,6 +535,8 @@ struct BridgedFunction { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getForwardingSubstitutionMap() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeIntoContext(BridgedASTType ty) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType + mapTypeIntoContext(BridgedType ty) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedBasicBlock getFirstBlock() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedBasicBlock getLastBlock() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclRef getDeclRef() const; @@ -553,6 +573,8 @@ struct BridgedFunction { BRIDGED_INLINE void setIsPerformanceConstraint(bool isPerfConstraint) const; BRIDGED_INLINE bool isResilientNominalDecl(BridgedDeclObj decl) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getLoweredType(BridgedASTType type, bool maximallyAbstracted) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType + getLoweredType(BridgedCanType type) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getLoweredType(BridgedType type) const; BRIDGED_INLINE BridgedLinkage getLinkage() const; BRIDGED_INLINE void setLinkage(BridgedLinkage linkage) const; @@ -810,6 +832,8 @@ struct BridgedInstruction { BRIDGED_INLINE void RefElementAddrInst_setImmutable(bool isImmutable) const; BRIDGED_INLINE bool RefTailAddrInst_isImmutable() const; BRIDGED_INLINE SwiftInt PartialApplyInst_numArguments() const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap + PartialApplyInst_getSubstitutionMap() const; BRIDGED_INLINE SwiftInt ApplyInst_numArguments() const; BRIDGED_INLINE bool ApplyInst_getNonThrowing() const; BRIDGED_INLINE bool ApplyInst_getNonAsync() const; @@ -842,6 +866,8 @@ struct BridgedInstruction { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock BranchInst_getTargetBlock() const; BRIDGED_INLINE SwiftInt SwitchEnumInst_getNumCases() const; BRIDGED_INLINE SwiftInt SwitchEnumInst_getCaseIndex(SwiftInt idx) const; + BRIDGED_INLINE OptionalBridgedBasicBlock + SwitchEnumInst_getSuccessorForDefault() const; BRIDGED_INLINE SwiftInt StoreInst_getStoreOwnership() const; BRIDGED_INLINE SwiftInt AssignInst_getAssignOwnership() const; BRIDGED_INLINE MarkDependenceKind MarkDependenceInst_dependenceKind() const; @@ -956,6 +982,8 @@ struct BridgedArgument { BRIDGED_INLINE void setReborrow(bool reborrow) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getDecl() const; BRIDGED_INLINE void copyFlags(BridgedArgument fromArgument) const; + BRIDGED_INLINE BridgedValue::Ownership getOwnership() const; + BRIDGED_INLINE void replaceAllUsesWith(BridgedArgument arg) const; }; struct OptionalBridgedArgument { @@ -988,6 +1016,9 @@ struct BridgedBasicBlock { BRIDGED_INLINE SwiftInt getNumArguments() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument getArgument(SwiftInt index) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument addBlockArgument(BridgedType type, BridgedValue::Ownership ownership) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument + insertPhiArgument(SwiftInt index, BridgedType type, + BridgedValue::Ownership ownership) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument addFunctionArgument(BridgedType type) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedArgument insertFunctionArgument(SwiftInt atPosition, BridgedType type, BridgedValue::Ownership ownership, @@ -1337,6 +1368,8 @@ struct BridgedBuilder{ SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createDestructureStruct(BridgedValue str) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createTuple(BridgedType type, BridgedValueArray elements) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction + createTuple(BridgedValueArray elements) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createTupleExtract(BridgedValue str, SwiftInt elementIndex) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createTupleElementAddr(BridgedValue addr, @@ -1451,6 +1484,8 @@ struct BridgedContext { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap(BridgedType type) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getBuiltinIntegerType(SwiftInt bitWidth) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getTupleType(BridgedArrayRef elementTypes) const; + SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getTupleTypeWithLabels( + BridgedArrayRef elementTypes, BridgedArrayRef labels) const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getSwiftArrayDecl() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getSwiftMutableSpanDecl() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue getSILUndef(BridgedType type) const; diff --git a/include/swift/SIL/SILBridgingImpl.h b/include/swift/SIL/SILBridgingImpl.h index e5c031d7fbf45..69d4a3bf71a0d 100644 --- a/include/swift/SIL/SILBridgingImpl.h +++ b/include/swift/SIL/SILBridgingImpl.h @@ -321,6 +321,19 @@ bool BridgedType::isAddress() const { return unbridged().isAddress(); } +static inline BridgedValueCategory bridge(swift::SILValueCategory category) { + switch (category) { + case swift::SILValueCategory::Address: + return BridgedValueCategory::Address; + case swift::SILValueCategory::Object: + return BridgedValueCategory::Object; + } +} + +BridgedValueCategory BridgedType::getCategory() const { + return bridge(unbridged().getCategory()); +} + BridgedCanType BridgedType::getCanType() const { return unbridged().getASTType(); } @@ -437,6 +450,19 @@ BridgedType BridgedType::getEnumCasePayload(EnumElementIterator i, BridgedFuncti return swift::SILType(); } +BridgedDeclObj BridgedType::getEnumElementDecl(EnumElementIterator i) const { + swift::EnumElementDecl *elt = *unbridge(i); + return {elt}; +} + +BridgedType BridgedType::getEnumCasePayload(SwiftInt caseIndex, + BridgedFunction f) const { + swift::EnumElementDecl *elt = unbridged().getEnumElement(caseIndex); + if (elt->hasAssociatedValues()) + return unbridged().getEnumElementType(elt, f.getFunction()); + return swift::SILType(); +} + SwiftInt BridgedType::getNumTupleElements() const { return unbridged().getNumTupleElements(); } @@ -445,6 +471,12 @@ BridgedType BridgedType::getTupleElementType(SwiftInt idx) const { return unbridged().getTupleElementType(idx); } +swift::Identifier BridgedType::getTupleElementLabel(SwiftInt idx) const { + return llvm::cast(unbridged().getASTType().getPointer()) + ->getElement(idx) + .getName(); +} + BridgedType BridgedType::getFunctionTypeWithNoEscape(bool withNoEscape) const { auto fnType = unbridged().getAs(); auto newTy = fnType->getWithExtInfo(fnType->getExtInfo().withNoEscape(true)); @@ -456,6 +488,26 @@ BridgedArgumentConvention BridgedType::getCalleeConvention() const { return getArgumentConvention(fnType->getCalleeConvention()); } +BridgedType BridgedType::mapTypeOutOfContext() const { + return unbridged().mapTypeOutOfContext(); +} + +static inline swift::SILValueCategory unbridge(BridgedValueCategory category) { + switch (category) { + case BridgedValueCategory::Address: + return swift::SILValueCategory::Address; + case BridgedValueCategory::Object: + return swift::SILValueCategory::Object; + } +} + +BridgedType +BridgedType_getPrimitiveType(BridgedCanType canType, + BridgedValueCategory silValueCategory) { + return swift::SILType::getPrimitiveType(canType.unbridged(), + unbridge(silValueCategory)); +} + //===----------------------------------------------------------------------===// // BridgedValue //===----------------------------------------------------------------------===// @@ -611,6 +663,15 @@ void BridgedArgument::copyFlags(BridgedArgument fromArgument) const { fArg->copyFlags(static_cast(fromArgument.getArgument())); } +BridgedValue::Ownership BridgedArgument::getOwnership() const { + swift::ValueBase *val = getArgument(); + return BridgedValue{SwiftObject{val}}.getOwnership(); +} + +void BridgedArgument::replaceAllUsesWith(BridgedArgument arg) const { + getArgument()->replaceAllUsesWith(arg.getArgument()); +} + swift::SILArgument * _Nullable OptionalBridgedArgument::unbridged() const { if (!obj) return nullptr; @@ -689,6 +750,14 @@ BridgedLocation BridgedFunction::getLocation() const { return {swift::SILDebugLocation(getFunction()->getLocation(), getFunction()->getDebugScope())}; } +BridgedNullableSourceFile BridgedFunction::getSourceFile() const { + return {getFunction()->getSourceFile()}; +} + +BridgedArrayRef BridgedFunction::getFilesForModule() const { + return getFunction()->getModule().getSwiftModule()->getFiles(); +} + bool BridgedFunction::isAccessor() const { if (auto *valDecl = getFunction()->getDeclRef().getDecl()) { return llvm::isa(valDecl); @@ -725,6 +794,10 @@ BridgedASTType BridgedFunction::mapTypeIntoContext(BridgedASTType ty) const { return {getFunction()->mapTypeIntoContext(ty.unbridged()).getPointer()}; } +BridgedType BridgedFunction::mapTypeIntoContext(BridgedType ty) const { + return {getFunction()->mapTypeIntoContext(ty.unbridged())}; +} + OptionalBridgedBasicBlock BridgedFunction::getFirstBlock() const { return {getFunction()->empty() ? nullptr : getFunction()->getEntryBlock()}; } @@ -904,6 +977,14 @@ BridgedType BridgedFunction::getLoweredType(BridgedASTType type, bool maximallyA return BridgedType(getFunction()->getLoweredType(type.type)); } +BridgedType BridgedFunction::getLoweredType(BridgedCanType type) const { + swift::Lowering::AbstractionPattern pattern( + getFunction()->getLoweredFunctionType()->getSubstGenericSignature(), + type.unbridged()); + return getFunction()->getModule().Types.getLoweredType( + pattern, type.unbridged(), swift::TypeExpansionContext::minimal()); +} + BridgedType BridgedFunction::getLoweredType(BridgedType type) const { return BridgedType(getFunction()->getLoweredType(type.unbridged())); } @@ -1079,6 +1160,12 @@ bool BridgedInstruction::isIdenticalTo(BridgedInstruction inst) const { return unbridged()->isIdenticalTo(inst.unbridged()); } +BridgedSubstitutionMap +BridgedInstruction::PartialApplyInst_getSubstitutionMap() const { + auto *pai = llvm::cast(unbridged()); + return {pai->getSubstitutionMap()}; +} + SwiftInt BridgedInstruction::MultipleValueInstruction_getNumResults() const { return getAs()->getNumResults(); } @@ -1455,6 +1542,14 @@ SwiftInt BridgedInstruction::SwitchEnumInst_getCaseIndex(SwiftInt idx) const { return seInst->getModule().getCaseIndex(seInst->getCase(idx).first); } +OptionalBridgedBasicBlock +BridgedInstruction::SwitchEnumInst_getSuccessorForDefault() const { + auto *seInst = getAs(); + if (seInst->hasDefault()) + return {seInst->getDefaultBB()}; + return {nullptr}; +} + SwiftInt BridgedInstruction::StoreInst_getStoreOwnership() const { return (SwiftInt)getAs()->getOwnershipQualifier(); } @@ -1873,6 +1968,13 @@ BridgedArgument BridgedBasicBlock::addBlockArgument(BridgedType type, BridgedVal type.unbridged(), BridgedValue::unbridge(ownership))}; } +BridgedArgument +BridgedBasicBlock::insertPhiArgument(SwiftInt index, BridgedType type, + BridgedValue::Ownership ownership) const { + return {unbridged()->insertPhiArgument(index, type.unbridged(), + BridgedValue::unbridge(ownership))}; +} + BridgedArgument BridgedBasicBlock::addFunctionArgument(BridgedType type) const { return {unbridged()->createFunctionArgument(type.unbridged())}; } @@ -2629,6 +2731,23 @@ BridgedInstruction BridgedBuilder::createTuple(BridgedType type, BridgedValueArr elements.getValues(elementValues))}; } +BridgedInstruction +BridgedBuilder::createTuple(BridgedValueArray elements) const { + llvm::SmallVector elementValues; + llvm::ArrayRef values = elements.getValues(elementValues); + llvm::SmallVector tupleTyElts; + tupleTyElts.reserve(values.size()); + for (const swift::SILValue &value : values) { + tupleTyElts.emplace_back(value->getType().getASTType()); + } + swift::Type tupleTy = + swift::TupleType::get(tupleTyElts, unbridged().getASTContext()); + swift::SILType silTupleTy = + swift::SILType::getPrimitiveObjectType(tupleTy->getCanonicalType()); + + return {unbridged().createTuple(regularLoc(), silTupleTy, values)}; +} + BridgedInstruction BridgedBuilder::createTupleExtract(BridgedValue str, SwiftInt elementIndex) const { swift::SILValue v = str.getSILValue(); return {unbridged().createTupleExtract(regularLoc(), v, elementIndex)}; @@ -2893,6 +3012,24 @@ BridgedASTType BridgedContext::getTupleType(BridgedArrayRef elementTypes) const return {swift::TupleType::get(elements, context->getModule()->getASTContext())}; } +BridgedASTType +BridgedContext::getTupleTypeWithLabels(BridgedArrayRef elementTypes, + BridgedArrayRef labels) const { + assert(elementTypes.getCount() == labels.getCount()); + llvm::SmallVector elements; + elements.reserve(elementTypes.getCount()); + llvm::ArrayRef elementTypesArr = + elementTypes.unbridged(); + llvm::ArrayRef labelsArr = + labels.unbridged(); + for (const auto &[bridgedElmtTy, label] : + llvm::zip_equal(elementTypesArr, labelsArr)) { + elements.emplace_back(bridgedElmtTy.unbridged(), label); + } + return { + swift::TupleType::get(elements, context->getModule()->getASTContext())}; +} + BridgedDeclObj BridgedContext::getSwiftArrayDecl() const { return {context->getModule()->getASTContext().getArrayDecl()}; } diff --git a/lib/AST/Bridging/DeclBridging.cpp b/lib/AST/Bridging/DeclBridging.cpp index d826d6d4c9536..e7f3bc4235ccf 100644 --- a/lib/AST/Bridging/DeclBridging.cpp +++ b/lib/AST/Bridging/DeclBridging.cpp @@ -90,6 +90,25 @@ BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(BridgedASTContext cContext, #define ABSTRACT_DECL(Id, Parent) DECL(Id, Parent) #include "swift/AST/DeclNodes.def" +// Define `.asValueDecl` on each BridgedXXXDecl type that's also a +// ValueDecl. +#define DECL(Id, Parent) +#define VALUE_DECL(Id, Parent) \ + BridgedValueDecl Bridged##Id##Decl_asValueDecl(Bridged##Id##Decl decl) { \ + return static_cast(decl.unbridged()); \ + } +#include "swift/AST/DeclNodes.def" + +// Define `.asNominalTypeDecl` on each BridgedXXXDecl type that's also a +// NominalTypeDecl. +#define DECL(Id, Parent) +#define NOMINAL_TYPE_DECL(Id, Parent) \ + BridgedNominalTypeDecl Bridged##Id##Decl_asNominalTypeDecl( \ + Bridged##Id##Decl decl) { \ + return static_cast(decl.unbridged()); \ + } +#include "swift/AST/DeclNodes.def" + // Define `.asDeclContext` on each BridgedXXXDecl type that's also a // DeclContext. #define DECL(Id, Parent) @@ -100,6 +119,17 @@ BridgedDeclNameLoc BridgedDeclNameLoc_createParsed(BridgedASTContext cContext, #define ABSTRACT_CONTEXT_DECL(Id, Parent) CONTEXT_DECL(Id, Parent) #include "swift/AST/DeclNodes.def" +// Define `.asGenericContext` on each BridgedXXXDecl type that's also a +// GenericContext. +#define DECL(Id, Parent) +#define GENERIC_DECL(Id, Parent) \ + BridgedGenericContext Bridged##Id##Decl_asGenericContext( \ + Bridged##Id##Decl decl) { \ + return static_cast(decl.unbridged()); \ + } +#define ITERABLE_GENERIC_DECL(Id, Parent) GENERIC_DECL(Id, Parent) +#include "swift/AST/DeclNodes.def" + static StaticSpellingKind unbridged(BridgedStaticSpelling kind) { return static_cast(kind); } @@ -117,6 +147,15 @@ void BridgedDecl_forEachDeclToHoist(BridgedDecl cDecl, }); } +BridgedDeclContext BridgedDecl_getDeclContext(BridgedDecl decl) { + return decl.unbridged()->getDeclContext(); +} + +void BridgedValueDecl_setAccess(BridgedValueDecl decl, + swift::AccessLevel accessLevel) { + decl.unbridged()->setAccess(accessLevel); +} + BridgedAccessorDecl BridgedAccessorDecl_createParsed( BridgedASTContext cContext, BridgedDeclContext cDeclContext, swift::AccessorKind Kind, BridgedAbstractStorageDecl cStorage, @@ -334,7 +373,7 @@ convertToInheritedEntries(ASTContext &ctx, BridgedArrayRef cInheritedTypes) { [](auto &e) { return InheritedEntry(e.unbridged()); }); } -BridgedNominalTypeDecl BridgedEnumDecl_createParsed( +BridgedEnumDecl BridgedEnumDecl_createParsed( BridgedASTContext cContext, BridgedDeclContext cDeclContext, SourceLoc enumKeywordLoc, swift::Identifier name, SourceLoc nameLoc, BridgedNullableGenericParamList genericParamList, @@ -343,7 +382,7 @@ BridgedNominalTypeDecl BridgedEnumDecl_createParsed( SourceRange braceRange) { ASTContext &context = cContext.unbridged(); - NominalTypeDecl *decl = new (context) + auto *decl = new (context) EnumDecl(enumKeywordLoc, name, nameLoc, convertToInheritedEntries(context, cInheritedTypes), genericParamList.unbridged(), cDeclContext.unbridged()); diff --git a/lib/AST/Bridging/GenericsBridging.cpp b/lib/AST/Bridging/GenericsBridging.cpp index 15766836fd79e..92cd54a67ca86 100644 --- a/lib/AST/Bridging/GenericsBridging.cpp +++ b/lib/AST/Bridging/GenericsBridging.cpp @@ -59,6 +59,15 @@ BridgedGenericTypeParamDecl BridgedGenericTypeParamDecl_createParsed( return decl; } +BridgedGenericTypeParamDecl BridgedGenericTypeParamDecl_createImplicit( + BridgedDeclContext cDeclContext, swift::Identifier name, SwiftUInt depth, + SwiftUInt index, swift::GenericTypeParamKind paramKind) { + auto *param = GenericTypeParamDecl::createImplicit( + cDeclContext.unbridged(), name, depth, index, paramKind); + param->setDeclContext(cDeclContext.unbridged()); + return param; +} + BridgedTrailingWhereClause BridgedTrailingWhereClause_createParsed(BridgedASTContext cContext, SourceLoc whereKeywordLoc, diff --git a/lib/ASTGen/Sources/ASTGen/Decls.swift b/lib/ASTGen/Sources/ASTGen/Decls.swift index 900eb1ab440be..9d7dad619e76c 100644 --- a/lib/ASTGen/Sources/ASTGen/Decls.swift +++ b/lib/ASTGen/Sources/ASTGen/Decls.swift @@ -109,7 +109,7 @@ extension ASTGenVisitor { return decl } - func generate(enumDecl node: EnumDeclSyntax) -> BridgedNominalTypeDecl? { + func generate(enumDecl node: EnumDeclSyntax) -> BridgedEnumDecl? { let attrs = self.generateDeclAttributes(node, allowStatic: false) guard let (name, nameLoc) = self.generateIdentifierDeclNameAndLoc(node.name) else { return nil @@ -135,7 +135,7 @@ extension ASTGenVisitor { self.generate(memberBlockItemList: node.memberBlock.members) } let fp = self.generateFingerprint(declGroup: node) - decl.setParsedMembers( + decl.asNominalTypeDecl.setParsedMembers( members.lazy.bridgedArray(in: self), fingerprint: fp.bridged ) diff --git a/lib/ASTGen/Sources/ASTGen/Exprs.swift b/lib/ASTGen/Sources/ASTGen/Exprs.swift index c30c23e06fc50..0537dc2443138 100644 --- a/lib/ASTGen/Sources/ASTGen/Exprs.swift +++ b/lib/ASTGen/Sources/ASTGen/Exprs.swift @@ -370,7 +370,7 @@ extension ASTGenVisitor { defaultValueInitContext: nil ) param.setSpecifier(.default) - param.setImplicit() + param.asDecl.setImplicit() params.append(param) } }