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