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); }
bool CheckInternalsVisibleAttribute (Attribute a) { string assembly_name = a.GetString (); if (assembly_name.Length == 0) return false; AssemblyName aname = null; try { aname = new AssemblyName (assembly_name); } catch (FileLoadException) { } catch (ArgumentException) { } // Bad assembly name format if (aname == null) Report.Warning (1700, 3, a.Location, "Assembly reference `" + assembly_name + "' is invalid and cannot be resolved"); // Report error if we have defined Version or Culture else if (aname.Version != null || aname.CultureInfo != null) throw new Exception ("Friend assembly `" + a.GetString () + "' is invalid. InternalsVisibleTo cannot have version or culture specified."); else if (aname.GetPublicKey () == null && Name.GetPublicKey () != null && Name.GetPublicKey ().Length != 0) { Report.Error (1726, a.Location, "Friend assembly reference `" + aname.FullName + "' is invalid." + " Strong named assemblies must specify a public key in their InternalsVisibleTo declarations"); return false; } return true; }
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); }