Skip to content

Commit

Permalink
put keyword param names in backticks (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbrix authored Oct 18, 2023
1 parent 8dea786 commit a84ef91
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 147 deletions.
168 changes: 86 additions & 82 deletions swiftwinrt/code_writers.h

Large diffs are not rendered by default.

33 changes: 26 additions & 7 deletions swiftwinrt/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace swiftwinrt
}
}

private:
private:
void construct(metadata_type const* type)
{
// Check if this is a typedef_base, otherwise we could fail trying to get the swift_abi_namespace
Expand Down Expand Up @@ -437,7 +437,7 @@ namespace swiftwinrt
auto class_name = get_attribute_value<ElemSig::SystemType>(attribute, 0).name;
return type.get_cache().find_required(class_name);
}

inline TypeDef get_exclusive_to(typedef_base const& type)
{
return get_exclusive_to(type.type());
Expand Down Expand Up @@ -523,7 +523,7 @@ namespace swiftwinrt
return name;
}
}

inline std::string put_in_backticks_if_needed(std::string name) {
// any lowercase swift keywords neet to be put in backticks
static auto keyWords = std::set<std::string>{
Expand Down Expand Up @@ -582,7 +582,7 @@ namespace swiftwinrt
result[0] = tolower(result[0]);

// One or two leading capitals: GetFoo -> getFoo / UInt32 -> uint32
// 3+ leading capitals or mixed digits, keep the last one:
// 3+ leading capitals or mixed digits, keep the last one:
// UIElement -> uiElement / HELLOWorld -> helloWorld / R8G8B8Alpha -> r8g8b8Alpha
if (result.size() > 1 && isupper(result[1]) || isdigit(result[1])){
result[1] = tolower(result[1]);
Expand Down Expand Up @@ -639,12 +639,31 @@ namespace swiftwinrt
return to_camel_case(field.Name());
}

inline std::string_view get_swift_name(Param const& param)
inline std::string get_swift_name(Param const& param)
{
return put_in_backticks_if_needed(std::string(param.Name()));
}

inline std::string local_swift_param_name(std::string const& param_name)
{
std::string local_name = "_";
// if the param name starts with backticks
if (param_name.starts_with('`'))
{
local_name.append(param_name.substr(1, param_name.size() - 2));
}
else {
local_name.append(param_name);
}
return local_name;
}

inline std::string local_swift_param_name(std::string_view const& param_name)
{
return param.Name();
return local_swift_param_name(std::string(param_name));
}

inline std::string_view get_swift_name(function_param const& param)
inline std::string get_swift_name(function_param const& param)
{
return get_swift_name(param.def);
}
Expand Down
2 changes: 1 addition & 1 deletion swiftwinrt/type_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace swiftwinrt
std::string get_full_swift_type_name(writer const&, TypeDef const& type);
std::string get_full_swift_type_name(writer const&, const metadata_type* type);
std::string get_swift_module(std::string_view const& ns);
std::string_view get_swift_name(function_param const&);
std::string get_swift_name(function_param const&);

param_category get_category(const metadata_type*, TypeDef*);

Expand Down
48 changes: 48 additions & 0 deletions tests/test_component/Sources/CWinRT/include/test_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ typedef interface __x_ABI_Ctest__component_CIVoidToVoidDelegate __x_ABI_Ctest__c

#endif // ____x_ABI_Ctest__component_CInterfaceWithReturnDelegate_FWD_DEFINED__

#ifndef ____x_ABI_Ctest__component_CWithKeyword_FWD_DEFINED__
#define ____x_ABI_Ctest__component_CWithKeyword_FWD_DEFINED__
typedef interface __x_ABI_Ctest__component_CWithKeyword __x_ABI_Ctest__component_CWithKeyword;

#endif // ____x_ABI_Ctest__component_CWithKeyword_FWD_DEFINED__

// Parameterized interface forward declarations (C)

// Collection interface definitions
Expand Down Expand Up @@ -3666,3 +3672,45 @@ struct __x_ABI_Ctest__component_CStructWithEnum
EXTERN_C const IID IID___x_ABI_Ctest__component_CInterfaceWithReturnDelegate;
#endif /* !defined(____x_ABI_Ctest__component_CInterfaceWithReturnDelegate_INTERFACE_DEFINED__) */

#if !defined(____x_ABI_Ctest__component_CWithKeyword_INTERFACE_DEFINED__)
#define ____x_ABI_Ctest__component_CWithKeyword_INTERFACE_DEFINED__
typedef struct __x_ABI_Ctest__component_CWithKeywordVtbl
{
BEGIN_INTERFACE

HRESULT (STDMETHODCALLTYPE* QueryInterface)(__x_ABI_Ctest__component_CWithKeyword* This,
REFIID riid,
void** ppvObject);
ULONG (STDMETHODCALLTYPE* AddRef)(__x_ABI_Ctest__component_CWithKeyword* This);
ULONG (STDMETHODCALLTYPE* Release)(__x_ABI_Ctest__component_CWithKeyword* This);
HRESULT (STDMETHODCALLTYPE* GetIids)(__x_ABI_Ctest__component_CWithKeyword* This,
ULONG* iidCount,
IID** iids);
HRESULT (STDMETHODCALLTYPE* GetRuntimeClassName)(__x_ABI_Ctest__component_CWithKeyword* This,
HSTRING* className);
HRESULT (STDMETHODCALLTYPE* GetTrustLevel)(__x_ABI_Ctest__component_CWithKeyword* This,
TrustLevel* trustLevel);
HRESULT (STDMETHODCALLTYPE* Enum)(__x_ABI_Ctest__component_CWithKeyword* This,
HSTRING extension);
HRESULT (STDMETHODCALLTYPE* get_Struct)(__x_ABI_Ctest__component_CWithKeyword* This,
HSTRING* value);
HRESULT (STDMETHODCALLTYPE* put_Struct)(__x_ABI_Ctest__component_CWithKeyword* This,
HSTRING value);
HRESULT (STDMETHODCALLTYPE* add_Repeat)(__x_ABI_Ctest__component_CWithKeyword* This,
__x_ABI_C__FIEventHandler_1_IInspectable* handler,
EventRegistrationToken* token);
HRESULT (STDMETHODCALLTYPE* remove_Repeat)(__x_ABI_Ctest__component_CWithKeyword* This,
EventRegistrationToken token);

END_INTERFACE
} __x_ABI_Ctest__component_CWithKeywordVtbl;

interface __x_ABI_Ctest__component_CWithKeyword
{
CONST_VTBL struct __x_ABI_Ctest__component_CWithKeywordVtbl* lpVtbl;
};


EXTERN_C const IID IID___x_ABI_Ctest__component_CWithKeyword;
#endif /* !defined(____x_ABI_Ctest__component_CWithKeyword_INTERFACE_DEFINED__) */

Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public enum __IMPL_Windows_Foundation {
}

public var type: PropertyType { propertyType }
public var isNumericScalar: Bool {
public var isNumericScalar: Bool {
switch propertyType {
case .int16, .int32, .int64, .uint8, .uint16, .uint32, .uint64, .single, .double: return true
default: return false
Expand All @@ -225,13 +225,13 @@ public enum __IMPL_Windows_Foundation {
public func getBoolean() -> Bool { _value as! Bool }
public func getString() -> String { _value as! String }
public func getGuid() -> test_component.GUID { _value as! test_component.GUID }
public func getDateTime() -> DateTime { _value as! DateTime }
public func getDateTime() -> DateTime { _value as! DateTime }
public func getTimeSpan() -> TimeSpan { _value as! TimeSpan }
public func getPoint() -> Point { _value as! Point }
public func getSize() -> Size { _value as! Size }
public func getRect() -> Rect { _value as! Rect }

public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? {
public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? {
guard iid == __ABI_Windows_Foundation.IPropertyValueWrapper.IID else { return nil }
guard let thisAsIPropValue = __ABI_Windows_Foundation.IPropertyValueWrapper(self) else { fatalError("creating non-nil wrapper shouldn't fail") }
return thisAsIPropValue.queryInterface(iid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum __IMPL_Windows_Foundation_Collections {
return try! this.add_MapChangedImpl(abi)
},
remove: { [weak this = _IObservableMap] in
try? this?.remove_MapChangedImpl($0)
try? this?.remove_MapChangedImpl($0)
}
)
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IP
public func _getABI<T>() -> UnsafeMutablePointer<T>? {
if T.self == CABI.self {
return RawPointer(_default)
}
}
if T.self == C_IInspectable.self {
return RawPointer(_default)
}
Expand Down Expand Up @@ -53,7 +53,7 @@ public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IP
return try! this.add_MapChangedImpl(abi)
},
remove: { [weak this = _IObservableMap] in
try? this?.remove_MapChangedImpl($0)
try? this?.remove_MapChangedImpl($0)
}
)
}()
Expand Down Expand Up @@ -128,7 +128,7 @@ public final class StringMap : WinRTClass, IMap, IIterable, IObservableMap {
public func _getABI<T>() -> UnsafeMutablePointer<T>? {
if T.self == CABI.self {
return RawPointer(_default)
}
}
if T.self == C_IInspectable.self {
return RawPointer(_default)
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public final class StringMap : WinRTClass, IMap, IIterable, IObservableMap {
return try! this.add_MapChangedImpl(abi)
},
remove: { [weak this = _IObservableMap] in
try? this?.remove_MapChangedImpl($0)
try? this?.remove_MapChangedImpl($0)
}
)
}()
Expand All @@ -239,7 +239,7 @@ public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IProp
public func _getABI<T>() -> UnsafeMutablePointer<T>? {
if T.self == CABI.self {
return RawPointer(_default)
}
}
if T.self == C_IInspectable.self {
return RawPointer(_default)
}
Expand Down Expand Up @@ -277,7 +277,7 @@ public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IProp
return try! this.add_MapChangedImpl(abi)
},
remove: { [weak this = _IObservableMap] in
try? this?.remove_MapChangedImpl($0)
try? this?.remove_MapChangedImpl($0)
}
)
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class Deferral : WinRTClass, IClosable {
public func _getABI<T>() -> UnsafeMutablePointer<T>? {
if T.self == CABI.self {
return RawPointer(_default)
}
}
if T.self == C_IInspectable.self {
return RawPointer(_default)
}
Expand Down
137 changes: 137 additions & 0 deletions tests/test_component/Sources/test_component/test_component+ABI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ private var IID___x_ABI_Ctest__component_CInterfaceWithReturnDelegate: test_comp
.init(Data1: 0xB0EBC406, Data2: 0x17C0, Data3: 0x5703, Data4: ( 0xB9,0xC7,0x50,0xBE,0x67,0x5B,0xBC,0x95 ))// B0EBC406-17C0-5703-B9C7-50BE675BBC95
}

private var IID___x_ABI_Ctest__component_CWithKeyword: test_component.IID {
.init(Data1: 0x77E9FBAD, Data2: 0x3DCE, Data3: 0x5E50, Data4: ( 0xB4,0x39,0x91,0x91,0xF5,0x23,0x2A,0x84 ))// 77E9FBAD-3DCE-5E50-B439-9191F5232A84
}

private var IID___x_ABI_Ctest__component_CIObjectHandler: test_component.IID {
.init(Data1: 0x5DD35752, Data2: 0x9800, Data3: 0x5961, Data4: ( 0x80,0xDE,0xFC,0x5E,0x20,0x9E,0x6E,0x2D ))// 5DD35752-9800-5961-80DE-FC5E209E6E2D
}
Expand Down Expand Up @@ -1929,6 +1933,139 @@ public enum __ABI_test_component {
)

public typealias InterfaceWithReturnDelegateWrapper = InterfaceWrapperBase<__IMPL_test_component.InterfaceWithReturnDelegateImpl>
open class WithKeyword: test_component.IInspectable {
override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CWithKeyword }

open func EnumImpl(_ `extension`: HSTRING?) throws {
_ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.Enum(pThis, `extension`))
}
}

open func get_StructImpl() throws -> HSTRING? {
var value: HSTRING?
_ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.get_Struct(pThis, &value))
}
return value
}

