コード例 #1
0
        //
        // Whether a type is unmanaged.  This is used by the unsafe code (25.2)
        //
        public static bool IsUnmanagedType(TypeSpec t)
        {
            var ds = t.MemberDefinition as DeclSpace;

            if (ds != null)
            {
                return(ds.IsUnmanagedType());
            }

            if (t.Kind == MemberKind.Void)
            {
                return(true);
            }

            // Someone did the work of checking if the ElementType of t is unmanaged.  Let's not repeat it.
            if (t.IsPointer)
            {
                return(IsUnmanagedType(GetElementType(t)));
            }

            if (!TypeSpec.IsValueType(t))
            {
                return(false);
            }

            if (t.IsNested && t.DeclaringType.IsGenericOrParentIsGeneric)
            {
                return(false);
            }

            return(true);
        }
コード例 #2
0
ファイル: delegate.cs プロジェクト: zakipharaon/mono
		protected override Expression DoResolve (ResolveContext ec)
		{
			constructor_method = Delegate.GetConstructor (type);

			var invoke_method = Delegate.GetInvokeMethod (type);

			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 = delegate_method.ReturnType;
			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 (delegate_method.IsConditionallyExcluded (ec, 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 || !TypeSpec.IsReferenceType (expr.Type)))
				method_group.InstanceExpression = new BoxedCast (expr, ec.BuiltinTypes.Object);

			eclass = ExprClass.Value;
			return this;
		}
コード例 #3
0
        public void EmitResultLift(EmitContext ec, TypeSpec type, bool statement)
        {
            if (!NullShortCircuit)
            {
                throw new InternalErrorException();
            }

            bool     value_rt = TypeSpec.IsValueType(type);
            TypeSpec lifted;

            if (value_rt)
            {
                if (type.IsNullableType)
                {
                    lifted = type;
                }
                else
                {
                    lifted = Nullable.NullableInfo.MakeType(ec.Module, type);
                    ec.Emit(OpCodes.Newobj, Nullable.NullableInfo.GetConstructor(lifted));
                }
            }
            else
            {
                lifted = null;
            }

            var end = ec.DefineLabel();

            if (value_on_stack || !statement)
            {
                ec.Emit(OpCodes.Br_S, end);
            }

            ec.MarkLabel(NullOperatorLabel);

            if (value_on_stack)
            {
                ec.Emit(OpCodes.Pop);
            }

            if (!statement)
            {
                if (value_rt)
                {
                    Nullable.LiftedNull.Create(lifted, Location.Null).Emit(ec);
                }
                else
                {
                    ec.EmitNull();
                }
            }

            ec.MarkLabel(end);
        }
コード例 #4
0
        public override void Error_ValueCannotBeConverted(ResolveContext ec, Location loc, TypeSpec t, bool expl)
        {
            if (t.IsGenericParameter)
            {
                ec.Report.Error(403, loc,
                                "Cannot convert null to the type parameter `{0}' because it could be a value " +
                                "type. Consider using `default ({0})' instead", t.Name);
                return;
            }

            if (TypeSpec.IsValueType(t))
            {
                ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
                                TypeManager.CSharpName(t));
                return;
            }

            base.Error_ValueCannotBeConverted(ec, loc, t, expl);
        }