コード例 #1
0
        private bool FindFunctionTypeForVariable(IVariable variable, Expression code, bool MakeNewStuffUp)
        {
            bool variableIsFunction = variable as Function != null;
            bool variableIsFuncType = variable.DataType.IsFuncType();
            bool variableIsDelegate = variable.DataType.IsDelegate();

            foreach (var e in code.GetChildExpressions())
            {
                Expression otherExpression = null;
                IVariable  expVariable     = null;
                if (e.ExpressionType == Instruction.CALLFUNC2 && variableIsFuncType)
                {
                    bool isVoid = e.IsVoidContext();
                    if (!isVoid)
                    {
                        otherExpression = e.GetOtherSideOfBinaryExpression();
                    }
                    IVariable otherExpressionVariable = null;
                    if (otherExpression != null)
                    {
                        otherExpressionVariable = otherExpression.Variable;
                    }

                    DataType otherExpressionDataType   = DataType.Void;
                    int      otherExpressionStructType = -1;
                    if (otherExpressionVariable != null)
                    {
                        otherExpressionDataType   = otherExpressionVariable.DataType;
                        otherExpressionStructType = otherExpressionVariable.StructType;
                    }
                    else
                    {
                        if (isVoid)
                        {
                            otherExpressionDataType = DataType.Void;
                        }
                        else
                        {
                            otherExpressionDataType = DataType.AnyNonVoidType;
                        }
                    }

                    var argExpressions    = (e.Arg3 ?? Expression.Empty).FlattenContainerExpression(Instruction.Comma).ToArray();
                    var variables         = GetVariablesFromExpressions(argExpressions);
                    var matchingFuncTypes = ainFile.MatchingFunctionTypes(otherExpressionDataType, otherExpressionStructType, variables, variables.Length).Distinct().ToArray();

                    var firstMatchingFuncType = matchingFuncTypes.FirstOrDefault();
                    if (firstMatchingFuncType != null)
                    {
                        if (MakeNewStuffUp || matchingFuncTypes.Skip(1).FirstOrDefault() == null)
                        {
                            variable.StructType = firstMatchingFuncType.Index;
                        }
                    }
                }
                if (variableIsFunction && e.ExpressionType == Instruction.RETURN)
                {
                    otherExpression = e.Arg1;
                    expVariable     = variable;
                }
                else
                {
                    expVariable = e.Variable;
                    if (variable == expVariable)
                    {
                        otherExpression = e.GetOtherSideOfBinaryExpression();
                    }
                }

                if (otherExpression != null)
                {
                    Function otherFunction = null;
                    if (otherExpression.ExpressionType == Instruction.PUSH)
                    {
                        otherFunction = ainFile.GetFunction(otherExpression.Value);
                    }
                    else if (otherExpression.ExpressionType == Instruction.S_PUSH)
                    {
                        otherFunction = ainFile.GetFunction(ainFile.GetString(otherExpression.Value));
                    }
                    else
                    {
                        var otherVariable = otherExpression.Variable;
                        if (otherVariable != null && !variableIsDelegate && otherVariable.DataType.IsFuncType() && otherVariable.StructType != -1)
                        {
                            variable.StructType = otherVariable.StructType;
                            return(true);
                        }
                        if (otherVariable != null && variableIsDelegate && otherVariable.DataType.IsDelegate() && otherVariable.StructType != -1)
                        {
                            variable.StructType = otherVariable.StructType;
                            return(true);
                        }
                    }
                    if (otherFunction != null)
                    {
                        FunctionType matchingFuncType = null;
                        if (!variableIsDelegate)
                        {
                            if (!MakeNewStuffUp)
                            {
                                matchingFuncType = ainFile.GetFuncTypeUnique(otherFunction);
                            }
                            else
                            {
                                matchingFuncType = ainFile.GetFuncType(otherFunction);
                            }
                            if (matchingFuncType != null)
                            {
                                variable.StructType = matchingFuncType.Index;
                                return(true);
                            }
                        }
                        else
                        {
                            if (!MakeNewStuffUp)
                            {
                                matchingFuncType = ainFile.GetDelegateUnique(otherFunction);
                            }
                            else
                            {
                                matchingFuncType = ainFile.GetDelegateType(otherFunction);
                            }
                            if (matchingFuncType != null)
                            {
                                variable.StructType = matchingFuncType.Index;
                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    var functionParameter = e.GetFunctionCallParameter();
                    if (functionParameter != null)
                    {
                        if (functionParameter != null && !variableIsDelegate && functionParameter.DataType.IsFuncType() && functionParameter.StructType != -1)
                        {
                            variable.StructType = functionParameter.StructType;
                            return(true);
                        }
                        if (functionParameter != null && variableIsDelegate && functionParameter.DataType.IsDelegate() && functionParameter.StructType != -1)
                        {
                            variable.StructType = functionParameter.StructType;
                            return(true);
                        }
                    }
                    else if (variable.DataType.IsArray())
                    {
                        var parent = e.Parent;
                        if (parent != null && (parent.ExpressionType == Instruction.A_PUSHBACK || parent.ExpressionType == Instruction.A_INSERT))
                        {
                            var       arg2 = parent.Arg2;
                            IVariable var2 = null;
                            if (arg2 != null)
                            {
                                var2 = arg2.Variable;
                            }
                            if (var2 != null)
                            {
                                if (!variableIsDelegate && var2.DataType.IsFuncType() && var2.StructType != -1)
                                {
                                    variable.StructType = var2.StructType;
                                    return(true);
                                }
                                if (variableIsDelegate && var2.DataType.IsDelegate() && var2.StructType != -1)
                                {
                                    variable.StructType = var2.StructType;
                                    return(true);
                                }
                            }
                        }
                    }
                    else if (e.ExpressionType == Instruction.CALLFUNC2)
                    {
                    }
                }
            }
            return(false);
        }