Dump the pidl of a lnk file.
[wine] / tools / winedump / msmangle.c
1 /*
2  *  Demangle VC++ symbols into C function prototypes
3  *
4  *  Copyright 2000 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winedump.h"
25
26 /* Type for parsing mangled types */
27 typedef struct _compound_type
28 {
29   char  dest_type;
30   int   flags;
31   int   have_qualifiers;
32   char *expression;
33 } compound_type;
34
35
36 /* Initialise a compound type structure */
37 #define INIT_CT(ct) do { memset (&ct, 0, sizeof (ct)); } while (0)
38
39 /* free the memory used by a compound structure */
40 #define FREE_CT(ct) do { if (ct.expression) free (ct.expression); } while (0)
41
42 /* Flags for data types */
43 #define DATA_VTABLE   0x1
44
45 /* Internal functions */
46 static char *demangle_datatype (char **str, compound_type *ct,
47                                 parsed_symbol* sym);
48
49 static char *get_constraints_convention_1 (char **str, compound_type *ct);
50
51 static char *get_constraints_convention_2 (char **str, compound_type *ct);
52
53 static char *get_type_string (const char c, const int constraints);
54
55 static int   get_type_constant (const char c, const int constraints);
56
57 static char *get_pointer_type_string (compound_type *ct,
58                                       const char *expression);
59
60
61 /*******************************************************************
62  *         demangle_symbol
63  *
64  * Demangle a C++ linker symbol into a C prototype
65  */
66 int symbol_demangle (parsed_symbol *sym)
67 {
68   compound_type ct;
69   int is_static = 0, is_const = 0;
70   char *function_name = NULL;
71   char *class_name = NULL;
72   char *name, *const_status;
73   static unsigned int hash = 0; /* In case of overloaded functions */
74   unsigned int data_flags = 0;
75
76   assert (globals.do_code);
77   assert (sym && sym->symbol);
78
79   hash++;
80
81   /* MS mangled names always begin with '?' */
82   name = sym->symbol;
83   if (*name++ != '?')
84     return -1;
85
86   if (VERBOSE)
87     puts ("Attempting to demangle symbol");
88
89   /* Then function name or operator code */
90   if (*name == '?')
91   {
92     /* C++ operator code (one character, or two if the first is '_') */
93     switch (*++name)
94     {
95     case '0': function_name = strdup ("ctor"); break;
96     case '1': function_name = strdup ("dtor"); break;
97     case '2': function_name = strdup ("operator_new"); break;
98     case '3': function_name = strdup ("operator_delete"); break;
99     case '4': function_name = strdup ("operator_equals"); break;
100     case '5': function_name = strdup ("operator_shiftright"); break;
101     case '6': function_name = strdup ("operator_shiftleft"); break;
102     case '7': function_name = strdup ("operator_not"); break;
103     case '8': function_name = strdup ("operator_equalsequals"); break;
104     case '9': function_name = strdup ("operator_notequals"); break;
105     case 'A': function_name = strdup ("operator_array"); break;
106     case 'C': function_name = strdup ("operator_dereference"); break;
107     case 'D': function_name = strdup ("operator_multiply"); break;
108     case 'E': function_name = strdup ("operator_plusplus"); break;
109     case 'F': function_name = strdup ("operator_minusminus"); break;
110     case 'G': function_name = strdup ("operator_minus"); break;
111     case 'H': function_name = strdup ("operator_plus"); break;
112     case 'I': function_name = strdup ("operator_address"); break;
113     case 'J': function_name = strdup ("operator_dereferencememberptr"); break;
114     case 'K': function_name = strdup ("operator_divide"); break;
115     case 'L': function_name = strdup ("operator_modulo"); break;
116     case 'M': function_name = strdup ("operator_lessthan"); break;
117     case 'N': function_name = strdup ("operator_lessthanequal"); break;
118     case 'O': function_name = strdup ("operator_greaterthan"); break;
119     case 'P': function_name = strdup ("operator_greaterthanequal"); break;
120     case 'Q': function_name = strdup ("operator_comma"); break;
121     case 'R': function_name = strdup ("operator_functioncall"); break;
122     case 'S': function_name = strdup ("operator_complement"); break;
123     case 'T': function_name = strdup ("operator_xor"); break;
124     case 'U': function_name = strdup ("operator_logicalor"); break;
125     case 'V': function_name = strdup ("operator_logicaland"); break;
126     case 'W': function_name = strdup ("operator_or"); break;
127     case 'X': function_name = strdup ("operator_multiplyequals"); break;
128     case 'Y': function_name = strdup ("operator_plusequals"); break;
129     case 'Z': function_name = strdup ("operator_minusequals"); break;
130     case '_':
131       switch (*++name)
132       {
133       case '0': function_name = strdup ("operator_divideequals"); break;
134       case '1': function_name = strdup ("operator_moduloequals"); break;
135       case '2': function_name = strdup ("operator_shiftrightequals"); break;
136       case '3': function_name = strdup ("operator_shiftleftequals"); break;
137       case '4': function_name = strdup ("operator_andequals"); break;
138       case '5': function_name = strdup ("operator_orequals"); break;
139       case '6': function_name = strdup ("operator_xorequals"); break;
140       case '7': function_name = strdup ("vftable"); data_flags = DATA_VTABLE; break;
141       case '8': function_name = strdup ("vbtable"); data_flags = DATA_VTABLE; break;
142       case '9': function_name = strdup ("vcall"); data_flags = DATA_VTABLE; break;
143       case 'A': function_name = strdup ("typeof"); data_flags = DATA_VTABLE; break;
144       case 'B': function_name = strdup ("local_static_guard"); data_flags = DATA_VTABLE; break;
145       case 'C': function_name = strdup ("string"); data_flags = DATA_VTABLE; break;
146       case 'D': function_name = strdup ("vbase_dtor"); data_flags = DATA_VTABLE; break;
147       case 'E': function_name = strdup ("vector_dtor"); break;
148       case 'G': function_name = strdup ("scalar_dtor"); break;
149       case 'H': function_name = strdup ("vector_ctor_iter"); break;
150       case 'I': function_name = strdup ("vector_dtor_iter"); break;
151       case 'J': function_name = strdup ("vector_vbase_ctor_iter"); break;
152       case 'L': function_name = strdup ("eh_vector_ctor_iter"); break;
153       case 'M': function_name = strdup ("eh_vector_dtor_iter"); break;
154       case 'N': function_name = strdup ("eh_vector_vbase_ctor_iter"); break;
155       case 'O': function_name = strdup ("copy_ctor_closure"); break;
156       case 'S': function_name = strdup ("local_vftable"); data_flags = DATA_VTABLE; break;
157       case 'T': function_name = strdup ("local_vftable_ctor_closure"); break;
158       case 'U': function_name = strdup ("operator_new_vector"); break;
159       case 'V': function_name = strdup ("operator_delete_vector"); break;
160       case 'X': function_name = strdup ("placement_new_closure"); break;
161       case 'Y': function_name = strdup ("placement_delete_closure"); break;
162       default:
163         return -1;
164       }
165       break;
166     default:
167       /* FIXME: Other operators */
168       return -1;
169     }
170     name++;
171   }
172   else
173   {
174     /* Type or function name terminated by '@' */
175     function_name = name;
176     while (*name && *name++ != '@') ;
177     if (!*name)
178       return -1;
179     function_name = str_substring (function_name, name - 1);
180   }
181
182   /* Either a class name, or '@' if the symbol is not a class member */
183   if (*name == '@')
184   {
185     class_name = strdup ("global"); /* Non member function (or a datatype) */
186     name++;
187   }
188   else
189   {
190     /* Class the function is associated with, terminated by '@@' */
191     class_name = name;
192     while (*name && *name++ != '@') ;
193     if (*name++ != '@')
194       return -1;
195     class_name = str_substring (class_name, name - 2);
196   }
197
198   /* Function/Data type and access level */
199   /* FIXME: why 2 possible letters for each option? */
200   switch(*name++)
201   {
202   /* Data */
203
204   case '0' : /* private static */
205   case '1' : /* protected static */
206   case '2' : /* public static */
207     is_static = 1;
208     /* Fall through */
209   case '3' : /* non static */
210   case '4' : /* non static */
211     /* Data members need to be implemented: report */
212     INIT_CT (ct);
213     if (!demangle_datatype (&name, &ct, sym))
214     {
215       if (VERBOSE)
216         printf ("/*FIXME: %s: unknown data*/\n", sym->symbol);
217       return -1;
218     }
219     sym->flags |= SYM_DATA;
220     sym->argc = 1;
221     sym->arg_name[0] = str_create (5, OUTPUT_UC_DLL_NAME, "_", class_name,
222                                   is_static ? "static_" : "_", function_name);
223     sym->arg_text[0] = str_create (3, ct.expression, " ", sym->arg_name[0]);
224     FREE_CT (ct);
225     return 0;
226     break;
227
228   case '6' : /* compiler generated static */
229   case '7' : /* compiler generated static */
230     if (data_flags & DATA_VTABLE)
231     {
232       sym->flags |= SYM_DATA;
233       sym->argc = 1;
234       sym->arg_name[0] = str_create (5, OUTPUT_UC_DLL_NAME, "_", class_name,
235                                      "_", function_name);
236       sym->arg_text[0] = str_create (2, "void *", sym->arg_name[0]);
237
238       if (VERBOSE)
239         puts ("Demangled symbol OK [vtable]");
240       return 0;
241     }
242     return -1;
243     break;
244
245   /* Functions */
246
247   case 'E' : /* private virtual */
248   case 'F' : /* private virtual */
249   case 'M' : /* protected virtual */
250   case 'N' : /* protected virtual */
251   case 'U' : /* public virtual */
252   case 'V' : /* public virtual */
253     /* Virtual functions need to be added to the exported vtable: report */
254     if (VERBOSE)
255       printf ("/*FIXME %s: %s::%s is virtual-add to vftable*/\n", sym->symbol,
256               class_name, function_name);
257     /* Fall through */
258   case 'A' : /* private */
259   case 'B' : /* private */
260   case 'I' : /* protected */
261   case 'J' : /* protected */
262   case 'Q' : /* public */
263   case 'R' : /* public */
264     /* Implicit 'this' pointer */
265     sym->arg_text [sym->argc] = str_create (3, "struct ", class_name, " *");
266     sym->arg_type [sym->argc] = ARG_POINTER;
267     sym->arg_flag [sym->argc] = 0;
268     sym->arg_name [sym->argc++] = strdup ("_this");
269     /* New struct definitions can be 'grep'ed out for making a fixup header */
270     if (VERBOSE)
271       printf ("struct %s { void **vtable; /*FIXME: class definition */ };\n", class_name);
272     break;
273   case 'C' : /* private: static */
274   case 'D' : /* private: static */
275   case 'K' : /* protected: static */
276   case 'L' : /* protected: static */
277   case 'S' : /* public: static */
278   case 'T' : /* public: static */
279     is_static = 1; /* No implicit this pointer */
280     break;
281   case 'Y' :
282   case 'Z' :
283     break;
284   /* FIXME: G,H / O,P / W,X are private / protected / public thunks */
285   default:
286     return -1;
287   }
288
289   /* If there is an implicit this pointer, const status follows */
290   if (sym->argc)
291   {
292    switch (*name++)
293    {
294    case 'A': break; /* non-const */
295    case 'B': is_const = CT_CONST; break;
296    case 'C': is_const = CT_VOLATILE; break;
297    case 'D': is_const = (CT_CONST | CT_VOLATILE); break;
298    default:
299      return -1;
300    }
301   }
302
303   /* Next is the calling convention */
304   switch (*name++)
305   {
306   case 'A': /* __cdecl */
307   case 'B': /* __cdecl __declspec(dllexport) */
308     if (!sym->argc)
309     {
310       sym->flags |= SYM_CDECL;
311       break;
312     }
313     /* Else fall through */
314   case 'C': /* __pascal */
315   case 'D': /* __pascal __declspec(dllexport) */
316   case 'E': /* __thiscall */
317   case 'F': /* __thiscall __declspec(dllexport) */
318   case 'G': /* __stdcall */
319   case 'H': /* __stdcall __declspec(dllexport) */
320   case 'I': /* __fastcall */
321   case 'J': /* __fastcall __declspec(dllexport)*/
322   case 'K': /* default (none given) */
323     if (sym->argc)
324       sym->flags |= SYM_THISCALL;
325     else
326       sym->flags |= SYM_STDCALL;
327     break;
328   default:
329     return -1;
330   }
331
332   /* Return type, or @ if 'void' */
333   if (*name == '@')
334   {
335     sym->return_text = strdup ("void");
336     sym->return_type = ARG_VOID;
337     name++;
338   }
339   else
340   {
341     INIT_CT (ct);
342     if (!demangle_datatype (&name, &ct, sym))
343       return -1;
344     sym->return_text = ct.expression;
345     sym->return_type = get_type_constant(ct.dest_type, ct.flags);
346     ct.expression = NULL;
347     FREE_CT (ct);
348   }
349
350   /* Now come the function arguments */
351   while (*name && *name != 'Z')
352   {
353     /* Decode each data type and append it to the argument list */
354     if (*name != '@')
355     {
356       INIT_CT (ct);
357       if (!demangle_datatype(&name, &ct, sym))
358         return -1;
359
360       if (strcmp (ct.expression, "void"))
361       {
362         sym->arg_text [sym->argc] = ct.expression;
363         ct.expression = NULL;
364         sym->arg_type [sym->argc] = get_type_constant (ct.dest_type, ct.flags);
365         sym->arg_flag [sym->argc] = ct.flags;
366         sym->arg_name[sym->argc] = str_create_num (1, sym->argc, "arg");
367         sym->argc++;
368       }
369       else
370         break; /* 'void' terminates an argument list */
371       FREE_CT (ct);
372     }
373     else
374       name++;
375   }
376
377   while (*name == '@')
378     name++;
379
380   /* Functions are always terminated by 'Z'. If we made it this far and
381    * Don't find it, we have incorrectly identified a data type.
382    */
383   if (*name != 'Z')
384     return -1;
385
386   /* Note: '()' after 'Z' means 'throws', but we don't care here */
387
388   /* Create the function name. Include a unique number because otherwise
389    * overloaded functions could have the same c signature.
390    */
391   switch (is_const)
392   {
393   case (CT_CONST | CT_VOLATILE): const_status = "_const_volatile"; break;
394   case CT_CONST: const_status = "_const"; break;
395   case CT_VOLATILE: const_status = "_volatile"; break;
396   default: const_status = "_"; break;
397   }
398   sym->function_name = str_create_num (4, hash, class_name, "_",
399        function_name, is_static ? "_static" : const_status);
400
401   assert (sym->return_text);
402   assert (sym->flags);
403   assert (sym->function_name);
404
405   free (class_name);
406   free (function_name);
407
408   if (VERBOSE)
409     puts ("Demangled symbol OK");
410
411   return 0;
412 }
413
414
415 /*******************************************************************
416  *         demangle_datatype
417  *
418  * Attempt to demangle a C++ data type, which may be compound.
419  * a compound type is made up of a number of simple types. e.g:
420  * char** = (pointer to (pointer to (char)))
421  *
422  * Uses a simple recursive descent algorithm that is broken
423  * and/or incomplete, without a doubt ;-)
424  */
425 static char *demangle_datatype (char **str, compound_type *ct,
426                                 parsed_symbol* sym)
427 {
428   char *iter;
429
430   assert (str && *str);
431   assert (ct);
432
433   iter = *str;
434
435   if (!get_constraints_convention_1 (&iter, ct))
436     return NULL;
437
438   if (*iter == '_')
439   {
440     /* MS type: __int8,__int16 etc */
441     ct->flags |= CT_EXTENDED;
442     iter++;
443   }
444
445   switch (*iter)
446   {
447     case 'C': case 'D': case 'E': case 'F': case 'G':
448     case 'H': case 'I': case 'J': case 'K': case 'M':
449     case 'N': case 'O': case 'X': case 'Z':
450       /* Simple data types */
451       ct->dest_type = *iter++;
452       if (!get_constraints_convention_2 (&iter, ct))
453         return NULL;
454       ct->expression = get_type_string (ct->dest_type, ct->flags);
455       break;
456     case 'U':
457     case 'V':
458       /* Class/struct/union */
459       ct->dest_type = *iter++;
460       if (*iter == '0' || *iter == '1')
461       {
462         /* Referring to class type (implicit 'this') */
463         char *stripped;
464         if (!sym->argc)
465           return NULL;
466
467         iter++;
468         /* Apply our constraints to the base type (struct xxx *) */
469         stripped = strdup (sym->arg_text [0]);
470         if (!stripped)
471           fatal ("Out of Memory");
472
473         /* If we're a reference, re-use the pointer already in the type */
474         if (!(ct->flags & CT_BY_REFERENCE))
475           stripped[ strlen (stripped) - 2] = '\0'; /* otherwise, strip it */
476
477         ct->expression = str_create (2, ct->flags & CT_CONST ? "const " :
478                          ct->flags & CT_VOLATILE ? "volatile " : "", stripped);
479         free (stripped);
480       }
481       else if (*iter != '@')
482       {
483         /* The name of the class/struct, followed by '@@' */
484         char *struct_name = iter;
485         while (*iter && *iter++ != '@') ;
486         if (*iter++ != '@')
487           return NULL;
488         struct_name = str_substring (struct_name, iter - 2);
489         ct->expression = str_create (4, ct->flags & CT_CONST ? "const " :
490                          ct->flags & CT_VOLATILE ? "volatile " : "", "struct ",
491                          struct_name, ct->flags & CT_BY_REFERENCE ? " *" : "");
492         free (struct_name);
493       }
494       break;
495     case 'Q': /* FIXME: Array Just treated as pointer currently  */
496     case 'P': /* Pointer */
497       {
498         compound_type sub_ct;
499         INIT_CT (sub_ct);
500
501         ct->dest_type = *iter++;
502         if (!get_constraints_convention_2 (&iter, ct))
503           return NULL;
504
505         /* FIXME: P6 = Function pointer, others who knows.. */
506         if (isdigit (*iter))
507         {
508           if (*iter == '6')
509           {
510               int sub_expressions = 0;
511               /* FIXME: there are a tons of memory leaks here */
512               /* FIXME: this is still broken in some cases and it has to be
513                * merged with the function prototype parsing above...
514                */
515               iter += iter[1] == 'A' ? 2 : 3; /* FIXME */
516               if (!demangle_datatype (&iter, &sub_ct, sym))
517                   return NULL;
518               ct->expression = str_create(2, sub_ct.expression, " (*)(");
519               if (*iter != '@')
520               {
521                   while (*iter != 'Z')
522                   {
523                       FREE_CT (sub_ct);
524                       INIT_CT (sub_ct);
525                       if (!demangle_datatype (&iter, &sub_ct, sym))
526                           return NULL;
527                       if (sub_expressions)
528                               ct->expression = str_create(3, ct->expression, ", ", sub_ct.expression);
529                       else
530                               ct->expression = str_create(2, ct->expression, sub_ct.expression);
531                       while (*iter == '@') iter++;
532                       sub_expressions++;
533                   }
534               } else while (*iter == '@') iter++;
535               iter++;
536               ct->expression = str_create(2, ct->expression, ")");
537           }
538           else
539               return NULL;
540         }
541         else
542         {
543             /* Recurse to get the pointed-to type */
544             if (!demangle_datatype (&iter, &sub_ct, sym))
545                 return NULL;
546
547             ct->expression = get_pointer_type_string (ct, sub_ct.expression);
548         }
549
550         FREE_CT (sub_ct);
551       }
552       break;
553     case '0': case '1': case '2': case '3': case '4':
554     case '5': case '6': case '7': case '8': case '9':
555       /* Referring back to previously parsed type */
556       if (sym->argc >= (size_t)('0' - *iter))
557         return NULL;
558       ct->dest_type = sym->arg_type ['0' - *iter];
559       ct->expression = strdup (sym->arg_text ['0' - *iter]);
560       iter++;
561       break;
562     default :
563       return NULL;
564   }
565   if (!ct->expression)
566     return NULL;
567
568   return (char *)(*str = iter);
569 }
570
571
572 /* Constraints:
573  * There are two conventions for specifying data type constaints. I
574  * don't know how the compiler chooses between them, but I suspect it
575  * is based on ensuring that linker names are unique.
576  * Convention 1. The data type modifier is given first, followed
577  *   by the data type it operates on. '?' means passed by value,
578  *   'A' means passed by reference. Note neither of these characters
579  *   is a valid base data type. This is then followed by a character
580  *   specifying constness or volatilty.
581  * Convention 2. The base data type (which is never '?' or 'A') is
582  *   given first. The character modifier is optionally given after
583  *   the base type character. If a valid character mofifier is present,
584  *   then it only applies to the current data type if the character
585  *   after that is not 'A' 'B' or 'C' (Because this makes a convention 1
586  *   constraint for the next data type).
587  *
588  * The conventions are usually mixed within the same symbol.
589  * Since 'C' is both a qualifier and a data type, I suspect that
590  * convention 1 allows specifying e.g. 'volatile signed char*'. In
591  * convention 2 this would be 'CC' which is ambigious (i.e. Is it two
592  * pointers, or a single pointer + modifier?). In convention 1 it
593  * is encoded as '?CC' which is not ambigious. This probably
594  * holds true for some other types as well.
595  */
596
597 /*******************************************************************
598  *         get_constraints_convention_1
599  *
600  * Get type constraint information for a data type
601  */
602 static char *get_constraints_convention_1 (char **str, compound_type *ct)
603 {
604   char *iter = *str, **retval = str;
605
606   if (ct->have_qualifiers)
607     return (char *)*str; /* Previously got constraints for this type */
608
609   if (*iter == '?' || *iter == 'A')
610   {
611     ct->have_qualifiers = 1;
612     ct->flags |= (*iter++ == '?' ? 0 : CT_BY_REFERENCE);
613
614     switch (*iter++)
615     {
616     case 'A' :
617       break; /* non-const, non-volatile */
618     case 'B' :
619       ct->flags |= CT_CONST;
620       break;
621     case 'C' :
622       ct->flags |= CT_VOLATILE;
623       break;
624     default  :
625       return NULL;
626     }
627   }
628
629   return (char *)(*retval = iter);
630 }
631
632
633 /*******************************************************************
634  *         get_constraints_convention_2
635  *
636  * Get type constraint information for a data type
637  */
638 static char *get_constraints_convention_2 (char **str, compound_type *ct)
639 {
640   char *iter = *str, **retval = str;
641
642   /* FIXME: Why do arrays have both convention 1 & 2 constraints? */
643   if (ct->have_qualifiers && ct->dest_type != 'Q')
644     return (char *)*str; /* Previously got constraints for this type */
645
646   ct->have_qualifiers = 1; /* Even if none, we've got all we're getting */
647
648   switch (*iter)
649   {
650   case 'A' :
651     if (iter[1] != 'A' && iter[1] != 'B' && iter[1] != 'C')
652       iter++;
653     break;
654   case 'B' :
655     ct->flags |= CT_CONST;
656     iter++;
657     break;
658   case 'C' :
659     /* See note above, if we find 'C' it is _not_ a signed char */
660     ct->flags |= CT_VOLATILE;
661     iter++;
662     break;
663   }
664
665   return (char *)(*retval = iter);
666 }
667
668
669 /*******************************************************************
670  *         get_type_string
671  *
672  * Return a string containing the name of a data type
673  */
674 static char *get_type_string (const char c, const int constraints)
675 {
676   char *type_string;
677
678   if (constraints & CT_EXTENDED)
679   {
680     switch (c)
681     {
682     case 'D': type_string = "__int8"; break;
683     case 'E': type_string = "unsigned __int8"; break;
684     case 'F': type_string = "__int16"; break;
685     case 'G': type_string = "unsigned __int16"; break;
686     case 'H': type_string = "__int32"; break;
687     case 'I': type_string = "unsigned __int32"; break;
688     case 'J': type_string = "__int64"; break;
689     case 'K': type_string = "unsigned __int64"; break;
690     case 'L': type_string = "__int128"; break;
691     case 'M': type_string = "unsigned __int128"; break;
692     case 'N': type_string = "int"; break; /* bool */
693     case 'W': type_string = "WCHAR"; break; /* wchar_t */
694     default:
695       return NULL;
696    }
697   }
698   else
699   {
700     switch (c)
701     {
702     case 'C': /* Signed char, fall through */
703     case 'D': type_string = "char"; break;
704     case 'E': type_string = "unsigned char"; break;
705     case 'F': type_string = "short int"; break;
706     case 'G': type_string = "unsigned short int"; break;
707     case 'H': type_string = "int"; break;
708     case 'I': type_string = "unsigned int"; break;
709     case 'J': type_string = "long"; break;
710     case 'K': type_string = "unsigned long"; break;
711     case 'M': type_string = "float"; break;
712     case 'N': type_string = "double"; break;
713     case 'O': type_string = "long double"; break;
714     /* FIXME: T = union */
715     case 'U':
716     case 'V': type_string = "struct"; break;
717     case 'X': return strdup ("void");
718     case 'Z': return strdup ("...");
719     default:
720       return NULL;
721    }
722   }
723
724   return str_create (3, constraints & CT_CONST ? "const " :
725                      constraints & CT_VOLATILE ? "volatile " : "", type_string,
726                      constraints & CT_BY_REFERENCE ? " *" : "");
727 }
728
729
730 /*******************************************************************
731  *         get_type_constant
732  *
733  * Get the ARG_* constant for this data type
734  */
735 static int get_type_constant (const char c, const int constraints)
736 {
737   /* Any reference type is really a pointer */
738   if (constraints & CT_BY_REFERENCE)
739      return ARG_POINTER;
740
741   switch (c)
742   {
743   case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I':
744   case 'J': case 'K':
745     return ARG_LONG;
746   case 'M':
747     return ARG_FLOAT;
748   case 'N': case 'O':
749     return ARG_DOUBLE;
750   case 'P': case 'Q':
751     return ARG_POINTER;
752   case 'U': case 'V':
753     return ARG_STRUCT;
754   case 'X':
755     return ARG_VOID;
756   case 'Z':
757   default:
758     return -1;
759   }
760 }
761
762
763 /*******************************************************************
764  *         get_pointer_type_string
765  *
766  * Return a string containing 'pointer to expression'
767  */
768 static char *get_pointer_type_string (compound_type *ct,
769                                       const char *expression)
770 {
771   /* FIXME: set a compound flag for bracketing expression if needed */
772   return str_create (3, ct->flags & CT_CONST ? "const " :
773                      ct->flags & CT_VOLATILE ? "volatile " : "", expression,
774                      ct->flags & CT_BY_REFERENCE ? " **" : " *");
775
776 }