// File: /workspace/github_c_makefile_star2_O2/johsteffens[P]beth/xoico/src/xoico.xo.c
// Function: xoico_signature_s_parse
er_t xoico_signature_s_parse( xoico_signature_s* o, const xoico_host* host, x_source* source )
{
    // xoico_signature.x:141:1
    BLM_INIT_LEVEL(0);
    xoico_compiler_s* compiler = xoico_host_a_compiler(host);
    st_s* name_buf = ((st_s*)BLM_LEVEL_T_PUSH(0,st_s,st_s_create()));
    
    if( x_source_parse_bl(source," #?'extending'" ) )
    {
        tp_t tp_name = 0;
        BLM_TRY(xoico_host_a_parse_name_tp(host,source, (&(tp_name)) ))
        const xoico_signature_s* signature = xoico_compiler_s_get_signature(compiler,tp_name );
        if( !signature ) BLM_RETURNV(er_t, x_source_parse_error_fa(source,"Could not find predefined signature '#<sc_t>'.", xoico_host_a_nameof(host,tp_name ) ))
        xoico_signature_s_copy(o,signature );
        x_source_point_s_setup_from_source(&(o->source_point),source );
        BLM_TRY(x_source_parse_fa(source," #name", name_buf ))  // empty name is allowed
        o->name = xoico_compiler_s_entypeof(compiler,name_buf->sc );
        BLM_TRY(x_source_parse_fa(source," (" ))
        BLM_TRY(xoico_args_s_append(&(o->args),host, source ))
        BLM_TRY(x_source_parse_fa(source," )" ))
    }
    else
    {
        x_source_point_s_setup_from_source(&(o->source_point),source );
        BLM_TRY(xoico_typespec_s_parse(&(o->typespec_ret),host, source ))
        o->typespec_ret.flag_addressable = false;
    
        // get name
        BLM_TRY(x_source_parse_fa(source," #name", name_buf ))
        o->name = xoico_compiler_s_entypeof(compiler,name_buf->sc );
    
        BLM_TRY(x_source_parse_fa(source," (" ))
    
        if( !x_source_parse_bl(source," #=?')'" ) )
        {BLM_INIT_LEVEL(2);
            xoico_arg_s* arg = ((xoico_arg_s*)BLM_LEVEL_T_PUSH(2,xoico_arg_s,xoico_arg_s_create()));
            BLM_TRY(xoico_arg_s_parse(arg,host, source ))
            if( !x_source_parse_bl(source," #=?')'" ) ) BLM_TRY(x_source_parse_fa(source,", " ))
            if( arg->name == TYPEOF_o )
            {
                xoico_arg_s_attach( &(o->arg_o ),  ((xoico_arg_s*)bcore_fork(arg)));
                BLM_TRY(xoico_args_s_parse(&(o->args),host, source ))
            }
            else
            {
                x_array_push_d(((x_array*)(&(o->args))),((x_inst*)(((xoico_arg_s*)bcore_fork(arg)) )));
                BLM_TRY(xoico_args_s_append(&(o->args),host, source ))
            }
        BLM_DOWN();}
        else
        {
            BLM_TRY(xoico_args_s_parse(&(o->args),host, source ))
        }
    
        BLM_TRY(x_source_parse_fa(source," )" ))
    }
    
    /// if return type is a name in the argument list, copy argument typespec to return typespec
    const xoico_arg_s* ret_arg = ( o->typespec_ret.indirection == 0 ) ? xoico_signature_s_get_arg_by_name(o,o->typespec_ret.type ) : NULL;
    
    if( ret_arg )
    {
        if( o->typespec_ret.access_class != 0  || o->typespec_ret.transient || o->typespec_ret.indirection != 0 )
        {
            BLM_RETURNV(er_t, x_source_parse_error_fa(source,"Return typespec: Argument name used as type." ))
        }
        xoico_typespec_s_copy(&(o->typespec_ret),&(ret_arg->typespec ));
        BCORE_PASS_CREATE(xoico_typespec_transient_s,o->typespec_ret.transient)->cast_to_var = ret_arg->name;
        xoico_arg_s_attach( &(o->direct_return_arg ),  ((xoico_arg_s*)bcore_fork(((xoico_arg_s*)(ret_arg)))));
    }
    
    BLM_TRY(xoico_signature_s_set_global_name(o,host ))
    
    BLM_RETURNV(er_t, 0)
}

