-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: New IL instruction or new IL pattern to represent an invocation of a method implementation found in specific base type #12886
Comments
Something to think about before diving too deep into a new IL instruction: .NET currently does not support the composition of open instance delegates to generic interface methods. Some background: http://higherlogics.blogspot.com/2011/08/open-instance-delegate-for-generic.html While the linked blog post was written in 2011, it remains true on netcoreapp2.2 today. - Just tested to make sure. Similarly, before adding a bunch of magic, maybe add a regression suite on this stuff. Developers hate when syntactically valid programs are semantically not supported. |
@jzabroski, public class C
{
public virtual void DoNothing<T>()=> Console.WriteLine(typeof(T));
}
var dynamicMethod = new DynamicMethod("a", typeof(void), new[] { typeof(C) }, typeof(object), true);
var ilgen = dynamicMethod.GetILGenerator();
ilgen.Emit(OpCodes.Ldarg_0);
ilgen.Emit(OpCodes.Dup);
ilgen.Emit(OpCodes.Ldvirtftn, typeof(C).GetMethod("DoNothing").MakeGenericMethod(typeof(int)));
ilgen.Emit(OpCodes.Calli, SignatureHelper.GetMethodSigHelper(CallingConventions.HasThis, typeof(void)));
ilgen.Emit(OpCodes.Ret);
(dynamicMethod.CreateDelegate(typeof(Action<C>)) as Action<C>).Invoke(new C()) Now why does |
@mburbea What SO post are you referring to? Thanks. |
The blog post you linked mentions a SO link.
…On Fri, Jul 5, 2019, 2:13 PM John Zabroski ***@***.***> wrote:
@mburbea <https://github.com/mburbea> What SO post are you referring to?
Thanks.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/coreclr/issues/25156?email_source=notifications&email_token=AABUB4OHI45VOQDLELKX673P56FOBA5CNFSM4HX5GFO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZKCNBA#issuecomment-508831364>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABUB4KBASDBNYJ4K6WRPBLP56FOBANCNFSM4HX5GFOQ>
.
|
OK, my point is that open delegates should use the same dispatch logic as the rest of the CLR, unless there is a good reason not to. @AlekseyTs said at the end of his request:
Rather than add new instructions or patterns, we should probably first revisit what and why the CLR does what it does today. The only clue is this paragraph from the ECMA-335 CLI specification (emphasis mine):
My hunch has always been that the constraint that the Virtual Execution System (VES) causes this to not be possible, presumably because it is too low-level to know how to handle such things. But I've not investigated. I guess now that it's open source it's possible to investigate. My second hunch is that calli bypasses the VES entirely. |
@jzabroski This post I wrote How do .NET delegates work? might help you get started if you want to investigate (I don't think the post on its own answers your question, but it might help) |
These are my thoughts on the additional runtime support needed for the We should be able to support this with a variation on the constrained prefix. This is my idea of what the spec would generally look like.
Description: If Add note about how Correctness: Verifiability: |
@davidwrighton Can you edit your post to fix the |
How is this different from simple |
The difference is that the actual method called may not be the same as the method referenced by the instruction, if the method is overrided by the type it is constrained to. The primary purpose for the new instruction is around behavior of default interface methods, and the multiple-inheritance style overriding that is available through their use. (Although there are some more subtle benefits for base calls on classes.) |
What is the state of this feature currently? I'm really looking forward to it! |
In C# and VB we are planning to add support for a special expression representing an invocation of an implementation of a particular virtual method from a specific base type. This includes virtual methods declared in classes as well as in interfaces.
Compilers have to make sure that there is a most specific implementation of the method in the specified base type, and, in that case, emit an instruction that “says”: “Runtime, call the most specific implementation of this method in the type specified. If the method is an interface method, the implementation method should be called with a virtual dispatch. If the method is a class method, the implementation method should be called non virtually.”
The IL instruction or the pattern should capture the following information about the target of the invocation:
Possibly a usage of constrained prefix can be expanded. Or a new IL instruction added. Also, we should make sure that it is possible to create function pointers that capture the same semantics, primary usage scenarios - creation/invocation of delegates.
The text was updated successfully, but these errors were encountered: