Release 970202
[wine] / debugger / types.c
1 /*
2  * File types.c - datatype handling stuff for internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
6  * This really doesn't do much at the moment, but it forms the framework
7  * upon which full support for datatype handling will eventually be hung.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include <assert.h>
14 #include <fcntl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <malloc.h>
21
22 #include "win.h"
23 #include "pe_image.h"
24 #include "peexe.h"
25 #include "debugger.h"
26 #include "peexe.h"
27 #include "xmalloc.h"
28
29 #define NR_TYPE_HASH 521
30
31 int               DEBUG_nchar;
32 static int        DEBUG_maxchar = 1024;
33
34 struct en_values
35 {
36   struct en_values* next;
37   char            * name;
38   int               value;
39 };
40
41 struct member
42 {
43   struct member   * next;
44   char            * name;
45   struct datatype * type;
46   int               offset;
47   int               size;
48 };
49
50 struct datatype
51 {
52   enum  debug_type type;
53   struct datatype * next;
54   char * name;
55   union
56   {
57     struct
58     {
59       char    basic_type;
60       char  * output_format;
61       char    basic_size;
62       unsigned b_signed:1;
63     } basic;
64     struct
65     {
66       unsigned short bitoff;
67       unsigned short nbits;
68       struct datatype * basetype;
69     } bitfield;
70
71     struct
72     {
73       struct datatype * pointsto;
74     } pointer;
75     struct
76     {
77       struct datatype * rettype;
78     } funct;
79     struct
80     {
81       int               start;
82       int               end;
83       struct datatype * basictype;
84     } array;
85     struct
86     {
87       int               size;
88       struct member * members;
89     } structure;
90     struct
91     {
92       struct en_values * members;
93     } enumeration;
94   } un;
95 };
96
97 #define BASIC_INT               1
98 #define BASIC_CHAR              2
99 #define BASIC_LONG              3
100 #define BASIC_UINT              4
101 #define BASIC_LUI               5
102 #define BASIC_LONGLONG          6
103 #define BASIC_ULONGLONGI        7
104 #define BASIC_SHORT             8
105 #define BASIC_SHORTUI           9
106 #define BASIC_SCHAR             10
107 #define BASIC_UCHAR             11
108 #define BASIC_FLT               12
109 #define BASIC_LONG_DOUBLE       13
110 #define BASIC_DOUBLE            14
111 #define BASIC_CMPLX_INT         15
112 #define BASIC_CMPLX_FLT         16
113 #define BASIC_CMPLX_DBL         17
114 #define BASIC_CMPLX_LONG_DBL    18
115 #define BASIC_VOID              19
116
117 struct datatype * DEBUG_TypeInt = NULL;
118 struct datatype * DEBUG_TypeIntConst = NULL;
119 struct datatype * DEBUG_TypeUSInt = NULL;
120 struct datatype * DEBUG_TypeString = NULL;
121
122 /*
123  * All of the types that have been defined so far.
124  */
125 static struct datatype * type_hash_table[NR_TYPE_HASH + 1];
126 static struct datatype * pointer_types = NULL;
127
128 static unsigned int type_hash( const char * name )
129 {
130     unsigned int hash = 0;
131     unsigned int tmp;
132     const char * p;
133
134     p = name;
135
136     while (*p) 
137       {
138         hash = (hash << 4) + *p++;
139
140         if( (tmp = (hash & 0xf0000000)) )
141           {
142             hash ^= tmp >> 24;
143           }
144         hash &= ~tmp;
145       }
146     return hash % NR_TYPE_HASH;
147 }
148
149
150 static struct datatype *
151 DEBUG_InitBasic(int type, char * name, int size, int b_signed, 
152                             char * output_format)
153 {
154   int hash;
155
156   struct datatype * dt;
157   dt = (struct datatype *) xmalloc(sizeof(struct datatype));
158
159   if( dt != NULL )
160     {
161       if( name != NULL )
162         {
163           hash = type_hash(name);
164         }
165       else
166         {
167           hash = NR_TYPE_HASH;
168         }
169
170       dt->type = BASIC;
171       dt->name = name;
172       dt->next = type_hash_table[hash];
173       type_hash_table[hash] = dt;
174       dt->un.basic.basic_type = type;
175       dt->un.basic.basic_size = size;
176       dt->un.basic.b_signed = b_signed;
177       dt->un.basic.output_format = output_format;
178     }
179
180   return dt;
181 }
182
183 static
184 struct datatype *
185 DEBUG_LookupDataType(enum debug_type xtype, int hash, const char * typename)
186 {
187   struct datatype * dt = NULL;
188
189   if( typename != NULL )
190     {
191       for( dt = type_hash_table[hash]; dt; dt = dt->next )
192         {
193           if( xtype != dt->type || dt->name == NULL 
194               || dt->name[0] != typename[0])
195             {
196               continue;
197             }
198                   
199           if( strcmp(dt->name, typename) == 0 )
200             {
201               return dt;
202             }
203         }
204     }
205
206   return dt;
207 }
208
209 struct datatype *
210 DEBUG_NewDataType(enum debug_type xtype, const char * typename)
211 {
212   struct datatype * dt = NULL;
213   int hash;
214
215   /*
216    * The last bucket is special, and is used to hold typeless names.
217    */
218   if( typename == NULL )
219     {
220       hash = NR_TYPE_HASH;
221     }
222   else
223     {
224       hash = type_hash(typename);
225     }
226
227   dt = DEBUG_LookupDataType(xtype, hash, typename);
228
229   if( dt == NULL )
230     {
231       dt = (struct datatype *) xmalloc(sizeof(struct datatype));
232       
233       if( dt != NULL )
234         {
235           memset(dt, 0, sizeof(*dt));
236       
237           dt->type = xtype;
238           if( typename != NULL )
239             {
240               dt->name = xstrdup(typename);
241             }
242           else
243             {
244               dt->name = NULL;
245             }
246           if( xtype == POINTER )
247             {
248               dt->next = pointer_types;
249               pointer_types = dt;
250             }
251           else
252             {
253               dt->next = type_hash_table[hash];
254               type_hash_table[hash] = dt;
255             }
256         }
257     }
258
259   return dt;
260 }
261
262 struct datatype *
263 DEBUG_FindOrMakePointerType(struct datatype * reftype)
264 {
265   struct datatype * dt = NULL;
266
267   if( reftype != NULL )
268     {
269       for( dt = pointer_types; dt; dt = dt->next )
270         {
271           if( dt->type != POINTER )
272             {
273               continue;
274             }
275                   
276           if( dt->un.pointer.pointsto == reftype )
277             {
278               return dt;
279             }
280         }
281     }
282
283   if( dt == NULL )
284     {
285       dt = (struct datatype *) xmalloc(sizeof(struct datatype));
286       
287       if( dt != NULL )
288         {
289           dt->type = POINTER;
290           dt->un.pointer.pointsto = reftype;
291           dt->next = pointer_types;
292           pointer_types = dt;
293         }
294     }
295
296   return dt;
297 }
298
299 void
300 DEBUG_InitTypes()
301 {
302   static int beenhere = 0;
303   struct datatype * chartype;
304
305   if( beenhere++ != 0 )
306     {
307       return;
308     }
309
310   /*
311    * Special version of int used with constants of various kinds.
312    */
313   DEBUG_TypeIntConst = DEBUG_InitBasic(BASIC_INT,NULL,4,1,"%d");
314
315   /*
316    * Initialize a few builtin types.
317    */
318
319
320   DEBUG_TypeInt = DEBUG_InitBasic(BASIC_INT,"int",4,1,"%d");
321   chartype = DEBUG_InitBasic(BASIC_CHAR,"char",1,1,"'%c'");
322   DEBUG_InitBasic(BASIC_LONG,"long int",4,1,"%d");
323   DEBUG_TypeUSInt = DEBUG_InitBasic(BASIC_UINT,"unsigned int",4,0,"%d");
324   DEBUG_InitBasic(BASIC_LUI,"long unsigned int",4,0,"%d");
325   DEBUG_InitBasic(BASIC_LONGLONG,"long long int",8,1,"%ld");
326   DEBUG_InitBasic(BASIC_ULONGLONGI,"long long unsigned int",8,0,"%ld");
327   DEBUG_InitBasic(BASIC_SHORT,"short int",2,1,"%d");
328   DEBUG_InitBasic(BASIC_SHORTUI,"short unsigned int",2,0,"%d");
329   DEBUG_InitBasic(BASIC_SCHAR,"signed char",1,1,"'%c'");
330   DEBUG_InitBasic(BASIC_UCHAR,"unsigned char",1,0,"'%c'");
331   DEBUG_InitBasic(BASIC_FLT,"float",4,0,"%f");
332   DEBUG_InitBasic(BASIC_LONG_DOUBLE,"double",8,0,"%lf");
333   DEBUG_InitBasic(BASIC_DOUBLE,"long double",12,0,NULL);
334   DEBUG_InitBasic(BASIC_CMPLX_INT,"complex int",8,1,NULL);
335   DEBUG_InitBasic(BASIC_CMPLX_FLT,"complex float",8,0,NULL);
336   DEBUG_InitBasic(BASIC_CMPLX_DBL,"complex double",16,0,NULL);
337   DEBUG_InitBasic(BASIC_CMPLX_LONG_DBL,"complex long double",24,0,NULL);
338   DEBUG_InitBasic(BASIC_VOID,"void",0,0,NULL);
339
340   DEBUG_TypeString = DEBUG_NewDataType(POINTER, NULL);
341   DEBUG_SetPointerType(DEBUG_TypeString, chartype);
342
343   /*
344    * Now initialize the builtins for codeview.
345    */
346   DEBUG_InitCVDataTypes();
347
348 }
349
350 long long int
351 DEBUG_GetExprValue(DBG_ADDR * addr, char ** format)
352 {
353   unsigned int rtn;
354   struct datatype * type2 = NULL;
355   struct en_values * e;
356   char * def_format = "0x%x";
357
358   rtn = 0;
359   assert(addr->type != NULL);
360
361   switch(addr->type->type)
362     {
363     case BASIC:
364       if (!DBG_CHECK_READ_PTR( addr,  addr->type->un.basic.basic_size)) 
365         {
366           return 0;
367         }
368
369       memcpy(&rtn, (char *) addr->off, addr->type->un.basic.basic_size);
370       if(    (addr->type->un.basic.b_signed)
371           && ((addr->type->un.basic.basic_size & 3) != 0)
372           && ((rtn >> (addr->type->un.basic.basic_size * 8 - 1)) != 0) )
373         {
374           rtn = rtn | ((-1) << (addr->type->un.basic.basic_size * 8));
375         }
376       if( addr->type->un.basic.output_format != NULL )
377         {
378           def_format = addr->type->un.basic.output_format;
379         }
380
381       /*
382        * Check for single character prints that are out of range.
383        */
384       if( addr->type->un.basic.basic_size == 1
385           && strcmp(def_format, "'%c'") == 0 
386           && ((rtn < 0x20) || (rtn > 0x80)) )
387         {
388           def_format = "%d";
389         }
390       break;
391     case POINTER:
392       if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
393       rtn = (unsigned int)  *((unsigned char **)addr->off);
394       type2 = addr->type->un.pointer.pointsto;
395       if( type2->type == BASIC && type2->un.basic.basic_size == 1 )
396         {
397           def_format = "\"%s\"";
398           break;
399         }
400       else
401         {
402           def_format = "0x%8.8x";
403         }
404       break;
405     case ARRAY:
406     case STRUCT:
407       if (!DBG_CHECK_READ_PTR( addr, 1 )) return 0;
408       rtn = (unsigned int)  *((unsigned char **)addr->off);
409       def_format = "0x%8.8x";
410       break;
411     case ENUM:
412       rtn = (unsigned int)  *((unsigned char **)addr->off);
413       for(e = addr->type->un.enumeration.members; e; e = e->next )
414         {
415           if( e->value == rtn )
416             {
417               break;
418             }
419         }
420       if( e != NULL )
421         {
422           rtn = (int) e->name;
423           def_format = "%s";
424         }
425       else
426         {
427           def_format = "%d";
428         }
429       break;
430     default:
431       rtn = 0;
432       break;
433     }
434
435
436   if( format != NULL )
437     {
438       *format = def_format;
439     }
440   return rtn;
441 }
442
443 unsigned int
444 DEBUG_TypeDerefPointer(DBG_ADDR * addr, struct datatype ** newtype)
445 {
446   /*
447    * Make sure that this really makes sense.
448    */
449   if( addr->type->type != POINTER )
450     {
451       *newtype = NULL;
452       return 0;
453     }
454
455   *newtype = addr->type->un.pointer.pointsto;
456   return *(unsigned int*) (addr->off);
457 }
458
459 unsigned int
460 DEBUG_FindStructElement(DBG_ADDR * addr, const char * ele_name, int * tmpbuf)
461 {
462   struct member * m;
463   unsigned int    mask;
464
465   /*
466    * Make sure that this really makes sense.
467    */
468   if( addr->type->type != STRUCT )
469     {
470       addr->type = NULL;
471       return FALSE;
472     }
473
474   for(m = addr->type->un.structure.members; m; m = m->next)
475     {
476       if( strcmp(m->name, ele_name) == 0 )
477         {
478           addr->type = m->type;
479           if( (m->offset & 7) != 0 || (m->size & 7) != 0)
480             {
481               /*
482                * Bitfield operation.  We have to extract the field and store
483                * it in a temporary buffer so that we get it all right.
484                */
485               *tmpbuf = ((*(int* ) (addr->off + (m->offset >> 3))) >> (m->offset & 7));
486               addr->off = (int) tmpbuf;
487
488               mask = 0xffffffff << (m->size);
489               *tmpbuf &= ~mask;
490               /*
491                * OK, now we have the correct part of the number.
492                * Check to see whether the basic type is signed or not, and if so,
493                * we need to sign extend the number.
494                */
495               if( m->type->type == BASIC && m->type->un.basic.b_signed != 0
496                   && (*tmpbuf & (1 << (m->size - 1))) != 0 )
497                 {
498                   *tmpbuf |= mask;
499                 }
500             }
501           else
502             {
503               addr->off += (m->offset >> 3);
504             }
505           return TRUE;
506         }
507     }
508
509   addr->type = NULL;
510   return FALSE;
511 }
512
513 int
514 DEBUG_SetStructSize(struct datatype * dt, int size)
515 {
516   assert(dt->type == STRUCT);
517
518   if( dt->un.structure.members != NULL )
519     {
520       return FALSE;
521     }
522
523   dt->un.structure.size = size;
524   dt->un.structure.members = NULL;
525
526   return TRUE;
527 }
528
529 int
530 DEBUG_CopyFieldlist(struct datatype * dt, struct datatype * dt2)
531 {
532
533   assert( dt->type == dt2->type && ((dt->type == STRUCT) || (dt->type == ENUM)));
534
535   if( dt->type == STRUCT )
536     {
537       dt->un.structure.members = dt2->un.structure.members;
538     }
539   else
540     {
541       dt->un.enumeration.members = dt2->un.enumeration.members;
542     }
543
544   return TRUE;
545 }
546
547 int
548 DEBUG_AddStructElement(struct datatype * dt, char * name, struct datatype * type, 
549                        int offset, int size)
550 {
551   struct member * m;
552   struct member * last;
553   struct en_values * e;
554
555   if( dt->type == STRUCT )
556     {
557       for(last = dt->un.structure.members; last; last = last->next)
558         {
559           if(    (last->name[0] == name[0]) 
560               && (strcmp(last->name, name) == 0) )
561             {
562               return TRUE;
563             }
564           if( last->next == NULL )
565             {
566               break;
567             }
568         }
569       m = (struct member *) xmalloc(sizeof(struct member));
570       if( m == FALSE )
571         {
572           return FALSE;
573         }
574       
575       m->name = xstrdup(name);
576       m->type = type;
577       m->offset = offset;
578       m->size = size;
579       if( last == NULL )
580         {
581           m->next = dt->un.structure.members;
582           dt->un.structure.members = m;
583         }
584       else
585         {
586           last->next = m;
587           m->next = NULL;
588         }
589       /*
590        * If the base type is bitfield, then adjust the offsets here so that we
591        * are able to look things up without lots of falter-all.
592        */
593       if( type->type == BITFIELD )
594         {
595           m->offset += m->type->un.bitfield.bitoff;
596           m->size = m->type->un.bitfield.nbits;
597           m->type = m->type->un.bitfield.basetype;
598         }
599     }
600   else if( dt->type == ENUM )
601     {
602       e = (struct en_values *) xmalloc(sizeof(struct en_values));
603       if( e == FALSE )
604         {
605           return FALSE;
606         }
607       
608       e->name = xstrdup(name);
609       e->value = offset;
610       e->next = dt->un.enumeration.members;
611       dt->un.enumeration.members = e;
612     }
613   else
614     {
615       assert(FALSE);
616     }
617   return TRUE;
618 }
619
620 struct datatype * 
621 DEBUG_GetPointerType(struct datatype * dt)
622 {
623   if( dt->type == POINTER )
624     {
625       return dt->un.pointer.pointsto;
626     }
627
628   return NULL;
629 }
630
631 int
632 DEBUG_SetPointerType(struct datatype * dt, struct datatype * dt2)
633 {
634   switch(dt->type)
635     {
636     case POINTER:
637       dt->un.pointer.pointsto = dt2;
638       break;
639     case FUNC:
640       dt->un.funct.rettype = dt2;
641       break;
642     default:
643       assert(FALSE);
644     }
645
646   return TRUE;
647 }
648
649 int
650 DEBUG_SetArrayParams(struct datatype * dt, int min, int max, struct datatype * dt2)
651 {
652   assert(dt->type == ARRAY);
653   dt->un.array.start = min;
654   dt->un.array.end   = max;
655   dt->un.array.basictype = dt2;
656
657   return TRUE;
658 }
659
660 int
661 DEBUG_SetBitfieldParams(struct datatype * dt, int offset, int nbits, 
662                         struct datatype * dt2)
663 {
664   assert(dt->type == BITFIELD);
665   dt->un.bitfield.bitoff   = offset;
666   dt->un.bitfield.nbits    = nbits;
667   dt->un.bitfield.basetype = dt2;
668
669   return TRUE;
670 }
671
672 int DEBUG_GetObjectSize(struct datatype * dt)
673 {
674   if( dt == NULL )
675     {
676       return 0;
677     }
678
679   switch(dt->type)
680     {
681     case BASIC:
682       return dt->un.basic.basic_size;
683     case POINTER:
684       return sizeof(int *);
685     case STRUCT:
686       return dt->un.structure.size;
687     case ENUM:
688       return sizeof(int);
689     case ARRAY:
690       return (dt->un.array.end - dt->un.array.start) 
691         * DEBUG_GetObjectSize(dt->un.array.basictype);
692     case BITFIELD:
693       /*
694        * Bitfields have to be handled seperately later on
695        * when we insert the element into the structure.
696        */
697       return 0;
698     case TYPEDEF:
699     case FUNC:
700     case CONST:
701       assert(FALSE);
702     }
703   return 0;
704 }
705
706 unsigned int
707 DEBUG_ArrayIndex(DBG_ADDR * addr, DBG_ADDR * result, int index)
708 {
709   int size;
710
711   /*
712    * Make sure that this really makes sense.
713    */
714   if( addr->type->type == POINTER )
715     {
716       /*
717        * Get the base type, so we know how much to index by.
718        */
719       size = DEBUG_GetObjectSize(addr->type->un.pointer.pointsto);
720       result->type = addr->type->un.pointer.pointsto;
721       result->off = (*(unsigned int*) (addr->off)) + size * index;
722     }
723   else if (addr->type->type == ARRAY)
724     {
725       size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
726       result->type = addr->type->un.array.basictype;
727       result->off = addr->off + size * (index - addr->type->un.array.start);
728     }
729
730   return TRUE;
731 }
732
733 /***********************************************************************
734  *           DEBUG_Print
735  *
736  * Implementation of the 'print' command.
737  */
738 void
739 DEBUG_Print( const DBG_ADDR *addr, int count, char format, int level )
740 {
741   DBG_ADDR        addr1;
742   int             i;
743   struct member * m;
744   char          * pnt;
745   int             size;
746   long long int   value;
747
748   if (count != 1)
749     {
750       fprintf( stderr, "Count other than 1 is meaningless in 'print' command\n" );
751       return;
752     }
753   
754   if( addr->type == NULL )
755     {
756       fprintf(stderr, "Unable to evaluate expression\n");
757       goto leave;
758     }
759   
760   if( level == 0 )
761     {
762       DEBUG_nchar = 0;
763     }
764
765   if( DEBUG_nchar > DEBUG_maxchar )
766     {
767       fprintf(stderr, "...");
768       goto leave;
769     }
770
771   if( format == 'i' || format == 's' || format == 'w' || format == 'b' )
772     {
773       fprintf( stderr, "Format specifier '%c' is meaningless in 'print' command\n", format );
774       format = '\0';
775     }
776
777   switch(addr->type->type)
778     {
779     case BASIC:
780     case ENUM:
781     case CONST:
782     case POINTER:
783       DEBUG_PrintBasic(addr, 1, format);
784       break;
785     case STRUCT:
786       DEBUG_nchar += fprintf(stderr, "{");
787       for(m = addr->type->un.structure.members; m; m = m->next)
788         {
789           addr1 = *addr;
790           DEBUG_FindStructElement(&addr1, m->name,
791                                   (int *) &value);
792           DEBUG_nchar += fprintf(stderr, "%s=", m->name);
793           DEBUG_Print(&addr1, 1, format, level + 1);
794           if( m->next != NULL )
795             {
796               DEBUG_nchar += fprintf(stderr, ", ");
797             }
798           if( DEBUG_nchar > DEBUG_maxchar )
799             {
800               fprintf(stderr, "...}");
801               goto leave;
802             }
803         }
804       DEBUG_nchar += fprintf(stderr, "}");
805       break;
806     case ARRAY:
807       /*
808        * Loop over all of the entries, printing stuff as we go.
809        */
810       size = DEBUG_GetObjectSize(addr->type->un.array.basictype);
811       if( size == 1 )
812         {
813           /*
814            * Special handling for character arrays.
815            */
816           pnt = (char *) addr->off;
817           DEBUG_nchar += fprintf(stderr, "\"");
818           for( i=addr->type->un.array.start; i < addr->type->un.array.end; i++ )
819             {
820               fputc(*pnt++, stderr);
821               DEBUG_nchar++;
822               if( DEBUG_nchar > DEBUG_maxchar )
823                 {
824                   fprintf(stderr, "...\"");
825                   goto leave;
826                 }
827             }
828           DEBUG_nchar += fprintf(stderr, "\"");
829           break;
830         }
831       addr1 = *addr;
832       addr1.type = addr->type->un.array.basictype;
833       DEBUG_nchar += fprintf(stderr, "{");
834       for( i=addr->type->un.array.start; i <= addr->type->un.array.end; i++ )
835         {
836           DEBUG_Print(&addr1, 1, format, level + 1);
837           addr1.off += size;
838           if( i == addr->type->un.array.end )
839             {
840               DEBUG_nchar += fprintf(stderr, "}");
841             }
842           else
843             {
844               DEBUG_nchar += fprintf(stderr, ", ");
845             }
846           if( DEBUG_nchar > DEBUG_maxchar )
847             {
848               fprintf(stderr, "...}");
849               goto leave;
850             }
851         }
852       break;
853     default:
854       assert(FALSE);
855       break;
856     }
857
858 leave:
859
860   if( level == 0 )
861     {
862       DEBUG_nchar += fprintf(stderr, "\n");
863     }
864   return;
865 }
866
867 int
868 DEBUG_DumpTypes()
869 {
870   struct datatype * dt = NULL;
871   struct member * m;
872   int hash;
873   int nm;
874   char * name;
875   char * member_name;
876
877   for(hash = 0; hash < NR_TYPE_HASH + 1; hash++)
878     {
879       for( dt = type_hash_table[hash]; dt; dt = dt->next )
880         {
881           name =  "none";
882           if( dt->name != NULL )
883             {
884               name = dt->name;
885             }
886           switch(dt->type)
887             {
888             case BASIC:
889               fprintf(stderr, "0x%p - BASIC(%s)\n",
890                       dt, name);
891               break;
892             case POINTER:
893               fprintf(stderr, "0x%p - POINTER(%s)(%p)\n",
894                       dt, name, dt->un.pointer.pointsto);
895               break;
896             case STRUCT:
897               member_name = "none";
898               nm = 0;
899               if( dt->un.structure.members != NULL
900                   && dt->un.structure.members->name != NULL )
901                 {
902                   member_name = dt->un.structure.members->name;
903                   for( m = dt->un.structure.members; m; m = m->next)
904                     {
905                       nm++;
906                     }
907                 }
908               fprintf(stderr, "0x%p - STRUCT(%s) %d %d %s\n", dt, name,
909                       dt->un.structure.size, nm, member_name);
910               break;
911             case ARRAY:
912               fprintf(stderr, "0x%p - ARRAY(%s)(%p)\n",
913                       dt, name, dt->un.array.basictype);
914               break;
915             case ENUM:
916               fprintf(stderr, "0x%p - ENUM(%s)\n",
917                       dt, name);
918               break;
919             case BITFIELD:
920               fprintf(stderr, "0x%p - BITFIELD(%s)\n", dt, name);
921               break;
922             case FUNC:
923               fprintf(stderr, "0x%p - FUNC(%s)(%p)\n",
924                       dt, name, dt->un.funct.rettype);
925               break;
926             case CONST:
927             case TYPEDEF:
928               fprintf(stderr, "What???\n");
929               break;
930             }
931         }
932     }
933   return TRUE;
934 }
935
936
937 enum debug_type DEBUG_GetType(struct datatype * dt)
938 {
939   return dt->type;
940 }
941
942 struct datatype *
943 DEBUG_TypeCast(enum debug_type type, const char * name)
944 {
945   int                     hash;
946   struct datatype       * rtn;
947
948   /*
949    * The last bucket is special, and is used to hold typeless names.
950    */
951   if( name == NULL )
952     {
953       hash = NR_TYPE_HASH;
954     }
955   else
956     {
957       hash = type_hash(name);
958     }
959
960   rtn = DEBUG_LookupDataType(type, hash, name);
961
962   return rtn;
963
964 }
965
966 int
967 DEBUG_PrintTypeCast(struct datatype * dt)
968 {
969   char          * name;
970
971   name =  "none";
972   if( dt->name != NULL )
973     {
974       name = dt->name;
975     }
976
977   switch(dt->type)
978     {
979     case BASIC:
980       fprintf(stderr, "%s", name);
981       break;
982     case POINTER:
983       DEBUG_PrintTypeCast(dt->un.pointer.pointsto);
984       fprintf(stderr, "*");
985       break;
986     case STRUCT:
987       fprintf(stderr, "struct %s", name);
988       break;
989     case ARRAY:
990       fprintf(stderr, "%s[]", name);
991       break;
992     case ENUM:
993       fprintf(stderr, "enum %s", name);
994       break;
995     case BITFIELD:
996       fprintf(stderr, "unsigned %s", name);
997       break;
998     case FUNC:
999       DEBUG_PrintTypeCast(dt->un.funct.rettype);
1000       fprintf(stderr, "(*%s)()", name);
1001       break;
1002     case CONST:
1003     case TYPEDEF:
1004       fprintf(stderr, "What???\n");
1005       break;
1006     }
1007
1008   return TRUE;
1009 }
1010
1011