private static void ValidateGoto(LabelTarget target, ref Expression value, string targetParameter, string valueParameter, Type type) { ContractUtils.RequiresNotNull(target, targetParameter); if (value == null) { if (target.Type != typeof(void)) { throw Error.LabelMustBeVoidOrHaveExpression(nameof(target)); } if (type != null) { TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } } } else { ValidateGotoType(target.Type, ref value, valueParameter); } }
/// <summary> /// Creates a <see cref="CatchBlock"/> representing a catch statement with the specified elements. /// </summary> /// <param name="type">The <see cref="Type"/> of <see cref="Exception"/> this <see cref="CatchBlock"/> will handle.</param> /// <param name="variable">A <see cref="ParameterExpression"/> representing a reference to the <see cref="Exception"/> object caught by this handler.</param> /// <param name="body">The body of the catch statement.</param> /// <param name="filter">The body of the <see cref="Exception"/> filter.</param> /// <returns>The created <see cref="CatchBlock"/>.</returns> /// <remarks><paramref name="type"/> must be non-null and match the type of <paramref name="variable"/> (if it is supplied).</remarks> public static CatchBlock MakeCatchBlock(Type type, ParameterExpression variable, Expression body, Expression filter) { ContractUtils.RequiresNotNull(type, nameof(type)); ContractUtils.Requires(variable == null || TypeUtils.AreEquivalent(variable.Type, type), nameof(variable)); if (variable == null) { TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } } else if (variable.IsByRef) { throw Error.VariableMustNotBeByRef(variable, variable.Type, nameof(variable)); } ExpressionUtils.RequiresCanRead(body, nameof(body)); if (filter != null) { ExpressionUtils.RequiresCanRead(filter, nameof(filter)); if (filter.Type != typeof(bool)) { throw Error.ArgumentMustBeBoolean(nameof(filter)); } } return(new CatchBlock(type, variable, body, filter)); }
/// <summary> /// Creates a <see cref="NewArrayExpression"/> that represents creating an array that has a specified rank. /// </summary> /// <param name="type">A <see cref="System.Type"/> that represents the element type of the array.</param> /// <param name="bounds">An <see cref="IEnumerable{T}"/> that contains <see cref="Expression"/> objects to use to populate the <see cref="NewArrayExpression.Expressions"/> collection.</param> /// <returns>A <see cref="NewArrayExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="ExpressionType.NewArrayBounds"/> and the <see cref="NewArrayExpression.Expressions"/> property set to the specified value.</returns> public static NewArrayExpression NewArrayBounds(Type type, IEnumerable <Expression> bounds) { ContractUtils.RequiresNotNull(type, nameof(type)); ContractUtils.RequiresNotNull(bounds, nameof(bounds)); if (type == typeof(void)) { throw Error.ArgumentCannotBeOfTypeVoid(nameof(type)); } TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } ReadOnlyCollection <Expression> boundsList = bounds.ToReadOnly(); int dimensions = boundsList.Count; if (dimensions <= 0) { throw Error.BoundsCannotBeLessThanOne(nameof(bounds)); } for (int i = 0; i < dimensions; i++) { Expression expr = boundsList[i]; RequiresCanRead(expr, nameof(bounds), i); if (!TypeUtils.IsInteger(expr.Type)) { throw Error.ArgumentMustBeInteger(nameof(bounds), i); } } Type arrayType; if (dimensions == 1) { //To get a vector, need call Type.MakeArrayType(). //Type.MakeArrayType(1) gives a non-vector array, which will cause type check error. arrayType = type.MakeArrayType(); } else { arrayType = type.MakeArrayType(dimensions); } return(NewArrayExpression.Make(ExpressionType.NewArrayBounds, arrayType, bounds.ToReadOnly())); }
/// <summary> /// Creates a <see cref="NewArrayExpression"/> of the specified type from the provided initializers. /// </summary> /// <param name="type">A Type that represents the element type of the array.</param> /// <param name="initializers">The expressions used to create the array elements.</param> /// <returns>A <see cref="NewArrayExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="ExpressionType.NewArrayInit"/> and the <see cref="NewArrayExpression.Expressions"/> property set to the specified value.</returns> public static NewArrayExpression NewArrayInit(Type type, IEnumerable <Expression> initializers) { ContractUtils.RequiresNotNull(type, nameof(type)); ContractUtils.RequiresNotNull(initializers, nameof(initializers)); if (type == typeof(void)) { throw Error.ArgumentCannotBeOfTypeVoid(nameof(type)); } TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } ReadOnlyCollection <Expression> initializerList = initializers.ToReadOnly(); Expression[] newList = null; for (int i = 0, n = initializerList.Count; i < n; i++) { Expression expr = initializerList[i]; RequiresCanRead(expr, nameof(initializers), i); if (!TypeUtils.AreReferenceAssignable(type, expr.Type)) { if (!TryQuote(type, ref expr)) { throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type); } if (newList == null) { newList = new Expression[initializerList.Count]; for (int j = 0; j < i; j++) { newList[j] = initializerList[j]; } } } if (newList != null) { newList[i] = expr; } } if (newList != null) { initializerList = new TrueReadOnlyCollection <Expression>(newList); } return(NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList)); }
/// <summary> /// Creates a <see cref="TypeBinaryExpression"/> that compares run-time type identity. /// </summary> /// <param name="expression">An <see cref="Expression"/> to set the <see cref="Expression"/> property equal to.</param> /// <param name="type">A <see cref="Type"/> to set the <see cref="TypeBinaryExpression.TypeOperand"/> property equal to.</param> /// <returns>A <see cref="TypeBinaryExpression"/> for which the <see cref="NodeType"/> property is equal to <see cref="TypeEqual"/> and for which the <see cref="Expression"/> and <see cref="TypeBinaryExpression.TypeOperand"/> properties are set to the specified values.</returns> public static TypeBinaryExpression TypeEqual(Expression expression, Type type) { RequiresCanRead(expression, "expression"); ContractUtils.RequiresNotNull(type, "type"); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(); } return(new TypeBinaryExpression(expression, type, ExpressionType.TypeEqual)); }
/// <summary> /// Creates a <see cref="ParameterExpression"/> node that can be used to identify a parameter or a variable in an expression tree. /// </summary> /// <param name="type">The type of the parameter or variable.</param> /// <param name="name">The name of the parameter or variable, used for debugging or pretty printing purpose only.</param> /// <returns>A <see cref="ParameterExpression"/> node with the specified name and type.</returns> public static ParameterExpression Variable(Type type, string name) { Validate(type); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } return(ParameterExpression.Make(type, name, isByRef: false)); }
/// <summary> /// Creates a <see cref="TypeBinaryExpression"/>. /// </summary> /// <param name="expression">An <see cref="Expression"/> to set the <see cref="Expression"/> property equal to.</param> /// <param name="type">A <see cref="Type"/> to set the <see cref="TypeBinaryExpression.TypeOperand"/> property equal to.</param> /// <returns>A <see cref="TypeBinaryExpression"/> for which the <see cref="NodeType"/> property is equal to <see cref="TypeIs"/> and for which the <see cref="Expression"/> and <see cref="TypeBinaryExpression.TypeOperand"/> properties are set to the specified values.</returns> public static TypeBinaryExpression TypeIs(Expression expression, Type type) { RequiresCanRead(expression, nameof(expression)); ContractUtils.RequiresNotNull(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } return(new TypeBinaryExpression(expression, type, ExpressionType.TypeIs)); }
/// <summary> /// Creates a <see cref="ParameterExpression" /> node that can be used to identify a parameter or a variable in an expression tree. /// </summary> /// <param name="type">The type of the parameter or variable.</param> /// <param name="name">The name of the parameter or variable, used for debugging or pretty printing purpose only.</param> /// <returns>A <see cref="ParameterExpression" /> node with the specified name and type.</returns> public static ParameterExpression Variable(Type type, string name) { ContractUtils.RequiresNotNull(type, "type"); if (type == typeof(void)) { throw Error.ArgumentCannotBeOfTypeVoid(); } if (type.IsByRef) { throw Error.TypeMustNotBeByRef(); } return(ParameterExpression.Make(type, name, false)); }
/// <summary> /// Creates a <see cref="LabelTarget"/> representing a label with the given type and name. /// </summary> /// <param name="type">The type of value that is passed when jumping to the label.</param> /// <param name="name">The name of the label.</param> /// <returns>The new <see cref="LabelTarget"/>.</returns> public static LabelTarget Label(Type type, string name) { ContractUtils.RequiresNotNull(type, nameof(type)); TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } return(new LabelTarget(type, name)); }
/// <summary> /// Creates a <see cref="DefaultExpression"/> that has the <see cref="Type"/> property set to the specified type. /// </summary> /// <param name="type">A <see cref="Type"/> to set the <see cref="Type"/> property equal to.</param> /// <returns> /// A <see cref="DefaultExpression"/> that has the <see cref="NodeType"/> property equal to /// <see cref="ExpressionType.Default"/> and the <see cref="Type"/> property set to the specified type. /// </returns> public static DefaultExpression Default(Type type) { ContractUtils.RequiresNotNull(type, nameof(type)); TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } return(new DefaultExpression(type)); }
/// <summary> /// Creates a <see cref="ConstantExpression"/> that has the <see cref="P:ConstantExpression.Value"/> /// and <see cref="P:ConstantExpression.Type"/> properties set to the specified values. . /// </summary> /// <param name="value">An <see cref="System.Object"/> to set the <see cref="P:ConstantExpression.Value"/> property equal to.</param> /// <param name="type">A <see cref="System.Type"/> to set the <see cref="P:Expression.Type"/> property equal to.</param> /// <returns> /// A <see cref="ConstantExpression"/> that has the <see cref="P:Expression.NodeType"/> property equal to /// <see cref="F:ExpressionType.Constant"/> and the <see cref="P:ConstantExpression.Value"/> and /// <see cref="P:Expression.Type"/> properties set to the specified values. /// </returns> public static ConstantExpression Constant(object value, Type type) { ContractUtils.RequiresNotNull(type, nameof(type)); TypeUtils.ValidateType(type, nameof(type)); if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } if (value == null) { if (type == typeof(object)) { return(new ConstantExpression(null)); } if (!type.GetTypeInfo().IsValueType || type.IsNullableType()) { return(new TypedConstantExpression(null, type)); } } else { Type valueType = value.GetType(); if (type == valueType) { return(new ConstantExpression(value)); } if (type.IsAssignableFrom(valueType)) { return(new TypedConstantExpression(value, type)); } } throw Error.ArgumentTypesMustMatch(); }
/// <summary> /// Creates a <see cref="ParameterExpression"/> node that can be used to identify a parameter or a variable in an expression tree. /// </summary> /// <param name="type">The type of the parameter or variable.</param> /// <param name="name">The name of the parameter or variable, used for debugging or pretty printing purpose only.</param> /// <returns>A <see cref="ParameterExpression"/> node with the specified name and type.</returns> public static ParameterExpression Variable(Type type, string name) { ContractUtils.RequiresNotNull(type, nameof(type)); TypeUtils.ValidateType(type, nameof(type)); if (type == typeof(void)) { throw Error.ArgumentCannotBeOfTypeVoid(nameof(type)); } if (type.IsByRef) { throw Error.TypeMustNotBeByRef(nameof(type)); } if (type.IsPointer) { throw Error.TypeMustNotBePointer(nameof(type)); } return(ParameterExpression.Make(type, name, isByRef: false)); }