[generator] Remove the non-generic API in AttributeManager in favor of the generic versions. (#1824)
The generic versions result in nicer code, and it's also less logic to port to IKVM. Generator diff: https://gist.github.com/rolfbjarne/c70664b59c915040ed7cf95dba68c1d6 (no related changes)
This commit is contained in:
Родитель
a1c2ab8ea7
Коммит
4f7df26a98
|
@ -221,7 +221,7 @@ namespace XamCore.ObjCRuntime
|
|||
// Find the maximum of "field size + field offset" for each field.
|
||||
foreach (var field in type.GetFields (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
|
||||
#if BGENERATOR
|
||||
var fieldOffset = (FieldOffsetAttribute) AttributeManager.GetCustomAttribute (field, TypeManager.FieldOffsetAttribute);
|
||||
var fieldOffset = AttributeManager.GetCustomAttribute<FieldOffsetAttribute> (field);
|
||||
#else
|
||||
var fieldOffset = (FieldOffsetAttribute) Attribute.GetCustomAttribute (field, typeof (FieldOffsetAttribute));
|
||||
#endif
|
||||
|
@ -293,7 +293,7 @@ namespace XamCore.ObjCRuntime
|
|||
// composite struct
|
||||
foreach (var field in type.GetFields (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
|
||||
#if BGENERATOR
|
||||
var marshalAs = (MarshalAsAttribute) AttributeManager.GetCustomAttribute (field, TypeManager.MarshalAsAttribute);
|
||||
var marshalAs = AttributeManager.GetCustomAttribute<MarshalAsAttribute> (field);
|
||||
#else
|
||||
var marshalAs = (MarshalAsAttribute) Attribute.GetCustomAttribute (field, typeof (MarshalAsAttribute));
|
||||
#endif
|
||||
|
|
|
@ -390,9 +390,7 @@ class BindingTouch {
|
|||
|
||||
TypeManager.Initialize (api);
|
||||
|
||||
foreach (object attr in AttributeManager.GetCustomAttributes (api, TypeManager.LinkWithAttribute)) {
|
||||
LinkWithAttribute linkWith = (LinkWithAttribute) attr;
|
||||
|
||||
foreach (var linkWith in AttributeManager.GetCustomAttributes<LinkWithAttribute> (api)) {
|
||||
if (!linkwith.Contains (linkWith.LibraryName)) {
|
||||
Console.Error.WriteLine ("Missing native library {0}, please use `--link-with' to specify the path to this library.", linkWith.LibraryName);
|
||||
return 1;
|
||||
|
@ -413,12 +411,12 @@ class BindingTouch {
|
|||
var strong_dictionaries = new List<Type> ();
|
||||
foreach (var t in api.GetTypes ()){
|
||||
if ((process_enums && t.IsEnum) ||
|
||||
AttributeManager.HasAttribute (t, TypeManager.BaseTypeAttribute) ||
|
||||
AttributeManager.HasAttribute (t, TypeManager.ProtocolAttribute) ||
|
||||
AttributeManager.HasAttribute (t, TypeManager.StaticAttribute) ||
|
||||
AttributeManager.HasAttribute (t, TypeManager.PartialAttribute))
|
||||
AttributeManager.HasAttribute<BaseTypeAttribute> (t) ||
|
||||
AttributeManager.HasAttribute<ProtocolAttribute> (t) ||
|
||||
AttributeManager.HasAttribute<StaticAttribute> (t) ||
|
||||
AttributeManager.HasAttribute<PartialAttribute> (t))
|
||||
types.Add (t);
|
||||
if (AttributeManager.HasAttribute (t, TypeManager.StrongDictionaryAttribute))
|
||||
if (AttributeManager.HasAttribute<StrongDictionaryAttribute> (t))
|
||||
strong_dictionaries.Add (t);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,54 +3,41 @@ using System.Reflection;
|
|||
|
||||
public static class AttributeManager
|
||||
{
|
||||
public static System.Attribute GetCustomAttribute (ICustomAttributeProvider provider, Type type)
|
||||
public static T GetCustomAttribute<T> (ICustomAttributeProvider provider) where T: System.Attribute
|
||||
{
|
||||
if (provider == null)
|
||||
return null;
|
||||
|
||||
var type = typeof (T);
|
||||
var pi = provider as ParameterInfo;
|
||||
if (pi != null)
|
||||
return Attribute.GetCustomAttribute (pi, type);
|
||||
return (T) Attribute.GetCustomAttribute (pi, type);
|
||||
var mi = provider as MemberInfo;
|
||||
if (mi != null)
|
||||
return Attribute.GetCustomAttribute (mi, type);
|
||||
return (T) Attribute.GetCustomAttribute (mi, type);
|
||||
var asm = provider as Assembly;
|
||||
if (asm != null)
|
||||
return Attribute.GetCustomAttribute (asm, type);
|
||||
return (T) Attribute.GetCustomAttribute (asm, type);
|
||||
throw new BindingException (1051, true, "Internal error: Don't know how to get attributes for {0}. Please file a bug report (http://bugzilla.xamarin.com) with a test case.", provider.GetType ().FullName);
|
||||
}
|
||||
|
||||
public static T GetCustomAttribute <T> (ICustomAttributeProvider provider) where T: System.Attribute
|
||||
{
|
||||
return (T) GetCustomAttribute (provider, typeof (T));
|
||||
}
|
||||
|
||||
public static T [] GetCustomAttributes<T> (ICustomAttributeProvider provider) where T : System.Attribute
|
||||
{
|
||||
return (T []) provider.GetCustomAttributes (typeof (T), false);
|
||||
}
|
||||
|
||||
public static object [] GetCustomAttributes (ICustomAttributeProvider provider, Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new System.ArgumentNullException (nameof (type));
|
||||
|
||||
return (System.Attribute []) provider.GetCustomAttributes (type, false);
|
||||
}
|
||||
|
||||
public static object [] GetCustomAttributes (ICustomAttributeProvider provider)
|
||||
{
|
||||
return provider.GetCustomAttributes (false);
|
||||
}
|
||||
|
||||
public static bool HasAttribute<T> (ICustomAttributeProvider provider) where T : Attribute
|
||||
{
|
||||
return HasAttribute (provider, typeof (T));
|
||||
var attribs = provider.GetCustomAttributes (typeof (T), false);
|
||||
if (attribs == null || attribs.Length == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool HasAttribute (ICustomAttributeProvider provider, string type_name)
|
||||
{
|
||||
foreach (var attr in AttributeManager.GetCustomAttributes (provider)) {
|
||||
foreach (var attr in provider.GetCustomAttributes (false)) {
|
||||
if (attr.GetType ().Name == type_name)
|
||||
return true;
|
||||
}
|
||||
|
@ -58,15 +45,6 @@ public static class AttributeManager
|
|||
return false;
|
||||
}
|
||||
|
||||
public static bool HasAttribute (ICustomAttributeProvider provider, Type attribute_type)
|
||||
{
|
||||
var attribs = GetCustomAttributes (provider, attribute_type);
|
||||
if (attribs == null || attribs.Length == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static ICustomAttributeProvider GetReturnTypeCustomAttributes (MethodInfo method)
|
||||
{
|
||||
return method.ReturnTypeCustomAttributes;
|
||||
|
|
|
@ -35,7 +35,7 @@ public partial class Generator {
|
|||
|
||||
void CopyObsolete (ICustomAttributeProvider provider)
|
||||
{
|
||||
foreach (ObsoleteAttribute oa in AttributeManager.GetCustomAttributes (provider, TypeManager.ObsoleteAttribute))
|
||||
foreach (var oa in AttributeManager.GetCustomAttributes<ObsoleteAttribute> (provider))
|
||||
print ("[Obsolete (\"{0}\", {1})]", oa.Message, oa.IsError ? "true" : "false");
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ public partial class Generator {
|
|||
// - call/emit PrintPlatformAttributes on the type
|
||||
void GenerateEnum (Type type)
|
||||
{
|
||||
if (AttributeManager.HasAttribute (type, TypeManager.FlagsAttribute))
|
||||
if (AttributeManager.HasAttribute<FlagsAttribute> (type))
|
||||
print ("[Flags]");
|
||||
|
||||
var native = AttributeManager.GetCustomAttribute<NativeAttribute> (type);
|
||||
|
|
|
@ -22,7 +22,7 @@ public partial class Generator {
|
|||
|
||||
public void GenerateFilter (Type type)
|
||||
{
|
||||
var is_abstract = AttributeManager.HasAttribute (type, TypeManager.AbstractAttribute);
|
||||
var is_abstract = AttributeManager.HasAttribute<AbstractAttribute> (type);
|
||||
var filter = AttributeManager.GetCustomAttribute<CoreImageFilterAttribute> (type);
|
||||
var base_type = AttributeManager.GetCustomAttribute<BaseTypeAttribute> (type);
|
||||
var type_name = type.Name;
|
||||
|
|
|
@ -49,83 +49,6 @@ public static class TypeManager {
|
|||
|
||||
public static Type Constants;
|
||||
|
||||
/* attributes */
|
||||
public static Type AbstractAttribute;
|
||||
public static Type AdvancedAttribute;
|
||||
public static Type AlignAttribute;
|
||||
public static Type AppearanceAttribute;
|
||||
public static Type AsyncAttribute;
|
||||
public static Type AutoreleaseAttribute;
|
||||
public static Type AvailabilityBaseAttribute;
|
||||
public static Type BaseTypeAttribute;
|
||||
public static Type BindAttribute;
|
||||
public static Type BlockCallbackAttribute;
|
||||
public static Type CategoryAttribute;
|
||||
public static Type CCallbackAttribute;
|
||||
public static Type CheckDisposedAttribute;
|
||||
public static Type CoreImageFilterAttribute;
|
||||
public static Type CoreImageFilterPropertyAttribute;
|
||||
public static Type DebuggerBrowsableAttribute;
|
||||
public static Type DebuggerDisplayAttribute;
|
||||
public static Type DefaultValueAttribute;
|
||||
public static Type DefaultValueFromArgumentAttribute;
|
||||
public static Type DelegateApiNameAttribute;
|
||||
public static Type DelegateNameAttribute;
|
||||
public static Type DesignatedInitializerAttribute;
|
||||
public static Type DisableZeroCopyAttribute;
|
||||
public static Type DisposeAttribute;
|
||||
public static Type EditorBrowsableAttribute;
|
||||
public static Type EventArgsAttribute;
|
||||
public static Type EventNameAttribute;
|
||||
public static Type ExportAttribute;
|
||||
public static Type FactoryAttribute;
|
||||
public static Type FieldAttribute;
|
||||
public static Type FieldOffsetAttribute;
|
||||
public static Type FlagsAttribute;
|
||||
public static Type InternalAttribute;
|
||||
public static Type IsThreadStaticAttribute;
|
||||
public static Type LinkWithAttribute;
|
||||
public static Type ManualAttribute;
|
||||
public static Type MarshalAsAttribute;
|
||||
public static Type MarshalNativeExceptionsAttribute;
|
||||
public static Type ModelAttribute;
|
||||
public static Type NativeAttribute;
|
||||
public static Type NewAttribute;
|
||||
public static Type NoDefaultValueAttribute;
|
||||
public static Type NotificationAttribute;
|
||||
public static Type NotImplementedAttribute;
|
||||
public static Type NullAllowedAttribute;
|
||||
public static Type ObsoleteAttribute;
|
||||
public static Type OptionalImplementationAttribute;
|
||||
static Type OutAttribute;
|
||||
public static Type OverrideAttribute;
|
||||
public static Type ParamArrayAttribute;
|
||||
public static Type ParamsAttribute;
|
||||
public static Type PartialAttribute;
|
||||
public static Type PlainStringAttribute;
|
||||
public static Type PostGetAttribute;
|
||||
public static Type PostSnippetAttribute;
|
||||
public static Type PreSnippetAttribute;
|
||||
public static Type ProbePresenceAttribute;
|
||||
public static Type PrologueSnippetAttribute;
|
||||
public static Type ProtectedAttribute;
|
||||
public static Type ProtocolAttribute;
|
||||
public static Type ProtocolizeAttribute;
|
||||
public static Type ProxyAttribute;
|
||||
public static Type RegisterAttribute;
|
||||
public static Type ReleaseAttribute;
|
||||
public static Type RetainAttribute;
|
||||
public static Type SealedAttribute;
|
||||
public static Type StaticAttribute;
|
||||
public static Type StrongDictionaryAttribute;
|
||||
public static Type SyntheticAttribute;
|
||||
public static Type TargetAttribute;
|
||||
public static Type ThreadSafeAttribute;
|
||||
public static Type TransientAttribute;
|
||||
public static Type UnifiedInternalAttribute;
|
||||
public static Type WrapAttribute;
|
||||
public static Type ZeroCopyStringsAttribute;
|
||||
|
||||
/* Different binding types */
|
||||
|
||||
public static Type DictionaryContainerType;
|
||||
|
@ -189,9 +112,7 @@ public static class TypeManager {
|
|||
|
||||
static Assembly api_assembly;
|
||||
static Assembly corlib_assembly;
|
||||
static Assembly system_assembly;
|
||||
static Assembly platform_assembly;
|
||||
static Assembly binding_assembly;
|
||||
|
||||
public static Assembly CorlibAssembly {
|
||||
get { return corlib_assembly; }
|
||||
|
@ -232,7 +153,7 @@ public static class TypeManager {
|
|||
|
||||
public static bool IsOutParameter (ParameterInfo pi)
|
||||
{
|
||||
return AttributeManager.HasAttribute (pi, OutAttribute);
|
||||
return AttributeManager.HasAttribute<OutAttribute> (pi);
|
||||
}
|
||||
|
||||
public static Type GetUnderlyingEnumType (Type type)
|
||||
|
@ -244,9 +165,7 @@ public static class TypeManager {
|
|||
{
|
||||
api_assembly = api;
|
||||
corlib_assembly = typeof (object).Assembly;
|
||||
system_assembly = typeof (System.ComponentModel.BrowsableAttribute).Assembly;
|
||||
platform_assembly = typeof (NSObject).Assembly;
|
||||
binding_assembly = typeof (ProtocolizeAttribute).Assembly;
|
||||
|
||||
/* corlib */
|
||||
System_Attribute = Lookup (corlib_assembly, "System", "Attribute");
|
||||
|
@ -290,83 +209,6 @@ public static class TypeManager {
|
|||
Constants = Lookup (platform_assembly, "", "Constants");
|
||||
}
|
||||
|
||||
/* attributes */
|
||||
AbstractAttribute = Lookup (binding_assembly, "", "AbstractAttribute");
|
||||
AdvancedAttribute = Lookup (binding_assembly, "", "AdvancedAttribute");
|
||||
AlignAttribute = Lookup (binding_assembly, "", "AlignAttribute");
|
||||
AppearanceAttribute = Lookup (binding_assembly, "", "AppearanceAttribute");
|
||||
AsyncAttribute = Lookup (binding_assembly, "", "AsyncAttribute");
|
||||
AutoreleaseAttribute = Lookup (binding_assembly, "", "AutoreleaseAttribute");
|
||||
AvailabilityBaseAttribute = Lookup (platform_assembly, "ObjCRuntime", "AvailabilityBaseAttribute");
|
||||
BaseTypeAttribute = Lookup (binding_assembly, "", "BaseTypeAttribute");
|
||||
BindAttribute = Lookup (binding_assembly, "", "BindAttribute");
|
||||
BlockCallbackAttribute = Lookup (binding_assembly, "", "BlockCallbackAttribute");
|
||||
CategoryAttribute = Lookup (binding_assembly, "", "CategoryAttribute");
|
||||
CCallbackAttribute = Lookup (binding_assembly, "", "CCallbackAttribute");
|
||||
CheckDisposedAttribute = Lookup (binding_assembly, "", "CheckDisposedAttribute");
|
||||
CoreImageFilterAttribute = Lookup (binding_assembly, "", "CoreImageFilterAttribute");
|
||||
CoreImageFilterPropertyAttribute = Lookup (binding_assembly, "", "CoreImageFilterPropertyAttribute");
|
||||
DebuggerBrowsableAttribute = Lookup (corlib_assembly, "System.Diagnostics", "DebuggerBrowsableAttribute");
|
||||
DebuggerDisplayAttribute = Lookup (corlib_assembly, "System.Diagnostics", "DebuggerDisplayAttribute");
|
||||
DefaultValueAttribute = Lookup (binding_assembly, "", "DefaultValueAttribute");
|
||||
DefaultValueFromArgumentAttribute = Lookup (binding_assembly, "", "DefaultValueFromArgumentAttribute");
|
||||
DelegateApiNameAttribute = Lookup (binding_assembly, "", "DelegateApiNameAttribute");
|
||||
DelegateNameAttribute = Lookup (binding_assembly, "", "DelegateNameAttribute");
|
||||
DesignatedInitializerAttribute = Lookup (binding_assembly, "", "DesignatedInitializerAttribute");
|
||||
DisableZeroCopyAttribute = Lookup (binding_assembly, "", "DisableZeroCopyAttribute");
|
||||
DisposeAttribute = Lookup (binding_assembly, "", "DisposeAttribute");
|
||||
EditorBrowsableAttribute = Lookup (system_assembly, "System.ComponentModel", "EditorBrowsableAttribute");
|
||||
EventArgsAttribute = Lookup (binding_assembly, "", "EventArgsAttribute");
|
||||
EventNameAttribute = Lookup (binding_assembly, "", "EventNameAttribute");
|
||||
ExportAttribute = Lookup (platform_assembly, "Foundation", "ExportAttribute");
|
||||
FactoryAttribute = Lookup (binding_assembly, "", "FactoryAttribute");
|
||||
FieldAttribute = Lookup (platform_assembly, "Foundation", "FieldAttribute");
|
||||
FieldOffsetAttribute = Lookup (corlib_assembly, "System.Runtime.InteropServices", "FieldOffsetAttribute");
|
||||
FlagsAttribute = Lookup (corlib_assembly, "System", "FlagsAttribute");
|
||||
InternalAttribute = Lookup (binding_assembly, "", "InternalAttribute");
|
||||
IsThreadStaticAttribute = Lookup (binding_assembly, "", "IsThreadStaticAttribute");
|
||||
LinkWithAttribute = Lookup (platform_assembly, "ObjCRuntime", "LinkWithAttribute");
|
||||
ManualAttribute = Lookup (binding_assembly, "", "ManualAttribute");
|
||||
MarshalAsAttribute = Lookup (corlib_assembly, "System.Runtime.InteropServices", "MarshalAsAttribute");
|
||||
MarshalNativeExceptionsAttribute = Lookup (binding_assembly, "", "MarshalNativeExceptionsAttribute");
|
||||
ModelAttribute = Lookup (platform_assembly, "Foundation", "ModelAttribute");
|
||||
NativeAttribute = Lookup (platform_assembly, "ObjCRuntime", "NativeAttribute");
|
||||
NewAttribute = Lookup (binding_assembly, "", "NewAttribute");
|
||||
NoDefaultValueAttribute = Lookup (binding_assembly, "", "NoDefaultValueAttribute");
|
||||
NotificationAttribute = Lookup (binding_assembly, "", "NotificationAttribute");
|
||||
NotImplementedAttribute = Lookup (binding_assembly, "", "NotImplementedAttribute");
|
||||
NullAllowedAttribute = Lookup (binding_assembly, "", "NullAllowedAttribute");
|
||||
ObsoleteAttribute = Lookup (corlib_assembly, "System", "ObsoleteAttribute");
|
||||
OptionalImplementationAttribute = Lookup (binding_assembly, "", "OptionalImplementationAttribute");
|
||||
OutAttribute = Lookup (corlib_assembly, "System.Runtime.InteropServices", "OutAttribute");
|
||||
OverrideAttribute = Lookup (binding_assembly, "", "OverrideAttribute");
|
||||
ParamArrayAttribute = Lookup (corlib_assembly, "System", "ParamArrayAttribute");
|
||||
ParamsAttribute = Lookup (binding_assembly, "", "ParamsAttribute");
|
||||
PartialAttribute = Lookup (binding_assembly, "", "PartialAttribute");
|
||||
PlainStringAttribute = Lookup (binding_assembly, "", "PlainStringAttribute");
|
||||
PostGetAttribute = Lookup (binding_assembly, "", "PostGetAttribute");
|
||||
PostSnippetAttribute = Lookup (binding_assembly, "", "PostSnippetAttribute");
|
||||
PreSnippetAttribute = Lookup (binding_assembly, "", "PreSnippetAttribute");
|
||||
ProbePresenceAttribute = Lookup (binding_assembly, "", "ProbePresenceAttribute");
|
||||
PrologueSnippetAttribute = Lookup (binding_assembly, "", "PrologueSnippetAttribute");
|
||||
ProtectedAttribute = Lookup (binding_assembly, "", "ProtectedAttribute");
|
||||
ProtocolAttribute = Lookup (platform_assembly, "Foundation", "ProtocolAttribute");
|
||||
ProtocolizeAttribute = Lookup (binding_assembly, "", "ProtocolizeAttribute");
|
||||
ProxyAttribute = Lookup (binding_assembly, "", "ProxyAttribute");
|
||||
RegisterAttribute = Lookup (platform_assembly, "Foundation", "RegisterAttribute");
|
||||
ReleaseAttribute = Lookup (binding_assembly, "", "ReleaseAttribute");
|
||||
RetainAttribute = Lookup (binding_assembly, "", "RetainAttribute");
|
||||
SealedAttribute = Lookup (binding_assembly, "", "SealedAttribute");
|
||||
StaticAttribute = Lookup (binding_assembly, "", "StaticAttribute");
|
||||
StrongDictionaryAttribute = Lookup (binding_assembly, "", "StrongDictionaryAttribute");
|
||||
SyntheticAttribute = Lookup (binding_assembly, "", "SyntheticAttribute");
|
||||
TargetAttribute = Lookup (binding_assembly, "", "TargetAttribute");
|
||||
ThreadSafeAttribute = Lookup (binding_assembly, "", "ThreadSafeAttribute");
|
||||
TransientAttribute = Lookup (binding_assembly, "", "TransientAttribute");
|
||||
UnifiedInternalAttribute = Lookup (binding_assembly, "", "UnifiedInternalAttribute");
|
||||
WrapAttribute = Lookup (binding_assembly, "", "WrapAttribute");
|
||||
ZeroCopyStringsAttribute = Lookup (binding_assembly, "", "ZeroCopyStringsAttribute");
|
||||
|
||||
/* Different binding types */
|
||||
|
||||
DictionaryContainerType = Lookup (platform_assembly, "Foundation", "DictionaryContainer");
|
||||
|
|
355
src/generator.cs
355
src/generator.cs
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
Загрузка…
Ссылка в новой задаче