HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.2k stars 658 forks source link

Accessing argument metadata from tclass_field #11823

Open Aidan63 opened 1 week ago

Aidan63 commented 1 week ago

Currently there doesn't seem to be a way to access argument metadata on an extern function.

extern class Foo {
    extern function bar(@:cpp.Marshal(c_string) s:String):Void;
}

In the above case there is no way to access the @:cpp.Marshal metadata from a tclass_field. In non extern functions you can inspect the cf_expr TFunction but for externs there is no expression.

I think I've found the place in typeloadFields which types functions but I'm not sure what a good approach is, append the arguments meta to cf_meta and key it by argument name somehow? Add a new cf_argument_meta field?

End goal of this to to have the compiler generate glue code for converting to and from common haxe and c++ types by annotating arguments, it would greatly simplify things being able to look at all extern classes up front as opposed to accumulating a list of all found functions while iterating over the ast.

Simn commented 1 week ago

Extern functions have been quite annoying in this regard. An adjacent problem is argument default values, which are also part of the expression and required a workaround for externs.

I'd be happy to redesign this, but I'm not sure how. The cleanest approach would probably be to merge cf_kind and cf_expr, but that would cause a rather large amount of friction and I don't know if this is worth it for the potential gains.

I don't think something like cf_argument_meta would be that great because then we have redundant information for non-externs.

Aidan63 commented 5 days ago

ah, right, I remember coming across the @:value meta for abstract functions with default values, that would be another benefit of any change.

My initial thought was to just re-use or do something very similar to that, but that does just feel like piling on the bodges.