widl: Print all NdrFcShort parameters as shorts to avoid warnings for negative values.
[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 <limits.h>
34
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
40
41 #include "typegen.h"
42 #include "expr.h"
43
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
48
49 static const var_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
52
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
55 {
56     struct list entry;
57     const type_t *structure;
58     unsigned int baseoff;
59     const expr_t *expr;
60 };
61
62 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
63 static unsigned int 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 static const var_t *find_array_or_string_in_struct(const type_t *type);
67 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
68                                type_t *type,
69                                const char *name, unsigned int *typestring_offset);
70
71 const char *string_of_type(unsigned char type)
72 {
73     switch (type)
74     {
75     case RPC_FC_BYTE: return "FC_BYTE";
76     case RPC_FC_CHAR: return "FC_CHAR";
77     case RPC_FC_SMALL: return "FC_SMALL";
78     case RPC_FC_USMALL: return "FC_USMALL";
79     case RPC_FC_WCHAR: return "FC_WCHAR";
80     case RPC_FC_SHORT: return "FC_SHORT";
81     case RPC_FC_USHORT: return "FC_USHORT";
82     case RPC_FC_LONG: return "FC_LONG";
83     case RPC_FC_ULONG: return "FC_ULONG";
84     case RPC_FC_FLOAT: return "FC_FLOAT";
85     case RPC_FC_HYPER: return "FC_HYPER";
86     case RPC_FC_DOUBLE: return "FC_DOUBLE";
87     case RPC_FC_ENUM16: return "FC_ENUM16";
88     case RPC_FC_ENUM32: return "FC_ENUM32";
89     case RPC_FC_IGNORE: return "FC_IGNORE";
90     case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
91     case RPC_FC_RP: return "FC_RP";
92     case RPC_FC_UP: return "FC_UP";
93     case RPC_FC_OP: return "FC_OP";
94     case RPC_FC_FP: return "FC_FP";
95     case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
96     case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
97     case RPC_FC_STRUCT: return "FC_STRUCT";
98     case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
99     case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
100     case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
101     case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
102     case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103     case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
104     case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
105     case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
106     case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
107     case RPC_FC_CARRAY: return "FC_CARRAY";
108     case RPC_FC_CVARRAY: return "FC_CVARRAY";
109     case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110     case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
111     case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112     case RPC_FC_POINTER: return "FC_POINTER";
113     case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
114     case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
115     case RPC_FC_CSTRING: return "FC_CSTRING";
116     case RPC_FC_WSTRING: return "FC_WSTRING";
117     default:
118         error("string_of_type: unknown type 0x%02x\n", type);
119         return NULL;
120     }
121 }
122
123 static unsigned char get_pointer_fc(const type_t *type)
124 {
125     assert(is_ptr(type));
126     /* FIXME: see corresponding hack in set_type - we shouldn't be getting
127      * the pointer type from an alias, rather determining it from the
128      * position */
129     return type->type;
130 }
131
132 static int get_struct_type(const type_t *type)
133 {
134   int has_pointer = 0;
135   int has_conformance = 0;
136   int has_variance = 0;
137   var_t *field;
138   var_list_t *fields;
139
140   if (type->type != RPC_FC_STRUCT) return type->type;
141
142   fields = type_struct_get_fields(type);
143
144   if (get_padding(fields))
145     return RPC_FC_BOGUS_STRUCT;
146
147   if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
148   {
149     type_t *t = field->type;
150
151     if (is_user_type(t))
152       return RPC_FC_BOGUS_STRUCT;
153
154     if (field->type->declarray)
155     {
156         if (is_string_type(field->attrs, field->type))
157         {
158             if (is_conformant_array(field->type))
159                 has_conformance = 1;
160             has_variance = 1;
161             continue;
162         }
163
164         if (is_array(type_array_get_element(field->type)))
165             return RPC_FC_BOGUS_STRUCT;
166
167         if (type_array_has_conformance(field->type))
168         {
169             has_conformance = 1;
170             if (field->type->declarray && list_next(fields, &field->entry))
171                 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
172                         field->name);
173         }
174         if (type_array_has_variance(field->type))
175             has_variance = 1;
176
177         t = type_array_get_element(field->type);
178     }
179
180     switch (get_struct_type(t))
181     {
182     /*
183      * RPC_FC_BYTE, RPC_FC_STRUCT, etc
184      *  Simple types don't effect the type of struct.
185      *  A struct containing a simple struct is still a simple struct.
186      *  So long as we can block copy the data, we return RPC_FC_STRUCT.
187      */
188     case 0: /* void pointer */
189     case RPC_FC_BYTE:
190     case RPC_FC_CHAR:
191     case RPC_FC_SMALL:
192     case RPC_FC_USMALL:
193     case RPC_FC_WCHAR:
194     case RPC_FC_SHORT:
195     case RPC_FC_USHORT:
196     case RPC_FC_LONG:
197     case RPC_FC_ULONG:
198     case RPC_FC_INT3264:
199     case RPC_FC_UINT3264:
200     case RPC_FC_HYPER:
201     case RPC_FC_FLOAT:
202     case RPC_FC_DOUBLE:
203     case RPC_FC_STRUCT:
204     case RPC_FC_ENUM32:
205       break;
206
207     case RPC_FC_RP:
208       return RPC_FC_BOGUS_STRUCT;
209
210     case RPC_FC_UP:
211     case RPC_FC_FP:
212     case RPC_FC_OP:
213       if (pointer_size != 4)
214         return RPC_FC_BOGUS_STRUCT;
215       /* pointers to interfaces aren't really pointers and have to be
216        * marshalled specially so they make the structure complex */
217       if (type_pointer_get_ref(t)->type == RPC_FC_IP)
218         return RPC_FC_BOGUS_STRUCT;
219       has_pointer = 1;
220       break;
221
222     case RPC_FC_SMFARRAY:
223     case RPC_FC_LGFARRAY:
224     case RPC_FC_SMVARRAY:
225     case RPC_FC_LGVARRAY:
226     case RPC_FC_CARRAY:
227     case RPC_FC_CVARRAY:
228     case RPC_FC_BOGUS_ARRAY:
229     {
230       unsigned int ptr_type = get_attrv(field->attrs, ATTR_POINTERTYPE);
231       if (!ptr_type || ptr_type == RPC_FC_RP)
232         return RPC_FC_BOGUS_STRUCT;
233       else if (pointer_size != 4)
234         return RPC_FC_BOGUS_STRUCT;
235       has_pointer = 1;
236       break;
237     }
238
239     /*
240      * Propagate member attributes
241      *  a struct should be at least as complex as its member
242      */
243     case RPC_FC_CVSTRUCT:
244       has_conformance = 1;
245       has_variance = 1;
246       has_pointer = 1;
247       break;
248
249     case RPC_FC_CPSTRUCT:
250       has_conformance = 1;
251       if (list_next( fields, &field->entry ))
252           error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
253                   field->name);
254       has_pointer = 1;
255       break;
256
257     case RPC_FC_CSTRUCT:
258       has_conformance = 1;
259       if (list_next( fields, &field->entry ))
260           error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
261                   field->name);
262       break;
263
264     case RPC_FC_PSTRUCT:
265       has_pointer = 1;
266       break;
267
268     default:
269       error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
270       /* fallthru - treat it as complex */
271
272     /* as soon as we see one of these these members, it's bogus... */
273     case RPC_FC_ENCAPSULATED_UNION:
274     case RPC_FC_NON_ENCAPSULATED_UNION:
275     case RPC_FC_BOGUS_STRUCT:
276     case RPC_FC_ENUM16:
277       return RPC_FC_BOGUS_STRUCT;
278     }
279   }
280
281   if( has_variance )
282   {
283     if ( has_conformance )
284       return RPC_FC_CVSTRUCT;
285     else
286       return RPC_FC_BOGUS_STRUCT;
287   }
288   if( has_conformance && has_pointer )
289     return RPC_FC_CPSTRUCT;
290   if( has_conformance )
291     return RPC_FC_CSTRUCT;
292   if( has_pointer )
293     return RPC_FC_PSTRUCT;
294   return RPC_FC_STRUCT;
295 }
296
297 static unsigned char get_array_type(const type_t *type)
298 {
299     unsigned char fc;
300     const expr_t *size_is;
301     const type_t *elem_type;
302
303     if (!is_array(type))
304         return type->type;
305
306     elem_type = type_array_get_element(type);
307     size_is = type_array_get_conformance(type);
308
309     if (!size_is)
310     {
311         unsigned int align = 0;
312         unsigned int size = type_memsize(elem_type, &align);
313         if (size * type_array_get_dim(type) > 0xffffuL)
314             fc = RPC_FC_LGFARRAY;
315         else
316             fc = RPC_FC_SMFARRAY;
317     }
318     else
319         fc = RPC_FC_CARRAY;
320
321     if (type_array_has_variance(type))
322     {
323         if (fc == RPC_FC_SMFARRAY)
324             fc = RPC_FC_SMVARRAY;
325         else if (fc == RPC_FC_LGFARRAY)
326             fc = RPC_FC_LGVARRAY;
327         else if (fc == RPC_FC_CARRAY)
328             fc = RPC_FC_CVARRAY;
329     }
330
331     if (is_user_type(elem_type))
332         fc = RPC_FC_BOGUS_ARRAY;
333     else if (is_struct(elem_type->type))
334     {
335         switch (get_struct_type(elem_type))
336         {
337         case RPC_FC_BOGUS_STRUCT:
338             fc = RPC_FC_BOGUS_ARRAY;
339             break;
340         }
341     }
342     else if (elem_type->type == RPC_FC_ENUM16)
343     {
344         /* is 16-bit enum - if so, wire size differs from mem size and so
345          * the array cannot be block copied, which means the array is complex */
346         fc = RPC_FC_BOGUS_ARRAY;
347     }
348     else if (is_union(elem_type->type))
349         fc = RPC_FC_BOGUS_ARRAY;
350     else if (is_ptr(elem_type))
351     {
352         /* ref pointers cannot just be block copied. unique pointers to
353          * interfaces need special treatment. either case means the array is
354          * complex */
355         if (get_pointer_fc(elem_type) == RPC_FC_RP ||
356             type_pointer_get_ref(elem_type)->type == RPC_FC_IP)
357             fc = RPC_FC_BOGUS_ARRAY;
358     }
359
360     return fc;
361 }
362
363 int is_struct(unsigned char type)
364 {
365     switch (type)
366     {
367     case RPC_FC_STRUCT:
368     case RPC_FC_PSTRUCT:
369     case RPC_FC_CSTRUCT:
370     case RPC_FC_CPSTRUCT:
371     case RPC_FC_CVSTRUCT:
372     case RPC_FC_BOGUS_STRUCT:
373         return 1;
374     default:
375         return 0;
376     }
377 }
378
379 static int is_non_complex_struct(const type_t *type)
380 {
381     switch (get_struct_type(type))
382     {
383     case RPC_FC_STRUCT:
384     case RPC_FC_PSTRUCT:
385     case RPC_FC_CSTRUCT:
386     case RPC_FC_CPSTRUCT:
387     case RPC_FC_CVSTRUCT:
388         return 1;
389     default:
390         return 0;
391     }
392 }
393
394 int is_union(unsigned char type)
395 {
396     switch (type)
397     {
398     case RPC_FC_ENCAPSULATED_UNION:
399     case RPC_FC_NON_ENCAPSULATED_UNION:
400         return 1;
401     default:
402         return 0;
403     }
404 }
405
406 static int type_has_pointers(const type_t *type)
407 {
408     if (is_user_type(type))
409         return FALSE;
410     else if (is_ptr(type))
411         return TRUE;
412     else if (is_array(type))
413         return type_has_pointers(type_array_get_element(type));
414     else if (is_struct(type->type))
415     {
416         var_list_t *fields = type_struct_get_fields(type);
417         const var_t *field;
418         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
419         {
420             if (type_has_pointers(field->type))
421                 return TRUE;
422         }
423     }
424     else if (is_union(type->type))
425     {
426         var_list_t *fields;
427         const var_t *field;
428         fields = type_union_get_cases(type);
429         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
430         {
431             if (field->type && type_has_pointers(field->type))
432                 return TRUE;
433         }
434     }
435
436     return FALSE;
437 }
438
439 static int type_has_full_pointer(const type_t *type)
440 {
441     if (is_user_type(type))
442         return FALSE;
443     else if (type->type == RPC_FC_FP)
444         return TRUE;
445     else if (is_ptr(type))
446         return FALSE;
447     else if (is_array(type))
448         return type_has_full_pointer(type_array_get_element(type));
449     else if (is_struct(type->type))
450     {
451         var_list_t *fields = type_struct_get_fields(type);
452         const var_t *field;
453         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
454         {
455             if (type_has_full_pointer(field->type))
456                 return TRUE;
457         }
458     }
459     else if (is_union(type->type))
460     {
461         var_list_t *fields;
462         const var_t *field;
463         fields = type_union_get_cases(type);
464         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
465         {
466             if (field->type && type_has_full_pointer(field->type))
467                 return TRUE;
468         }
469     }
470
471     return FALSE;
472 }
473
474 static unsigned short user_type_offset(const char *name)
475 {
476     user_type_t *ut;
477     unsigned short off = 0;
478     LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
479     {
480         if (strcmp(name, ut->name) == 0)
481             return off;
482         ++off;
483     }
484     error("user_type_offset: couldn't find type (%s)\n", name);
485     return 0;
486 }
487
488 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
489 {
490     type->typestring_offset = offset;
491     if (file) type->tfswrite = FALSE;
492 }
493
494 static void guard_rec(type_t *type)
495 {
496     /* types that contain references to themselves (like a linked list),
497        need to be shielded from infinite recursion when writing embedded
498        types  */
499     if (type->typestring_offset)
500         type->tfswrite = FALSE;
501     else
502         type->typestring_offset = 1;
503 }
504
505 static type_t *get_user_type(const type_t *t, const char **pname)
506 {
507     for (;;)
508     {
509         type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
510         if (ut)
511         {
512             if (pname)
513                 *pname = t->name;
514             return ut;
515         }
516
517         if (type_is_alias(t))
518             t = type_alias_get_aliasee(t);
519         else
520             return 0;
521     }
522 }
523
524 int is_user_type(const type_t *t)
525 {
526     return get_user_type(t, NULL) != NULL;
527 }
528
529 static int is_embedded_complex(const type_t *type)
530 {
531     unsigned char tc = type->type;
532     return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
533         || (is_ptr(type) && type_pointer_get_ref(type)->type == RPC_FC_IP);
534 }
535
536 static const char *get_context_handle_type_name(const type_t *type)
537 {
538     const type_t *t;
539     for (t = type; is_ptr(t); t = type_pointer_get_ref(t))
540         if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
541             return t->name;
542     assert(0);
543     return NULL;
544 }
545
546 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
547     do { \
548         if (file) \
549             fprintf(file, "/* %2u */\n", typestring_offset); \
550         print_file((file), 2, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
551     } \
552     while (0)
553
554 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
555 static void print_file(FILE *file, int indent, const char *format, ...)
556 {
557     va_list va;
558     va_start(va, format);
559     print(file, indent, format, va);
560     va_end(va);
561 }
562
563 void print(FILE *file, int indent, const char *format, va_list va)
564 {
565     if (file)
566     {
567         if (format[0] != '\n')
568             while (0 < indent--)
569                 fprintf(file, "    ");
570         vfprintf(file, format, va);
571     }
572 }
573
574
575 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
576 {
577     if (decl_indirect(t))
578     {
579         print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
580                    local_var_prefix, n, local_var_prefix, n);
581         print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
582     }
583     else if (is_ptr(t) || is_array(t))
584         print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
585 }
586
587 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
588 {
589     const var_t *var;
590
591     if (!is_void(type_function_get_rettype(func->type)))
592         write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
593
594     if (!type_get_function_args(func->type))
595         return;
596
597     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
598         write_var_init(file, indent, var->type, var->name, local_var_prefix);
599
600     fprintf(file, "\n");
601 }
602
603 static void write_formatdesc(FILE *f, int indent, const char *str)
604 {
605     print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
606     print_file(f, indent, "{\n");
607     print_file(f, indent + 1, "short Pad;\n");
608     print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
609     print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
610     print_file(f, indent, "\n");
611 }
612
613 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
614 {
615     clear_all_offsets();
616
617     print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
618                get_size_typeformatstring(stmts, pred));
619
620     print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
621                get_size_procformatstring(stmts, pred));
622
623     fprintf(f, "\n");
624     write_formatdesc(f, indent, "TYPE");
625     write_formatdesc(f, indent, "PROC");
626     fprintf(f, "\n");
627     print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
628     print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
629     print_file(f, indent, "\n");
630 }
631
632 static inline int is_base_type(unsigned char type)
633 {
634     switch (type)
635     {
636     case RPC_FC_BYTE:
637     case RPC_FC_CHAR:
638     case RPC_FC_USMALL:
639     case RPC_FC_SMALL:
640     case RPC_FC_WCHAR:
641     case RPC_FC_USHORT:
642     case RPC_FC_SHORT:
643     case RPC_FC_ULONG:
644     case RPC_FC_LONG:
645     case RPC_FC_HYPER:
646     case RPC_FC_IGNORE:
647     case RPC_FC_FLOAT:
648     case RPC_FC_DOUBLE:
649     case RPC_FC_ENUM16:
650     case RPC_FC_ENUM32:
651     case RPC_FC_ERROR_STATUS_T:
652     case RPC_FC_BIND_PRIMITIVE:
653         return TRUE;
654
655     default:
656         return FALSE;
657     }
658 }
659
660 int decl_indirect(const type_t *t)
661 {
662     return is_user_type(t)
663         || (!is_base_type(t->type)
664             && !is_ptr(t)
665             && !is_array(t));
666 }
667
668 static unsigned int write_procformatstring_type(FILE *file, int indent,
669                                                 const char *name,
670                                                 const type_t *type,
671                                                 const attr_list_t *attrs,
672                                                 int is_return)
673 {
674     unsigned int size;
675
676     int is_in = is_attr(attrs, ATTR_IN);
677     int is_out = is_attr(attrs, ATTR_OUT);
678
679     if (!is_in && !is_out) is_in = TRUE;
680
681     if (!type->declarray && is_base_type(type->type))
682     {
683         if (is_return)
684             print_file(file, indent, "0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
685         else
686             print_file(file, indent, "0x4e,    /* FC_IN_PARAM_BASETYPE */\n");
687
688         if (type->type == RPC_FC_BIND_PRIMITIVE)
689         {
690             print_file(file, indent, "0x%02x,    /* FC_IGNORE */\n", RPC_FC_IGNORE);
691             size = 2; /* includes param type prefix */
692         }
693         else if (is_base_type(type->type))
694         {
695             print_file(file, indent, "0x%02x,    /* %s */\n", type->type, string_of_type(type->type));
696             size = 2; /* includes param type prefix */
697         }
698         else
699         {
700             error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
701             size = 0;
702         }
703     }
704     else
705     {
706         if (is_return)
707             print_file(file, indent, "0x52,    /* FC_RETURN_PARAM */\n");
708         else if (is_in && is_out)
709             print_file(file, indent, "0x50,    /* FC_IN_OUT_PARAM */\n");
710         else if (is_out)
711             print_file(file, indent, "0x51,    /* FC_OUT_PARAM */\n");
712         else
713             print_file(file, indent, "0x4d,    /* FC_IN_PARAM */\n");
714
715         print_file(file, indent, "0x01,\n");
716         print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
717         size = 4; /* includes param type prefix */
718     }
719     return size;
720 }
721
722 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
723 {
724     const statement_t *stmt;
725     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
726     {
727         if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
728         {
729             const statement_t *stmt_func;
730             if (!pred(stmt->u.type))
731                 continue;
732             STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
733             {
734                 const var_t *func = stmt_func->u.var;
735                 if (is_local(func->attrs)) continue;
736                 /* emit argument data */
737                 if (type_get_function_args(func->type))
738                 {
739                     const var_t *var;
740                     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
741                         write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
742                 }
743
744                 /* emit return value data */
745                 if (is_void(type_function_get_rettype(func->type)))
746                 {
747                     print_file(file, indent, "0x5b,    /* FC_END */\n");
748                     print_file(file, indent, "0x5c,    /* FC_PAD */\n");
749                 }
750                 else
751                     write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
752             }
753         }
754         else if (stmt->type == STMT_LIBRARY)
755             write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
756     }
757 }
758
759 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
760 {
761     int indent = 0;
762
763     print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
764     print_file(file, indent, "{\n");
765     indent++;
766     print_file(file, indent, "0,\n");
767     print_file(file, indent, "{\n");
768     indent++;
769
770     write_procformatstring_stmts(file, indent, stmts, pred);
771
772     print_file(file, indent, "0x0\n");
773     indent--;
774     print_file(file, indent, "}\n");
775     indent--;
776     print_file(file, indent, "};\n");
777     print_file(file, indent, "\n");
778 }
779
780 static int write_base_type(FILE *file, unsigned char fc, unsigned int *typestring_offset)
781 {
782     if (is_base_type(fc))
783     {
784         print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
785         *typestring_offset += 1;
786         return 1;
787     }
788
789     return 0;
790 }
791
792 /* write conformance / variance descriptor */
793 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
794                                            unsigned int baseoff, const type_t *type,
795                                            const expr_t *expr)
796 {
797     unsigned char operator_type = 0;
798     unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
799     const char *conftype_string = "";
800     const char *operator_string = "no operators";
801     const expr_t *subexpr;
802
803     if (!expr)
804     {
805         print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
806         return 4;
807     }
808
809     if (!structure)
810     {
811         /* Top-level conformance calculations are done inline.  */
812         print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
813                     RPC_FC_TOP_LEVEL_CONFORMANCE);
814         print_file (file, 2, "0x0,\n");
815         print_file (file, 2, "NdrFcShort(0x0),\n");
816         return 4;
817     }
818
819     if (expr->is_const)
820     {
821         if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
822             error("write_conf_or_var_desc: constant value %ld is greater than "
823                   "the maximum constant size of %d\n", expr->cval,
824                   UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
825
826         print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
827                    RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
828         print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
829         print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
830
831         return 4;
832     }
833
834     if (is_ptr(type) || (is_array(type) && !type->declarray))
835     {
836         conftype = RPC_FC_POINTER_CONFORMANCE;
837         conftype_string = "field pointer, ";
838     }
839
840     subexpr = expr;
841     switch (subexpr->type)
842     {
843     case EXPR_PPTR:
844         subexpr = subexpr->ref;
845         operator_type = RPC_FC_DEREFERENCE;
846         operator_string = "FC_DEREFERENCE";
847         break;
848     case EXPR_DIV:
849         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
850         {
851             subexpr = subexpr->ref;
852             operator_type = RPC_FC_DIV_2;
853             operator_string = "FC_DIV_2";
854         }
855         break;
856     case EXPR_MUL:
857         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
858         {
859             subexpr = subexpr->ref;
860             operator_type = RPC_FC_MULT_2;
861             operator_string = "FC_MULT_2";
862         }
863         break;
864     case EXPR_SUB:
865         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
866         {
867             subexpr = subexpr->ref;
868             operator_type = RPC_FC_SUB_1;
869             operator_string = "FC_SUB_1";
870         }
871         break;
872     case EXPR_ADD:
873         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
874         {
875             subexpr = subexpr->ref;
876             operator_type = RPC_FC_ADD_1;
877             operator_string = "FC_ADD_1";
878         }
879         break;
880     default:
881         break;
882     }
883
884     if (subexpr->type == EXPR_IDENTIFIER)
885     {
886         const type_t *correlation_variable = NULL;
887         unsigned char correlation_variable_type;
888         unsigned char param_type = 0;
889         unsigned int offset = 0;
890         const var_t *var;
891         var_list_t *fields = type_struct_get_fields(structure);
892
893         if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
894         {
895             unsigned int align = 0;
896             /* FIXME: take alignment into account */
897             if (var->name && !strcmp(var->name, subexpr->u.sval))
898             {
899                 correlation_variable = var->type;
900                 break;
901             }
902             offset += type_memsize(var->type, &align);
903         }
904         if (!correlation_variable)
905             error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
906                   subexpr->u.sval);
907
908         correlation_variable = expr_resolve_type(NULL, structure, expr);
909
910         offset -= baseoff;
911         correlation_variable_type = correlation_variable->type;
912
913         switch (correlation_variable_type)
914         {
915         case RPC_FC_CHAR:
916         case RPC_FC_SMALL:
917             param_type = RPC_FC_SMALL;
918             break;
919         case RPC_FC_BYTE:
920         case RPC_FC_USMALL:
921             param_type = RPC_FC_USMALL;
922             break;
923         case RPC_FC_WCHAR:
924         case RPC_FC_SHORT:
925         case RPC_FC_ENUM16:
926             param_type = RPC_FC_SHORT;
927             break;
928         case RPC_FC_USHORT:
929             param_type = RPC_FC_USHORT;
930             break;
931         case RPC_FC_LONG:
932         case RPC_FC_ENUM32:
933             param_type = RPC_FC_LONG;
934             break;
935         case RPC_FC_ULONG:
936             param_type = RPC_FC_ULONG;
937             break;
938         default:
939             error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
940                 correlation_variable_type);
941         }
942
943         print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
944                    conftype | param_type, conftype_string, string_of_type(param_type));
945         print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
946         print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
947                    offset, offset);
948     }
949     else
950     {
951         unsigned int callback_offset = 0;
952         struct expr_eval_routine *eval;
953         int found = 0;
954
955         LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
956         {
957             if (!strcmp (eval->structure->name, structure->name)
958                 && !compare_expr (eval->expr, expr))
959             {
960                 found = 1;
961                 break;
962             }
963             callback_offset++;
964         }
965
966         if (!found)
967         {
968             eval = xmalloc (sizeof(*eval));
969             eval->structure = structure;
970             eval->baseoff = baseoff;
971             eval->expr = expr;
972             list_add_tail (&expr_eval_routines, &eval->entry);
973         }
974
975         if (callback_offset > USHRT_MAX)
976             error("Maximum number of callback routines reached\n");
977
978         print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
979         print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
980         print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
981     }
982     return 4;
983 }
984
985 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
986 {
987     int have_align = FALSE;
988     unsigned int size = 0;
989     const var_t *v;
990
991     if (!fields) return 0;
992     LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
993     {
994         unsigned int falign = 0;
995         unsigned int fsize = type_memsize(v->type, &falign);
996         if (!have_align)
997         {
998             *align = falign;
999             have_align = TRUE;
1000         }
1001         size = ROUND_SIZE(size, falign);
1002         size += fsize;
1003     }
1004
1005     size = ROUND_SIZE(size, *align);
1006     return size;
1007 }
1008
1009 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1010 {
1011     unsigned int size, maxs = 0;
1012     unsigned int align = *pmaxa;
1013     const var_t *v;
1014
1015     if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1016     {
1017         /* we could have an empty default field with NULL type */
1018         if (v->type)
1019         {
1020             size = type_memsize(v->type, &align);
1021             if (maxs < size) maxs = size;
1022             if (*pmaxa < align) *pmaxa = align;
1023         }
1024     }
1025
1026     return maxs;
1027 }
1028
1029 int get_padding(const var_list_t *fields)
1030 {
1031     unsigned short offset = 0;
1032     int salign = -1;
1033     const var_t *f;
1034
1035     if (!fields)
1036         return 0;
1037
1038     LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1039     {
1040         type_t *ft = f->type;
1041         unsigned int align = 0;
1042         unsigned int size = type_memsize(ft, &align);
1043         if (salign == -1)
1044             salign = align;
1045         offset = ROUND_SIZE(offset, align);
1046         offset += size;
1047     }
1048
1049     return ROUNDING(offset, salign);
1050 }
1051
1052 unsigned int type_memsize(const type_t *t, unsigned int *align)
1053 {
1054     unsigned int size = 0;
1055
1056     if (type_is_alias(t))
1057         size = type_memsize(type_alias_get_aliasee(t), align);
1058     else if (t->declarray && is_conformant_array(t))
1059     {
1060         type_memsize(type_array_get_element(t), align);
1061         size = 0;
1062     }
1063     else if (is_ptr(t) || is_conformant_array(t))
1064     {
1065         assert( pointer_size );
1066         size = pointer_size;
1067         if (size > *align) *align = size;
1068     }
1069     else switch (t->type)
1070     {
1071     case RPC_FC_BYTE:
1072     case RPC_FC_CHAR:
1073     case RPC_FC_USMALL:
1074     case RPC_FC_SMALL:
1075         size = 1;
1076         if (size > *align) *align = size;
1077         break;
1078     case RPC_FC_WCHAR:
1079     case RPC_FC_USHORT:
1080     case RPC_FC_SHORT:
1081     case RPC_FC_ENUM16:
1082         size = 2;
1083         if (size > *align) *align = size;
1084         break;
1085     case RPC_FC_ULONG:
1086     case RPC_FC_LONG:
1087     case RPC_FC_ERROR_STATUS_T:
1088     case RPC_FC_ENUM32:
1089     case RPC_FC_FLOAT:
1090         size = 4;
1091         if (size > *align) *align = size;
1092         break;
1093     case RPC_FC_HYPER:
1094     case RPC_FC_DOUBLE:
1095         size = 8;
1096         if (size > *align) *align = size;
1097         break;
1098     case RPC_FC_STRUCT:
1099     case RPC_FC_CVSTRUCT:
1100     case RPC_FC_CPSTRUCT:
1101     case RPC_FC_CSTRUCT:
1102     case RPC_FC_PSTRUCT:
1103     case RPC_FC_BOGUS_STRUCT:
1104         size = fields_memsize(type_struct_get_fields(t), align);
1105         break;
1106     case RPC_FC_ENCAPSULATED_UNION:
1107         size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1108         break;
1109     case RPC_FC_NON_ENCAPSULATED_UNION:
1110         size = union_memsize(type_union_get_cases(t), align);
1111         break;
1112     case RPC_FC_SMFARRAY:
1113     case RPC_FC_LGFARRAY:
1114     case RPC_FC_SMVARRAY:
1115     case RPC_FC_LGVARRAY:
1116     case RPC_FC_BOGUS_ARRAY:
1117         size = type_array_get_dim(t) * type_memsize(type_array_get_element(t), align);
1118         break;
1119     default:
1120         error("type_memsize: Unknown type 0x%x\n", t->type);
1121         size = 0;
1122     }
1123
1124     return size;
1125 }
1126
1127 int is_full_pointer_function(const var_t *func)
1128 {
1129     const var_t *var;
1130     if (type_has_full_pointer(type_function_get_rettype(func->type)))
1131         return TRUE;
1132     if (!type_get_function_args(func->type))
1133         return FALSE;
1134     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1135         if (type_has_full_pointer( var->type ))
1136             return TRUE;
1137     return FALSE;
1138 }
1139
1140 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1141 {
1142     print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1143                    is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1144     fprintf(file, "\n");
1145 }
1146
1147 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1148 {
1149     print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1150     fprintf(file, "\n");
1151 }
1152
1153 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, unsigned int offset)
1154 {
1155     short absoff = type_pointer_get_ref(type)->typestring_offset;
1156     short reloff = absoff - (offset + 2);
1157     int ptr_attr = is_ptr(type_pointer_get_ref(type)) ? 0x10 : 0x0;
1158
1159     print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1160                type->type, ptr_attr, string_of_type(type->type));
1161     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1162                reloff, reloff, absoff);
1163     return 4;
1164 }
1165
1166 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1167 {
1168     unsigned char fc = type_pointer_get_ref(type)->type;
1169     /* for historical reasons, write_simple_pointer also handled string types,
1170      * but no longer does. catch bad uses of the function with this check */
1171     if (is_string_type(type->attrs, type))
1172         error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1173     print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1174                type->type, string_of_type(type->type));
1175     print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1176     print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1177     return 4;
1178 }
1179
1180 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1181 {
1182     print_file(file, 0, "/* %u (", tfsoff);
1183     write_type_decl(file, t, NULL);
1184     print_file(file, 0, ") */\n");
1185 }
1186
1187 static unsigned int write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1188 {
1189     unsigned int offset = *typestring_offset;
1190
1191     print_start_tfs_comment(file, type, offset);
1192     update_tfsoff(type, offset, file);
1193
1194     if (type_pointer_get_ref(type)->typestring_offset)
1195         *typestring_offset += write_nonsimple_pointer(file, type, offset);
1196     else if (is_base_type(type_pointer_get_ref(type)->type))
1197         *typestring_offset += write_simple_pointer(file, type);
1198
1199     return offset;
1200 }
1201
1202 static int processed(const type_t *type)
1203 {
1204     return type->typestring_offset && !type->tfswrite;
1205 }
1206
1207 static int user_type_has_variable_size(const type_t *t)
1208 {
1209     if (is_ptr(t))
1210         return TRUE;
1211     else
1212         switch (get_struct_type(t))
1213         {
1214         case RPC_FC_PSTRUCT:
1215         case RPC_FC_CSTRUCT:
1216         case RPC_FC_CPSTRUCT:
1217         case RPC_FC_CVSTRUCT:
1218             return TRUE;
1219         }
1220     /* Note: Since this only applies to user types, we can't have a conformant
1221        array here, and strings should get filed under pointer in this case.  */
1222     return FALSE;
1223 }
1224
1225 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1226 {
1227     unsigned int start, absoff, flags;
1228     unsigned int align = 0, ualign = 0;
1229     const char *name = NULL;
1230     type_t *utype = get_user_type(type, &name);
1231     unsigned int usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1232     unsigned int size = type_memsize(type, &align);
1233     unsigned short funoff = user_type_offset(name);
1234     short reloff;
1235
1236     guard_rec(type);
1237
1238     if (is_base_type(utype->type))
1239     {
1240         absoff = *tfsoff;
1241         print_start_tfs_comment(file, utype, absoff);
1242         print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
1243         print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1244         *tfsoff += 2;
1245     }
1246     else
1247     {
1248         if (!processed(utype))
1249             write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1250         absoff = utype->typestring_offset;
1251     }
1252
1253     if (utype->type == RPC_FC_RP)
1254         flags = 0x40;
1255     else if (utype->type == RPC_FC_UP)
1256         flags = 0x80;
1257     else
1258         flags = 0;
1259
1260     start = *tfsoff;
1261     update_tfsoff(type, start, file);
1262     print_start_tfs_comment(file, type, start);
1263     print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1264     print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1265                flags | (align - 1), align - 1, flags);
1266     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1267     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1268     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1269     *tfsoff += 8;
1270     reloff = absoff - *tfsoff;
1271     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1272     *tfsoff += 2;
1273 }
1274
1275 static inline unsigned char make_signed(unsigned char fc)
1276 {
1277     switch(fc)
1278     {
1279         case RPC_FC_USMALL:
1280             return RPC_FC_SMALL;
1281         case RPC_FC_USHORT:
1282             return RPC_FC_SHORT;
1283         case RPC_FC_ULONG:
1284             return RPC_FC_LONG;
1285         default:
1286             return fc;
1287     }
1288 }
1289
1290 static void write_member_type(FILE *file, const type_t *cont,
1291                               const attr_list_t *attrs, const type_t *type,
1292                               unsigned int *corroff, unsigned int *tfsoff)
1293 {
1294     if (is_embedded_complex(type) && !is_conformant_array(type))
1295     {
1296         unsigned int absoff;
1297         short reloff;
1298
1299         if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1300         {
1301             absoff = *corroff;
1302             *corroff += 8;
1303         }
1304         else
1305         {
1306             absoff = type->typestring_offset;
1307         }
1308         reloff = absoff - (*tfsoff + 2);
1309
1310         print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1311         /* FIXME: actually compute necessary padding */
1312         print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1313         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1314                    reloff, reloff, absoff);
1315         *tfsoff += 4;
1316     }
1317     else if (is_ptr(type) || is_conformant_array(type))
1318     {
1319         unsigned char fc = (get_struct_type(cont) == RPC_FC_BOGUS_STRUCT
1320                             ? RPC_FC_POINTER
1321                             : RPC_FC_LONG);
1322         print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1323         *tfsoff += 1;
1324     }
1325     else if (!write_base_type(file, make_signed(type->type), tfsoff))
1326         error("Unsupported member type 0x%x\n", type->type);
1327 }
1328
1329 static void write_end(FILE *file, unsigned int *tfsoff)
1330 {
1331     if (*tfsoff % 2 == 0)
1332     {
1333         print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1334         *tfsoff += 1;
1335     }
1336     print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1337     *tfsoff += 1;
1338 }
1339
1340 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1341 {
1342     unsigned int offset = 0;
1343     var_list_t *fs = type_struct_get_fields(type);
1344     var_t *f;
1345
1346     if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1347     {
1348         unsigned int align = 0;
1349         type_t *ft = f->type;
1350         if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
1351         {
1352             unsigned int absoff = ft->typestring_offset;
1353             short reloff = absoff - (*tfsoff + 6);
1354             print_file(file, 0, "/* %d */\n", *tfsoff);
1355             print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
1356             print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1357             write_conf_or_var_desc(file, current_structure, offset, ft,
1358                                    get_attrp(f->attrs, ATTR_SWITCHIS));
1359             print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1360                        reloff, reloff, absoff);
1361             *tfsoff += 8;
1362         }
1363
1364         /* FIXME: take alignment into account */
1365         offset += type_memsize(ft, &align);
1366     }
1367 }
1368
1369 static int write_no_repeat_pointer_descriptions(
1370     FILE *file, type_t *type,
1371     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1372     unsigned int *typestring_offset)
1373 {
1374     int written = 0;
1375     unsigned int align;
1376
1377     if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1378     {
1379         unsigned int memsize;
1380
1381         print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1382         print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1383
1384         /* pointer instance */
1385         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1386         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1387         *typestring_offset += 6;
1388
1389         if (is_ptr(type))
1390         {
1391             if (is_string_type(type->attrs, type))
1392                 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1393             else
1394                 write_pointer_tfs(file, type, typestring_offset);
1395         }
1396         else
1397         {
1398             unsigned absoff = type->typestring_offset;
1399             short reloff = absoff - (*typestring_offset + 2);
1400             /* FIXME: get pointer attributes from field */
1401             print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1402             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1403                        reloff, reloff, absoff);
1404             *typestring_offset += 4;
1405         }
1406
1407         align = 0;
1408         memsize = type_memsize(type, &align);
1409         *offset_in_memory += memsize;
1410         /* increment these separately as in the case of conformant (varying)
1411          * structures these start at different values */
1412         *offset_in_buffer += memsize;
1413
1414         return 1;
1415     }
1416
1417     if (is_non_complex_struct(type))
1418     {
1419         const var_t *v;
1420         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1421         {
1422             if (offset_in_memory && offset_in_buffer)
1423             {
1424                 unsigned int padding;
1425                 align = 0;
1426                 type_memsize(v->type, &align);
1427                 padding = ROUNDING(*offset_in_memory, align);
1428                 *offset_in_memory += padding;
1429                 *offset_in_buffer += padding;
1430             }
1431             written += write_no_repeat_pointer_descriptions(
1432                 file, v->type,
1433                 offset_in_memory, offset_in_buffer, typestring_offset);
1434         }
1435     }
1436     else
1437     {
1438         unsigned int memsize;
1439         align = 0;
1440         memsize = type_memsize(type, &align);
1441         *offset_in_memory += memsize;
1442         /* increment these separately as in the case of conformant (varying)
1443          * structures these start at different values */
1444         *offset_in_buffer += memsize;
1445     }
1446
1447     return written;
1448 }
1449
1450 static int write_pointer_description_offsets(
1451     FILE *file, const attr_list_t *attrs, type_t *type,
1452     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1453     unsigned int *typestring_offset)
1454 {
1455     int written = 0;
1456     unsigned int align;
1457
1458     if (is_ptr(type) && type_pointer_get_ref(type)->type != RPC_FC_IP)
1459     {
1460         if (offset_in_memory && offset_in_buffer)
1461         {
1462             unsigned int memsize;
1463
1464             /* pointer instance */
1465             /* FIXME: sometimes from end of structure, sometimes from beginning */
1466             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1467             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1468
1469             align = 0;
1470             memsize = type_memsize(type, &align);
1471             *offset_in_memory += memsize;
1472             /* increment these separately as in the case of conformant (varying)
1473              * structures these start at different values */
1474             *offset_in_buffer += memsize;
1475         }
1476         *typestring_offset += 4;
1477
1478         if (is_string_type(attrs, type))
1479             write_string_tfs(file, NULL, type, NULL, typestring_offset);
1480         else if (processed(type_pointer_get_ref(type)) ||
1481                  is_base_type(type_pointer_get_ref(type)->type))
1482             write_pointer_tfs(file, type, typestring_offset);
1483         else
1484             error("write_pointer_description_offsets: type format string unknown\n");
1485
1486         return 1;
1487     }
1488
1489     if (is_array(type))
1490     {
1491         return write_pointer_description_offsets(
1492             file, attrs, type_array_get_element(type), offset_in_memory,
1493             offset_in_buffer, typestring_offset);
1494     }
1495     else if (is_non_complex_struct(type))
1496     {
1497         /* otherwise search for interesting fields to parse */
1498         const var_t *v;
1499         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1500         {
1501             if (offset_in_memory && offset_in_buffer)
1502             {
1503                 unsigned int padding;
1504                 align = 0;
1505                 type_memsize(v->type, &align);
1506                 padding = ROUNDING(*offset_in_memory, align);
1507                 *offset_in_memory += padding;
1508                 *offset_in_buffer += padding;
1509             }
1510             written += write_pointer_description_offsets(
1511                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1512                 typestring_offset);
1513         }
1514     }
1515     else
1516     {
1517         if (offset_in_memory && offset_in_buffer)
1518         {
1519             unsigned int memsize;
1520             align = 0;
1521             memsize = type_memsize(type, &align);
1522             *offset_in_memory += memsize;
1523             /* increment these separately as in the case of conformant (varying)
1524              * structures these start at different values */
1525             *offset_in_buffer += memsize;
1526         }
1527     }
1528
1529     return written;
1530 }
1531
1532 /* Note: if file is NULL return value is number of pointers to write, else
1533  * it is the number of type format characters written */
1534 static int write_fixed_array_pointer_descriptions(
1535     FILE *file, const attr_list_t *attrs, type_t *type,
1536     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1537     unsigned int *typestring_offset)
1538 {
1539     unsigned int align;
1540     int pointer_count = 0;
1541     int real_type = get_array_type( type );
1542
1543     if (real_type == RPC_FC_SMFARRAY || real_type == RPC_FC_LGFARRAY)
1544     {
1545         unsigned int temp = 0;
1546         /* unfortunately, this needs to be done in two passes to avoid
1547          * writing out redundant FC_FIXED_REPEAT descriptions */
1548         pointer_count = write_pointer_description_offsets(
1549             NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1550         if (pointer_count > 0)
1551         {
1552             unsigned int increment_size;
1553             unsigned int offset_of_array_pointer_mem = 0;
1554             unsigned int offset_of_array_pointer_buf = 0;
1555
1556             align = 0;
1557             increment_size = type_memsize(type_array_get_element(type), &align);
1558
1559             print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1560             print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1561             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1562             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1563             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1564             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1565             *typestring_offset += 10;
1566
1567             pointer_count = write_pointer_description_offsets(
1568                 file, attrs, type, &offset_of_array_pointer_mem,
1569                 &offset_of_array_pointer_buf, typestring_offset);
1570         }
1571     }
1572     else if (is_struct(type->type))
1573     {
1574         const var_t *v;
1575         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1576         {
1577             if (offset_in_memory && offset_in_buffer)
1578             {
1579                 unsigned int padding;
1580                 align = 0;
1581                 type_memsize(v->type, &align);
1582                 padding = ROUNDING(*offset_in_memory, align);
1583                 *offset_in_memory += padding;
1584                 *offset_in_buffer += padding;
1585             }
1586             pointer_count += write_fixed_array_pointer_descriptions(
1587                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1588                 typestring_offset);
1589         }
1590     }
1591     else
1592     {
1593         if (offset_in_memory && offset_in_buffer)
1594         {
1595             unsigned int memsize;
1596             align = 0;
1597             memsize = type_memsize(type, &align);
1598             *offset_in_memory += memsize;
1599             /* increment these separately as in the case of conformant (varying)
1600              * structures these start at different values */
1601             *offset_in_buffer += memsize;
1602         }
1603     }
1604
1605     return pointer_count;
1606 }
1607
1608 /* Note: if file is NULL return value is number of pointers to write, else
1609  * it is the number of type format characters written */
1610 static int write_conformant_array_pointer_descriptions(
1611     FILE *file, const attr_list_t *attrs, type_t *type,
1612     unsigned int offset_in_memory, unsigned int *typestring_offset)
1613 {
1614     unsigned int align;
1615     int pointer_count = 0;
1616
1617     if (is_conformant_array(type) && !type_array_has_variance(type))
1618     {
1619         unsigned int temp = 0;
1620         /* unfortunately, this needs to be done in two passes to avoid
1621          * writing out redundant FC_VARIABLE_REPEAT descriptions */
1622         pointer_count = write_pointer_description_offsets(
1623             NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1624         if (pointer_count > 0)
1625         {
1626             unsigned int increment_size;
1627             unsigned int offset_of_array_pointer_mem = offset_in_memory;
1628             unsigned int offset_of_array_pointer_buf = offset_in_memory;
1629
1630             align = 0;
1631             increment_size = type_memsize(type_array_get_element(type), &align);
1632
1633             if (increment_size > USHRT_MAX)
1634                 error("array size of %u bytes is too large\n", increment_size);
1635
1636             print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1637             print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1638             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1639             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1640             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1641             *typestring_offset += 8;
1642
1643             pointer_count = write_pointer_description_offsets(
1644                 file, attrs, type_array_get_element(type),
1645                 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1646                 typestring_offset);
1647         }
1648     }
1649
1650     return pointer_count;
1651 }
1652
1653 /* Note: if file is NULL return value is number of pointers to write, else
1654  * it is the number of type format characters written */
1655 static int write_varying_array_pointer_descriptions(
1656     FILE *file, const attr_list_t *attrs, type_t *type,
1657     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1658     unsigned int *typestring_offset)
1659 {
1660     unsigned int align;
1661     int pointer_count = 0;
1662
1663     if (is_array(type) && type_array_has_variance(type))
1664     {
1665         unsigned int temp = 0;
1666         /* unfortunately, this needs to be done in two passes to avoid
1667          * writing out redundant FC_VARIABLE_REPEAT descriptions */
1668         pointer_count = write_pointer_description_offsets(
1669             NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1670         if (pointer_count > 0)
1671         {
1672             unsigned int increment_size;
1673
1674             align = 0;
1675             increment_size = type_memsize(type_array_get_element(type), &align);
1676
1677             if (increment_size > USHRT_MAX)
1678                 error("array size of %u bytes is too large\n", increment_size);
1679
1680             print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1681             print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1682             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1683             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1684             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1685             *typestring_offset += 8;
1686
1687             pointer_count = write_pointer_description_offsets(
1688                 file, attrs, type, offset_in_memory,
1689                 offset_in_buffer, typestring_offset);
1690         }
1691     }
1692     else if (is_struct(type->type))
1693     {
1694         const var_t *v;
1695         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1696         {
1697             if (offset_in_memory && offset_in_buffer)
1698             {
1699                 unsigned int padding;
1700
1701                 if (is_array(v->type) && type_array_has_variance(v->type))
1702                 {
1703                     *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1704                     /* skip over variance and offset in buffer */
1705                     *offset_in_buffer += 8;
1706                 }
1707
1708                 align = 0;
1709                 type_memsize(v->type, &align);
1710                 padding = ROUNDING(*offset_in_memory, align);
1711                 *offset_in_memory += padding;
1712                 *offset_in_buffer += padding;
1713             }
1714             pointer_count += write_varying_array_pointer_descriptions(
1715                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1716                 typestring_offset);
1717         }
1718     }
1719     else
1720     {
1721         if (offset_in_memory && offset_in_buffer)
1722         {
1723             unsigned int memsize;
1724             align = 0;
1725             memsize = type_memsize(type, &align);
1726             *offset_in_memory += memsize;
1727             /* increment these separately as in the case of conformant (varying)
1728              * structures these start at different values */
1729             *offset_in_buffer += memsize;
1730         }
1731     }
1732
1733     return pointer_count;
1734 }
1735
1736 static void write_pointer_description(FILE *file, type_t *type,
1737                                       unsigned int *typestring_offset)
1738 {
1739     unsigned int offset_in_buffer;
1740     unsigned int offset_in_memory;
1741
1742     /* pass 1: search for single instance of a pointer (i.e. don't descend
1743      * into arrays) */
1744     if (!is_array(type))
1745     {
1746         offset_in_memory = 0;
1747         offset_in_buffer = 0;
1748         write_no_repeat_pointer_descriptions(
1749             file, type,
1750             &offset_in_memory, &offset_in_buffer, typestring_offset);
1751     }
1752
1753     /* pass 2: search for pointers in fixed arrays */
1754     offset_in_memory = 0;
1755     offset_in_buffer = 0;
1756     write_fixed_array_pointer_descriptions(
1757         file, NULL, type,
1758         &offset_in_memory, &offset_in_buffer, typestring_offset);
1759
1760     /* pass 3: search for pointers in conformant only arrays (but don't descend
1761      * into conformant varying or varying arrays) */
1762     if ((!type->declarray || !current_structure) && is_conformant_array(type))
1763         write_conformant_array_pointer_descriptions(
1764             file, NULL, type, 0, typestring_offset);
1765     else if (get_struct_type(type) == RPC_FC_CPSTRUCT)
1766     {
1767         unsigned int align = 0;
1768         type_t *carray = find_array_or_string_in_struct(type)->type;
1769         write_conformant_array_pointer_descriptions(
1770             file, NULL, carray,
1771             type_memsize(type, &align),
1772             typestring_offset);
1773     }
1774
1775     /* pass 4: search for pointers in varying arrays */
1776     offset_in_memory = 0;
1777     offset_in_buffer = 0;
1778     write_varying_array_pointer_descriptions(
1779             file, NULL, type,
1780             &offset_in_memory, &offset_in_buffer, typestring_offset);
1781 }
1782
1783 int is_declptr(const type_t *t)
1784 {
1785   return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1786 }
1787
1788 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1789                                      type_t *type,
1790                                      const char *name, unsigned int *typestring_offset)
1791 {
1792     unsigned int start_offset;
1793     unsigned char rtype;
1794
1795     if (is_declptr(type))
1796     {
1797         unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1798         int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
1799         if (!pointer_type)
1800             pointer_type = RPC_FC_RP;
1801         print_start_tfs_comment(file, type, *typestring_offset);
1802         print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1803                    pointer_type, flag, string_of_type(pointer_type),
1804                    flag ? " [simple_pointer]" : "");
1805         *typestring_offset += 2;
1806         if (!flag)
1807         {
1808             print_file(file, 2, "NdrFcShort(0x2),\n");
1809             *typestring_offset += 2;
1810         }
1811     }
1812
1813     start_offset = *typestring_offset;
1814     update_tfsoff(type, start_offset, file);
1815
1816     if (is_array(type))
1817         rtype = type_array_get_element(type)->type;
1818     else
1819         rtype = type_pointer_get_ref(type)->type;
1820
1821     if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1822     {
1823         error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1824         return start_offset;
1825     }
1826
1827     if (type->declarray && !is_conformant_array(type))
1828     {
1829         unsigned int dim = type_array_get_dim(type);
1830
1831         /* FIXME: multi-dimensional array */
1832         if (0xffffu < dim)
1833             error("array size for parameter %s exceeds %u bytes by %u bytes\n",
1834                   name, 0xffffu, dim - 0xffffu);
1835
1836         if (rtype == RPC_FC_CHAR)
1837             WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1838         else
1839             WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1840         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1841         *typestring_offset += 2;
1842
1843         print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
1844         *typestring_offset += 2;
1845
1846         return start_offset;
1847     }
1848     else if (is_conformant_array(type))
1849     {
1850         unsigned int align = 0;
1851
1852         if (rtype == RPC_FC_CHAR)
1853             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1854         else
1855             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1856         print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1857         *typestring_offset += 2;
1858
1859         *typestring_offset += write_conf_or_var_desc(
1860             file, current_structure,
1861             (type->declarray && current_structure
1862              ? type_memsize(current_structure, &align)
1863              : 0),
1864             type, type_array_get_conformance(type));
1865
1866         return start_offset;
1867     }
1868     else
1869     {
1870         if (rtype == RPC_FC_WCHAR)
1871             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1872         else
1873             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1874         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1875         *typestring_offset += 2;
1876
1877         return start_offset;
1878     }
1879 }
1880
1881 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1882                                     const char *name, unsigned int *typestring_offset)
1883 {
1884     const expr_t *length_is = type_array_get_variance(type);
1885     const expr_t *size_is = type_array_get_conformance(type);
1886     unsigned int align = 0;
1887     unsigned int size;
1888     unsigned int start_offset;
1889     int real_type;
1890     int has_pointer;
1891     int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1892     unsigned int baseoff
1893         = type->declarray && current_structure
1894         ? type_memsize(current_structure, &align)
1895         : 0;
1896
1897     if (!pointer_type)
1898         pointer_type = RPC_FC_RP;
1899
1900     if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
1901         has_pointer = TRUE;
1902     else
1903         has_pointer = type_has_pointers(type_array_get_element(type));
1904
1905     align = 0;
1906     size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
1907     real_type = get_array_type( type );
1908
1909     start_offset = *typestring_offset;
1910     update_tfsoff(type, start_offset, file);
1911     print_start_tfs_comment(file, type, start_offset);
1912     print_file(file, 2, "0x%02x,\t/* %s */\n", real_type, string_of_type(real_type));
1913     print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
1914     *typestring_offset += 2;
1915
1916     align = 0;
1917     if (real_type != RPC_FC_BOGUS_ARRAY)
1918     {
1919         if (real_type == RPC_FC_LGFARRAY || real_type == RPC_FC_LGVARRAY)
1920         {
1921             print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
1922             *typestring_offset += 4;
1923         }
1924         else
1925         {
1926             print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1927             *typestring_offset += 2;
1928         }
1929
1930         if (is_conformant_array(type))
1931             *typestring_offset
1932                 += write_conf_or_var_desc(file, current_structure, baseoff,
1933                                           type, size_is);
1934
1935         if (real_type == RPC_FC_SMVARRAY || real_type == RPC_FC_LGVARRAY)
1936         {
1937             unsigned int elalign = 0;
1938             unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
1939             unsigned int dim = type_array_get_dim(type);
1940
1941             if (real_type == RPC_FC_LGVARRAY)
1942             {
1943                 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
1944                 *typestring_offset += 4;
1945             }
1946             else
1947             {
1948                 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
1949                 *typestring_offset += 2;
1950             }
1951
1952             print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
1953             *typestring_offset += 2;
1954         }
1955
1956         if (length_is)
1957             *typestring_offset
1958                 += write_conf_or_var_desc(file, current_structure, baseoff,
1959                                           type, length_is);
1960
1961         if (has_pointer && (!type->declarray || !current_structure))
1962         {
1963             print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
1964             print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1965             *typestring_offset += 2;
1966             write_pointer_description(file, type, typestring_offset);
1967             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1968             *typestring_offset += 1;
1969         }
1970
1971         write_member_type(file, type, NULL, type_array_get_element(type), NULL, typestring_offset);
1972         write_end(file, typestring_offset);
1973     }
1974     else
1975     {
1976         unsigned int dim = size_is ? 0 : type_array_get_dim(type);
1977         print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
1978         *typestring_offset += 2;
1979         *typestring_offset
1980             += write_conf_or_var_desc(file, current_structure, baseoff,
1981                                       type, size_is);
1982         *typestring_offset
1983             += write_conf_or_var_desc(file, current_structure, baseoff,
1984                                       type, length_is);
1985         write_member_type(file, type, NULL, type_array_get_element(type), NULL, typestring_offset);
1986         write_end(file, typestring_offset);
1987     }
1988
1989     return start_offset;
1990 }
1991
1992 static const var_t *find_array_or_string_in_struct(const type_t *type)
1993 {
1994     const var_list_t *fields = type_struct_get_fields(type);
1995     const var_t *last_field;
1996     const type_t *ft;
1997     int real_type;
1998
1999     if (!fields || list_empty(fields))
2000         return NULL;
2001
2002     last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2003     ft = last_field->type;
2004
2005     if (ft->declarray && is_conformant_array(ft))
2006         return last_field;
2007
2008     real_type = get_struct_type( type );
2009     if (real_type == RPC_FC_CSTRUCT || real_type == RPC_FC_CPSTRUCT || real_type == RPC_FC_CVSTRUCT)
2010         return find_array_or_string_in_struct(ft);
2011     else
2012         return NULL;
2013 }
2014
2015 static void write_struct_members(FILE *file, const type_t *type,
2016                                  unsigned int *corroff, unsigned int *typestring_offset)
2017 {
2018     const var_t *field;
2019     unsigned short offset = 0;
2020     int salign = -1;
2021     int padding;
2022     var_list_t *fields = type_struct_get_fields(type);
2023
2024     if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2025     {
2026         type_t *ft = field->type;
2027         if (!ft->declarray || !is_conformant_array(ft))
2028         {
2029             unsigned int align = 0;
2030             unsigned int size = type_memsize(ft, &align);
2031             if (salign == -1)
2032                 salign = align;
2033             if ((align - 1) & offset)
2034             {
2035                 unsigned char fc = 0;
2036                 switch (align)
2037                 {
2038                 case 4:
2039                     fc = RPC_FC_ALIGNM4;
2040                     break;
2041                 case 8:
2042                     fc = RPC_FC_ALIGNM8;
2043                     break;
2044                 default:
2045                     error("write_struct_members: cannot align type %d\n", ft->type);
2046                 }
2047                 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2048                 offset = ROUND_SIZE(offset, align);
2049                 *typestring_offset += 1;
2050             }
2051             write_member_type(file, type, field->attrs, field->type, corroff,
2052                               typestring_offset);
2053             offset += size;
2054         }
2055     }
2056
2057     padding = ROUNDING(offset, salign);
2058     if (padding)
2059     {
2060         print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2061                    RPC_FC_STRUCTPAD1 + padding - 1,
2062                    padding);
2063         *typestring_offset += 1;
2064     }
2065
2066     write_end(file, typestring_offset);
2067 }
2068
2069 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2070                                      const char *name, unsigned int *tfsoff)
2071 {
2072     const type_t *save_current_structure = current_structure;
2073     unsigned int total_size;
2074     const var_t *array;
2075     unsigned int start_offset;
2076     unsigned int array_offset;
2077     int has_pointers = 0;
2078     unsigned int align = 0;
2079     unsigned int corroff;
2080     var_t *f;
2081     int real_type = get_struct_type( type );
2082     var_list_t *fields = type_struct_get_fields(type);
2083
2084     guard_rec(type);
2085     current_structure = type;
2086
2087     total_size = type_memsize(type, &align);
2088     if (total_size > USHRT_MAX)
2089         error("structure size for %s exceeds %d bytes by %d bytes\n",
2090               name, USHRT_MAX, total_size - USHRT_MAX);
2091
2092     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2093         has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2094                                              FALSE, tfsoff);
2095     if (!has_pointers) has_pointers = type_has_pointers(type);
2096
2097     array = find_array_or_string_in_struct(type);
2098     if (array && !processed(array->type))
2099         array_offset
2100             = is_string_type(array->attrs, array->type)
2101             ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2102             : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2103
2104     corroff = *tfsoff;
2105     write_descriptors(file, type, tfsoff);
2106
2107     start_offset = *tfsoff;
2108     update_tfsoff(type, start_offset, file);
2109     print_start_tfs_comment(file, type, start_offset);
2110     print_file(file, 2, "0x%x,\t/* %s */\n", real_type, string_of_type(real_type));
2111     print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2112     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2113     *tfsoff += 4;
2114
2115     if (array)
2116     {
2117         unsigned int absoff = array->type->typestring_offset;
2118         short reloff = absoff - *tfsoff;
2119         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2120                    reloff, reloff, absoff);
2121         *tfsoff += 2;
2122     }
2123     else if (real_type == RPC_FC_BOGUS_STRUCT)
2124     {
2125         print_file(file, 2, "NdrFcShort(0x0),\n");
2126         *tfsoff += 2;
2127     }
2128
2129     if (real_type == RPC_FC_BOGUS_STRUCT)
2130     {
2131         /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2132            nothing is written to file yet.  On the actual writing pass,
2133            this will have been updated.  */
2134         unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2135         int reloff = absoff - *tfsoff;
2136         assert( reloff >= 0 );
2137         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2138                    reloff, reloff, absoff);
2139         *tfsoff += 2;
2140     }
2141     else if ((real_type == RPC_FC_PSTRUCT) ||
2142              (real_type == RPC_FC_CPSTRUCT) ||
2143              (real_type == RPC_FC_CVSTRUCT && has_pointers))
2144     {
2145         print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2146         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2147         *tfsoff += 2;
2148         write_pointer_description(file, type, tfsoff);
2149         print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2150         *tfsoff += 1;
2151     }
2152
2153     write_struct_members(file, type, &corroff, tfsoff);
2154
2155     if (real_type == RPC_FC_BOGUS_STRUCT)
2156     {
2157         const var_t *f;
2158
2159         type->ptrdesc = *tfsoff;
2160         if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2161         {
2162             type_t *ft = f->type;
2163             if (is_ptr(ft))
2164             {
2165                 if (is_string_type(f->attrs, ft))
2166                     write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2167                 else
2168                     write_pointer_tfs(file, ft, tfsoff);
2169             }
2170             else if (!ft->declarray && is_conformant_array(ft))
2171             {
2172                 unsigned int absoff = ft->typestring_offset;
2173                 short reloff = absoff - (*tfsoff + 2);
2174                 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2175                 /* FIXME: We need to store pointer attributes for arrays
2176                    so we don't lose pointer_default info.  */
2177                 if (ptr_type == 0)
2178                     ptr_type = RPC_FC_UP;
2179                 print_file(file, 0, "/* %d */\n", *tfsoff);
2180                 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2181                            string_of_type(ptr_type));
2182                 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2183                            reloff, reloff, absoff);
2184                 *tfsoff += 4;
2185             }
2186         }
2187         if (type->ptrdesc == *tfsoff)
2188             type->ptrdesc = 0;
2189     }
2190
2191     current_structure = save_current_structure;
2192     return start_offset;
2193 }
2194
2195 static unsigned int write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2196                                            unsigned char flags, unsigned int offset,
2197                                            unsigned int *typeformat_offset)
2198 {
2199     unsigned int start_offset = *typeformat_offset;
2200     short reloff = offset - (*typeformat_offset + 2);
2201     int in_attr, out_attr;
2202     in_attr = is_attr(attrs, ATTR_IN);
2203     out_attr = is_attr(attrs, ATTR_OUT);
2204     if (!in_attr && !out_attr) in_attr = 1;
2205
2206     if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2207         flags |= 0x04;
2208
2209     print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2210                pointer_type,
2211                flags,
2212                string_of_type(pointer_type));
2213     if (file)
2214     {
2215         if (flags & 0x04)
2216             fprintf(file, " [allocated_on_stack]");
2217         if (flags & 0x10)
2218             fprintf(file, " [pointer_deref]");
2219         fprintf(file, " */\n");
2220     }
2221
2222     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", reloff, offset);
2223     *typeformat_offset += 4;
2224
2225     return start_offset;
2226 }
2227
2228 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2229 {
2230     if (t == NULL)
2231     {
2232         print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2233     }
2234     else if (is_base_type(t->type))
2235     {
2236         print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2237                    t->type, string_of_type(t->type));
2238     }
2239     else if (t->typestring_offset)
2240     {
2241         short reloff = t->typestring_offset - *tfsoff;
2242         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2243                    reloff, reloff, t->typestring_offset);
2244     }
2245     else
2246         error("write_branch_type: type unimplemented (0x%x)\n", t->type);
2247
2248     *tfsoff += 2;
2249 }
2250
2251 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2252 {
2253     unsigned int align = 0;
2254     unsigned int start_offset;
2255     unsigned int size = type_memsize(type, &align);
2256     var_list_t *fields;
2257     unsigned int nbranch = 0;
2258     type_t *deftype = NULL;
2259     short nodeftype = 0xffff;
2260     var_t *f;
2261
2262     guard_rec(type);
2263
2264     fields = type_union_get_cases(type);
2265
2266     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2267     {
2268         expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2269         if (cases)
2270             nbranch += list_count(cases);
2271         if (f->type)
2272             write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2273     }
2274
2275     start_offset = *tfsoff;
2276     update_tfsoff(type, start_offset, file);
2277     print_start_tfs_comment(file, type, start_offset);
2278     if (type->type == RPC_FC_ENCAPSULATED_UNION)
2279     {
2280         const var_t *sv = type_union_get_switch_value(type);
2281         const type_t *st = sv->type;
2282
2283         switch (st->type)
2284         {
2285         case RPC_FC_CHAR:
2286         case RPC_FC_SMALL:
2287         case RPC_FC_USMALL:
2288         case RPC_FC_SHORT:
2289         case RPC_FC_USHORT:
2290         case RPC_FC_LONG:
2291         case RPC_FC_ULONG:
2292         case RPC_FC_ENUM16:
2293         case RPC_FC_ENUM32:
2294             print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2295             print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2296                        0x40 | st->type, string_of_type(st->type));
2297             *tfsoff += 2;
2298             break;
2299         default:
2300             error("union switch type must be an integer, char, or enum\n");
2301         }
2302     }
2303     else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2304     {
2305         static const expr_t dummy_expr;  /* FIXME */
2306         const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2307
2308         switch (st->type)
2309         {
2310         case RPC_FC_CHAR:
2311         case RPC_FC_SMALL:
2312         case RPC_FC_USMALL:
2313         case RPC_FC_SHORT:
2314         case RPC_FC_USHORT:
2315         case RPC_FC_LONG:
2316         case RPC_FC_ULONG:
2317         case RPC_FC_ENUM16:
2318         case RPC_FC_ENUM32:
2319             print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
2320             print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2321                        st->type, string_of_type(st->type));
2322             *tfsoff += 2;
2323             break;
2324         default:
2325             error("union switch type must be an integer, char, or enum\n");
2326         }
2327
2328         *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2329         print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2330         *tfsoff += 2;
2331     }
2332
2333     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2334     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2335     *tfsoff += 4;
2336
2337     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2338     {
2339         type_t *ft = f->type;
2340         expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2341         int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2342         expr_t *c;
2343
2344         if (cases == NULL && !deflt)
2345             error("union field %s with neither case nor default attribute\n", f->name);
2346
2347         if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2348         {
2349             /* MIDL doesn't check for duplicate cases, even though that seems
2350                like a reasonable thing to do, it just dumps them to the TFS
2351                like we're going to do here.  */
2352             print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2353             *tfsoff += 4;
2354             write_branch_type(file, ft, tfsoff);
2355         }
2356
2357         /* MIDL allows multiple default branches, even though that seems
2358            illogical, it just chooses the last one, which is what we will
2359            do.  */
2360         if (deflt)
2361         {
2362             deftype = ft;
2363             nodeftype = 0;
2364         }
2365     }
2366
2367     if (deftype)
2368     {
2369         write_branch_type(file, deftype, tfsoff);
2370     }
2371     else
2372     {
2373         print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2374         *tfsoff += 2;
2375     }
2376
2377     return start_offset;
2378 }
2379
2380 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2381                                  unsigned int *typeformat_offset)
2382 {
2383     unsigned int i;
2384     unsigned int start_offset = *typeformat_offset;
2385     expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2386
2387     if (iid)
2388     {
2389         print_file(file, 2, "0x2f,  /* FC_IP */\n");
2390         print_file(file, 2, "0x5c,  /* FC_PAD */\n");
2391         *typeformat_offset
2392             += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2393     }
2394     else
2395     {
2396         const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2397         const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2398
2399         if (! uuid)
2400             error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2401
2402         update_tfsoff(type, start_offset, file);
2403         print_start_tfs_comment(file, type, start_offset);
2404         print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2405         print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2406         print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2407         print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2408         print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2409         for (i = 0; i < 8; ++i)
2410             print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2411
2412         if (file)
2413             fprintf(file, "\n");
2414
2415         *typeformat_offset += 18;
2416     }
2417     return start_offset;
2418 }
2419
2420 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2421                                             const var_t *var,
2422                                             unsigned int *typeformat_offset)
2423 {
2424     unsigned int start_offset = *typeformat_offset;
2425     unsigned char flags = 0;
2426
2427     if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2428         flags |= NDR_STRICT_CONTEXT_HANDLE;
2429
2430     if (is_ptr(type))
2431         flags |= 0x80;
2432     if (is_attr(var->attrs, ATTR_IN))
2433     {
2434         flags |= 0x40;
2435         if (!is_attr(var->attrs, ATTR_OUT))
2436             flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2437     }
2438     if (is_attr(var->attrs, ATTR_OUT))
2439         flags |= 0x20;
2440
2441     WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2442     print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2443     /* return and can't be null values overlap */
2444     if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2445         print_file(file, 0, "can't be null, ");
2446     if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2447         print_file(file, 0, "serialize, ");
2448     if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2449         print_file(file, 0, "no serialize, ");
2450     if (flags & NDR_STRICT_CONTEXT_HANDLE)
2451         print_file(file, 0, "strict, ");
2452     if ((flags & 0x21) == 0x20)
2453         print_file(file, 0, "out, ");
2454     if ((flags & 0x21) == 0x21)
2455         print_file(file, 0, "return, ");
2456     if (flags & 0x40)
2457         print_file(file, 0, "in, ");
2458     if (flags & 0x80)
2459         print_file(file, 0, "via ptr, ");
2460     print_file(file, 0, "*/\n");
2461     print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2462     print_file(file, 2, "0, /* FIXME: param num */\n");
2463     *typeformat_offset += 4;
2464
2465     return start_offset;
2466 }
2467
2468 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2469                                                type_t *type, const var_t *var,
2470                                                unsigned int *typeformat_offset)
2471 {
2472     unsigned int offset;
2473
2474     if (is_context_handle(type))
2475         return write_contexthandle_tfs(file, type, var, typeformat_offset);
2476
2477     if (is_user_type(type))
2478     {
2479         write_user_tfs(file, type, typeformat_offset);
2480         return type->typestring_offset;
2481     }
2482
2483     if (is_string_type(var->attrs, type))
2484         return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2485
2486     if (is_array(type))
2487     {
2488         int ptr_type;
2489         unsigned int off;
2490         off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2491         ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2492         /* Top level pointers to conformant arrays may be handled specially
2493            since we can bypass the pointer, but if the array is buried
2494            beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2495            always need to write the pointer.  */
2496         if (!ptr_type && var->type != type)
2497           /* FIXME:  This should use pointer_default, but the information
2498              isn't kept around for arrays.  */
2499           ptr_type = RPC_FC_UP;
2500         if (ptr_type && ptr_type != RPC_FC_RP)
2501         {
2502             unsigned int absoff = type->typestring_offset;
2503             short reloff = absoff - (*typeformat_offset + 2);
2504             off = *typeformat_offset;
2505             print_file(file, 0, "/* %d */\n", off);
2506             print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2507                        string_of_type(ptr_type));
2508             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2509                        reloff, reloff, absoff);
2510             *typeformat_offset += 4;
2511         }
2512         return off;
2513     }
2514
2515     if (!is_ptr(type))
2516     {
2517         /* basic types don't need a type format string */
2518         if (is_base_type(type->type))
2519             return 0;
2520
2521         if (processed(type)) return type->typestring_offset;
2522
2523         switch (type->type)
2524         {
2525         case RPC_FC_STRUCT:
2526         case RPC_FC_PSTRUCT:
2527         case RPC_FC_CSTRUCT:
2528         case RPC_FC_CPSTRUCT:
2529         case RPC_FC_CVSTRUCT:
2530         case RPC_FC_BOGUS_STRUCT:
2531             return write_struct_tfs(file, type, var->name, typeformat_offset);
2532         case RPC_FC_ENCAPSULATED_UNION:
2533         case RPC_FC_NON_ENCAPSULATED_UNION:
2534             return write_union_tfs(file, type, typeformat_offset);
2535         case RPC_FC_IGNORE:
2536         case RPC_FC_BIND_PRIMITIVE:
2537             /* nothing to do */
2538             return 0;
2539         default:
2540             error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2541         }
2542     }
2543     else if (last_ptr(type))
2544     {
2545         unsigned int start_offset = *typeformat_offset;
2546         int in_attr = is_attr(var->attrs, ATTR_IN);
2547         int out_attr = is_attr(var->attrs, ATTR_OUT);
2548         const type_t *base = type_pointer_get_ref(type);
2549
2550         if (base->type == RPC_FC_IP
2551             || (base->type == 0
2552                 && is_attr(var->attrs, ATTR_IIDIS)))
2553         {
2554             return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2555         }
2556
2557         /* special case for pointers to base types */
2558         if (is_base_type(base->type))
2559         {
2560             print_file(file, indent, "0x%x, 0x%x,    /* %s %s[simple_pointer] */\n",
2561                        type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
2562                        string_of_type(type->type),
2563                        (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2564             print_file(file, indent, "0x%02x,    /* %s */\n", base->type, string_of_type(base->type));
2565             print_file(file, indent, "0x5c,          /* FC_PAD */\n");
2566             *typeformat_offset += 4;
2567             return start_offset;
2568         }
2569     }
2570
2571     assert(is_ptr(type));
2572
2573     offset = write_typeformatstring_var(file, indent, func,
2574                                         type_pointer_get_ref(type), var,
2575                                         typeformat_offset);
2576     if (file)
2577         fprintf(file, "/* %2u */\n", *typeformat_offset);
2578     return write_pointer_only_tfs(file, var->attrs, type->type,
2579                            !last_ptr(type) ? 0x10 : 0,
2580                            offset, typeformat_offset);
2581 }
2582
2583 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2584                                 const char *name, int write_ptr, unsigned int *tfsoff)
2585 {
2586     int retmask = 0;
2587
2588     if (is_user_type(type))
2589     {
2590         write_user_tfs(file, type, tfsoff);
2591     }
2592     else if (is_string_type(attrs, type))
2593     {
2594         write_string_tfs(file, attrs, type, name, tfsoff);
2595     }
2596     else if (is_ptr(type))
2597     {
2598         type_t *ref = type_pointer_get_ref(type);
2599
2600         if (ref->type == RPC_FC_IP
2601             || (ref->type == 0
2602                 && is_attr(attrs, ATTR_IIDIS)))
2603         {
2604             write_ip_tfs(file, attrs, type, tfsoff);
2605         }
2606         else
2607         {
2608             if (!processed(ref) && !is_base_type(ref->type))
2609                 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2610
2611             if (write_ptr)
2612                 write_pointer_tfs(file, type, tfsoff);
2613
2614             retmask |= 1;
2615         }
2616     }
2617     else if (type->declarray && is_conformant_array(type))
2618         ;    /* conformant arrays and strings are handled specially */
2619     else if (is_array(type))
2620     {
2621         write_array_tfs(file, attrs, type, name, tfsoff);
2622         if (is_conformant_array(type))
2623             retmask |= 1;
2624     }
2625     else if (is_struct(type->type))
2626     {
2627         if (!processed(type))
2628             write_struct_tfs(file, type, name, tfsoff);
2629     }
2630     else if (is_union(type->type))
2631     {
2632         if (!processed(type))
2633             write_union_tfs(file, type, tfsoff);
2634     }
2635     else if (!is_base_type(type->type))
2636         error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2637               name, type->type);
2638
2639     return retmask;
2640 }
2641
2642 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2643                                       type_pred_t pred, unsigned int *typeformat_offset)
2644 {
2645     const var_t *var;
2646     const statement_t *stmt;
2647
2648     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2649     {
2650         const type_t *iface;
2651         const statement_t *stmt_func;
2652
2653         if (stmt->type == STMT_LIBRARY)
2654         {
2655             process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2656             continue;
2657         }
2658         else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
2659             continue;
2660
2661         iface = stmt->u.type;
2662         if (!pred(iface))
2663             continue;
2664
2665         current_iface = iface;
2666         STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2667         {
2668             const var_t *func = stmt_func->u.var;
2669                 if (is_local(func->attrs)) continue;
2670
2671                 if (!is_void(type_function_get_rettype(func->type)))
2672                 {
2673                     var_t v = *func;
2674                     v.type = type_function_get_rettype(func->type);
2675                     update_tfsoff(type_function_get_rettype(func->type),
2676                                   write_typeformatstring_var(
2677                                       file, 2, NULL,
2678                                       type_function_get_rettype(func->type),
2679                                       &v, typeformat_offset),
2680                                   file);
2681                 }
2682
2683                 current_func = func;
2684                 if (type_get_function_args(func->type))
2685                     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2686                         update_tfsoff(
2687                             var->type,
2688                             write_typeformatstring_var(
2689                                 file, 2, func, var->type, var,
2690                                 typeformat_offset),
2691                             file);
2692         }
2693     }
2694
2695     return *typeformat_offset + 1;
2696 }
2697
2698 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2699 {
2700     unsigned int typeformat_offset = 2;
2701
2702     return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2703 }
2704
2705
2706 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2707 {
2708     int indent = 0;
2709
2710     print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2711     print_file(file, indent, "{\n");
2712     indent++;
2713     print_file(file, indent, "0,\n");
2714     print_file(file, indent, "{\n");
2715     indent++;
2716     print_file(file, indent, "NdrFcShort(0x0),\n");
2717
2718     set_all_tfswrite(TRUE);
2719     process_tfs(file, stmts, pred);
2720
2721     print_file(file, indent, "0x0\n");
2722     indent--;
2723     print_file(file, indent, "}\n");
2724     indent--;
2725     print_file(file, indent, "};\n");
2726     print_file(file, indent, "\n");
2727 }
2728
2729 static unsigned int get_required_buffer_size_type(
2730     const type_t *type, const char *name, unsigned int *alignment)
2731 {
2732     const char *uname;
2733     const type_t *utype;
2734
2735     *alignment = 0;
2736     if ((utype = get_user_type(type, &uname)))
2737     {
2738         return get_required_buffer_size_type(utype, uname, alignment);
2739     }
2740     else
2741     {
2742         switch (get_struct_type(type))
2743         {
2744         case RPC_FC_BYTE:
2745         case RPC_FC_CHAR:
2746         case RPC_FC_USMALL:
2747         case RPC_FC_SMALL:
2748             *alignment = 4;
2749             return 1;
2750
2751         case RPC_FC_WCHAR:
2752         case RPC_FC_USHORT:
2753         case RPC_FC_SHORT:
2754         case RPC_FC_ENUM16:
2755             *alignment = 4;
2756             return 2;
2757
2758         case RPC_FC_ULONG:
2759         case RPC_FC_LONG:
2760         case RPC_FC_ENUM32:
2761         case RPC_FC_FLOAT:
2762         case RPC_FC_ERROR_STATUS_T:
2763             *alignment = 4;
2764             return 4;
2765
2766         case RPC_FC_HYPER:
2767         case RPC_FC_DOUBLE:
2768             *alignment = 8;
2769             return 8;
2770
2771         case RPC_FC_IGNORE:
2772         case RPC_FC_BIND_PRIMITIVE:
2773             return 0;
2774
2775         case RPC_FC_STRUCT:
2776             if (!type_struct_get_fields(type)) return 0;
2777             return fields_memsize(type_struct_get_fields(type), alignment);
2778
2779         case RPC_FC_RP:
2780         {
2781             const type_t *ref = type_pointer_get_ref(type);
2782             return is_base_type( ref->type ) || get_struct_type(ref) == RPC_FC_STRUCT ?
2783                 get_required_buffer_size_type( ref, name, alignment ) : 0;
2784         }
2785
2786         case RPC_FC_SMFARRAY:
2787         case RPC_FC_LGFARRAY:
2788             return type_array_get_dim(type) * get_required_buffer_size_type(type_array_get_element(type), name, alignment);
2789
2790         default:
2791             return 0;
2792         }
2793     }
2794 }
2795
2796 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2797 {
2798     int in_attr = is_attr(var->attrs, ATTR_IN);
2799     int out_attr = is_attr(var->attrs, ATTR_OUT);
2800
2801     if (!in_attr && !out_attr)
2802         in_attr = 1;
2803
2804     *alignment = 0;
2805
2806     if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2807         pass == PASS_RETURN)
2808     {
2809         if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2810         {
2811             *alignment = 4;
2812             return 20;
2813         }
2814
2815         if (!is_string_type(var->attrs, var->type))
2816             return get_required_buffer_size_type(var->type, var->name,
2817                                                  alignment);
2818     }
2819     return 0;
2820 }
2821
2822 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
2823 {
2824     const var_t *var;
2825     unsigned int total_size = 0, alignment;
2826
2827     if (type_get_function_args(func->type))
2828     {
2829         LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2830         {
2831             total_size += get_required_buffer_size(var, &alignment, pass);
2832             total_size += alignment;
2833         }
2834     }
2835
2836     if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
2837     {
2838         var_t v = *func;
2839         v.type = type_function_get_rettype(func->type);
2840         total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2841         total_size += alignment;
2842     }
2843     return total_size;
2844 }
2845
2846 static void print_phase_function(FILE *file, int indent, const char *type,
2847                                  const char *local_var_prefix, enum remoting_phase phase,
2848                                  const var_t *var, unsigned int type_offset)
2849 {
2850     const char *function;
2851     switch (phase)
2852     {
2853     case PHASE_BUFFERSIZE:
2854         function = "BufferSize";
2855         break;
2856     case PHASE_MARSHAL:
2857         function = "Marshall";
2858         break;
2859     case PHASE_UNMARSHAL:
2860         function = "Unmarshall";
2861         break;
2862     case PHASE_FREE:
2863         function = "Free";
2864         break;
2865     default:
2866         assert(0);
2867         return;
2868     }
2869
2870     print_file(file, indent, "Ndr%s%s(\n", type, function);
2871     indent++;
2872     print_file(file, indent, "&__frame->_StubMsg,\n");
2873     print_file(file, indent, "%s%s%s%s%s,\n",
2874                (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
2875                (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2876                local_var_prefix,
2877                (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
2878                var->name);
2879     print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2880                type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
2881     if (phase == PHASE_UNMARSHAL)
2882         print_file(file, indent, "0);\n");
2883     indent--;
2884 }
2885
2886 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
2887                           enum remoting_phase phase, enum pass pass, const var_t *var,
2888                           const char *varname)
2889 {
2890     type_t *type = var->type;
2891     unsigned int size;
2892     unsigned int alignment = 0;
2893     unsigned char rtype;
2894
2895     /* no work to do for other phases, buffer sizing is done elsewhere */
2896     if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
2897         return;
2898
2899     rtype = is_ptr(type) ? type_pointer_get_ref(type)->type : type->type;
2900
2901     switch (rtype)
2902     {
2903         case RPC_FC_BYTE:
2904         case RPC_FC_CHAR:
2905         case RPC_FC_SMALL:
2906         case RPC_FC_USMALL:
2907             size = 1;
2908             alignment = 1;
2909             break;
2910
2911         case RPC_FC_WCHAR:
2912         case RPC_FC_USHORT:
2913         case RPC_FC_SHORT:
2914         case RPC_FC_ENUM16:
2915             size = 2;
2916             alignment = 2;
2917             break;
2918
2919         case RPC_FC_ULONG:
2920         case RPC_FC_LONG:
2921         case RPC_FC_ENUM32:
2922         case RPC_FC_FLOAT:
2923         case RPC_FC_ERROR_STATUS_T:
2924             size = 4;
2925             alignment = 4;
2926             break;
2927
2928         case RPC_FC_HYPER:
2929         case RPC_FC_DOUBLE:
2930             size = 8;
2931             alignment = 8;
2932             break;
2933
2934         case RPC_FC_IGNORE:
2935         case RPC_FC_BIND_PRIMITIVE:
2936             /* no marshalling needed */
2937             return;
2938
2939         default:
2940             error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2941             size = 0;
2942     }
2943
2944     if (phase == PHASE_MARSHAL)
2945         print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
2946     print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2947                 alignment - 1, alignment - 1);
2948
2949     if (phase == PHASE_MARSHAL)
2950     {
2951         print_file(file, indent, "*(");
2952         write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2953         if (is_ptr(type))
2954             fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2955         else
2956             fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2957         fprintf(file, "%s%s", local_var_prefix, varname);
2958         fprintf(file, ";\n");
2959     }
2960     else if (phase == PHASE_UNMARSHAL)
2961     {
2962         print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2963         write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2964         fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2965         print_file(file, indent, "{\n");
2966         print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
2967         print_file(file, indent, "}\n");
2968         print_file(file, indent, "%s%s%s",
2969                    (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
2970                    local_var_prefix, varname);
2971         if (pass == PASS_IN && is_ptr(type))
2972             fprintf(file, " = (");
2973         else
2974             fprintf(file, " = *(");
2975         write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2976         fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2977     }
2978
2979     print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2980     write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
2981     fprintf(file, ");\n");
2982 }
2983
2984 /* returns whether the MaxCount, Offset or ActualCount members need to be
2985  * filled in for the specified phase */
2986 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2987 {
2988     return (phase != PHASE_UNMARSHAL);
2989 }
2990
2991 expr_t *get_size_is_expr(const type_t *t, const char *name)
2992 {
2993     expr_t *x = NULL;
2994
2995     for ( ; is_array(t); t = type_array_get_element(t))
2996         if (type_array_has_conformance(t))
2997         {
2998             if (!x)
2999                 x = type_array_get_conformance(t);
3000             else
3001                 error("%s: multidimensional conformant"
3002                       " arrays not supported at the top level\n",
3003                       name);
3004         }
3005
3006     return x;
3007 }
3008
3009 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3010                                               enum remoting_phase phase, const var_t *var)
3011 {
3012     const type_t *type = var->type;
3013     /* get fundamental type for the argument */
3014     for (;;)
3015     {
3016         if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3017             break;
3018         else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3019             break;
3020         else if (is_array(type) || is_string_type(var->attrs, type))
3021         {
3022             if (is_conformance_needed_for_phase(phase) && is_array(type))
3023             {
3024                 if (type_array_has_conformance(type))
3025                 {
3026                     print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3027                     write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3028                     fprintf(file, ";\n\n");
3029                 }
3030                 if (type_array_has_variance(type))
3031                 {
3032                     print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3033                     print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3034                     write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3035                     fprintf(file, ";\n\n");
3036                 }
3037             }
3038             break;
3039         }
3040         else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
3041         {
3042             if (is_conformance_needed_for_phase(phase))
3043             {
3044                 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3045                 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3046                 fprintf(file, ";\n\n");
3047             }
3048             break;
3049         }
3050         else if (type->type == RPC_FC_IP || is_void(type))
3051         {
3052             expr_t *iid;
3053
3054             if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3055             {
3056                 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3057                 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3058                 fprintf( file, ";\n\n" );
3059             }
3060             break;
3061         }
3062         else if (is_ptr(type))
3063             type = type_pointer_get_ref(type);
3064         else
3065             break;
3066     }
3067 }
3068
3069 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3070                                enum pass pass, enum remoting_phase phase, const var_t *var)
3071 {
3072     int in_attr, out_attr, pointer_type;
3073     const type_t *type = var->type;
3074     unsigned char rtype;
3075     unsigned int start_offset = type->typestring_offset;
3076
3077     pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3078     if (!pointer_type)
3079         pointer_type = RPC_FC_RP;
3080
3081     in_attr = is_attr(var->attrs, ATTR_IN);
3082     out_attr = is_attr(var->attrs, ATTR_OUT);
3083     if (!in_attr && !out_attr)
3084         in_attr = 1;
3085
3086     if (phase != PHASE_FREE)
3087         switch (pass)
3088         {
3089         case PASS_IN:
3090             if (!in_attr) return;
3091             break;
3092         case PASS_OUT:
3093             if (!out_attr) return;
3094             break;
3095         case PASS_RETURN:
3096             break;
3097         }
3098
3099     write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3100     rtype = get_struct_type(type);
3101
3102     if (is_context_handle(type))
3103     {
3104         if (phase == PHASE_MARSHAL)
3105         {
3106             if (pass == PASS_IN)
3107             {
3108                 /* if the context_handle attribute appears in the chain of types
3109                  * without pointers being followed, then the context handle must
3110                  * be direct, otherwise it is a pointer */
3111                 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3112                 print_file(file, indent, "NdrClientContextMarshall(\n");
3113                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3114                 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3115                 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3116             }
3117             else
3118             {
3119                 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3120                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3121                 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3122                 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3123                 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3124             }
3125         }
3126         else if (phase == PHASE_UNMARSHAL)
3127         {
3128             if (pass == PASS_OUT)
3129             {
3130                 if (!in_attr)
3131                     print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3132                 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3133                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3134                 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3135                 print_file(file, indent + 1, "__frame->_Handle);\n");
3136             }
3137             else
3138             {
3139                 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3140                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3141                 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3142             }
3143         }
3144     }
3145     else if (is_user_type(var->type))
3146     {
3147         print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3148     }
3149     else if (is_string_type(var->attrs, var->type))
3150     {
3151         if (is_array(type) && !is_conformant_array(type))
3152             print_phase_function(file, indent, "NonConformantString", local_var_prefix,
3153                                  phase, var, start_offset);
3154         else
3155         {
3156             if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
3157                 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
3158                                      start_offset - (is_conformant_array(type) ? 4 : 2));
3159             else
3160                 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3161                                      phase, var, start_offset);
3162         }
3163     }
3164     else if (is_array(type))
3165     {
3166         unsigned char tc = get_array_type( type );
3167         const char *array_type = "FixedArray";
3168
3169         /* We already have the size_is expression since it's at the
3170            top level, but do checks for multidimensional conformant
3171            arrays.  When we handle them, we'll need to extend this
3172            function to return a list, and then we'll actually use
3173            the return value.  */
3174         get_size_is_expr(type, var->name);
3175
3176         if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3177         {
3178             array_type = "VaryingArray";
3179         }
3180         else if (tc == RPC_FC_CARRAY)
3181         {
3182             array_type = "ConformantArray";
3183         }
3184         else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3185         {
3186             array_type = (tc == RPC_FC_BOGUS_ARRAY
3187                           ? "ComplexArray"
3188                           : "ConformantVaryingArray");
3189         }
3190
3191         if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3192         print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3193         if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3194         {
3195             /* these are all unmarshalled by allocating memory */
3196             if (tc == RPC_FC_BOGUS_ARRAY ||
3197                 tc == RPC_FC_CVARRAY ||
3198                 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3199                 (tc == RPC_FC_CARRAY && !in_attr))
3200             {
3201                 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3202                 indent++;
3203                 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3204             }
3205         }
3206     }
3207     else if (!is_ptr(var->type) && is_base_type(rtype))
3208     {
3209         if (phase != PHASE_FREE)
3210             print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3211     }
3212     else if (!is_ptr(var->type))
3213     {
3214         switch (rtype)
3215         {
3216         case RPC_FC_STRUCT:
3217         case RPC_FC_PSTRUCT:
3218             print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3219             break;
3220         case RPC_FC_CSTRUCT:
3221         case RPC_FC_CPSTRUCT:
3222             print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3223             break;
3224         case RPC_FC_CVSTRUCT:
3225             print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3226             break;
3227         case RPC_FC_BOGUS_STRUCT:
3228             print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3229             break;
3230         default:
3231             error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
3232         }
3233     }
3234     else
3235     {
3236         const type_t *ref = type_pointer_get_ref(type);
3237         if (type->type == RPC_FC_RP && is_base_type(ref->type))
3238         {
3239             if (phase != PHASE_FREE)
3240                 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3241         }
3242         else if (type->type == RPC_FC_RP && get_struct_type(ref) == RPC_FC_STRUCT &&
3243                  !is_user_type(ref))
3244         {
3245             if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3246                 print_phase_function(file, indent, "SimpleStruct",
3247                                      local_var_prefix, phase, var,
3248                                      ref->typestring_offset);
3249         }
3250         else
3251         {
3252             if (ref->type == RPC_FC_IP)
3253                 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3254             else
3255                 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3256         }
3257     }
3258     fprintf(file, "\n");
3259 }
3260
3261 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3262                               enum pass pass, enum remoting_phase phase)
3263 {
3264     if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3265     {
3266         unsigned int size = get_function_buffer_size( func, pass );
3267         print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3268     }
3269
3270     if (pass == PASS_RETURN)
3271     {
3272         var_t var;
3273         var = *func;
3274         var.type = type_function_get_rettype(func->type);
3275         var.name = xstrdup( "_RetVal" );
3276         write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3277         free( var.name );
3278     }
3279     else
3280     {
3281         const var_t *var;
3282         if (!type_get_function_args(func->type))
3283             return;
3284         LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3285             write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3286     }
3287 }
3288
3289
3290 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3291 {
3292     return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3293 }
3294
3295
3296 unsigned int get_size_procformatstring_func(const var_t *func)
3297 {
3298     const var_t *var;
3299     unsigned int size = 0;
3300
3301     /* argument list size */
3302     if (type_get_function_args(func->type))
3303         LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3304             size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3305
3306     /* return value size */
3307     if (is_void(type_function_get_rettype(func->type)))
3308         size += 2; /* FC_END and FC_PAD */
3309     else
3310         size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3311
3312     return size;
3313 }
3314
3315 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3316 {
3317     const statement_t *stmt;
3318     unsigned int size = 1;
3319
3320     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3321     {
3322         const type_t *iface;
3323         const statement_t *stmt_func;
3324
3325         if (stmt->type == STMT_LIBRARY)
3326         {
3327             size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3328             continue;
3329         }
3330         else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
3331             continue;
3332
3333         iface = stmt->u.type;
3334         if (!pred(iface))
3335             continue;
3336
3337         STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3338         {
3339             const var_t *func = stmt_func->u.var;
3340             if (!is_local(func->attrs))
3341                 size += get_size_procformatstring_func( func );
3342         }
3343     }
3344     return size;
3345 }
3346
3347 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3348 {
3349     set_all_tfswrite(FALSE);
3350     return process_tfs(NULL, stmts, pred);
3351 }
3352
3353 void declare_stub_args( FILE *file, int indent, const var_t *func )
3354 {
3355     int in_attr, out_attr;
3356     int i = 0;
3357     const var_t *var;
3358
3359     /* declare return value '_RetVal' */
3360     if (!is_void(type_function_get_rettype(func->type)))
3361     {
3362         print_file(file, indent, "%s", "");
3363         write_type_decl_left(file, type_function_get_rettype(func->type));
3364         fprintf(file, " _RetVal;\n");
3365     }
3366
3367     if (!type_get_function_args(func->type))
3368         return;
3369
3370     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3371     {
3372         int is_string = is_string_type(var->attrs, var->type);
3373
3374         in_attr = is_attr(var->attrs, ATTR_IN);
3375         out_attr = is_attr(var->attrs, ATTR_OUT);
3376         if (!out_attr && !in_attr)
3377             in_attr = 1;
3378
3379         if (is_context_handle(var->type))
3380             print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3381         else
3382         {
3383             if (!in_attr && !is_conformant_array(var->type) && !is_string)
3384             {
3385                 type_t *type_to_print;
3386                 print_file(file, indent, "%s", "");
3387                 if (var->type->declarray)
3388                     type_to_print = var->type;
3389                 else
3390                     type_to_print = type_pointer_get_ref(var->type);
3391                 write_type_decl(file, type_to_print, "_W%u", i++);
3392                 fprintf(file, ";\n");
3393             }
3394
3395             print_file(file, indent, "%s", "");
3396             write_type_decl_left(file, var->type);
3397             fprintf(file, " ");
3398             if (var->type->declarray) {
3399                 fprintf(file, "(*%s)", var->name);
3400             } else
3401                 fprintf(file, "%s", var->name);
3402             write_type_right(file, var->type, FALSE);
3403             fprintf(file, ";\n");
3404
3405             if (decl_indirect(var->type))
3406                 print_file(file, indent, "void *_p_%s;\n", var->name);
3407         }
3408     }
3409 }
3410
3411
3412 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3413 {
3414     int in_attr, out_attr;
3415     int i = 0, sep = 0;
3416     const var_t *var;
3417
3418     if (!type_get_function_args(func->type))
3419         return;
3420
3421     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3422     {
3423         int is_string = is_string_type(var->attrs, var->type);
3424         in_attr = is_attr(var->attrs, ATTR_IN);
3425         out_attr = is_attr(var->attrs, ATTR_OUT);
3426         if (!out_attr && !in_attr)
3427             in_attr = 1;
3428
3429         if (!in_attr)
3430         {
3431             print_file(file, indent, "%s%s", local_var_prefix, var->name);
3432
3433             if (is_context_handle(var->type))
3434             {
3435                 fprintf(file, " = NdrContextHandleInitialize(\n");
3436                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3437                 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3438                            var->type->typestring_offset);
3439             }
3440             else if (is_array(var->type) &&
3441                      type_array_has_conformance(var->type))
3442             {
3443                 unsigned int size, align = 0;
3444                 type_t *type = var->type;
3445
3446                 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3447                 for ( ;
3448                      is_array(type) && type_array_has_conformance(type);
3449                      type = type_array_get_element(type))
3450                 {
3451                     write_expr(file, type_array_get_conformance(type), TRUE,
3452                                TRUE, NULL, NULL, local_var_prefix);
3453                     fprintf(file, " * ");
3454                 }
3455                 size = type_memsize(type, &align);
3456                 fprintf(file, "%u);\n", size);
3457             }
3458             else if (!is_string)
3459             {
3460                 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3461                 if (is_ptr(var->type) && !last_ptr(var->type))
3462                     print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3463                 i++;
3464             }
3465
3466             sep = 1;
3467         }
3468     }
3469     if (sep)
3470         fprintf(file, "\n");
3471 }
3472
3473
3474 int write_expr_eval_routines(FILE *file, const char *iface)
3475 {
3476     static const char *var_name = "pS";
3477     static const char *var_name_expr = "pS->";
3478     int result = 0;
3479     struct expr_eval_routine *eval;
3480     unsigned short callback_offset = 0;
3481
3482     LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3483     {
3484         const char *name = eval->structure->name;
3485         result = 1;
3486
3487         print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3488                    iface, name, callback_offset);
3489         print_file(file, 0, "{\n");
3490         print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3491                     name, var_name, name, eval->baseoff);
3492         print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3493         print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3494         write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3495         fprintf(file, ";\n");
3496         print_file(file, 0, "}\n\n");
3497         callback_offset++;
3498     }
3499     return result;
3500 }
3501
3502 void write_expr_eval_routine_list(FILE *file, const char *iface)
3503 {
3504     struct expr_eval_routine *eval;
3505     struct expr_eval_routine *cursor;
3506     unsigned short callback_offset = 0;
3507
3508     fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3509     fprintf(file, "{\n");
3510
3511     LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3512     {
3513         const char *name = eval->structure->name;
3514         print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3515         callback_offset++;
3516         list_remove(&eval->entry);
3517         free(eval);
3518     }
3519
3520     fprintf(file, "};\n\n");
3521 }
3522
3523 void write_user_quad_list(FILE *file)
3524 {
3525     user_type_t *ut;
3526
3527     if (list_empty(&user_type_list))
3528         return;
3529
3530     fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3531     fprintf(file, "{\n");
3532     LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3533     {
3534         const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3535         print_file(file, 1, "{\n");
3536         print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3537         print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3538         print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3539         print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3540         print_file(file, 1, "}%s\n", sep);
3541     }
3542     fprintf(file, "};\n\n");
3543 }
3544
3545 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3546 {
3547     const struct str_list_entry_t *endpoint;
3548     const char *p;
3549
3550     /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3551     print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3552     LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3553     {
3554         print_file( f, 1, "{ (const unsigned char *)\"" );
3555         for (p = endpoint->str; *p && *p != ':'; p++)
3556         {
3557             if (*p == '"' || *p == '\\') fputc( '\\', f );
3558             fputc( *p, f );
3559         }
3560         if (!*p) goto error;
3561         if (p[1] != '[') goto error;
3562
3563         fprintf( f, "\", (const unsigned char *)\"" );
3564         for (p += 2; *p && *p != ']'; p++)
3565         {
3566             if (*p == '"' || *p == '\\') fputc( '\\', f );
3567             fputc( *p, f );
3568         }
3569         if (*p != ']') goto error;
3570         fprintf( f, "\" },\n" );
3571     }
3572     print_file( f, 0, "};\n\n" );
3573     return;
3574
3575 error:
3576     error("Invalid endpoint syntax '%s'\n", endpoint->str);
3577 }
3578
3579 void write_exceptions( FILE *file )
3580 {
3581     fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3582     fprintf( file, "\n");
3583     fprintf( file, "#include \"wine/exception.h\"\n");
3584     fprintf( file, "#undef RpcTryExcept\n");
3585     fprintf( file, "#undef RpcExcept\n");
3586     fprintf( file, "#undef RpcEndExcept\n");
3587     fprintf( file, "#undef RpcTryFinally\n");
3588     fprintf( file, "#undef RpcFinally\n");
3589     fprintf( file, "#undef RpcEndFinally\n");
3590     fprintf( file, "#undef RpcExceptionCode\n");
3591     fprintf( file, "#undef RpcAbnormalTermination\n");
3592     fprintf( file, "\n");
3593     fprintf( file, "struct __exception_frame;\n");
3594     fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3595     fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3596     fprintf( file, "\n");
3597     fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3598     fprintf( file, "    EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3599     fprintf( file, "    __filter_func                 filter; \\\n");
3600     fprintf( file, "    __finally_func                finally; \\\n");
3601     fprintf( file, "    sigjmp_buf                    jmp; \\\n");
3602     fprintf( file, "    DWORD                         code; \\\n");
3603     fprintf( file, "    unsigned char                 abnormal_termination; \\\n");
3604     fprintf( file, "    unsigned char                 filter_level; \\\n");
3605     fprintf( file, "    unsigned char                 finally_level;\n");
3606     fprintf( file, "\n");
3607     fprintf( file, "struct __exception_frame\n{\n");
3608     fprintf( file, "    __DECL_EXCEPTION_FRAME\n");
3609     fprintf( file, "};\n");
3610     fprintf( file, "\n");
3611     fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3612     fprintf( file, "                                       EXCEPTION_REGISTRATION_RECORD *frame,\n");
3613     fprintf( file, "                                       CONTEXT *context,\n");
3614     fprintf( file, "                                       EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3615     fprintf( file, "{\n");
3616     fprintf( file, "    struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3617     fprintf( file, "\n");
3618     fprintf( file, "    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3619     fprintf( file, "    {\n" );
3620     fprintf( file, "        if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3621     fprintf( file, "        {\n" );
3622     fprintf( file, "            exc_frame->abnormal_termination = 1;\n");
3623     fprintf( file, "            exc_frame->finally( exc_frame );\n");
3624     fprintf( file, "        }\n" );
3625     fprintf( file, "        return ExceptionContinueSearch;\n");
3626     fprintf( file, "    }\n" );
3627     fprintf( file, "    exc_frame->code = record->ExceptionCode;\n");
3628     fprintf( file, "    if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3629     fprintf( file, "    {\n");
3630     fprintf( file, "        __wine_rtl_unwind( frame, record );\n");
3631     fprintf( file, "        if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3632     fprintf( file, "        {\n");
3633     fprintf( file, "            exc_frame->abnormal_termination = 1;\n");
3634     fprintf( file, "            exc_frame->finally( exc_frame );\n");
3635     fprintf( file, "            __wine_pop_frame( frame );\n");
3636     fprintf( file, "        }\n");
3637     fprintf( file, "        exc_frame->filter_level = 0;\n");
3638     fprintf( file, "        siglongjmp( exc_frame->jmp, 1 );\n");
3639     fprintf( file, "    }\n");
3640     fprintf( file, "    return ExceptionContinueSearch;\n");
3641     fprintf( file, "}\n");
3642     fprintf( file, "\n");
3643     fprintf( file, "#define RpcTryExcept \\\n");
3644     fprintf( file, "    if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3645     fprintf( file, "    { \\\n");
3646     fprintf( file, "        if (!__frame->finally_level) \\\n" );
3647     fprintf( file, "            __wine_push_frame( &__frame->frame ); \\\n");
3648     fprintf( file, "        __frame->filter_level = __frame->finally_level + 1;\n" );
3649     fprintf( file, "\n");
3650     fprintf( file, "#define RpcExcept(expr) \\\n");
3651     fprintf( file, "        if (!__frame->finally_level) \\\n" );
3652     fprintf( file, "            __wine_pop_frame( &__frame->frame ); \\\n");
3653     fprintf( file, "        __frame->filter_level = 0; \\\n" );
3654     fprintf( file, "    } \\\n");
3655     fprintf( file, "    else \\\n");
3656     fprintf( file, "\n");
3657     fprintf( file, "#define RpcEndExcept\n");
3658     fprintf( file, "\n");
3659     fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3660     fprintf( file, "\n");
3661     fprintf( file, "#define RpcTryFinally \\\n");
3662     fprintf( file, "    if (!__frame->filter_level) \\\n");
3663     fprintf( file, "        __wine_push_frame( &__frame->frame ); \\\n");
3664     fprintf( file, "    __frame->finally_level = __frame->filter_level + 1;\n");
3665     fprintf( file, "\n");
3666     fprintf( file, "#define RpcFinally \\\n");
3667     fprintf( file, "    if (!__frame->filter_level) \\\n");
3668     fprintf( file, "        __wine_pop_frame( &__frame->frame ); \\\n");
3669     fprintf( file, "    __frame->finally_level = 0;\n");
3670     fprintf( file, "\n");
3671     fprintf( file, "#define RpcEndFinally\n");
3672     fprintf( file, "\n");
3673     fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3674     fprintf( file, "\n");
3675     fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3676     fprintf( file, "    do { \\\n");
3677     fprintf( file, "        __frame->frame.Handler = __widl_exception_handler; \\\n");
3678     fprintf( file, "        __frame->filter = (__filter_func)(filter_func); \\\n" );
3679     fprintf( file, "        __frame->finally = (__finally_func)(finally_func); \\\n");
3680     fprintf( file, "        __frame->abnormal_termination = 0; \\\n");
3681     fprintf( file, "        __frame->filter_level = 0; \\\n");
3682     fprintf( file, "        __frame->finally_level = 0; \\\n");
3683     fprintf( file, "    } while (0)\n");
3684     fprintf( file, "\n");
3685     fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3686     fprintf( file, "\n");
3687     fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3688     fprintf( file, "    do { (void)(filter_func); } while(0)\n");
3689     fprintf( file, "\n");
3690     fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3691     fprintf( file, "    DWORD code;\n");
3692     fprintf( file, "\n");
3693     fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");
3694 }