[generator] Add a TypeManager.GetUnderlyingEnumType to abstract away ikvm and reflection differences. (#1786)
Generator diff: https://gist.github.com/rolfbjarne/94dbf673e284197845766cc698a64ff3
This commit is contained in:
Родитель
e35229f91b
Коммит
d5e8238822
|
@ -60,7 +60,7 @@ public partial class Generator {
|
||||||
var fields = new Dictionary<FieldInfo, FieldAttribute> ();
|
var fields = new Dictionary<FieldInfo, FieldAttribute> ();
|
||||||
Tuple<FieldInfo, FieldAttribute> null_field = null;
|
Tuple<FieldInfo, FieldAttribute> null_field = null;
|
||||||
Tuple<FieldInfo, FieldAttribute> default_symbol = null;
|
Tuple<FieldInfo, FieldAttribute> default_symbol = null;
|
||||||
var underlying_type = GetCSharpTypeName (Enum.GetUnderlyingType (type));
|
var underlying_type = GetCSharpTypeName (TypeManager.GetUnderlyingEnumType (type));
|
||||||
print ("public enum {0} : {1} {{", type.Name, underlying_type);
|
print ("public enum {0} : {1} {{", type.Name, underlying_type);
|
||||||
indent++;
|
indent++;
|
||||||
foreach (var f in type.GetFields ()) {
|
foreach (var f in type.GetFields ()) {
|
||||||
|
|
|
@ -230,6 +230,11 @@ public static class TypeManager {
|
||||||
return AttributeManager.HasAttribute (pi, OutAttribute);
|
return AttributeManager.HasAttribute (pi, OutAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Type GetUnderlyingEnumType (Type type)
|
||||||
|
{
|
||||||
|
return Enum.GetUnderlyingType (type);
|
||||||
|
}
|
||||||
|
|
||||||
public static void Initialize (Assembly api)
|
public static void Initialize (Assembly api)
|
||||||
{
|
{
|
||||||
api_assembly = api;
|
api_assembly = api;
|
||||||
|
|
|
@ -973,7 +973,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
if (t.IsEnum) {
|
if (t.IsEnum) {
|
||||||
var enumType = t;
|
var enumType = t;
|
||||||
|
|
||||||
t = Enum.GetUnderlyingType (t);
|
t = TypeManager.GetUnderlyingEnumType (t);
|
||||||
|
|
||||||
if (UnifiedAPI && HasAttribute (enumType, TypeManager.NativeAttribute)) {
|
if (UnifiedAPI && HasAttribute (enumType, TypeManager.NativeAttribute)) {
|
||||||
if (t != TypeManager.System_Int64 && t != TypeManager.System_UInt64)
|
if (t != TypeManager.System_Int64 && t != TypeManager.System_UInt64)
|
||||||
|
@ -1357,7 +1357,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
if (originalReturnType == TypeManager.NSNumber) {
|
if (originalReturnType == TypeManager.NSNumber) {
|
||||||
if (!NSNumberReturnMap.TryGetValue (retType, out append)) {
|
if (!NSNumberReturnMap.TryGetValue (retType, out append)) {
|
||||||
if (retType.IsEnum) {
|
if (retType.IsEnum) {
|
||||||
var enumType = Enum.GetUnderlyingType (retType);
|
var enumType = TypeManager.GetUnderlyingEnumType (retType);
|
||||||
if (!NSNumberReturnMap.TryGetValue (enumType, out append))
|
if (!NSNumberReturnMap.TryGetValue (enumType, out append))
|
||||||
throw new BindingException (1049, true, GetBindAsExceptionString ("unbox", retType.Name, originalReturnType.Name, "container", minfo.mi.Name));
|
throw new BindingException (1049, true, GetBindAsExceptionString ("unbox", retType.Name, originalReturnType.Name, "container", minfo.mi.Name));
|
||||||
}
|
}
|
||||||
|
@ -1529,7 +1529,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if (!Compat && IsNativeEnum (pi.ParameterType)) {
|
} else if (!Compat && IsNativeEnum (pi.ParameterType)) {
|
||||||
Type underlyingEnumType = Enum.GetUnderlyingType (pi.ParameterType);
|
Type underlyingEnumType = TypeManager.GetUnderlyingEnumType (pi.ParameterType);
|
||||||
pars.AppendFormat ("{0} {1}", GetNativeEnumType (pi.ParameterType), pi.Name.GetSafeParamName ());
|
pars.AppendFormat ("{0} {1}", GetNativeEnumType (pi.ParameterType), pi.Name.GetSafeParamName ());
|
||||||
invoke.AppendFormat ("({1}) ({2}) {0}", pi.Name.GetSafeParamName (), FormatType (null, pi.ParameterType), FormatType (null, underlyingEnumType));
|
invoke.AppendFormat ("({1}) ({2}) {0}", pi.Name.GetSafeParamName (), FormatType (null, pi.ParameterType), FormatType (null, underlyingEnumType));
|
||||||
continue;
|
continue;
|
||||||
|
@ -1628,7 +1628,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
return "(" + PrimitiveType (pi.ParameterType, enum_mode: enum_mode) + ")" + pi.Name.GetSafeParamName ();
|
return "(" + PrimitiveType (pi.ParameterType, enum_mode: enum_mode) + ")" + pi.Name.GetSafeParamName ();
|
||||||
|
|
||||||
if (enum_mode == EnumMode.NativeBits && IsNativeEnum (pi.ParameterType) && !Compat)
|
if (enum_mode == EnumMode.NativeBits && IsNativeEnum (pi.ParameterType) && !Compat)
|
||||||
return "(" + GetNativeEnumType (pi.ParameterType) + ") (" + PrimitiveType (Enum.GetUnderlyingType (pi.ParameterType)) + ") " + pi.Name.GetSafeParamName ();
|
return "(" + GetNativeEnumType (pi.ParameterType) + ") (" + PrimitiveType (TypeManager.GetUnderlyingEnumType (pi.ParameterType)) + ") " + pi.Name.GetSafeParamName ();
|
||||||
|
|
||||||
if (IsNativeType (pi.ParameterType))
|
if (IsNativeType (pi.ParameterType))
|
||||||
return pi.Name.GetSafeParamName ();
|
return pi.Name.GetSafeParamName ();
|
||||||
|
@ -1861,7 +1861,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
// nint or nuint
|
// nint or nuint
|
||||||
string GetNativeEnumType (Type type)
|
string GetNativeEnumType (Type type)
|
||||||
{
|
{
|
||||||
var underlyingEnumType = Enum.GetUnderlyingType (type);
|
var underlyingEnumType = TypeManager.GetUnderlyingEnumType (type);
|
||||||
if (TypeManager.System_Int64 == underlyingEnumType) {
|
if (TypeManager.System_Int64 == underlyingEnumType) {
|
||||||
return "nint";
|
return "nint";
|
||||||
} else if (TypeManager.System_UInt64 == underlyingEnumType) {
|
} else if (TypeManager.System_UInt64 == underlyingEnumType) {
|
||||||
|
@ -2589,7 +2589,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
string castToEnum = "";
|
string castToEnum = "";
|
||||||
|
|
||||||
if (pi.PropertyType.IsEnum){
|
if (pi.PropertyType.IsEnum){
|
||||||
fetchType = Enum.GetUnderlyingType (pi.PropertyType);
|
fetchType = TypeManager.GetUnderlyingEnumType (pi.PropertyType);
|
||||||
castToUnderlying = "(" + fetchType + "?)";
|
castToUnderlying = "(" + fetchType + "?)";
|
||||||
castToEnum = "(" + FormatType (dictType, pi.PropertyType) + "?)";
|
castToEnum = "(" + FormatType (dictType, pi.PropertyType) + "?)";
|
||||||
}
|
}
|
||||||
|
@ -2841,7 +2841,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
else if (propertyType == TypeManager.System_String_Array){
|
else if (propertyType == TypeManager.System_String_Array){
|
||||||
print ("return NSArray.StringArrayFromHandle (value);");
|
print ("return NSArray.StringArrayFromHandle (value);");
|
||||||
} else {
|
} else {
|
||||||
Type underlying = propertyType.IsEnum ? Enum.GetUnderlyingType (propertyType) : propertyType;
|
Type underlying = propertyType.IsEnum ? TypeManager.GetUnderlyingEnumType (propertyType) : propertyType;
|
||||||
string cast = propertyType.IsEnum ? "(" + propertyType.FullName + ") " : "";
|
string cast = propertyType.IsEnum ? "(" + propertyType.FullName + ") " : "";
|
||||||
|
|
||||||
if (underlying == TypeManager.System_Int32)
|
if (underlying == TypeManager.System_Int32)
|
||||||
|
@ -3303,7 +3303,7 @@ public partial class Generator : IMemberGatherer {
|
||||||
if (IsNativeEnum (mi.ReturnType) && enum_mode == EnumMode.Bit32) {
|
if (IsNativeEnum (mi.ReturnType) && enum_mode == EnumMode.Bit32) {
|
||||||
// Check if we got UInt32.MaxValue, which should probably be UInt64.MaxValue (if the enum
|
// Check if we got UInt32.MaxValue, which should probably be UInt64.MaxValue (if the enum
|
||||||
// in question actually has that value at least).
|
// in question actually has that value at least).
|
||||||
var type = Enum.GetUnderlyingType (mi.ReturnType) == TypeManager.System_UInt64 ? "ulong" : "long";
|
var type = TypeManager.GetUnderlyingEnumType (mi.ReturnType) == TypeManager.System_UInt64 ? "ulong" : "long";
|
||||||
var itype = type == "ulong" ? "uint" : "int";
|
var itype = type == "ulong" ? "uint" : "int";
|
||||||
var value = Enum.ToObject (mi.ReturnType, type == "ulong" ? (object) ulong.MaxValue : (object) long.MaxValue);
|
var value = Enum.ToObject (mi.ReturnType, type == "ulong" ? (object) ulong.MaxValue : (object) long.MaxValue);
|
||||||
if (Array.IndexOf (Enum.GetValues (mi.ReturnType), value) >= 0) {
|
if (Array.IndexOf (Enum.GetValues (mi.ReturnType), value) >= 0) {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче