IsNullableType() public static method

public static IsNullableType ( System.TypeSpec t ) : bool
t System.TypeSpec
return bool
Example #1
0
        public override Constant ConvertExplicitly(bool inCheckedContext, Type targetType)
        {
            if (targetType.IsPointer)
            {
                if (type == TypeManager.null_type || this is NullPointer)
                {
                    return(new EmptyConstantCast(new NullPointer(loc), targetType));
                }

                return(null);
            }

            // Exlude internal compiler types
            if (targetType == InternalType.AnonymousMethod)
            {
                return(null);
            }

            if (type != TypeManager.null_type && !Convert.ImplicitStandardConversionExists(this, targetType))
            {
                return(null);
            }

            if (TypeManager.IsReferenceType(targetType))
            {
                return(new NullLiteral(targetType, loc));
            }

            if (TypeManager.IsNullableType(targetType))
            {
                return(Nullable.LiftedNull.Create(targetType, loc));
            }

            return(null);
        }
Example #2
0
        public override Constant ConvertExplicitly(bool inCheckedContext, Type targetType)
        {
            if (targetType.IsPointer)
            {
                return(new EmptyConstantCast(new NullPointer(loc), targetType));
            }

            // Exlude internal compiler types
            if (targetType == TypeManager.anonymous_method_type)
            {
                return(null);
            }

            if (TypeManager.IsReferenceType(targetType))
            {
                return(new EmptyConstantCast(this, targetType));
            }

            if (TypeManager.IsNullableType(targetType))
            {
                return(Nullable.LiftedNull.Create(targetType, loc));
            }

            return(null);
        }
Example #3
0
        public override Expression DoResolve(ResolveContext ec)
        {
            constructor_method = Delegate.GetConstructor(ec.Compiler, ec.CurrentType, type);

            MethodInfo invoke_method = Delegate.GetInvokeMethod(ec.Compiler, ec.CurrentType, type);

            method_group.DelegateType       = type;
            method_group.CustomErrorHandler = this;

            Arguments arguments = CreateDelegateMethodArguments(TypeManager.GetParameterData(invoke_method), loc);

            method_group = method_group.OverloadResolve(ec, ref arguments, false, loc);
            if (method_group == null)
            {
                return(null);
            }

            delegate_method = (MethodInfo)method_group;

            if (TypeManager.IsNullableType(delegate_method.DeclaringType))
            {
                ec.Report.Error(1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
                                TypeManager.GetFullNameSignature(delegate_method));
                return(null);
            }

            Invocation.IsSpecialMethodInvocation(ec, delegate_method, loc);

            ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;

            if (emg != null)
            {
                delegate_instance_expression = emg.ExtensionExpression;
                Type e_type = delegate_instance_expression.Type;
                if (TypeManager.IsValueType(e_type))
                {
                    ec.Report.Error(1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
                                    TypeManager.CSharpSignature(delegate_method), TypeManager.CSharpName(e_type));
                }
            }

            Type       rt       = TypeManager.TypeToCoreType(delegate_method.ReturnType);
            Expression ret_expr = new TypeExpression(rt, loc);

            if (!Delegate.IsTypeCovariant(ret_expr, (TypeManager.TypeToCoreType(invoke_method.ReturnType))))
            {
                Error_ConversionFailed(ec, delegate_method, ret_expr);
            }

            if (Invocation.IsMethodExcluded(delegate_method, loc))
            {
                ec.Report.SymbolRelatedToPreviousError(delegate_method);
                MethodOrOperator m = TypeManager.GetMethod(delegate_method) as MethodOrOperator;
                if (m != null && m.IsPartialDefinition)
                {
                    ec.Report.Error(762, loc, "Cannot create delegate from partial method declaration `{0}'",
                                    TypeManager.CSharpSignature(delegate_method));
                }
                else
                {
                    ec.Report.Error(1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
                                    TypeManager.CSharpSignature(delegate_method));
                }
            }

            DoResolveInstanceExpression(ec);
            eclass = ExprClass.Value;
            return(this);
        }
        public void Resolve(ResolveContext rc, Parameter p)
        {
            var expr = Resolve(rc);

            if (expr == null)
            {
                return;
            }

            expr = Child;

            if (!(expr is Constant || expr is DefaultValueExpression || (expr is New && ((New)expr).IsDefaultStruct)))
            {
                rc.Report.Error(1736, Location,
                                "The expression being assigned to optional parameter `{0}' must be a constant or default value",
                                p.Name);

                return;
            }

            var parameter_type = p.Type;

            if (type == parameter_type)
            {
                return;
            }

            var res = Convert.ImplicitConversionStandard(rc, expr, parameter_type, Location);

            if (res != null)
            {
                if (TypeManager.IsNullableType(parameter_type) && res is Nullable.Wrap)
                {
                    Nullable.Wrap wrap = (Nullable.Wrap)res;
                    res = wrap.Child;
                    if (!(res is Constant))
                    {
                        rc.Report.Error(1770, Location,
                                        "The expression being assigned to nullable optional parameter `{0}' must be default value",
                                        p.Name);
                        return;
                    }
                }

                if (!expr.IsNull && TypeManager.IsReferenceType(parameter_type) && parameter_type != TypeManager.string_type)
                {
                    rc.Report.Error(1763, Location,
                                    "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
                                    p.Name, parameter_type.GetSignatureForError());

                    return;
                }

                this.expr = res;
                return;
            }

            rc.Report.Error(1750, Location,
                            "Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
                            type.GetSignatureForError(), parameter_type.GetSignatureForError());
        }
