Example #1
0
 /// <summary>
 /// Initializes new instance of <see cref="MethodBodyGenerator"/>
 /// </summary>
 public MethodBodyGenerator(IMethodBase method, System.Reflection.Emit.ILGenerator generator, bool emitInfo = false) : base(generator, emitInfo)
 {
     Method     = method;
     ReturnType = method.ReturnType;
     Context    = method.Context;
     SyntaxTree = method.SyntaxBody;
 }
        protected override void visitMethodBase(IMethodBase method)
        {
            if (method is Constructor const_)
            {
                const_.parentClass = ((Class)this.currentInterface);
            }
            else if (method is Method meth)
            {
                meth.parentInterface = this.currentInterface;
            }
            else if (method is GlobalFunction)
            {
            }
            else if (method is Lambda)
            {
            }
            else
            {
            }

            foreach (var param in method.parameters)
            {
                param.parentMethod = method;
            }

            this.parentNodeStack.push(method);
            base.visitMethodBase(method);
            this.parentNodeStack.pop();
        }
        private bool HandleGetComponent(MethodInvocationExpression node, IMethodBase method)
        {
            if ((method.Name != "GetComponent" && method.Name != "AddComponent") || node.Arguments.Count == 0)
            {
                return(false);
            }

            if (node.Arguments[0].ExpressionType != TypeSystemServices.StringType)
            {
                return(false);
            }

            var be = node.ParentNode as BinaryExpression;

            if (be != null && be.Operator == BinaryOperatorType.Assign && be.Right == node)
            {
                node.ParentNode.Replace(be.Right, CodeBuilder.CreateCast(be.Left.ExpressionType, be.Right));
                return(true);
            }

            var castTarget = FindRelatedParameterDefinition(node.ParentNode, node);

            if (castTarget != null)
            {
                node.ParentNode.Replace(node, CodeBuilder.CreateCast(castTarget, node));
            }

            return(false);
        }
Example #4
0
        private bool IsCalledByMethod(IMethodBody callerMethodBody, IMethodBase subjectMethod)
        {
            var localInvokedMethods     = GetLocalInvokedMethods(callerMethodBody);
            var localInvokedMethodsName = localInvokedMethods.Select(a => a.Identifier.GetField());

            return(localInvokedMethodsName.Any(a => a.Equals(subjectMethod.Name)));
        }
Example #5
0
 public override TypeName [] GetParameterTypes(IMethodBase method)
 {
     return(method.Value().Parameters.Select(p => new TypeName()
     {
         Namespace = p.ParameterType.GetCorrectNamespace(), Name = p.ParameterType.GetCorrectName()
     }).ToArray());
 }
 public override TypeName [] GetParameterTypes(IMethodBase method)
 {
     return(method.Value().Parameters.Select(p => new TypeName()
     {
         Name = p.Type
     }).ToArray());
 }
Example #7
0
 private void RunAnalyzers(IMethodBase method)
 {
     foreach (var item in _methodAnalyzers.Where(a => a is ICodeAnalyzer <IMethodBase>))
     {
         item.Analyze(method);
         _analyzeErrors.AddRange(item.GetResult());
     }
 }
 protected override void visitMethodBase(IMethodBase methodBase)
 {
     if (!(methodBase is Lambda))
     {
         this.varNames.resetScope();
     }
     base.visitMethodBase(methodBase);
 }
Example #9
0
        public CallableSignature(IMethodBase method)
        {
            if (null == method)
            {
                throw new ArgumentNullException("method");
            }

            Initialize(method.GetParameters(), method.ReturnType, method.AcceptVarArgs);
        }
Example #10
0
 public MethodParameter(string name, IType type, Expression initializer, string leadingTrivia)
 {
     this.name          = name;
     this.type          = type;
     this.initializer   = initializer;
     this.leadingTrivia = leadingTrivia;
     this.fieldDecl     = null;
     this.parentMethod  = null;
     this.references    = new List <MethodParameterReference>();
 }
Example #11
0
 public AstTransformer(string name)
 {
     this.name             = name;
     this.errorMan         = new ErrorManager();
     this.currentFile      = null;
     this.currentInterface = null;
     this.currentMethod    = null;
     this.currentClosure   = null;
     this.currentStatement = null;
 }
