// File: /workspace/github_c_makefile_star2_O3/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 0x14dc9
cmpb $0x1, 0x30(%r9)
jne 0x14dc9
movq 0x38(%r9), %rax
movq 0x108(%rax), %r9
jmp 0x14dcc
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 0x14a85
addq $0x10, %rsp
testq %rax, %rax
je 0x14f47
movq %rax, %r13
movl 0x114(%rax), %eax
movslq 0x964(%r15), %rcx
leaq (%rcx,%rcx,2), %rcx
cmpl (%r15,%rcx,8), %eax
jne 0x14e8f
movl (%r13), %eax
testl %eax, %eax
je 0x14ebf
cmpl $0x2, %eax
jne 0x14f31
cmpl $0x2, %ebp
jne 0x14f31
movq 0x108(%r13), %rcx
cmpl $0x1, (%rcx)
jne 0x14f31
movq 0x8(%rsp), %rax
cmpl $0x1, (%rax)
je 0x14f31
movq 0x18(%rcx), %rdi
addq $0x4, %rdi
movq %rcx, 0x10(%rsp)
movq %r12, %rsi
callq 0x3210
movq 0x10(%rsp), %rsi
testl %eax, %eax
jne 0x14f31
movq 0x8(%rsp), %rdx
movq %r13, 0x18(%rdx)
movq 0x18(%rbx), %rdi
callq 0x19c98
movb %r14b, 0x118(%r13)
jmp 0x150c1
movzbl (%r12), %ecx
cmpl $0x23, %ecx
je 0x14f47
cmpl $0x5f, %ecx
jne 0x150d3
testl %eax, %eax
je 0x14f47
cmpb $0x0, 0x1(%r12)
jne 0x150db
jmp 0x14f47
testl %ebp, %ebp
jne 0x14f31
movq 0x108(%r13), %rcx
cmpl $0x1a, (%rcx)
jne 0x14f31
movq 0x8(%rsp), %rax
cmpl $0x1a, (%rax)
jne 0x14f31
cmpb %r14b, 0x118(%r13)
jne 0x14f31
movq %rcx, 0x10(%rsp)
movq %r13, %rdi
addq $0x4, %rdi
movq %r12, %rsi
callq 0x3210
movq 0x10(%rsp), %rdi
testl %eax, %eax
jne 0x14f31
movq 0x8(%rsp), %rsi
callq 0x1a220
testb %al, %al
je 0x14f31
cmpl $0x0, 0x11c(%r13)
js 0x14f31
movq 0x108(%r13), %rsi
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rdx
callq 0x19c98
jmp 0x150c1
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x622f4(%rip), %rsi     # 0x77234
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
testb %r14b, %r14b
je 0x14f6c
cmpl $0x0, 0x964(%r15)
je 0x14f6c
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x622fe(%rip), %rsi     # 0x77263
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
cmpl $0x1, %ebp
ja 0x14fb4
movq 0x8(%rsp), %r13
movl (%r13), %eax
cmpl $0x1, %eax
jne 0x14f99
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x622fc(%rip), %rsi     # 0x7728a
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movl (%r13), %eax
cmpl $0x2, %eax
jne 0x14fb4
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x62308(%rip), %rsi     # 0x772b5
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x18(%rbx), %rdi
movl $0x148, %esi            # imm = 0x148
callq 0x7a70
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 0x1500c
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 0x14ff3
jmp 0x15011
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 0x15088
movzbl (%rbp), %ecx
cmpl $0x23, %ecx
je 0x15088
cmpl $0x5f, %ecx
jne 0x15080
cmpb $0x0, 0x5(%r13)
je 0x15088
movq %r13, %rdi
callq 0x1563b
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 0x14f47
movq %r15, %rdi
movq %r13, %rsi
callq 0x14d10
testb %al, %al
jne 0x14f47
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %r8
movq 0x20(%r8), %rdi
leaq 0x6214b(%rip), %rdx     # 0x7724c
movq %r12, %rcx
xorl %eax, %eax
callq *0x10(%r8)
jmp 0x14f47