MochiLibraries / Biohazrd

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

Biohazrd should have a dedicated fallback type instead of using int #109

Open PathogenDavid opened 3 years ago

PathogenDavid commented 3 years ago

Right now if Biohazrd fails to emit a type, it will emit it as int in order to make sure the C# code still compiles:

[FieldOffset(0)] public /* Failed to emit TranslatedNormalField ShortPair: ClangTypeReference is not supported by the C# output generator. */
int ShortPair;

This is great during generator development because it means you can test unaffected portions of the library without having to resolve these issues.

Unfortunately it also becomes non-obvious that affected members are broken.

In this scenario, we should emit a special type to signify that this member is broken. (We cannot realistically go back and mark the member as obsolete because this is last-chance error handling that occurs while the file is being written. Ideally we might do that for certain cases, but it'd be part of the verification stage.)

PathogenDavid commented 3 years ago

Note: Make sure to update the documentation for KludgeUnknownClangTypesIntoBuiltinTypesTransformation when this is completed.

PathogenDavid commented 3 years ago

Another quirk revealed by this issue: If more than one function fails to emit a type it can break disambiguation by operator overloading. IE:

class MyClass {}; // <-- Remove this declaration with a transform

void Write(int* x);
void Write(MyClass* x);

will become:

void Write(int* x);
void Write(/* Failed to emit TranslatedParameter x: Failed to resolve `Ref resolved by MyClass` during emit time. `*/ int* x);

Which cannot build.

PathogenDavid commented 3 years ago

Honestly we should instead just fail these sorts of methods during the verification stage.

Edit: We actually need to do this after the broken declaration extractor runs because it can break references. (It'd actually probably have to be a persistent transformation.)

PathogenDavid commented 3 years ago

Honestly we should instead just fail these sorts of methods during the verification stage.

https://github.com/InfectedLibraries/Biohazrd/issues/47

PathogenDavid commented 2 years ago

While we should definitely fail these methods during verification, I think it could be valuable to have the ability to last-minute remove a failed declaration.

We already buffer the contents of the file with a StringBuilder inside CodeWriter, we could pretty easily use StringBuilder.Insert or StringBuilder.Remove to either #if-disable/remove broken code instead of straining ourselves to make it valid.