diff --git a/include/swift/SIL/SILBridging.h b/include/swift/SIL/SILBridging.h index f8be3a9845796..c7e778ecee55a 100644 --- a/include/swift/SIL/SILBridging.h +++ b/include/swift/SIL/SILBridging.h @@ -47,6 +47,7 @@ namespace swift { class ValueBase; class Operand; class ForwardingInstruction; +class SILType; class SILFunction; class SILBasicBlock; class SILSuccessor; @@ -58,13 +59,19 @@ class SILVTableEntry; class SILVTable; class SILWitnessTable; class SILDefaultWitnessTable; +class SILDebugLocation; class NominalTypeDecl; class VarDecl; +class Type; class TypeBase; +class SubstitutionMap; class SwiftPassInvocation; class GenericSpecializationInformation; class LifetimeDependenceInfo; class IndexSubset; +enum class ResultConvention : uint8_t; +class SILResultInfo; +class SILParameterInfo; } bool swiftModulesInitialized(); @@ -83,58 +90,18 @@ struct BridgedResultInfo { swift::TypeBase * _Nonnull type; BridgedResultConvention convention; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedResultInfo() {} - -#ifdef USED_IN_CPP_SOURCE - inline static BridgedResultConvention - castToResultConvention(swift::ResultConvention convention) { - return static_cast(convention); - } - - BridgedResultInfo(swift::SILResultInfo resultInfo): - type(resultInfo.getInterfaceType().getPointer()), - convention(castToResultConvention(resultInfo.getConvention())) - {} -#endif + BRIDGED_INLINE static BridgedResultConvention castToResultConvention(swift::ResultConvention convention); + BRIDGED_INLINE BridgedResultInfo(swift::SILResultInfo resultInfo); }; struct OptionalBridgedResultInfo { - swift::TypeBase * _Nullable type = nullptr; - BridgedResultConvention convention = BridgedResultConvention::Indirect; - - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - OptionalBridgedResultInfo() {} - -#ifdef USED_IN_CPP_SOURCE - OptionalBridgedResultInfo(std::optional resultInfo) { - if (resultInfo) { - type = resultInfo->getInterfaceType().getPointer(); - convention = - BridgedResultInfo::castToResultConvention(resultInfo->getConvention()); - } - } -#endif + swift::TypeBase * _Nullable type; + BridgedResultConvention convention; }; struct BridgedResultInfoArray { BridgedArrayRef resultInfoArray; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedResultInfoArray() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedResultInfoArray(llvm::ArrayRef results) - : resultInfoArray(results) {} - - llvm::ArrayRef unbridged() const { - return resultInfoArray.unbridged(); - } -#endif - BRIDGED_INLINE SwiftInt count() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE @@ -160,44 +127,6 @@ enum class BridgedArgumentConvention { Pack_Out }; -#ifdef USED_IN_CPP_SOURCE -inline swift::ParameterConvention getParameterConvention(BridgedArgumentConvention convention) { - switch (convention) { - case BridgedArgumentConvention::Indirect_In: return swift::ParameterConvention::Indirect_In; - case BridgedArgumentConvention::Indirect_In_Guaranteed: return swift::ParameterConvention::Indirect_In_Guaranteed; - case BridgedArgumentConvention::Indirect_Inout: return swift::ParameterConvention::Indirect_Inout; - case BridgedArgumentConvention::Indirect_InoutAliasable: return swift::ParameterConvention::Indirect_InoutAliasable; - case BridgedArgumentConvention::Indirect_In_CXX: return swift::ParameterConvention::Indirect_In_CXX; - case BridgedArgumentConvention::Indirect_Out: break; - case BridgedArgumentConvention::Direct_Owned: return swift::ParameterConvention::Direct_Owned; - case BridgedArgumentConvention::Direct_Unowned: return swift::ParameterConvention::Direct_Unowned; - case BridgedArgumentConvention::Direct_Guaranteed: return swift::ParameterConvention::Direct_Guaranteed; - case BridgedArgumentConvention::Pack_Owned: return swift::ParameterConvention::Pack_Owned; - case BridgedArgumentConvention::Pack_Inout: return swift::ParameterConvention::Pack_Inout; - case BridgedArgumentConvention::Pack_Guaranteed: return swift::ParameterConvention::Pack_Guaranteed; - case BridgedArgumentConvention::Pack_Out: break; - } - llvm_unreachable("invalid parameter convention"); -} - -inline BridgedArgumentConvention getArgumentConvention(swift::ParameterConvention convention) { - switch (convention) { - case swift::ParameterConvention::Indirect_In: return BridgedArgumentConvention::Indirect_In; - case swift::ParameterConvention::Indirect_In_Guaranteed: return BridgedArgumentConvention::Indirect_In_Guaranteed; - case swift::ParameterConvention::Indirect_Inout: return BridgedArgumentConvention::Indirect_Inout; - case swift::ParameterConvention::Indirect_InoutAliasable: return BridgedArgumentConvention::Indirect_InoutAliasable; - case swift::ParameterConvention::Indirect_In_CXX: return BridgedArgumentConvention::Indirect_In_CXX; - case swift::ParameterConvention::Direct_Owned: return BridgedArgumentConvention::Direct_Owned; - case swift::ParameterConvention::Direct_Unowned: return BridgedArgumentConvention::Direct_Unowned; - case swift::ParameterConvention::Direct_Guaranteed: return BridgedArgumentConvention::Direct_Guaranteed; - case swift::ParameterConvention::Pack_Owned: return BridgedArgumentConvention::Pack_Owned; - case swift::ParameterConvention::Pack_Inout: return BridgedArgumentConvention::Pack_Inout; - case swift::ParameterConvention::Pack_Guaranteed: return BridgedArgumentConvention::Pack_Guaranteed; - } - llvm_unreachable("invalid parameter convention"); -} -#endif - struct BridgedParameterInfo { swift::TypeBase * _Nonnull type; BridgedArgumentConvention convention; @@ -206,42 +135,13 @@ struct BridgedParameterInfo { BridgedParameterInfo(swift::TypeBase * _Nonnull type, BridgedArgumentConvention convention, uint8_t options) : type(type), convention(convention), options(options) {} -#ifdef USED_IN_CPP_SOURCE - inline static BridgedArgumentConvention - castToArgumentConvention(swift::ParameterConvention convention) { - return static_cast( - swift::SILArgumentConvention(convention).Value); - } - - BridgedParameterInfo(swift::SILParameterInfo parameterInfo): - type(parameterInfo.getInterfaceType().getPointer()), - convention(castToArgumentConvention(parameterInfo.getConvention())), - options(parameterInfo.getOptions().toRaw()) - {} - - swift::SILParameterInfo unbridged() const { - return swift::SILParameterInfo(swift::CanType(type), getParameterConvention(convention), - swift::SILParameterInfo::Options(options)); - } -#endif + BRIDGED_INLINE BridgedParameterInfo(swift::SILParameterInfo parameterInfo); + BRIDGED_INLINE swift::SILParameterInfo unbridged() const; }; struct BridgedParameterInfoArray { BridgedArrayRef parameterInfoArray; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedParameterInfoArray() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedParameterInfoArray(llvm::ArrayRef parameters) - : parameterInfoArray(parameters) {} - - llvm::ArrayRef unbridged() const { - return parameterInfoArray.unbridged(); - } -#endif - BRIDGED_INLINE SwiftInt count() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE @@ -251,19 +151,6 @@ struct BridgedParameterInfoArray { struct BridgedYieldInfoArray { BridgedArrayRef yieldInfoArray; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedYieldInfoArray() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedYieldInfoArray(llvm::ArrayRef yields) - : yieldInfoArray(yields) {} - - llvm::ArrayRef unbridged() const { - return yieldInfoArray.unbridged(); - } -#endif - BRIDGED_INLINE SwiftInt count() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE @@ -275,16 +162,7 @@ struct BridgedLifetimeDependenceInfo { SwiftUInt targetIndex; bool immortal; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedLifetimeDependenceInfo() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedLifetimeDependenceInfo(swift::LifetimeDependenceInfo info) - : inheritLifetimeParamIndices(info.getInheritIndices()), - scopeLifetimeParamIndices(info.getScopeIndices()), - targetIndex(info.getTargetIndex()), immortal(info.isImmortal()) {} -#endif + BRIDGED_INLINE BridgedLifetimeDependenceInfo(swift::LifetimeDependenceInfo info); BRIDGED_INLINE bool empty() const; BRIDGED_INLINE bool checkInherit(SwiftInt index) const; @@ -297,21 +175,6 @@ struct BridgedLifetimeDependenceInfo { struct BridgedLifetimeDependenceInfoArray { BridgedArrayRef lifetimeDependenceInfoArray; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedLifetimeDependenceInfoArray() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedLifetimeDependenceInfoArray( - llvm::ArrayRef lifetimeDependenceInfo) - : lifetimeDependenceInfoArray(lifetimeDependenceInfo) {} - - llvm::ArrayRef unbridged() const { - return lifetimeDependenceInfoArray - .unbridged(); - } -#endif - BRIDGED_INLINE SwiftInt count() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLifetimeDependenceInfo @@ -335,11 +198,7 @@ enum class BridgedLinkage { struct BridgedASTType { swift::TypeBase * _Nullable type; -#ifdef USED_IN_CPP_SOURCE - swift::Type unbridged() const { - return type; - } -#endif + BRIDGED_INLINE swift::Type unbridged() const; BRIDGED_INLINE BridgedOwnedString getDebugDescription() const; @@ -418,17 +277,8 @@ struct BridgedType { SWIFT_IMPORT_UNSAFE BRIDGED_INLINE EnumElementIterator getNext() const; }; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedType() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedType(swift::SILType t) : opaqueValue(t.getOpaqueValue()) {} - - swift::SILType unbridged() const { - return swift::SILType::getFromOpaqueValue(opaqueValue); - } -#endif + BRIDGED_INLINE BridgedType(swift::SILType t); + BRIDGED_INLINE swift::SILType unbridged() const; BRIDGED_INLINE BridgedOwnedString getDebugDescription() const; BRIDGED_INLINE bool isNull() const; @@ -609,18 +459,8 @@ enum class BridgedMemoryBehavior { struct BridgedLocation { uint64_t storage[3]; - // Ensure that this struct value type will be indirectly returned on - // Windows ARM64 - BridgedLocation() {} - -#ifdef USED_IN_CPP_SOURCE - BridgedLocation(const swift::SILDebugLocation &loc) { - *reinterpret_cast(&storage) = loc; - } - const swift::SILDebugLocation &getLoc() const { - return *reinterpret_cast(&storage); - } -#endif + BRIDGED_INLINE BridgedLocation(const swift::SILDebugLocation &loc); + BRIDGED_INLINE const swift::SILDebugLocation &getLoc() const; BridgedOwnedString getDebugDescription() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getAutogeneratedLocation() const; @@ -790,12 +630,7 @@ struct OptionalBridgedGlobalVar { struct BridgedMultiValueResult { SwiftObject obj; -#ifdef USED_IN_CPP_SOURCE - swift::MultipleValueInstructionResult * _Nonnull unbridged() const { - return static_cast(obj); - } -#endif - + BRIDGED_INLINE swift::MultipleValueInstructionResult * _Nonnull unbridged() const; SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction getParent() const; BRIDGED_INLINE SwiftInt getIndex() const; }; @@ -803,15 +638,8 @@ struct BridgedMultiValueResult { struct BridgedSubstitutionMap { uint64_t storage[1]; -#ifdef USED_IN_CPP_SOURCE - BridgedSubstitutionMap(swift::SubstitutionMap map) { - *reinterpret_cast(&storage) = map; - } - swift::SubstitutionMap unbridged() const { - return *reinterpret_cast(&storage); - } -#endif - + BRIDGED_INLINE BridgedSubstitutionMap(swift::SubstitutionMap map); + BRIDGED_INLINE swift::SubstitutionMap unbridged() const; BRIDGED_INLINE BridgedSubstitutionMap(); BRIDGED_INLINE bool isEmpty() const; BRIDGED_INLINE bool hasAnySubstitutableParams() const; diff --git a/include/swift/SIL/SILBridgingImpl.h b/include/swift/SIL/SILBridgingImpl.h index b11410216ea23..b1c44db884828 100644 --- a/include/swift/SIL/SILBridgingImpl.h +++ b/include/swift/SIL/SILBridgingImpl.h @@ -46,12 +46,21 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS // BridgedResultInfo //===----------------------------------------------------------------------===// +BridgedResultConvention BridgedResultInfo::castToResultConvention(swift::ResultConvention convention) { + return static_cast(convention); +} + +BridgedResultInfo::BridgedResultInfo(swift::SILResultInfo resultInfo): + type(resultInfo.getInterfaceType().getPointer()), + convention(castToResultConvention(resultInfo.getConvention())) +{} + SwiftInt BridgedResultInfoArray::count() const { - return unbridged().size(); + return resultInfoArray.unbridged().size(); } BridgedResultInfo BridgedResultInfoArray::at(SwiftInt resultIndex) const { - return BridgedResultInfo(unbridged()[resultIndex]); + return BridgedResultInfo(resultInfoArray.unbridged()[resultIndex]); } //===----------------------------------------------------------------------===// @@ -59,36 +68,92 @@ BridgedResultInfo BridgedResultInfoArray::at(SwiftInt resultIndex) const { //===----------------------------------------------------------------------===// SwiftInt BridgedYieldInfoArray::count() const { - return unbridged().size(); + return yieldInfoArray.unbridged().size(); } BridgedParameterInfo BridgedYieldInfoArray::at(SwiftInt resultIndex) const { - return BridgedParameterInfo(unbridged()[resultIndex]); + return BridgedParameterInfo(yieldInfoArray.unbridged()[resultIndex]); } //===----------------------------------------------------------------------===// // BridgedParameterInfo //===----------------------------------------------------------------------===// +inline swift::ParameterConvention getParameterConvention(BridgedArgumentConvention convention) { + switch (convention) { + case BridgedArgumentConvention::Indirect_In: return swift::ParameterConvention::Indirect_In; + case BridgedArgumentConvention::Indirect_In_Guaranteed: return swift::ParameterConvention::Indirect_In_Guaranteed; + case BridgedArgumentConvention::Indirect_Inout: return swift::ParameterConvention::Indirect_Inout; + case BridgedArgumentConvention::Indirect_InoutAliasable: return swift::ParameterConvention::Indirect_InoutAliasable; + case BridgedArgumentConvention::Indirect_In_CXX: return swift::ParameterConvention::Indirect_In_CXX; + case BridgedArgumentConvention::Indirect_Out: break; + case BridgedArgumentConvention::Direct_Owned: return swift::ParameterConvention::Direct_Owned; + case BridgedArgumentConvention::Direct_Unowned: return swift::ParameterConvention::Direct_Unowned; + case BridgedArgumentConvention::Direct_Guaranteed: return swift::ParameterConvention::Direct_Guaranteed; + case BridgedArgumentConvention::Pack_Owned: return swift::ParameterConvention::Pack_Owned; + case BridgedArgumentConvention::Pack_Inout: return swift::ParameterConvention::Pack_Inout; + case BridgedArgumentConvention::Pack_Guaranteed: return swift::ParameterConvention::Pack_Guaranteed; + case BridgedArgumentConvention::Pack_Out: break; + } + llvm_unreachable("invalid parameter convention"); +} + +inline BridgedArgumentConvention getArgumentConvention(swift::ParameterConvention convention) { + switch (convention) { + case swift::ParameterConvention::Indirect_In: return BridgedArgumentConvention::Indirect_In; + case swift::ParameterConvention::Indirect_In_Guaranteed: return BridgedArgumentConvention::Indirect_In_Guaranteed; + case swift::ParameterConvention::Indirect_Inout: return BridgedArgumentConvention::Indirect_Inout; + case swift::ParameterConvention::Indirect_InoutAliasable: return BridgedArgumentConvention::Indirect_InoutAliasable; + case swift::ParameterConvention::Indirect_In_CXX: return BridgedArgumentConvention::Indirect_In_CXX; + case swift::ParameterConvention::Direct_Owned: return BridgedArgumentConvention::Direct_Owned; + case swift::ParameterConvention::Direct_Unowned: return BridgedArgumentConvention::Direct_Unowned; + case swift::ParameterConvention::Direct_Guaranteed: return BridgedArgumentConvention::Direct_Guaranteed; + case swift::ParameterConvention::Pack_Owned: return BridgedArgumentConvention::Pack_Owned; + case swift::ParameterConvention::Pack_Inout: return BridgedArgumentConvention::Pack_Inout; + case swift::ParameterConvention::Pack_Guaranteed: return BridgedArgumentConvention::Pack_Guaranteed; + } + llvm_unreachable("invalid parameter convention"); +} + +inline BridgedArgumentConvention castToArgumentConvention(swift::SILArgumentConvention convention) { + return static_cast(convention.Value); +} + +BridgedParameterInfo::BridgedParameterInfo(swift::SILParameterInfo parameterInfo): + type(parameterInfo.getInterfaceType().getPointer()), + convention(getArgumentConvention(parameterInfo.getConvention())), + options(parameterInfo.getOptions().toRaw()) +{} + +swift::SILParameterInfo BridgedParameterInfo::unbridged() const { + return swift::SILParameterInfo(swift::CanType(type), getParameterConvention(convention), + swift::SILParameterInfo::Options(options)); +} + SwiftInt BridgedParameterInfoArray::count() const { - return unbridged().size(); + return parameterInfoArray.unbridged().size(); } BridgedParameterInfo BridgedParameterInfoArray::at(SwiftInt parameterIndex) const { - return BridgedParameterInfo(unbridged()[parameterIndex]); + return BridgedParameterInfo(parameterInfoArray.unbridged()[parameterIndex]); } //===----------------------------------------------------------------------===// // BridgedLifetimeDependenceInfo //===----------------------------------------------------------------------===// +BridgedLifetimeDependenceInfo::BridgedLifetimeDependenceInfo(swift::LifetimeDependenceInfo info) + : inheritLifetimeParamIndices(info.getInheritIndices()), + scopeLifetimeParamIndices(info.getScopeIndices()), + targetIndex(info.getTargetIndex()), immortal(info.isImmortal()) {} + SwiftInt BridgedLifetimeDependenceInfoArray::count() const { - return unbridged().size(); + return lifetimeDependenceInfoArray.unbridged().size(); } BridgedLifetimeDependenceInfo BridgedLifetimeDependenceInfoArray::at(SwiftInt index) const { - return BridgedLifetimeDependenceInfo(unbridged()[index]); + return BridgedLifetimeDependenceInfo(lifetimeDependenceInfoArray.unbridged()[index]); } bool BridgedLifetimeDependenceInfo::empty() const { @@ -141,6 +206,10 @@ BridgedOwnedString BridgedLifetimeDependenceInfo::getDebugDescription() const { // BridgedASTType //===----------------------------------------------------------------------===// +swift::Type BridgedASTType::unbridged() const { + return type; +} + BridgedOwnedString BridgedASTType::getDebugDescription() const { return BridgedOwnedString(unbridged().getString()); } @@ -167,7 +236,7 @@ bool BridgedASTType::isInteger() const { BridgedResultInfoArray BridgedASTType::SILFunctionType_getResultsWithError() const { - return unbridged()->castTo()->getResultsWithError(); + return {unbridged()->castTo()->getResultsWithError()}; } SwiftInt BridgedASTType::SILFunctionType_getNumIndirectFormalResultsWithError() const { @@ -183,11 +252,17 @@ SwiftInt BridgedASTType::SILFunctionType_getNumPackResults() const { OptionalBridgedResultInfo BridgedASTType::SILFunctionType_getErrorResult() const { auto fnTy = unbridged()->castTo(); - return OptionalBridgedResultInfo(fnTy->getOptionalErrorResult()); + auto resultInfo = fnTy->getOptionalErrorResult(); + if (resultInfo) { + return {resultInfo->getInterfaceType().getPointer(), + BridgedResultInfo::castToResultConvention(resultInfo->getConvention())}; + } + return {nullptr, BridgedResultConvention::Indirect}; + } BridgedParameterInfoArray BridgedASTType::SILFunctionType_getParameters() const { - return unbridged()->castTo()->getParameters(); + return {unbridged()->castTo()->getParameters()}; } bool BridgedASTType::SILFunctionType_hasSelfParam() const { @@ -199,19 +274,25 @@ bool BridgedASTType::SILFunctionType_isTrivialNoescape() const { } BridgedYieldInfoArray BridgedASTType::SILFunctionType_getYields() const { - return unbridged()->castTo()->getYields(); + return {unbridged()->castTo()->getYields()}; } BridgedLifetimeDependenceInfoArray BridgedASTType::SILFunctionType_getLifetimeDependencies() const { auto fnTy = unbridged()->castTo(); - return fnTy->getLifetimeDependencies(); + return {fnTy->getLifetimeDependencies()}; } //===----------------------------------------------------------------------===// // BridgedType //===----------------------------------------------------------------------===// +BridgedType::BridgedType(swift::SILType t) : opaqueValue(t.getOpaqueValue()) {} + +swift::SILType BridgedType::unbridged() const { + return swift::SILType::getFromOpaqueValue(opaqueValue); +} + BridgedType::EnumElementIterator BridgedType::EnumElementIterator::getNext() const { return EnumElementIterator(std::next(unbridged())); } @@ -557,10 +638,6 @@ SwiftInt OptionalBridgedOperand::distanceTo(BridgedOperand element) const { retu // BridgedArgument //===----------------------------------------------------------------------===// -inline BridgedArgumentConvention castToArgumentConvention(swift::SILArgumentConvention convention) { - return static_cast(convention.Value); -} - swift::SILArgument * _Nonnull BridgedArgument::getArgument() const { return static_cast(obj); } @@ -586,6 +663,14 @@ void BridgedArgument::copyFlags(BridgedArgument fromArgument) const { // BridgedSubstitutionMap //===----------------------------------------------------------------------===// +BridgedSubstitutionMap::BridgedSubstitutionMap(swift::SubstitutionMap map) { + *reinterpret_cast(&storage) = map; +} + +swift::SubstitutionMap BridgedSubstitutionMap::unbridged() const { + return *reinterpret_cast(&storage); +} + BridgedSubstitutionMap::BridgedSubstitutionMap() : BridgedSubstitutionMap(swift::SubstitutionMap()) { } @@ -601,6 +686,12 @@ bool BridgedSubstitutionMap::hasAnySubstitutableParams() const { // BridgedLocation //===----------------------------------------------------------------------===// +BridgedLocation::BridgedLocation(const swift::SILDebugLocation &loc) { + *reinterpret_cast(&storage) = loc; +} +const swift::SILDebugLocation &BridgedLocation::getLoc() const { + return *reinterpret_cast(&storage); +} BridgedLocation BridgedLocation::getAutogeneratedLocation() const { return getLoc().getAutogeneratedLocation(); } @@ -850,6 +941,10 @@ OptionalBridgedInstruction BridgedGlobalVar::getFirstStaticInitInst() const { // BridgedMultiValueResult //===----------------------------------------------------------------------===// +swift::MultipleValueInstructionResult * _Nonnull BridgedMultiValueResult::unbridged() const { + return static_cast(obj); +} + BridgedInstruction BridgedMultiValueResult::getParent() const { return {unbridged()->getParent()}; }