IsGenericParameter() public static méthode

public static IsGenericParameter ( System.TypeSpec type ) : bool
type System.TypeSpec
Résultat bool
Exemple #1
0
        static string GetSignatureForDoc(Type type)
        {
#if GMCS_SOURCE
            if (TypeManager.IsGenericParameter(type))
            {
                return((type.DeclaringMethod != null ? "``" : "`") + TypeManager.GenericParameterPosition(type));
            }

            if (TypeManager.IsGenericType(type))
            {
                string g = type.Namespace;
                if (g != null && g.Length > 0)
                {
                    g += '.';
                }
                int idx = type.Name.LastIndexOf('`');
                g += (idx < 0 ? type.Name : type.Name.Substring(0, idx)) + '{';
                int argpos = 0;
                foreach (Type t in type.GetGenericArguments())
                {
                    g += (argpos++ > 0 ? "," : String.Empty) + GetSignatureForDoc(t);
                }
                g += '}';
                return(g);
            }
#endif

            string name = type.FullName != null ? type.FullName : type.Name;
            return(name.Replace("+", ".").Replace('&', '@'));
        }
Exemple #2
0
        public void SymbolRelatedToPreviousError(Type type)
        {
            if (reporting_disabled > 0 || !printer.HasRelatedSymbolSupport)
            {
                return;
            }

            type = TypeManager.DropGenericTypeArguments(type);

            if (TypeManager.IsGenericParameter(type))
            {
                TypeParameter tp = TypeManager.LookupTypeParameter(type);
                if (tp != null)
                {
                    SymbolRelatedToPreviousError(tp.Location, "");
                    return;
                }
            }

            if (type is TypeBuilder)
            {
                DeclSpace temp_ds = TypeManager.LookupDeclSpace(type);
                SymbolRelatedToPreviousError(temp_ds.Location, TypeManager.CSharpName(type));
            }
            else if (TypeManager.HasElementType(type))
            {
                SymbolRelatedToPreviousError(TypeManager.GetElementType(type));
            }
            else
            {
                SymbolRelatedToPreviousError(type.Assembly.Location, TypeManager.CSharpName(type));
            }
        }
Exemple #3
0
        void DoResolveInstanceExpression(ResolveContext ec)
        {
            //
            // Argument is another delegate
            //
            if (delegate_instance_expression != null)
            {
                return;
            }

            if (method_group.IsStatic)
            {
                delegate_instance_expression = null;
                return;
            }

            Expression instance = method_group.InstanceExpression;

            if (instance != null && instance != EmptyExpression.Null)
            {
                delegate_instance_expression = instance;
                Type instance_type = delegate_instance_expression.Type;
                if (TypeManager.IsValueType(instance_type) || TypeManager.IsGenericParameter(instance_type))
                {
                    delegate_instance_expression = new BoxedCast(
                        delegate_instance_expression, TypeManager.object_type);
                }
            }
            else
            {
                delegate_instance_expression = ec.GetThis(loc);
            }
        }
Exemple #4
0
        public virtual bool CheckAccessibility(InterfaceMemberBase member)
        {
            if (parameter_type == null || TypeManager.IsGenericParameter(parameter_type))
            {
                return(true);
            }

            return(member.IsAccessibleAs(parameter_type));
        }
Exemple #5
0
        public override void Emit(EmitContext ec)
        {
            ec.ig.Emit(OpCodes.Ldnull);

#if GMCS_SOURCE
            // Only to make verifier happy
            if (TypeManager.IsGenericParameter(type))
            {
                ec.ig.Emit(OpCodes.Unbox_Any, type);
            }
#endif
        }
Exemple #6
0
        //
        // The stack contains the pointer and the value of type `type'
        //
        public void EmitStoreFromPtr(TypeSpec type)
        {
            if (type.IsEnum)
            {
                type = EnumSpec.GetUnderlyingType(type);
            }

            switch (type.BuiltinType)
            {
            case BuiltinTypeSpec.Type.Int:
            case BuiltinTypeSpec.Type.UInt:
                ig.Emit(OpCodes.Stind_I4);
                return;

            case BuiltinTypeSpec.Type.Long:
            case BuiltinTypeSpec.Type.ULong:
                ig.Emit(OpCodes.Stind_I8);
                return;

            case BuiltinTypeSpec.Type.Char:
            case BuiltinTypeSpec.Type.Short:
            case BuiltinTypeSpec.Type.UShort:
                ig.Emit(OpCodes.Stind_I2);
                return;

            case BuiltinTypeSpec.Type.Float:
                ig.Emit(OpCodes.Stind_R4);
                return;

            case BuiltinTypeSpec.Type.Double:
                ig.Emit(OpCodes.Stind_R8);
                return;

            case BuiltinTypeSpec.Type.Byte:
            case BuiltinTypeSpec.Type.SByte:
            case BuiltinTypeSpec.Type.Bool:
                ig.Emit(OpCodes.Stind_I1);
                return;

            case BuiltinTypeSpec.Type.IntPtr:
                ig.Emit(OpCodes.Stind_I);
                return;
            }

            if (type.IsStruct || TypeManager.IsGenericParameter(type))
            {
                Emit(OpCodes.Stobj, type);
            }
            else
            {
                ig.Emit(OpCodes.Stind_Ref);
            }
        }
