IsReferenceType() public static méthode

public static IsReferenceType ( System.TypeSpec t ) : bool
t System.TypeSpec
Résultat bool
Exemple #1
0
        bool CanBeVolatile()
        {
            if (TypeManager.IsReferenceType(MemberType))
            {
                return(true);
            }

            switch (MemberType.BuiltinType)
            {
            case BuiltinTypeSpec.Type.Bool:
            case BuiltinTypeSpec.Type.Char:
            case BuiltinTypeSpec.Type.SByte:
            case BuiltinTypeSpec.Type.Byte:
            case BuiltinTypeSpec.Type.Short:
            case BuiltinTypeSpec.Type.UShort:
            case BuiltinTypeSpec.Type.Int:
            case BuiltinTypeSpec.Type.UInt:
            case BuiltinTypeSpec.Type.Float:
            case BuiltinTypeSpec.Type.UIntPtr:
            case BuiltinTypeSpec.Type.IntPtr:
                return(true);
            }

            if (MemberType.IsEnum)
            {
                return(true);
            }

            return(false);
        }
Exemple #2
0
        protected virtual Constant DoResolveValue(ResolveContext ec)
        {
            Constant value = initializer.ResolveAsConstant(ec, this);

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

            Constant c = value.ConvertImplicitly(MemberType);

            if (c == null)
            {
                if (TypeManager.IsReferenceType(MemberType))
                {
                    Error_ConstantCanBeInitializedWithNullOnly(MemberType, Location, GetSignatureForError(), Report);
                }
                else
                {
                    value.Error_ValueCannotBeConverted(ec, Location, MemberType, false);
                }
            }

            return(c);
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
0
        protected virtual Expression DoResolveInitializer(ResolveContext rc)
        {
            if (in_transit)
            {
                field.Compiler.Report.Error(110, expr.Location,
                                            "The evaluation of the constant value for `{0}' involves a circular definition",
                                            GetSignatureForError());

                expr = null;
            }
            else
            {
                in_transit = true;
                expr       = expr.Resolve(rc);
            }

            in_transit = false;

            if (expr != null)
            {
                Constant c = expr as Constant;
                if (c != null)
                {
                    c = field.ConvertInitializer(rc, c);
                }

                if (c == null)
                {
                    if (TypeManager.IsReferenceType(field.MemberType))
                    {
                        Error_ConstantCanBeInitializedWithNullOnly(rc, field.MemberType, expr.Location, GetSignatureForError());
                    }
                    else if (!(expr is Constant))
                    {
                        Error_ExpressionMustBeConstant(rc, expr.Location, GetSignatureForError());
                    }
                    else
                    {
                        expr.Error_ValueCannotBeConverted(rc, expr.Location, field.MemberType, false);
                    }
                }

                expr = c;
            }

            if (expr == null)
            {
                expr = New.Constantify(field.MemberType, Location);
                if (expr == null)
                {
                    expr = Constant.CreateConstantFromValue(field.MemberType, null, Location);
                }
                expr = expr.Resolve(rc);
            }

            return(expr);
        }
Exemple #6
0
        public static bool IsConstantTypeValid(Type t)
        {
            if (TypeManager.IsBuiltinOrEnum(t))
            {
                return(true);
            }

            if (TypeManager.IsGenericParameter(t) || t.IsPointer)
            {
                return(false);
            }

            return(TypeManager.IsReferenceType(t));
        }
        bool CanBeVolatile()
        {
            if (TypeManager.IsReferenceType(MemberType))
            {
                return(true);
            }

            if (MemberType == TypeManager.bool_type || MemberType == TypeManager.char_type ||
                MemberType == TypeManager.sbyte_type || MemberType == TypeManager.byte_type ||
                MemberType == TypeManager.short_type || MemberType == TypeManager.ushort_type ||
                MemberType == TypeManager.int32_type || MemberType == TypeManager.uint32_type ||
                MemberType == TypeManager.float_type ||
                MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
            {
                return(true);
            }

            if (MemberType.IsEnum)
            {
                return(true);
            }

            return(false);
        }
Exemple #8
0
        protected override Expression DoResolve(ResolveContext ec)
        {
            constructor_method = Delegate.GetConstructor(type);

            var invoke_method = Delegate.GetInvokeMethod(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 (delegate_method.DeclaringType.IsNullableType)
            {
                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;

            if (!Delegate.IsTypeCovariant(ec, rt, invoke_method.ReturnType))
            {
                Expression ret_expr = new TypeExpression(rt, loc);
                Error_ConversionFailed(ec, delegate_method, ret_expr);
            }

            if (delegate_method.IsConditionallyExcluded(ec.Module.Compiler, 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, ec.BuiltinTypes.Object);
            }

            eclass = ExprClass.Value;
            return(this);
        }
        public void ResolveDefaultValue(ResolveContext rc)
        {
            //
            // Default value was specified using an expression
            //
            if (default_expr != null)
            {
                ((DefaultParameterValueExpression)default_expr).Resolve(rc, this);
                return;
            }

            if (attributes == null)
            {
                return;
            }

            var opt_attr = attributes.Search(rc.Module.PredefinedAttributes.OptionalParameter);
            var def_attr = attributes.Search(rc.Module.PredefinedAttributes.DefaultParameterValue);

            if (def_attr != null)
            {
                if (def_attr.Resolve() == null)
                {
                    return;
                }

                var default_expr_attr = def_attr.GetParameterDefaultValue();
                if (default_expr_attr == null)
                {
                    return;
                }

                var dpa_rc = def_attr.CreateResolveContext();
                default_expr = default_expr_attr.Resolve(dpa_rc);

                if (default_expr is BoxedCast)
                {
                    default_expr = ((BoxedCast)default_expr).Child;
                }

                Constant c = default_expr as Constant;
                if (c == null)
                {
                    if (parameter_type == TypeManager.object_type)
                    {
                        rc.Report.Error(1910, default_expr.Location,
                                        "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
                                        default_expr.Type.GetSignatureForError());
                    }
                    else
                    {
                        rc.Report.Error(1909, default_expr.Location,
                                        "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
                                        default_expr.Type.GetSignatureForError());;
                    }

                    default_expr = null;
                    return;
                }

                if (TypeSpecComparer.IsEqual(default_expr.Type, parameter_type) ||
                    (default_expr is NullConstant && TypeManager.IsReferenceType(parameter_type) && !parameter_type.IsGenericParameter) ||
                    TypeSpecComparer.IsEqual(parameter_type, TypeManager.object_type))
                {
                    return;
                }

                //
                // LAMESPEC: Some really weird csc behaviour which we have to mimic
                // User operators returning same type as parameter type are considered
                // valid for this attribute only
                //
                // struct S { public static implicit operator S (int i) {} }
                //
                // void M ([DefaultParameterValue (3)]S s)
                //
                var expr = Convert.ImplicitUserConversion(dpa_rc, default_expr, parameter_type, loc);
                if (expr != null && TypeSpecComparer.IsEqual(expr.Type, parameter_type))
                {
                    return;
                }

                rc.Report.Error(1908, default_expr.Location, "The type of the default value should match the type of the parameter");
                return;
            }

            if (opt_attr != null)
            {
                default_expr = EmptyExpression.MissingValue;
            }
        }
        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());
        }
Exemple #11
0
        public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
        {
            Report Report = RootContext.ToplevelTypes.Compiler.Report;

            if (a.Type == pa.In && ModFlags == Modifier.OUT)
            {
                Report.Error(36, a.Location, "An out parameter cannot have the `In' attribute");
                return;
            }

            if (a.Type == pa.ParamArray)
            {
                Report.Error(674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
                return;
            }

            if (a.Type == PredefinedAttributes.Get.Out && (ModFlags & Modifier.REF) == Modifier.REF &&
                !OptAttributes.Contains(pa.In))
            {
                Report.Error(662, a.Location,
                             "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
                return;
            }

            if (a.Type == pa.CLSCompliant)
            {
                Report.Warning(3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
            }

            if (HasDefaultValue && (a.Type == pa.DefaultParameterValue || a.Type == pa.OptionalParameter))
            {
                Report.Error(1745, a.Location,
                             "Cannot specify `{0}' attribute on optional parameter `{1}'",
                             TypeManager.CSharpName(a.Type).Replace("Attribute", ""), Name);
                return;
            }

            if (a.Type == pa.DefaultParameterValue)
            {
                object val = a.GetParameterDefaultValue();
                if (val != null)
                {
                    Type t = val.GetType();
                    if (t.IsArray || TypeManager.IsSubclassOf(t, TypeManager.type_type))
                    {
                        if (parameter_type == TypeManager.object_type)
                        {
                            if (!t.IsArray)
                            {
                                t = TypeManager.type_type;
                            }

                            Report.Error(1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
                                         TypeManager.CSharpName(t));
                        }
                        else
                        {
                            Report.Error(1909, a.Location, "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
                                         TypeManager.CSharpName(parameter_type));;
                        }
                        return;
                    }
                }

                if (parameter_type == TypeManager.object_type ||
                    (val == null && !TypeManager.IsGenericParameter(parameter_type) && TypeManager.IsReferenceType(parameter_type)) ||
                    (val != null && TypeManager.TypeToCoreType(val.GetType()) == parameter_type))
                {
                    builder.SetConstant(val);
                }
                else
                {
                    Report.Error(1908, a.Location, "The type of the default value should match the type of the parameter");
                }
                return;
            }

            base.ApplyAttributeBuilder(a, cb, pa);
        }