crypt32: Introduce function to encode an array of items as a set.
[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 fields_memsize(const var_list_t *fields, unsigned int *align);
63 static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65                                 const char *name, int write_ptr, unsigned int *tfsoff);
66
67 const char *string_of_type(unsigned char type)
68 {
69     switch (type)
70     {
71     case RPC_FC_BYTE: return "FC_BYTE";
72     case RPC_FC_CHAR: return "FC_CHAR";
73     case RPC_FC_SMALL: return "FC_SMALL";
74     case RPC_FC_USMALL: return "FC_USMALL";
75     case RPC_FC_WCHAR: return "FC_WCHAR";
76     case RPC_FC_SHORT: return "FC_SHORT";
77     case RPC_FC_USHORT: return "FC_USHORT";
78     case RPC_FC_LONG: return "FC_LONG";
79     case RPC_FC_ULONG: return "FC_ULONG";
80     case RPC_FC_FLOAT: return "FC_FLOAT";
81     case RPC_FC_HYPER: return "FC_HYPER";
82     case RPC_FC_DOUBLE: return "FC_DOUBLE";
83     case RPC_FC_ENUM16: return "FC_ENUM16";
84     case RPC_FC_ENUM32: return "FC_ENUM32";
85     case RPC_FC_IGNORE: return "FC_IGNORE";
86     case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
87     case RPC_FC_RP: return "FC_RP";
88     case RPC_FC_UP: return "FC_UP";
89     case RPC_FC_OP: return "FC_OP";
90     case RPC_FC_FP: return "FC_FP";
91     case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
92     case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
93     case RPC_FC_STRUCT: return "FC_STRUCT";
94     case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
95     case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
96     case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
97     case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
98     case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
99     case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
100     case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
101     case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
102     case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
103     case RPC_FC_CARRAY: return "FC_CARRAY";
104     case RPC_FC_CVARRAY: return "FC_CVARRAY";
105     case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
106     default:
107         error("string_of_type: unknown type 0x%02x\n", type);
108         return NULL;
109     }
110 }
111
112 int is_struct(unsigned char type)
113 {
114     switch (type)
115     {
116     case RPC_FC_STRUCT:
117     case RPC_FC_PSTRUCT:
118     case RPC_FC_CSTRUCT:
119     case RPC_FC_CPSTRUCT:
120     case RPC_FC_CVSTRUCT:
121     case RPC_FC_BOGUS_STRUCT:
122         return 1;
123     default:
124         return 0;
125     }
126 }
127
128 static int is_non_complex_struct(const type_t *type)
129 {
130     switch (type->type)
131     {
132     case RPC_FC_STRUCT:
133     case RPC_FC_PSTRUCT:
134     case RPC_FC_CSTRUCT:
135     case RPC_FC_CPSTRUCT:
136     case RPC_FC_CVSTRUCT:
137         return 1;
138     default:
139         return 0;
140     }
141 }
142
143 int is_union(unsigned char type)
144 {
145     switch (type)
146     {
147     case RPC_FC_ENCAPSULATED_UNION:
148     case RPC_FC_NON_ENCAPSULATED_UNION:
149         return 1;
150     default:
151         return 0;
152     }
153 }
154
155 static unsigned short user_type_offset(const char *name)
156 {
157     user_type_t *ut;
158     unsigned short off = 0;
159     LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
160     {
161         if (strcmp(name, ut->name) == 0)
162             return off;
163         ++off;
164     }
165     error("user_type_offset: couldn't find type (%s)\n", name);
166     return 0;
167 }
168
169 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
170 {
171     type->typestring_offset = offset;
172     if (file) type->tfswrite = FALSE;
173 }
174
175 static void guard_rec(type_t *type)
176 {
177     /* types that contain references to themselves (like a linked list),
178        need to be shielded from infinite recursion when writing embedded
179        types  */
180     if (type->typestring_offset)
181         type->tfswrite = FALSE;
182     else
183         type->typestring_offset = 1;
184 }
185
186 static type_t *get_user_type(const type_t *t, const char **pname)
187 {
188     for (;;)
189     {
190         type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
191         if (ut)
192         {
193             if (pname)
194                 *pname = t->name;
195             return ut;
196         }
197
198         if (t->kind == TKIND_ALIAS)
199             t = t->orig;
200         else
201             return 0;
202     }
203 }
204
205 static int is_user_type(const type_t *t)
206 {
207     return get_user_type(t, NULL) != NULL;
208 }
209
210 static int is_embedded_complex(const type_t *type)
211 {
212     unsigned char tc = type->type;
213     return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
214         || (is_ptr(type) && type->ref->type == RPC_FC_IP);
215 }
216
217 static int compare_expr(const expr_t *a, const expr_t *b)
218 {
219     int ret;
220
221     if (a->type != b->type)
222         return a->type - b->type;
223
224     switch (a->type)
225     {
226         case EXPR_NUM:
227         case EXPR_HEXNUM:
228         case EXPR_TRUEFALSE:
229             return a->u.lval - b->u.lval;
230         case EXPR_DOUBLE:
231             return a->u.dval - b->u.dval;
232         case EXPR_IDENTIFIER:
233             return strcmp(a->u.sval, b->u.sval);
234         case EXPR_COND:
235             ret = compare_expr(a->ref, b->ref);
236             if (ret != 0)
237                 return ret;
238             ret = compare_expr(a->u.ext, b->u.ext);
239             if (ret != 0)
240                 return ret;
241             return compare_expr(a->ext2, b->ext2);
242         case EXPR_OR:
243         case EXPR_AND:
244         case EXPR_ADD:
245         case EXPR_SUB:
246         case EXPR_MUL:
247         case EXPR_DIV:
248         case EXPR_SHL:
249         case EXPR_SHR:
250             ret = compare_expr(a->ref, b->ref);
251             if (ret != 0)
252                 return ret;
253             return compare_expr(a->u.ext, b->u.ext);
254         case EXPR_NOT:
255         case EXPR_NEG:
256         case EXPR_PPTR:
257         case EXPR_CAST:
258         case EXPR_SIZEOF:
259             return compare_expr(a->ref, b->ref);
260         case EXPR_VOID:
261             return 0;
262     }
263     return -1;
264 }
265
266 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
267     do { \
268         if (file) \
269             fprintf(file, "/* %2u */\n", typestring_offset); \
270         print_file((file), 2, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
271     } \
272     while (0)
273
274 static void print_file(FILE *file, int indent, const char *format, ...)
275 {
276     va_list va;
277     va_start(va, format);
278     print(file, indent, format, va);
279     va_end(va);
280 }
281
282 void print(FILE *file, int indent, const char *format, va_list va)
283 {
284     if (file)
285     {
286         if (format[0] != '\n')
287             while (0 < indent--)
288                 fprintf(file, "    ");
289         vfprintf(file, format, va);
290     }
291 }
292
293 void write_parameters_init(FILE *file, int indent, const func_t *func)
294 {
295     const var_t *var;
296
297     if (!func->args)
298         return;
299
300     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
301     {
302         const type_t *t = var->type;
303         const char *n = var->name;
304         if (decl_indirect(t))
305             print_file(file, indent, "MIDL_memset(&%s, 0, sizeof %s);\n", n, n);
306         else if (is_ptr(t) || is_array(t))
307             print_file(file, indent, "%s = 0;\n", n);
308     }
309
310     fprintf(file, "\n");
311 }
312
313 static void write_formatdesc(FILE *f, int indent, const char *str)
314 {
315     print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
316     print_file(f, indent, "{\n");
317     print_file(f, indent + 1, "short Pad;\n");
318     print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
319     print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
320     print_file(f, indent, "\n");
321 }
322
323 void write_formatstringsdecl(FILE *f, int indent, ifref_list_t *ifaces, int for_objects)
324 {
325     print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
326                get_size_typeformatstring(ifaces, for_objects));
327
328     print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
329                get_size_procformatstring(ifaces, for_objects));
330
331     fprintf(f, "\n");
332     write_formatdesc(f, indent, "TYPE");
333     write_formatdesc(f, indent, "PROC");
334     fprintf(f, "\n");
335     print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
336     print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
337     print_file(f, indent, "\n");
338 }
339
340 static inline int is_base_type(unsigned char type)
341 {
342     switch (type)
343     {
344     case RPC_FC_BYTE:
345     case RPC_FC_CHAR:
346     case RPC_FC_USMALL:
347     case RPC_FC_SMALL:
348     case RPC_FC_WCHAR:
349     case RPC_FC_USHORT:
350     case RPC_FC_SHORT:
351     case RPC_FC_ULONG:
352     case RPC_FC_LONG:
353     case RPC_FC_HYPER:
354     case RPC_FC_IGNORE:
355     case RPC_FC_FLOAT:
356     case RPC_FC_DOUBLE:
357     case RPC_FC_ENUM16:
358     case RPC_FC_ENUM32:
359     case RPC_FC_ERROR_STATUS_T:
360     case RPC_FC_BIND_PRIMITIVE:
361         return TRUE;
362
363     default:
364         return FALSE;
365     }
366 }
367
368 int decl_indirect(const type_t *t)
369 {
370     return is_user_type(t)
371         || (!is_base_type(t->type)
372             && !is_ptr(t)
373             && !is_array(t));
374 }
375
376 static size_t write_procformatstring_var(FILE *file, int indent,
377                                          const var_t *var, int is_return)
378 {
379     size_t size;
380     const type_t *type = var->type;
381
382     int is_in = is_attr(var->attrs, ATTR_IN);
383     int is_out = is_attr(var->attrs, ATTR_OUT);
384
385     if (!is_in && !is_out) is_in = TRUE;
386
387     if (!type->declarray && is_base_type(type->type))
388     {
389         if (is_return)
390             print_file(file, indent, "0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
391         else
392             print_file(file, indent, "0x4e,    /* FC_IN_PARAM_BASETYPE */\n");
393
394         if (type->type == RPC_FC_BIND_PRIMITIVE)
395         {
396             print_file(file, indent, "0x%02x,    /* FC_IGNORE */\n", RPC_FC_IGNORE);
397             size = 2; /* includes param type prefix */
398         }
399         else if (is_base_type(type->type))
400         {
401             print_file(file, indent, "0x%02x,    /* %s */\n", type->type, string_of_type(type->type));
402             size = 2; /* includes param type prefix */
403         }
404         else
405         {
406             error("Unknown/unsupported type: %s (0x%02x)\n", var->name, type->type);
407             size = 0;
408         }
409     }
410     else
411     {
412         if (is_return)
413             print_file(file, indent, "0x52,    /* FC_RETURN_PARAM */\n");
414         else if (is_in && is_out)
415             print_file(file, indent, "0x50,    /* FC_IN_OUT_PARAM */\n");
416         else if (is_out)
417             print_file(file, indent, "0x51,    /* FC_OUT_PARAM */\n");
418         else
419             print_file(file, indent, "0x4d,    /* FC_IN_PARAM */\n");
420
421         print_file(file, indent, "0x01,\n");
422         print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
423         size = 4; /* includes param type prefix */
424     }
425     return size;
426 }
427
428 void write_procformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
429 {
430     const ifref_t *iface;
431     int indent = 0;
432     const var_t *var;
433
434     print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
435     print_file(file, indent, "{\n");
436     indent++;
437     print_file(file, indent, "0,\n");
438     print_file(file, indent, "{\n");
439     indent++;
440
441     if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
442     {
443         if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
444             continue;
445
446         if (iface->iface->funcs)
447         {
448             const func_t *func;
449             LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
450             {
451                 if (is_local(func->def->attrs)) continue;
452                 /* emit argument data */
453                 if (func->args)
454                 {
455                     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
456                         write_procformatstring_var(file, indent, var, FALSE);
457                 }
458
459                 /* emit return value data */
460                 var = func->def;
461                 if (is_void(var->type))
462                 {
463                     print_file(file, indent, "0x5b,    /* FC_END */\n");
464                     print_file(file, indent, "0x5c,    /* FC_PAD */\n");
465                 }
466                 else
467                     write_procformatstring_var(file, indent, var, TRUE);
468             }
469         }
470     }
471
472     print_file(file, indent, "0x0\n");
473     indent--;
474     print_file(file, indent, "}\n");
475     indent--;
476     print_file(file, indent, "};\n");
477     print_file(file, indent, "\n");
478 }
479
480 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
481 {
482     if (is_base_type(type->type))
483     {
484         print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
485         *typestring_offset += 1;
486         return 1;
487     }
488
489     return 0;
490 }
491
492 /* write conformance / variance descriptor */
493 static size_t write_conf_or_var_desc(FILE *file, const func_t *func, const type_t *structure,
494                                      unsigned int baseoff, const expr_t *expr)
495 {
496     unsigned char operator_type = 0;
497     const char *operator_string = "no operators";
498     const expr_t *subexpr;
499     unsigned char correlation_type;
500
501     if (!file) return 4; /* optimisation for sizing pass */
502
503     if (expr->is_const)
504     {
505         if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
506             error("write_conf_or_var_desc: constant value %ld is greater than "
507                   "the maximum constant size of %d\n", expr->cval,
508                   UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
509
510         print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
511                    RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
512         print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
513         print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
514
515         return 4;
516     }
517
518     subexpr = expr;
519     switch (subexpr->type)
520     {
521     case EXPR_PPTR:
522         subexpr = subexpr->ref;
523         operator_type = RPC_FC_DEREFERENCE;
524         operator_string = "FC_DEREFERENCE";
525         break;
526     case EXPR_DIV:
527         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
528         {
529             subexpr = subexpr->ref;
530             operator_type = RPC_FC_DIV_2;
531             operator_string = "FC_DIV_2";
532         }
533         break;
534     case EXPR_MUL:
535         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
536         {
537             subexpr = subexpr->ref;
538             operator_type = RPC_FC_MULT_2;
539             operator_string = "FC_MULT_2";
540         }
541         break;
542     case EXPR_SUB:
543         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
544         {
545             subexpr = subexpr->ref;
546             operator_type = RPC_FC_SUB_1;
547             operator_string = "FC_SUB_1";
548         }
549         break;
550     case EXPR_ADD:
551         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
552         {
553             subexpr = subexpr->ref;
554             operator_type = RPC_FC_ADD_1;
555             operator_string = "FC_ADD_1";
556         }
557         break;
558     default:
559         break;
560     }
561
562     if (subexpr->type == EXPR_IDENTIFIER)
563     {
564         const type_t *correlation_variable = NULL;
565         unsigned char correlation_variable_type;
566         unsigned char param_type = 0;
567         const char *param_type_string = NULL;
568         size_t offset;
569
570         if (structure)
571         {
572             const var_t *var;
573
574             offset = 0;
575             if (structure->fields) LIST_FOR_EACH_ENTRY( var, structure->fields, const var_t, entry )
576             {
577                 unsigned int align = 0;
578                 /* FIXME: take alignment into account */
579                 if (var->name && !strcmp(var->name, subexpr->u.sval))
580                 {
581                     correlation_variable = var->type;
582                     break;
583                 }
584                 offset += type_memsize(var->type, &align);
585             }
586             if (!correlation_variable)
587                 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
588                       subexpr->u.sval);
589
590             offset -= baseoff;
591             correlation_type = RPC_FC_NORMAL_CONFORMANCE;
592         }
593         else
594         {
595             const var_t *var;
596
597             offset = sizeof(void *);
598             if (func->args) LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
599             {
600                 if (!strcmp(var->name, subexpr->u.sval))
601                 {
602                     correlation_variable = var->type;
603                     break;
604                 }
605                 /* FIXME: not all stack variables are sizeof(void *) */
606                 offset += sizeof(void *);
607             }
608             if (!correlation_variable)
609                 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
610                     subexpr->u.sval);
611
612             correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
613         }
614
615         correlation_variable_type = correlation_variable->type;
616
617         switch (correlation_variable_type)
618         {
619         case RPC_FC_CHAR:
620         case RPC_FC_SMALL:
621             param_type = RPC_FC_SMALL;
622             param_type_string = "FC_SMALL";
623             break;
624         case RPC_FC_BYTE:
625         case RPC_FC_USMALL:
626             param_type = RPC_FC_USMALL;
627             param_type_string = "FC_USMALL";
628             break;
629         case RPC_FC_WCHAR:
630         case RPC_FC_SHORT:
631         case RPC_FC_ENUM16:
632             param_type = RPC_FC_SHORT;
633             param_type_string = "FC_SHORT";
634             break;
635         case RPC_FC_USHORT:
636             param_type = RPC_FC_USHORT;
637             param_type_string = "FC_USHORT";
638             break;
639         case RPC_FC_LONG:
640         case RPC_FC_ENUM32:
641             param_type = RPC_FC_LONG;
642             param_type_string = "FC_LONG";
643             break;
644         case RPC_FC_ULONG:
645             param_type = RPC_FC_ULONG;
646             param_type_string = "FC_ULONG";
647             break;
648         case RPC_FC_RP:
649         case RPC_FC_UP:
650         case RPC_FC_OP:
651         case RPC_FC_FP:
652             if (sizeof(void *) == 4)  /* FIXME */
653             {
654                 param_type = RPC_FC_LONG;
655                 param_type_string = "FC_LONG";
656             }
657             else
658             {
659                 param_type = RPC_FC_HYPER;
660                 param_type_string = "FC_HYPER";
661             }
662             break;
663         default:
664             error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
665                 correlation_variable_type);
666         }
667
668         print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
669                 correlation_type | param_type,
670                 correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter, " : "",
671                 param_type_string);
672         print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
673         print_file(file, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
674                    offset,
675                    correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "x86 stack size / " : "",
676                    offset);
677     }
678     else
679     {
680         unsigned int callback_offset = 0;
681
682         if (structure)
683         {
684             struct expr_eval_routine *eval;
685             int found = 0;
686
687             LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
688             {
689                 if (!strcmp(eval->structure->name, structure->name) &&
690                     !compare_expr(eval->expr, expr))
691                 {
692                     found = 1;
693                     break;
694                 }
695                 callback_offset++;
696             }
697
698             if (!found)
699             {
700                 unsigned int align = 0;
701                 eval = xmalloc(sizeof(*eval));
702                 eval->structure = structure;
703                 eval->structure_size = fields_memsize(structure->fields, &align);
704                 eval->expr = expr;
705                 list_add_tail(&expr_eval_routines, &eval->entry);
706             }
707
708             correlation_type = RPC_FC_NORMAL_CONFORMANCE;
709         }
710         else
711         {
712             error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
713             correlation_type = RPC_FC_TOP_LEVEL_CONFORMANCE;
714         }
715
716         if (callback_offset > USHRT_MAX)
717             error("Maximum number of callback routines reached\n");
718
719         print_file(file, 2, "0x%x, /* Corr desc: %s */\n",
720                    correlation_type,
721                    correlation_type == RPC_FC_TOP_LEVEL_CONFORMANCE ? "parameter" : "");
722         print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
723         print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
724     }
725     return 4;
726 }
727
728 static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
729 {
730     size_t size = 0;
731     const var_t *v;
732
733     if (!fields) return 0;
734     LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
735         size += type_memsize(v->type, align);
736
737     return size;
738 }
739
740 static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
741 {
742     size_t size, maxs = 0;
743     unsigned int align = *pmaxa;
744     const var_t *v;
745
746     if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
747     {
748         /* we could have an empty default field with NULL type */
749         if (v->type)
750         {
751             size = type_memsize(v->type, &align);
752             if (maxs < size) maxs = size;
753             if (*pmaxa < align) *pmaxa = align;
754         }
755     }
756
757     return maxs;
758 }
759
760 size_t type_memsize(const type_t *t, unsigned int *align)
761 {
762     size_t size = 0;
763
764     if (t->declarray && is_conformant_array(t))
765     {
766         type_memsize(t->ref, align);
767         size = 0;
768     }
769     else if (is_ptr(t) || is_conformant_array(t))
770     {
771         size = sizeof(void *);
772         if (size > *align) *align = size;
773     }
774     else switch (t->type)
775     {
776     case RPC_FC_BYTE:
777     case RPC_FC_CHAR:
778     case RPC_FC_USMALL:
779     case RPC_FC_SMALL:
780         size = 1;
781         if (size > *align) *align = size;
782         break;
783     case RPC_FC_WCHAR:
784     case RPC_FC_USHORT:
785     case RPC_FC_SHORT:
786     case RPC_FC_ENUM16:
787         size = 2;
788         if (size > *align) *align = size;
789         break;
790     case RPC_FC_ULONG:
791     case RPC_FC_LONG:
792     case RPC_FC_ERROR_STATUS_T:
793     case RPC_FC_ENUM32:
794     case RPC_FC_FLOAT:
795         size = 4;
796         if (size > *align) *align = size;
797         break;
798     case RPC_FC_HYPER:
799     case RPC_FC_DOUBLE:
800         size = 8;
801         if (size > *align) *align = size;
802         break;
803     case RPC_FC_STRUCT:
804     case RPC_FC_CVSTRUCT:
805     case RPC_FC_CPSTRUCT:
806     case RPC_FC_CSTRUCT:
807     case RPC_FC_PSTRUCT:
808     case RPC_FC_BOGUS_STRUCT:
809         size = fields_memsize(t->fields, align);
810         break;
811     case RPC_FC_ENCAPSULATED_UNION:
812     case RPC_FC_NON_ENCAPSULATED_UNION:
813         size = union_memsize(t->fields, align);
814         break;
815     case RPC_FC_SMFARRAY:
816     case RPC_FC_LGFARRAY:
817     case RPC_FC_SMVARRAY:
818     case RPC_FC_LGVARRAY:
819     case RPC_FC_BOGUS_ARRAY:
820         size = t->dim * type_memsize(t->ref, align);
821         break;
822     default:
823         error("type_memsize: Unknown type %d\n", t->type);
824         size = 0;
825     }
826
827     return size;
828 }
829
830 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
831 {
832     short absoff = type->ref->typestring_offset;
833     short reloff = absoff - (offset + 2);
834     int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;
835
836     print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
837                type->type, ptr_attr, string_of_type(type->type));
838     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
839                reloff, reloff, absoff);
840     return 4;
841 }
842
843 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
844 {
845     print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
846                type->type, string_of_type(type->type));
847     print_file(file, 2, "0x%02x,\t/* %s */\n", type->ref->type,
848                string_of_type(type->ref->type));
849     print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
850     return 4;
851 }
852
853 static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
854 {
855     unsigned int offset = *typestring_offset;
856
857     print_file(file, 0, "/* %d */\n", offset);
858     update_tfsoff(type, offset, file);
859
860     if (type->ref->typestring_offset)
861         *typestring_offset += write_nonsimple_pointer(file, type, offset);
862     else if (is_base_type(type->ref->type))
863         *typestring_offset += write_simple_pointer(file, type);
864
865     return offset;
866 }
867
868 static int processed(const type_t *type)
869 {
870     return type->typestring_offset && !type->tfswrite;
871 }
872
873 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
874 {
875     unsigned int start, absoff, flags;
876     unsigned int align = 0, ualign = 0;
877     const char *name;
878     type_t *utype = get_user_type(type, &name);
879     size_t usize = type_memsize(utype, &ualign);
880     size_t size = type_memsize(type, &align);
881     unsigned short funoff = user_type_offset(name);
882     short reloff;
883
884     guard_rec(type);
885
886     if (is_base_type(utype->type))
887     {
888         absoff = *tfsoff;
889         print_file(file, 0, "/* %d */\n", absoff);
890         print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
891         print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
892         *tfsoff += 2;
893     }
894     else
895     {
896         if (!processed(utype))
897             write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
898         absoff = utype->typestring_offset;
899     }
900
901     if (utype->type == RPC_FC_RP)
902         flags = 0x40;
903     else if (utype->type == RPC_FC_UP)
904         flags = 0x80;
905     else
906         flags = 0;
907
908     start = *tfsoff;
909     update_tfsoff(type, start, file);
910     print_file(file, 0, "/* %d */\n", start);
911     print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
912     print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
913                flags | (align - 1), align - 1, flags);
914     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
915     print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
916     print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
917     *tfsoff += 8;
918     reloff = absoff - *tfsoff;
919     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
920     *tfsoff += 2;
921 }
922
923 static void write_member_type(FILE *file, type_t *type, const var_t *field,
924                               unsigned int *corroff, unsigned int *tfsoff)
925 {
926     if (is_embedded_complex(type))
927     {
928         size_t absoff;
929         short reloff;
930
931         if (is_union(type->type) && is_attr(field->attrs, ATTR_SWITCHIS))
932         {
933             absoff = *corroff;
934             *corroff += 8;
935         }
936         else
937         {
938             absoff = type->typestring_offset;
939         }
940         reloff = absoff - (*tfsoff + 2);
941
942         print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
943         /* FIXME: actually compute necessary padding */
944         print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
945         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
946                    reloff, reloff, absoff);
947         *tfsoff += 4;
948     }
949     else if (is_ptr(type))
950     {
951         print_file(file, 2, "0x8,\t/* FC_LONG */\n");
952         *tfsoff += 1;
953     }
954     else if (!write_base_type(file, type, tfsoff))
955         error("Unsupported member type 0x%x\n", type->type);
956 }
957
958 static void write_end(FILE *file, unsigned int *tfsoff)
959 {
960     if (*tfsoff % 2 == 0)
961     {
962         print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
963         *tfsoff += 1;
964     }
965     print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
966     *tfsoff += 1;
967 }
968
969 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
970 {
971     unsigned int offset = 0;
972     var_list_t *fs = type->fields;
973     var_t *f;
974
975     if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
976     {
977         unsigned int align = 0;
978         type_t *ft = f->type;
979         if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
980         {
981             unsigned int absoff = ft->typestring_offset;
982             short reloff = absoff - (*tfsoff + 6);
983             print_file(file, 0, "/* %d */\n", *tfsoff);
984             print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
985             print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
986             write_conf_or_var_desc(file, current_func, current_structure, offset,
987                                    get_attrp(f->attrs, ATTR_SWITCHIS));
988             print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
989                        reloff, reloff, absoff);
990             *tfsoff += 8;
991         }
992
993         /* FIXME: take alignment into account */
994         offset += type_memsize(ft, &align);
995     }
996 }
997
998 static int write_no_repeat_pointer_descriptions(
999     FILE *file, type_t *type,
1000     size_t *offset_in_memory, size_t *offset_in_buffer,
1001     unsigned int *typestring_offset)
1002 {
1003     int written = 0;
1004     unsigned int align;
1005
1006     if (is_ptr(type))
1007     {
1008         print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1009         print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1010
1011         /* pointer instance */
1012         print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1013         print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1014         *typestring_offset += 6;
1015
1016         if (processed(type->ref) || is_base_type(type->ref->type))
1017             write_pointer_tfs(file, type, typestring_offset);
1018         else
1019             error("write_pointer_description: type format string unknown\n");
1020
1021         align = 0;
1022         *offset_in_memory += type_memsize(type, &align);
1023         /* FIXME: is there a case where these two are different? */
1024         align = 0;
1025         *offset_in_buffer += type_memsize(type, &align);
1026
1027         return 1;
1028     }
1029
1030     if (is_non_complex_struct(type))
1031     {
1032         const var_t *v;
1033         LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1034             written += write_no_repeat_pointer_descriptions(
1035                 file, v->type,
1036                 offset_in_memory, offset_in_buffer, typestring_offset);
1037     }
1038     else
1039     {
1040         align = 0;
1041         *offset_in_memory += type_memsize(type, &align);
1042         /* FIXME: is there a case where these two are different? */
1043         align = 0;
1044         *offset_in_buffer += type_memsize(type, &align);
1045     }
1046
1047     return written;
1048 }
1049
1050 static int write_pointer_description_offsets(
1051     FILE *file, const attr_list_t *attrs, type_t *type,
1052     size_t *offset_in_memory, size_t *offset_in_buffer,
1053     unsigned int *typestring_offset)
1054 {
1055     int written = 0;
1056     unsigned int align;
1057
1058     if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1059     {
1060         if (offset_in_memory && offset_in_buffer)
1061         {
1062             /* pointer instance */
1063             /* FIXME: sometimes from end of structure, sometimes from beginning */
1064             print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1065             print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1066
1067             align = 0;
1068             *offset_in_memory += type_memsize(type, &align);
1069             /* FIXME: is there a case where these two are different? */
1070             align = 0;
1071             *offset_in_buffer += type_memsize(type, &align);
1072         }
1073         *typestring_offset += 4;
1074
1075         if (processed(type->ref) || is_base_type(type->ref->type))
1076             write_pointer_tfs(file, type, typestring_offset);
1077         else
1078             error("write_pointer_description_offsets: type format string unknown\n");
1079
1080         return 1;
1081     }
1082
1083     if (is_array(type))
1084     {
1085         return write_pointer_description_offsets(
1086             file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1087                                                  typestring_offset);
1088     }
1089     else if (is_non_complex_struct(type))
1090     {
1091         /* otherwise search for interesting fields to parse */
1092         const var_t *v;
1093         LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1094         {
1095             written += write_pointer_description_offsets(
1096                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1097                 typestring_offset);
1098         }
1099     }
1100     else
1101     {
1102         align = 0;
1103         if (offset_in_memory)
1104             *offset_in_memory += type_memsize(type, &align);
1105         /* FIXME: is there a case where these two are different? */
1106         align = 0;
1107         if (offset_in_buffer)
1108             *offset_in_buffer += type_memsize(type, &align);
1109     }
1110
1111     return written;
1112 }
1113
1114 /* Note: if file is NULL return value is number of pointers to write, else
1115  * it is the number of type format characters written */
1116 static int write_fixed_array_pointer_descriptions(
1117     FILE *file, const attr_list_t *attrs, type_t *type,
1118     size_t *offset_in_memory, size_t *offset_in_buffer,
1119     unsigned int *typestring_offset)
1120 {
1121     unsigned int align;
1122     int pointer_count = 0;
1123
1124     if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
1125     {
1126         unsigned int temp = 0;
1127         /* unfortunately, this needs to be done in two passes to avoid
1128          * writing out redundant FC_FIXED_REPEAT descriptions */
1129         pointer_count = write_pointer_description_offsets(
1130             NULL, attrs, type->ref, NULL, NULL, &temp);
1131         if (pointer_count > 0)
1132         {
1133             unsigned int increment_size;
1134             size_t offset_of_array_pointer_mem = 0;
1135             size_t offset_of_array_pointer_buf = 0;
1136
1137             align = 0;
1138             increment_size = type_memsize(type->ref, &align);
1139
1140             print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1141             print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1142             print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
1143             print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1144             print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1145             print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1146             *typestring_offset += 10;
1147
1148             pointer_count = write_pointer_description_offsets(
1149                 file, attrs, type, &offset_of_array_pointer_mem,
1150                 &offset_of_array_pointer_buf, typestring_offset);
1151         }
1152     }
1153     else if (is_struct(type->type))
1154     {
1155         const var_t *v;
1156         LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1157         {
1158             pointer_count += write_fixed_array_pointer_descriptions(
1159                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1160                 typestring_offset);
1161         }
1162     }
1163     else
1164     {
1165         align = 0;
1166         if (offset_in_memory)
1167             *offset_in_memory += type_memsize(type, &align);
1168         /* FIXME: is there a case where these two are different? */
1169         align = 0;
1170         if (offset_in_buffer)
1171             *offset_in_buffer += type_memsize(type, &align);
1172     }
1173
1174     return pointer_count;
1175 }
1176
1177 /* Note: if file is NULL return value is number of pointers to write, else
1178  * it is the number of type format characters written */
1179 static int write_conformant_array_pointer_descriptions(
1180     FILE *file, const attr_list_t *attrs, type_t *type,
1181     size_t *offset_in_memory, size_t *offset_in_buffer,
1182     unsigned int *typestring_offset)
1183 {
1184     unsigned int align;
1185     int pointer_count = 0;
1186
1187     if (is_conformant_array(type) && !type->length_is)
1188     {
1189         unsigned int temp = 0;
1190         /* unfortunately, this needs to be done in two passes to avoid
1191          * writing out redundant FC_VARIABLE_REPEAT descriptions */
1192         pointer_count = write_pointer_description_offsets(
1193             NULL, attrs, type->ref, NULL, NULL, &temp);
1194         if (pointer_count > 0)
1195         {
1196             unsigned int increment_size;
1197             size_t offset_of_array_pointer_mem = 0;
1198             size_t offset_of_array_pointer_buf = 0;
1199
1200             align = 0;
1201             increment_size = type_memsize(type->ref, &align);
1202
1203             if (increment_size > USHRT_MAX)
1204                 error("array size of %u bytes is too large\n", increment_size);
1205
1206             print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1207             print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1208             print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1209             print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1210             print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1211             *typestring_offset += 8;
1212
1213             pointer_count = write_pointer_description_offsets(
1214                 file, attrs, type->ref, &offset_of_array_pointer_mem,
1215                 &offset_of_array_pointer_buf, typestring_offset);
1216         }
1217     }
1218     else if (is_struct(type->type))
1219     {
1220         const var_t *v;
1221         LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1222         {
1223             pointer_count += write_conformant_array_pointer_descriptions(
1224                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1225                 typestring_offset);
1226         }
1227     }
1228     else
1229     {
1230         align = 0;
1231         if (offset_in_memory)
1232             *offset_in_memory += type_memsize(type, &align);
1233         /* FIXME: is there a case where these two are different? */
1234         align = 0;
1235         if (offset_in_buffer)
1236             *offset_in_buffer += type_memsize(type, &align);
1237     }
1238
1239     return pointer_count;
1240 }
1241
1242 /* Note: if file is NULL return value is number of pointers to write, else
1243  * it is the number of type format characters written */
1244 static int write_varying_array_pointer_descriptions(
1245     FILE *file, const attr_list_t *attrs, type_t *type,
1246     size_t *offset_in_memory, size_t *offset_in_buffer,
1247     unsigned int *typestring_offset)
1248 {
1249     unsigned int align;
1250     int pointer_count = 0;
1251
1252     /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1253
1254     if (is_array(type) && type->length_is)
1255     {
1256         unsigned int temp = 0;
1257         /* unfortunately, this needs to be done in two passes to avoid
1258          * writing out redundant FC_VARIABLE_REPEAT descriptions */
1259         pointer_count = write_pointer_description_offsets(
1260             NULL, attrs, type->ref, NULL, NULL, &temp);
1261         if (pointer_count > 0)
1262         {
1263             unsigned int increment_size;
1264             size_t offset_of_array_pointer_mem = 0;
1265             size_t offset_of_array_pointer_buf = 0;
1266
1267             align = 0;
1268             increment_size = type_memsize(type->ref, &align);
1269
1270             if (increment_size > USHRT_MAX)
1271                 error("array size of %u bytes is too large\n", increment_size);
1272
1273             print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1274             print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1275             print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1276             print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1277             print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
1278             *typestring_offset += 8;
1279
1280             pointer_count = write_pointer_description_offsets(
1281                 file, attrs, type, &offset_of_array_pointer_mem,
1282                 &offset_of_array_pointer_buf, typestring_offset);
1283         }
1284     }
1285     else if (is_struct(type->type))
1286     {
1287         const var_t *v;
1288         LIST_FOR_EACH_ENTRY( v, type->fields, const var_t, entry )
1289         {
1290             pointer_count += write_varying_array_pointer_descriptions(
1291                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1292                 typestring_offset);
1293         }
1294     }
1295     else
1296     {
1297         align = 0;
1298         if (offset_in_memory)
1299             *offset_in_memory += type_memsize(type, &align);
1300         /* FIXME: is there a case where these two are different? */
1301         align = 0;
1302         if (offset_in_buffer)
1303             *offset_in_buffer += type_memsize(type, &align);
1304     }
1305
1306     return pointer_count;
1307 }
1308
1309 static void write_pointer_description(FILE *file, type_t *type,
1310                                       unsigned int *typestring_offset)
1311 {
1312     size_t offset_in_buffer;
1313     size_t offset_in_memory;
1314
1315     /* pass 1: search for single instance of a pointer (i.e. don't descend
1316      * into arrays) */
1317     offset_in_memory = 0;
1318     offset_in_buffer = 0;
1319     write_no_repeat_pointer_descriptions(
1320         file, type,
1321         &offset_in_memory, &offset_in_buffer, typestring_offset);
1322
1323     /* pass 2: search for pointers in fixed arrays */
1324     offset_in_memory = 0;
1325     offset_in_buffer = 0;
1326     write_fixed_array_pointer_descriptions(
1327         file, NULL, type,
1328         &offset_in_memory, &offset_in_buffer, typestring_offset);
1329
1330     /* pass 3: search for pointers in conformant only arrays (but don't descend
1331      * into conformant varying or varying arrays) */
1332     offset_in_memory = 0;
1333     offset_in_buffer = 0;
1334     write_conformant_array_pointer_descriptions(
1335         file, NULL, type,
1336         &offset_in_memory, &offset_in_buffer, typestring_offset);
1337
1338    /* pass 4: search for pointers in varying arrays */
1339     offset_in_memory = 0;
1340     offset_in_buffer = 0;
1341     write_varying_array_pointer_descriptions(
1342             file, NULL, type,
1343             &offset_in_memory, &offset_in_buffer, typestring_offset);
1344 }
1345
1346 static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1347                                const type_t *type,
1348                                const char *name, unsigned int *typestring_offset)
1349 {
1350     size_t start_offset = *typestring_offset;
1351     unsigned char flags = 0;
1352     int pointer_type;
1353     unsigned char rtype;
1354
1355     if (is_ptr(type))
1356     {
1357         pointer_type = type->type;
1358         type = type->ref;
1359     }
1360     else
1361         pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1362
1363     if (!pointer_type)
1364         pointer_type = RPC_FC_RP;
1365
1366     if (!get_attrp(attrs, ATTR_SIZEIS))
1367         flags |= RPC_FC_P_SIMPLEPOINTER;
1368
1369     rtype = type->type;
1370
1371     if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1372     {
1373         error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1374         return start_offset;
1375     }
1376
1377     print_file(file, 2,"0x%x, 0x%x,    /* %s%s */\n",
1378                pointer_type, flags,
1379                pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"),
1380                (flags & RPC_FC_P_SIMPLEPOINTER) ? " [simple_pointer]" : "");
1381     *typestring_offset += 2;
1382
1383     if (!(flags & RPC_FC_P_SIMPLEPOINTER))
1384     {
1385         print_file(file, 2, "NdrFcShort(0x2),\n");
1386         *typestring_offset += 2;
1387     }
1388
1389     if (type->declarray && !is_conformant_array(type))
1390     {
1391         /* FIXME: multi-dimensional array */
1392         if (0xffffuL < type->dim)
1393             error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1394                   name, 0xffffu, type->dim - 0xffffu);
1395
1396         if (rtype == RPC_FC_CHAR)
1397             WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1398         else
1399             WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1400         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1401         *typestring_offset += 2;
1402
1403         print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1404         *typestring_offset += 2;
1405
1406         return start_offset;
1407     }
1408     else if (type->size_is)
1409     {
1410         unsigned int align = 0;
1411
1412         if (rtype == RPC_FC_CHAR)
1413             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1414         else
1415             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1416         print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1417         *typestring_offset += 2;
1418
1419         *typestring_offset += write_conf_or_var_desc(
1420             file, current_func, current_structure,
1421             (type->declarray && current_structure
1422              ? type_memsize(current_structure, &align)
1423              : 0),
1424             type->size_is);
1425
1426         return start_offset;
1427     }
1428     else
1429     {
1430         if (rtype == RPC_FC_CHAR)
1431             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1432         else
1433             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1434         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1435         *typestring_offset += 2;
1436
1437         return start_offset;
1438     }
1439 }
1440
1441 static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1442                               const char *name, unsigned int *typestring_offset)
1443 {
1444     const expr_t *length_is = type->length_is;
1445     const expr_t *size_is = type->size_is;
1446     unsigned int align = 0;
1447     size_t size;
1448     size_t start_offset;
1449     int has_pointer;
1450     int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1451     if (!pointer_type)
1452         pointer_type = RPC_FC_RP;
1453
1454     has_pointer = FALSE;
1455     if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1456         has_pointer = TRUE;
1457
1458     size = type_memsize(type, &align);
1459     if (size == 0)              /* conformant array */
1460         size = type_memsize(type->ref, &align);
1461
1462     start_offset = *typestring_offset;
1463     update_tfsoff(type, start_offset, file);
1464     print_file(file, 0, "/* %lu */\n", start_offset);
1465     print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
1466     print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1467     *typestring_offset += 2;
1468
1469     align = 0;
1470     if (type->type != RPC_FC_BOGUS_ARRAY)
1471     {
1472         unsigned char tc = type->type;
1473         unsigned int baseoff
1474             = type->declarray && current_structure
1475             ? type_memsize(current_structure, &align)
1476             : 0;
1477
1478         if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
1479         {
1480             print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
1481             *typestring_offset += 4;
1482         }
1483         else
1484         {
1485             print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1486             *typestring_offset += 2;
1487         }
1488
1489         if (is_conformant_array(type))
1490             *typestring_offset
1491                 += write_conf_or_var_desc(file, current_func, current_structure,
1492                                           baseoff, size_is);
1493
1494         if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
1495         {
1496             unsigned int elalign = 0;
1497             size_t elsize = type_memsize(type->ref, &elalign);
1498
1499             if (type->type == RPC_FC_LGVARRAY)
1500             {
1501                 print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
1502                 *typestring_offset += 4;
1503             }
1504             else
1505             {
1506                 print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
1507                 *typestring_offset += 2;
1508             }
1509
1510             print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
1511             *typestring_offset += 2;
1512         }
1513
1514         if (length_is)
1515             *typestring_offset
1516                 += write_conf_or_var_desc(file, current_func, current_structure,
1517                                           baseoff, length_is);
1518
1519         if (has_pointer)
1520         {
1521             print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1522             print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1523             *typestring_offset += 2;
1524             write_pointer_description(file, type, typestring_offset);
1525             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1526             *typestring_offset += 1;
1527         }
1528
1529         write_member_type(file, type->ref, NULL, NULL, typestring_offset);
1530         write_end(file, typestring_offset);
1531     }
1532     else
1533         error("%s: complex arrays unimplemented\n", name);
1534
1535     return start_offset;
1536 }
1537
1538 static const var_t *find_array_or_string_in_struct(const type_t *type)
1539 {
1540     const var_t *last_field = LIST_ENTRY( list_tail(type->fields), const var_t, entry );
1541     const type_t *ft = last_field->type;
1542
1543     if (ft->declarray && is_conformant_array(ft))
1544         return last_field;
1545
1546     if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1547         return find_array_or_string_in_struct(last_field->type);
1548     else
1549         return NULL;
1550 }
1551
1552 static void write_struct_members(FILE *file, const type_t *type,
1553                                  unsigned int *corroff, unsigned int *typestring_offset)
1554 {
1555     const var_t *field;
1556
1557     if (type->fields) LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
1558     {
1559         type_t *ft = field->type;
1560         if (!ft->declarray || !is_conformant_array(ft))
1561             write_member_type(file, ft, field, corroff, typestring_offset);
1562     }
1563
1564     write_end(file, typestring_offset);
1565 }
1566
1567 static size_t write_struct_tfs(FILE *file, type_t *type,
1568                                const char *name, unsigned int *tfsoff)
1569 {
1570     const type_t *save_current_structure = current_structure;
1571     unsigned int total_size;
1572     const var_t *array;
1573     size_t start_offset;
1574     size_t array_offset;
1575     int has_pointers = 0;
1576     unsigned int align = 0;
1577     unsigned int corroff;
1578     var_t *f;
1579
1580     guard_rec(type);
1581     current_structure = type;
1582
1583     total_size = type_memsize(type, &align);
1584     if (total_size > USHRT_MAX)
1585         error("structure size for %s exceeds %d bytes by %d bytes\n",
1586               name, USHRT_MAX, total_size - USHRT_MAX);
1587
1588     if (type->fields) LIST_FOR_EACH_ENTRY(f, type->fields, var_t, entry)
1589         has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
1590                                              FALSE, tfsoff);
1591
1592     array = find_array_or_string_in_struct(type);
1593     if (array && !processed(array->type))
1594         array_offset
1595             = is_attr(array->attrs, ATTR_STRING)
1596             ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1597             : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1598
1599     corroff = *tfsoff;
1600     write_descriptors(file, type, tfsoff);
1601
1602     start_offset = *tfsoff;
1603     update_tfsoff(type, start_offset, file);
1604     print_file(file, 0, "/* %d */\n", start_offset);
1605     print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1606     print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1607     print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
1608     *tfsoff += 4;
1609
1610     if (array)
1611     {
1612         unsigned int absoff = array->type->typestring_offset;
1613         short reloff = absoff - *tfsoff;
1614         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1615                    reloff, reloff, absoff);
1616         *tfsoff += 2;
1617     }
1618     else if (type->type == RPC_FC_BOGUS_STRUCT)
1619     {
1620         print_file(file, 2, "NdrFcShort(0x0),\n");
1621         *tfsoff += 2;
1622     }
1623
1624     if (type->type == RPC_FC_BOGUS_STRUCT)
1625     {
1626
1627         print_file(file, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1628         *tfsoff += 2;
1629     }
1630     else if ((type->type == RPC_FC_PSTRUCT) ||
1631              (type->type == RPC_FC_CPSTRUCT) ||
1632              (type->type == RPC_FC_CVSTRUCT && has_pointers))
1633     {
1634         print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1635         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1636         *tfsoff += 2;
1637         write_pointer_description(file, type, tfsoff);
1638         print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1639         *tfsoff += 1;
1640     }
1641
1642     write_struct_members(file, type, &corroff, tfsoff);
1643
1644     current_structure = save_current_structure;
1645     return start_offset;
1646 }
1647
1648 static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
1649                                      unsigned char flags, size_t offset,
1650                                      unsigned int *typeformat_offset)
1651 {
1652     size_t start_offset = *typeformat_offset;
1653     short reloff = offset - (*typeformat_offset + 2);
1654     int in_attr, out_attr;
1655     in_attr = is_attr(attrs, ATTR_IN);
1656     out_attr = is_attr(attrs, ATTR_OUT);
1657     if (!in_attr && !out_attr) in_attr = 1;
1658
1659     if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1660         flags |= 0x04;
1661
1662     print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1663                pointer_type,
1664                flags,
1665                string_of_type(pointer_type));
1666     if (file)
1667     {
1668         if (flags & 0x04)
1669             fprintf(file, " [allocated_on_stack]");
1670         if (flags & 0x10)
1671             fprintf(file, " [pointer_deref]");
1672         fprintf(file, " */\n");
1673     }
1674
1675     print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1676     *typeformat_offset += 4;
1677
1678     return start_offset;
1679 }
1680
1681 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1682 {
1683     if (t == NULL)
1684     {
1685         print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
1686     }
1687     else if (is_base_type(t->type))
1688     {
1689         print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1690                    t->type, string_of_type(t->type));
1691     }
1692     else if (t->typestring_offset)
1693     {
1694         short reloff = t->typestring_offset - *tfsoff;
1695         print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1696                    reloff, reloff, t->typestring_offset);
1697     }
1698     else
1699         error("write_branch_type: type unimplemented (0x%x)\n", t->type);
1700
1701     *tfsoff += 2;
1702 }
1703
1704 static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1705 {
1706     unsigned int align = 0;
1707     unsigned int start_offset;
1708     size_t size = type_memsize(type, &align);
1709     var_list_t *fields;
1710     size_t nbranch = 0;
1711     type_t *deftype = NULL;
1712     short nodeftype = 0xffff;
1713     var_t *f;
1714
1715     guard_rec(type);
1716
1717     if (type->type == RPC_FC_ENCAPSULATED_UNION)
1718     {
1719         const var_t *uv = LIST_ENTRY(list_tail(type->fields), const var_t, entry);
1720         fields = uv->type->fields;
1721     }
1722     else
1723         fields = type->fields;
1724
1725     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1726     {
1727         expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1728         if (cases)
1729             nbranch += list_count(cases);
1730         if (f->type)
1731             write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
1732     }
1733
1734     start_offset = *tfsoff;
1735     update_tfsoff(type, start_offset, file);
1736     print_file(file, 0, "/* %d */\n", start_offset);
1737     if (type->type == RPC_FC_ENCAPSULATED_UNION)
1738     {
1739         const var_t *sv = LIST_ENTRY(list_head(type->fields), const var_t, entry);
1740         const type_t *st = sv->type;
1741
1742         switch (st->type)
1743         {
1744         case RPC_FC_CHAR:
1745         case RPC_FC_SMALL:
1746         case RPC_FC_USMALL:
1747         case RPC_FC_SHORT:
1748         case RPC_FC_USHORT:
1749         case RPC_FC_LONG:
1750         case RPC_FC_ULONG:
1751         case RPC_FC_ENUM16:
1752         case RPC_FC_ENUM32:
1753             print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
1754             print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
1755                        0x40 | st->type, string_of_type(st->type));
1756             *tfsoff += 2;
1757             break;
1758         default:
1759             error("union switch type must be an integer, char, or enum\n");
1760         }
1761     }
1762     print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
1763     print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
1764     *tfsoff += 4;
1765
1766     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
1767     {
1768         type_t *ft = f->type;
1769         expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
1770         int deflt = is_attr(f->attrs, ATTR_DEFAULT);
1771         expr_t *c;
1772
1773         if (cases == NULL && !deflt)
1774             error("union field %s with neither case nor default attribute\n", f->name);
1775
1776         if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
1777         {
1778             /* MIDL doesn't check for duplicate cases, even though that seems
1779                like a reasonable thing to do, it just dumps them to the TFS
1780                like we're going to do here.  */
1781             print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
1782             *tfsoff += 4;
1783             write_branch_type(file, ft, tfsoff);
1784         }
1785
1786         /* MIDL allows multiple default branches, even though that seems
1787            illogical, it just chooses the last one, which is what we will
1788            do.  */
1789         if (deflt)
1790         {
1791             deftype = ft;
1792             nodeftype = 0;
1793         }
1794     }
1795
1796     if (deftype)
1797     {
1798         write_branch_type(file, deftype, tfsoff);
1799     }
1800     else
1801     {
1802         print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
1803         *tfsoff += 2;
1804     }
1805
1806     return start_offset;
1807 }
1808
1809 static size_t write_ip_tfs(FILE *file, const func_t *func, const attr_list_t *attrs,
1810                            type_t *type, unsigned int *typeformat_offset)
1811 {
1812     size_t i;
1813     size_t start_offset = *typeformat_offset;
1814     const var_t *iid = get_attrp(attrs, ATTR_IIDIS);
1815
1816     if (iid)
1817     {
1818         expr_t expr;
1819
1820         expr.type = EXPR_IDENTIFIER;
1821         expr.ref  = NULL;
1822         expr.u.sval = iid->name;
1823         expr.is_const = FALSE;
1824         print_file(file, 2, "0x2f,  /* FC_IP */\n");
1825         print_file(file, 2, "0x5c,  /* FC_PAD */\n");
1826         *typeformat_offset += write_conf_or_var_desc(file, func, NULL, 0, &expr) + 2;
1827     }
1828     else
1829     {
1830         const type_t *base = is_ptr(type) ? type->ref : type;
1831         const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
1832
1833         if (! uuid)
1834             error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
1835
1836         update_tfsoff(type, start_offset, file);
1837         print_file(file, 0, "/* %d */\n", start_offset);
1838         print_file(file, 2, "0x2f,\t/* FC_IP */\n");
1839         print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1840         print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
1841         print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
1842         print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
1843         for (i = 0; i < 8; ++i)
1844             print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
1845
1846         if (file)
1847             fprintf(file, "\n");
1848
1849         *typeformat_offset += 18;
1850     }
1851     return start_offset;
1852 }
1853
1854 static int get_ptr_attr(const type_t *t, int def_type)
1855 {
1856     while (TRUE)
1857     {
1858         int ptr_attr = get_attrv(t->attrs, ATTR_POINTERTYPE);
1859         if (ptr_attr)
1860             return ptr_attr;
1861         if (t->kind != TKIND_ALIAS)
1862             return def_type;
1863         t = t->orig;
1864     }
1865 }
1866
1867 static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
1868                                          type_t *type, const var_t *var,
1869                                          unsigned int *typeformat_offset)
1870 {
1871     int pointer_type;
1872     size_t offset;
1873
1874     if (is_user_type(type))
1875     {
1876         write_user_tfs(file, type, typeformat_offset);
1877         return type->typestring_offset;
1878     }
1879
1880     if (type == var->type)      /* top-level pointers */
1881     {
1882         int pointer_attr = get_attrv(var->attrs, ATTR_POINTERTYPE);
1883         if (pointer_attr != 0 && !is_ptr(type) && !is_array(type))
1884             error("'%s': pointer attribute applied to non-pointer type\n", var->name);
1885
1886         if (pointer_attr == 0)
1887             pointer_attr = get_ptr_attr(type, RPC_FC_RP);
1888
1889         pointer_type = pointer_attr;
1890     }
1891     else
1892         pointer_type = get_ptr_attr(type, RPC_FC_UP);
1893
1894     if ((last_ptr(type) || last_array(type)) && is_ptrchain_attr(var, ATTR_STRING))
1895         return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
1896
1897     if (is_array(type))
1898         return write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
1899
1900     if (!is_ptr(type))
1901     {
1902         /* basic types don't need a type format string */
1903         if (is_base_type(type->type))
1904             return 0;
1905
1906         switch (type->type)
1907         {
1908         case RPC_FC_STRUCT:
1909         case RPC_FC_PSTRUCT:
1910         case RPC_FC_CSTRUCT:
1911         case RPC_FC_CPSTRUCT:
1912         case RPC_FC_CVSTRUCT:
1913         case RPC_FC_BOGUS_STRUCT:
1914             return write_struct_tfs(file, type, var->name, typeformat_offset);
1915         case RPC_FC_ENCAPSULATED_UNION:
1916         case RPC_FC_NON_ENCAPSULATED_UNION:
1917             return write_union_tfs(file, type, typeformat_offset);
1918         case RPC_FC_IGNORE:
1919         case RPC_FC_BIND_PRIMITIVE:
1920             /* nothing to do */
1921             return 0;
1922         default:
1923             error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
1924         }
1925     }
1926     else if (last_ptr(type))
1927     {
1928         size_t start_offset = *typeformat_offset;
1929         int in_attr = is_attr(var->attrs, ATTR_IN);
1930         int out_attr = is_attr(var->attrs, ATTR_OUT);
1931         const type_t *base = type->ref;
1932
1933         if (base->type == RPC_FC_IP)
1934         {
1935             return write_ip_tfs(file, func, var->attrs, type, typeformat_offset);
1936         }
1937
1938         /* special case for pointers to base types */
1939         if (is_base_type(base->type))
1940         {
1941             print_file(file, indent, "0x%x, 0x%x,    /* %s %s[simple_pointer] */\n",
1942                        pointer_type, (!in_attr && out_attr) ? 0x0C : 0x08,
1943                        string_of_type(pointer_type),
1944                        (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
1945             print_file(file, indent, "0x%02x,    /* %s */\n", base->type, string_of_type(base->type));
1946             print_file(file, indent, "0x5c,          /* FC_PAD */\n");
1947             *typeformat_offset += 4;
1948             return start_offset;
1949         }
1950     }
1951
1952     assert(is_ptr(type));
1953
1954     offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
1955     if (file)
1956         fprintf(file, "/* %2u */\n", *typeformat_offset);
1957     return write_pointer_only_tfs(file, var->attrs, pointer_type,
1958                            !last_ptr(type) ? 0x10 : 0,
1959                            offset, typeformat_offset);
1960 }
1961
1962 static void set_tfswrite(type_t *type, int val)
1963 {
1964     while (type->tfswrite != val)
1965     {
1966         type_t *utype = get_user_type(type, NULL);
1967
1968         type->tfswrite = val;
1969
1970         if (utype)
1971             set_tfswrite(utype, val);
1972
1973         if (type->kind == TKIND_ALIAS)
1974             type = type->orig;
1975         else if (is_ptr(type) || is_array(type))
1976             type = type->ref;
1977         else
1978         {
1979             if (type->fields)
1980             {
1981                 var_t *v;
1982                 LIST_FOR_EACH_ENTRY( v, type->fields, var_t, entry )
1983                     if (v->type)
1984                         set_tfswrite(v->type, val);
1985             }
1986
1987             return;
1988         }
1989     }
1990 }
1991
1992 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
1993                                 const char *name, int write_ptr, unsigned int *tfsoff)
1994 {
1995     int retmask = 0;
1996
1997     if (is_user_type(type))
1998     {
1999         write_user_tfs(file, type, tfsoff);
2000     }
2001     else if (is_ptr(type))
2002     {
2003         type_t *ref = type->ref;
2004
2005         if (ref->type == RPC_FC_IP)
2006         {
2007             write_ip_tfs(file, NULL, attrs, type, tfsoff);
2008         }
2009         else
2010         {
2011             if (!processed(ref) && !is_base_type(ref->type))
2012                 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2013
2014             if (write_ptr)
2015                 write_pointer_tfs(file, type, tfsoff);
2016
2017             retmask |= 1;
2018         }
2019     }
2020     else if (type->declarray && is_conformant_array(type))
2021         ;    /* conformant arrays and strings are handled specially */
2022     else if (is_array(type))
2023     {
2024         write_array_tfs(file, attrs, type, name, tfsoff);
2025     }
2026     else if (is_struct(type->type))
2027     {
2028         if (!processed(type))
2029             write_struct_tfs(file, type, name, tfsoff);
2030     }
2031     else if (is_union(type->type))
2032     {
2033         if (!processed(type))
2034             write_union_tfs(file, type, tfsoff);
2035     }
2036     else if (!is_base_type(type->type))
2037         error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2038               name, type->type);
2039
2040     return retmask;
2041 }
2042
2043 static void set_all_tfswrite(const ifref_list_t *ifaces, int val)
2044 {
2045     const ifref_t * iface;
2046     const func_t *func;
2047     const var_t *var;
2048
2049     if (ifaces)
2050         LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2051             if (iface->iface->funcs)
2052                 LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2053                     if (func->args)
2054                         LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2055                             set_tfswrite(var->type, val);
2056 }
2057
2058 static size_t process_tfs(FILE *file, const ifref_list_t *ifaces, int for_objects)
2059 {
2060     const var_t *var;
2061     const ifref_t *iface;
2062     unsigned int typeformat_offset = 2;
2063
2064     if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2065     {
2066         if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2067             continue;
2068
2069         if (iface->iface->funcs)
2070         {
2071             const func_t *func;
2072             LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2073             {
2074                 if (is_local(func->def->attrs)) continue;
2075
2076                 current_func = func;
2077                 if (func->args)
2078                     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2079                         update_tfsoff(
2080                             var->type,
2081                             write_typeformatstring_var(
2082                                 file, 2, func, var->type, var,
2083                                 &typeformat_offset),
2084                             file);
2085             }
2086         }
2087     }
2088
2089     return typeformat_offset + 1;
2090 }
2091
2092
2093 void write_typeformatstring(FILE *file, const ifref_list_t *ifaces, int for_objects)
2094 {
2095     int indent = 0;
2096
2097     print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2098     print_file(file, indent, "{\n");
2099     indent++;
2100     print_file(file, indent, "0,\n");
2101     print_file(file, indent, "{\n");
2102     indent++;
2103     print_file(file, indent, "NdrFcShort(0x0),\n");
2104
2105     set_all_tfswrite(ifaces, TRUE);
2106     process_tfs(file, ifaces, for_objects);
2107
2108     print_file(file, indent, "0x0\n");
2109     indent--;
2110     print_file(file, indent, "}\n");
2111     indent--;
2112     print_file(file, indent, "};\n");
2113     print_file(file, indent, "\n");
2114 }
2115
2116 static unsigned int get_required_buffer_size_type(
2117     const type_t *type, const char *name, unsigned int *alignment)
2118 {
2119     size_t size = 0;
2120
2121     *alignment = 0;
2122     if (is_user_type(type))
2123     {
2124         const char *uname;
2125         const type_t *utype = get_user_type(type, &uname);
2126         size = get_required_buffer_size_type(utype, uname, alignment);
2127     }
2128     else if (!is_ptr(type))
2129     {
2130         switch (type->type)
2131         {
2132         case RPC_FC_BYTE:
2133         case RPC_FC_CHAR:
2134         case RPC_FC_USMALL:
2135         case RPC_FC_SMALL:
2136             *alignment = 4;
2137             size = 1;
2138             break;
2139
2140         case RPC_FC_WCHAR:
2141         case RPC_FC_USHORT:
2142         case RPC_FC_SHORT:
2143         case RPC_FC_ENUM16:
2144             *alignment = 4;
2145             size = 2;
2146             break;
2147
2148         case RPC_FC_ULONG:
2149         case RPC_FC_LONG:
2150         case RPC_FC_ENUM32:
2151         case RPC_FC_FLOAT:
2152         case RPC_FC_ERROR_STATUS_T:
2153             *alignment = 4;
2154             size = 4;
2155             break;
2156
2157         case RPC_FC_HYPER:
2158         case RPC_FC_DOUBLE:
2159             *alignment = 8;
2160             size = 8;
2161             break;
2162
2163         case RPC_FC_IGNORE:
2164         case RPC_FC_BIND_PRIMITIVE:
2165             return 0;
2166
2167         case RPC_FC_STRUCT:
2168         case RPC_FC_PSTRUCT:
2169         {
2170             const var_t *field;
2171             if (!type->fields) return 0;
2172             LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2173             {
2174                 unsigned int alignment;
2175                 size += get_required_buffer_size_type(field->type, field->name,
2176                                                       &alignment);
2177             }
2178             break;
2179         }
2180
2181         case RPC_FC_RP:
2182             if (is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT)
2183                 size = get_required_buffer_size_type( type->ref, name, alignment );
2184             break;
2185
2186         case RPC_FC_SMFARRAY:
2187         case RPC_FC_LGFARRAY:
2188             size = type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2189             break;
2190
2191         case RPC_FC_SMVARRAY:
2192         case RPC_FC_LGVARRAY:
2193             get_required_buffer_size_type(type->ref, name, alignment);
2194             size = 0;
2195             break;
2196
2197         case RPC_FC_CARRAY:
2198         case RPC_FC_CVARRAY:
2199             get_required_buffer_size_type(type->ref, name, alignment);
2200             size = sizeof(void *);
2201             break;
2202
2203         default:
2204             error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
2205             return 0;
2206         }
2207     }
2208     return size;
2209 }
2210
2211 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2212 {
2213     int in_attr = is_attr(var->attrs, ATTR_IN);
2214     int out_attr = is_attr(var->attrs, ATTR_OUT);
2215
2216     if (!in_attr && !out_attr)
2217         in_attr = 1;
2218
2219     *alignment = 0;
2220
2221     if (pass == PASS_OUT)
2222     {
2223         if (out_attr && is_ptr(var->type))
2224         {
2225             type_t *type = var->type;
2226
2227             if (type->type == RPC_FC_STRUCT)
2228             {
2229                 const var_t *field;
2230                 unsigned int size = 36;
2231
2232                 if (!type->fields) return size;
2233                 LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2234                 {
2235                     unsigned int align;
2236                     size += get_required_buffer_size_type(
2237                         field->type, field->name, &align);
2238                 }
2239                 return size;
2240             }
2241         }
2242         return 0;
2243     }
2244     else
2245     {
2246         if ((!out_attr || in_attr) && !var->type->size_is
2247             && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2248         {
2249             if (is_ptr(var->type))
2250             {
2251                 type_t *type = var->type;
2252
2253                 if (is_base_type(type->type))
2254                 {
2255                     return 25;
2256                 }
2257                 else if (type->type == RPC_FC_STRUCT)
2258                 {
2259                     unsigned int size = 36;
2260                     const var_t *field;
2261
2262                     if (!type->fields) return size;
2263                     LIST_FOR_EACH_ENTRY( field, type->fields, const var_t, entry )
2264                     {
2265                         unsigned int align;
2266                         size += get_required_buffer_size_type(
2267                             field->type, field->name, &align);
2268                     }
2269                     return size;
2270                 }
2271             }
2272         }
2273
2274         return get_required_buffer_size_type(var->type, var->name, alignment);
2275     }
2276 }
2277
2278 static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
2279 {
2280     const var_t *var;
2281     unsigned int total_size = 0, alignment;
2282
2283     if (func->args)
2284     {
2285         LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2286         {
2287             total_size += get_required_buffer_size(var, &alignment, pass);
2288             total_size += alignment;
2289         }
2290     }
2291
2292     if (pass == PASS_OUT && !is_void(func->def->type))
2293     {
2294         total_size += get_required_buffer_size(func->def, &alignment, PASS_RETURN);
2295         total_size += alignment;
2296     }
2297     return total_size;
2298 }
2299
2300 static void print_phase_function(FILE *file, int indent, const char *type,
2301                                  enum remoting_phase phase,
2302                                  const var_t *var, unsigned int type_offset)
2303 {
2304     const char *function;
2305     switch (phase)
2306     {
2307     case PHASE_BUFFERSIZE:
2308         function = "BufferSize";
2309         break;
2310     case PHASE_MARSHAL:
2311         function = "Marshall";
2312         break;
2313     case PHASE_UNMARSHAL:
2314         function = "Unmarshall";
2315         break;
2316     case PHASE_FREE:
2317         function = "Free";
2318         break;
2319     default:
2320         assert(0);
2321         return;
2322     }
2323
2324     print_file(file, indent, "Ndr%s%s(\n", type, function);
2325     indent++;
2326     print_file(file, indent, "&_StubMsg,\n");
2327     print_file(file, indent, "%s%s%s%s,\n",
2328                (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2329                (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2330                (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2331                var->name);
2332     print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2333                type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2334     if (phase == PHASE_UNMARSHAL)
2335         print_file(file, indent, "0);\n");
2336     indent--;
2337 }
2338
2339 void print_phase_basetype(FILE *file, int indent, enum remoting_phase phase,
2340                           enum pass pass, const var_t *var,
2341                           const char *varname)
2342 {
2343     type_t *type = var->type;
2344     unsigned int size;
2345     unsigned int alignment = 0;
2346     unsigned char rtype;
2347
2348     /* no work to do for other phases, buffer sizing is done elsewhere */
2349     if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2350         return;
2351
2352     rtype = is_ptr(type) ? type->ref->type : type->type;
2353
2354     switch (rtype)
2355     {
2356         case RPC_FC_BYTE:
2357         case RPC_FC_CHAR:
2358         case RPC_FC_SMALL:
2359         case RPC_FC_USMALL:
2360             size = 1;
2361             alignment = 1;
2362             break;
2363
2364         case RPC_FC_WCHAR:
2365         case RPC_FC_USHORT:
2366         case RPC_FC_SHORT:
2367         case RPC_FC_ENUM16:
2368             size = 2;
2369             alignment = 2;
2370             break;
2371
2372         case RPC_FC_ULONG:
2373         case RPC_FC_LONG:
2374         case RPC_FC_ENUM32:
2375         case RPC_FC_FLOAT:
2376         case RPC_FC_ERROR_STATUS_T:
2377             size = 4;
2378             alignment = 4;
2379             break;
2380
2381         case RPC_FC_HYPER:
2382         case RPC_FC_DOUBLE:
2383             size = 8;
2384             alignment = 8;
2385             break;
2386
2387         case RPC_FC_IGNORE:
2388         case RPC_FC_BIND_PRIMITIVE:
2389             /* no marshalling needed */
2390             return;
2391
2392         default:
2393             error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2394             size = 0;
2395     }
2396
2397     print_file(file, indent, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2398                 alignment - 1, alignment - 1);
2399
2400     if (phase == PHASE_MARSHAL)
2401     {
2402         print_file(file, indent, "*(");
2403         write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2404         if (is_ptr(type))
2405             fprintf(file, " *)_StubMsg.Buffer = *");
2406         else
2407             fprintf(file, " *)_StubMsg.Buffer = ");
2408         fprintf(file, varname);
2409         fprintf(file, ";\n");
2410     }
2411     else if (phase == PHASE_UNMARSHAL)
2412     {
2413         if (pass == PASS_IN || pass == PASS_RETURN)
2414             print_file(file, indent, "");
2415         else
2416             print_file(file, indent, "*");
2417         fprintf(file, varname);
2418         if (pass == PASS_IN && is_ptr(type))
2419             fprintf(file, " = (");
2420         else
2421             fprintf(file, " = *(");
2422         write_type(file, is_ptr(type) ? type->ref : type, FALSE, NULL);
2423         fprintf(file, " *)_StubMsg.Buffer;\n");
2424     }
2425
2426     print_file(file, indent, "_StubMsg.Buffer += sizeof(");
2427     write_type(file, var->type, FALSE, NULL);
2428     fprintf(file, ");\n");
2429 }
2430
2431 /* returns whether the MaxCount, Offset or ActualCount members need to be
2432  * filled in for the specified phase */
2433 static inline int is_size_needed_for_phase(enum remoting_phase phase)
2434 {
2435     return (phase != PHASE_UNMARSHAL);
2436 }
2437
2438 void write_remoting_arguments(FILE *file, int indent, const func_t *func,
2439                               enum pass pass, enum remoting_phase phase)
2440 {
2441     int in_attr, out_attr, pointer_type;
2442     const var_t *var;
2443
2444     if (!func->args)
2445         return;
2446
2447     if (phase == PHASE_BUFFERSIZE)
2448     {
2449         unsigned int size = get_function_buffer_size( func, pass );
2450         print_file(file, indent, "_StubMsg.BufferLength = %u;\n", size);
2451     }
2452
2453     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2454     {
2455         const type_t *type = var->type;
2456         unsigned char rtype;
2457         size_t start_offset = type->typestring_offset;
2458
2459         pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2460         if (!pointer_type)
2461             pointer_type = RPC_FC_RP;
2462
2463         in_attr = is_attr(var->attrs, ATTR_IN);
2464         out_attr = is_attr(var->attrs, ATTR_OUT);
2465         if (!in_attr && !out_attr)
2466             in_attr = 1;
2467
2468         switch (pass)
2469         {
2470         case PASS_IN:
2471             if (!in_attr) continue;
2472             break;
2473         case PASS_OUT:
2474             if (!out_attr) continue;
2475             break;
2476         case PASS_RETURN:
2477             break;
2478         }
2479
2480         rtype = type->type;
2481
2482         if (is_user_type(var->type))
2483         {
2484             print_phase_function(file, indent, "UserMarshal", phase, var, start_offset);
2485         }
2486         else if (is_string_type(var->attrs, var->type))
2487         {
2488             if (is_array(type) && !is_conformant_array(type))
2489                 print_phase_function(file, indent, "NonConformantString", phase, var, start_offset);
2490             else
2491             {
2492                 if (type->size_is && is_size_needed_for_phase(phase))
2493                 {
2494                     print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2495                     write_expr(file, type->size_is, 1);
2496                     fprintf(file, ";\n");
2497                 }
2498
2499                 if ((phase == PHASE_FREE) || (pointer_type == RPC_FC_UP))
2500                     print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2501                 else
2502                     print_phase_function(file, indent, "ConformantString", phase, var,
2503                                          start_offset + (type->size_is ? 4 : 2));
2504             }
2505         }
2506         else if (is_array(type))
2507         {
2508             unsigned char tc = type->type;
2509             const char *array_type;
2510
2511             if (tc == RPC_FC_SMFARRAY || tc == RPC_FC_LGFARRAY)
2512                 array_type = "FixedArray";
2513             else if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2514             {
2515                 if (is_size_needed_for_phase(phase))
2516                 {
2517                     print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2518                     print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2519                     write_expr(file, type->length_is, 1);
2520                     fprintf(file, ";\n\n");
2521                 }
2522                 array_type = "VaryingArray";
2523             }
2524             else if (tc == RPC_FC_CARRAY)
2525             {
2526                 if (is_size_needed_for_phase(phase) && phase != PHASE_FREE)
2527                 {
2528                     print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2529                     write_expr(file, type->size_is, 1);
2530                     fprintf(file, ";\n\n");
2531                 }
2532                 array_type = "ConformantArray";
2533             }
2534             else if (tc == RPC_FC_CVARRAY)
2535             {
2536                 if (is_size_needed_for_phase(phase))
2537                 {
2538                     print_file(file, indent, "_StubMsg.MaxCount = (unsigned long)");
2539                     write_expr(file, type->size_is, 1);
2540                     fprintf(file, ";\n");
2541                     print_file(file, indent, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2542                     print_file(file, indent, "_StubMsg.ActualCount = (unsigned long)");
2543                     write_expr(file, type->length_is, 1);
2544                     fprintf(file, ";\n\n");
2545                 }
2546                 array_type = "ConformantVaryingArray";
2547             }
2548             else
2549                 array_type = "ComplexArray";
2550
2551             if (!in_attr && phase == PHASE_FREE)
2552             {
2553                 print_file(file, indent, "if (%s)\n", var->name);
2554                 indent++;
2555                 print_file(file, indent, "_StubMsg.pfnFree(%s);\n", var->name);
2556             }
2557             else if (phase != PHASE_FREE)
2558             {
2559                 if (pointer_type == RPC_FC_UP)
2560                     print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2561                 else
2562                     print_phase_function(file, indent, array_type, phase, var, start_offset);
2563             }
2564         }
2565         else if (!is_ptr(var->type) && is_base_type(rtype))
2566         {
2567             print_phase_basetype(file, indent, phase, pass, var, var->name);
2568         }
2569         else if (!is_ptr(var->type))
2570         {
2571             switch (rtype)
2572             {
2573             case RPC_FC_STRUCT:
2574             case RPC_FC_PSTRUCT:
2575                 print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset);
2576                 break;
2577             case RPC_FC_CSTRUCT:
2578             case RPC_FC_CPSTRUCT:
2579                 print_phase_function(file, indent, "ConformantStruct", phase, var, start_offset);
2580                 break;
2581             case RPC_FC_CVSTRUCT:
2582                 print_phase_function(file, indent, "ConformantVaryingStruct", phase, var, start_offset);
2583                 break;
2584             case RPC_FC_BOGUS_STRUCT:
2585                 print_phase_function(file, indent, "ComplexStruct", phase, var, start_offset);
2586                 break;
2587             case RPC_FC_RP:
2588                 if (is_base_type( var->type->ref->type ))
2589                 {
2590                     print_phase_basetype(file, indent, phase, pass, var, var->name);
2591                 }
2592                 else if (var->type->ref->type == RPC_FC_STRUCT)
2593                 {
2594                     if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2595                         print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2596                 }
2597                 else
2598                 {
2599                     const var_t *iid;
2600                     if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2601                         print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2602                     print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2603                 }
2604                 break;
2605             default:
2606                 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
2607             }
2608         }
2609         else
2610         {
2611             if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
2612             {
2613                 print_phase_basetype(file, indent, phase, pass, var, var->name);
2614             }
2615             else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
2616             {
2617                 if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
2618                     print_phase_function(file, indent, "SimpleStruct", phase, var, start_offset + 4);
2619             }
2620             else
2621             {
2622                 const var_t *iid;
2623                 if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
2624                     print_file( file, indent, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid->name );
2625                 print_phase_function(file, indent, "Pointer", phase, var, start_offset);
2626             }
2627         }
2628         fprintf(file, "\n");
2629     }
2630 }
2631
2632
2633 size_t get_size_procformatstring_var(const var_t *var)
2634 {
2635     return write_procformatstring_var(NULL, 0, var, FALSE);
2636 }
2637
2638
2639 size_t get_size_procformatstring_func(const func_t *func)
2640 {
2641     const var_t *var;
2642     size_t size = 0;
2643
2644     /* argument list size */
2645     if (func->args)
2646         LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2647             size += get_size_procformatstring_var(var);
2648
2649     /* return value size */
2650     if (is_void(func->def->type))
2651         size += 2; /* FC_END and FC_PAD */
2652     else
2653         size += get_size_procformatstring_var(func->def);
2654
2655     return size;
2656 }
2657
2658 size_t get_size_procformatstring(const ifref_list_t *ifaces, int for_objects)
2659 {
2660     const ifref_t *iface;
2661     size_t size = 1;
2662     const func_t *func;
2663
2664     if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, const ifref_t, entry )
2665     {
2666         if (for_objects != is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
2667             continue;
2668
2669         if (iface->iface->funcs)
2670             LIST_FOR_EACH_ENTRY( func, iface->iface->funcs, const func_t, entry )
2671                 if (!is_local(func->def->attrs))
2672                     size += get_size_procformatstring_func( func );
2673     }
2674     return size;
2675 }
2676
2677 size_t get_size_typeformatstring(const ifref_list_t *ifaces, int for_objects)
2678 {
2679     set_all_tfswrite(ifaces, FALSE);
2680     return process_tfs(NULL, ifaces, for_objects);
2681 }
2682
2683 static void write_struct_expr(FILE *h, const expr_t *e, int brackets,
2684                               const var_list_t *fields, const char *structvar)
2685 {
2686     switch (e->type) {
2687         case EXPR_VOID:
2688             break;
2689         case EXPR_NUM:
2690             fprintf(h, "%lu", e->u.lval);
2691             break;
2692         case EXPR_HEXNUM:
2693             fprintf(h, "0x%lx", e->u.lval);
2694             break;
2695         case EXPR_DOUBLE:
2696             fprintf(h, "%#.15g", e->u.dval);
2697             break;
2698         case EXPR_TRUEFALSE:
2699             if (e->u.lval == 0)
2700                 fprintf(h, "FALSE");
2701             else
2702                 fprintf(h, "TRUE");
2703             break;
2704         case EXPR_IDENTIFIER:
2705         {
2706             const var_t *field;
2707             LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2708                 if (!strcmp(e->u.sval, field->name))
2709                 {
2710                     fprintf(h, "%s->%s", structvar, e->u.sval);
2711                     break;
2712                 }
2713
2714             if (&field->entry == fields) error("no field found for identifier %s\n", e->u.sval);
2715             break;
2716         }
2717         case EXPR_NEG:
2718             fprintf(h, "-");
2719             write_struct_expr(h, e->ref, 1, fields, structvar);
2720             break;
2721         case EXPR_NOT:
2722             fprintf(h, "~");
2723             write_struct_expr(h, e->ref, 1, fields, structvar);
2724             break;
2725         case EXPR_PPTR:
2726             fprintf(h, "*");
2727             write_struct_expr(h, e->ref, 1, fields, structvar);
2728             break;
2729         case EXPR_CAST:
2730             fprintf(h, "(");
2731             write_type(h, e->u.tref, FALSE, NULL);
2732             fprintf(h, ")");
2733             write_struct_expr(h, e->ref, 1, fields, structvar);
2734             break;
2735         case EXPR_SIZEOF:
2736             fprintf(h, "sizeof(");
2737             write_type(h, e->u.tref, FALSE, NULL);
2738             fprintf(h, ")");
2739             break;
2740         case EXPR_SHL:
2741         case EXPR_SHR:
2742         case EXPR_MUL:
2743         case EXPR_DIV:
2744         case EXPR_ADD:
2745         case EXPR_SUB:
2746         case EXPR_AND:
2747         case EXPR_OR:
2748             if (brackets) fprintf(h, "(");
2749             write_struct_expr(h, e->ref, 1, fields, structvar);
2750             switch (e->type) {
2751                 case EXPR_SHL: fprintf(h, " << "); break;
2752                 case EXPR_SHR: fprintf(h, " >> "); break;
2753                 case EXPR_MUL: fprintf(h, " * "); break;
2754                 case EXPR_DIV: fprintf(h, " / "); break;
2755                 case EXPR_ADD: fprintf(h, " + "); break;
2756                 case EXPR_SUB: fprintf(h, " - "); break;
2757                 case EXPR_AND: fprintf(h, " & "); break;
2758                 case EXPR_OR:  fprintf(h, " | "); break;
2759                 default: break;
2760             }
2761             write_struct_expr(h, e->u.ext, 1, fields, structvar);
2762             if (brackets) fprintf(h, ")");
2763             break;
2764         case EXPR_COND:
2765             if (brackets) fprintf(h, "(");
2766             write_struct_expr(h, e->ref, 1, fields, structvar);
2767             fprintf(h, " ? ");
2768             write_struct_expr(h, e->u.ext, 1, fields, structvar);
2769             fprintf(h, " : ");
2770             write_struct_expr(h, e->ext2, 1, fields, structvar);
2771             if (brackets) fprintf(h, ")");
2772             break;
2773     }
2774 }
2775
2776
2777 void declare_stub_args( FILE *file, int indent, const func_t *func )
2778 {
2779     int in_attr, out_attr;
2780     int i = 0;
2781     const var_t *def = func->def;
2782     const var_t *var;
2783
2784     /* declare return value '_RetVal' */
2785     if (!is_void(def->type))
2786     {
2787         print_file(file, indent, "");
2788         write_type_left(file, def->type);
2789         fprintf(file, " _RetVal;\n");
2790     }
2791
2792     if (!func->args)
2793         return;
2794
2795     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2796     {
2797         int is_string = is_attr(var->attrs, ATTR_STRING);
2798
2799         in_attr = is_attr(var->attrs, ATTR_IN);
2800         out_attr = is_attr(var->attrs, ATTR_OUT);
2801         if (!out_attr && !in_attr)
2802             in_attr = 1;
2803
2804         if (!in_attr && !var->type->size_is && !is_string)
2805         {
2806             print_file(file, indent, "");
2807             write_type(file, var->type->ref, FALSE, "_W%u", i++);
2808             fprintf(file, ";\n");
2809         }
2810
2811         print_file(file, indent, "");
2812         write_type_left(file, var->type);
2813         fprintf(file, " ");
2814         if (var->type->declarray) {
2815             fprintf(file, "( *");
2816             write_name(file, var);
2817             fprintf(file, " )");
2818         } else
2819             write_name(file, var);
2820         write_type_right(file, var->type, FALSE);
2821         fprintf(file, ";\n");
2822
2823         if (decl_indirect(var->type))
2824             print_file(file, indent, "void *_p_%s = &%s;\n",
2825                        var->name, var->name);
2826     }
2827 }
2828
2829
2830 void assign_stub_out_args( FILE *file, int indent, const func_t *func )
2831 {
2832     int in_attr, out_attr;
2833     int i = 0, sep = 0;
2834     const var_t *var;
2835
2836     if (!func->args)
2837         return;
2838
2839     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2840     {
2841         int is_string = is_attr(var->attrs, ATTR_STRING);
2842         in_attr = is_attr(var->attrs, ATTR_IN);
2843         out_attr = is_attr(var->attrs, ATTR_OUT);
2844         if (!out_attr && !in_attr)
2845             in_attr = 1;
2846
2847         if (!in_attr)
2848         {
2849             print_file(file, indent, "");
2850             write_name(file, var);
2851
2852             if (var->type->size_is)
2853             {
2854                 unsigned int size, align = 0;
2855                 type_t *type = var->type;
2856
2857                 fprintf(file, " = NdrAllocate(&_StubMsg, ");
2858                 for ( ; type->size_is ; type = type->ref)
2859                 {
2860                     write_expr(file, type->size_is, TRUE);
2861                     fprintf(file, " * ");
2862                 }
2863                 size = type_memsize(type, &align);
2864                 fprintf(file, "%u);\n", size);
2865             }
2866             else if (!is_string)
2867             {
2868                 fprintf(file, " = &_W%u;\n", i);
2869                 if (is_ptr(var->type) && !last_ptr(var->type))
2870                     print_file(file, indent, "_W%u = 0;\n", i);
2871                 i++;
2872             }
2873
2874             sep = 1;
2875         }
2876     }
2877     if (sep)
2878         fprintf(file, "\n");
2879 }
2880
2881
2882 int write_expr_eval_routines(FILE *file, const char *iface)
2883 {
2884     int result = 0;
2885     struct expr_eval_routine *eval;
2886     unsigned short callback_offset = 0;
2887
2888     LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
2889     {
2890         int indent = 0;
2891         result = 1;
2892         print_file(file, indent, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2893                   iface, eval->structure->name, callback_offset);
2894         print_file(file, indent, "{\n");
2895         indent++;
2896         print_file(file, indent, "struct %s *" STRUCT_EXPR_EVAL_VAR " = (struct %s *)(pStubMsg->StackTop - %u);\n",
2897                    eval->structure->name, eval->structure->name, eval->structure_size);
2898         fprintf(file, "\n");
2899         print_file(file, indent, "pStubMsg->Offset = 0;\n"); /* FIXME */
2900         print_file(file, indent, "pStubMsg->MaxCount = (unsigned long)");
2901         write_struct_expr(file, eval->expr, 1, eval->structure->fields, STRUCT_EXPR_EVAL_VAR);
2902         fprintf(file, ";\n");
2903         indent--;
2904         print_file(file, indent, "}\n\n");
2905         callback_offset++;
2906     }
2907     return result;
2908 }
2909
2910 void write_expr_eval_routine_list(FILE *file, const char *iface)
2911 {
2912     struct expr_eval_routine *eval;
2913     struct expr_eval_routine *cursor;
2914     unsigned short callback_offset = 0;
2915
2916     fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2917     fprintf(file, "{\n");
2918
2919     LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
2920     {
2921         print_file(file, 1, "%s_%sExprEval_%04u,\n",
2922                    iface, eval->structure->name, callback_offset);
2923
2924         callback_offset++;
2925         list_remove(&eval->entry);
2926         free(eval);
2927     }
2928
2929     fprintf(file, "};\n\n");
2930 }
2931
2932 void write_user_quad_list(FILE *file)
2933 {
2934     user_type_t *ut;
2935
2936     if (list_empty(&user_type_list))
2937         return;
2938
2939     fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
2940     fprintf(file, "{\n");
2941     LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
2942     {
2943         const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
2944         print_file(file, 1, "{\n");
2945         print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
2946         print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
2947         print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
2948         print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
2949         print_file(file, 1, "}%s\n", sep);
2950     }
2951     fprintf(file, "};\n\n");
2952 }
2953
2954 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
2955 {
2956     const struct str_list_entry_t *endpoint;
2957     const char *p;
2958
2959     /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2960     print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
2961     LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
2962     {
2963         print_file( f, 1, "{ (const unsigned char *)\"" );
2964         for (p = endpoint->str; *p && *p != ':'; p++)
2965         {
2966             if (*p == '"' || *p == '\\') fputc( '\\', f );
2967             fputc( *p, f );
2968         }
2969         if (!*p) goto error;
2970         if (p[1] != '[') goto error;
2971
2972         fprintf( f, "\", (const unsigned char *)\"" );
2973         for (p += 2; *p && *p != ']'; p++)
2974         {
2975             if (*p == '"' || *p == '\\') fputc( '\\', f );
2976             fputc( *p, f );
2977         }
2978         if (*p != ']') goto error;
2979         fprintf( f, "\" },\n" );
2980     }
2981     print_file( f, 0, "};\n\n" );
2982     return;
2983
2984 error:
2985     error("Invalid endpoint syntax '%s'\n", endpoint->str);
2986 }