/// <summary> /// Initializes a new instance of the <see cref="Import"/> class with /// the specified namespace and nested imports. /// </summary> /// <param name="ns">The namespace the type must be imported at.</param> /// <param name="imports">The imports that are nested under this import.</param> public Import(string ns, params Import[] imports) : this(ns, null, imports) { Require.NotNull(imports, "imports"); Require.That(imports.Length > 0, "At least one import is required in a namespace", "imports"); }
private static void EmitString(ILGenerator il, string value) { Require.NotNull(value, "value"); il.Emit(OpCodes.Ldstr, value); }
/// <summary> /// Initializes a new instance of the <see cref="Import"/> class with /// the specified type and namespace. /// </summary> /// <param name="ns">The namespace the type must be imported at.</param> /// <param name="type">The type to be imported.</param> public Import(string ns, Type type) : this(ns, type, null) { Require.NotNull(type, "type"); }
public ConversionVisitor(Resolver resolver) { Require.NotNull(resolver, "resolver"); _resolver = resolver; }
// Emits the Ldelem* instruction for the appropriate type public static void EmitLoadElement(ILGenerator il, Type type) { Require.NotNull(type, "type"); if (!type.IsValueType) { il.Emit(OpCodes.Ldelem_Ref); } else if (type.IsEnum) { il.Emit(OpCodes.Ldelem, type); } else { switch (Type.GetTypeCode(type)) { case TypeCode.Boolean: case TypeCode.SByte: il.Emit(OpCodes.Ldelem_I1); break; case TypeCode.Byte: il.Emit(OpCodes.Ldelem_U1); break; case TypeCode.Int16: il.Emit(OpCodes.Ldelem_I2); break; case TypeCode.Char: case TypeCode.UInt16: il.Emit(OpCodes.Ldelem_U2); break; case TypeCode.Int32: il.Emit(OpCodes.Ldelem_I4); break; case TypeCode.UInt32: il.Emit(OpCodes.Ldelem_U4); break; case TypeCode.Int64: case TypeCode.UInt64: il.Emit(OpCodes.Ldelem_I8); break; case TypeCode.Single: il.Emit(OpCodes.Ldelem_R4); break; case TypeCode.Double: il.Emit(OpCodes.Ldelem_R8); break; default: il.Emit(OpCodes.Ldelem, type); break; } } }
public BoundExpressionCache(CachedDynamicExpression dynamicExpression) { Require.NotNull(dynamicExpression, "dynamicExpression"); _dynamicExpression = dynamicExpression; }
public static bool IsValidUnaryArgument(Type type) { Require.NotNull(type, "type"); return(ImplicitCastingTable.ContainsKey(type)); }
private Type ResolveExpressionType(Type left, Type right, Type commonType, ExpressionType type) { Require.NotNull(left, "left"); Require.NotNull(right, "right"); // TODO: Implicit/explicit operators and operators for the expression type. // Boolean operators. switch (type) { case ExpressionType.And: case ExpressionType.Or: case ExpressionType.Xor: case ExpressionType.AndBoth: case ExpressionType.OrBoth: if (TypeUtil.IsInteger(commonType)) { return(commonType); } else if (left != typeof(bool) || right != typeof(bool)) { throw new ExpressionsException("Invalid operand for expression type", ExpressionsExceptionType.TypeMismatch); } return(typeof(bool)); case ExpressionType.Greater: case ExpressionType.GreaterOrEquals: case ExpressionType.Less: case ExpressionType.LessOrEquals: if ( !(left.IsEnum || TypeUtil.IsNumeric(left)) || !(right.IsEnum || TypeUtil.IsNumeric(right)) ) { throw new ExpressionsException("Invalid operand for expression type", ExpressionsExceptionType.TypeMismatch); } return(typeof(bool)); case ExpressionType.Equals: case ExpressionType.NotEquals: case ExpressionType.In: case ExpressionType.LogicalAnd: case ExpressionType.LogicalOr: case ExpressionType.Compares: case ExpressionType.NotCompares: return(typeof(bool)); case ExpressionType.Add: if ( !(left == typeof(string) || right == typeof(string)) && !(TypeUtil.IsNumeric(left) && TypeUtil.IsNumeric(right)) ) { throw new ExpressionsException("Invalid operand for expression type", ExpressionsExceptionType.TypeMismatch); } return(commonType); case ExpressionType.Subtract: case ExpressionType.Multiply: case ExpressionType.Divide: case ExpressionType.Power: if (!TypeUtil.IsNumeric(left) || !TypeUtil.IsNumeric(right)) { throw new ExpressionsException("Invalid operand for expression type", ExpressionsExceptionType.TypeMismatch); } return(commonType); default: return(commonType); } }
private Type ResolveExpressionCommonType(Type left, Type right, bool allowStringConcat, bool allowBothImplicit, bool allowObjectCoercion) { Require.NotNull(left, "left"); Require.NotNull(right, "right"); // No casting required. if (left == right) { return(left); } if (allowObjectCoercion) { if (left == typeof(object)) { return(right); } if (right == typeof(object)) { return(left); } } // Special cast for adding strings. if (allowStringConcat && (left == typeof(string) || right == typeof(string))) { return(typeof(string)); } // Can we cast implicitly? var leftTable = TypeUtil.GetImplicitCastingTable(left); var rightTable = TypeUtil.GetImplicitCastingTable(right); if (leftTable != null && rightTable != null) { // See whether one of the types appears in the other. foreach (var leftType in leftTable) { if (leftType == right) { return(leftType); } } foreach (var rightType in rightTable) { if (rightType == left) { return(rightType); } } // Otherwise, find the first common type. int lowest = int.MaxValue; for (int i = 0; i < leftTable.Count; i++) { for (int j = 0; j < rightTable.Count; j++) { if (leftTable[i] == rightTable[j]) { lowest = Math.Min(lowest, j); } } } if (lowest != int.MaxValue) { if ( allowBothImplicit || (IsAllowableImplicit(left, rightTable[lowest]) && IsAllowableImplicit(right, rightTable[lowest])) ) { return(rightTable[lowest]); } } } // We can't cast implicitly. throw new ExpressionsException("Cannot resolve expression type", ExpressionsExceptionType.TypeMismatch); }
public BindingVisitor(Resolver resolver) { Require.NotNull(resolver, "resolver"); _resolver = resolver; }