void ReplaceAllFunctionAssignments(BlockStatement block, string functionName, TypeReference typeReference) { ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(functionName); block.AcceptVisitor(visitor, null); if (visitor.expressionsToReplace.Count == 1 && !visitor.hasExit && IsAssignmentTo(block.Children.Last(), functionName)) { Expression returnValue = GetAssignmentFromStatement(block.Children.Last()).Right; block.Children.RemoveAt(block.Children.Count - 1); block.Return(returnValue); } else { if (visitor.expressionsToReplace.Count > 0) { foreach (var expr in visitor.expressionsToReplace) { expr.Identifier = FunctionReturnValueName; } Expression init; init = ExpressionBuilder.CreateDefaultValueForType(typeReference); block.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, typeReference))); block.Children[0].Parent = block; block.Return(new IdentifierExpression(FunctionReturnValueName)); } } }
public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data) { if (!IsClassType(ClassType.Interface) && (methodDeclaration.Modifier & Modifiers.Visibility) == 0) { methodDeclaration.Modifier |= Modifiers.Public; } if ("Finalize".Equals(methodDeclaration.Name, StringComparison.InvariantCultureIgnoreCase) && methodDeclaration.Parameters.Count == 0 && methodDeclaration.Modifier == (Modifiers.Protected | Modifiers.Override) && methodDeclaration.Body.Children.Count == 1) { TryCatchStatement tcs = methodDeclaration.Body.Children[0] as TryCatchStatement; if (tcs != null && tcs.StatementBlock is BlockStatement && tcs.CatchClauses.Count == 0 && tcs.FinallyBlock is BlockStatement && tcs.FinallyBlock.Children.Count == 1) { ExpressionStatement se = tcs.FinallyBlock.Children[0] as ExpressionStatement; if (se != null) { InvocationExpression ie = se.Expression as InvocationExpression; if (ie != null && ie.Arguments.Count == 0 && ie.TargetObject is MemberReferenceExpression && (ie.TargetObject as MemberReferenceExpression).TargetObject is BaseReferenceExpression && "Finalize".Equals((ie.TargetObject as MemberReferenceExpression).MemberName, StringComparison.InvariantCultureIgnoreCase)) { DestructorDeclaration des = new DestructorDeclaration("Destructor", Modifiers.None, methodDeclaration.Attributes); ReplaceCurrentNode(des); des.Body = (BlockStatement)tcs.StatementBlock; return(base.VisitDestructorDeclaration(des, data)); } } } } if ((methodDeclaration.Modifier & (Modifiers.Static | Modifiers.Extern)) == Modifiers.Static && methodDeclaration.Body.Children.Count == 0) { foreach (AttributeSection sec in methodDeclaration.Attributes) { foreach (Attribute att in sec.Attributes) { if ("DllImport".Equals(att.Name, StringComparison.InvariantCultureIgnoreCase)) { methodDeclaration.Modifier |= Modifiers.Extern; methodDeclaration.Body = null; } } } } if (methodDeclaration.TypeReference.Type != "System.Void" && methodDeclaration.Body.Children.Count > 0) { if (IsAssignmentTo(methodDeclaration.Body.Children[methodDeclaration.Body.Children.Count - 1], methodDeclaration.Name)) { Expression returnValue = GetAssignmentFromStatement(methodDeclaration.Body.Children[methodDeclaration.Body.Children.Count - 1]).Right; methodDeclaration.Body.Children.RemoveAt(methodDeclaration.Body.Children.Count - 1); methodDeclaration.Body.Return(returnValue); } else { ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(methodDeclaration.Name); methodDeclaration.Body.AcceptVisitor(visitor, null); if (visitor.replacementCount > 0) { Expression init; init = ExpressionBuilder.CreateDefaultValueForType(methodDeclaration.TypeReference); methodDeclaration.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, methodDeclaration.TypeReference))); methodDeclaration.Body.Children[0].Parent = methodDeclaration.Body; methodDeclaration.Body.Return(new IdentifierExpression(FunctionReturnValueName)); } } } return(base.VisitMethodDeclaration(methodDeclaration, data)); }
public override object VisitMethodDeclaration(MethodDeclaration methodDeclaration, object data) { if (!IsClassType(ClassType.Interface) && (methodDeclaration.Modifier & Modifiers.Visibility) == 0) methodDeclaration.Modifier |= Modifiers.Public; if ("Finalize".Equals(methodDeclaration.Name, StringComparison.InvariantCultureIgnoreCase) && methodDeclaration.Parameters.Count == 0 && methodDeclaration.Modifier == (Modifiers.Protected | Modifiers.Override) && methodDeclaration.Body.Children.Count == 1) { TryCatchStatement tcs = methodDeclaration.Body.Children[0] as TryCatchStatement; if (tcs != null && tcs.StatementBlock is BlockStatement && tcs.CatchClauses.Count == 0 && tcs.FinallyBlock is BlockStatement && tcs.FinallyBlock.Children.Count == 1) { ExpressionStatement se = tcs.FinallyBlock.Children[0] as ExpressionStatement; if (se != null) { InvocationExpression ie = se.Expression as InvocationExpression; if (ie != null && ie.Arguments.Count == 0 && ie.TargetObject is MemberReferenceExpression && (ie.TargetObject as MemberReferenceExpression).TargetObject is BaseReferenceExpression && "Finalize".Equals((ie.TargetObject as MemberReferenceExpression).MemberName, StringComparison.InvariantCultureIgnoreCase)) { DestructorDeclaration des = new DestructorDeclaration("Destructor", Modifiers.None, methodDeclaration.Attributes); ReplaceCurrentNode(des); des.Body = (BlockStatement)tcs.StatementBlock; return base.VisitDestructorDeclaration(des, data); } } } } if ((methodDeclaration.Modifier & (Modifiers.Static | Modifiers.Extern)) == Modifiers.Static && methodDeclaration.Body.Children.Count == 0) { foreach (AttributeSection sec in methodDeclaration.Attributes) { foreach (Attribute att in sec.Attributes) { if ("DllImport".Equals(att.Name, StringComparison.InvariantCultureIgnoreCase)) { methodDeclaration.Modifier |= Modifiers.Extern; methodDeclaration.Body = null; } } } } if (methodDeclaration.TypeReference.Type != "System.Void" && methodDeclaration.Body.Children.Count > 0) { if (IsAssignmentTo(methodDeclaration.Body.Children[methodDeclaration.Body.Children.Count - 1], methodDeclaration.Name)) { Expression returnValue = GetAssignmentFromStatement(methodDeclaration.Body.Children[methodDeclaration.Body.Children.Count - 1]).Right; methodDeclaration.Body.Children.RemoveAt(methodDeclaration.Body.Children.Count - 1); methodDeclaration.Body.Return(returnValue); } else { ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(methodDeclaration.Name); methodDeclaration.Body.AcceptVisitor(visitor, null); if (visitor.replacementCount > 0) { Expression init; init = ExpressionBuilder.CreateDefaultValueForType(methodDeclaration.TypeReference); methodDeclaration.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, methodDeclaration.TypeReference))); methodDeclaration.Body.Children[0].Parent = methodDeclaration.Body; methodDeclaration.Body.Return(new IdentifierExpression(FunctionReturnValueName)); } } } return base.VisitMethodDeclaration(methodDeclaration, data); }
void ReplaceAllFunctionAssignments(BlockStatement block, string functionName, TypeReference typeReference) { ReturnStatementForFunctionAssignment visitor = new ReturnStatementForFunctionAssignment(functionName); block.AcceptVisitor(visitor, null); if (visitor.expressionsToReplace.Count == 1 && !visitor.hasExit && IsAssignmentTo(block.Children.Last(), functionName)) { Expression returnValue = GetAssignmentFromStatement(block.Children.Last()).Right; block.Children.RemoveAt(block.Children.Count - 1); block.Return(returnValue); } else { if (visitor.expressionsToReplace.Count > 0) { foreach (var expr in visitor.expressionsToReplace) { expr.Identifier = FunctionReturnValueName; } Expression init; init = ExpressionBuilder.CreateDefaultValueForType(typeReference); block.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(FunctionReturnValueName, init, typeReference))); block.Children[0].Parent = block; block.Return(new IdentifierExpression(FunctionReturnValueName)); } } }