cgen: use __attribute__((fastcall)) for fn types on cc != msvc (fixes compilation of doc examples)
parent
8ade188e0d
commit
0fd43a2530
|
|
@ -1345,24 +1345,34 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym ast.TypeSymbol) {
|
||||||
fn_name := sym.cname
|
fn_name := sym.cname
|
||||||
|
|
||||||
mut call_conv := ''
|
mut call_conv := ''
|
||||||
|
mut msvc_call_conv := ''
|
||||||
|
is_cc_msvc := g.pref.ccompiler == 'msvc'
|
||||||
for attr in func.attrs {
|
for attr in func.attrs {
|
||||||
match attr.name {
|
match attr.name {
|
||||||
'callconv' {
|
'callconv' {
|
||||||
call_conv = '__$attr.arg '
|
if is_cc_msvc {
|
||||||
|
msvc_call_conv = '__$attr.arg '
|
||||||
|
} else {
|
||||||
|
call_conv = '$attr.arg'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {}
|
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 {
|
for i, param in func.params {
|
||||||
g.type_definitions.write_string(g.typ(param.typ))
|
g.type_definitions.write_string(g.typ(param.typ))
|
||||||
if i < func.params.len - 1 {
|
if i < func.params.len - 1 {
|
||||||
g.type_definitions.write_string(',')
|
g.type_definitions.write_string(',')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.type_definitions.writeln(');')
|
g.type_definitions.writeln(')$call_conv_attribute_suffix;')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2153,7 +2153,7 @@ fn (mut g Gen) write_fn_attrs(attrs []ast.Attr) string {
|
||||||
'_fastcall' {
|
'_fastcall' {
|
||||||
fn_attrs += '__fastcall '
|
fn_attrs += '__fastcall '
|
||||||
}
|
}
|
||||||
"callconv" {
|
'callconv' {
|
||||||
fn_attrs += '__$attr.arg '
|
fn_attrs += '__$attr.arg '
|
||||||
}
|
}
|
||||||
'console' {
|
'console' {
|
||||||
|
|
|
||||||
|
|
@ -231,10 +231,12 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
}
|
}
|
||||||
'callconv' {
|
'callconv' {
|
||||||
if !fna.has_arg {
|
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'] {
|
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 {}
|
else {}
|
||||||
|
|
|
||||||
|
|
@ -234,10 +234,12 @@ pub fn (mut p Parser) parse_fn_type(name string) ast.Type {
|
||||||
match attr.name {
|
match attr.name {
|
||||||
'callconv' {
|
'callconv' {
|
||||||
if !attr.has_arg {
|
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'] {
|
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 {}
|
else {}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue