MochiLibraries / Biohazrd

A framework for automatically generating binding wrappers for C/C++ libraries
MIT License
60 stars 8 forks source link

Translate size_t as nuint and ptrdiff_t as nint #82

Closed PathogenDavid closed 2 years ago

PathogenDavid commented 3 years ago

It dawned on me that Clang (apparently?) doesn't consider size_t to be its own type, which seems odd to me. We should investigate if it is possible to determine if a CXType_ULongLong/CXType_UInt is actually a size_t and translate it as nint instead of the architecture-specific type.

PathogenDavid commented 3 years ago

Finally got around to investigating this, it is implemented as a built-in typedef to the appropriate type.

nint/nuint don't fit cleanly into CSharpBuiltinType, so I'm not going to use it for them:

PathogenDavid commented 3 years ago

To enable representing nint/nuint, I ported ManuallyImplementedTypeDeclaration/Reference from Ares.Biohazrd as ExternallyDefinedTypeDeclaration/Reference, so make sure to close https://github.com/InfectedLibraries/Biohazrd/issues/144 alongside this one. Actually that issue is slightly different, so it should not be closed.

PathogenDavid commented 3 years ago

The externally defined type reference/declaration helpers were added in https://github.com/InfectedLibraries/Biohazrd/commit/65b36c544a9a01340259fd73543637ab52ddfe44

I need to investigate slightly further to figure out the best way to identify size_t since it comes through as an internal typedef and we don't want to erroneously translate a custom size_t in a different namespace.

It's probably ok to replace size_t typedefs in the global namespace 99.999% of the time, but for whatever reason both Clang and GCC allow redefining it without so much as a warning. MSVC takes the sane approach and does not allow it. Derp, forgot size_t isn't available without stddef.h on other platforms.

PathogenDavid commented 3 years ago

size_t is unsigned by definition, amended title to reflect that. While we're at it we should add translating ptrdiff_t as a signed native integer too.

PathogenDavid commented 2 years ago

Done in https://github.com/MochiLibraries/Biohazrd/commit/a09893e8201e043d5dbdab75a4f5af635171cec3.

I also added support for intptr_t and uintptr_t since they're similar.

I considered using System.(U)IntPtr for them, but System.IntPtr is really meant more for opaque handles, whereas nint is meant for things you treat as numeric values and that's what intptr_t seems to be intended for as well.