// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_ident.c
// Function: identAdd
static Ident *identAdd(Idents *idents, const Modules *modules, const Blocks *blocks, IdentKind kind, const char *name, const Type *type, bool exported)
{
    const Type *rcvType = NULL;
    if (type->kind == TYPE_FN && type->sig.isMethod)
        rcvType = type->sig.param[0]->type;

    const Ident *existingIdent = identFindEx(idents, modules, blocks, blocks->module, name, rcvType, false, kind == IDENT_MODULE);

    if (existingIdent)
    {
        if (existingIdent->block == blocks->item[blocks->top].block)
        {
            // Forward type declaration resolution
            if (existingIdent->kind == IDENT_TYPE && existingIdent->type->kind == TYPE_FORWARD &&
                kind == IDENT_TYPE && type->kind != TYPE_FORWARD &&
                strcmp(existingIdent->type->typeIdent->name, name) == 0)
            {
                Ident *modifiableIdent = (Ident *)existingIdent;
                Type *modifiableType = (Type *)type;
                Type *modifiableIdentType = (Type *)existingIdent->type;

                modifiableType->typeIdent = existingIdent;
                typeDeepCopy(idents->storage, modifiableIdentType, type);
                modifiableIdent->exported = exported;

                return modifiableIdent;
            }

            // Function prototype resolution
            if (existingIdent->kind == IDENT_CONST && existingIdent->type->kind == TYPE_FN &&
                kind == IDENT_CONST && type->kind == TYPE_FN &&
                existingIdent->exported == exported &&
                strcmp(existingIdent->name, name) == 0 &&
                typeCompatible(existingIdent->type, type) &&
                existingIdent->prototypeOffset >= 0)
            {
                Ident *modifiableIdent = (Ident *)existingIdent;
                Type *modifiableIdentType = (Type *)existingIdent->type;

                typeDeepCopy(idents->storage, modifiableIdentType, type);

                return modifiableIdent;
            }

            idents->error->handler(idents->error->context, "Duplicate identifier %s", name);
        }
        else if (!identIsHidden(name) && !identIsPlaceholder(name) && existingIdent->block != 0 && !identIsOuterLocalVar(blocks, existingIdent))
        {
            idents->error->warningHandler(idents->error->context, idents->debug, "Shadowed identifier %s", name);
        }
    }

    if (exported && blocks->top != 0)
        idents->error->handler(idents->error->context, "Local identifier %s cannot be exported", name);

    if (kind == IDENT_CONST || kind == IDENT_VAR)
    {
        if (type->kind == TYPE_FORWARD)
            idents->error->handler(idents->error->context, "Unresolved forward type declaration for %s", name);

        if (type->kind == TYPE_VOID)
            idents->error->handler(idents->error->context, "Void variable or constant %s is not allowed", name);
    }

    Ident *ident = storageAdd(idents->storage, sizeof(Ident));
    ident->kind = kind;

    strncpy(ident->name, name, MAX_IDENT_LEN);
    ident->name[MAX_IDENT_LEN] = 0;

    ident->hash = hash(name);

    ident->type              = type;
    ident->module            = blocks->module;
    ident->block             = blocks->item[blocks->top].block;
    ident->exported          = exported;
    ident->globallyAllocated = false;
    ident->temporary         = false;
    ident->used              = exported || ident->module == 0 || identIsHidden(ident->name) || identIsPlaceholder(ident->name) || identIsMain(ident);  // Exported, predefined, hidden, placeholder identifiers and main() are always treated as used
    ident->prototypeOffset   = -1;
    ident->debug             = *(idents->debug);

    ident->next   = idents->first;
    idents->first = ident;

    return ident;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r12
movl %ecx, %ebp
movq %rdx, %r15
movq %rdi, %rbx
movb 0x50(%rsp), %r14b
cmpl $0x1a, (%r9)
movq %r9, 0x8(%rsp)
jne 0x19e37
cmpb $0x1, 0x30(%r9)
jne 0x19e37
movq 0x38(%r9), %rax
movq 0x108(%rax), %r9
jmp 0x19e3a
xorl %r9d, %r9d
movl 0x968(%r15), %ecx
xorl %eax, %eax
cmpl $0x4, %ebp
sete %al
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %r8
pushq %rax
pushq $0x0
callq 0x19ab1
addq $0x10, %rsp
testq %rax, %rax
je 0x19f96
movq %rax, %r13
movl 0x114(%rax), %eax
movslq 0x964(%r15), %rcx
leaq (%rcx,%rcx,2), %rcx
cmpl (%r15,%rcx,8), %eax
jne 0x19f56
movl (%r13), %r14d
testl %r14d, %r14d
je 0x19ede
cmpl $0x2, %r14d
jne 0x19f7b
cmpl $0x2, %ebp
jne 0x19ed5
movq 0x108(%r13), %rcx
cmpl $0x1, (%rcx)
jne 0x19ed5
movq 0x8(%rsp), %rax
cmpl $0x1, (%rax)
je 0x19ed5
movq 0x18(%rcx), %rdi
addq $0x4, %rdi
movq %rcx, 0x10(%rsp)
movq %r12, %rsi
callq 0x3210
movq 0x10(%rsp), %rsi
testl %eax, %eax
je 0x1a15a
testl %r14d, %r14d
jne 0x19f7b
testl %ebp, %ebp
jne 0x19f7b
movq 0x108(%r13), %r14
cmpl $0x1a, (%r14)
jne 0x19f7b
movq 0x8(%rsp), %rax
cmpl $0x1a, (%rax)
jne 0x19f7b
movb 0x50(%rsp), %al
cmpb %al, 0x118(%r13)
jne 0x19f7b
movq %r13, %rdi
addq $0x4, %rdi
movq %r12, %rsi
callq 0x3210
testl %eax, %eax
jne 0x19f7b
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x1f24f
testb %al, %al
je 0x19f7b
cmpl $0x0, 0x11c(%r13)
js 0x19f7b
movq 0x108(%r13), %rsi
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rdx
callq 0x1ec6f
jmp 0x1a10c
movzbl (%r12), %ecx
cmpl $0x23, %ecx
je 0x19f96
cmpl $0x5f, %ecx
jne 0x1a11e
testl %eax, %eax
je 0x19f96
cmpb $0x0, 0x1(%r12)
jne 0x1a126
jmp 0x19f96
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5e2aa(%rip), %rsi     # 0x78234
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movb 0x50(%rsp), %r14b
testb %r14b, %r14b
je 0x19fbb
cmpl $0x0, 0x964(%r15)
je 0x19fbb
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5e2af(%rip), %rsi     # 0x78263
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
cmpl $0x1, %ebp
ja 0x19fff
movq 0x8(%rsp), %r13
cmpl $0x1, (%r13)
jne 0x19fe2
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5e2af(%rip), %rsi     # 0x7828a
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
cmpl $0x2, (%r13)
jne 0x19fff
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5e2bd(%rip), %rsi     # 0x782b5
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x18(%rbx), %rdi
movl $0x148, %esi            # imm = 0x148
callq 0xcca8
movq %rax, %r13
movl %ebp, (%rax)
leaq 0x4(%rax), %rbp
movl $0xff, %edx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3080
movb $0x0, 0x103(%r13)
movb (%r12), %cl
testb %cl, %cl
je 0x1a057
incq %r12
movl $0x1505, %eax           # imm = 0x1505
movl %eax, %edx
shll $0x5, %edx
addl %eax, %edx
movsbl %cl, %eax
addl %edx, %eax
movb (%r12), %cl
incq %r12
testb %cl, %cl
jne 0x1a03e
jmp 0x1a05c
movl $0x1505, %eax           # imm = 0x1505
movl %eax, 0x104(%r13)
movq 0x8(%rsp), %rax
movq %rax, 0x108(%r13)
movl 0x968(%r15), %eax
movl %eax, 0x110(%r13)
movslq 0x964(%r15), %rcx
leaq (%rcx,%rcx,2), %rcx
movl (%r15,%rcx,8), %ecx
movl %ecx, 0x114(%r13)
movb %r14b, 0x118(%r13)
xorl %ecx, %ecx
movb %cl, 0x119(%r13)
movb %cl, 0x11b(%r13)
testl %eax, %eax
sete %al
orb %al, %r14b
movb $0x1, %al
jne 0x1a0d3
movzbl (%rbp), %ecx
cmpl $0x23, %ecx
je 0x1a0d3
cmpl $0x5f, %ecx
jne 0x1a0cb
cmpb $0x0, 0x5(%r13)
je 0x1a0d3
movq %r13, %rdi
callq 0x1a6aa
movb %al, 0x11a(%r13)
movl $0xffffffff, 0x11c(%r13) # imm = 0xFFFFFFFF
movq 0x20(%rbx), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x138(%r13)
movups (%rax), %xmm0
movups %xmm0, 0x128(%r13)
movq (%rbx), %rax
movq %rax, 0x140(%r13)
movq %r13, (%rbx)
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
je 0x19f96
movq %r15, %rdi
movq %r13, %rsi
callq 0x19d64
testb %al, %al
jne 0x19f96
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %r8
movq 0x20(%r8), %rdi
leaq 0x5e100(%rip), %rdx     # 0x7824c
movq %r12, %rcx
xorl %eax, %eax
callq *0x10(%r8)
jmp 0x19f96
movq 0x8(%rsp), %rdx
movq %r13, 0x18(%rdx)
movq 0x18(%rbx), %rdi
callq 0x1ec6f
movb 0x50(%rsp), %al
movb %al, 0x118(%r13)
jmp 0x1a10c