Example #5
0
        // <summary>
        //   Resolve is used in method definitions
        // </summary>
        public virtual Type Resolve(IMemberContext rc)
        {
            if (parameter_type != null)
            {
                return(parameter_type);
            }

            if (attributes != null)
            {
                attributes.AttachTo(this, rc);
            }

            TypeExpr texpr = TypeName.ResolveAsTypeTerminal(rc, false);

            if (texpr == null)
            {
                return(null);
            }

            parameter_type = texpr.Type;

            // Ignore all checks for dummy members
            AbstractPropertyEventMethod pem = rc as AbstractPropertyEventMethod;

            if (pem != null && pem.IsDummy)
            {
                return(parameter_type);
            }

            if (default_expr != null)
            {
                ResolveContext ec = new ResolveContext(rc);
                default_expr = default_expr.Resolve(ec);
                if (default_expr != null)
                {
                    Constant value = default_expr as Constant;
                    if (value == null)
                    {
                        if (default_expr != null)
                        {
                            bool is_valid = false;
                            if (default_expr is DefaultValueExpression)
                            {
                                is_valid = true;
                            }
                            else if (default_expr is New && ((New)default_expr).IsDefaultValueType)
                            {
                                is_valid = TypeManager.IsEqual(parameter_type, default_expr.Type) ||
                                           (TypeManager.IsNullableType(parameter_type) &&
                                            Convert.ImplicitNulableConversion(ec, default_expr, parameter_type) != EmptyExpression.Null);
                            }
                            else
                            {
                                rc.Compiler.Report.Error(1736, default_expr.Location,
                                                         "The expression being assigned to optional parameter `{0}' must be a constant or default value",
                                                         Name);
                                is_valid = true;
                            }

                            if (!is_valid)
                            {
                                default_expr = null;
                                ec.Compiler.Report.Error(1763, Location,
                                                         "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
                                                         Name, GetSignatureForError());
                            }
                        }
                    }
                    else
                    {
                        Constant c = value.ConvertImplicitly(parameter_type);
                        if (c == null)
                        {
                            if (parameter_type == TypeManager.object_type)
                            {
                                rc.Compiler.Report.Error(1763, Location,
                                                         "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
                                                         Name, GetSignatureForError());
                            }
                            else
                            {
                                rc.Compiler.Report.Error(1750, Location,
                                                         "Optional parameter value `{0}' cannot be converted to parameter type `{1}'",
                                                         value.GetValue(), GetSignatureForError());
                            }
                            default_expr = null;
                        }
                    }
                }
            }

            if ((modFlags & Parameter.Modifier.ISBYREF) != 0 &&
                TypeManager.IsSpecialType(parameter_type))
            {
                rc.Compiler.Report.Error(1601, Location, "Method or delegate parameter cannot be of type `{0}'",
                                         GetSignatureForError());
                return(null);
            }

            TypeManager.CheckTypeVariance(parameter_type,
                                          (modFlags & Parameter.Modifier.ISBYREF) != 0 ? Variance.None : Variance.Contravariant,
                                          rc);

            if (TypeManager.IsGenericParameter(parameter_type))
            {
                return(parameter_type);
            }

            if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute)
            {
                rc.Compiler.Report.Error(721, Location, "`{0}': static types cannot be used as parameters",
                                         texpr.GetSignatureForError());
                return(parameter_type);
            }

            if ((modFlags & Modifier.This) != 0 && (parameter_type.IsPointer || TypeManager.IsDynamicType(parameter_type)))
            {
                rc.Compiler.Report.Error(1103, Location, "The extension method cannot be of type `{0}'",
                                         TypeManager.CSharpName(parameter_type));
            }

            return(parameter_type);
        }
