Skip to content

[AlsoUsableFor] and [RAIIFree] #389

Closed

Description

@kennykerr

These attributes refer to other types without including their namespace, making it difficult to use in any meaningful way. It should ideally be something like this:

[RAIIFree("Windows.Win32.SystemServices.HeapDestroy")]
...
[AlsoUsableFor("Windows.Win32.SystemServices.HeapHandle")]
...

Activity

  1. kennykerr commented on Mar 25, 2021

    @kennykerr
    ContributorAuthor

    For now, I'm just assuming the type/function is in the same namespace as the type that includes the attribute but I'm guessing that may not always be the case.

  2. added
    namespaces
    usability
    on Mar 26, 2021
  3. sotteson1 commented on Mar 26, 2021

    @sotteson1
    Contributor

    I can fix it fairly easily. And HeapDestroy, it would include the class, right? Windows.Win32.SystemServices.Apis.HeapDestroy

  4. kennykerr commented on Mar 29, 2021

    @kennykerr
    ContributorAuthor

    No, the Apis class is an implementation detail of the metadata that would ideally not exist (e.g. ECMA 335 supports free functions). Logically, the HeapDestroy function is in the Windows.Win32.SystemServices namespace. So it would just be Windows.Win32.SystemServices.HeapDestroy.

  5. AArnott commented on Mar 29, 2021

    @AArnott
    Member

    The attributes are only in the metadata, so IMO they should refer to other members by their metadata names. The Apis class is not an implementation detail of the metadata, since all direct users of the metadata have to know about it and write projections based on it.

  6. kennykerr commented on Mar 29, 2021

    @kennykerr
    ContributorAuthor

    What I mean is that the very existence of the Apis class regrettable. It is a concession to the implementation choices of using C# to generate metadata. Had it not been for this implementation detail, the Apis class would not need to exist. Logically, at least for languages that support namespace-level functions (like C++ and Rust and perhaps others), the Apis class is just this odd placeholder type where we find the "free functions" for that namespace. There is after all no "Apis" class in the original Win32 API headers that this project parses.

    The C++ and Rust parsers perform type lookup via a two-string key consisting of namespace and name. In the example presented here, that would be "Windows.Win32.SystemServices" and "HeapDestroy". The Apis is not considered a namespace nor is it part of the name. I guess in C# you would consider these static methods of the Apis class, but that just seems like a very C# specific view of the world.

  7. AArnott commented on Mar 29, 2021

    @AArnott
    Member

    Ok, fair enough.

  8. sotteson1 commented on Mar 30, 2021

    @sotteson1
    Contributor

    It is a concession to the implementation choices of using C# to generate metadata. Had it not been for this implementation detail, the Apis class would not need to exist.

    I don't see any other way to represent methods in metadata without putting them on a type, or maybe you know how to do it. I would be happy to get rid of the Apis class if I can. I'm using a metadata writer to output to metadata, even though I start by walking a C# syntax tree generated by ClangSharp. I have flexibility to move away from C# when we need to (which I'm already doing a lot).

  9. kennykerr commented on Apr 2, 2021

    @kennykerr
    ContributorAuthor

    I think you're right. Software is full of compromise and this just seemed like one of those concessions we had to make to get the job done given the constraints we were given, metadata formats and such. I also didn’t fully grasp that you had moved away from a purely C# driven approach to metadata generation, so I was still thinking that’s all we had to work with.

    I have to say that I’m thrilled with what you've come up with for win32 metadata. For years I’ve dreamed of having metadata for Win32 APIs and you’ve single-handedly made that a reality. The only reason I made that comment about methods and classes is that IL technically supports free functions. I happen to know that because I wrote about it many years ago, but its been so long I have no idea whether that would work for us in practice nor does it really matter. The Apis class is perfectly fine – it doesn’t bother me in the least. My only point really was that the Apis class is just the construct we use for grouping functions within namespaces. It’s not part of the fully-qualified name of the function, at least in the way that I think about it – every construct has a namespace and a name – and in that worldview there’s no room for the Apis class name. But I can see how that is somewhat subjective. 😊

    I'm sorry if I gave the impression of being critical of the implementation. Far from it - I'm really quite thrilled.

  10. marler8997 commented on Apr 3, 2021

    @marler8997
    Contributor

    This is probably a silly idea, but you could get rid of Apis class by defining the last namespace as a static class rather than a namespace. So instead of:

    namespace Windows.Win32.ActiveDirectory
    {
        public static class Apis
        {
            public const uint ACTRL_DS_CONTROL_ACCESS = 256u;
            ...
        }
        public struct AccessControlEntry { }
        ...
    }

    You could do this:

    namespace Windows.Win32
    {
        public static class ActiveDirectory
        {
            public const uint ACTRL_DS_CONTROL_ACCESS = 256u;
            ...
            public struct AccessControlEntry { }
            ...
        }
    }

    This would remove the need for the Apis class but it would also mean that the sub-types within the top-level class would typically be printed as ActiveDirectory+AccessControlEntry and it's just weird. Seems like a a choice between the lesser of 2 evils.

  11. weltkante commented on Apr 6, 2021

    @weltkante

    IL technically supports free functions

    IMHO no, it doesn't, but it really depends on how you see it. The convention is to put "free" functions on the hidden module class. I don't know how well established this pattern is with tooling (maybe its even standardized somewhere), but at the end its just convention.

    Considering that the hidden module class is global you would lose the ability to classify methods by namespace/component if you pick up this convention.

  12. kennykerr commented on Apr 14, 2021

    @kennykerr
    ContributorAuthor

    I finally tried implementing RAIIFree in Rust (to generate the equivalent of a destructor) but because HANDLE is in the Windows.Win32.SystemServices namespace but CloseHandle is in a different namespace there's no way for me to reliably find it. If HANDLE were defined as follows, it would be a piece of cake:

    [RAIIFree("Windows.Win32.WindowsProgramming.CloseHandle")]
    [NativeTypedef]
    public struct HANDLE
    {
    	public IntPtr Value;
    }

    But perhaps HANDLE and CloseHandle belong in the same namespace? How are others handling this today?

  13. kennykerr commented on Apr 15, 2021

    @kennykerr
    ContributorAuthor

    Same for Windows.Win32.MenusAndResources.HMENU whose free function is Windows.Win32.WindowsAndMessaging.DestroyMenu.

    Same for HICON/HMENU/HCURSOR and possibly others.

  14. added
    blocking
    on May 14, 2021
  15. 17 remaining items

  16. kennykerr commented on Dec 9, 2022

    @kennykerr
    ContributorAuthor

    Note that storing the namespace as a type doesn't actually make things better. As I mentioned this is just encoded as a string anyway, but the real problem is that the function name is still stored as a string rather than a MethodDef record. Without that, a tool still needs to walk the ECMA-335 MethodDef table looking for the function since this table is not sorted. Now if the attribute were only used for functions with a single parameter, then this wouldn't be necessary to weed out functions that are un-callable - I assume that's still an issue.

  17. riverar commented on Dec 10, 2022

    @riverar
    Collaborator

    I'm still tinkering with the code for this, but perhaps we can augment the RAIIFreeAttribute with an optional integer containing the Row ID (RID) of the freeing method in the MethodDef table. This would be generated at winmd generation time. This will eliminate the MethodDef table scan, simplifying code generation.

    Example from my experimental winmd:

    [RAIIFree("Windows.Win32.Graphics.OpenGL.Apis+wglDeleteContext", 7821)]
    [InvalidHandleValue(-1L)]
    [InvalidHandleValue(0L)]
    [NativeTypedef]
    public struct HGLRC
    {
      public IntPtr Value;
    }

    Screenshot from ILSpy showing relevant MethodDef table record:
    image

    Q: Do we still need the method name in the final output?

  18. kennykerr commented on Dec 10, 2022

    @kennykerr
    ContributorAuthor

    If the MethodDef row ID is available, the name isn't needed since that can be looked up directly. Then it can simply be:

    [RAIIFree("Windows.Win32.Graphics.OpenGL", 7821)]
  19. KalleOlaviNiemitalo commented on Dec 11, 2022

    @KalleOlaviNiemitalo

    a tool still needs to walk the ECMA-335 MethodDef table looking for the function since this table is not sorted

    The methods of a class have to be consecutive, and the methods of a COM interface have to be in the vtable order; but would it be feasible to sort the methods of Apis classes by name, and perhaps add an attribute to indicate this was done? Projections could then do a binary search.

    Is it possible that a metadata assembly of an extension SDK will need to use RAIIFreeAttribute on a type of its own (or a parameter or a return value, if supported) but reference a freeing method in the Windows SDK metadata? In that case, the MethodDef row ID would not be reliable, as the version might not match exactly.

  20. kennykerr commented on Dec 12, 2022

    @kennykerr
    ContributorAuthor

    Yes, a simpler approach may be to guarantee that the functions in the Apis class are sorted by name to allow a binary search. Then the following simpler attribute would be sufficient:

    [RAIIFree("Windows.Win32.Graphics.OpenGL.wglDeleteContext")]
  21. riverar commented on Dec 12, 2022

    @riverar
    Collaborator

    Don't think that'll be possible as metadata relies on .NET for the build up and emission of the winmd, and I don't think there are any ordering guarantees or knobs to tweak there. Will take a deeper look though!

  22. mikebattista commented on Mar 8, 2023

    @mikebattista
    Contributor

    Assigning to you Rafael Rivera (@riverar) since it sounds like you've gotten pretty far with this already.

  23. mikebattista commented on May 10, 2023

    @mikebattista
    Contributor

    At this point, all AlsoUsableFor and RAIIFree definitions are in the same namespace as the struct they're on except for:

    • FreeLibrary in Windows.Win32.System.LibraryLoader while HMODULE/HINSTANCE in Windows.Win32.Foundation
    • Local/GlobalFree in Windows.Win32.System.Memory while HLOCAL/HGLOBAL in Windows.Win32.Foundation

    Should we just move these Free functions to Windows.Win32.Foundation and call it a day?

    Looking at the metadata now compared to 2 years ago when this was filed, I think it's fair to enforce that all AlsoUsableFor and RAIIFree relationships must exist in the same namespace. A violation of that would be a bug in the metadata.

  24. kennykerr commented on May 12, 2023

    @kennykerr
    ContributorAuthor

    That's fine with me.

  25. added a commit that references this issue on Mar 28, 2026
    bc43f34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

blockingnamespacesrustusability

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions