diff --git a/src/error.cs b/src/error.cs index c6bd9cfe404b..d2e845b9f783 100644 --- a/src/error.cs +++ b/src/error.cs @@ -57,6 +57,10 @@ // BI1040 The selector {0} on type {1} is found multiple times with different argument out states on argument {2}. // BI1041 The selector {0} on type {1} is found multiple times with different argument types on argument {2} - {3} : {4}. // BI1042 Missing '[Field (LibraryName=value)]' for {field_pi.Name} (e.g."__Internal") +// BI1043 Repeated overload {mi.Name} and no [DelegateApiNameAttribute] provided to generate property name on host class. +// BI1044 Repeated name '{apiName.Name}' provided in [DelegateApiNameAttribute]. +// BI1045 Only a single [DefaultEnumValue] attribute can be used inside enum {type.Name}. +// BI1046 The [Field] constant {fa.SymbolName} cannot only be used once inside enum {type.Name}. // BI11xx warnings // BI1101 Trying to use a string as a [Target] // BI1102 Using the deprecated EventArgs for a delegate signature in {0}.{1}, please use DelegateName instead diff --git a/src/generator-enums.cs b/src/generator-enums.cs index 39cee8f3dde0..92d92f5211d5 100644 --- a/src/generator-enums.cs +++ b/src/generator-enums.cs @@ -21,6 +21,14 @@ public ErrorDomainAttribute (string domain) public string ErrorDomain { get; set; } } +[AttributeUsage (AttributeTargets.Field)] +public class DefaultEnumValueAttribute : Attribute { + + public DefaultEnumValueAttribute () + { + } +} + public partial class Generator { static string GetCSharpTypeName (Type type) @@ -47,6 +55,12 @@ static string GetCSharpTypeName (Type type) } } + void CopyObsolete (ICustomAttributeProvider provider) + { + foreach (ObsoleteAttribute oa in provider.GetCustomAttributes (typeof (ObsoleteAttribute), false)) + print ("[Obsolete (\"{0}\", {1})]", oa.Message, oa.IsError ? "true" : "false"); + } + // caller already: // - setup the header and namespace // - call/emit PrintPlatformAttributes on the type @@ -62,25 +76,44 @@ void GenerateEnum (Type type) else print ("[Native (\"{0}\")]", native.NativeName); } + CopyObsolete (type); + var unique_constants = new HashSet (); var fields = new Dictionary (); - print ("public enum {0} : {1} {{", type.Name, GetCSharpTypeName (Enum.GetUnderlyingType (type))); + Tuple null_field = null; + Tuple default_symbol = null; + var underlying_type = GetCSharpTypeName (Enum.GetUnderlyingType (type)); + print ("public enum {0} : {1} {{", type.Name, underlying_type); indent++; foreach (var f in type.GetFields ()) { // skip value__ field if (f.IsSpecialName) continue; PrintPlatformAttributes (f); + CopyObsolete (f); print ("{0} = {1},", f.Name, f.GetRawConstantValue ()); var fa = GetAttribute (f); if (fa == null) continue; if (f.IsUnavailable ()) continue; - fields.Add (f, fa); + if (fa.SymbolName == null) + null_field = new Tuple (f, fa); + else if (unique_constants.Contains (fa.SymbolName)) + throw new BindingException (1046, true, $"The [Field] constant {fa.SymbolName} cannot only be used once inside enum {type.Name}."); + else { + fields.Add (f, fa); + unique_constants.Add (fa.SymbolName); + } + if (GetAttribute (f) != null) { + if (default_symbol != null) + throw new BindingException (1045, true, $"Only a single [DefaultEnumValue] attribute can be used inside enum {type.Name}."); + default_symbol = new Tuple (f, fa); + } } indent--; print ("}"); + unique_constants.Clear (); var library_name = type.Namespace; var error = GetAttribute (type); @@ -89,7 +122,7 @@ void GenerateEnum (Type type) // the *Extensions has the same version requirement as the enum itself PrintPlatformAttributes (type); print ("[CompilerGenerated]"); - print ("static public class {0}Extensions {{", type.Name); + print ("static public partial class {0}Extensions {{", type.Name); indent++; // note: not every binding namespace will start with ns.Prefix (e.g. MonoTouch.) @@ -146,13 +179,23 @@ void GenerateEnum (Type type) print ("public static NSString GetConstant (this {0} self)", type.Name); print ("{"); indent++; + print ("switch (({0}) self) {{", underlying_type); + var default_symbol_name = default_symbol?.Item2.SymbolName; + // more than one enum member can share the same numeric value - ref: #46285 foreach (var kvp in fields) { - print ("if (self == {0}.{1})", type.Name, kvp.Key.Name); + print ("case {0}: // {1}.{2}", Convert.ToInt64 (kvp.Key.GetValue (null)), type.Name, kvp.Key.Name); + var sn = kvp.Value.SymbolName; + if (sn == default_symbol_name) + print ("default:"); indent++; - print ("return {0};", kvp.Value.SymbolName); + print ("return {0};", sn); indent--; } - print ("return null;"); + print ("}"); + if (default_symbol_name == null) { + // note: a `[Field (null)]` does not need extra code + print ("return null;"); + } indent--; print ("}"); @@ -163,7 +206,11 @@ void GenerateEnum (Type type) indent++; print ("if (constant == null)"); indent++; - print ("throw new ArgumentNullException (nameof (constant));"); + // if we do not have a enum value that maps to a null field then we throw + if (null_field == null) + print ("throw new ArgumentNullException (nameof (constant));"); + else + print ("return {0}.{1};", type.Name, null_field.Item1.Name); indent--; foreach (var kvp in fields) { print ("else if (constant == {0})", kvp.Value.SymbolName); @@ -171,7 +218,11 @@ void GenerateEnum (Type type) print ("return {0}.{1};", type.Name, kvp.Key.Name); indent--; } - print ("throw new NotSupportedException (constant + \" has no associated enum value in \" + nameof ({0}) + \" on this platform.\");", type.Name); + // if there's no default then we throw on unknown constants + if (default_symbol == null) + print ("throw new NotSupportedException (constant + \" has no associated enum value in \" + nameof ({0}) + \" on this platform.\");", type.Name); + else + print ("return {0}.{1};", type.Name, default_symbol.Item1.Name); indent--; print ("}"); } diff --git a/tests/generator/Makefile b/tests/generator/Makefile index 96ae45c6c0e8..4b75a66389e2 100644 --- a/tests/generator/Makefile +++ b/tests/generator/Makefile @@ -14,7 +14,7 @@ include $(TOP)/Make.config IOS_CURRENT_DIR=$(IOS_DESTDIR)/Library/Frameworks/Xamarin.iOS.framework/Versions/Current IOS_GENERATOR = $(IOS_CURRENT_DIR)/bin/btouch /baselib:$(IOS_CURRENT_DIR)/lib/mono/2.1/monotouch.dll /unsafe /compiler:$(IOS_CURRENT_DIR)/bin/smcs IOS_TESTS = bug15283 bug15307 bug15799 bug16036 sof20696157 bug23041 bug27430 bug27428 bug34042 btouch-with-hyphen-in-name property-redefination-ios arrayfromhandlebug bug36457 bug39614 bug40282 bug17232 bug24078-ignore-methods-events strong-dict-support-templated-dicts -IOS_CUSTOM_TESTS = forum54078 desk63279 desk79124 multiple-api-definitions1 multiple-api-definitions2 bug29493 classNameCollision bi1036 bug37527 bug27986 bug35176 +IOS_CUSTOM_TESTS = forum54078 desk63279 desk79124 multiple-api-definitions1 multiple-api-definitions2 bug29493 classNameCollision bi1036 bug37527 bug27986 bug35176 bi1046 MAC_CURRENT_DIR=$(MAC_DESTDIR)/Library/Frameworks/Xamarin.Mac.framework/Versions/Current MAC_GENERATOR = $(MAC_CURRENT_DIR)/bin/bmac @@ -121,9 +121,23 @@ bug35176: echo "Error: Expected 4 Introduced attributes in generated code."; exit 1; \ fi +bug46292: + @rm -Rf $@.tmpdir + @mkdir -p $@.tmpdir + $(if $(V),,@echo "$@";) $(IOS_GENERATOR) --sourceonly:$@.source -tmpdir=$@.tmpdir $@.cs --process-enums + @if ! grep -r Obsolete bug46292.tmpdir/BindingTests > /dev/null; then \ + echo "error: Could not find Obsolete attribute in generated code."; exit 1; \ + fi + @if [ `grep -r Obsolete bug46292.tmpdir/BindingTests | wc -l` -ne 2 ]; then \ + echo "Error: Expected 2 Obsolete attributes in generated code."; exit 1; \ + fi + bi1036: $(if $(V),,@echo "$@";) $(IOS_GENERATOR) $@.cs | grep BI1036 >/dev/null 2>&1 +bi1046: + $(if $(V),,@echo "$@";) $(IOS_GENERATOR) $@.cs --process-enums | grep BI1046 >/dev/null 2>&1 + clean-local:: rm -f *.dll *.source rm -Rf *.tmpdir diff --git a/tests/generator/bi1046.cs b/tests/generator/bi1046.cs new file mode 100644 index 000000000000..9482bf2dc370 --- /dev/null +++ b/tests/generator/bi1046.cs @@ -0,0 +1,13 @@ +using System; +using MonoTouch.Foundation; + +namespace BindingTests +{ + public enum HMAccessoryCategoryType { + [Field ("HMAccessoryCategoryTypeGarageDoorOpener")] + DoorOpener, + + [Field ("HMAccessoryCategoryTypeGarageDoorOpener")] + GarageDoorOpener = DoorOpener, + } +} diff --git a/tests/generator/bug46292.cs b/tests/generator/bug46292.cs new file mode 100644 index 000000000000..57b59a46f65e --- /dev/null +++ b/tests/generator/bug46292.cs @@ -0,0 +1,13 @@ +using System; +using MonoTouch.Foundation; + +namespace BindingTests { + [Obsolete ("Type level obsolete must also be copied")] + public enum HMAccessoryCategoryType : int { + [Field ("HMAccessoryCategoryTypeGarageDoorOpener")] + GarageDoorOpener = 0, + + [Obsolete ("Use GarageDoorOpener")] + DoorOpener = GarageDoorOpener, + } +} \ No newline at end of file