mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.2 KiB
107 lines
3.2 KiB
// <file> |
|
// <copyright see="prj:///doc/copyright.txt"/> |
|
// <license see="prj:///doc/license.txt"/> |
|
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
// <version>$Revision$</version> |
|
// </file> |
|
|
|
using System; |
|
|
|
namespace ICSharpCode.NRefactory.Ast |
|
{ |
|
public abstract class Expression : AbstractNode, INullable |
|
{ |
|
public static Expression Null { |
|
get { |
|
return NullExpression.Instance; |
|
} |
|
} |
|
|
|
public virtual bool IsNull { |
|
get { |
|
return false; |
|
} |
|
} |
|
|
|
public static Expression CheckNull(Expression expression) |
|
{ |
|
return expression == null ? NullExpression.Instance : expression; |
|
} |
|
|
|
/// <summary> |
|
/// Returns the existing expression plus the specified integer value. |
|
/// The old <paramref name="expr"/> object is not modified, but might be a subobject on the new expression |
|
/// (and thus its parent property is modified). |
|
/// </summary> |
|
public static Expression AddInteger(Expression expr, int value) |
|
{ |
|
PrimitiveExpression pe = expr as PrimitiveExpression; |
|
if (pe != null && pe.Value is int) { |
|
int newVal = (int)pe.Value + value; |
|
return new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); |
|
} |
|
BinaryOperatorExpression boe = expr as BinaryOperatorExpression; |
|
if (boe != null && boe.Op == BinaryOperatorType.Add) { |
|
// clone boe: |
|
boe = new BinaryOperatorExpression(boe.Left, boe.Op, boe.Right); |
|
|
|
boe.Right = AddInteger(boe.Right, value); |
|
if (boe.Right is PrimitiveExpression && ((PrimitiveExpression)boe.Right).Value is int) { |
|
int newVal = (int)((PrimitiveExpression)boe.Right).Value; |
|
if (newVal == 0) { |
|
return boe.Left; |
|
} else if (newVal < 0) { |
|
((PrimitiveExpression)boe.Right).Value = -newVal; |
|
boe.Op = BinaryOperatorType.Subtract; |
|
} |
|
} |
|
return boe; |
|
} |
|
if (boe != null && boe.Op == BinaryOperatorType.Subtract) { |
|
pe = boe.Right as PrimitiveExpression; |
|
if (pe != null && pe.Value is int) { |
|
int newVal = (int)pe.Value - value; |
|
if (newVal == 0) |
|
return boe.Left; |
|
|
|
// clone boe: |
|
boe = new BinaryOperatorExpression(boe.Left, boe.Op, boe.Right); |
|
|
|
if (newVal < 0) { |
|
newVal = -newVal; |
|
boe.Op = BinaryOperatorType.Add; |
|
} |
|
boe.Right = new PrimitiveExpression(newVal, newVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); |
|
return boe; |
|
} |
|
} |
|
BinaryOperatorType opType = BinaryOperatorType.Add; |
|
if (value < 0) { |
|
value = -value; |
|
opType = BinaryOperatorType.Subtract; |
|
} |
|
return new BinaryOperatorExpression(expr, opType, new PrimitiveExpression(value, value.ToString(System.Globalization.NumberFormatInfo.InvariantInfo))); |
|
} |
|
} |
|
|
|
internal sealed class NullExpression : Expression |
|
{ |
|
internal static readonly NullExpression Instance = new NullExpression(); |
|
|
|
public override bool IsNull { |
|
get { |
|
return true; |
|
} |
|
} |
|
|
|
public override object AcceptVisitor(IAstVisitor visitor, object data) |
|
{ |
|
return null; |
|
} |
|
|
|
public override string ToString() |
|
{ |
|
return String.Format("[NullExpression]"); |
|
} |
|
} |
|
}
|
|
|