Exemplo n.º 1
0
        public override void Emit(TypeContainer tc)
        {
            base.Emit(tc);

            if (has_extension_method)
            {
                PredefinedAttributes.Get.Extension.EmitAttribute(Builder);
            }

            // FIXME: Does this belong inside SRE.AssemblyBuilder instead?
            PredefinedAttribute pa = PredefinedAttributes.Get.RuntimeCompatibility;

            if (pa.IsDefined && (OptAttributes == null || !OptAttributes.Contains(pa)))
            {
                ConstructorInfo ci = TypeManager.GetPredefinedConstructor(
                    pa.Type, Location.Null, Type.EmptyTypes);
                PropertyInfo [] pis = new PropertyInfo [1];
                pis [0] = TypeManager.GetPredefinedProperty(pa.Type,
                                                            "WrapNonExceptionThrows", Location.Null, TypeManager.bool_type);
                object [] pargs = new object [1];
                pargs [0] = true;
                Builder.SetCustomAttribute(new CustomAttributeBuilder(ci, new object [0], pis, pargs));
            }

            if (declarative_security != null)
            {
                MethodInfo add_permission   = typeof(AssemblyBuilder).GetMethod("AddPermissionRequests", BindingFlags.Instance | BindingFlags.NonPublic);
                object     builder_instance = Builder;

                try {
                    // Microsoft runtime hacking
                    if (add_permission == null)
                    {
                        Type assembly_builder = typeof(AssemblyBuilder).Assembly.GetType("System.Reflection.Emit.AssemblyBuilderData");
                        add_permission = assembly_builder.GetMethod("AddPermissionRequests", BindingFlags.Instance | BindingFlags.NonPublic);

                        FieldInfo fi = typeof(AssemblyBuilder).GetField("m_assemblyData", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
                        builder_instance = fi.GetValue(Builder);
                    }

                    object[] args = new object [] { declarative_security [SecurityAction.RequestMinimum],
                                                    declarative_security [SecurityAction.RequestOptional],
                                                    declarative_security [SecurityAction.RequestRefuse] };
                    add_permission.Invoke(builder_instance, args);
                }
                catch {
                    Report.RuntimeMissingSupport(Location.Null, "assembly permission setting");
                }
            }
        }
Exemplo n.º 2
0
 public bool?CheckAllowDynamic()
 {
     if (OptAttributes != null)
     {
         if (OptAttributes.Contains(Module.PredefinedAttributes.AsAllowDynamicAttribute))
         {
             return(true);
         }
         else if (OptAttributes.Contains(Module.PredefinedAttributes.AsForbidDynamicAttribute))
         {
             return(false);
         }
     }
     return(null);
 }
Exemplo n.º 3
0
        public override void ApplyAttributeBuilder(Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
        {
            if (a.Type == pa.In && ModFlags == Modifier.OUT)
            {
                a.Report.Error(36, a.Location, "An out parameter cannot have the `In' attribute");
                return;
            }

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

            if (a.Type == pa.Out && (ModFlags & Modifier.REF) == Modifier.REF &&
                !OptAttributes.Contains(pa.In))
            {
                a.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)
            {
                a.Report.Warning(3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
            }

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

                if (a.Type == pa.DefaultParameterValue)
                {
                    return;
                }
            }

            base.ApplyAttributeBuilder(a, ctor, cdata, pa);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
        {
            if (a.Type == TypeManager.in_attribute_type && ModFlags == Modifier.OUT)
            {
                Report.Error(36, a.Location, "An out parameter cannot have the `In' attribute");
                return;
            }

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

            if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) == Modifier.REF &&
                TypeManager.in_attribute_type != null && !OptAttributes.Contains(TypeManager.in_attribute_type))
            {
                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 == TypeManager.cls_compliant_attribute_type)
            {
                Report.Warning(3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
            }

            // TypeManager.default_parameter_value_attribute_type is null if !NET_2_0, or if System.dll is not referenced
            if (a.Type == TypeManager.default_parameter_value_attribute_type)
            {
                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 DefaultValue attribute",
                                         TypeManager.CSharpName(t));
                        }
                        else
                        {
                            Report.Error(1909, a.Location, "The DefaultValue attribute is not applicable on parameters of type `{0}'",
                                         TypeManager.CSharpName(parameter_type));;
                        }
                        return;
                    }
                }

                if (parameter_type == TypeManager.object_type ||
                    (val == null && !TypeManager.IsValueType(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);
        }