Пример #1
0
        protected internal Invocation(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor)
        {
            var methodReference = invocationExpression.Annotation <Mono.Cecil.MethodReference>();

            _invocationExpression = invocationExpression;

            if (methodReference != null)
            {
                MethodInfo methodInfo = null;
                Member = methodInfo = methodReference.GetActualMethod <MethodInfo>();

                if (IsInitializeArray(methodInfo))
                {
                    var first           = _invocationExpression.Arguments.First().AcceptVisitor(Visitor, ParentScope);
                    var invocation      = _invocationExpression.Arguments.Last() as NRefactory.InvocationExpression;
                    var second          = invocation.Arguments.First();
                    var memberReference = invocationExpression.Target as NRefactory.MemberReferenceExpression;
                    var target          = memberReference.Target as NRefactory.TypeReferenceExpression;
                    var type            = target.Type.GetActualType();
                    var parameters      = methodReference.Parameters;
                    return;
                }
            }

            BuildInvocation();
        }
        protected internal Invocation(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            
            var methodReference = invocationExpression.Annotation<Mono.Cecil.MethodReference>();

            _invocationExpression = invocationExpression;

            if (methodReference != null) {
                MethodInfo methodInfo = null;
                Member = methodInfo = methodReference.GetActualMethod<MethodInfo>();

                if (IsInitializeArray(methodInfo)) {
                    var first = _invocationExpression.Arguments.First().AcceptVisitor(Visitor, ParentScope);
                    var invocation = _invocationExpression.Arguments.Last() as NRefactory.InvocationExpression;
                    var second = invocation.Arguments.First();
                    var memberReference = invocationExpression.Target as NRefactory.MemberReferenceExpression;
                    var target = memberReference.Target as NRefactory.TypeReferenceExpression;
                    var type = target.Type.GetActualType();
                    var parameters = methodReference.Parameters;
                    return;
                }
            }

            BuildInvocation();
        }
Пример #3
0
        public override void VisitInvocationExpression(ICSharpCode.NRefactory.CSharp.InvocationExpression invocationExpression)
        {
            base.VisitInvocationExpression(invocationExpression);
            var result = Resolver.Resolve(invocationExpression) as InvocationResolveResult;

            if (result == null)
            {
                Trace.WriteLine(String.Format("Unknown invocation resolution at {0}", invocationExpression));
                return;
            }
            var methodDeclaringType = GetTypeOrCreateExternal(result.Member.DeclaringType);

            CheckCallForSubtype(invocationExpression.Arguments, result.Member);

            var targetDeclaringType         = GetTypeOrCreateExternal(result.TargetResult.Type);
            var currentDeclaringTypeResolve = Resolver.Resolve(invocationExpression.GetParent <TypeDeclaration>());

            if (currentDeclaringTypeResolve.IsError)
            {
                return;
            }
            var    currentMethod        = invocationExpression.GetParent <MethodDeclaration>();
            string fromReference        = currentMethod == null ? "(field initializer)" : currentMethod.Name;
            var    currentDeclaringType = (Class)GetTypeOrCreateExternal(currentDeclaringTypeResolve.Type);

            if (currentDeclaringType.IsChildOf(methodDeclaringType))
            {
                var  items  = currentDeclaringType.GetPathTo(methodDeclaringType);
                bool direct = currentDeclaringType.IsDirectChildOf(methodDeclaringType);
                foreach (var item in items)
                {
                    item.InternalReuse.Add(new Reuse(direct, ReuseType.MethodCall, result.Member.Name,
                                                     currentDeclaringType, fromReference));
                }
            }
            else if (targetDeclaringType.IsChildOf(methodDeclaringType))
            {
                var  items  = targetDeclaringType.GetPathTo(methodDeclaringType);
                bool direct = targetDeclaringType.IsDirectChildOf(methodDeclaringType);
                foreach (var item in items)
                {
                    item.InternalReuse.Add(new Reuse(direct, ReuseType.MethodCall, result.Member.Name,
                                                     currentDeclaringType, fromReference));
                }
            }

            if (result.IsVirtualCall && (currentDeclaringType == methodDeclaringType || currentDeclaringType.IsChildOf(methodDeclaringType)))
            {
                var method = CreateMethod(result.Member);
                //maybe a downcall somewhere
                foreach (var downcallCandidate in methodDeclaringType.AllDerivedTypes().Where(t => t.DeclaredMethods.Contains(method)))
                {
                    var relation = downcallCandidate.GetImmediateParent(methodDeclaringType);
                    relation.Downcalls.Add(
                        new Downcall(relation.BaseType, relation.DerivedType, method, fromReference));
                }
            }
        }
        protected internal MethodOf(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            var memberReference = invocationExpression.Arguments.First() as NRefactory.MemberReferenceExpression;
            var loadMethodTokenInvocation = memberReference.Target as NRefactory.InvocationExpression;
            var methodReference = loadMethodTokenInvocation.Arguments.First().Annotation<MethodReference>();

            _invocationExpression = invocationExpression;
            Member = methodReference.GetActualMethod<MethodInfo>();
            InternalType = typeof(RuntimeMethodHandle);
        }
Пример #5
0
        private object EvaluateInvocationExpression(ICSharpCode.NRefactory.CSharp.InvocationExpression invocationExpression, CodeContext codeContext)
        {
            List <object> argumentValues = new List <object>();

            foreach (var unevaluated in invocationExpression.Arguments)
            {
                argumentValues.Add(EvaluateExpression(unevaluated, codeContext));
            }

            return(EvaluateInvocationExpression(invocationExpression.Target, argumentValues, codeContext));
        }
        protected internal MethodOf(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor)
        {
            var memberReference           = invocationExpression.Arguments.First() as NRefactory.MemberReferenceExpression;
            var loadMethodTokenInvocation = memberReference.Target as NRefactory.InvocationExpression;
            var methodReference           = loadMethodTokenInvocation.Arguments.First().Annotation <MethodReference>();

            _invocationExpression = invocationExpression;
            Member       = methodReference.GetActualMethod <MethodInfo>();
            InternalType = typeof(RuntimeMethodHandle);
        }
