Am Sonntag, dem 04.08.2024 um 01:17 +0200 schrieb Alejandro Colomar:

> 
> FUTURE DIRECTIONS:
> 
>       We could make it work with array parameters to functions, and
>       somehow magically return the length designator of the array,
>       regardless of it being really a pointer.

And maybe flexible array members with "counted_by" attribute.



> +
> +/* Implement the lengthof keyword: Return the length of an array,
> +   that is, the number of elements in the array.  */
> +
> +tree
> +c_lengthof_type (location_t loc, tree type)
> +{
> +  enum tree_code type_code;
> +
> +  type_code = TREE_CODE (type);
> +  if (!COMPLETE_TYPE_P (type))
> +    {
> +      error_at (loc,
> +             "invalid application of %<lengthof%> to incomplete type %qT",
> +             type);
> +      return error_mark_node;
> +    }
> +  if (type_code != ARRAY_TYPE)
> +    {
> +      error_at (loc, "invalid application of %<lengthof%> to type %qT", 
> type);
> +      return error_mark_node;
> +    }

I would swap these two errors, because the first is more specific and
less helpful if you pass an incomplete struct, where it would be better
to get the second error.

Martin

> +
> +  return array_type_nelts_top (type);
> +}

Reply via email to