private IValue doFunctionCall(ParserRuleContext context, string targetFunctionName, List <IValue> parameters, IValue target, ClepsType targetType, bool allowVoidReturn)
        {
            IValue         dereferencedTarget = target == null? null : GetDereferencedRegisterOrNull(target);
            BasicClepsType dereferencedType   = target == null? targetType as BasicClepsType : dereferencedTarget.ExpressionType as BasicClepsType;

            if (dereferencedType == null)
            {
                string errorMessage = String.Format("Could not dereference expression on type {0}", targetType.GetClepsTypeString());
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //just return something to avoid stalling
                return(CodeGenerator.CreateByte(0));
            }

            ClepsClass targetClepsClass = ClassManager.GetClass(dereferencedType.GetClepsTypeString());


            List <ClepsVariable> functionOverloads;
            bool isStatic;

            if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName))
            {
                isStatic          = true;
                functionOverloads = targetClepsClass.StaticMemberMethods[targetFunctionName];
            }
            else if (target != null && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName))
            {
                isStatic          = false;
                functionOverloads = targetClepsClass.MemberMethods[targetFunctionName];
            }
            else
            {
                string errorMessage = String.Format("Class {0} does not contain a {1}static function called {2}.", targetClepsClass.FullyQualifiedName, target == null? "" : "member or ", targetFunctionName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            int    matchedPosition;
            string fnMatchErrorMessage;

            if (!FunctionOverloadManager.FindMatchingFunctionType(TypeManager, functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage))
            {
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, fnMatchErrorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition].VariableType as FunctionClepsType;

            if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidClepsType.GetVoidType())
            {
                string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(isStatic? null : dereferencedTarget, dereferencedType, targetFunctionName, chosenFunctionType, parameters);

            return(returnValue);
        }
        private IValue doFunctionCall(ClepsParser.FunctionCallContext functionCall, ClepsType targetType, bool isMemberFunctionsAccessible, bool allowVoidReturn)
        {
            ClepsClass targetClepsClass;

            if (targetType is BasicClepsType)
            {
                targetClepsClass = ClassManager.GetClass(targetType.GetClepsTypeString());
            }
            else if (targetType is BasicStaticClepsType)
            {
                targetClepsClass            = ClassManager.GetClass(targetType.GetClepsTypeString());
                isMemberFunctionsAccessible = false;
            }
            else
            {
                throw new NotImplementedException("Function calls on non basic types not supported");
            }

            string        targetFunctionName = functionCall.FunctionName.GetText();
            List <IValue> parameters         = functionCall._FunctionParameters.Select(p => Visit(p) as IValue).ToList();

            ClepsType functionType = null;

            if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName))
            {
                functionType = targetClepsClass.StaticMemberMethods[targetFunctionName];
            }
            else if (isMemberFunctionsAccessible && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName))
            {
                functionType = targetClepsClass.MemberMethods[targetFunctionName];
            }

            if (functionType == null)
            {
                string errorMessage = String.Format("Class {0} does not contain a function called {1}", targetClepsClass.FullyQualifiedName, targetFunctionName);
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            List <ClepsType> functionOverloads = new List <ClepsType> {
                functionType
            };

            int    matchedPosition;
            string fnMatchErrorMessage;

            if (!FunctionOverloadManager.FindMatchingFunctionType(functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage))
            {
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, fnMatchErrorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition] as FunctionClepsType;

            if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidType.GetVoidType())
            {
                string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName);
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(FullyQualifiedClassName, targetFunctionName, chosenFunctionType, parameters);

            return(returnValue);
        }