Provide an first-class mechanism for emitting trampolines #236
Labels
Area-OutputGeneration
Issues concerning the process of generating output from Biohazrd
Area-Trampolines
Issues concerning the trampolines API
Concept-OutputFriendliness
Issues concerning the friendliness of using the Biohazrd output
Language-C#
Issues specific to C#
Right now developing custom trampolines (such as
ImGuiStringFunctionWrapperDeclaration
) is slightly brittle because you end up taking a dependency on how Biohazrd emitted its own trampoline.For instance, while working on #235 I broke most of the string trampolines generated for Dear ImGui since they didn't know to start emitting things as byrefs.
We should support an extensible API for developing trampoline methods. This could also be used to simplify the spaghetti mess that is the current output generation for functions.
Good candidates to use the new API:
Since a lot of work is happening in function emit, I'm doing this at the same time:
DisableRuntimeMarshalling
#233These were also resolved while performing this work:
bool
orchar
appears in function pointers. #101Remaining work:
PassthroughReturnAdapter
has special logic for handling them.)CanEmitDefaultValue
should only be true if a default value would be emittedTrampoline
should not check ifDefaultValue
is not null (Adapters may provide their own default value that doesn't fit into our model.)Consider having trampoline names inherit from their function by default instead of copying it.Decided not to do this because it's very annoying for parameter names and I think it's probably better for things to just be consistent.Same thing for parameters. (Slightly complicates determining temporary names ahead of time. This isn't a huge deal except I had the idea to eventually add an API for ensuring temporaries were uniquely named.)This is a pain due to how adapters work (adapters don't necessarily 1:1 correlate with parameters) so I think it's better to just not.TrampolineCollection
TrampolineBuilder
Trampoline
?Trampolines
folder.BoolToByteAdapter
/ByteToBoolReturnAdapter
with a more generalUnsafeAsAdapter
/UnsafeAsReturnAdapter
instead.CastAdapter
/CastReturnAdapter
with three modes:Explicit
,Implicit
,UnsafeAs
.This could maybe replaceDecided not to do this, these adapters only sort-of fit into this shape. They only appear on the native function trampolines so it feels weird to have cast adapters in that context.NonBlittableTypeAdapter
andNonBlittableTypeReturnAdapter
too, although we'd need to add type references for theNativeBoolean
/NativeChar
to handle the late generation of those types.UsePedanticConversion
in the process of doing this. Not a huge deal, let's wait and see if anyone actually needs it and we can consider adding a dedicatedCastKind
just for it. (Or maybe it can just be its own adapter. Generator authors could create this adapter pretty easily if the wanted to.)ReturnByReferenceAdapter
toReturnByBufferAdapter
ReturnByImplicitBufferAdapter
to disambiguate it fromByRefReturnAdapter
. (The former is for implicit return buffers, the latter is for C# byref returns.)ThisPointerAdapter
to not hold on to the type forthis
and instead infer it from the context like we did before.Other things which may wait:
SetLastError
(might just be synthetic adapters with no input.)Allowing type transformation to participate in types within trampolinesTransformationContext
can't really express that it's inside aTrampoline
. It's also an un-fun edge case. Ideally types should be settled by the time trampolines are added (hence why we verify the types didn't change.)(Safe) Mutability of trampolines. (I think the current level of mutability is fine. Anything more complicated than what we have right now would require proper transformation-like support and I don't think there's a strong need. Can revisit if we ever find a compelling use-case.Trampoline
is a record with some init properties andTrampolineCollection
offers some safe mutation, maybe there's more we could add? I didn't have it in mind very much while designing the API. Allowing adapter mutation could be burdensome on implementers.)CSharpCodeGenerator
. (Might keep for temporary backwards compatibility.) -- Let's keep it for now and remove it when pipelines are finished.The text was updated successfully, but these errors were encountered: