Esempio n. 1
0
        public void PrepareCleanup(EmitContext ec)
        {
            IsAvailableForReuse = true;

            //
            // Release any captured reference type stack variables
            // to imitate real stack behavour and help GC stuff early
            //
            if (TypeSpec.IsReferenceType(type))
            {
                ec.AddStatementEpilog(this);
            }
        }
Esempio n. 2
0
        bool RequiresBoxing()
        {
            var instance_type = instance.Type;

            if (instance_type.IsGenericParameter && !(instance is This) && TypeSpec.IsReferenceType(instance_type))
            {
                return(true);
            }

            if (instance_type.IsStructOrEnum)
            {
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        bool CanBeVolatile()
        {
            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 (TypeSpec.IsReferenceType(MemberType))
            {
                return(true);
            }

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

            if (MemberType.IsEnum)
            {
                switch (EnumSpec.GetUnderlyingType(MemberType).BuiltinType)
                {
                case BuiltinTypeSpec.Type.SByte:
                case BuiltinTypeSpec.Type.Byte:
                case BuiltinTypeSpec.Type.Short:
                case BuiltinTypeSpec.Type.UShort:
                case BuiltinTypeSpec.Type.Int:
                case BuiltinTypeSpec.Type.UInt:
                    return(true);

                default:
                    return(false);
                }
            }

            return(false);
        }
Esempio n. 4
0
        public override void Emit(EmitContext ec)
        {
            base.Emit(ec);

            var field = (Field)spec.MemberDefinition;

            field.IsAvailableForReuse = true;

            //
            // Release any captured reference type stack variables
            // to imitate real stack behavour and help GC stuff early
            //
            if (TypeSpec.IsReferenceType(type))
            {
                ec.AddStatementEpilog(this);
            }
        }
Esempio n. 5
0
        public void EmitLoad(EmitContext ec)
        {
            var instance_type = instance.Type;

            //
            // Push the instance expression
            //
            if (addressRequired)
            {
                //
                // If the expression implements IMemoryLocation, then
                // we can optimize and use AddressOf on the
                // return.
                //
                // If not we have to use some temporary storage for
                // it.
                var iml = instance as IMemoryLocation;
                if (iml != null)
                {
                    iml.AddressOf(ec, AddressOp.Load);
                }
                else
                {
                    LocalTemporary temp = new LocalTemporary(instance_type);
                    instance.Emit(ec);
                    temp.Store(ec);
                    temp.AddressOf(ec, AddressOp.Load);
                }

                return;
            }

            instance.Emit(ec);

            // Only to make verifier happy
            if (instance_type.IsGenericParameter && !(instance is This) && TypeSpec.IsReferenceType(instance_type))
            {
                ec.Emit(OpCodes.Box, instance_type);
            }
            else if (instance_type.IsStructOrEnum)
            {
                ec.Emit(OpCodes.Box, instance_type);
            }
        }
Esempio n. 6
0
        protected override Expression DoResolve(ResolveContext ec)
        {
            constructor_method = Delegate.GetConstructor(type);

            var invoke_method = Delegate.GetInvokeMethod(type);

            ResolveConditionalAccessReceiver(ec);

            Arguments arguments = CreateDelegateMethodArguments(ec, 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);
            }

            if (!AllowSpecialMethodsInvocation)
            {
                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 (TypeSpec.IsValueType(e_type))
                {
                    ec.Report.Error(1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
                                    delegate_method.GetSignatureForError(), e_type.GetSignatureForError());
                }
            }

            TypeSpec rt = method_group.BestCandidateReturnType;

            if (rt.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
            {
                rt = ec.BuiltinTypes.Object;
            }

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

            if (method_group.IsConditionallyExcluded)
            {
                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 || !TypeSpec.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);
                if (attributes != null)
                {
                    ResolveCallerAttributes(rc);
                }

                return;
            }

            if (attributes == null)
            {
                return;
            }

            var pa       = rc.Module.PredefinedAttributes;
            var def_attr = attributes.Search(pa.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.BuiltinType == BuiltinTypeSpec.Type.Object)
                    {
                        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 && TypeSpec.IsReferenceType(parameter_type) && !parameter_type.IsGenericParameter) ||
                    parameter_type.BuiltinType == BuiltinTypeSpec.Type.Object)
                {
                    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;
            }

            var opt_attr = attributes.Search(pa.OptionalParameter);

            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 (parameter_type.IsNullableType && 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 && TypeSpec.IsReferenceType(parameter_type) && parameter_type.BuiltinType != BuiltinTypeSpec.Type.String)
                {
                    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());
        }
Esempio n. 9
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 (expr is DefaultLiteralExpression)
                    {
                        // It's handled bellow in New.Constantify
                    }
                    else if (TypeSpec.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, 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);
        }