public override void OnMethodInvocationExpression(MethodInvocationExpression node) { if (node.Target.Entity != null && node.Target.Entity == TypeSystemServices.ICallableType.GetMembers().Single(m => m.Name == "Call")) { var mreOriginal = (MemberReferenceExpression)node.Target; var newMethod = TypeSystemServices.Map(typeof(Delegate).GetMethod("DynamicInvoke", BindingFlags.Instance | BindingFlags.Public)); var newTarget = new MemberReferenceExpression(mreOriginal.Target, newMethod.Name); newTarget.Entity = newMethod; node.Replace(node.Target, newTarget); } else if (node.Target.Entity != null && node.Target.Entity.EntityType == EntityType.Method) { var parameters = ((IMethodBase)node.Target.Entity).GetParameters(); var args = node.Arguments.ToArray(); for (int i = 0; i < args.Length && i < parameters.Length; i++) { if (args[i].ExpressionType != null && TypeSystemServices.IsCallable(args[i].ExpressionType) && !TypeSystemServices.IsCallable(parameters[i].Type)) { node.Replace(args[i], InjectCast(args[i])); } } } base.OnMethodInvocationExpression(node); }
public override void OnReferenceExpression(ReferenceExpression node) { IEntity entity = NameResolutionService.Resolve(node.Name); if (entity != null) { base.OnReferenceExpression(node); return; } if (node.Name.StartsWith("@")) { string refComponentName = node.Name.Substring(1); StringLiteralExpression literal = CodeBuilder.CreateStringLiteral(refComponentName); ExternalConstructor constructor = new ExternalConstructor(TypeSystemServices, _componentReferenceConstructor); MethodInvocationExpression invocation = CodeBuilder.CreateConstructorInvocation(constructor, literal); node.ParentNode.Replace(node, invocation); return; } else if (node.ParentNode is MethodInvocationExpression) { MethodInvocationExpression mie = (MethodInvocationExpression)node.ParentNode; //Transform the first parameter of Component ctor to string. if (mie.Target.ToString() == "Component") { StringLiteralExpression literal = CodeBuilder.CreateStringLiteral(node.Name); mie.Replace(node, literal); return; } } base.OnReferenceExpression(node); }
public override void OnMethodInvocationExpression(MethodInvocationExpression node) { var target = node.Target as ReferenceExpression; if (target != null && target.Name == "Array" && target.Entity.EntityType == EntityType.Constructor) { var invokedMethod = (IMethodBase)node.Target.Entity; var parameters = invokedMethod.GetParameters(); if (parameters.Length == 0) { //if we have no parameters we are instantiating an "empty" array which probably will be populated by through the "Add" method. //this will not work (user will get compilation errors in C#), but lets not crash here anyway. var array = new GenericReferenceExpression { Target = new ReferenceExpression("array"), GenericArguments = TypeReferenceCollection.FromArray(CodeBuilder.CreateTypeReference(TypeSystemServices.ObjectType)) }; node.Replace(node.Target, array); node.Arguments.Add(CodeBuilder.CreateIntegerLiteral(0)); } else if (parameters.Length == 1 && parameters[0].Type == TypeSystemServices.IntType) { //if we have one parameter of type int, we are invoking the ctor with capacity, which is equivalent to instantiate an array with such size var array = new GenericReferenceExpression { Target = new ReferenceExpression("array"), GenericArguments = TypeReferenceCollection.FromArray(CodeBuilder.CreateTypeReference(TypeSystemServices.ObjectType)) }; node.Replace(node.Target, array); } else { if (node.Arguments[0].ExpressionType == TypeSystemServices.IEnumerableType) { Context.Errors.Add(CompilerErrorFactory.InvalidArray(node)); } ReplaceCurrentNode(node.Arguments[0]); } } base.OnMethodInvocationExpression(node); }
private void HandleUnityScriptLangUtilsParseXInvocations(MethodInvocationExpression node, IType targetType, ReferenceExpression targetTypeAsExpression) { if (node.Arguments[0].ExpressionType == TypeSystemServices.StringType) { var parseMethod = targetType.GetMembers().OfType <IMethod>().Single(m => m.Name == "Parse" && m.GetParameters().Length == 1); node.Replace(node.Target, CodeBuilder.CreateMemberReference(targetTypeAsExpression, parseMethod)); } else if (node.Arguments[0].ExpressionType != targetType) { Console.WriteLine($"Warning: call to {node} ({node.LexicalInfo}) should be translated to `checked( ({targetType.Name}) {node.Arguments[0]});` but the converter does not support `checked expressions`. Consider adding it manually to the generated code. "); ReplaceCurrentNode(CodeBuilder.CreateCast(targetType, node.Arguments[0])); } else { ReplaceCurrentNode(node.Arguments[0]); } }
public override void OnMethodInvocationExpression(MethodInvocationExpression node) { var original = node.Target; base.OnMethodInvocationExpression(node); var member = node.Target.Entity as IMember; if (member == null) { return; } if (member.DeclaringType is ICallableType && node.Target.ToCodeString().Contains("Invoke")) { // Convert explicit delegate Invoke() method invocation to method invocation syntax // i.e: d.Invoke(p1, p2) => d(p1, p2); node.Replace(node.Target, original); } }
public override void OnMethodInvocationExpression(MethodInvocationExpression node) { base.OnMethodInvocationExpression(node); if (node.Target.Entity == null || (node.Target.Entity.EntityType != EntityType.Method && node.Target.Entity.EntityType != EntityType.Constructor && node.Target.Entity.EntityType != EntityType.Property)) { return; } var parameters = ((IMethodBase)node.Target.Entity).GetParameters(); for (int i = 0; i < node.Arguments.Count && i < parameters.Length; i++) { var arg = node.Arguments[i]; var param = parameters[i]; if (arg.ExpressionType.IsEnum ^ param.Type.IsEnum) { node.Replace(arg, CodeBuilder.CreateCast(param.Type, arg)); } } }