Example #1
0
        public Expression CallOperator(Source src, DataType dt, string op, params Expression[] args)
        {
            var fc   = new FunctionCompiler(_compiler, dt, null);
            var type = Operator.Parse(args.Length, op);

            foreach (var m in dt.Operators)
            {
                if (m.Type == type && fc.IsArgumentListCompatible(m.Parameters, args))
                {
                    switch (args.Length)
                    {
                    case 1:
                        return(new CallUnOp(src, m, args[0]));

                    case 2:
                        return(new CallBinOp(src, m, args[0], args[1]));
                    }
                }
            }

            foreach (var m in dt.Operators)
            {
                if (m.Type == type)
                {
                    if (fc.IsArgumentListCompatibleUsingImplicitCasts(m.Parameters, args))
                    {
                        fc.ApplyImplicitCastsOnArgumentList(m.Parameters, args);

                        switch (args.Length)
                        {
                        case 1:
                            return(new CallUnOp(src, m, args[0]));

                        case 2:
                            return(new CallBinOp(src, m, args[0], args[1]));
                        }
                    }
                }
            }

            var pl    = "(";
            var first = true;

            foreach (var a in args)
            {
                if (!first)
                {
                    pl += ",";
                }
                pl   += a.ReturnType;
                first = false;
            }
            pl += ")";

            Log.Error(src, ErrorCode.E0000, "No matching operator overload: '" + dt + "." + op + pl + "'");
            return(Expression.Invalid);
        }
Example #2
0
        public Expression CallMethod(Source src, DataType dt, Expression obj, string method, params Expression[] args)
        {
            var fc = new FunctionCompiler(_compiler, dt, obj);
            var mg = fc.CompileExpression(Parser.ParseExpression(Log, src, method));

            if (mg.IsInvalid)
            {
                return(mg);
            }

            if (mg is MethodGroup)
            {
                foreach (var m in (mg as MethodGroup).Candidates)
                {
                    if (fc.IsArgumentListCompatible(m.Parameters, args))
                    {
                        return(new CallMethod(src, obj, m, args));
                    }
                }

                foreach (var m in (mg as MethodGroup).Candidates)
                {
                    var processedArgs = fc.TryApplyDefaultValuesOnArgumentList(src, m.Parameters, args);

                    if (fc.IsArgumentListCompatibleUsingImplicitCasts(m.Parameters, processedArgs))
                    {
                        fc.ApplyImplicitCastsOnArgumentList(m.Parameters, processedArgs);
                        return(new CallMethod(src, obj, m, processedArgs));
                    }
                }
            }

            var pl    = "(";
            var first = true;

            foreach (var a in args)
            {
                if (!first)
                {
                    pl += ",";
                }
                pl   += a.ReturnType;
                first = false;
            }
            pl += ")";

            Log.Error(src, ErrorCode.E0000, "No matching method overload: '" + dt + "." + method + pl + "'");
            return(Expression.Invalid);
        }
Example #3
0
        public Expression NewObject(Source src, DataType type, params Expression[] args)
        {
            bool found = false;
            var  fc    = new FunctionCompiler(_compiler, _il);

            type.PopulateMembers();

            foreach (var m in type.Constructors)
            {
                if (m.IsPublic)
                {
                    if (fc.IsArgumentListCompatible(m.Parameters, args))
                    {
                        return(new NewObject(src, m, args));
                    }

                    found = true;
                }
            }

            if (found)
            {
                foreach (var m in type.Constructors)
                {
                    if (m.IsPublic)
                    {
                        var processedArgs = fc.TryApplyDefaultValuesOnArgumentList(src, m.Parameters, args);

                        if (fc.IsArgumentListCompatibleUsingImplicitCasts(m.Parameters, processedArgs))
                        {
                            fc.ApplyImplicitCastsOnArgumentList(m.Parameters, processedArgs);
                            return(new NewObject(src, m, processedArgs));
                        }
                    }
                }
            }

            Log.Error(src, ErrorCode.E0000, "Constructor not found: '" + type + "..ctor'");
            return(Expression.Invalid);
        }