winecfg: New resource strings for appdefaults.
[wine] / tools / widl / typegen.c
1 /*
2  * Format String Generator for IDL Compiler
3  *
4  * Copyright 2005-2006 Eric Kohl
5  * Copyright 2005-2006 Robert Shearman
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #include <limits.h>
35
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "windef.h"
41 #include "wine/list.h"
42
43 #include "widl.h"
44 #include "typegen.h"
45
46 static const func_t *current_func;
47 static const type_t *current_structure;
48
49 /* name of the structure variable for structure callbacks */
50 #define STRUCT_EXPR_EVAL_VAR "pS"
51
52 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
53
54 struct expr_eval_routine
55 {
56     struct list entry;
57     const type_t *structure;
58     size_t structure_size;
59     const expr_t *expr;
60 };
61
62 static size_t type_memsize(const type_t *t, int ptr_level, const expr_t *array);
63 static size_t fields_memsize(const var_t *v);
64
65 static int compare_expr(const expr_t *a, const expr_t *b)
66 {
67     int ret;
68
69     if (a->type != b->type)
70         return a->type - b->type;
71
72     switch (a->type)
73     {
74         case EXPR_NUM:
75         case EXPR_HEXNUM:
76         case EXPR_TRUEFALSE:
77             return a->u.lval - b->u.lval;
78         case EXPR_IDENTIFIER:
79             return strcmp(a->u.sval, b->u.sval);
80         case EXPR_COND:
81             ret = compare_expr(a->ref, b->ref);
82             if (ret != 0)
83                 return ret;
84             ret = compare_expr(a->u.ext, b->u.ext);
85             if (ret != 0)
86                 return ret;
87             return compare_expr(a->ext2, b->ext2);
88         case EXPR_OR:
89         case EXPR_AND:
90         case EXPR_ADD:
91         case EXPR_SUB:
92         case EXPR_MUL:
93         case EXPR_DIV:
94         case EXPR_SHL:
95         case EXPR_SHR:
96             ret = compare_expr(a->ref, b->ref);
97             if (ret != 0)
98                 return ret;
99             return compare_expr(a->u.ext, b->u.ext);
100         case EXPR_NOT:
101         case EXPR_NEG:
102         case EXPR_PPTR:
103         case EXPR_CAST:
104         case EXPR_SIZEOF:
105             return compare_expr(a->ref, b->ref);
106         case EXPR_VOID:
107             return 0;
108     }
109     return -1;
110 }
111
112 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
113     do { \
114         if (file) \
115             fprintf(file, "/* %2u */\n", typestring_offset); \
116         print_file((file), 2, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
117     } \
118     while (0)
119
120 static int print_file(FILE *file, int indent, const char *format, ...)
121 {
122     va_list va;
123     int i, r;
124
125     if (!file) return 0;
126
127     va_start(va, format);
128     for (i = 0; i < indent; i++)
129         fprintf(file, "    ");
130     r = vfprintf(file, format, va);
131     va_end(va);
132     return r;
133 }
134
135 static inline int type_has_ref(const type_t *type)
136 {
137     return (type->type == 0 && type->ref);
138 }
139
140 static inline int is_base_type(unsigned char type)
141 {
142     switch (type)
143     {
144     case RPC_FC_BYTE:
145     case RPC_FC_CHAR:
146     case RPC_FC_USMALL:
147     case RPC_FC_SMALL:
148     case RPC_FC_WCHAR:
149     case RPC_FC_USHORT:
150     case RPC_FC_SHORT:
151     case RPC_FC_ULONG:
152     case RPC_FC_LONG:
153     case RPC_FC_HYPER:
154     case RPC_FC_IGNORE:
155     case RPC_FC_FLOAT:
156     case RPC_FC_DOUBLE:
157     case RPC_FC_ENUM16:
158     case RPC_FC_ENUM32:
159     case RPC_FC_ERROR_STATUS_T:
160     case RPC_FC_BIND_PRIMITIVE:
161         return TRUE;
162
163     default:
164         return FALSE;
165     }
166 }
167
168 unsigned char ref_type(const type_t *type)
169 {
170     while (type_has_ref(type))
171         type = type->ref;
172
173     return type->type;
174 }
175
176 static size_t write_procformatstring_var(FILE *file, int indent,
177     const var_t *var, int is_return, unsigned int *type_offset)
178 {
179     size_t size;
180     int ptr_level = var->ptr_level;
181     const type_t *type = var->type;
182
183     int is_in = is_attr(var->attrs, ATTR_IN);
184     int is_out = is_attr(var->attrs, ATTR_OUT);
185
186     if (!is_in && !is_out) is_in = TRUE;
187
188     if (ptr_level == 0 && type_has_ref(type))
189         type = type->ref;
190
191     if (ptr_level == 0 && !var->array && is_base_type(type->type))
192     {
193         if (is_return)
194             print_file(file, indent, "0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
195         else
196             print_file(file, indent, "0x4e,    /* FC_IN_PARAM_BASETYPE */\n");
197
198         switch(type->type)
199         {
200 #define CASE_BASETYPE(fctype) \
201         case RPC_##fctype: \
202             print_file(file, indent, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
203             size = 2; /* includes param type prefix */ \
204             break
205
206         CASE_BASETYPE(FC_BYTE);
207         CASE_BASETYPE(FC_CHAR);
208         CASE_BASETYPE(FC_WCHAR);
209         CASE_BASETYPE(FC_USHORT);
210         CASE_BASETYPE(FC_SHORT);
211         CASE_BASETYPE(FC_ULONG);
212         CASE_BASETYPE(FC_LONG);
213         CASE_BASETYPE(FC_HYPER);
214         CASE_BASETYPE(FC_IGNORE);
215         CASE_BASETYPE(FC_USMALL);
216         CASE_BASETYPE(FC_SMALL);
217         CASE_BASETYPE(FC_FLOAT);
218         CASE_BASETYPE(FC_DOUBLE);
219         CASE_BASETYPE(FC_ERROR_STATUS_T);
220 #undef CASE_BASETYPE
221
222         case RPC_FC_BIND_PRIMITIVE:
223             print_file(file, indent, "0x%02x,    /* FC_IGNORE */\n", RPC_FC_IGNORE);
224             size = 2; /* includes param type prefix */
225             break;
226
227         default:
228             error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
229             size = 0;
230         }
231     }
232     else
233     {
234         if (is_return)
235             print_file(file, indent, "0x52,    /* FC_RETURN_PARAM */\n");
236         else if (is_in && is_out)
237             print_file(file, indent, "0x50,    /* FC_IN_OUT_PARAM */\n");
238         else if (is_out)
239             print_file(file, indent, "0x51,    /* FC_OUT_PARAM */\n");
240         else
241             print_file(file, indent, "0x4d,    /* FC_IN_PARAM */\n");
242
243         print_file(file, indent, "0x01,\n");
244         print_file(file, indent, "NdrFcShort(0x%x),\n", *type_offset);
245         size = 4; /* includes param type prefix */
246     }
247     *type_offset += get_size_typeformatstring_var(var);
248     return size;
249 }
250
251 void write_procformatstring(FILE *file, const ifref_t *ifaces)
252 {
253     const ifref_t *iface = ifaces;
254     int indent = 0;
255     var_t *var;
256     unsigned int type_offset = 2;
257
258     print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
259     print_file(file, indent, "{\n");
260     indent++;
261     print_file(file, indent, "0,\n");
262     print_file(file, indent, "{\n");
263     indent++;
264
265     END_OF_LIST(iface);
266
267     for (; iface; iface = PREV_LINK(iface))
268     {
269         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
270             continue;
271
272         if (iface->iface->funcs)
273         {
274             func_t *func = iface->iface->funcs;
275             while (NEXT_LINK(func)) func = NEXT_LINK(func);
276             for (; func; func = PREV_LINK(func))
277             {
278                 /* emit argument data */
279                 if (func->args)
280                 {
281                     var = func->args;
282                     while (NEXT_LINK(var)) var = NEXT_LINK(var);
283                     while (var)
284                     {
285                         write_procformatstring_var(file, indent, var, FALSE,
286                                                    &type_offset);
287
288                         var = PREV_LINK(var);
289                     }
290                 }
291
292                 /* emit return value data */
293                 var = func->def;
294                 if (is_void(var->type, NULL))
295                 {
296                     print_file(file, indent, "0x5b,    /* FC_END */\n");
297                     print_file(file, indent, "0x5c,    /* FC_PAD */\n");
298                 }
299                 else
300                     write_procformatstring_var(file, indent, var, TRUE,
301                                                &type_offset);
302             }
303         }
304     }
305
306     print_file(file, indent, "0x0\n");
307     indent--;
308     print_file(file, indent, "}\n");
309     indent--;
310     print_file(file, indent, "};\n");
311     print_file(file, indent, "\n");
312 }
313
314 /* write conformance / variance descriptor */
315 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure, const expr_t *expr)
316 {
317     unsigned char operator_type = 0;
318     const char *operator_string = "no operators";
319     const expr_t *subexpr = expr;
320     unsigned char correlation_type;
321
322     if (!file) return 4; /* optimisation for sizing pass */
323
324     if (expr->is_const)
325     {
326         if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
327             error("write_conf_or_var_desc: constant value %ld is greater than "
328                   "the maximum constant size of %d\n", expr->cval,
329                   UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
330
331         print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
332                    RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
333         print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
334         print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
335
336         return 4;
337     }
338
339     switch (subexpr->type)
340     {
341     case EXPR_PPTR:
342         subexpr = subexpr->ref;
343         operator_type = RPC_FC_DEREFERENCE;
344         operator_string = "FC_DEREFERENCE";
345         break;
346     case EXPR_DIV:
347         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
348         {
349             subexpr = subexpr->ref;
350             operator_type = RPC_FC_DIV_2;
351             operator_string = "FC_DIV_2";
352         }
353         break;
354     case EXPR_MUL:
355         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
356         {
357             subexpr = subexpr->ref;
358             operator_type = RPC_FC_MULT_2;
359             operator_string = "FC_MULT_2";
360         }
361         break;
362     case EXPR_SUB:
363         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
364         {
365             subexpr = subexpr->ref;
366             operator_type = RPC_FC_SUB_1;
367             operator_string = "FC_SUB_1";
368         }
369         break;
370     case EXPR_ADD:
371         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
372         {
373             subexpr = subexpr->ref;
374             operator_type = RPC_FC_ADD_1;
375             operator_string = "FC_ADD_1";
376         }
377         break;
378     default:
379         break;
380     }
381
382     if (subexpr->type == EXPR_IDENTIFIER)
383     {
384         const type_t *correlation_variable = NULL;
385         unsigned char correlation_variable_type;
386         unsigned char param_type = 0;
387         const char *param_type_string = NULL;
388         size_t offset;
389
390         if (structure)
391         {
392             const var_t *var;
393
394             for (offset = 0, var = structure->fields; var; var = NEXT_LINK(var))
395             {
396                 offset -= type_memsize(var->type, var->ptr_level, var->array);
397                 if (!strcmp(var->name, subexpr->u.sval))
398                 {
399                     correlation_variable = var->type;
400                     break;
401                 }
402             }
403             if (!correlation_variable)
404                 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
405                       subexpr->u.sval);
406
407             correlation_type = RPC_FC_NORMAL_CONFORMANCE;
408         }
409         else
410         {
411             const var_t *var = func->args;
412
413             while (NEXT_LINK(var)) var = NEXT_LINK(var);
414             /* FIXME: not all stack variables are sizeof(void *) */
415             for (offset = 0; var; offset += sizeof(void *), var = PREV_LINK(var))
416             {
417                 if (!strcmp(var->name, subexpr->u.sval))
418                 {
419                     correlation_variable = var->type;
420                     break;
421                 }
422             }
423             if (!correlation_variable)
424                 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
425                     subexpr->u.sval);
426
427             correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
428         }
429
430         correlation_variable_type = ref_type(correlation_variable);
431
432         switch (correlation_variable_type)
433         {
434         case RPC_FC_CHAR:
435         case RPC_FC_SMALL:
436             param_type = RPC_FC_SMALL;
437             param_type_string = "FC_SMALL";
438             break;
439         case RPC_FC_BYTE:
440         case RPC_FC_USMALL:
441             param_type = RPC_FC_USMALL;
442             param_type_string = "FC_USMALL";
443             break;
444         case RPC_FC_WCHAR:
445         case RPC_FC_SHORT:
446             param_type = RPC_FC_SHORT;
447             param_type_string = "FC_SHORT";
448             break;
449         case RPC_FC_USHORT:
450             param_type = RPC_FC_USHORT;
451             param_type_string = "FC_USHORT";
452             break;
453         case RPC_FC_LONG:
454             param_type = RPC_FC_LONG;
455             param_type_string = "FC_LONG";
456             break;
457         case RPC_FC_ULONG:
458             param_type = RPC_FC_ULONG;
459             param_type_string = "FC_ULONG";
460             break;
461         default:
462             error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
463                 correlation_variable_type);
464         }
465
466         print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
467                 correlation_type | param_type,
468                 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
469                 param_type_string);
470         print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
471         print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
472                    offset,
473                    correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
474                    offset);
475     }
476     else
477     {
478         unsigned int callback_offset = 0;
479
480         if (structure)
481         {
482             struct expr_eval_routine *eval;
483             int found = 0;
484
485             LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
486             {
487                 if (!strcmp(eval->structure->name, structure->name) &&
488                     !compare_expr(eval->expr, expr))
489                 {
490                     found = 1;
491                     break;
492                 }
493                 callback_offset++;
494             }
495
496             if (!found)
497             {
498                 eval = xmalloc(sizeof(*eval));
499                 eval->structure = structure;
500                 eval->structure_size = fields_memsize(structure->fields);
501                 eval->expr = expr;
502                 list_add_tail(&expr_eval_routines, &eval->entry);
503             }
504
505             correlation_type = RPC_FC_NORMAL_CONFORMANCE;
506         }
507         else
508         {
509             error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
510             correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
511         }
512
513         if (callback_offset > USHRT_MAX)
514             error("Maximum number of callback routines reached\n");
515
516         print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
517                    correlation_type,
518                    correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
519         print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
520         print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
521     }
522     return 4;
523 }
524
525 static size_t fields_memsize(const var_t *v)
526 {
527     size_t size = 0;
528     const var_t *first = v;
529     if (!v) return 0;
530     while (NEXT_LINK(v)) v = NEXT_LINK(v);
531     while (v) {
532         size += type_memsize(v->type, v->ptr_level, v->array);
533         if (v == first) break;
534         v = PREV_LINK(v);
535     }
536     return size;
537 }
538
539 static size_t type_memsize(const type_t *t, int ptr_level, const expr_t *array)
540 {
541     size_t size = 0;
542
543     if (ptr_level)
544         return sizeof(void *);
545
546     if (type_has_ref(t))
547         return type_memsize(t->ref, 0 /* FIXME */, NULL);
548
549     switch (t->type)
550     {
551     case RPC_FC_BYTE:
552     case RPC_FC_CHAR:
553     case RPC_FC_USMALL:
554     case RPC_FC_SMALL:
555         size = 1;
556         break;
557     case RPC_FC_WCHAR:
558     case RPC_FC_USHORT:
559     case RPC_FC_SHORT:
560     case RPC_FC_ENUM16:
561         size = 2;
562         break;
563     case RPC_FC_ULONG:
564     case RPC_FC_LONG:
565     case RPC_FC_ERROR_STATUS_T:
566     case RPC_FC_ENUM32:
567     case RPC_FC_FLOAT:
568         size = 4;
569         break;
570     case RPC_FC_HYPER:
571     case RPC_FC_DOUBLE:
572         size = 8;
573         break;
574     case RPC_FC_STRUCT:
575     case RPC_FC_CVSTRUCT:
576     case RPC_FC_CPSTRUCT:
577     case RPC_FC_CSTRUCT:
578     case RPC_FC_PSTRUCT:
579     case RPC_FC_BOGUS_STRUCT:
580     case RPC_FC_ENCAPSULATED_UNION:
581     case RPC_FC_NON_ENCAPSULATED_UNION:
582         size = fields_memsize(t->fields);
583         break;
584     default:
585         error("type_memsize: Unknown type %d\n", t->type);
586         size = 0;
587     }
588
589     if (array)
590     {
591         if (array->is_const)
592             size *= array->cval;
593         else
594             size = 0;
595     }
596
597     return size;
598 }
599
600 size_t get_type_memsize(const type_t *type)
601 {
602     return type_memsize(type, 0, NULL);
603 }
604
605 static int write_pointers(FILE *file, const attr_t *attrs,
606                           const type_t *type, int ptr_level,
607                           const expr_t *array, int level,
608                           unsigned int *typestring_offset)
609 {
610     int pointers_written = 0;
611     const var_t *v;
612
613     /* don't generate a pointer for first-level arrays since we want to
614     * descend into them to write their pointers, not stop here */
615     if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
616     {
617         return write_pointers(file, NULL, type, 0, NULL, level + 1, typestring_offset);
618     }
619
620     if (ptr_level != 0)
621     {
622         /* FIXME: only general algorithm implemented, not the actual writing */
623         error("write_pointers: Writing type format string for pointer is unimplemented\n");
624         return 1;
625     }
626
627     /* FIXME: search through all refs for pointers too */
628     while(type_has_ref(type))
629         type = type->ref;
630
631     switch (type->type)
632     {
633         /* note: don't descend into complex structures or unions since these
634          * will always be generated as a separate type */
635         case RPC_FC_STRUCT:
636         case RPC_FC_CVSTRUCT:
637         case RPC_FC_CPSTRUCT:
638         case RPC_FC_CSTRUCT:
639         case RPC_FC_PSTRUCT:
640             v = type->fields;
641             if (!v) break;
642             while (NEXT_LINK(v)) v = NEXT_LINK(v);
643             for (; v; v = PREV_LINK(v))
644                 pointers_written += write_pointers(file, v->attrs, v->type,
645                                                    v->ptr_level, v->array,
646                                                    level + 1,
647                                                    typestring_offset);
648
649             break;
650
651         default:
652             /* nothing to do */
653             break;
654     }
655
656     return pointers_written;
657 }
658
659 static size_t write_pointer_description(FILE *file, const attr_t *attrs,
660                                         const type_t *type, int ptr_level,
661                                         const expr_t *array, int level,
662                                         size_t typestring_offset)
663 {
664     size_t size = 0;
665     const var_t *v;
666
667     /* don't generate a pointer for first-level arrays since we want to
668      * descend into them to write their pointers, not stop here */
669     if ((level == 0 || ptr_level == 0) && is_array_type(attrs, ptr_level, array))
670     {
671         return write_pointer_description(file, NULL, type, 0, NULL,
672                                          level + 1, typestring_offset);
673     }
674
675     if (ptr_level != 0)
676     {
677         /* FIXME: only general algorithm implemented, not the actual writing */
678         error("write_pointer_description: Writing pointer description is unimplemented\n");
679         return 0;
680     }
681
682     /* FIXME: search through all refs for pointers too */
683
684     switch (type->type)
685     {
686         /* note: don't descend into complex structures or unions since these
687          * will always be generated as a separate type */
688         case RPC_FC_STRUCT:
689         case RPC_FC_CVSTRUCT:
690         case RPC_FC_CPSTRUCT:
691         case RPC_FC_CSTRUCT:
692         case RPC_FC_PSTRUCT:
693             v = type->fields;
694             if (!v) break;
695             while (NEXT_LINK(v)) v = NEXT_LINK(v);
696             for (; v; v = PREV_LINK(v))
697                 size += write_pointer_description(file, v->attrs, v->type,
698                                                   v->ptr_level, v->array,
699                                                   level + 1,
700                                                   typestring_offset);
701
702             break;
703
704         default:
705             /* nothing to do */
706             break;
707     }
708
709     return size;
710 }
711
712 static size_t write_string_tfs(FILE *file, const attr_t *attrs,
713                                const type_t *type, const expr_t *array,
714                                const char *name, unsigned int *typestring_offset)
715 {
716     const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
717     int has_size = size_is && (size_is->type != EXPR_VOID);
718     size_t start_offset = *typestring_offset;
719     unsigned char flags = 0;
720     int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
721     unsigned char rtype;
722
723     if (!pointer_type)
724         pointer_type = RPC_FC_RP;
725
726     if (!get_attrp(attrs, ATTR_SIZEIS))
727         flags |= RPC_FC_P_SIMPLEPOINTER;
728
729     rtype = ref_type(type);
730
731     if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
732     {
733         error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
734         return start_offset;
735     }
736
737     print_file(file, 2,"0x%x, 0x%x,    /* %s%s */\n",
738                pointer_type, flags,
739                pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
740                (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
741     *typestring_offset += 2;
742
743     if (!(flags & RPC_FC_P_SIMPLEPOINTER))
744     {
745         print_file(file, 2, "NdrFcShort(0x2),\n");
746         *typestring_offset += 2;
747     }
748
749     if (array && array->is_const)
750     {
751         if (array->cval > USHRT_MAX)
752             error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
753                   name, USHRT_MAX, array->cval - USHRT_MAX);
754
755         if (rtype == RPC_FC_CHAR)
756             WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
757         else
758             WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
759         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
760         *typestring_offset += 2;
761
762         print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", array->cval, array->cval);
763         *typestring_offset += 2;
764
765         return start_offset;
766     }
767     else if (has_size)
768     {
769         if (rtype == RPC_FC_CHAR)
770             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
771         else
772             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
773         print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
774         *typestring_offset += 2;
775
776         *typestring_offset += write_conf_or_var_desc(file, current_func, NULL, size_is);
777
778         return start_offset;
779     }
780     else
781     {
782         if (rtype == RPC_FC_CHAR)
783             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
784         else
785             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
786         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
787         *typestring_offset += 2;
788
789         return start_offset;
790     }
791 }
792
793 static size_t write_array_tfs(FILE *file, const attr_t *attrs,
794                               const type_t *type, const expr_t *array,
795                               const char *name, unsigned int *typestring_offset)
796 {
797     const expr_t *length_is = get_attrp(attrs, ATTR_LENGTHIS);
798     const expr_t *size_is = get_attrp(attrs, ATTR_SIZEIS);
799     int has_length = length_is && (length_is->type != EXPR_VOID);
800     int has_size = (size_is && (size_is->type != EXPR_VOID)) || !array->is_const;
801     size_t start_offset;
802     int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
803     if (!pointer_type)
804         pointer_type = RPC_FC_RP;
805
806     print_file(file, 2, "0x%x, 0x00,    /* %s */\n",
807                pointer_type,
808                pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"));
809     print_file(file, 2, "NdrFcShort(0x2),\n");
810     *typestring_offset += 4;
811
812     if (array && NEXT_LINK(array)) /* multi-dimensional array */
813     {
814         error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name);
815         return 0;
816     }
817     else
818     {
819         size_t pointer_start_offset = *typestring_offset;
820         int has_pointer = 0;
821
822         if (write_pointers(file, attrs, type, 0, array, 0, typestring_offset) > 0)
823             has_pointer = 1;
824
825         start_offset = *typestring_offset;
826
827         if (!has_length && !has_size)
828         {
829             /* fixed array */
830             size_t size = type_memsize(type, 0, array);
831             if (size < USHRT_MAX)
832             {
833                 WRITE_FCTYPE(file, FC_SMFARRAY, *typestring_offset);
834                 /* alignment */
835                 print_file(file, 2, "0x%x, /* 0 */\n", 0);
836                 /* size */
837                 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", size, size);
838                 *typestring_offset += 4;
839             }
840             else
841             {
842                 WRITE_FCTYPE(file, FC_LGFARRAY, *typestring_offset);
843                 /* alignment */
844                 print_file(file, 2, "0x%x, /* 0 */\n", 0);
845                 /* size */
846                 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", size, size);
847                 *typestring_offset += 6;
848             }
849
850             if (has_pointer)
851             {
852                 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
853                 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
854                 *typestring_offset += 2;
855                 *typestring_offset = write_pointer_description(file, attrs,
856                     type, 0, array, 0, pointer_start_offset);
857                 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
858                 *typestring_offset += 1;
859             }
860
861             print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
862             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
863             *typestring_offset += 2;
864
865             return start_offset;
866         }
867         else if (has_length && !has_size)
868         {
869             /* varying array */
870             size_t element_size = type_memsize(type, 0, NULL);
871             size_t elements = array->cval;
872             size_t total_size = element_size * elements;
873
874             if (total_size < USHRT_MAX)
875             {
876                 WRITE_FCTYPE(file, FC_SMVARRAY, *typestring_offset);
877                 /* alignment */
878                 print_file(file, 2, "0x%x, /* 0 */\n", 0);
879                 /* total size */
880                 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", total_size, total_size);
881                 /* number of elements */
882                 print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", elements, elements);
883                 *typestring_offset += 6;
884             }
885             else
886             {
887                 WRITE_FCTYPE(file, FC_LGVARRAY, *typestring_offset);
888                 /* alignment */
889                 print_file(file, 2, "0x%x, /* 0 */\n", 0);
890                 /* total size */
891                 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", total_size, total_size);
892                 /* number of elements */
893                 print_file(file, 2, "NdrFcLong(0x%x), /* %d */\n", elements, elements);
894                 *typestring_offset += 10;
895             }
896             /* element size */
897             print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
898             *typestring_offset += 2;
899
900             *typestring_offset += write_conf_or_var_desc(file, current_func,
901                                                          current_structure,
902                                                          length_is);
903
904             if (has_pointer)
905             {
906                 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
907                 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
908                 *typestring_offset += 2;
909                 *typestring_offset += write_pointer_description(file, attrs,
910                     type, 0, array, 0, pointer_start_offset);
911                 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
912                 *typestring_offset += 1;
913             }
914
915             print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
916             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
917             *typestring_offset += 2;
918
919             return start_offset;
920         }
921         else if (!has_length && has_size)
922         {
923             /* conformant array */
924             size_t element_size = type_memsize(type, 0, NULL);
925
926             WRITE_FCTYPE(file, FC_CARRAY, *typestring_offset);
927             /* alignment */
928             print_file(file, 2, "0x%x, /* 0 */\n", 0);
929             /* element size */
930             print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
931             *typestring_offset += 4;
932
933             *typestring_offset += write_conf_or_var_desc(file, current_func,
934                                                          current_structure,
935                                                          size_is ? size_is : array);
936
937             if (has_pointer)
938             {
939                 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
940                 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
941                 *typestring_offset += 2;
942                 *typestring_offset += write_pointer_description(file, attrs,
943                     type, 0, array, 0, pointer_start_offset);
944                 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
945                 *typestring_offset += 1;
946             }
947
948             print_file(file, 2, "0x%x, /* FIXME: write out conversion data */\n", type->type);
949             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
950             *typestring_offset += 2;
951
952             return start_offset;
953         }
954         else
955         {
956             /* conformant varying array */
957             size_t element_size = type_memsize(type, 0, NULL);
958
959             WRITE_FCTYPE(file, FC_CVARRAY, *typestring_offset);
960             /* alignment */
961             print_file(file, 2, "0x%x, /* 0 */\n", 0);
962             /* element size */
963             print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", element_size, element_size);
964             *typestring_offset += 4;
965
966             *typestring_offset += write_conf_or_var_desc(file, current_func,
967                                                          current_structure,
968                                                          size_is ? size_is : array);
969             *typestring_offset += write_conf_or_var_desc(file, current_func,
970                                                          current_structure,
971                                                          length_is);
972
973             if (has_pointer)
974             {
975                 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
976                 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
977                 *typestring_offset += 2;
978                 *typestring_offset += write_pointer_description(file, attrs,
979                     type, 0, array, 0, pointer_start_offset);
980                 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
981                 *typestring_offset += 1;
982             }
983
984             print_file(file, 2, "0x0, /* FIXME: write out conversion data */\n");
985             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
986             *typestring_offset += 2;
987
988             return start_offset;
989         }
990     }
991 }
992
993 static const var_t *find_array_or_string_in_struct(const type_t *type)
994 {
995     /* last field is the first in the fields linked list */
996     const var_t *last_field = type->fields;
997     if (is_array_type(last_field->attrs, last_field->ptr_level, last_field->array))
998         return last_field;
999
1000     assert((last_field->type->type == RPC_FC_CSTRUCT) ||
1001            (last_field->type->type == RPC_FC_CPSTRUCT) ||
1002            (last_field->type->type == RPC_FC_CVSTRUCT));
1003
1004     return find_array_or_string_in_struct(last_field->type);
1005 }
1006
1007 static size_t write_struct_members(FILE *file, const type_t *type)
1008 {
1009     size_t typestring_size = 0;
1010     var_t *field;
1011
1012     field = type->fields;
1013     while (NEXT_LINK(field)) field = NEXT_LINK(field);
1014     for (; field; field = PREV_LINK(field))
1015     {
1016         unsigned char rtype = ref_type(field->type);
1017
1018         if (is_base_type(rtype))
1019         {
1020             switch (rtype)
1021             {
1022 #define CASE_BASETYPE(fctype) \
1023             case RPC_##fctype: \
1024                 print_file(file, 2, "0x%02x,\t\t/* " #fctype " */\n", RPC_##fctype); \
1025                 typestring_size++; \
1026                 break;
1027             CASE_BASETYPE(FC_BYTE);
1028             CASE_BASETYPE(FC_CHAR);
1029             CASE_BASETYPE(FC_SMALL);
1030             CASE_BASETYPE(FC_USMALL);
1031             CASE_BASETYPE(FC_WCHAR);
1032             CASE_BASETYPE(FC_SHORT);
1033             CASE_BASETYPE(FC_USHORT);
1034             CASE_BASETYPE(FC_LONG);
1035             CASE_BASETYPE(FC_ULONG);
1036             CASE_BASETYPE(FC_FLOAT);
1037             CASE_BASETYPE(FC_HYPER);
1038             CASE_BASETYPE(FC_DOUBLE);
1039             CASE_BASETYPE(FC_ENUM16);
1040             CASE_BASETYPE(FC_ENUM32);
1041             CASE_BASETYPE(FC_IGNORE);
1042             CASE_BASETYPE(FC_ERROR_STATUS_T);
1043             default:
1044                 break;
1045 #undef CASE_BASETYPE
1046             }
1047         }
1048         else
1049             error("Unsupported member type 0x%x\n", rtype);
1050     }
1051
1052     if (typestring_size % 1)
1053     {
1054         print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1055         typestring_size++;
1056     }
1057
1058     print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1059     typestring_size++;
1060
1061     return typestring_size;
1062 }
1063
1064 static size_t write_struct_tfs(FILE *file, const type_t *type,
1065                                const char *name, unsigned int *typestring_offset)
1066 {
1067     unsigned int total_size;
1068     const var_t *array;
1069     size_t start_offset;
1070     size_t array_offset;
1071     size_t pointer_offset;
1072
1073     switch (type->type)
1074     {
1075     case RPC_FC_STRUCT:
1076     case RPC_FC_PSTRUCT:
1077         total_size = type_memsize(type, 0, NULL);
1078
1079         if (total_size > USHRT_MAX)
1080             error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1081                   name, USHRT_MAX, total_size - USHRT_MAX);
1082
1083         if (type->type == RPC_FC_PSTRUCT)
1084         {
1085             pointer_offset = *typestring_offset;
1086             write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1087         }
1088         else pointer_offset = 0; /* silence warning */
1089
1090         start_offset = *typestring_offset;
1091         if (type->type == RPC_FC_STRUCT)
1092             WRITE_FCTYPE(file, FC_STRUCT, *typestring_offset);
1093         else
1094             WRITE_FCTYPE(file, FC_PSTRUCT, *typestring_offset);
1095         /* alignment */
1096         print_file(file, 2, "0x3,\n"); /* FIXME */
1097         /* total size */
1098         print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1099         *typestring_offset += 4;
1100
1101         if (type->type == RPC_FC_PSTRUCT)
1102         {
1103             print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1104             print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1105             *typestring_offset += 2;
1106             *typestring_offset += write_pointer_description(file, NULL,
1107                 type, 0, NULL, 0, pointer_offset);
1108             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1109             *typestring_offset += 1;
1110         }
1111
1112         /* member layout */
1113         *typestring_offset += write_struct_members(file, type);
1114         return start_offset;
1115     case RPC_FC_CSTRUCT:
1116     case RPC_FC_CPSTRUCT:
1117         total_size = type_memsize(type, 0, NULL);
1118
1119         if (total_size > USHRT_MAX)
1120             error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1121                   name, USHRT_MAX, total_size - USHRT_MAX);
1122
1123         array = find_array_or_string_in_struct(type);
1124         current_structure = type;
1125         array_offset = write_array_tfs(file, array->attrs, array->type,
1126                                        array->array, array->name,
1127                                        typestring_offset);
1128         current_structure = NULL;
1129
1130         if (type->type == RPC_FC_CPSTRUCT)
1131         {
1132             pointer_offset = *typestring_offset;
1133             write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset);
1134         }
1135         else pointer_offset = 0; /* silence warning */
1136
1137         start_offset = *typestring_offset;
1138         if (type->type == RPC_FC_CSTRUCT)
1139             WRITE_FCTYPE(file, FC_CSTRUCT, *typestring_offset);
1140         else
1141             WRITE_FCTYPE(file, FC_CPSTRUCT, *typestring_offset);
1142         /* alignment */
1143         print_file(file, 2, "0x0,\n");
1144         /* total size */
1145         print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1146         *typestring_offset += 4;
1147         print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1148                    array_offset - *typestring_offset,
1149                    array_offset - *typestring_offset,
1150                    array_offset);
1151         *typestring_offset += 2;
1152
1153         if (type->type == RPC_FC_CPSTRUCT)
1154         {
1155             print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1156             print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1157             *typestring_offset += 2;
1158             *typestring_offset += write_pointer_description(file, NULL,
1159                 type, 0, NULL, 0, pointer_offset);
1160             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1161             *typestring_offset += 1;
1162         }
1163
1164         print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1165         *typestring_offset += 1;
1166
1167         return start_offset;
1168     case RPC_FC_CVSTRUCT:
1169         total_size = type_memsize(type, 0, NULL);
1170
1171         if (total_size > USHRT_MAX)
1172             error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1173                   name, USHRT_MAX, total_size - USHRT_MAX);
1174
1175         array = find_array_or_string_in_struct(type);
1176         current_structure = type;
1177         if (is_attr(array->attrs, ATTR_STRING))
1178             array_offset = write_string_tfs(file, array->attrs, array->type,
1179                                             array->array, array->name,
1180                                             typestring_offset);
1181         else
1182             array_offset = write_array_tfs(file, array->attrs, array->type,
1183                                            array->array, array->name,
1184                                            typestring_offset);
1185         current_structure = NULL;
1186
1187         pointer_offset = *typestring_offset;
1188         if (!write_pointers(file, NULL, type, 0, NULL, 0, typestring_offset))
1189             pointer_offset = 0;
1190
1191         start_offset = *typestring_offset;
1192         WRITE_FCTYPE(file, FC_CVSTRUCT, *typestring_offset);
1193         /* alignment */
1194         print_file(file, 2, "0x0,\n");
1195         /* total size */
1196         print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", total_size, total_size);
1197         *typestring_offset += 4;
1198         print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1199                    array_offset - *typestring_offset,
1200                    array_offset - *typestring_offset,
1201                    array_offset);
1202         *typestring_offset += 2;
1203
1204         if (pointer_offset != 0)
1205         {
1206             print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1207             print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1208             *typestring_offset += 2;
1209             *typestring_offset += write_pointer_description(file, NULL,
1210                 type, 0, NULL, 0, pointer_offset);
1211             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1212             *typestring_offset += 1;
1213         }
1214
1215         print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1216         *typestring_offset += 1;
1217
1218         return start_offset;
1219     default:
1220         error("write_struct_tfs: Unimplemented for type 0x%x\n", type->type);
1221         return *typestring_offset;
1222     }
1223 }
1224
1225 static void write_pointer_only_tfs(FILE *file, const attr_t *attrs, size_t offset, unsigned int *typeformat_offset)
1226 {
1227     int in_attr, out_attr;
1228     unsigned char flags = 0;
1229     int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1230     if (!pointer_type) pointer_type = RPC_FC_RP;
1231     in_attr = is_attr(attrs, ATTR_IN);
1232     out_attr = is_attr(attrs, ATTR_OUT);
1233     if (!in_attr && !out_attr) in_attr = 1;
1234
1235     if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1236         flags |= 0x04;
1237
1238     print_file(file, 2, "0x%x, 0x%x,\t\t/* %s%s */\n",
1239                pointer_type,
1240                flags,
1241                pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
1242                (flags & 0x04) ? " [allocated_on_stack]" : "");
1243     print_file(file, 2, "NdrFcShort(0x%x),    /* %d */\n", offset, offset);
1244     *typeformat_offset += 4;
1245 }
1246
1247 static size_t write_union_tfs(FILE *file, const attr_t *attrs,
1248                               const type_t *type, const char *name,
1249                               unsigned int *typeformat_offset)
1250 {
1251     error("write_union_tfs: Unimplemented\n");
1252     return *typeformat_offset;
1253 }
1254
1255 static size_t write_typeformatstring_var(FILE *file, int indent,
1256                                          const var_t *var, unsigned int *typeformat_offset)
1257 {
1258     const type_t *type = var->type;
1259     int ptr_level = var->ptr_level;
1260
1261     chat("write_typeformatstring_var: %s\n", var->name);
1262
1263     while (TRUE)
1264     {
1265         chat("write_typeformatstring: type->type = 0x%x, type->name = %s, ptr_level = %d\n", type->type, type->name, ptr_level);
1266
1267         if (is_string_type(var->attrs, ptr_level, var->array))
1268             return write_string_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1269
1270         if (is_array_type(var->attrs, ptr_level, var->array))
1271             return write_array_tfs(file, var->attrs, type, var->array, var->name, typeformat_offset);
1272
1273         if (ptr_level == 0)
1274         {
1275             /* follow reference if the type has one */
1276             if (type_has_ref(type))
1277             {
1278                 type = type->ref;
1279                 /* FIXME: get new ptr_level from type */
1280                 continue;
1281             }
1282
1283             /* basic types don't need a type format string */
1284             if (is_base_type(type->type))
1285                 return 0;
1286
1287             switch (type->type)
1288             {
1289             case RPC_FC_STRUCT:
1290             case RPC_FC_PSTRUCT:
1291             case RPC_FC_CSTRUCT:
1292             case RPC_FC_CPSTRUCT:
1293             case RPC_FC_CVSTRUCT:
1294             case RPC_FC_BOGUS_STRUCT:
1295                 return write_struct_tfs(file, type, var->name, typeformat_offset);
1296             case RPC_FC_ENCAPSULATED_UNION:
1297             case RPC_FC_NON_ENCAPSULATED_UNION:
1298                 return write_union_tfs(file, var->attrs, type, var->name, typeformat_offset);
1299             case RPC_FC_IGNORE:
1300             case RPC_FC_BIND_PRIMITIVE:
1301                 /* nothing to do */
1302                 return 0;
1303             default:
1304                 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1305             }
1306         }
1307         else if (ptr_level == 1 && !type_has_ref(type))
1308         {
1309             size_t start_offset = *typeformat_offset;
1310             int in_attr = is_attr(var->attrs, ATTR_IN);
1311             int out_attr = is_attr(var->attrs, ATTR_OUT);
1312             int pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1313             if (!pointer_type) pointer_type = RPC_FC_RP;
1314
1315             /* special case for pointers to base types */
1316             switch (type->type)
1317             {
1318 #define CASE_BASETYPE(fctype) \
1319             case RPC_##fctype: \
1320                 print_file(file, indent, "0x%x, 0x%x,    /* %s %s[simple_pointer] */\n", \
1321                            pointer_type, \
1322                            (!in_attr && out_attr) ? 0x0C : 0x08, \
1323                            pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"), \
1324                            (!in_attr && out_attr) ? "[allocated_on_stack] " : ""); \
1325                 print_file(file, indent, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
1326                 print_file(file, indent, "0x5c,          /* FC_PAD */\n"); \
1327                 *typeformat_offset += 4; \
1328                 return start_offset
1329             CASE_BASETYPE(FC_BYTE);
1330             CASE_BASETYPE(FC_CHAR);
1331             CASE_BASETYPE(FC_SMALL);
1332             CASE_BASETYPE(FC_USMALL);
1333             CASE_BASETYPE(FC_WCHAR);
1334             CASE_BASETYPE(FC_SHORT);
1335             CASE_BASETYPE(FC_USHORT);
1336             CASE_BASETYPE(FC_LONG);
1337             CASE_BASETYPE(FC_ULONG);
1338             CASE_BASETYPE(FC_FLOAT);
1339             CASE_BASETYPE(FC_HYPER);
1340             CASE_BASETYPE(FC_DOUBLE);
1341             CASE_BASETYPE(FC_ENUM16);
1342             CASE_BASETYPE(FC_ENUM32);
1343             CASE_BASETYPE(FC_IGNORE);
1344             CASE_BASETYPE(FC_ERROR_STATUS_T);
1345             default:
1346                 break;
1347             }
1348         }
1349
1350         assert(ptr_level > 0);
1351
1352         if (file)
1353             fprintf(file, "/* %2u */\n", *typeformat_offset);
1354         write_pointer_only_tfs(file, var->attrs, 2, typeformat_offset);
1355
1356         ptr_level--;
1357     }
1358 }
1359
1360
1361 void write_typeformatstring(FILE *file, const ifref_t *ifaces)
1362 {
1363     int indent = 0;
1364     var_t *var;
1365     unsigned int typeformat_offset;
1366     const ifref_t *iface = ifaces;
1367
1368     print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1369     print_file(file, indent, "{\n");
1370     indent++;
1371     print_file(file, indent, "0,\n");
1372     print_file(file, indent, "{\n");
1373     indent++;
1374     print_file(file, indent, "NdrFcShort(0x0),\n");
1375     typeformat_offset = 2;
1376
1377     END_OF_LIST(iface);
1378
1379     for (; iface; iface = PREV_LINK(iface))
1380     {
1381         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1382             continue;
1383
1384         if (iface->iface->funcs)
1385         {
1386             func_t *func = iface->iface->funcs;
1387             while (NEXT_LINK(func)) func = NEXT_LINK(func);
1388             for (; func; func = PREV_LINK(func))
1389             {
1390                 current_func = func;
1391                 if (func->args)
1392                 {
1393                     var = func->args;
1394                     while (NEXT_LINK(var)) var = NEXT_LINK(var);
1395                     while (var)
1396                     {
1397                         write_typeformatstring_var(file, indent, var,
1398                                                    &typeformat_offset);
1399                         var = PREV_LINK(var);
1400                     }
1401                 }
1402             }
1403         }
1404     }
1405
1406     print_file(file, indent, "0x0\n");
1407     indent--;
1408     print_file(file, indent, "}\n");
1409     indent--;
1410     print_file(file, indent, "};\n");
1411     print_file(file, indent, "\n");
1412 }
1413
1414 static unsigned int get_required_buffer_size_type(
1415     const type_t *type, int ptr_level, const expr_t *array,
1416     const char *name, unsigned int *alignment)
1417 {
1418     *alignment = 0;
1419     if (ptr_level == 0 && !array && !type_has_ref(type))
1420     {
1421         switch (type->type)
1422         {
1423         case RPC_FC_BYTE:
1424         case RPC_FC_CHAR:
1425         case RPC_FC_USMALL:
1426         case RPC_FC_SMALL:
1427             *alignment = 4;
1428             return 1;
1429
1430         case RPC_FC_WCHAR:
1431         case RPC_FC_USHORT:
1432         case RPC_FC_SHORT:
1433             *alignment = 4;
1434             return 2;
1435
1436         case RPC_FC_ULONG:
1437         case RPC_FC_LONG:
1438         case RPC_FC_FLOAT:
1439         case RPC_FC_ERROR_STATUS_T:
1440             *alignment = 4;
1441             return 4;
1442
1443         case RPC_FC_HYPER:
1444         case RPC_FC_DOUBLE:
1445             *alignment = 8;
1446             return 8;
1447
1448         case RPC_FC_IGNORE:
1449         case RPC_FC_BIND_PRIMITIVE:
1450             return 0;
1451
1452         case RPC_FC_STRUCT:
1453         {
1454             size_t size = 0;
1455             const var_t *field;
1456             for (field = type->fields; field; field = NEXT_LINK(field))
1457             {
1458                 unsigned int alignment;
1459                 size += get_required_buffer_size_type(
1460                     field->type, field->ptr_level, field->array, field->name,
1461                     &alignment);
1462             }
1463             return size;
1464         }
1465
1466         default:
1467             error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
1468             return 0;
1469         }
1470     }
1471     if (ptr_level == 0 && type_has_ref(type))
1472         return get_required_buffer_size_type(type->ref, 0 /* FIXME */, array, name, alignment);
1473     return 0;
1474 }
1475
1476 unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
1477 {
1478     expr_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1479     int has_size = (size_is && (size_is->type != EXPR_VOID));
1480     int in_attr = is_attr(var->attrs, ATTR_IN);
1481     int out_attr = is_attr(var->attrs, ATTR_OUT);
1482
1483     if (!in_attr && !out_attr)
1484         in_attr = 1;
1485
1486     *alignment = 0;
1487
1488     if (pass == PASS_OUT)
1489     {
1490         if (out_attr && var->ptr_level > 0)
1491         {
1492             type_t *type = var->type;
1493             while (type->type == 0 && type->ref)
1494                 type = type->ref;
1495
1496             if (type->type == RPC_FC_STRUCT)
1497             {
1498                 const var_t *field;
1499                 unsigned int size = 36;
1500                 for (field = type->fields; field; field = NEXT_LINK(field))
1501                 {
1502                     unsigned int align;
1503                     size += get_required_buffer_size_type(
1504                         field->type, field->ptr_level, field->array, field->name,
1505                         &align);
1506                 }
1507                 return size;
1508             }
1509         }
1510         return 0;
1511     }
1512     else
1513     {
1514         if ((!out_attr || in_attr) && !has_size && !is_attr(var->attrs, ATTR_STRING) && !var->array)
1515         {
1516             if (var->ptr_level > 0 || (var->ptr_level == 0 && type_has_ref(var->type)))
1517             {
1518                 type_t *type = var->type;
1519                 while (type->type == 0 && type->ref)
1520                     type = type->ref;
1521
1522                 if (is_base_type(type->type))
1523                 {
1524                     return 25;
1525                 }
1526                 else if (type->type == RPC_FC_STRUCT)
1527                 {
1528                     unsigned int size = 36;
1529                     const var_t *field;
1530                     for (field = type->fields; field; field = NEXT_LINK(field))
1531                     {
1532                         unsigned int align;
1533                         size += get_required_buffer_size_type(
1534                             field->type, field->ptr_level, field->array, field->name,
1535                             &align);
1536                     }
1537                     return size;
1538                 }
1539             }
1540         }
1541
1542         return get_required_buffer_size_type(var->type, var->ptr_level, var->array, var->name, alignment);
1543     }
1544 }
1545
1546 static void print_phase_function(FILE *file, int indent, const char *type,
1547                                  enum remoting_phase phase,
1548                                  const char *varname, unsigned int type_offset)
1549 {
1550     const char *function;
1551     switch (phase)
1552     {
1553     case PHASE_BUFFERSIZE:
1554         function = "BufferSize";
1555         break;
1556     case PHASE_MARSHAL:
1557         function = "Marshall";
1558         break;
1559     case PHASE_UNMARSHAL:
1560         function = "Unmarshall";
1561         break;
1562     case PHASE_FREE:
1563         function = "Free";
1564         break;
1565     default:
1566         assert(0);
1567         return;
1568     }
1569
1570     print_file(file, indent, "Ndr%s%s(\n", type, function);
1571     indent++;
1572     print_file(file, indent, "&_StubMsg,\n");
1573     print_file(file, indent, "%s%s,\n",
1574                (phase == PHASE_UNMARSHAL) ? "(unsigned char **)&" : "(unsigned char *)",
1575                varname);
1576     print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1577                type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
1578     if (phase == PHASE_UNMARSHAL)
1579         print_file(file, indent, "0);\n");
1580     indent--;
1581 }
1582
1583 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
1584                           enum pass pass, const var_t *var,
1585                           const char *varname)
1586 {
1587     const type_t *type = var->type;
1588     unsigned int size;
1589     unsigned int alignment = 0;
1590     unsigned char rtype;
1591
1592     /* no work to do for other phases, buffer sizing is done elsewhere */
1593     if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
1594         return;
1595
1596     rtype = ref_type(type);
1597
1598     switch (rtype)
1599     {
1600         case RPC_FC_BYTE:
1601         case RPC_FC_CHAR:
1602         case RPC_FC_SMALL:
1603         case RPC_FC_USMALL:
1604             size = 1;
1605             alignment = 1;
1606             break;
1607
1608         case RPC_FC_WCHAR:
1609         case RPC_FC_USHORT:
1610         case RPC_FC_SHORT:
1611             size = 2;
1612             alignment = 2;
1613             break;
1614
1615         case RPC_FC_ULONG:
1616         case RPC_FC_LONG:
1617         case RPC_FC_FLOAT:
1618         case RPC_FC_ERROR_STATUS_T:
1619             size = 4;
1620             alignment = 4;
1621             break;
1622
1623         case RPC_FC_HYPER:
1624         case RPC_FC_DOUBLE:
1625             size = 8;
1626             alignment = 8;
1627             break;
1628
1629         case RPC_FC_IGNORE:
1630         case RPC_FC_BIND_PRIMITIVE:
1631             /* no marshalling needed */
1632             return;
1633
1634         default:
1635             error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
1636             size = 0;
1637     }
1638
1639     print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1640                 alignment - 1, alignment - 1);
1641
1642     if (phase == PHASE_MARSHAL)
1643     {
1644         print_file(file, indent, "*(");
1645         write_type(file, var->type, NULL, var->tname);
1646         if (var->ptr_level)
1647             fprintf(file, " *)_StubMsg.Buffer = *");
1648         else
1649             fprintf(file, " *)_StubMsg.Buffer = ");
1650         fprintf(file, varname);
1651         fprintf(file, ";\n");
1652     }
1653     else if (phase == PHASE_UNMARSHAL)
1654     {
1655         if (pass == PASS_IN || pass == PASS_RETURN)
1656             print_file(file, indent, "");
1657         else
1658             print_file(file, indent, "*");
1659         fprintf(file, varname);
1660         if (pass == PASS_IN && var->ptr_level)
1661             fprintf(file, " = (");
1662         else
1663             fprintf(file, " = *(");
1664         write_type(file, var->type, NULL, var->tname);
1665         fprintf(file, " *)_StubMsg.Buffer;\n");
1666     }
1667
1668     print_file(file, indent, "_StubMsg.Buffer += sizeof(");
1669     write_type(file, var->type, NULL, var->tname);
1670     fprintf(file, ");\n");
1671 }
1672
1673 /* returns whether the MaxCount, Offset or ActualCount members need to be
1674  * filled in for the specified phase */
1675 static inline int is_size_needed_for_phase(enum remoting_phase phase)
1676 {
1677     return (phase != PHASE_UNMARSHAL);
1678 }
1679
1680 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
1681                               unsigned int *type_offset, enum pass pass,
1682                               enum remoting_phase phase)
1683 {
1684     const expr_t *length_is;
1685     const expr_t *size_is;
1686     int in_attr, out_attr, has_length, has_size, pointer_type;
1687     var_t *var;
1688
1689     if (!func->args)
1690         return;
1691
1692     var = func->args;
1693     while (NEXT_LINK(var)) var = NEXT_LINK(var);
1694     for (; var; *type_offset += get_size_typeformatstring_var(var), var = PREV_LINK(var))
1695     {
1696         const type_t *type = var->type;
1697         unsigned char rtype;
1698
1699         length_is = get_attrp(var->attrs, ATTR_LENGTHIS);
1700         size_is = get_attrp(var->attrs, ATTR_SIZEIS);
1701         has_length = length_is && (length_is->type != EXPR_VOID);
1702         has_size = (size_is && (size_is->type != EXPR_VOID)) || (var->array && !var->array->is_const);
1703
1704         pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
1705         if (!pointer_type)
1706             pointer_type = RPC_FC_RP;
1707
1708         in_attr = is_attr(var->attrs, ATTR_IN);
1709         out_attr = is_attr(var->attrs, ATTR_OUT);
1710         if (!in_attr && !out_attr)
1711             in_attr = 1;
1712
1713         switch (pass)
1714         {
1715         case PASS_IN:
1716             if (!in_attr)
1717                 continue;
1718             break;
1719         case PASS_OUT:
1720             if (!out_attr)
1721                 continue;
1722             break;
1723         case PASS_RETURN:
1724             break;
1725         }
1726
1727         rtype = ref_type(type);
1728
1729         if (is_string_type(var->attrs, var->ptr_level, var->array))
1730         {
1731             if (var->array && var->array->is_const)
1732                 print_phase_function(file, indent, "NonConformantString", phase, var->name, *type_offset);
1733             else
1734             {
1735                 if (size_is && is_size_needed_for_phase(phase))
1736                 {
1737                     print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1738                     write_expr(file, size_is, 1);
1739                     fprintf(file, ";\n");
1740                 }
1741
1742                 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
1743                     print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1744                 else
1745                     print_phase_function(file, indent, "ConformantString", phase, var->name,
1746                                          *type_offset + (has_size ? 4 : 2));
1747             }
1748         }
1749         else if (is_array_type(var->attrs, var->ptr_level, var->array))
1750         {
1751             const char *array_type;
1752
1753             if (var->array && NEXT_LINK(var->array)) /* multi-dimensional array */
1754                 array_type = "ComplexArray";
1755             else
1756             {
1757                 if (!has_length && !has_size)
1758                     array_type = "FixedArray";
1759                 else if (has_length && !has_size)
1760                 {
1761                     if (is_size_needed_for_phase(phase))
1762                     {
1763                         print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1764                         print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1765                         write_expr(file, length_is, 1);
1766                         fprintf(file, ";\n\n");
1767                     }
1768                     array_type = "VaryingArray";
1769                 }
1770                 else if (!has_length && has_size)
1771                 {
1772                     if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
1773                     {
1774                         print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1775                         write_expr(file, size_is ? size_is : var->array, 1);
1776                         fprintf(file, ";\n\n");
1777                     }
1778                     array_type = "ConformantArray";
1779                 }
1780                 else
1781                 {
1782                     if (is_size_needed_for_phase(phase))
1783                     {
1784                         print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
1785                         write_expr(file, size_is ? size_is : var->array, 1);
1786                         fprintf(file, ";\n");
1787                         print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1788                         print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
1789                         write_expr(file, length_is, 1);
1790                         fprintf(file, ";\n\n");
1791                     }
1792                     array_type = "ConformantVaryingArray";
1793                 }
1794             }
1795
1796             if (!in_attr && phase == PHASE_FREE)
1797             {
1798                 print_file(file, indent, "if (%s)\n", var->name);
1799                 indent++;
1800                 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
1801             }
1802             else if (phase != PHASE_FREE)
1803             {
1804                 if (pointer_type == RPC_FC_UP)
1805                     print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1806                 else
1807                     print_phase_function(file, indent, array_type, phase, var->name, *type_offset + 4);
1808             }
1809         }
1810         else if (var->ptr_level == 0 && is_base_type(rtype))
1811         {
1812             print_phase_basetype(file, indent, phase, pass, var, var->name);
1813         }
1814         else if (var->ptr_level == 0)
1815         {
1816             const char *ndrtype;
1817
1818             switch (rtype)
1819             {
1820             case RPC_FC_STRUCT:
1821                 ndrtype = "SimpleStruct";
1822                 break;
1823             case RPC_FC_CSTRUCT:
1824             case RPC_FC_CPSTRUCT:
1825                 ndrtype = "ConformantStruct";
1826                 break;
1827             case RPC_FC_CVSTRUCT:
1828                 ndrtype = "ConformantVaryingStruct";
1829                 break;
1830             case RPC_FC_BOGUS_STRUCT:
1831                 ndrtype = "ComplexStruct";
1832                 break;
1833             default:
1834                 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1835                     var->name, rtype, var->ptr_level);
1836                 ndrtype = NULL;
1837             }
1838
1839             print_phase_function(file, indent, ndrtype, phase, var->name, *type_offset);
1840         }
1841         else
1842         {
1843             if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
1844             {
1845                 print_phase_basetype(file, indent, phase, pass, var, var->name);
1846             }
1847             else if ((var->ptr_level == 1) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
1848             {
1849                 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
1850                     print_phase_function(file, indent, "SimpleStruct", phase, var->name, *type_offset + 4);
1851             }
1852             else
1853             {
1854                 print_phase_function(file, indent, "Pointer", phase, var->name, *type_offset);
1855             }
1856         }
1857         fprintf(file, "\n");
1858     }
1859 }
1860
1861
1862 size_t get_size_procformatstring_var(const var_t *var)
1863 {
1864     unsigned int type_offset = 2;
1865     return write_procformatstring_var(NULL, 0, var, FALSE, &type_offset);
1866 }
1867
1868
1869 size_t get_size_typeformatstring_var(const var_t *var)
1870 {
1871     unsigned int type_offset = 0;
1872     write_typeformatstring_var(NULL, 0, var, &type_offset);
1873     return type_offset;
1874 }
1875
1876 size_t get_size_procformatstring(const ifref_t *ifaces)
1877 {
1878     const ifref_t *iface = ifaces;
1879     size_t size = 1;
1880     func_t *func;
1881     var_t *var;
1882
1883     END_OF_LIST(iface);
1884
1885     for (; iface; iface = PREV_LINK(iface))
1886     {
1887         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1888             continue;
1889
1890         if (iface->iface->funcs)
1891         {
1892             func = iface->iface->funcs;
1893             while (NEXT_LINK(func)) func = NEXT_LINK(func);
1894             while (func)
1895             {
1896                 /* argument list size */
1897                 if (func->args)
1898                 {
1899                     var = func->args;
1900                     while (NEXT_LINK(var)) var = NEXT_LINK(var);
1901                     while (var)
1902                     {
1903                         size += get_size_procformatstring_var(var);
1904                         var = PREV_LINK(var);
1905                     }
1906                 }
1907
1908                 var = func->def;
1909                 /* return value size */
1910                 if (is_void(var->type, NULL))
1911                     size += 2;
1912                 else
1913                     size += get_size_procformatstring_var(var);
1914
1915                 func = PREV_LINK(func);
1916             }
1917         }
1918     }
1919     return size;
1920 }
1921
1922 size_t get_size_typeformatstring(const ifref_t *ifaces)
1923 {
1924     const ifref_t *iface = ifaces;
1925     size_t size = 3;
1926     func_t *func;
1927     var_t *var;
1928
1929     END_OF_LIST(iface);
1930
1931     for (; iface; iface = PREV_LINK(iface))
1932     {
1933         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
1934             continue;
1935
1936         if (iface->iface->funcs)
1937         {
1938             func = iface->iface->funcs;
1939             while (NEXT_LINK(func)) func = NEXT_LINK(func);
1940             while (func)
1941             {
1942                 /* argument list size */
1943                 if (func->args)
1944                 {
1945                     var = func->args;
1946                     while (NEXT_LINK(var)) var = NEXT_LINK(var);
1947                     while (var)
1948                     {
1949                         size += get_size_typeformatstring_var(var);
1950                         var = PREV_LINK(var);
1951                     }
1952                 }
1953
1954                 func = PREV_LINK(func);
1955             }
1956         }
1957     }
1958     return size;
1959 }
1960
1961 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
1962                               const var_t *fields, const char *structvar)
1963 {
1964     switch (e->type) {
1965         case EXPR_VOID:
1966             break;
1967         case EXPR_NUM:
1968             fprintf(h, "%ld", e->u.lval);
1969             break;
1970         case EXPR_HEXNUM:
1971             fprintf(h, "0x%lx", e->u.lval);
1972             break;
1973         case EXPR_TRUEFALSE:
1974             if (e->u.lval == 0)
1975                 fprintf(h, "FALSE");
1976             else
1977                 fprintf(h, "TRUE");
1978             break;
1979         case EXPR_IDENTIFIER:
1980         {
1981             const var_t *field;
1982             for (field = fields; field; field = NEXT_LINK(field))
1983             {
1984                 if (!strcmp(e->u.sval, field->name))
1985                 {
1986                     fprintf(h, "%s->%s", structvar, e->u.sval);
1987                     break;
1988                 }
1989             }
1990             if (!field) error("no field found for identifier %s\n", e->u.sval);
1991             break;
1992         }
1993         case EXPR_NEG:
1994             fprintf(h, "-");
1995             write_struct_expr(h, e->ref, 1, fields, structvar);
1996             break;
1997         case EXPR_NOT:
1998             fprintf(h, "~");
1999             write_struct_expr(h, e->ref, 1, fields, structvar);
2000             break;
2001         case EXPR_PPTR:
2002             fprintf(h, "*");
2003             write_struct_expr(h, e->ref, 1, fields, structvar);
2004             break;
2005         case EXPR_CAST:
2006             fprintf(h, "(");
2007             write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2008             fprintf(h, ")");
2009             write_struct_expr(h, e->ref, 1, fields, structvar);
2010             break;
2011         case EXPR_SIZEOF:
2012             fprintf(h, "sizeof(");
2013             write_type(h, e->u.tref->ref, NULL, e->u.tref->name);
2014             fprintf(h, ")");
2015             break;
2016         case EXPR_SHL:
2017         case EXPR_SHR:
2018         case EXPR_MUL:
2019         case EXPR_DIV:
2020         case EXPR_ADD:
2021         case EXPR_SUB:
2022         case EXPR_AND:
2023         case EXPR_OR:
2024             if (brackets) fprintf(h, "(");
2025             write_struct_expr(h, e->ref, 1, fields, structvar);
2026             switch (e->type) {
2027                 case EXPR_SHL: fprintf(h, " << "); break;
2028                 case EXPR_SHR: fprintf(h, " >> "); break;
2029                 case EXPR_MUL: fprintf(h, " * "); break;
2030                 case EXPR_DIV: fprintf(h, " / "); break;
2031                 case EXPR_ADD: fprintf(h, " + "); break;
2032                 case EXPR_SUB: fprintf(h, " - "); break;
2033                 case EXPR_AND: fprintf(h, " & "); break;
2034                 case EXPR_OR:  fprintf(h, " | "); break;
2035                 default: break;
2036             }
2037             write_struct_expr(h, e->u.ext, 1, fields, structvar);
2038             if (brackets) fprintf(h, ")");
2039             break;
2040         case EXPR_COND:
2041             if (brackets) fprintf(h, "(");
2042             write_struct_expr(h, e->ref, 1, fields, structvar);
2043             fprintf(h, " ? ");
2044             write_struct_expr(h, e->u.ext, 1, fields, structvar);
2045             fprintf(h, " : ");
2046             write_struct_expr(h, e->ext2, 1, fields, structvar);
2047             if (brackets) fprintf(h, ")");
2048             break;
2049     }
2050 }
2051
2052 int write_expr_eval_routines(FILE *file, const char *iface)
2053 {
2054     int result = 0;
2055     struct expr_eval_routine *eval;
2056     unsigned short callback_offset = 0;
2057
2058     LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2059     {
2060         int indent = 0;
2061         result = 1;
2062         print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2063                   iface, eval->structure->name, callback_offset);
2064         print_file(file, indent, "{\n");
2065         indent++;
2066         print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2067                    eval->structure->name, eval->structure->name, eval->structure_size);
2068         fprintf(file, "\n");
2069         print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2070         print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2071         write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2072         fprintf(file, ";\n");
2073         indent--;
2074         print_file(file, indent, "}\n\n");
2075         callback_offset++;
2076     }
2077     return result;
2078 }
2079
2080 void write_expr_eval_routine_list(FILE *file, const char *iface)
2081 {
2082     struct expr_eval_routine *eval;
2083     struct expr_eval_routine *cursor;
2084     unsigned short callback_offset = 0;
2085
2086     fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2087     fprintf(file, "{\n");
2088
2089     LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2090     {
2091         print_file(file, 1, "%s_%sExprEval_%04u,\n",
2092                    iface, eval->structure->name, callback_offset);
2093
2094         callback_offset++;
2095         list_remove(&eval->entry);
2096         free(eval);
2097     }
2098
2099     fprintf(file, "};\n\n");
2100 }