On Aug 5, 2024, at 16:59, Alejandro Colomar <a...@kernel.org> wrote:


The “counted-by” attribute currently is not in the TYPE system, and
we plan to add it into the TYPE system later through language
standard (or an GCC extension).  If that happens, then both the
“sizeof” and the “__lengthof__” operators should be automatically
evaluate the “size" or the “length” for the expr through its TYPE.
(Just as the current VLA, its size and length already in the TYPE,
therefore both “sizeof” and “__lengthof__” should evaluate VLA.

I'm curious; how do you plan to make counted_by as part of the type
system?  I've read the paper for using a .identifier length designator
(n3188; <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3188.htm>),
but that's a constant, and doesn't use an attribute.

The “counted_by” attribute is only a temporary and practical solution at this 
moment
 to build a direct relationship between the length of the array and and array 
itself in the
source code level, but not touching the TYPE system at all.
The final plan is similar as the solution in the above paper you
referred.

i.e, currently, with “counted_by” attribute:

struct foo {
  unsigned int count;
  char array [] _attribute__ ((counted_by (count));
};

Later, when the relationship is built into TYPE, the above will become:

struct foo {
  unsigned int count;
  char array [.count];
};

That will be the cleanest solution to this problem.
However, might take a much longer time to final get into the compiler.

Thanks.

Qing

Reply via email to