// File: /workspace/github_c_makefile_star2_O2/anuwu[P]ERPLAG-Compiler/typeChecker.c
// Function: validateVar
tokenID validateVar (moduleST *baseModule , astNode *varASTNode, varST **searchedVar)
{
	int isValidVarIndex ;
	varST *locSearchedVar ;

	locSearchedVar = searchVar (baseModule, varASTNode->tok->lexeme) ;

	if (searchedVar != NULL)
		*searchedVar = locSearchedVar ;

	if (locSearchedVar == NULL)
	{
		errSemantic () ;
		#ifdef _WIN64
			printf ("In module \"%s\" at line %d, variable \"%s\" is undeclared\n", getParentModuleName (baseModule), varASTNode->tok->lineNumber, varASTNode->tok->lexeme) ;
		#endif
		#if defined __linux__ || defined __MACH__
			printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " is undeclared\n", getParentModuleName (baseModule), varASTNode->tok->lineNumber, varASTNode->tok->lexeme) ;
		#endif
		realBase->semanticError = 1 ;

		if (varASTNode->child != NULL)
			validVarIndex (baseModule, varASTNode->child) ;

		return (tokenID)0 ;
	}
	else if (locSearchedVar->datatype != TK_ARRAY)
	{
		if (varASTNode->child != NULL)
		{
			errSemantic () ;
			#ifdef _WIN64
				printf ("In module \"%s\" at line %d, primitive \"%s\" variable \"%s\" cannot be indexed\n", getParentModuleName (baseModule), varASTNode->child->tok->lineNumber, typeIDToString (locSearchedVar->datatype), locSearchedVar->lexeme) ;
			#endif
			#if defined __linux__ || defined __MACH__
				printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "primitive %s" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be indexed\n", getParentModuleName (baseModule), varASTNode->child->tok->lineNumber, typeIDToString (locSearchedVar->datatype), locSearchedVar->lexeme) ;
			#endif
			realBase->semanticError = 1 ;

			validVarIndex (baseModule, varASTNode->child) ;
		}

		return locSearchedVar->datatype ;
	}
	else if (varASTNode->child != NULL)		// It is array and has an index
	{
		isValidVarIndex = validVarIndex(baseModule, varASTNode->child) ;	// 0 if invalid static/dyanmic, TK_NUM if valid static, TK_ID if valid dynamic

		if (!isValidVarIndex)
			realBase->semanticError = 1 ;
		else if (isVarStaticArr (locSearchedVar) && isValidVarIndex == TK_NUM)		// Static array
		{
			if (!validStaticArrStaticIndex (baseModule, locSearchedVar, varASTNode->child))
				realBase->semanticError = 1 ;
		}
		
		return locSearchedVar->arrayIndices->type ;
	}
	else
		return TK_ARRAY ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rsi), %rax
movq 0x8(%rax), %rsi
callq 0xac19
movq %rax, %r15
testq %r12, %r12
je 0xcf00
movq %r15, (%r12)
testq %r15, %r15
je 0xcf74
movl 0x8(%r15), %r12d
movq 0x30(%r14), %rsi
cmpl $0x3b, %r12d
jne 0xcfc9
testq %rsi, %rsi
je 0xd035
movq %rbx, %rdi
callq 0xcd92
testl %eax, %eax
je 0xcf54
movl %eax, %r12d
movq %r15, %rdi
callq 0xcd66
cmpl $0x71, %r12d
jne 0xcf68
testl %eax, %eax
je 0xcf68
movq 0x30(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xce24
testl %eax, %eax
jne 0xcf68
leaq 0x230bd(%rip), %rax     # 0x30018
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x10(%r15), %rax
movl (%rax), %r12d
jmp 0xd039
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xd671
movq %rbx, %rdi
callq 0xb395
movq 0x8(%r14), %rcx
movl 0x10(%rcx), %edx
movq 0x8(%rcx), %rcx
leaq 0x48ad(%rip), %rdi      # 0x11845
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x2306f(%rip), %rax     # 0x30018
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x30(%r14), %rsi
testq %rsi, %rsi
je 0xd039
movq %rbx, %rdi
callq 0xcd92
jmp 0xd039
testq %rsi, %rsi
je 0xd039
xorl %eax, %eax
callq 0xd671
movq %rbx, %rdi
callq 0xb395
movq %rax, %r12
movq 0x30(%r14), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %r13d
movl 0x8(%r15), %edi
callq 0xbb77
movq (%r15), %r8
leaq 0x51d6(%rip), %rdi      # 0x121d5
movq %r12, %rsi
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x10b0
leaq 0x23002(%rip), %rax     # 0x30018
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0xcd92
movl 0x8(%r15), %r12d
jmp 0xd039
pushq $0x3b
popq %r12
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq