cgen: use __attribute__((fastcall)) for fn types on cc != msvc (fixes compilation of doc examples)

pull/14027/head
Delyan Angelov 2022-04-14 08:06:08 +03:00
parent 8ade188e0d
commit 0fd43a2530
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 23 additions and 9 deletions

View File

@ -1345,24 +1345,34 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) {
fn_name := sym.cname
mut call_conv := ''
mut msvc_call_conv := ''
is_cc_msvc := g.pref.ccompiler == 'msvc'
for attr in func.attrs {
match attr.name {
'callconv' {
call_conv = '__$attr.arg '
if is_cc_msvc {
msvc_call_conv = '__$attr.arg '
} else {
call_conv = '$attr.arg'
}
}
else {}
}
}
call_conv_attribute_suffix := if call_conv.len != 0 {
'__attribute__(($call_conv))'
} else {
''
}
g.type_definitions.write_string('typedef ${g.typ(func.return_type)} ($call_conv*$fn_name)(')
g.type_definitions.write_string('typedef ${g.typ(func.return_type)} ($msvc_call_conv*$fn_name)(')
for i, param in func.params {
g.type_definitions.write_string(g.typ(param.typ))
if i < func.params.len - 1 {
g.type_definitions.write_string(',')
}
}
g.type_definitions.writeln(');')
g.type_definitions.writeln(')$call_conv_attribute_suffix;')
}
}

View File

@ -2153,7 +2153,7 @@ fn (mut g Gen) write_fn_attrs(attrs []ast.Attr) string {
'_fastcall' {
fn_attrs += '__fastcall '
}
"callconv" {
'callconv' {
fn_attrs += '__$attr.arg '
}
'console' {

View File

@ -231,10 +231,12 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
}
'callconv' {
if !fna.has_arg {
p.error_with_pos('callconv attribute is present but its value is missing', p.prev_tok.pos())
p.error_with_pos('callconv attribute is present but its value is missing',
p.prev_tok.pos())
}
if fna.arg !in ['stdcall', 'fastcall', 'cdecl'] {
p.error_with_pos('unsupported calling convention, supported are stdcall, fastcall and cdecl', p.prev_tok.pos())
p.error_with_pos('unsupported calling convention, supported are stdcall, fastcall and cdecl',
p.prev_tok.pos())
}
}
else {}

View File

@ -234,10 +234,12 @@ pub fn (mut p Parser) parse_fn_type(name string) ast.Type {
match attr.name {
'callconv' {
if !attr.has_arg {
p.error_with_pos('callconv attribute is present but its value is missing', p.prev_tok.pos())
p.error_with_pos('callconv attribute is present but its value is missing',
p.prev_tok.pos())
}
if attr.arg !in ['stdcall', 'fastcall', 'cdecl'] {
p.error_with_pos('unsupported calling convention, supported are stdcall, fastcall and cdecl', p.prev_tok.pos())
p.error_with_pos('unsupported calling convention, supported are stdcall, fastcall and cdecl',
p.prev_tok.pos())
}
}
else {}