// File: /workspace/github_c_makefile_star2_O1/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 0x10f83
leaq 0x4e18(%r14), %rdi
movq 0x52a8(%r14), %rdx
leaq 0x6246a(%rip), %rcx     # 0x733e0
movq 0x10(%rsp), %rsi
xorl %r8d, %r8d
callq 0x1fbf9
leaq 0x4ba8(%r14), %rbx
movq %rbx, %rdi
movl $0x38, %esi
callq 0x1b41f
xorl %ebp, %ebp
cmpl $0x45, 0x4bd0(%r14)
jne 0x11155
leaq 0x4e18(%r14), %r12
xorl %eax, %eax
xorl %r13d, %r13d
movq %rbx, 0x18(%rsp)
testb $0x1, %al
je 0x10fd3
movq 0x5118(%r14), %rdi
leaq 0x6634f(%rip), %rsi     # 0x77319
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 0x10e7a
movq 0x28(%rsp), %rbp
movb 0x15(%rbp), %al
movq %r15, 0x20(%rsp)
cmpl $0x31, 0x4bd0(%r14)
movq %rax, 0x30(%rsp)
jne 0x110a2
cmpl $0x1, 0xc(%rsp)
je 0x11045
movq 0x5118(%r14), %rdi
leaq 0x66312(%rip), %rsi     # 0x7734e
xorl %eax, %eax
callq *0x50f8(%r14)
cmpb $0x1, 0x15(%rbp)
jne 0x11062
movq 0x5118(%r14), %rdi
leaq 0x66325(%rip), %rsi     # 0x7737e
xorl %eax, %eax
callq *0x50f8(%r14)
movq %rbx, %rdi
callq 0x1a984
movq %rbp, 0x38(%rsp)
movq %r14, %rdi
leaq 0x38(%rsp), %rbx
movq %rbx, %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdx
callq 0x1617b
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x12293
incl %r13d
movl %r13d, 0x8(%rsp)
jmp 0x110cd
testl %r13d, %r13d
je 0x110c5
movl %r13d, 0x8(%rsp)
movq 0x5118(%r14), %rdi
leaq 0x662f6(%rip), %rsi     # 0x773b0
xorl %eax, %eax
callq *0x50f8(%r14)
jmp 0x110cd
movl $0x0, 0x8(%rsp)
movslq 0xc(%rsp), %r13
testq %r13, %r13
jle 0x11126
leaq 0x50(%rsp), %r15
xorl %ebx, %ebx
cmpb $0x1, 0x40(%rsp,%rbx)
jne 0x110ff
movq 0x5118(%r14), %rdi
leaq 0x662f4(%rip), %rsi     # 0x773e7
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 0x1fbf9
incq %rbx
addq $0x100, %r15            # imm = 0x100
cmpq %rbx, %r13
jne 0x110de
cmpl $0x3f, 0x4bd0(%r14)
jne 0x1114c
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x1a984
movl 0x8(%rsp), %r13d
movq 0x30(%rsp), %rax
jmp 0x10fb8
movq 0x18(%rsp), %rbx
movl 0x8(%rsp), %ebp
movq %rbx, %rdi
movl $0x39, %esi
callq 0x1b41f
movq 0x10(%rsp), %rax
movl %ebp, 0x4(%rax)
cmpl $0x41, 0x4bd0(%r14)
jne 0x111fe
movq %rbx, %rdi
callq 0x1a984
cmpl $0x38, 0x4bd0(%r14)
jne 0x11207
leaq 0x4e18(%r14), %r13
leaq 0x4220(%r14), %rsi
movq %r13, %rdi
movl $0x16, %edx
callq 0x1ea48
movq %rax, %r12
movb $0x1, 0x14(%rax)
movq %rbx, %rdi
callq 0x1a984
movq %r14, %rdi
xorl %esi, %esi
callq 0xfb34
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x1f6e2
cmpl $0x3f, 0x4bd0(%r14)
je 0x111ac
movq %rbx, %rdi
movl $0x39, %esi
callq 0x1b41f
cmpl $0x1, 0x10(%r12)
jne 0x11214
movq 0x28(%r12), %rax
movq (%rax), %rax
movq 0x108(%rax), %r12
jmp 0x11214
movq 0x5218(%r14), %r12
jmp 0x11214
movq %r14, %rdi
xorl %esi, %esi
callq 0xfb34
movq %rax, %r12
movq 0x10(%rsp), %rax
movq %r12, 0x90(%rax)
movl (%r12), %eax
cmpl $0x18, %eax
ja 0x11269
movl $0x1ec0000, %ecx        # imm = 0x1EC0000
btl %eax, %ecx
jae 0x11269
leaq 0x4e18(%r14), %r15
addq $0x4220, %r14           # imm = 0x4220
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1edd3
leaq 0x661b1(%rip), %rcx     # 0x77407
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x1fbf9
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq