From 77db50bce852753c866f65cd14bc0efee178a0e7 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 7 Feb 2025 22:03:15 +0100 Subject: [PATCH] Allow function types to have a calling convention. #1938 --- releasenotes.md | 1 + src/compiler/sema_decls.c | 11 ++++++++++- test/test_suite/functions/call_conv_fntype.c3 | 6 ++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/test_suite/functions/call_conv_fntype.c3 diff --git a/releasenotes.md b/releasenotes.md index 3fb859607..3ff1a5116 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -46,6 +46,7 @@ - Fixing various issues around shifts, like `z <<= { 1, 2 }`. - `return (any)&foo` would not be reported as an escaping variable if `foo` was a pointer or slice. - Incorrect error message when providing too many associated values for enum #1934. +- Allow function types to have a calling convention. #1938 ### Stdlib changes - Added '%h' and '%H' for printing out binary data in hexadecimal using the formatter. diff --git a/src/compiler/sema_decls.c b/src/compiler/sema_decls.c index b81798b59..6652dffd6 100755 --- a/src/compiler/sema_decls.c +++ b/src/compiler/sema_decls.c @@ -2450,6 +2450,11 @@ static const char *attribute_domain_to_string(AttributeDomain domain) // Helper method INLINE bool update_abi(Decl *decl, CallABI abi) { + if (decl->decl_kind == DECL_TYPEDEF) + { + decl->typedef_decl.decl->fntype_decl.abi = abi; + return true; + } decl->func_decl.signature.abi = abi; return true; } @@ -2520,7 +2525,7 @@ static bool sema_analyse_attribute(SemaContext *context, ResolvedAttrData *attr_ [ATTRIBUTE_BENCHMARK] = ATTR_FUNC, [ATTRIBUTE_BIGENDIAN] = ATTR_BITSTRUCT, [ATTRIBUTE_BUILTIN] = ATTR_MACRO | ATTR_FUNC | ATTR_GLOBAL | ATTR_CONST, - [ATTRIBUTE_CALLCONV] = ATTR_FUNC | ATTR_INTERFACE_METHOD, + [ATTRIBUTE_CALLCONV] = ATTR_FUNC | ATTR_DEF | ATTR_INTERFACE_METHOD, [ATTRIBUTE_COMPACT] = ATTR_STRUCT | ATTR_UNION, [ATTRIBUTE_CONST] = ATTR_MACRO, [ATTRIBUTE_DEPRECATED] = USER_DEFINED_TYPES | CALLABLE_TYPE | ATTR_CONST | ATTR_GLOBAL | ATTR_MEMBER | ATTR_BITSTRUCT_MEMBER | ATTR_INTERFACE, @@ -2616,6 +2621,10 @@ static bool sema_analyse_attribute(SemaContext *context, ResolvedAttrData *attr_ decl->func_decl.attr_winmain = true; break; case ATTRIBUTE_CALLCONV: + if (domain == ATTR_DEF && (decl->decl_kind != DECL_TYPEDEF || !decl->typedef_decl.is_func)) + { + RETURN_SEMA_ERROR(attr, "'@callconv' cannot only be used with fn types."); + } if (!expr) RETURN_SEMA_ERROR(decl, "Expected a string argument."); if (expr && !sema_analyse_expr(context, expr)) return false; if (!expr_is_const_string(expr)) RETURN_SEMA_ERROR(expr, "Expected a constant string value as argument."); diff --git a/test/test_suite/functions/call_conv_fntype.c3 b/test/test_suite/functions/call_conv_fntype.c3 new file mode 100644 index 000000000..58f40a3b9 --- /dev/null +++ b/test/test_suite/functions/call_conv_fntype.c3 @@ -0,0 +1,6 @@ +def Foo = fn int(String x, int z) @callconv("veccall"); + +fn int v(String x, int z) @callconv("veccall") +{ + return 1; +} \ No newline at end of file