// File: /workspace/github_c_makefile_star2_O3/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 0xb8e8
movq %rax, %r15
testq %r12, %r12
je 0xe1d5
movq %r15, (%r12)
testq %r15, %r15
je 0xe249
movl 0x8(%r15), %r12d
movq 0x30(%r14), %rsi
cmpl $0x3b, %r12d
jne 0xe29e
testq %rsi, %rsi
je 0xe30a
movq %rbx, %rdi
callq 0xe063
testl %eax, %eax
je 0xe229
movl %eax, %r12d
movq %r15, %rdi
callq 0xe01a
cmpl $0x71, %r12d
jne 0xe23d
testl %eax, %eax
je 0xe23d
movq 0x30(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe0f7
testl %eax, %eax
jne 0xe23d
leaq 0x22df8(%rip), %rax     # 0x31028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x10(%r15), %rax
movl (%rax), %r12d
jmp 0xe310
xorl %r12d, %r12d
xorl %eax, %eax
callq 0xe989
movq %rbx, %rdi
callq 0xc0ec
movq 0x8(%r14), %rcx
movl 0x10(%rcx), %edx
movq 0x8(%rcx), %rcx
leaq 0x45e8(%rip), %rdi      # 0x12855
movq %rax, %rsi
xorl %eax, %eax
callq 0x10b0
leaq 0x22daa(%rip), %rax     # 0x31028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x30(%r14), %rsi
testq %rsi, %rsi
je 0xe310
movq %rbx, %rdi
callq 0xe063
jmp 0xe310
testq %rsi, %rsi
je 0xe310
xorl %eax, %eax
callq 0xe989
movq %rbx, %rdi
callq 0xc0ec
movq %rax, %r12
movq 0x30(%r14), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %r13d
movl 0x8(%r15), %edi
callq 0xc9b3
movq (%r15), %r8
leaq 0x4f01(%rip), %rdi      # 0x131d5
movq %r12, %rsi
movl %r13d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x10b0
leaq 0x22d3d(%rip), %rax     # 0x31028
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq 0x30(%r14), %rsi
movq %rbx, %rdi
callq 0xe063
movl 0x8(%r15), %r12d
jmp 0xe310
movl $0x3b, %r12d
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq