Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 59 additions & 8 deletions src/generator-enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -62,25 +76,44 @@ void GenerateEnum (Type type)
else
print ("[Native (\"{0}\")]", native.NativeName);
}
CopyObsolete (type);

var unique_constants = new HashSet<string> ();
var fields = new Dictionary<FieldInfo, FieldAttribute> ();
print ("public enum {0} : {1} {{", type.Name, GetCSharpTypeName (Enum.GetUnderlyingType (type)));
Tuple<FieldInfo, FieldAttribute> null_field = null;
Tuple<FieldInfo, FieldAttribute> 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<FieldAttribute> (f);
if (fa == null)
continue;
if (f.IsUnavailable ())
continue;
fields.Add (f, fa);
if (fa.SymbolName == null)
null_field = new Tuple<FieldInfo, FieldAttribute> (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<DefaultEnumValueAttribute> (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<FieldInfo, FieldAttribute> (f, fa);
}
}
indent--;
print ("}");
unique_constants.Clear ();

var library_name = type.Namespace;
var error = GetAttribute<ErrorDomainAttribute> (type);
Expand All @@ -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.)
Expand Down Expand Up @@ -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 ("}");

Expand All @@ -163,15 +206,23 @@ 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);
indent++;
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 ("}");
}
Expand Down
16 changes: 15 additions & 1 deletion tests/generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions tests/generator/bi1046.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using MonoTouch.Foundation;

namespace BindingTests
{
public enum HMAccessoryCategoryType {
[Field ("HMAccessoryCategoryTypeGarageDoorOpener")]
DoorOpener,

[Field ("HMAccessoryCategoryTypeGarageDoorOpener")]
GarageDoorOpener = DoorOpener,
}
}
13 changes: 13 additions & 0 deletions tests/generator/bug46292.cs
Original file line number Diff line number Diff line change
@@ -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,
}
}