[Cobolforgcc-devel] Re: %union
Status: Pre-Alpha
Brought to you by:
timjosling
From: Tim J. <te...@me...> - 2001-06-05 20:19:29
|
Keisuke, I have experimented with several different options here and none of them seem ideal to me. By having a union, it means that you need to say ptr->which_one.member every time you access something. Alternatively you can do what gcc does, which is have a union but dfine accessor macros eg TREE_USER(ptr)=1; I don't like accessor macros really because you have to keep looking up what actual fields they are accessing. As far as I can tell, a union is not type safe. If you use the wrong member it will still not work. The approach I took is to define seperate structures and do casts. At first, I used to cast on every access, which became tedious very quickly (you still see a fair bit of this code lying around). So now I cast into a variable which is of the right type and use that variable. This would not cause any runtime slowdown as the cast is opimized away. This also gives me a good opportunity to put in an assert or a type check to improve the integrity of the program. This is the closest to type safeness I have been able to get. eg void myfun (void* ptr1) { struct xxx1 *ptr2; ptr2=(struct xxx1 *) ptr1; if (ptr2->type != what_i_expect) abort(); IMHO this gives the best combination of type checking and least typing, but I woudl like a better solution. By the way I will be starting work on IF again this weekend which is a three day weekend here. Tim Josling Keisuke Nishida wrote: > > Tim, > > It seems that you don't use %union in your parser and use lots > of type castings instead. Is there a good reason to do so? > I don't think too much use of type casting is a good thing > because it disables the C compiler from doing type checking. > > Keisuke |