Example #12
0
        private bool AreConnected(IMethodBase method1, IMethodBase method2)
        {
            var intersectOfUsedFields = GetUsedClassFieldsIn(method1.MethodBody)
                                        .Intersect(GetUsedClassFieldsIn(method2.MethodBody));
            var connectedBySharedClassField = intersectOfUsedFields.Any();

            var connectedByCallMethod = IsCalledByMethod(method1.MethodBody, method2) ||
                                        IsCalledByMethod(method2.MethodBody, method1);

            return(connectedBySharedClassField || connectedByCallMethod);
        }
        private static string GetFileName(IStackFrame frame, IMethodBase method)
        {
            var returnVal = frame.GetFileName();

            if (!string.IsNullOrWhiteSpace(returnVal))
            {
                return(returnVal);
            }

            return(method.ReflectedType != null ? method.ReflectedType.FullName : "(unknown)");
        }
Example #14
0
        private bool BinaryOperatorFor(IMethodBase method, out BinaryOperatorType op)
        {
            if (method == _methodCache.Instance.RuntimeServices_EqualityOperator)
            {
                op = BinaryOperatorType.Equality;
                return(true);
            }

            if (!method.Name.StartsWith("op_"))
            {
                op = BinaryOperatorType.None;
                return(false);
            }

            return(Enum.TryParse(method.Name.Substring("op_".Length), true, out op));
        }
        protected override void visitMethodBase(IMethodBase method)
        {
            if (this.stage != InferTypesStage.Methods)
            {
                return;
            }

            foreach (var plugin in this.plugins)
            {
                if (plugin.handleMethod(method))
                {
                    return;
                }
            }

            base.visitMethodBase(method);
        }
Example #16
0
        public override void LeaveMethodInvocationExpression(MethodInvocationExpression node)
        {
            IMethodBase methodBase = node.Target.Entity as IMethodBase;

            if (methodBase != null && methodBase.AcceptVarArgs)
            {
                IParameter[] parameters = methodBase.GetParameters();
                if (parameters.Length == ((ICollection)node.Arguments).Count)
                {
                    IType expressionType = node.Arguments[-1].ExpressionType;
                    if (expressionType == parameters[parameters.Length + -1].Type)
                    {
                        node.Arguments[-1] = new UnaryExpression(UnaryOperatorType.Explode, node.Arguments[-1]);
                    }
                }
            }
        }
        private static string GetMethod(IMethodBase method)
        {
            var methodName = string.IsNullOrEmpty(method.Name) ? "unknown" : method.Name;

            if (method.ReflectedType != null)
            {
                methodName = string.Format("{0}.{1}", method.ReflectedType.FullName, methodName);
            }

            var parameters = method.GetParameters();

            if (parameters.Length > 0)
            {
                return(string.Format("{0}({1})", methodName, string.Join(", ", parameters.Select(p => string.Format("{0} {1}", p.ParameterType, p.Name)))));
            }

            return(string.Format("{0}()", methodName));
        }
Example #18
0
        /// <summary>
        ///   Checks if the method is public or protected.
        /// </summary>
        /// <param name = "method"></param>
        /// <returns></returns>
        public static bool IsAccessible(this IMethodBase method)
        {
            // Accessibility supported by the full framework and CoreCLR
            if (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly)
            {
                return(true);
            }

            if (method.IsFamilyAndAssembly)
            {
                return(true);
            }

            if (method.DeclaringType.Assembly().IsInternalToDynamicProxy() && method.IsAssembly)
            {
                return(true);
            }
            return(false);
        }
Example #19
0
 void PushArguments(IMethodBase entity, ExpressionCollection args)
 {
     IParameter[] parameters = entity.GetParameters();
     for (int i=0; i<args.Count; ++i)
     {
         IType parameterType = parameters[i].Type;
         Expression arg = args[i];
         /*
         InternalParameter internalparam = parameters[i] as InternalParameter;
         if ((parameterType.IsByRef) ||
             (internalparam != null &&
             internalparam.Parameter.IsByRef)
             )
          */
         if (parameters[i].IsByRef)
         {
             LoadAddress(arg);
         }
         else
         {
             arg.Accept(this);
             EmitCastIfNeeded(parameterType, PopType());
         }
     }
 }
Example #20
0
		public ICallableType GetCallableType(IMethodBase method)
		{
			var signature = new CallableSignature(method);
			return GetCallableType(signature);
		}