open func put_StructImpl(_ value: HSTRING?) throws {
_ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.put_Struct(pThis, value))
}
}

open func add_RepeatImpl(_ handler: UnsafeMutablePointer<__x_ABI_C__FIEventHandler_1_IInspectable>?) throws -> EventRegistrationToken {
var token: EventRegistrationToken = .init()
_ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.add_Repeat(pThis, handler, &token))
}
return token
}

open func remove_RepeatImpl(_ token: EventRegistrationToken) throws {
_ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.remove_Repeat(pThis, token))
}
}

}

internal static var WithKeywordVTable: __x_ABI_Ctest__component_CWithKeywordVtbl = .init(
QueryInterface: {
guard let pUnk = $0, let riid = $1, let ppvObject = $2 else { return E_INVALIDARG }
ppvObject.pointee = nil

switch riid.pointee {
case IUnknown.IID, IInspectable.IID, ISwiftImplemented.IID, IAgileObject.IID, WithKeywordWrapper.IID:
_ = pUnk.pointee.lpVtbl.pointee.AddRef(pUnk)
ppvObject.pointee = UnsafeMutableRawPointer(pUnk)
return S_OK
default:
guard let instance = WithKeywordWrapper.tryUnwrapFrom(raw: pUnk),
let iUnknownRef = instance.queryInterface(riid.pointee) else { return failWith(err: E_NOINTERFACE )}
ppvObject.pointee = UnsafeMutableRawPointer(iUnknownRef.ref)
return S_OK

}
},

AddRef: {
guard let wrapper = WithKeywordWrapper.fromRaw($0) else { return 1 }
_ = wrapper.retain()
return ULONG(_getRetainCount(wrapper.takeUnretainedValue()))
},

Release: {
guard let wrapper = WithKeywordWrapper.fromRaw($0) else { return 1 }
return ULONG(_getRetainCount(wrapper.takeRetainedValue()))
},

GetIids: {
let size = MemoryLayout<test_component.IID>.size
let iids = CoTaskMemAlloc(UInt64(size) * 3).assumingMemoryBound(to: test_component.IID.self)
iids[0] = IUnknown.IID
iids[1] = IInspectable.IID
iids[2] = __ABI_test_component.WithKeywordWrapper.IID
$1!.pointee = 3
$2!.pointee = iids
return S_OK
},

GetRuntimeClassName: {
_ = $0
let hstring = try! HString("test_component.WithKeyword").detach()
$1!.pointee = hstring
return S_OK
},

GetTrustLevel: {
_ = $0
$1!.pointee = TrustLevel(rawValue: 0)
return S_OK
},

Enum: {
do {
guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let `extension`: String = .init(from: $1)
try __unwrapped__instance.`enum`(`extension`)
return S_OK
} catch { return failWith(err: E_FAIL) }
},

get_Struct: {
guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let value = __unwrapped__instance.`struct`
$1?.initialize(to: try! HString(value).detach())
return S_OK
},

put_Struct: {
guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let value: String = .init(from: $1)
__unwrapped__instance.`struct` = value
return S_OK
},

add_Repeat: {
guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
guard let handler = test_component.__x_ABI_C__FIEventHandler_1_IInspectableWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG }
let token = __unwrapped__instance.`repeat`.addHandler(handler)
$2?.initialize(to: .from(swift: token))
return S_OK
},

remove_Repeat: {
guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let token: EventRegistrationToken = $1
__unwrapped__instance.`repeat`.removeHandler(token)
return S_OK
}
)

public typealias WithKeywordWrapper = InterfaceWrapperBase<__IMPL_test_component.WithKeywordImpl>
public class _ABI_NonBlittableBoolStruct {
public var val: __x_ABI_Ctest__component_CNonBlittableBoolStruct = .init()
public init() { }
Expand Down
Loading

0 comments on commit a84ef91

Please sign in to comment.