public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) { if (a.Type == pa.AttributeUsage) { if (!BaseType.IsAttribute && spec.BuiltinType != BuiltinTypeSpec.Type.Attribute) { Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ()); } } if (a.Type == pa.Conditional && !BaseType.IsAttribute) { Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes"); return; } if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) { a.Error_MissingGuidAttribute (); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } if (a.Type.IsConditionallyExcluded (this, Location)) return; base.ApplyAttributeBuilder (a, ctor, cdata, pa); }
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) { if (a.Type == pa.Conditional) { if (IsExplicitImpl) { Error_ConditionalAttributeIsNotValid (); return; } if ((ModFlags & Modifiers.OVERRIDE) != 0) { Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ()); return; } if (ReturnType.Kind != MemberKind.Void) { Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ()); return; } if (IsInterface) { Report.Error (582, Location, "Conditional not valid on interface members"); return; } if (MethodData.implementing != null) { Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType); Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'", GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing)); return; } for (int i = 0; i < parameters.Count; ++i) { if ((parameters.FixedParameters [i].ModFlags & Parameter.Modifier.OUT) != 0) { Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ()); return; } } } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } base.ApplyAttributeBuilder (a, ctor, cdata, pa); }
public void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) { if (a.IsValidSecurityAttribute ()) { if (declarative_security == null) declarative_security = new Dictionary<SecurityAction, PermissionSet> (); a.ExtractSecurityPermissionSet (declarative_security); return; } if (a.Type == pa.AssemblyCulture) { string value = a.GetString (); if (value == null || value.Length == 0) return; if (RootContext.Target == Target.Exe) { a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty"); return; } if (value == "neutral") value = ""; if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetCulture (value, a.Location); } return; } if (a.Type == pa.AssemblyVersion) { string value = a.GetString (); if (value == null || value.Length == 0) return; var vinfo = IsValidAssemblyVersion (value.Replace ('*', '0')); if (vinfo == null) { a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value)); return; } if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetVersion (vinfo, a.Location); } return; } if (a.Type == pa.AssemblyAlgorithmId) { const int pos = 2; // skip CA header uint alg = (uint) cdata [pos]; alg |= ((uint) cdata [pos + 1]) << 8; alg |= ((uint) cdata [pos + 2]) << 16; alg |= ((uint) cdata [pos + 3]) << 24; if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetAlgorithmId (alg, a.Location); } return; } if (a.Type == pa.AssemblyFlags) { const int pos = 2; // skip CA header uint flags = (uint) cdata[pos]; flags |= ((uint) cdata [pos + 1]) << 8; flags |= ((uint) cdata [pos + 2]) << 16; flags |= ((uint) cdata [pos + 3]) << 24; // Ignore set PublicKey flag if assembly is not strongnamed if ((flags & (uint) AssemblyNameFlags.PublicKey) != 0 && public_key == null) flags &= ~(uint) AssemblyNameFlags.PublicKey; if (RootContext.Target == Target.Module) { SetCustomAttribute (ctor, cdata); } else { builder_extra.SetFlags (flags, a.Location); } return; } if (a.Type == pa.TypeForwarder) { TypeSpec t = a.GetArgumentType (); if (t == null || TypeManager.HasElementType (t)) { Report.Error (735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute"); return; } if (emitted_forwarders == null) { emitted_forwarders = new Dictionary<ITypeDefinition, Attribute> (); } else if (emitted_forwarders.ContainsKey (t.MemberDefinition)) { Report.SymbolRelatedToPreviousError (emitted_forwarders[t.MemberDefinition].Location, null); Report.Error (739, a.Location, "A duplicate type forward of type `{0}'", TypeManager.CSharpName (t)); return; } emitted_forwarders.Add (t.MemberDefinition, a); if (t.MemberDefinition.DeclaringAssembly == this) { Report.SymbolRelatedToPreviousError (t); Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly", TypeManager.CSharpName (t)); return; } if (t.IsNested) { Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type", TypeManager.CSharpName (t)); return; } builder_extra.AddTypeForwarder (t, a.Location); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } if (a.Type == pa.InternalsVisibleTo) { string assembly_name = a.GetString (); if (assembly_name.Length == 0) return; AssemblyName aname = null; try { aname = new AssemblyName (assembly_name); } catch (Exception) { Report.Warning (1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved", assembly_name); return; } if (aname.Version != null || aname.CultureInfo != null || aname.ProcessorArchitecture != ProcessorArchitecture.None) { Report.Error (1725, a.Location, "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified", assembly_name); return; } // TODO: GetPublicKey () does not work on .NET when AssemblyName is constructed from a string if (public_key != null && aname.GetPublicKey () == null) { Report.Error (1726, a.Location, "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations", assembly_name); return; } } else if (a.Type == pa.RuntimeCompatibility) { wrap_non_exception_throws_custom = true; } SetCustomAttribute (ctor, cdata); }
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa) { if (a.IsValidSecurityAttribute ()) { if (declarative_security == null) declarative_security = new Dictionary<SecurityAction, PermissionSet> (); a.ExtractSecurityPermissionSet (declarative_security); return; } if (a.Type == pa.AssemblyCulture) { string value = a.GetString (); if (value == null || value.Length == 0) return; if (RootContext.Target == Target.Exe) { a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty"); return; } try { var fi = typeof (AssemblyBuilder).GetField ("culture", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField); fi.SetValue (CodeGen.Assembly.Builder, value == "neutral" ? "" : value); } catch { Report.RuntimeMissingSupport (a.Location, "AssemblyCultureAttribute setting"); } return; } if (a.Type == pa.AssemblyVersion) { string value = a.GetString (); if (value == null || value.Length == 0) return; var vinfo = IsValidAssemblyVersion (value.Replace ('*', '0')); if (vinfo == null) { a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value)); return; } try { var fi = typeof (AssemblyBuilder).GetField ("version", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField); fi.SetValue (CodeGen.Assembly.Builder, vinfo); } catch { Report.RuntimeMissingSupport (a.Location, "AssemblyVersionAttribute setting"); } return; } if (a.Type == pa.AssemblyAlgorithmId) { const int pos = 2; // skip CA header uint alg = (uint) cdata [pos]; alg |= ((uint) cdata [pos + 1]) << 8; alg |= ((uint) cdata [pos + 2]) << 16; alg |= ((uint) cdata [pos + 3]) << 24; try { var fi = typeof (AssemblyBuilder).GetField ("algid", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField); fi.SetValue (CodeGen.Assembly.Builder, alg); } catch { Report.RuntimeMissingSupport (a.Location, "AssemblyAlgorithmIdAttribute setting"); } return; } if (a.Type == pa.AssemblyFlags) { const int pos = 2; // skip CA header uint flags = (uint) cdata[pos]; flags |= ((uint) cdata[pos + 1]) << 8; flags |= ((uint) cdata[pos + 2]) << 16; flags |= ((uint) cdata[pos + 3]) << 24; // Ignore set PublicKey flag if assembly is not strongnamed if ((flags & (uint) AssemblyNameFlags.PublicKey) != 0 && (CodeGen.Assembly.Builder.GetName ().KeyPair == null)) flags &= ~(uint)AssemblyNameFlags.PublicKey; try { var fi = typeof (AssemblyBuilder).GetField ("flags", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField); fi.SetValue (CodeGen.Assembly.Builder, flags); } catch { Report.RuntimeMissingSupport (a.Location, "AssemblyFlagsAttribute setting"); } return; } if (a.Type == pa.InternalsVisibleTo && !CheckInternalsVisibleAttribute (a)) return; if (a.Type == pa.TypeForwarder) { TypeSpec t = a.GetArgumentType (); if (t == null || TypeManager.HasElementType (t)) { Report.Error (735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute"); return; } if (emitted_forwarders == null) { emitted_forwarders = new Dictionary<ITypeDefinition, Attribute> (); } else if (emitted_forwarders.ContainsKey (t.MemberDefinition)) { Report.SymbolRelatedToPreviousError(emitted_forwarders[t.MemberDefinition].Location, null); Report.Error(739, a.Location, "A duplicate type forward of type `{0}'", TypeManager.CSharpName(t)); return; } emitted_forwarders.Add(t.MemberDefinition, a); if (t.Assembly == Builder) { Report.SymbolRelatedToPreviousError (t); Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly", TypeManager.CSharpName (t)); return; } if (t.IsNested) { Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type", TypeManager.CSharpName (t)); return; } if (add_type_forwarder == null) { add_type_forwarder = typeof (AssemblyBuilder).GetMethod ("AddTypeForwarder", BindingFlags.NonPublic | BindingFlags.Instance); if (add_type_forwarder == null) { Report.RuntimeMissingSupport (a.Location, "TypeForwardedTo attribute"); return; } } add_type_forwarder.Invoke (Builder, new object[] { t.GetMetaInfo () }); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } Builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata); }
public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa) { if (a.IsValidSecurityAttribute ()) { if (declarative_security == null) declarative_security = new ListDictionary (); a.ExtractSecurityPermissionSet (declarative_security); return; } if (a.Type == pa.AssemblyCulture) { string value = a.GetString (); if (value == null || value.Length == 0) return; if (RootContext.Target == Target.Exe) { a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty"); return; } } if (a.Type == pa.AssemblyVersion) { string value = a.GetString (); if (value == null || value.Length == 0) return; value = value.Replace ('*', '0'); if (!IsValidAssemblyVersion (value)) { a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value)); return; } } if (a.Type == pa.InternalsVisibleTo && !CheckInternalsVisibleAttribute (a)) return; if (a.Type == pa.TypeForwarder) { Type t = a.GetArgumentType (); if (t == null || TypeManager.HasElementType (t)) { Report.Error (735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute"); return; } t = TypeManager.DropGenericTypeArguments (t); if (emitted_forwarders == null) { emitted_forwarders = new ListDictionary(); } else if (emitted_forwarders.Contains(t)) { Report.SymbolRelatedToPreviousError(((Attribute)emitted_forwarders[t]).Location, null); Report.Error(739, a.Location, "A duplicate type forward of type `{0}'", TypeManager.CSharpName(t)); return; } emitted_forwarders.Add(t, a); if (TypeManager.LookupDeclSpace (t) != null) { Report.SymbolRelatedToPreviousError (t); Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly", TypeManager.CSharpName (t)); return; } if (t.DeclaringType != null) { Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type", TypeManager.CSharpName (t)); return; } if (add_type_forwarder == null) { add_type_forwarder = typeof (AssemblyBuilder).GetMethod ("AddTypeForwarder", BindingFlags.NonPublic | BindingFlags.Instance); if (add_type_forwarder == null) { Report.RuntimeMissingSupport (a.Location, "TypeForwardedTo attribute"); return; } } add_type_forwarder.Invoke (Builder, new object[] { t }); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } Builder.SetCustomAttribute (cb); }
public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa) { if (a.Type == pa.AttributeUsage) { if (!TypeManager.IsAttributeType (BaseType) && TypeBuilder.FullName != "System.Attribute") { Report.Error (641, a.Location, "Attribute `{0}' is only valid on classes derived from System.Attribute", a.GetSignatureForError ()); } } if (a.Type == pa.Conditional && !TypeManager.IsAttributeType (BaseType)) { Report.Error (1689, a.Location, "Attribute `System.Diagnostics.ConditionalAttribute' is only valid on methods or attribute classes"); return; } if (a.Type == pa.ComImport && !attributes.Contains (pa.Guid)) { a.Error_MissingGuidAttribute (); return; } if (a.Type == pa.Extension) { a.Error_MisusedExtensionAttribute (); return; } if (AttributeTester.IsAttributeExcluded (a.Type, Location)) return; base.ApplyAttributeBuilder (a, cb, pa); }