Example #21
0
		void PushArguments(IMethodBase entity, ExpressionCollection args)
		{
			var parameters = entity.GetParameters();
			for (var i=0; i<args.Count; ++i)
			{
				var parameterType = parameters[i].Type;
				var arg = args[i];
				if (parameters[i].IsByRef)
					LoadAddress(arg);
				else
					LoadExpressionWithType(parameterType, arg);
			}
		}
Example #22
0
 public abstract string GetParameterManagedTypeName(IMethodBase m, int index);
Example #23
0
 public abstract string GetMethodName(IMethodBase m);
 public override string GetMethodName(IMethodBase m)
 {
     return(m.Value()?.Name);
 }
Example #25
0
        public string CreateMethod(IMethodBase sourceMethod)

        {
            if (sourceMethod == null)
            {
                throw new Exception("sourceMethod is null");
            }

            if (sourceMethod.IsFinal)
            {
                throw new Exception("Final method could not be extended");
            }


            if (!(sourceMethod.Visibility == CompilerVisibility.Protected ||
                  sourceMethod.Visibility == CompilerVisibility.Public))

            {
                throw new Exception("Only protected and public methods can be extended");
            }

            var attributes = sourceMethod.DataContractAttributes.OfType <IAttributeItem>();

            if (attributes != null && attributes.Any(attribute =>
                                                     attribute.Name == "Wrappable" &&
                                                     string.Equals(attribute.Parameters[0].TypeValue, Boolean.FalseString)))

            {
                throw new Exception("Only Wrappable methods could be extended");
            }

            if (attributes != null && attributes.Any(attribute => attribute.Name.Contains("InternalUseOnlyAttribute")))
            {
                throw new Exception("Method is marked with InternalUseOnlyAttribute and can not be extended");
            }

            string parametersString = string.Empty;
            string nextParameters   = string.Empty;

            var parameters = sourceMethod.Parameters.OfType <IMethodParameter>();

            if (parameters != null && parameters.Any())
            {
                parametersString = string.Join(", ",
                                               parameters.Select(parameter =>
                                                                 GetTypeNameFromCompilerType(parameter.TypeCompiler, parameter.TypeName) + " " + parameter.Name +
                                                                 (parameter.IsArray ? "[]" : string.Empty)));
                nextParameters = string.Join(", ",
                                             parameters.Select(parameter => parameter.Name + (parameter.IsArray ? "[]" : string.Empty)));
            }

            string        nextString = $"next {sourceMethod.Name}({nextParameters});";
            StringBuilder methodText = new StringBuilder();
            string        returnType =
                GetTypeNameFromCompilerType(sourceMethod.ReturnType.TypeCompiler, sourceMethod.ReturnType.TypeName);

            methodText.AppendLine(
                $"{sourceMethod.Visibility.ToString().ToLower()}{(sourceMethod.IsStatic ? StaticString : string.Empty)} {returnType} {sourceMethod.Name}({parametersString})");
            methodText.AppendLine("{");

            if (sourceMethod.ReturnType.TypeCompiler != CompilerBaseType.Void)
            {
                methodText.AppendLine($"\t{returnType} ret = {nextString}");
                methodText.AppendLine();
                methodText.AppendLine($"\treturn ret;");
            }
            else
            {
                methodText.AppendLine($"\t{nextString}");
            }

            methodText.AppendLine("}");


            return(methodText.ToString());
        }
Example #26
0
 public abstract TypeName [] GetParameterTypes(IMethodBase method);
Example #27
0
 public void SetParent(IMethodBase parent)
 {
     Parent = parent;
 }
 public void processMethodBase(IMethodBase method)
 {
     base.visitMethodBase(method);
 }
        public override string GetMethodReturnManagedTypeName(IMethodBase method)
        {
            var m = method.Value();

            return(m is Ctor ? null : ((Method)m)?.ManagedReturn);
        }
Example #30
0
 public void CheckForSideEffects(IMethodBase method)
 {
     _method = method;
     FindUnsafeModifications();
 }
Example #31
0
 public abstract string GetMethodReturnManagedTypeName(IMethodBase m);
Example #32
0
        public ICallableType GetCallableType(IMethodBase method)
        {
            var signature = new CallableSignature(method);

            return(GetCallableType(signature));
        }
 public override string GetParameterManagedTypeName(IMethodBase m, int index)
 {
     return(m.Value()?.Parameters [index]?.Type);
 }