Exemple #7
0
 public static void Error_InvalidConstantType(Type t, Location loc, Report Report)
 {
     if (TypeManager.IsGenericParameter(t))
     {
         Report.Error(1959, loc,
                      "Type parameter `{0}' cannot be declared const", TypeManager.CSharpName(t));
     }
     else
     {
         Report.Error(283, loc,
                      "The type `{0}' cannot be declared const", TypeManager.CSharpName(t));
     }
 }
Exemple #8
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));
        }
Exemple #9
0
 public override void Error_ValueCannotBeConverted(EmitContext ec, Location loc, Type t, bool expl)
 {
     if (TypeManager.IsGenericParameter(t))
     {
         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);
     }
     else
     {
         Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
                      TypeManager.CSharpName(t));
     }
 }
        //
        // The stack contains the pointer and the value of type `type'
        //
        public void EmitStoreFromPtr(TypeSpec type)
        {
            if (type.IsEnum)
            {
                type = EnumSpec.GetUnderlyingType(type);
            }

            if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
            {
                ig.Emit(OpCodes.Stind_I4);
            }
            else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
            {
                ig.Emit(OpCodes.Stind_I8);
            }
            else if (type == TypeManager.char_type || type == TypeManager.short_type ||
                     type == TypeManager.ushort_type)
            {
                ig.Emit(OpCodes.Stind_I2);
            }
            else if (type == TypeManager.float_type)
            {
                ig.Emit(OpCodes.Stind_R4);
            }
            else if (type == TypeManager.double_type)
            {
                ig.Emit(OpCodes.Stind_R8);
            }
            else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
                     type == TypeManager.bool_type)
            {
                ig.Emit(OpCodes.Stind_I1);
            }
            else if (type == TypeManager.intptr_type)
            {
                ig.Emit(OpCodes.Stind_I);
            }
            else if (TypeManager.IsStruct(type) || TypeManager.IsGenericParameter(type))
            {
                Emit(OpCodes.Stobj, type);
            }
            else
            {
                ig.Emit(OpCodes.Stind_Ref);
            }
        }
 //
 // Load the object from the pointer.
 //
 public void EmitLoadFromPtr(TypeSpec t)
 {
     if (t == TypeManager.int32_type)
     {
         ig.Emit(OpCodes.Ldind_I4);
     }
     else if (t == TypeManager.uint32_type)
     {
         ig.Emit(OpCodes.Ldind_U4);
     }
     else if (t == TypeManager.short_type)
     {
         ig.Emit(OpCodes.Ldind_I2);
     }
     else if (t == TypeManager.ushort_type)
     {
         ig.Emit(OpCodes.Ldind_U2);
     }
     else if (t == TypeManager.char_type)
     {
         ig.Emit(OpCodes.Ldind_U2);
     }
     else if (t == TypeManager.byte_type)
     {
         ig.Emit(OpCodes.Ldind_U1);
     }
     else if (t == TypeManager.sbyte_type)
     {
         ig.Emit(OpCodes.Ldind_I1);
     }
     else if (t == TypeManager.uint64_type)
     {
         ig.Emit(OpCodes.Ldind_I8);
     }
     else if (t == TypeManager.int64_type)
     {
         ig.Emit(OpCodes.Ldind_I8);
     }
     else if (t == TypeManager.float_type)
     {
         ig.Emit(OpCodes.Ldind_R4);
     }
     else if (t == TypeManager.double_type)
     {
         ig.Emit(OpCodes.Ldind_R8);
     }
     else if (t == TypeManager.bool_type)
     {
         ig.Emit(OpCodes.Ldind_I1);
     }
     else if (t == TypeManager.intptr_type)
     {
         ig.Emit(OpCodes.Ldind_I);
     }
     else if (t.IsEnum)
     {
         if (t == TypeManager.enum_type)
         {
             ig.Emit(OpCodes.Ldind_Ref);
         }
         else
         {
             EmitLoadFromPtr(EnumSpec.GetUnderlyingType(t));
         }
     }
     else if (TypeManager.IsStruct(t) || TypeManager.IsGenericParameter(t))
     {
         Emit(OpCodes.Ldobj, t);
     }
     else if (t.IsPointer)
     {
         ig.Emit(OpCodes.Ldind_I);
     }
     else
     {
         ig.Emit(OpCodes.Ldind_Ref);
     }
 }
Exemple #12
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);
        }
Exemple #13
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);
        }