Skip to content

Commit

Permalink
Linker-annotate Type/TypeInfo/RuntimeType (dotnet#37243)
Browse files Browse the repository at this point in the history
This annotates methods that linker was complaining about when analyzing their method bodies. We'll probably want to annotate additional methods that are unsafe, but linker doesn't see them.
  • Loading branch information
MichalStrehovsky authored Jun 2, 2020
1 parent 3f776fb commit 1cf4bde
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3378,6 +3378,7 @@ public override Type MakeArrayType(int rank)

[DebuggerStepThrough]
[DebuggerHidden]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public override object? InvokeMember(
string name, BindingFlags bindingFlags, Binder? binder, object? target,
object?[]? providedArgs, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ protected TypeInfo() { }

public virtual Type[] GenericTypeParameters => IsGenericTypeDefinition ? GetGenericArguments() : Type.EmptyTypes;

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public virtual EventInfo? GetDeclaredEvent(string name) => GetEvent(name, TypeInfo.DeclaredOnlyLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public virtual FieldInfo? GetDeclaredField(string name) => GetField(name, TypeInfo.DeclaredOnlyLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public virtual MethodInfo? GetDeclaredMethod(string name) => GetMethod(name, TypeInfo.DeclaredOnlyLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
public virtual TypeInfo? GetDeclaredNestedType(string name) => GetNestedType(name, TypeInfo.DeclaredOnlyLookup)?.GetTypeInfo();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public virtual PropertyInfo? GetDeclaredProperty(string name) => GetProperty(name, TypeInfo.DeclaredOnlyLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public virtual IEnumerable<MethodInfo> GetDeclaredMethods(string name)
{
foreach (MethodInfo method in GetMethods(TypeInfo.DeclaredOnlyLookup))
Expand All @@ -31,13 +41,39 @@ public virtual IEnumerable<MethodInfo> GetDeclaredMethods(string name)
}
}

public virtual IEnumerable<ConstructorInfo> DeclaredConstructors => GetConstructors(TypeInfo.DeclaredOnlyLookup);
public virtual IEnumerable<EventInfo> DeclaredEvents => GetEvents(TypeInfo.DeclaredOnlyLookup);
public virtual IEnumerable<FieldInfo> DeclaredFields => GetFields(TypeInfo.DeclaredOnlyLookup);
public virtual IEnumerable<MemberInfo> DeclaredMembers => GetMembers(TypeInfo.DeclaredOnlyLookup);
public virtual IEnumerable<MethodInfo> DeclaredMethods => GetMethods(TypeInfo.DeclaredOnlyLookup);
public virtual IEnumerable<ConstructorInfo> DeclaredConstructors
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
get => GetConstructors(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<EventInfo> DeclaredEvents
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
get => GetEvents(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<FieldInfo> DeclaredFields
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
get => GetFields(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<MemberInfo> DeclaredMembers
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
get => GetMembers(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<MethodInfo> DeclaredMethods
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
get => GetMethods(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
get
{
foreach (Type t in GetNestedTypes(TypeInfo.DeclaredOnlyLookup))
Expand All @@ -46,7 +82,12 @@ public virtual IEnumerable<System.Reflection.TypeInfo> DeclaredNestedTypes
}
}
}
public virtual IEnumerable<PropertyInfo> DeclaredProperties => GetProperties(TypeInfo.DeclaredOnlyLookup);

public virtual IEnumerable<PropertyInfo> DeclaredProperties
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
get => GetProperties(TypeInfo.DeclaredOnlyLookup);
}

public virtual IEnumerable<Type> ImplementedInterfaces => GetInterfaces();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public virtual Type[] FindInterfaces(TypeFilter filter, object? filterCriteria)
return ret;
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public virtual MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter? filter, object? filterCriteria)
{
// Define the work arrays
Expand Down
76 changes: 75 additions & 1 deletion src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,20 @@ public virtual Type[] GetGenericParameterConstraints()
public virtual bool IsSecurityTransparent => throw NotImplemented.ByDesign;

public virtual StructLayoutAttribute? StructLayoutAttribute => throw new NotSupportedException();
public ConstructorInfo? TypeInitializer => GetConstructorImpl(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, Type.EmptyTypes, null);

public ConstructorInfo? TypeInitializer
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
get => GetConstructorImpl(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, CallingConventions.Any, Type.EmptyTypes, null);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public ConstructorInfo? GetConstructor(Type[] types) => GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, types, null);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public ConstructorInfo? GetConstructor(BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers) => GetConstructor(bindingAttr, binder, CallingConventions.Any, types, modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public ConstructorInfo? GetConstructor(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers)
{
if (types == null)
Expand All @@ -134,21 +144,38 @@ public virtual Type[] GetGenericParameterConstraints()
}
return GetConstructorImpl(bindingAttr, binder, callConvention, types, modifiers);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
protected abstract ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public abstract ConstructorInfo[] GetConstructors(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
public EventInfo? GetEvent(string name) => GetEvent(name, Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public abstract EventInfo? GetEvent(string name, BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public abstract EventInfo[] GetEvents(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
public FieldInfo? GetField(string name) => GetField(name, Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public abstract FieldInfo? GetField(string name, BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public abstract FieldInfo[] GetFields(BindingFlags bindingAttr);

public MemberInfo[] GetMember(string name) => GetMember(name, Type.DefaultLookup);
Expand All @@ -158,17 +185,27 @@ public virtual Type[] GetGenericParameterConstraints()
public MemberInfo[] GetMembers() => GetMembers(Type.DefaultLookup);
public abstract MemberInfo[] GetMembers(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo? GetMethod(string name) => GetMethod(name, Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public MethodInfo? GetMethod(string name, BindingFlags bindingAttr)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
return GetMethodImpl(name, bindingAttr, null, CallingConventions.Any, null, null);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo? GetMethod(string name, Type[] types) => GetMethod(name, types, null);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo? GetMethod(string name, Type[] types, ParameterModifier[]? modifiers) => GetMethod(name, Type.DefaultLookup, null, types, modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers) => GetMethod(name, bindingAttr, binder, CallingConventions.Any, types, modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers)
{
if (name == null)
Expand All @@ -183,11 +220,19 @@ public virtual Type[] GetGenericParameterConstraints()
return GetMethodImpl(name, bindingAttr, binder, callConvention, types, modifiers);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
protected abstract MethodInfo? GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types) => GetMethod(name, genericParameterCount, types, null);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, ParameterModifier[]? modifiers) => GetMethod(name, genericParameterCount, Type.DefaultLookup, null, types, modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers) => GetMethod(name, genericParameterCount, bindingAttr, binder, CallingConventions.Any, types, modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers)
{
if (name == null)
Expand All @@ -204,35 +249,56 @@ public virtual Type[] GetGenericParameterConstraints()
return GetMethodImpl(name, genericParameterCount, bindingAttr, binder, callConvention, types, modifiers);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
protected virtual MethodInfo? GetMethodImpl(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public abstract MethodInfo[] GetMethods(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public Type? GetNestedType(string name) => GetNestedType(name, Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
public abstract Type? GetNestedType(string name, BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
public abstract Type[] GetNestedTypes(BindingFlags bindingAttr);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public PropertyInfo? GetProperty(string name) => GetProperty(name, Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public PropertyInfo? GetProperty(string name, BindingFlags bindingAttr)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
return GetPropertyImpl(name, bindingAttr, null, null, null, null);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public PropertyInfo? GetProperty(string name, Type? returnType)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
return GetPropertyImpl(name, Type.DefaultLookup, null, returnType, null, null);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public PropertyInfo? GetProperty(string name, Type[] types) => GetProperty(name, null, types);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public PropertyInfo? GetProperty(string name, Type? returnType, Type[] types) => GetProperty(name, returnType, types, null);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public PropertyInfo? GetProperty(string name, Type? returnType, Type[] types, ParameterModifier[]? modifiers) => GetProperty(name, Type.DefaultLookup, null, returnType, types, modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public PropertyInfo? GetProperty(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[] types, ParameterModifier[]? modifiers)
{
if (name == null)
Expand All @@ -242,9 +308,13 @@ public virtual Type[] GetGenericParameterConstraints()
return GetPropertyImpl(name, bindingAttr, binder, returnType, types, modifiers);
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
protected abstract PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);

public virtual MemberInfo[] GetDefaultMembers() => throw NotImplemented.ByDesign;
Expand Down Expand Up @@ -302,11 +372,15 @@ protected virtual TypeCode GetTypeCodeImpl()

[DebuggerHidden]
[DebuggerStepThrough]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args) => InvokeMember(name, invokeAttr, binder, target, args, null, null, null);

[DebuggerHidden]
[DebuggerStepThrough]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, CultureInfo? culture) => InvokeMember(name, invokeAttr, binder, target, args, null, culture, null);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public abstract object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters);

public Type? GetInterface(string name) => GetInterface(name, ignoreCase: false);
Expand Down

0 comments on commit 1cf4bde

Please sign in to comment.