Example #6
0
        protected override Expression DoResolve(ResolveContext ec)
        {
            constructor_method = Delegate.GetConstructor(ec.Compiler, ec.CurrentType, type);

            var invoke_method = Delegate.GetInvokeMethod(ec.Compiler, type);

            Arguments arguments = CreateDelegateMethodArguments(invoke_method.Parameters, invoke_method.Parameters.Types, loc);

            method_group = method_group.OverloadResolve(ec, ref arguments, this, OverloadResolver.Restrictions.CovariantDelegate);
            if (method_group == null)
            {
                return(null);
            }

            var delegate_method = method_group.BestCandidate;

            if (TypeManager.IsNullableType(delegate_method.DeclaringType))
            {
                ec.Report.Error(1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
                                delegate_method.GetSignatureForError());
                return(null);
            }

            Invocation.IsSpecialMethodInvocation(ec, delegate_method, loc);

            ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;

            if (emg != null)
            {
                method_group.InstanceExpression = emg.ExtensionExpression;
                TypeSpec e_type = emg.ExtensionExpression.Type;
                if (TypeManager.IsValueType(e_type))
                {
                    ec.Report.Error(1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
                                    delegate_method.GetSignatureForError(), TypeManager.CSharpName(e_type));
                }
            }

            TypeSpec   rt       = delegate_method.ReturnType;
            Expression ret_expr = new TypeExpression(rt, loc);

            if (!Delegate.IsTypeCovariant(ret_expr, invoke_method.ReturnType))
            {
                Error_ConversionFailed(ec, delegate_method, ret_expr);
            }

            if (delegate_method.IsConditionallyExcluded(loc))
            {
                ec.Report.SymbolRelatedToPreviousError(delegate_method);
                MethodOrOperator m = delegate_method.MemberDefinition as MethodOrOperator;
                if (m != null && m.IsPartialDefinition)
                {
                    ec.Report.Error(762, loc, "Cannot create delegate from partial method declaration `{0}'",
                                    delegate_method.GetSignatureForError());
                }
                else
                {
                    ec.Report.Error(1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
                                    TypeManager.CSharpSignature(delegate_method));
                }
            }

            var expr = method_group.InstanceExpression;

            if (expr != null && (expr.Type.IsGenericParameter || !TypeManager.IsReferenceType(expr.Type)))
            {
                method_group.InstanceExpression = new BoxedCast(expr, TypeManager.object_type);
            }

            eclass = ExprClass.Value;
            return(this);
        }