// this doesn't take into account auxiliary data such as: sync roots, vtables and so on public static int SizeOfUsefulData(this Object o) { o.AssertNotNull(); var t = o.GetType(); if (t.IsValueType) { // todo. will crash for structures that have a reference-type fields return Marshal.SizeOf(t); } else if (t.IsArray) { // todo. won't work for arrays that have differently sized elements var elcount = o.AssertCast<Array>().Dims().Product(); return elcount * t.GetElementType().SizeOfUsefulData(); } else if (t.IsClass) { // todo. to be implemented throw AssertionHelper.Fail(); } else { throw AssertionHelper.Fail(); } }
public static Lambda InvokedLambda(this Node n) { var ci = n as CollectionInit; if (ci != null) return ci.Ctor.InvokedLambda(); var oi = n as ObjectInit; if (oi != null) return oi.Ctor.InvokedLambda(); if (n == null) return null; if (n is Apply) { var app = n.AssertCast<Apply>(); return app == null ? null : app.Callee as Lambda; } else if (n is Eval) { var eval = n.AssertCast<Eval>(); return eval.Callee.InvokedLambda(); } else { return null; } }
public static Type Ret(this MethodBase mi) { if (mi == null) return null; if (mi is MethodInfo || mi is MethodBuilder || mi is DynamicMethod) { return mi.AssertCast<MethodInfo>().ReturnType; } else if (mi is ConstructorBuilder || mi is ConstructorInfo) { return typeof(void); } else { throw AssertionHelper.Fail(); } }
private static Node SimplifyConditions(this Operator op) { var opt = op.OperatorType; if (opt == OperatorType.Equal || opt == OperatorType.NotEqual) { var c_false = op.Children.SingleOrDefault(c => c is Const && ((Const)c).Value is bool && (bool)((Const)c).Value == false); var c_true = op.Children.SingleOrDefault(c => c is Const && ((Const)c).Value is bool && (bool)((Const)c).Value == true); var cmpWithFalse = c_false != null; var cmpWithTrue = c_true != null; var equivToNegation = (cmpWithFalse && opt == OperatorType.Equal) || (cmpWithTrue && opt == OperatorType.NotEqual); if (equivToNegation) { var redux = cmpWithTrue ? op.Children.Except(c_true).Single() : op.Children.Except(c_false).Single(); return SimplifyConditions((Operator)Operator.Not(redux.AssertCast<Expression>())); } var redundantLogicalOp = (cmpWithTrue && opt == OperatorType.Equal) || (cmpWithFalse && opt == OperatorType.NotEqual); if (redundantLogicalOp) { var redux = cmpWithTrue ? op.Children.Except(c_true).Single() : op.Children.Except(c_false).Single(); return redux.CurrentTransform(); } } if (opt == OperatorType.Not) { var child_op = op.Children.Single() as Operator; if (child_op != null) { var copt = child_op.OperatorType; if (copt == OperatorType.Not) { var redux = child_op.Args.Single(); return redux.CurrentTransform(); } Func<OperatorType, OperatorType?> negate = op_type => { switch (op_type) { case OperatorType.Equal: return OperatorType.NotEqual; case OperatorType.GreaterThan: return OperatorType.LessThanOrEqual; case OperatorType.GreaterThanOrEqual: return OperatorType.LessThan; case OperatorType.LessThan: return OperatorType.GreaterThanOrEqual; case OperatorType.LessThanOrEqual: return OperatorType.GreaterThan; case OperatorType.NotEqual: return OperatorType.Equal; default: return null; } }; var negated = negate(copt); if (negated != null) { var equiv = Operator.Create(negated.Value, child_op.Args); return equiv.SimplifyConditions(); } var child_bop = child_op.AssertCast<BinaryOperator>(); var child_lhs = child_bop == null ? null : child_bop.Lhs; var child_rhs = child_bop == null ? null : child_bop.Rhs; if (copt == OperatorType.AndAlso) { var equiv = Operator.OrElse(Operator.Not(child_lhs), Operator.Not(child_rhs)); return equiv.SimplifyConditions(); } else if (copt == OperatorType.OrElse) { var equiv = Operator.AndAlso(Operator.Not(child_lhs), Operator.Not(child_rhs)); return equiv.SimplifyConditions(); } else if (copt == OperatorType.Xor) { var equiv = Operator.Xor(Operator.Not(child_lhs), child_rhs); return equiv.SimplifyConditions(); } } } if (opt == OperatorType.Xor) { var bop = op.AssertCast<BinaryOperator>(); var lhs = bop == null ? null : bop.Lhs as Operator; var rhs = bop == null ? null : bop.Rhs as Operator; if (lhs != null && lhs.OperatorType == OperatorType.Not && rhs != null && rhs.OperatorType == OperatorType.Not) { var equiv = Operator.Xor(lhs.Args.Single(), rhs.Args.Single()); return equiv.SimplifyConditions(); } } return (Operator)op.DefaultTransform(); }
public static Type Type(this MemberInfo mi) { if (mi is FieldInfo) { var fi = mi.AssertCast<FieldInfo>(); return fi.FieldType; } else if (mi is PropertyInfo) { var pi = mi.AssertCast<PropertyInfo>(); return pi.PropertyType; } else { throw AssertionHelper.Fail(); } }
public static void SetValue(this MemberInfo mi, Object target, Object value) { if (mi is FieldInfo) { var fi = mi.AssertCast<FieldInfo>(); fi.SetValue(target, value); } else if (mi is PropertyInfo) { var pi = mi.AssertCast<PropertyInfo>(); pi = pi.DeclaringType.GetProperties(BF.All | BF.DeclOnly).AssertSingle(pid => pid.MetadataToken == pi.MetadataToken); var set = pi.GetSetMethod(true); set.Invoke(target, new []{value}); } else { throw AssertionHelper.Fail(); } }
public static bool CanWrite(this MemberInfo mi) { if (mi is FieldInfo) { return true; } else if (mi is PropertyInfo) { var pi = mi.AssertCast<PropertyInfo>(); return pi.CanWrite; } else { throw AssertionHelper.Fail(); } }
public static Visibility Visibility(this MemberInfo mi) { mi.AssertNotNull(); if (mi is Type) { var t = mi.AssertCast<Type>(); if (t.IsNested) { if (t.IsNestedPublic) return Reflection.Visibility.Public; if (t.IsNestedFamORAssem) return Reflection.Visibility.FamilyOrAssembly; if (t.IsNestedFamily) return Reflection.Visibility.Family; if (t.IsNestedAssembly) return Reflection.Visibility.Assembly; if (t.IsNestedFamANDAssem) return Reflection.Visibility.FamilyAndAssembly; if (t.IsNestedPrivate) return Reflection.Visibility.Private; throw AssertionHelper.Fail(); } else { if (t.IsPublic) return Reflection.Visibility.Public; return Reflection.Visibility.Assembly; } } else if (mi is FieldInfo) { var fi = mi.AssertCast<FieldInfo>(); if (fi.IsPublic) return Reflection.Visibility.Public; if (fi.IsFamilyOrAssembly) return Reflection.Visibility.FamilyOrAssembly; if (fi.IsFamily) return Reflection.Visibility.Family; if (fi.IsAssembly) return Reflection.Visibility.Assembly; if (fi.IsFamilyAndAssembly) return Reflection.Visibility.FamilyAndAssembly; if (fi.IsPrivate) return Reflection.Visibility.Private; throw AssertionHelper.Fail(); } else if (mi is MethodBase) { var mb = mi.AssertCast<MethodBase>(); if (mb.IsPublic) return Reflection.Visibility.Public; if (mb.IsFamilyOrAssembly) return Reflection.Visibility.FamilyOrAssembly; if (mb.IsFamily) return Reflection.Visibility.Family; if (mb.IsAssembly) return Reflection.Visibility.Assembly; if (mb.IsFamilyAndAssembly) return Reflection.Visibility.FamilyAndAssembly; if (mb.IsPrivate) return Reflection.Visibility.Private; throw AssertionHelper.Fail(); } else if (mi is PropertyInfo) { var pi = mi.AssertCast<PropertyInfo>(); var getter = pi.CanRead ? null : pi.GetGetMethod(true); var setter = pi.CanRead ? null : pi.GetGetMethod(true); if (getter == null || setter == null) { throw AssertionHelper.Fail(); } else if (getter == null || setter == null) { return (getter ?? setter).Visibility(); } else { var g_vis = getter.Visibility(); var s_vis = setter.Visibility(); return (Visibility)Math.Max((int)g_vis, (int)s_vis); } } else { throw AssertionHelper.Fail(); } }
public static Prop InvokedProp(this Node n) { if (n == null) return null; if (n is Assign) { var ass = (Assign)n; return ass.Lhs.InvokedProp(); } else if (n is Prop) { var prop = (Prop)n; return prop; } else if (n is Apply) { var app = n.AssertCast<Apply>(); var prop = app == null ? null : app.Callee as Prop; return prop; } else { return null; } }
public static ArgsInfo InvocationArgsInfo(this Node n) { var ci = n as CollectionInit; if (ci != null) return ci.Ctor.InvocationArgsInfo(); var oi = n as ObjectInit; if (oi != null) return oi.Ctor.InvocationArgsInfo(); if (n == null) return null; if (n is Apply) { var app = n.AssertCast<Apply>(); return app.ArgsInfo; } else if (n is Eval) { var eval = n.AssertCast<Eval>(); return eval.Callee.InvocationArgsInfo(); } else { return null; } }
public static BinaryOperator Binary(this Operator op) { return op.AssertCast<BinaryOperator>(); }
public static UnaryOperator Unary(this Operator op) { return op.AssertCast<UnaryOperator>(); }
public static FluentRule DefaultEngine(this Configuration.Rule generic_rule) { var type_rule = generic_rule.AssertCast<Configuration.TypeRule>().AssertNotNull(); return DefaultEngine(type_rule); }
public static FluentConfig DefaultEngine(this Configuration.Config generic_config) { var type_config = generic_config.AssertCast<Configuration.TypeConfig>().AssertNotNull(); return DefaultEngine(type_config); }