// File: /workspace/github_c_makefile_star2_O2/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 0x1052e
cmpb $0x1, 0x30(%r9)
jne 0x1052e
movq 0x38(%r9), %rax
movq 0x108(%rax), %r9
jmp 0x10531
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 0x101f1
addq $0x10, %rsp
testq %rax, %rax
je 0x106ac
movq %rax, %r13
movl 0x114(%rax), %eax
movslq 0x964(%r15), %rcx
imulq $0x18, %rcx, %rcx
cmpl (%r15,%rcx), %eax
jne 0x105f4
movl (%r13), %eax
testl %eax, %eax
je 0x10624
cmpl $0x2, %eax
jne 0x10696
cmpl $0x2, %ebp
jne 0x10696
movq 0x108(%r13), %rcx
cmpl $0x1, (%rcx)
jne 0x10696
movq 0x8(%rsp), %rax
cmpl $0x1, (%rax)
je 0x10696
movq 0x18(%rcx), %rdi
addq $0x4, %rdi
movq %rcx, 0x10(%rsp)
movq %r12, %rsi
callq 0x3210
movq 0x10(%rsp), %rsi
testl %eax, %eax
jne 0x10696
movq 0x8(%rsp), %rdx
movq %r13, 0x18(%rdx)
movq 0x18(%rbx), %rdi
callq 0x14fa7
movb %r14b, 0x118(%r13)
jmp 0x107fe
movzbl (%r12), %ecx
cmpl $0x23, %ecx
je 0x106ac
cmpl $0x5f, %ecx
jne 0x10810
testl %eax, %eax
je 0x106ac
cmpb $0x0, 0x1(%r12)
jne 0x10818
jmp 0x106ac
testl %ebp, %ebp
jne 0x10696
movq 0x108(%r13), %rcx
cmpl $0x1a, (%rcx)
jne 0x10696
movq 0x8(%rsp), %rax
cmpl $0x1a, (%rax)
jne 0x10696
cmpb %r14b, 0x118(%r13)
jne 0x10696
movq %rcx, 0x10(%rsp)
movq %r13, %rdi
addq $0x4, %rdi
movq %r12, %rsi
callq 0x3210
movq 0x10(%rsp), %rdi
testl %eax, %eax
jne 0x10696
movq 0x8(%rsp), %rsi
callq 0x155ab
testb %al, %al
je 0x10696
cmpl $0x0, 0x11c(%r13)
js 0x10696
movq 0x108(%r13), %rsi
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rdx
callq 0x14fa7
jmp 0x107fe
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5ab2f(%rip), %rsi     # 0x6b1d4
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
testb %r14b, %r14b
je 0x106d1
cmpl $0x0, 0x964(%r15)
je 0x106d1
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5ab39(%rip), %rsi     # 0x6b203
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
cmpl $0x1, %ebp
ja 0x10717
movq 0x8(%rsp), %r14
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x106fc
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5ab38(%rip), %rsi     # 0x6b22a
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movl (%r14), %eax
cmpl $0x2, %eax
jne 0x10717
movq 0x28(%rbx), %rcx
movq 0x20(%rcx), %rdi
leaq 0x5ab45(%rip), %rsi     # 0x6b255
movq %r12, %rdx
xorl %eax, %eax
callq *(%rcx)
movq 0x18(%rbx), %rdi
movl $0x148, %esi            # imm = 0x148
callq 0x4144
movq %rax, %r13
movl %ebp, (%rax)
leaq 0x4(%rax), %rbp
movl $0xff, %edx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3080
xorl %r14d, %r14d
movb %r14b, 0x103(%r13)
movq %r12, %rdi
callq 0x10cf5
movl %eax, 0x104(%r13)
movq 0x8(%rsp), %rax
movq %rax, 0x108(%r13)
movl 0x968(%r15), %eax
movl %eax, 0x110(%r13)
movslq 0x964(%r15), %rcx
imulq $0x18, %rcx, %rcx
movl (%r15,%rcx), %ecx
movl %ecx, 0x114(%r13)
movb 0x50(%rsp), %cl
movb %cl, 0x118(%r13)
movb %r14b, 0x119(%r13)
movb %r14b, 0x11b(%r13)
testl %eax, %eax
sete %al
orb %al, %cl
movb $0x1, %al
jne 0x107c8
movzbl (%rbp), %ecx
cmpl $0x23, %ecx
je 0x107c8
cmpl $0x5f, %ecx
jne 0x107c0
cmpb $0x0, 0x5(%r13)
je 0x107c8
movq %r13, %rdi
callq 0x10cad
movb %al, 0x11a(%r13)
orl $-0x1, 0x11c(%r13)
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 0x106ac
movq %r15, %rdi
movq %r13, %rsi
callq 0x1047a
testb %al, %al
jne 0x106ac
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %r8
movq 0x20(%r8), %rdi
leaq 0x5a9ae(%rip), %rdx     # 0x6b1ec
movq %r12, %rcx
xorl %eax, %eax
callq *0x10(%r8)
jmp 0x106ac