// File: /workspace/github_c_makefile_star2_O2/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 0x8196
leaq 0x4e18(%r14), %rdi
movq 0x52a8(%r14), %rdx
leaq 0x5e257(%rip), %rcx     # 0x663e0
movq 0x10(%rsp), %rsi
xorl %r8d, %r8d
callq 0x15e11
leaq 0x4ba8(%r14), %rbx
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0x118d3
xorl %ebp, %ebp
cmpl $0x45, 0x4bd0(%r14)
jne 0x836a
leaq 0x4e18(%r14), %r12
xorl %eax, %eax
xorl %r15d, %r15d
movq %rbx, 0x18(%rsp)
testb $0x1, %al
je 0x81e4
movq 0x5118(%r14), %rdi
leaq 0x620e2(%rip), %rsi     # 0x6a2bd
xorl %eax, %eax
callq *0x50f8(%r14)
andq $0x0, 0x28(%rsp)
andl $0x0, 0xc(%rsp)
movl $0x1, (%rsp)
movq %r14, %rdi
leaq 0x50(%rsp), %rsi
leaq 0x40(%rsp), %rdx
pushq $0x10
popq %rcx
leaq 0xc(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x8091
movq 0x28(%rsp), %rbp
movb 0x15(%rbp), %al
andq $0x0, 0x20(%rsp)
cmpl $0x31, 0x4bd0(%r14)
movq %rax, 0x30(%rsp)
jne 0x82ae
cmpl $0x1, 0xc(%rsp)
je 0x8253
movq 0x5118(%r14), %rdi
leaq 0x620ab(%rip), %rsi     # 0x6a2f2
xorl %eax, %eax
callq *0x50f8(%r14)
movb 0x15(%rbp), %al
testb $0x1, %al
je 0x826e
movq 0x5118(%r14), %rdi
leaq 0x620bd(%rip), %rsi     # 0x6a322
xorl %eax, %eax
callq *0x50f8(%r14)
movq %rbx, %rdi
callq 0x10f6e
movq %rbp, 0x38(%rsp)
movq %r14, %rdi
leaq 0x38(%rsp), %rbx
movq %rbx, %rsi
leaq 0x20(%rsp), %r13
movq %r13, %rdx
callq 0xd3a5
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x97b3
incl %r15d
movl %r15d, 0x8(%rsp)
jmp 0x82d9
testl %r15d, %r15d
je 0x82d1
movl %r15d, 0x8(%rsp)
movq 0x5118(%r14), %rdi
leaq 0x6208e(%rip), %rsi     # 0x6a354
xorl %eax, %eax
callq *0x50f8(%r14)
jmp 0x82d9
movl $0x0, 0x8(%rsp)
movl 0xc(%rsp), %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
leaq 0x50(%rsp), %r15
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x833b
cmpb $0x1, 0x40(%rsp,%rbx)
jne 0x8317
movq 0x5118(%r14), %rdi
leaq 0x62080(%rip), %rsi     # 0x6a38b
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 0x15e11
incq %rbx
addq $0x100, %r15            # imm = 0x100
jmp 0x82f1
cmpl $0x3f, 0x4bd0(%r14)
jne 0x8361
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x10f6e
movl 0x8(%rsp), %r15d
movq 0x30(%rsp), %rax
jmp 0x81c9
movq 0x18(%rsp), %rbx
movl 0x8(%rsp), %ebp
pushq $0x39
popq %rsi
movq %rbx, %rdi
callq 0x118d3
movq 0x10(%rsp), %rax
movl %ebp, 0x4(%rax)
cmpl $0x41, 0x4bd0(%r14)
jne 0x840d
movq %rbx, %rdi
callq 0x10f6e
cmpl $0x38, 0x4bd0(%r14)
jne 0x8416
leaq 0x4e18(%r14), %r15
leaq 0x4220(%r14), %rsi
pushq $0x16
popq %rdx
movq %r15, %rdi
callq 0x14da9
movq %rax, %r12
movb $0x1, 0x14(%rax)
movq %rbx, %rdi
callq 0x10f6e
movq %r14, %rdi
xorl %esi, %esi
callq 0x6e28
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x159f0
cmpl $0x3f, 0x4bd0(%r14)
je 0x83bd
pushq $0x39
popq %rsi
movq %rbx, %rdi
callq 0x118d3
cmpl $0x1, 0x10(%r12)
jne 0x8423
movq 0x28(%r12), %rax
movq (%rax), %rax
movq 0x108(%rax), %r12
jmp 0x8423
movq 0x5218(%r14), %r12
jmp 0x8423
movq %r14, %rdi
xorl %esi, %esi
callq 0x6e28
movq %rax, %r12
movq 0x10(%rsp), %rax
movq %r12, 0x90(%rax)
movl (%r12), %eax
cmpl $0x18, %eax
ja 0x8478
movl $0x1ec0000, %ecx        # imm = 0x1EC0000
btl %eax, %ecx
jae 0x8478
leaq 0x4e18(%r14), %r15
addq $0x4220, %r14           # imm = 0x4220
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x15100
leaq 0x61f46(%rip), %rcx     # 0x6a3ab
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x15e11
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq