// File: /workspace/github_c_makefile_star2_O3/vtereshkov[P]umka-lang/src/umka_decl.c
// Function: parseSignature
static void parseSignature(Compiler *comp, Signature *sig)
{
    // Dummy hidden parameter that allows any function to be converted to a closure
    if (!sig->isMethod)
        typeAddParam(&comp->types, sig, comp->anyType, "#upvalues", (Const){0});

    // Formal parameter list
    lexEat(&comp->lex, TOK_LPAR);
    int numDefaultParams = 0;

    if (comp->lex.tok.kind == TOK_IDENT)
    {
        bool variadicParamListFound = false;
        while (1)
        {
            if (variadicParamListFound)
                comp->error.handler(comp->error.context, "Variadic parameter list should be the last parameter");

            IdentName paramNames[MAX_PARAMS];
            bool paramExported[MAX_PARAMS];
            const Type *paramType = NULL;
            int numParams = 0;
            parseTypedIdentList(comp, paramNames, paramExported, MAX_PARAMS, &numParams, &paramType, true);

            variadicParamListFound = paramType->isVariadicParamList;

            // ["=" expr]
            Const defaultConstant = {0};
            if (comp->lex.tok.kind == TOK_EQ)
            {
                if (numParams != 1)
                    comp->error.handler(comp->error.context, "Parameter list cannot have common default value");

                if (paramType->isVariadicParamList)
                    comp->error.handler(comp->error.context, "Variadic parameter list cannot have default value");

                lexNext(&comp->lex);

                const Type *defaultType = paramType;
                parseExpr(comp, &defaultType, &defaultConstant);
                doAssertImplicitTypeConv(comp, paramType, &defaultType, &defaultConstant);

                numDefaultParams++;
            }
            else
            {
                if (numDefaultParams != 0)
                    comp->error.handler(comp->error.context, "Parameters with default values should be the last ones");
            }

            for (int i = 0; i < numParams; i++)
            {
                if (paramExported[i])
                    comp->error.handler(comp->error.context, "Parameter %s cannot be exported", paramNames[i]);

                typeAddParam(&comp->types, sig, paramType, paramNames[i], defaultConstant);
            }

            if (comp->lex.tok.kind != TOK_COMMA)
                break;
            lexNext(&comp->lex);
        }
    }
    lexEat(&comp->lex, TOK_RPAR);
    sig->numDefaultParams = numDefaultParams;

    // Result type
    if (comp->lex.tok.kind == TOK_COLON)
    {
        lexNext(&comp->lex);
        if (comp->lex.tok.kind == TOK_LPAR)
        {
            // Result type list (syntactic sugar - actually a structure type)
            Type *listType = typeAdd(&comp->types, &comp->blocks, TYPE_STRUCT);
            listType->isExprList = true;

            lexNext(&comp->lex);

            while (1)
            {
                const Type *fieldType = parseType(comp, NULL);
                typeAddField(&comp->types, listType, fieldType, NULL);

                if (comp->lex.tok.kind != TOK_COMMA)
                    break;
                lexNext(&comp->lex);
            }
            lexEat(&comp->lex, TOK_RPAR);

            if (listType->numItems == 1)
                sig->resultType = listType->field[0]->type;
            else
                sig->resultType = listType;
        }
        else
            // Single result type
            sig->resultType = parseType(comp, NULL);
    }
    else
        sig->resultType = comp->voidType;

    // Structured result parameter
    if (typeStructured(sig->resultType))
        typeAddParam(&comp->types, sig, typeAddPtrTo(&comp->types, &comp->blocks, sig->resultType), "#result", (Const){0});
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1058, %rsp           # imm = 0x1058
movq %rdi, %r14
movq %rsi, 0x10(%rsp)
cmpb $0x0, 0x8(%rsi)
jne 0xbdca
leaq 0x4e18(%r14), %rdi
movq 0x52a8(%r14), %rdx
leaq 0x66623(%rip), %rcx     # 0x723e0
movq 0x10(%rsp), %rsi
xorl %r8d, %r8d
callq 0x1ab67
leaq 0x4ba8(%r14), %rbx
movq %rbx, %rdi
movl $0x38, %esi
callq 0x1637d
xorl %ebp, %ebp
cmpl $0x45, 0x4bd0(%r14)
jne 0xbf9d
leaq 0x4e18(%r14), %r12
xorl %eax, %eax
xorl %r13d, %r13d
movq %rbx, 0x18(%rsp)
testb $0x1, %al
je 0xbe1a
movq 0x5118(%r14), %rdi
leaq 0x6a508(%rip), %rsi     # 0x76319
xorl %eax, %eax
callq *0x50f8(%r14)
xorl %r15d, %r15d
movq %r15, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x1, (%rsp)
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movl $0x10, %ecx
leaq 0xc(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0xbcc1
movq 0x28(%rsp), %rbp
movb 0x15(%rbp), %al
movq %r15, 0x20(%rsp)
cmpl $0x31, 0x4bd0(%r14)
movq %rax, 0x30(%rsp)
jne 0xbeea
cmpl $0x1, 0xc(%rsp)
je 0xbe8f
movq 0x5118(%r14), %rdi
leaq 0x6a4cb(%rip), %rsi     # 0x7634e
xorl %eax, %eax
callq *0x50f8(%r14)
movb 0x15(%rbp), %al
testb $0x1, %al
je 0xbeaa
movq 0x5118(%r14), %rdi
leaq 0x6a4dd(%rip), %rsi     # 0x7637e
xorl %eax, %eax
callq *0x50f8(%r14)
movq %rbx, %rdi
callq 0x15914
movq %rbp, 0x38(%rsp)
movq %r14, %rdi
leaq 0x38(%rsp), %rbx
movq %rbx, %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdx
callq 0x111c3
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0xd17b
incl %r13d
movl %r13d, 0x8(%rsp)
jmp 0xbf15
testl %r13d, %r13d
je 0xbf0d
movl %r13d, 0x8(%rsp)
movq 0x5118(%r14), %rdi
leaq 0x6a4ae(%rip), %rsi     # 0x763b0
xorl %eax, %eax
callq *0x50f8(%r14)
jmp 0xbf15
movl $0x0, 0x8(%rsp)
movslq 0xc(%rsp), %r13
testq %r13, %r13
jle 0xbf6e
leaq 0x50(%rsp), %r15
xorl %ebx, %ebx
cmpb $0x1, 0x40(%rsp,%rbx)
jne 0xbf47
movq 0x5118(%r14), %rdi
leaq 0x6a4ac(%rip), %rsi     # 0x763e7
movq %r15, %rdx
xorl %eax, %eax
callq *0x50f8(%r14)
movq 0x20(%rsp), %r8
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x1ab67
incq %rbx
addq $0x100, %r15            # imm = 0x100
cmpq %rbx, %r13
jne 0xbf26
cmpl $0x3f, 0x4bd0(%r14)
jne 0xbf94
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x15914
movl 0x8(%rsp), %r13d
movq 0x30(%rsp), %rax
jmp 0xbdff
movq 0x18(%rsp), %rbx
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
movl $0x39, %esi
callq 0x1637d
movq 0x10(%rsp), %rax
movl %ebp, 0x4(%rax)
cmpl $0x41, 0x4bd0(%r14)
jne 0xc046
movq %rbx, %rdi
callq 0x15914
cmpl $0x38, 0x4bd0(%r14)
jne 0xc04f
leaq 0x4e18(%r14), %r13
leaq 0x4220(%r14), %rsi
movq %r13, %rdi
movl $0x16, %edx
callq 0x19a70
movq %rax, %r12
movb $0x1, 0x14(%rax)
movq %rbx, %rdi
callq 0x15914
movq %r14, %rdi
xorl %esi, %esi
callq 0xa954
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x1a697
cmpl $0x3f, 0x4bd0(%r14)
je 0xbff4
movq %rbx, %rdi
movl $0x39, %esi
callq 0x1637d
cmpl $0x1, 0x10(%r12)
jne 0xc05c
movq 0x28(%r12), %rax
movq (%rax), %rax
movq 0x108(%rax), %r12
jmp 0xc05c
movq 0x5218(%r14), %r12
jmp 0xc05c
movq %r14, %rdi
xorl %esi, %esi
callq 0xa954
movq %rax, %r12
movq 0x10(%rsp), %rax
movq %r12, 0x90(%rax)
movl (%r12), %eax
cmpl $0x18, %eax
ja 0xc0b1
movl $0x1ec0000, %ecx        # imm = 0x1EC0000
btl %eax, %ecx
jae 0xc0b1
leaq 0x4e18(%r14), %r15
addq $0x4220, %r14           # imm = 0x4220
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x19e01
leaq 0x6a369(%rip), %rcx     # 0x76407
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x1ab67
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq