Type checking for typedef's (new feature)

Gregory Smith greg at utcsri.UUCP
Thu Jun 5 01:41:58 AEST 1986


In article <138 at ace.UUCP> henk at ace.UUCP (Henk Hesselink) writes:
>In article <361 at batcomputer.TN.CORNELL.EDU> garry%cadif-oak at cu-arpa.cs.cornell.edu.arpa writes:
>
>> I am reminded of a long-standing pet peeve of mine: I can get
>> type-checking on structures and unions. I can get type-checking
>> on primitive types (int, float, ...). I CANNOT get type-checking
>> on datatypes declared via 'typedef' EXCEPT in terms of their underlying
>> types. (I assume this all is still true in the new language standard.)
>> 
>> 
>> Is it reasonable? Is it hard to implement? Comments?.
>
>It is certainly reasonable, a little more type-checking in C would not
>be at all amiss, but:
>
>"It must be emphasised that a typedef declaration does not create a
>new type in any sense" (K&R, page 141).
>
It *could* be optional.

>The problem is that typedefs get converted back to basic types fairly
>early on in pass 1 of pcc (which, plus derivatives thereof, constitutes
>a sizeable chunk of all C compilers), while serious type-checking is
>mostly done in pass 2.  This means 1) letting pcc1 keep typedefs intact,
>2) augmenting the intermediate code to allow this kind of information
>to be specified and 3) getting pcc2 to do something useful with it.

I don't think the second pass would need to know about it.
Types are stored in PCC as a string of 2-bit qualifiers preceding
a 'basic type' specifier:

	..!QUAL!QUAL!QUAL!QUAL!BASIC ( # of qualifiers depends on word size )

The qualifiers are FTN,PTR,ARY ( function returning, pointer to, array of )
and the fourth state (00) is used to 0-pad on the left. The basic types
are things like INT, UNSIGNED, STRTYPE ( i.e. struct ) ENUMTYPE ( enums ).
By adding a TYPDEF basic type, the typedef information could be retained and
checked in the first pass. The type words with TYPDEF could then be easily
converted to their actual types before writing the intermediate file. In fact
this is exactly what is done with enums, which are not seen by the second
pass and appear in the intermediate file as int constants.
The big problem is deciding *when* to issue a warning and when to
overlook mismatches.  Suppose you have 'typedef int foo;' and 'foo
i,j,k;'. then obviously i=j is ok, and so is i=j+k. But the '+' sees
two typedefs in the latter, and has to be smart enough to check them,
find out what they really are, and bump the result back to 'foo'. And
if you have 'int q', is j+q of type int or foo? do 'q +=i ' and/or
'i=j+q' give warnings? If good semantics for this could be worked out,
it could be a real aid to writing portable programs.

>Hard to implement?  Depends on where you stand, but certainly not some-
>thing you'd hack up in an afternoon.
>
True.

-- 
"We demand rigidly defined areas of doubt and uncertainty!" - Vroomfondel
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg



More information about the Comp.lang.c mailing list