er_t xoico_signature_s_expand_declaration( const xoico_signature_s* o, const xoico_host* host, sc_t sc_func_global_name, sz_t indent, x_sink* sink )
{
    // xoico_signature.x:217:1
    
    BLM_TRY(xoico_signature_s_expand_ret(o,host, sink ))
    x_sink_push_fa(sink," #<sc_t>( ", sc_func_global_name );
    
    if( o->arg_o )
    {
        BLM_TRY(xoico_arg_s_expand(o->arg_o,host, sink ))
        BLM_TRY(xoico_args_s_expand(&(o->args),host, false, sink ))
        x_sink_push_fa(sink," )" );
    }
    else
    {
        if( o->args.size > 0 )
        {
            BLM_TRY(xoico_args_s_expand(&(o->args),host, true, sink ))
        }
        else
        {
            x_sink_push_fa(sink,"void" );
        }
        x_sink_push_fa(sink," )" );
    }
    return  0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1081a8(%rip), %rax    # 0x117dd0
movq (%rax), %rsi
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x717f5
andq $0x0, 0x10(%r12)
movq %rbx, %rdi
callq 0xdab3
movq %rax, %r13
xorl %eax, %eax
callq 0x8eb65
movabsq $-0x503db0e6e1c2e704, %rdx # imm = 0xAFC24F191E3D18FC
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0x71967
movq %rax, %rbp
leaq 0x9a83e(%rip), %rsi     # 0xaa4af
movq %r15, %rdi
callq 0xe5ff
testb %al, %al
je 0xfd30
leaq 0x38(%rsp), %rdx
andq $0x0, (%rdx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdcb4
testq %rax, %rax
jne 0xfe31
movq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0xffca
testq %rax, %rax
je 0xfe10
movq %r14, %rdi
movq %rax, %rsi
callq 0xf93d
leaq 0x80(%r14), %rdi
movq %r15, %rsi
callq 0xdc7d
leaq 0xb2452(%rip), %rsi     # 0xc2127
movq %r15, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xd600
testq %rax, %rax
jne 0xfe31
movq 0x8(%rbp), %rsi
addq $0x140, %r13            # imm = 0x140
movq %r13, %rdi
callq 0x6f48c
movq %rax, 0x8(%r14)
leaq 0x9a7e4(%rip), %rsi     # 0xaa4ed
movq %r15, %rdi
xorl %eax, %eax
callq 0xd600
testq %rax, %rax
jne 0xfe31
leaq 0x58(%r14), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xf5d6
jmp 0xfdd0
leaq 0x80(%r14), %rdi
movq %r15, %rsi
callq 0xdc7d
leaq 0x20(%r14), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xe149
testq %rax, %rax
jne 0xfe31
movb $0x0, 0x4e(%r14)
leaq 0xb23c4(%rip), %rsi     # 0xc2127
movq %r15, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xd600
testq %rax, %rax
jne 0xfe31
movq 0x8(%rbp), %rsi
addq $0x140, %r13            # imm = 0x140
movq %r13, %rdi
callq 0x6f48c
movq %rax, 0x8(%r14)
leaq 0x9a756(%rip), %rsi     # 0xaa4ed
movq %r15, %rdi
xorl %eax, %eax
callq 0xd600
testq %rax, %rax
jne 0xfe31
leaq 0x9a73f(%rip), %rsi     # 0xaa4f0
movq %r15, %rdi
callq 0xe5ff
testb %al, %al
je 0xfe50
leaq 0x58(%r14), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10022
testq %rax, %rax
jne 0xfe31
leaq 0xb910a(%rip), %rsi     # 0xc8ee6
movq %r15, %rdi
xorl %eax, %eax
callq 0xd600
testq %rax, %rax
jne 0xfe31
cmpq $0x0, 0x40(%r14)
je 0xff0b
movq %r14, %rdi
movq %rbx, %rsi
callq 0xfb44
leaq 0x8(%rsp), %rdi
callq 0x71b64
xorl %r12d, %r12d
jmp 0xfe3e
movq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0xdf58
leaq 0x9a69a(%rip), %rsi     # 0xaa4be
movq %r15, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd7c4
movq %rax, %r12
leaq 0x8(%rsp), %rdi
callq 0x71b64
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38(%rsp), %r12
movq %r12, (%rsp)
leaq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x717f5
movq $0x2, 0x10(%r12)
callq 0xf0de
pushq $0x2
popq %rsi
movabsq $-0x21883bf5398e4338, %rdx # imm = 0xDE77C40AC671BCC8
movq %r12, %rdi
movq %rax, %rcx
callq 0x71967
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xf119
testq %rax, %rax
jne 0xff66
leaq 0x9a643(%rip), %rsi     # 0xaa4f0
movq %r15, %rdi
callq 0xe5ff
testb %al, %al
jne 0xfed3
leaq 0x9a49e(%rip), %rsi     # 0xaa35e
movq %r15, %rdi
xorl %eax, %eax
callq 0xd600
testq %rax, %rax
jne 0xff66
movq 0x50(%r13), %r12
movq %r13, %rdi
callq 0x6026d
movabsq $-0x509c1db379fe0942, %rcx # imm = 0xAF63E24C8601F6BE
cmpq %rcx, %r12
jne 0xff44
leaq 0x50(%r14), %rdi
movq %rax, %rsi
callq 0x10000
leaq 0x58(%r14), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10022
jmp 0xff61
movq 0x28(%r14), %rsi
movq %r14, %rdi
callq 0xfaa0
testq %rax, %rax
je 0xfdf6
cmpq $0x0, 0x30(%r14)
jne 0xff2e
cmpq $0x0, 0x38(%r14)
je 0xff80
leaq 0x9a5c3(%rip), %rsi     # 0xaa4f8
movq %r15, %rdi
xorl %eax, %eax
callq 0xd7c4
jmp 0xfe31
leaq 0x58(%r14), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xf6dd
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xf5d6
testq %rax, %rax
je 0xff73
movq %rax, %r12
leaq 0x38(%rsp), %rdi
jmp 0xfe39
movq %rsp, %rdi
callq 0x71b48
jmp 0xfdd5
movq %rax, %r12
leaq 0x20(%r14), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0xe075
movq 0x50(%r12), %r15
movq 0x38(%r14), %rax
testq %rax, %rax
jne 0xffaa
callq 0xdff3
movq %rax, 0x38(%r14)
movq %r15, 0x10(%rax)
leaq 0x78(%r14), %r15
movq %r12, %rdi
callq 0x6026d
movq %r15, %rdi
movq %rax, %rsi
callq 0x10000
jmp 0xfdf6