Пример #7
0
        private StringBuilder Refract(MethodDefinition m, InvocationExpression i)
        {
            Debug.Assert(i.Arguments.Count == 3);
            var result = new StringBuilder();

            var args = i.Arguments.ToArray();
            var a1   = args[1];

            args[1] = args[2];
            args[2] = a1;

            return(result.Append("refract(").Append(ArgsToString(args)).Append(")"));
        }
Пример #8
0
        private StringBuilder TextureLod(MethodDefinition m, InvocationExpression i)
        {
            Debug.Assert(i.Arguments.Count == 3);
            var args    = i.Arguments.ToArray();
            var sampler = args[0];
            var pos     = args[1];
            var lod     = args[2];

            var tref   = ShaderDefinition.ToCecil(typeof(ShaderDefinition.vec4));
            var zero   = new PrimitiveExpression("0.0f");
            var newPos = new ObjectCreateExpression(AstBuilder.ConvertType(tref), new[] { pos.Clone(), zero, lod.Clone() });

            var result = new StringBuilder();

            return(result.Append("tex2Dlod(").Append(ArgsToString(new[] { sampler, newPos })).Append(")"));
        }
Пример #9
0
        private bool TryHandleAnonymousMethod(NRefactory.Expression @this, NRefactory.InvocationExpression func, out Expression outExpression)
        {
            MethodReference methodReference = null;

            NRefactory.IdentifierExpression methodIdentifier = null;

            outExpression = null;

            if (func != null)
            {
                methodIdentifier = func.Arguments.Single() as NRefactory.IdentifierExpression;
                methodReference  = methodIdentifier.Annotation <MethodReference>();

                if (methodReference != null)
                {
                    object   target     = null;
                    Delegate @delegate  = null;
                    var      methodInfo = methodReference.GetActualMethod() as MethodInfo;

                    if (!methodInfo.IsStatic)
                    {
                        var context = RootScope.Context;

                        if (context != null)
                        {
                            target = context.Value;
                        }
                    }

                    @delegate     = methodInfo.CreateDelegate(InternalType, target);
                    outExpression = Expression.Constant(@delegate);
                    return(true);
                }
            }

            return(false);
        }
 public static Invocation Invocation(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
 {
     return(new Invocation(invocationExpression, scope, visitor));
 }
Пример #11
0
 private StringBuilder KeywordDiscard(MethodDefinition unused1, InvocationExpression unused2)
 {
     return(new StringBuilder("discard"));
 }
 public static MethodOf MethodOf(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
 {
     return(new MethodOf(invocationExpression, scope, visitor));
 }
Пример #13
0
 public override AstExpression VisitInvocationExpression(NRefactory.InvocationExpression invocationExpression, IScope scope)
 {
     return(AstExpression.Invocation(invocationExpression, scope, this));
 }
Пример #14
0
        public static bool TryGetFormattingParameters(CSharpInvocationResolveResult invocationResolveResult, InvocationExpression invocationExpression,
                                                      out Expression formatArgument, out IList <Expression> arguments,
                                                      Func <IParameter, Expression, bool> argumentFilter)
        {
            if (argumentFilter == null)
            {
                argumentFilter = (p, e) => true;
            }

            formatArgument = null;
            arguments      = new List <Expression>();

            // Serach for method of type: void Name(string format, params object[] args);
            if (invocationResolveResult.Member.SymbolKind == SymbolKind.Method)
            {
                var methods = invocationResolveResult.Member.DeclaringType.GetMethods(m => m.Name == invocationResolveResult.Member.Name).ToList();
                if (!methods.Any(m => m.Parameters.Count == 2 &&
                                 m.Parameters[0].Type.IsKnownType(KnownTypeCode.String) && parameterNames.Contains(m.Parameters[0].Name) &&
                                 m.Parameters[1].IsParams))
                {
                    return(false);
                }
            }

            var argumentToParameterMap = invocationResolveResult.GetArgumentToParameterMap();
            var resolvedParameters     = invocationResolveResult.Member.Parameters;
            var allArguments           = invocationExpression.Arguments.ToArray();

            for (int i = 0; i < allArguments.Length; i++)
            {
                var parameterIndex = argumentToParameterMap[i];
                if (parameterIndex < 0 || parameterIndex >= resolvedParameters.Count)
                {
                    // No valid mapping for this argument, skip it
                    continue;
                }
                var parameter = resolvedParameters[parameterIndex];
                var argument  = allArguments[i];
                if (i == 0 && parameter.Type.IsKnownType(KnownTypeCode.String) && parameterNames.Contains(parameter.Name))
                {
                    formatArgument = argument;
                }
                else if (formatArgument != null && parameter.IsParams && !invocationResolveResult.IsExpandedForm)
                {
                    var ace = argument as ArrayCreateExpression;
                    if (ace == null || ace.Initializer.IsNull)
                    {
                        return(false);
                    }
                    foreach (var element in ace.Initializer.Elements)
                    {
                        if (argumentFilter(parameter, element))
                        {
                            arguments.Add(argument);
                        }
                    }
                }
                else if (formatArgument != null && argumentFilter(parameter, argument))
                {
                    arguments.Add(argument);
                }
            }
            return(formatArgument != null);
        }