예제 #1
0
        public override Expression DoResolve(ParseContext parseContext)
        {
            if (_typeExpression == null)
            {
                return(this);
            }

            var typeExpression = _typeExpression.ResolveAsTypeTerminal(parseContext, false);

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

            _resolvedType = typeExpression.Type;

            if (_resolvedType.IsAbstract && _resolvedType.IsSealed)
            {
                parseContext.ReportError(
                    -244,
                    "The 'default value' operator cannot be applied to an operand of a static type.",
                    Span);
            }

            if (TypeManager.IsReferenceType(_resolvedType))
            {
                return(ConstantExpression.Create(_resolvedType, null, Span));
            }

            Type            = _resolvedType;
            ExpressionClass = ExpressionClass.Variable;

            return(this);
        }
예제 #2
0
        public override Expression DoResolve(ParseContext ec)
        {
            var typeExpression = _requestedType.ResolveAsTypeTerminal(ec, false);

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

            Type = _resolvedType = typeExpression.Type;

            if (Arguments == null)
            {
                var c = Constantify(_resolvedType);
                if (c != null)
                {
                    return(ReducedExpression.Create(c, this));
                }
            }

            if (TypeManager.IsGenericParameter(_resolvedType))
            {
                var gc = TypeManager.GetTypeParameterConstraints(_resolvedType);

                if ((gc == null) || (!gc.HasConstructorConstraint && !gc.IsValueType))
                {
                    ec.ReportError(
                        304,
                        string.Format(
                            "Cannot create an instance of the variable type '{0}' because it doesn't have the new() constraint.",
                            TypeManager.GetCSharpName(_resolvedType)),
                        Span);

                    return(null);
                }

                if ((_arguments != null) && (_arguments.Count != 0))
                {
                    ec.ReportError(
                        417,
                        string.Format(
                            "'{0}': cannot provide arguments when creating an instance of a variable type.",
                            TypeManager.GetCSharpName(_resolvedType)),
                        Span);

                    return(null);
                }

                ExpressionClass = ExpressionClass.Value;
                return(this);
            }

            if (_resolvedType.IsAbstract && _resolvedType.IsSealed)
            {
                ec.ReportError(
                    712,
                    string.Format(
                        "Cannot create an instance of the static class '{0}'.",
                        TypeManager.GetCSharpName(_resolvedType)),
                    Span);

                return(null);
            }

            if (_resolvedType.IsInterface || _resolvedType.IsAbstract)
            {
                ec.ReportError(
                    144,
                    string.Format(
                        "Cannot create an instance of the abstract class or interface '{0}'.",
                        TypeManager.GetCSharpName(_resolvedType)),
                    Span);

                return(null);
            }

            var isStruct = TypeManager.IsStruct(_resolvedType);

            ExpressionClass = ExpressionClass.Value;

            //
            // SRE returns a match for .ctor () on structs (the object constructor),
            // so we have to manually ignore it.
            //
            if (isStruct && (_arguments != null) && !_arguments.Any())
            {
                return(this);
            }

            // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
            var memberLookup = MemberLookupFinal(
                ec,
                _resolvedType,
                _resolvedType,
                ConstructorInfo.ConstructorName,
                MemberTypes.Constructor,
                AllBindingFlags | BindingFlags.DeclaredOnly,
                Span);

            if (_arguments != null)
            {
                _arguments.Resolve(ec);
            }

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

            _constructor = memberLookup as MethodGroupExpression;

            if (_constructor == null)
            {
                memberLookup.OnErrorUnexpectedKind(ec, ResolveFlags.MethodGroup, Span);
                return(null);
            }

            _constructor = _constructor.OverloadResolve(ec, ref _arguments, false, Span);

            return((_constructor == null) ? null : this);
        }