void HandleStaticFieldInitializers(IEnumerable <AstNode> members) { // Convert static constructor into field initializers if the class is BeforeFieldInit var staticCtor = members.OfType <ConstructorDeclaration>().FirstOrDefault(c => (c.Modifiers & Modifiers.Static) == Modifiers.Static); if (staticCtor != null) { MethodDef ctorMethodDef = staticCtor.Annotation <MethodDef>(); if (ctorMethodDef != null && ctorMethodDef.DeclaringType.IsBeforeFieldInit) { var mm = staticCtor.Annotation <MemberMapping>() ?? staticCtor.Body.Annotation <MemberMapping>(); Debug.Assert(mm != null); while (true) { ExpressionStatement es = staticCtor.Body.Statements.FirstOrDefault() as ExpressionStatement; if (es == null) { break; } AssignmentExpression assignment = es.Expression as AssignmentExpression; if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign) { break; } FieldDef fieldDef = assignment.Left.Annotation <IField>().ResolveFieldWithinSameModule(); if (fieldDef == null || !fieldDef.IsStatic) { break; } FieldDeclaration fieldDecl = members.OfType <FieldDeclaration>().FirstOrDefault(f => f.Annotation <FieldDef>() == fieldDef); if (fieldDecl == null) { break; } var ilRanges = assignment.GetAllRecursiveILRanges(); assignment.RemoveAllILRangesRecursive(); var varInit = fieldDecl.Variables.Single(); varInit.Initializer = assignment.Right.Detach(); var ctorIlRanges = new List <Tuple <MemberMapping, List <ILRange> > >(1); if (mm != null) { ctorIlRanges.Add(Tuple.Create(mm, ilRanges)); } fieldDecl.AddAnnotation(ctorIlRanges); es.Remove(); } if (context.Settings.RemoveEmptyDefaultConstructors && staticCtor.Body.Statements.Count == 0) { staticCtor.Remove(); } } } }
public override object VisitAssignmentExpression(AssignmentExpression assignment, object data) { base.VisitAssignmentExpression(assignment, data); // Combine "x = x op y" into "x op= y" BinaryOperatorExpression binary = assignment.Right as BinaryOperatorExpression; if (binary != null && assignment.Operator == AssignmentOperatorType.Assign) { if (CanConvertToCompoundAssignment(assignment.Left) && assignment.Left.IsMatch(binary.Left)) { assignment.Operator = GetAssignmentOperatorForBinaryOperator(binary.Operator); if (assignment.Operator != AssignmentOperatorType.Assign) { // If we found a shorter operator, get rid of the BinaryOperatorExpression: assignment.CopyAnnotationsFrom(binary); assignment.Right = binary.Right.WithAnnotation(assignment.Right.GetAllRecursiveILRanges()); assignment.AddAnnotation(new RestoreOriginalAssignOperatorAnnotation(binary)); } } } if (context.Settings.IntroduceIncrementAndDecrement && (assignment.Operator == AssignmentOperatorType.Add || assignment.Operator == AssignmentOperatorType.Subtract)) { // detect increment/decrement if (assignment.Right.IsMatch(new PrimitiveExpression(1))) { // only if it's not a custom operator if (assignment.Annotation <IMethod>() == null) { UnaryOperatorType type; // When the parent is an expression statement, pre- or post-increment doesn't matter; // so we can pick post-increment which is more commonly used (for (int i = 0; i < x; i++)) if (assignment.Parent is ExpressionStatement) { type = (assignment.Operator == AssignmentOperatorType.Add) ? UnaryOperatorType.PostIncrement : UnaryOperatorType.PostDecrement; } else { type = (assignment.Operator == AssignmentOperatorType.Add) ? UnaryOperatorType.Increment : UnaryOperatorType.Decrement; } assignment.ReplaceWith(new UnaryOperatorExpression(type, assignment.Left.Detach()).CopyAnnotationsFrom(assignment).WithAnnotation(assignment.GetAllRecursiveILRanges())); } } } return(null); }