RFR: 7903649: Field and global variables of array type should have indexed accessors
Maurizio Cimadamore
mcimadamore at openjdk.org
Tue Jan 30 10:20:39 UTC 2024
On Mon, 29 Jan 2024 22:57:57 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:
>> test/jtreg/generator/arrayAccess/TestArrayAccess.java line 48:
>>
>>> 46: try (Arena arena = Arena.ofConfined()) {
>>> 47: MemorySegment foo = Foo.allocate(arena);
>>> 48: for (int i = 0 ; i < 2 ; i++) {
>>
>> So, the array length is hard-coded here. What would it take to derive the length from the bindings?
>>
>> I think in this case it would be:
>>
>>
>> long count = Foo.layout().select(groupElement("ints1")).elementCount();
>>
>>
>> ?
>
> P.S. I guess what I'm implying here is: maybe we need a way to make getting the length of an array field easier as well
Good observation... unfortunately I'm not too sure how to address this - in the sense that a single field can have N length accessors (one per dimension) - well, or at least N static fields. We could simply expose the field layout (and leave it to the client to decode the dimensions) - but that's not too usable, and it also feels a bit redundant (since the field layout can be derived, as you show, from the struct layout). Also, for globals we ditched layouts (because of accessor naming troubles), so the same approach wouldn't work there.
It feels like doubling down on layouts is the right way to go - and it would be generally useful to maybe have a layout per field/global variable. But the logistics of it are complicated, especially for global variables (where you'd need a extra holder class for each global variable to store the layout), and naming can be a pain too.
-------------
PR Review Comment: https://git.openjdk.org/jextract/pull/198#discussion_r1470952961
More information about the jextract-dev
mailing list