VLAs as arbitrary expressions

Tom MacDonald uunet!fig.cray.com!tam
Fri Dec 20 13:02:47 PST 1991


>>>>>> Richard Stallman's example
> int foo (double a[b[n]], int b[5], int n)
> 
> int foo (double a[n], short n)
> 
> int foo (double a[(int)(n+4)], double n)
> 
> int foo (double a[*b], int *b)
> 
> int foo (double a[f(b)], struct hack b)
> 
> The last example shows that there are times when the context of use of
> a parameter gives no information about its type.

Just as a point of reference, I ran this example through Cray's
Standard C Compiler.  I changed the function names to foo1, foo2, ...
to avoid name conflicts.  The output that was generated was:

   cc-478 cc: ERROR File = q.c, Line = 9
     The identifier "b" is not declared.
   cc-526 cc: ERROR File = q.c, Line = 9
     The identifier "b" is declared to have an incomplete type.
   cc-489 cc: ERROR File = q.c, Line = 9
     A function call argument has an incomplete type.
   cc-601 cc: ERROR File = q.c, Line = 9
     The variable length array dimension size specifier "a" is not valid.

The first diagnostic is confusing but it is certainly possible to
figure out what is going on.

More importantly, all of the other's were accepted.  The point is that
this is not that difficult to implement.  I do not believe an argument
based solely on implementation difficulty is legitimate.  Document NCEG
91-055 in the last hard copy mailing contains the follow excerpt:

By assigning all non-constant identifiers a *universal* type that is
compatible with any type, the parsing may proceed to the matching `]'
token.  A possible solution to the "lexical ordering problem" then
involves tokenizing, syntactically analyzing, and marking where the
variably qualified expressions are found within dimension specifiers,
until the `)' that terminates the prototype is found.  A second scan
of the variably qualified expressions associates the identifiers with
their definitions, thereby completing the types.  If during the second
scan it is determined that an identifier in a size expression associates
with both a definition in function prototype scope, and a definition
outside the prototype, then a diagnostic message is produced.

This is how the Cray Standard C Compiler implements the VLA feature
in function prototypes.  The VLA size expression parse tree is preserved,
non-enum-member identifiers are assigned a *universal* type, a second
pass is made over VLA size expressions and all *universal* types are
replaced with their correct type. Finally, semantic analysis is performed.

It just wasn't that hard.

better-to-argue-that-it-violates-the-basic-tennants-of-C-ly yours,

Tom MacDonald
tamacray.com
uunet!cray!tam



More information about the Numeric-interest mailing list