widl: Add support for new-style interpreted stubs in the proc format strings.
[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 type_t *current_structure;
50 static const var_t *current_func;
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 *iface;
58     const type_t *cont_type;
59     char         *name;
60     unsigned int  baseoff;
61     const expr_t *expr;
62 };
63
64 enum type_context
65 {
66     TYPE_CONTEXT_TOPLEVELPARAM,
67     TYPE_CONTEXT_PARAM,
68     TYPE_CONTEXT_CONTAINER,
69     TYPE_CONTEXT_CONTAINER_NO_POINTERS,
70 };
71
72 /* parameter flags in Oif mode */
73 static const unsigned short MustSize = 0x0001;
74 static const unsigned short MustFree = 0x0002;
75 static const unsigned short IsPipe = 0x0004;
76 static const unsigned short IsIn = 0x0008;
77 static const unsigned short IsOut = 0x0010;
78 static const unsigned short IsReturn = 0x0020;
79 static const unsigned short IsBasetype = 0x0040;
80 static const unsigned short IsByValue = 0x0080;
81 static const unsigned short IsSimpleRef = 0x0100;
82 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
83 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
84
85 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
86 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
87 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align);
88 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
89 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
90                                 const char *name, int write_ptr, unsigned int *tfsoff);
91 static const var_t *find_array_or_string_in_struct(const type_t *type);
92 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
93                                type_t *type, int toplevel_param,
94                                const char *name, unsigned int *typestring_offset);
95 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
96                                                    const attr_list_t *attrs, int toplevel_param,
97                                                    unsigned int *alignment );
98 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
99
100 static const char *string_of_type(unsigned char type)
101 {
102     switch (type)
103     {
104     case RPC_FC_BYTE: return "FC_BYTE";
105     case RPC_FC_CHAR: return "FC_CHAR";
106     case RPC_FC_SMALL: return "FC_SMALL";
107     case RPC_FC_USMALL: return "FC_USMALL";
108     case RPC_FC_WCHAR: return "FC_WCHAR";
109     case RPC_FC_SHORT: return "FC_SHORT";
110     case RPC_FC_USHORT: return "FC_USHORT";
111     case RPC_FC_LONG: return "FC_LONG";
112     case RPC_FC_ULONG: return "FC_ULONG";
113     case RPC_FC_FLOAT: return "FC_FLOAT";
114     case RPC_FC_HYPER: return "FC_HYPER";
115     case RPC_FC_DOUBLE: return "FC_DOUBLE";
116     case RPC_FC_ENUM16: return "FC_ENUM16";
117     case RPC_FC_ENUM32: return "FC_ENUM32";
118     case RPC_FC_IGNORE: return "FC_IGNORE";
119     case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
120     case RPC_FC_RP: return "FC_RP";
121     case RPC_FC_UP: return "FC_UP";
122     case RPC_FC_OP: return "FC_OP";
123     case RPC_FC_FP: return "FC_FP";
124     case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
125     case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
126     case RPC_FC_STRUCT: return "FC_STRUCT";
127     case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
128     case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
129     case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
130     case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
131     case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
132     case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
133     case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
134     case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
135     case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
136     case RPC_FC_CARRAY: return "FC_CARRAY";
137     case RPC_FC_CVARRAY: return "FC_CVARRAY";
138     case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
139     case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
140     case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
141     case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
142     case RPC_FC_POINTER: return "FC_POINTER";
143     case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
144     case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
145     case RPC_FC_CSTRING: return "FC_CSTRING";
146     case RPC_FC_WSTRING: return "FC_WSTRING";
147     case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
148     case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
149     case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
150     case RPC_FC_IP: return "FC_IP";
151     case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
152     case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
153     case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
154     case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
155     case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
156     case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
157     case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
158     case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
159     case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
160     case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
161     case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
162     case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
163     case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
164     case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
165     case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
166     case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
167     case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
168     case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
169     case RPC_FC_PP: return "FC_PP";
170     case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
171     case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
172     case RPC_FC_DIV_2: return "FC_DIV_2";
173     case RPC_FC_MULT_2: return "FC_MULT_2";
174     case RPC_FC_ADD_1: return "FC_ADD_1";
175     case RPC_FC_SUB_1: return "FC_SUB_1";
176     case RPC_FC_CALLBACK: return "FC_CALLBACK";
177     case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
178     case RPC_FC_END: return "FC_END";
179     case RPC_FC_PAD: return "FC_PAD";
180     case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
181     case RPC_FC_RANGE: return "FC_RANGE";
182     case RPC_FC_INT3264: return "FC_INT3264";
183     case RPC_FC_UINT3264: return "FC_UINT3264";
184     default:
185         error("string_of_type: unknown type 0x%02x\n", type);
186         return NULL;
187     }
188 }
189
190 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
191 {
192     const type_t *t = type;
193     for (;;)
194     {
195         if (is_attr(t->attrs, attr))
196             return get_attrp(t->attrs, attr);
197         else if (type_is_alias(t))
198             t = type_alias_get_aliasee(t);
199         else return NULL;
200     }
201 }
202
203 unsigned char get_basic_fc(const type_t *type)
204 {
205     int sign = type_basic_get_sign(type);
206     switch (type_basic_get_type(type))
207     {
208     case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
209     case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
210     case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
211     case TYPE_BASIC_INT64: return RPC_FC_HYPER;
212     case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
213     case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
214     case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
215     case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
216     case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
217     case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
218     case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
219     case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
220     case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
221     case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
222     }
223     return 0;
224 }
225
226 static unsigned char get_basic_fc_signed(const type_t *type)
227 {
228     switch (type_basic_get_type(type))
229     {
230     case TYPE_BASIC_INT8: return RPC_FC_SMALL;
231     case TYPE_BASIC_INT16: return RPC_FC_SHORT;
232     case TYPE_BASIC_INT32: return RPC_FC_LONG;
233     case TYPE_BASIC_INT64: return RPC_FC_HYPER;
234     case TYPE_BASIC_INT: return RPC_FC_LONG;
235     case TYPE_BASIC_INT3264: return RPC_FC_INT3264;
236     case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
237     case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
238     case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
239     case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
240     case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
241     case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
242     case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
243     case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
244     }
245     return 0;
246 }
247
248 static inline unsigned int clamp_align(unsigned int align)
249 {
250     unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
251     if(align > packing) align = packing;
252     return align;
253 }
254
255 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
256 {
257     const type_t *t;
258     int pointer_type;
259
260     assert(is_ptr(type) || is_array(type));
261
262     pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
263     if (pointer_type)
264         return pointer_type;
265
266     for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
267     {
268         pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
269         if (pointer_type)
270             return pointer_type;
271     }
272
273     if (toplevel_param)
274         return RPC_FC_RP;
275     else if (is_ptr(type))
276         return type_pointer_get_default_fc(type);
277     else
278         return type_array_get_ptr_default_fc(type);
279 }
280
281 static unsigned char get_enum_fc(const type_t *type)
282 {
283     assert(type_get_type(type) == TYPE_ENUM);
284     if (is_aliaschain_attr(type, ATTR_V1ENUM))
285         return RPC_FC_ENUM32;
286     else
287         return RPC_FC_ENUM16;
288 }
289
290 static type_t *get_user_type(const type_t *t, const char **pname)
291 {
292     for (;;)
293     {
294         type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
295         if (ut)
296         {
297             if (pname)
298                 *pname = t->name;
299             return ut;
300         }
301
302         if (type_is_alias(t))
303             t = type_alias_get_aliasee(t);
304         else
305             return NULL;
306     }
307 }
308
309 static int is_user_type(const type_t *t)
310 {
311     return get_user_type(t, NULL) != NULL;
312 }
313
314 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
315 {
316     if (is_user_type(type))
317         return TGT_USER_TYPE;
318
319     if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
320         return TGT_CTXT_HANDLE;
321
322     if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
323         return TGT_STRING;
324
325     switch (type_get_type(type))
326     {
327     case TYPE_BASIC:
328         if (!(flags & TDT_IGNORE_RANGES) &&
329             (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
330             return TGT_RANGE;
331         return TGT_BASIC;
332     case TYPE_ENUM:
333         if (!(flags & TDT_IGNORE_RANGES) &&
334             (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
335             return TGT_RANGE;
336         return TGT_ENUM;
337     case TYPE_POINTER:
338         if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
339             (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
340             return TGT_IFACE_POINTER;
341         else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
342             return TGT_CTXT_HANDLE_POINTER;
343         else
344             return TGT_POINTER;
345     case TYPE_STRUCT:
346         return TGT_STRUCT;
347     case TYPE_ENCAPSULATED_UNION:
348     case TYPE_UNION:
349         return TGT_UNION;
350     case TYPE_ARRAY:
351         return TGT_ARRAY;
352     case TYPE_FUNCTION:
353     case TYPE_COCLASS:
354     case TYPE_INTERFACE:
355     case TYPE_MODULE:
356     case TYPE_VOID:
357     case TYPE_ALIAS:
358     case TYPE_BITFIELD:
359         break;
360     }
361     return TGT_INVALID;
362 }
363
364 static int get_padding(const var_list_t *fields)
365 {
366     unsigned short offset = 0;
367     unsigned int salign = 1;
368     const var_t *f;
369
370     if (!fields)
371         return 0;
372
373     LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
374     {
375         type_t *ft = f->type;
376         unsigned int align = 0;
377         unsigned int size = type_memsize_and_alignment(ft, &align);
378         align = clamp_align(align);
379         if (align > salign) salign = align;
380         offset = ROUND_SIZE(offset, align);
381         offset += size;
382     }
383
384     return ROUNDING(offset, salign);
385 }
386
387 static unsigned int get_stack_size( const type_t *type, const attr_list_t *attrs, int *by_value )
388 {
389     unsigned int stack_size;
390     int by_val;
391
392     switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
393     {
394     case TGT_BASIC:
395     case TGT_ENUM:
396     case TGT_RANGE:
397     case TGT_STRUCT:
398     case TGT_UNION:
399     case TGT_USER_TYPE:
400         stack_size = type_memsize( type );
401         by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
402         break;
403     default:
404         by_val = 0;
405         break;
406     }
407     if (!by_val) stack_size = pointer_size;
408     if (by_value) *by_value = by_val;
409     return ROUND_SIZE( stack_size, pointer_size );
410 }
411
412 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
413                                               const type_t *type )
414 {
415     unsigned char flags = 0;
416
417     if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
418
419     if (is_ptr(type) &&
420         !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
421         !is_attr( attrs, ATTR_CONTEXTHANDLE ))
422         flags |= 0x80;
423
424     if (is_attr(attrs, ATTR_IN))
425     {
426         flags |= 0x40;
427         if (!is_attr(attrs, ATTR_OUT)) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
428     }
429     if (is_attr(attrs, ATTR_OUT)) flags |= 0x20;
430
431     return flags;
432 }
433
434 static unsigned int get_rpc_flags( const attr_list_t *attrs )
435 {
436     unsigned int flags = 0;
437
438     if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
439     if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
440     if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
441     if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
442     if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
443     return flags;
444 }
445
446 unsigned char get_struct_fc(const type_t *type)
447 {
448   int has_pointer = 0;
449   int has_conformance = 0;
450   int has_variance = 0;
451   var_t *field;
452   var_list_t *fields;
453
454   fields = type_struct_get_fields(type);
455
456   if (get_padding(fields))
457     return RPC_FC_BOGUS_STRUCT;
458
459   if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
460   {
461     type_t *t = field->type;
462     enum typegen_type typegen_type;
463
464     typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
465
466     if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
467     {
468         if (is_string_type(field->attrs, field->type))
469         {
470             if (is_conformant_array(t))
471                 has_conformance = 1;
472             has_variance = 1;
473             continue;
474         }
475
476         if (is_array(type_array_get_element(field->type)))
477             return RPC_FC_BOGUS_STRUCT;
478
479         if (type_array_has_conformance(field->type))
480         {
481             has_conformance = 1;
482             if (list_next(fields, &field->entry))
483                 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
484                         field->name);
485         }
486         if (type_array_has_variance(t))
487             has_variance = 1;
488
489         t = type_array_get_element(t);
490         typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
491     }
492
493     switch (typegen_type)
494     {
495     case TGT_USER_TYPE:
496     case TGT_IFACE_POINTER:
497         return RPC_FC_BOGUS_STRUCT;
498     case TGT_BASIC:
499         if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
500             return RPC_FC_BOGUS_STRUCT;
501         break;
502     case TGT_ENUM:
503         if (get_enum_fc(t) == RPC_FC_ENUM16)
504             return RPC_FC_BOGUS_STRUCT;
505         break;
506     case TGT_POINTER:
507     case TGT_ARRAY:
508         if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
509             return RPC_FC_BOGUS_STRUCT;
510         has_pointer = 1;
511         break;
512     case TGT_UNION:
513         return RPC_FC_BOGUS_STRUCT;
514     case TGT_STRUCT:
515     {
516         unsigned char fc = get_struct_fc(t);
517         switch (fc)
518         {
519         case RPC_FC_STRUCT:
520             break;
521         case RPC_FC_CVSTRUCT:
522             has_conformance = 1;
523             has_variance = 1;
524             has_pointer = 1;
525             break;
526
527         case RPC_FC_CPSTRUCT:
528             has_conformance = 1;
529             if (list_next( fields, &field->entry ))
530                 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
531                         field->name);
532             has_pointer = 1;
533             break;
534
535         case RPC_FC_CSTRUCT:
536             has_conformance = 1;
537             if (list_next( fields, &field->entry ))
538                 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
539                       field->name);
540             break;
541
542         case RPC_FC_PSTRUCT:
543             has_pointer = 1;
544             break;
545
546         default:
547             error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
548             /* fallthru - treat it as complex */
549
550         /* as soon as we see one of these these members, it's bogus... */
551         case RPC_FC_BOGUS_STRUCT:
552             return RPC_FC_BOGUS_STRUCT;
553         }
554         break;
555     }
556     case TGT_RANGE:
557         return RPC_FC_BOGUS_STRUCT;
558     case TGT_STRING:
559         /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
560     case TGT_INVALID:
561     case TGT_CTXT_HANDLE:
562     case TGT_CTXT_HANDLE_POINTER:
563         /* checking after parsing should mean that we don't get here. if we do,
564          * it's a checker bug */
565         assert(0);
566     }
567   }
568
569   if( has_variance )
570   {
571     if ( has_conformance )
572       return RPC_FC_CVSTRUCT;
573     else
574       return RPC_FC_BOGUS_STRUCT;
575   }
576   if( has_conformance && has_pointer )
577     return RPC_FC_CPSTRUCT;
578   if( has_conformance )
579     return RPC_FC_CSTRUCT;
580   if( has_pointer )
581     return RPC_FC_PSTRUCT;
582   return RPC_FC_STRUCT;
583 }
584
585 static unsigned char get_array_fc(const type_t *type)
586 {
587     unsigned char fc;
588     const expr_t *size_is;
589     const type_t *elem_type;
590
591     elem_type = type_array_get_element(type);
592     size_is = type_array_get_conformance(type);
593
594     if (!size_is)
595     {
596         unsigned int size = type_memsize(elem_type);
597         if (size * type_array_get_dim(type) > 0xffffuL)
598             fc = RPC_FC_LGFARRAY;
599         else
600             fc = RPC_FC_SMFARRAY;
601     }
602     else
603         fc = RPC_FC_CARRAY;
604
605     if (type_array_has_variance(type))
606     {
607         if (fc == RPC_FC_SMFARRAY)
608             fc = RPC_FC_SMVARRAY;
609         else if (fc == RPC_FC_LGFARRAY)
610             fc = RPC_FC_LGVARRAY;
611         else if (fc == RPC_FC_CARRAY)
612             fc = RPC_FC_CVARRAY;
613     }
614
615     switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
616     {
617     case TGT_USER_TYPE:
618         fc = RPC_FC_BOGUS_ARRAY;
619         break;
620     case TGT_BASIC:
621         if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
622             pointer_size != 4)
623             fc = RPC_FC_BOGUS_ARRAY;
624         break;
625     case TGT_STRUCT:
626         switch (get_struct_fc(elem_type))
627         {
628         case RPC_FC_BOGUS_STRUCT:
629             fc = RPC_FC_BOGUS_ARRAY;
630             break;
631         }
632         break;
633     case TGT_ENUM:
634         /* is 16-bit enum - if so, wire size differs from mem size and so
635          * the array cannot be block copied, which means the array is complex */
636         if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
637             fc = RPC_FC_BOGUS_ARRAY;
638         break;
639     case TGT_UNION:
640     case TGT_IFACE_POINTER:
641         fc = RPC_FC_BOGUS_ARRAY;
642         break;
643     case TGT_POINTER:
644         /* ref pointers cannot just be block copied. unique pointers to
645          * interfaces need special treatment. either case means the array is
646          * complex */
647         if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
648             fc = RPC_FC_BOGUS_ARRAY;
649         break;
650     case TGT_RANGE:
651         fc = RPC_FC_BOGUS_ARRAY;
652         break;
653     case TGT_CTXT_HANDLE:
654     case TGT_CTXT_HANDLE_POINTER:
655     case TGT_STRING:
656     case TGT_INVALID:
657     case TGT_ARRAY:
658         /* nothing to do for everything else */
659         break;
660     }
661
662     return fc;
663 }
664
665 static int is_non_complex_struct(const type_t *type)
666 {
667     return (type_get_type(type) == TYPE_STRUCT &&
668             get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
669 }
670
671 static int type_has_pointers(const type_t *type)
672 {
673     switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
674     {
675     case TGT_USER_TYPE:
676         return FALSE;
677     case TGT_POINTER:
678         return TRUE;
679     case TGT_ARRAY:
680         return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
681     case TGT_STRUCT:
682     {
683         var_list_t *fields = type_struct_get_fields(type);
684         const var_t *field;
685         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
686         {
687             if (type_has_pointers(field->type))
688                 return TRUE;
689         }
690         break;
691     }
692     case TGT_UNION:
693     {
694         var_list_t *fields;
695         const var_t *field;
696         fields = type_union_get_cases(type);
697         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
698         {
699             if (field->type && type_has_pointers(field->type))
700                 return TRUE;
701         }
702         break;
703     }
704     case TGT_CTXT_HANDLE:
705     case TGT_CTXT_HANDLE_POINTER:
706     case TGT_STRING:
707     case TGT_IFACE_POINTER:
708     case TGT_BASIC:
709     case TGT_ENUM:
710     case TGT_RANGE:
711     case TGT_INVALID:
712         break;
713     }
714
715     return FALSE;
716 }
717
718 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
719                                  int toplevel_param)
720 {
721     switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
722     {
723     case TGT_USER_TYPE:
724         return FALSE;
725     case TGT_POINTER:
726         if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
727             return TRUE;
728         else
729             return FALSE;
730     case TGT_ARRAY:
731         if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
732             return TRUE;
733         else
734             return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
735     case TGT_STRUCT:
736     {
737         var_list_t *fields = type_struct_get_fields(type);
738         const var_t *field;
739         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
740         {
741             if (type_has_full_pointer(field->type, field->attrs, FALSE))
742                 return TRUE;
743         }
744         break;
745     }
746     case TGT_UNION:
747     {
748         var_list_t *fields;
749         const var_t *field;
750         fields = type_union_get_cases(type);
751         if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
752         {
753             if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
754                 return TRUE;
755         }
756         break;
757     }
758     case TGT_CTXT_HANDLE:
759     case TGT_CTXT_HANDLE_POINTER:
760     case TGT_STRING:
761     case TGT_IFACE_POINTER:
762     case TGT_BASIC:
763     case TGT_ENUM:
764     case TGT_RANGE:
765     case TGT_INVALID:
766         break;
767     }
768
769     return FALSE;
770 }
771
772 static unsigned short user_type_offset(const char *name)
773 {
774     user_type_t *ut;
775     unsigned short off = 0;
776     LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
777     {
778         if (strcmp(name, ut->name) == 0)
779             return off;
780         ++off;
781     }
782     error("user_type_offset: couldn't find type (%s)\n", name);
783     return 0;
784 }
785
786 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
787 {
788     type->typestring_offset = offset;
789     if (file) type->tfswrite = FALSE;
790 }
791
792 static void guard_rec(type_t *type)
793 {
794     /* types that contain references to themselves (like a linked list),
795        need to be shielded from infinite recursion when writing embedded
796        types  */
797     if (type->typestring_offset)
798         type->tfswrite = FALSE;
799     else
800         type->typestring_offset = 1;
801 }
802
803 static int is_embedded_complex(const type_t *type)
804 {
805     switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
806     {
807     case TGT_USER_TYPE:
808     case TGT_STRUCT:
809     case TGT_UNION:
810     case TGT_ARRAY:
811     case TGT_IFACE_POINTER:
812         return TRUE;
813     default:
814         return FALSE;
815     }
816 }
817
818 static const char *get_context_handle_type_name(const type_t *type)
819 {
820     const type_t *t;
821     for (t = type;
822          is_ptr(t) || type_is_alias(t);
823          t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
824         if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
825             return t->name;
826     assert(0);
827     return NULL;
828 }
829
830 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
831     do { \
832         if (file) \
833             fprintf(file, "/* %2u */\n", typestring_offset); \
834         print_file((file), 2, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
835     } \
836     while (0)
837
838 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
839 static void print_file(FILE *file, int indent, const char *format, ...)
840 {
841     va_list va;
842     va_start(va, format);
843     print(file, indent, format, va);
844     va_end(va);
845 }
846
847 void print(FILE *file, int indent, const char *format, va_list va)
848 {
849     if (file)
850     {
851         if (format[0] != '\n')
852             while (0 < indent--)
853                 fprintf(file, "    ");
854         vfprintf(file, format, va);
855     }
856 }
857
858
859 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
860 {
861     if (decl_indirect(t))
862     {
863         print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
864                    local_var_prefix, n, local_var_prefix, n);
865         print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
866     }
867     else if (is_ptr(t) || is_array(t))
868         print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
869 }
870
871 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
872 {
873     const var_t *var;
874
875     if (!is_void(type_function_get_rettype(func->type)))
876         write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
877
878     if (!type_get_function_args(func->type))
879         return;
880
881     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
882         write_var_init(file, indent, var->type, var->name, local_var_prefix);
883
884     fprintf(file, "\n");
885 }
886
887 static void write_formatdesc(FILE *f, int indent, const char *str)
888 {
889     print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
890     print_file(f, indent, "{\n");
891     print_file(f, indent + 1, "short Pad;\n");
892     print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
893     print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
894     print_file(f, indent, "\n");
895 }
896
897 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
898 {
899     clear_all_offsets();
900
901     print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
902                get_size_typeformatstring(stmts, pred));
903
904     print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
905                get_size_procformatstring(stmts, pred));
906
907     fprintf(f, "\n");
908     write_formatdesc(f, indent, "TYPE");
909     write_formatdesc(f, indent, "PROC");
910     fprintf(f, "\n");
911     print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
912     print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
913     print_file(f, indent, "\n");
914 }
915
916 int decl_indirect(const type_t *t)
917 {
918     if (is_user_type(t))
919         return TRUE;
920     return (type_get_type(t) != TYPE_BASIC &&
921             type_get_type(t) != TYPE_ENUM &&
922             type_get_type(t) != TYPE_POINTER &&
923             type_get_type(t) != TYPE_ARRAY);
924 }
925
926 static unsigned char get_parameter_fc( const type_t *type, const attr_list_t *attrs, int is_return,
927                                        unsigned short *flags, unsigned int *stack_size,
928                                        unsigned int *typestring_offset )
929 {
930     unsigned int alignment, server_size = 0, buffer_size = 0;
931     unsigned char fc = 0;
932     int is_byval;
933     int is_in = is_attr(attrs, ATTR_IN);
934     int is_out = is_attr(attrs, ATTR_OUT);
935
936     if (is_return) is_out = TRUE;
937     else if (!is_in && !is_out) is_in = TRUE;
938
939     *flags = 0;
940     *stack_size = get_stack_size( type, attrs, &is_byval );
941     *typestring_offset = type->typestring_offset;
942
943     if (is_in)     *flags |= IsIn;
944     if (is_out)    *flags |= IsOut;
945     if (is_return) *flags |= IsReturn;
946
947     if (!is_string_type( attrs, type ))
948         buffer_size = get_required_buffer_size_type( type, NULL, attrs, TRUE, &alignment );
949
950     switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
951     {
952     case TGT_BASIC:
953         *flags |= IsBasetype;
954         fc = get_basic_fc_signed( type );
955         if (fc == RPC_FC_BIND_PRIMITIVE)
956         {
957             buffer_size = 4;  /* actually 0 but avoids setting MustSize */
958             fc = RPC_FC_LONG;
959         }
960         break;
961     case TGT_ENUM:
962         *flags |= IsBasetype;
963         fc = get_enum_fc( type );
964         break;
965     case TGT_RANGE:
966         *flags |= IsByValue;
967         break;
968     case TGT_STRUCT:
969     case TGT_UNION:
970     case TGT_USER_TYPE:
971         *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
972         break;
973     case TGT_IFACE_POINTER:
974         *flags |= MustFree;
975         break;
976     case TGT_ARRAY:
977         *flags |= MustFree;
978         if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff &&
979             get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
980             *flags |= IsSimpleRef;
981         break;
982     case TGT_STRING:
983         *flags |= MustFree;
984         if (is_declptr( type ) && get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
985         {
986             /* skip over pointer description straight to string description */
987             if (is_conformant_array( type )) typestring_offset += 4;
988             else typestring_offset += 2;
989             *flags |= IsSimpleRef;
990         }
991         break;
992     case TGT_CTXT_HANDLE_POINTER:
993         *flags |= IsSimpleRef;
994         /* fall through */
995     case TGT_CTXT_HANDLE:
996         buffer_size = 20;
997         break;
998     case TGT_POINTER:
999         if (get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
1000         {
1001             const type_t *ref = type_pointer_get_ref( type );
1002
1003             if (!is_string_type( attrs, ref ))
1004                 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1005
1006             switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1007             {
1008             case TGT_BASIC:
1009                 *flags |= IsSimpleRef | IsBasetype;
1010                 fc = get_basic_fc( ref );
1011                 if (!is_in && is_out) server_size = pointer_size;
1012                 break;
1013             case TGT_ENUM:
1014                 if ((fc = get_enum_fc( ref )) == RPC_FC_ENUM32)
1015                 {
1016                     *flags |= IsSimpleRef | IsBasetype;
1017                     if (!is_in && is_out) server_size = pointer_size;
1018                 }
1019                 else
1020                 {
1021                     server_size = pointer_size;
1022                 }
1023                 break;
1024             case TGT_UNION:
1025             case TGT_USER_TYPE:
1026             case TGT_RANGE:
1027                 *flags |= IsSimpleRef | MustFree;
1028                 *typestring_offset = ref->typestring_offset;
1029                 if (!is_in && is_out) server_size = type_memsize( ref );
1030                 break;
1031             case TGT_STRING:
1032             case TGT_POINTER:
1033             case TGT_ARRAY:
1034             case TGT_CTXT_HANDLE:
1035             case TGT_CTXT_HANDLE_POINTER:
1036                 *flags |= MustFree;
1037                 server_size = pointer_size;
1038                 break;
1039             case TGT_IFACE_POINTER:
1040                 *flags |= MustFree;
1041                 if (is_in && is_out) server_size = pointer_size;
1042                 break;
1043             case TGT_STRUCT:
1044                 *flags |= IsSimpleRef | MustFree;
1045                 *typestring_offset = ref->typestring_offset;
1046                 switch (get_struct_fc(ref))
1047                 {
1048                 case RPC_FC_STRUCT:
1049                 case RPC_FC_PSTRUCT:
1050                 case RPC_FC_BOGUS_STRUCT:
1051                     if (!is_in && is_out) server_size = type_memsize( ref );
1052                     break;
1053                 default:
1054                     break;
1055                 }
1056                 break;
1057             case TGT_INVALID:
1058                 assert(0);
1059             }
1060         }
1061         else  /* not ref pointer */
1062         {
1063             *flags |= MustFree;
1064         }
1065         break;
1066     case TGT_INVALID:
1067         assert(0);
1068     }
1069
1070     if (!buffer_size) *flags |= MustSize;
1071
1072     if (server_size)
1073     {
1074         server_size = (server_size + 7) / 8;
1075         if (server_size < 8) *flags |= server_size << 13;
1076     }
1077     return fc;
1078 }
1079
1080 static unsigned char get_func_oi2_flags( const var_t *func )
1081 {
1082     const var_t *var;
1083     var_list_t *args = type_get_function_args( func->type );
1084     type_t *ret_type = type_function_get_rettype( func->type );
1085     unsigned char oi2_flags = 0x40;  /* HasExtensions */
1086     unsigned short flags;
1087     unsigned int stack_size, typestring_offset;
1088
1089     if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1090     {
1091         get_parameter_fc( var->type, var->attrs, 0, &flags, &stack_size, &typestring_offset );
1092         if (flags & MustSize)
1093         {
1094             if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1095             if (flags & IsOut) oi2_flags |= 0x01;  /* ServerMustSize */
1096         }
1097     }
1098
1099     if (!is_void( ret_type ))
1100     {
1101         oi2_flags |= 0x04;  /* HasRet */
1102         get_parameter_fc( ret_type, NULL, 1, &flags, &stack_size, &typestring_offset );
1103         if (flags & MustSize) oi2_flags |= 0x01;  /* ServerMustSize */
1104     }
1105     return oi2_flags;
1106 }
1107
1108 static unsigned int write_new_procformatstring_type(FILE *file, int indent,
1109                                                     const type_t *type,
1110                                                     const attr_list_t *attrs,
1111                                                     int is_return, unsigned int *stack_offset)
1112 {
1113     char buffer[64];
1114     unsigned int stack_size, typestring_offset;
1115     unsigned short flags;
1116     unsigned char fc = get_parameter_fc( type, attrs, is_return, &flags, &stack_size, &typestring_offset );
1117
1118     strcpy( buffer, "/* flags:" );
1119     if (flags & MustSize) strcat( buffer, " must size," );
1120     if (flags & MustFree) strcat( buffer, " must free," );
1121     if (flags & IsPipe) strcat( buffer, " pipe," );
1122     if (flags & IsIn) strcat( buffer, " in," );
1123     if (flags & IsOut) strcat( buffer, " out," );
1124     if (flags & IsReturn) strcat( buffer, " return," );
1125     if (flags & IsBasetype) strcat( buffer, " base type," );
1126     if (flags & IsByValue) strcat( buffer, " by value," );
1127     if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1128     if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1129     strcpy( buffer + strlen( buffer ) - 1, " */" );
1130     print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1131     print_file( file, indent, "NdrFcShort(0x%hx),       /* stack offset = %hu */\n",
1132                 *stack_offset, *stack_offset );
1133     if (flags & IsBasetype)
1134     {
1135         print_file( file, indent, "0x%02x,      /* %s */\n", fc, string_of_type(fc) );
1136         print_file( file, indent, "0x0,\n" );
1137     }
1138     else
1139         print_file( file, indent, "NdrFcShort(0x%x),    /* type offset = %u */\n",
1140                     typestring_offset, typestring_offset );
1141     *stack_offset += max( stack_size, pointer_size );
1142     return 6;
1143 }
1144
1145 static unsigned int write_old_procformatstring_type(FILE *file, int indent,
1146                                                     const type_t *type,
1147                                                     const attr_list_t *attrs,
1148                                                     int is_return, int is_interpreted)
1149 {
1150     unsigned int size;
1151
1152     int is_in = is_attr(attrs, ATTR_IN);
1153     int is_out = is_attr(attrs, ATTR_OUT);
1154
1155     if (!is_in && !is_out) is_in = TRUE;
1156
1157     if (type_get_type(type) == TYPE_BASIC ||
1158         type_get_type(type) == TYPE_ENUM)
1159     {
1160         unsigned char fc;
1161
1162         if (is_return)
1163             print_file(file, indent, "0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
1164         else
1165             print_file(file, indent, "0x4e,    /* FC_IN_PARAM_BASETYPE */\n");
1166
1167         if (type_get_type(type) == TYPE_ENUM)
1168         {
1169             fc = get_enum_fc(type);
1170         }
1171         else
1172         {
1173             fc = get_basic_fc_signed(type);
1174
1175             if (fc == RPC_FC_BIND_PRIMITIVE)
1176                 fc = RPC_FC_IGNORE;
1177         }
1178
1179         print_file(file, indent, "0x%02x,    /* %s */\n",
1180                    fc, string_of_type(fc));
1181         size = 2; /* includes param type prefix */
1182     }
1183     else
1184     {
1185         unsigned short offset = type->typestring_offset;
1186
1187         if (is_interpreted && is_array(type) &&
1188             type_array_is_decl_as_ptr(type) &&
1189             type->details.array.ptr_tfsoff)
1190             offset = type->details.array.ptr_tfsoff;
1191
1192         if (is_return)
1193             print_file(file, indent, "0x52,    /* FC_RETURN_PARAM */\n");
1194         else if (is_in && is_out)
1195             print_file(file, indent, "0x50,    /* FC_IN_OUT_PARAM */\n");
1196         else if (is_out)
1197             print_file(file, indent, "0x51,    /* FC_OUT_PARAM */\n");
1198         else
1199             print_file(file, indent, "0x4d,    /* FC_IN_PARAM */\n");
1200
1201         size = get_stack_size( type, attrs, NULL );
1202         print_file(file, indent, "0x%02x,\n", size / pointer_size );
1203         print_file(file, indent, "NdrFcShort(0x%x),     /* type offset = %u */\n", offset, offset);
1204         size = 4; /* includes param type prefix */
1205     }
1206     return size;
1207 }
1208
1209 int is_interpreted_func( const type_t *iface, const var_t *func )
1210 {
1211     const char *str;
1212     const var_t *var;
1213     const var_list_t *args = type_get_function_args( func->type );
1214     const type_t *ret_type = type_function_get_rettype( func->type );
1215
1216     if (type_get_type( ret_type ) == TYPE_BASIC)
1217     {
1218         switch (type_basic_get_type( ret_type ))
1219         {
1220         case TYPE_BASIC_INT64:
1221         case TYPE_BASIC_HYPER:
1222             /* return value must fit in a long_ptr */
1223             if (pointer_size < 8) return 0;
1224             break;
1225         case TYPE_BASIC_FLOAT:
1226         case TYPE_BASIC_DOUBLE:
1227             /* floating point values can't be returned */
1228             return 0;
1229         default:
1230             break;
1231         }
1232     }
1233     /* unions passed by value are not supported in Oi mode */
1234     if (stub_mode != MODE_Oif && args)
1235         LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1236         {
1237             if (type_get_type( var->type ) == TYPE_UNION ||
1238                 type_get_type( var->type ) == TYPE_ENCAPSULATED_UNION)
1239                 return 0;
1240         }
1241
1242     if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1243     if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1244     return (stub_mode != MODE_Os);
1245 }
1246
1247 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1248                                     const var_t *func, unsigned int *offset,
1249                                     unsigned short num_proc )
1250 {
1251     var_t *var;
1252     var_list_t *args = type_get_function_args( func->type );
1253     unsigned char explicit_fc, implicit_fc;
1254     unsigned char handle_flags;
1255     const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1256     unsigned char oi_flags = RPC_FC_PROC_OIF_RPCFLAGS | RPC_FC_PROC_OIF_NEWINIT;
1257     unsigned int rpc_flags = get_rpc_flags( func->attrs );
1258     unsigned int nb_args = 0;
1259     unsigned int stack_size = 0;
1260     unsigned short param_num = 0;
1261     unsigned short handle_stack_offset = 0;
1262     unsigned short handle_param_num = 0;
1263
1264     if (is_full_pointer_function( func )) oi_flags |= RPC_FC_PROC_OIF_FULLPTR;
1265     if (is_object( iface ))
1266     {
1267         oi_flags |= RPC_FC_PROC_OIF_OBJECT;
1268         if (stub_mode == MODE_Oif) oi_flags |= RPC_FC_PROC_OIF_OBJ_V2;
1269         stack_size += pointer_size;
1270     }
1271
1272     if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1273     {
1274         if (var == handle_var)
1275         {
1276             handle_stack_offset = stack_size;
1277             handle_param_num = param_num;
1278         }
1279         stack_size += get_stack_size( var->type, var->attrs, NULL );
1280         param_num++;
1281         nb_args++;
1282     }
1283     if (!is_void( type_function_get_rettype( func->type )))
1284     {
1285         stack_size += pointer_size;
1286         nb_args++;
1287     }
1288
1289     print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1290     print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1291                 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1292     print_file( file, indent, "0x%02x,\n", oi_flags );
1293     print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1294     print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1295     print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack size = %hu */\n", stack_size, stack_size );
1296     *offset += 10;
1297
1298     if (!implicit_fc)
1299     {
1300         switch (explicit_fc)
1301         {
1302         case RPC_FC_BIND_PRIMITIVE:
1303             handle_flags = 0;
1304             print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1305             print_file( file, indent, "0x%02x,\n", handle_flags );
1306             print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1307                         handle_stack_offset, handle_stack_offset );
1308             *offset += 4;
1309             break;
1310         case RPC_FC_BIND_GENERIC:
1311             handle_flags = type_memsize( handle_var->type );
1312             print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1313             print_file( file, indent, "0x%02x,\n", handle_flags );
1314             print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1315                         handle_stack_offset, handle_stack_offset );
1316             print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->type ) );
1317             print_file( file, indent, "0x%x,\t/* FC_PAD */\n", RPC_FC_PAD);
1318             *offset += 6;
1319             break;
1320         case RPC_FC_BIND_CONTEXT:
1321             handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->type );
1322             print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1323             print_file( file, indent, "0x%02x,\n", handle_flags );
1324             print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1325                         handle_stack_offset, handle_stack_offset );
1326             print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->type ) );
1327             print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1328             *offset += 6;
1329             break;
1330         }
1331     }
1332
1333     if (stub_mode == MODE_Oif)
1334     {
1335         unsigned char oi2_flags = get_func_oi2_flags( func );
1336         unsigned char ext_flags = 0;
1337         unsigned int size;
1338
1339         if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08;  /* HasNotify */
1340         if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10;  /* HasNotify2 */
1341
1342         size = get_function_buffer_size( func, PASS_IN );
1343         print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %hu */\n", size, size );
1344         size = get_function_buffer_size( func, PASS_OUT );
1345         print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %hu */\n", size, size );
1346         print_file( file, indent, "0x%02x,\n", oi2_flags );
1347         print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1348         print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1349         print_file( file, indent, "0x%02x,\n", ext_flags );
1350         print_file( file, indent, "NdrFcShort(0x0),\n" );  /* server corr hint */
1351         print_file( file, indent, "NdrFcShort(0x0),\n" );  /* client corr hint */
1352         print_file( file, indent, "NdrFcShort(0x0),\n" );  /* FIXME: notify index */
1353         *offset += 14;
1354         if (pointer_size == 8)
1355         {
1356             print_file( file, indent, "NdrFcShort(0x0),\n" );  /* floating point mask */
1357             *offset += 2;
1358         }
1359     }
1360 }
1361
1362 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1363                                          const var_t *func, unsigned int *offset,
1364                                          unsigned short num_proc )
1365 {
1366     unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1367     int is_interpreted = is_interpreted_func( iface, func );
1368     int is_new_style = is_interpreted && (stub_mode == MODE_Oif);
1369
1370     if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1371
1372     /* emit argument data */
1373     if (type_get_function_args(func->type))
1374     {
1375         const var_t *var;
1376         LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1377         {
1378             print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1379             if (is_new_style)
1380                 *offset += write_new_procformatstring_type(file, indent, var->type, var->attrs,
1381                                                            FALSE, &stack_offset);
1382             else
1383                 *offset += write_old_procformatstring_type(file, indent, var->type, var->attrs,
1384                                                            FALSE, is_interpreted);
1385         }
1386     }
1387
1388     /* emit return value data */
1389     if (is_void(type_function_get_rettype(func->type)))
1390     {
1391         if (!is_new_style)
1392         {
1393             print_file(file, 0, "/* %u (void) */\n", *offset);
1394             print_file(file, indent, "0x5b,    /* FC_END */\n");
1395             print_file(file, indent, "0x5c,    /* FC_PAD */\n");
1396             *offset += 2;
1397         }
1398     }
1399     else
1400     {
1401         print_file( file, 0, "/* %u (return value) */\n", *offset );
1402         if (is_new_style)
1403             *offset += write_new_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1404                                                        NULL, TRUE, &stack_offset);
1405         else
1406             *offset += write_old_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1407                                                        NULL, TRUE, is_interpreted);
1408     }
1409 }
1410
1411 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1412                                          type_pred_t pred, unsigned int *offset)
1413 {
1414     const statement_t *stmt;
1415     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1416     {
1417         if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1418         {
1419             const statement_t *stmt_func;
1420             const type_t *iface = stmt->u.type;
1421             const type_t *parent = type_iface_get_inherit( iface );
1422             int count = parent ? count_methods( parent ) : 0;
1423
1424             if (!pred(iface)) continue;
1425             STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1426             {
1427                 var_t *func = stmt_func->u.var;
1428                 if (is_local(func->attrs)) continue;
1429                 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1430             }
1431         }
1432         else if (stmt->type == STMT_LIBRARY)
1433             write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred, offset);
1434     }
1435 }
1436
1437 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1438 {
1439     int indent = 0;
1440     unsigned int offset = 0;
1441
1442     print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1443     print_file(file, indent, "{\n");
1444     indent++;
1445     print_file(file, indent, "0,\n");
1446     print_file(file, indent, "{\n");
1447     indent++;
1448
1449     write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1450
1451     print_file(file, indent, "0x0\n");
1452     indent--;
1453     print_file(file, indent, "}\n");
1454     indent--;
1455     print_file(file, indent, "};\n");
1456     print_file(file, indent, "\n");
1457 }
1458
1459 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1460 {
1461     const statement_t *stmt;
1462     int indent = 0;
1463
1464     print_file( file, indent,  "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1465                 iface->name );
1466     print_file( file, indent,  "{\n" );
1467     indent++;
1468     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1469     {
1470         var_t *func = stmt->u.var;
1471         if (is_local( func->attrs )) continue;
1472         print_file( file, indent,  "%u,  /* %s */\n", func->procstring_offset, func->name );
1473     }
1474     indent--;
1475     print_file( file, indent,  "};\n\n" );
1476 }
1477
1478 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1479 {
1480     unsigned char fc;
1481
1482     if (type_get_type(type) == TYPE_BASIC)
1483         fc = get_basic_fc_signed(type);
1484     else if (type_get_type(type) == TYPE_ENUM)
1485         fc = get_enum_fc(type);
1486     else
1487         return 0;
1488
1489     print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1490     *typestring_offset += 1;
1491     return 1;
1492 }
1493
1494 /* write conformance / variance descriptor */
1495 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1496                                            unsigned int baseoff, const type_t *type,
1497                                            const expr_t *expr)
1498 {
1499     unsigned char operator_type = 0;
1500     unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1501     const char *conftype_string = "field";
1502     const expr_t *subexpr;
1503     const type_t *iface = NULL;
1504     const char *name;
1505
1506     if (!expr)
1507     {
1508         print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1509         return 4;
1510     }
1511
1512     if (expr->is_const)
1513     {
1514         if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1515             error("write_conf_or_var_desc: constant value %d is greater than "
1516                   "the maximum constant size of %d\n", expr->cval,
1517                   UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1518
1519         print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1520                    RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1521         print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1522         print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1523
1524         return 4;
1525     }
1526
1527     if (!cont_type)  /* top-level conformance */
1528     {
1529         conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1530         conftype_string = "parameter";
1531         cont_type = current_func->type;
1532         name = current_func->name;
1533         iface = current_iface;
1534     }
1535     else
1536     {
1537         name = cont_type->name;
1538         if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1539         {
1540             conftype = RPC_FC_POINTER_CONFORMANCE;
1541             conftype_string = "field pointer";
1542         }
1543     }
1544
1545     subexpr = expr;
1546     switch (subexpr->type)
1547     {
1548     case EXPR_PPTR:
1549         subexpr = subexpr->ref;
1550         operator_type = RPC_FC_DEREFERENCE;
1551         break;
1552     case EXPR_DIV:
1553         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1554         {
1555             subexpr = subexpr->ref;
1556             operator_type = RPC_FC_DIV_2;
1557         }
1558         break;
1559     case EXPR_MUL:
1560         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1561         {
1562             subexpr = subexpr->ref;
1563             operator_type = RPC_FC_MULT_2;
1564         }
1565         break;
1566     case EXPR_SUB:
1567         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1568         {
1569             subexpr = subexpr->ref;
1570             operator_type = RPC_FC_SUB_1;
1571         }
1572         break;
1573     case EXPR_ADD:
1574         if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1575         {
1576             subexpr = subexpr->ref;
1577             operator_type = RPC_FC_ADD_1;
1578         }
1579         break;
1580     default:
1581         break;
1582     }
1583
1584     if (subexpr->type == EXPR_IDENTIFIER)
1585     {
1586         const type_t *correlation_variable = NULL;
1587         unsigned char param_type = 0;
1588         unsigned int offset = 0;
1589         const var_t *var;
1590         struct expr_loc expr_loc;
1591
1592         if (type_get_type(cont_type) == TYPE_FUNCTION)
1593         {
1594             var_list_t *args = type_get_function_args( cont_type );
1595
1596             if (is_object( iface )) offset += pointer_size;
1597             if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1598             {
1599                 if (var->name && !strcmp(var->name, subexpr->u.sval))
1600                 {
1601                     expr_loc.v = var;
1602                     correlation_variable = var->type;
1603                     break;
1604                 }
1605                 offset += get_stack_size( var->type, var->attrs, NULL );
1606             }
1607         }
1608         else
1609         {
1610             var_list_t *fields = type_struct_get_fields( cont_type );
1611
1612             if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1613             {
1614                 unsigned int size = field_memsize( var->type, &offset );
1615                 if (var->name && !strcmp(var->name, subexpr->u.sval))
1616                 {
1617                     expr_loc.v = var;
1618                     correlation_variable = var->type;
1619                     break;
1620                 }
1621                 offset += size;
1622             }
1623         }
1624
1625         if (!correlation_variable)
1626             error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1627         expr_loc.attr = NULL;
1628         correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1629
1630         offset -= baseoff;
1631
1632         if (type_get_type(correlation_variable) == TYPE_BASIC)
1633         {
1634             switch (get_basic_fc(correlation_variable))
1635             {
1636             case RPC_FC_CHAR:
1637             case RPC_FC_SMALL:
1638                 param_type = RPC_FC_SMALL;
1639                 break;
1640             case RPC_FC_BYTE:
1641             case RPC_FC_USMALL:
1642                 param_type = RPC_FC_USMALL;
1643                 break;
1644             case RPC_FC_WCHAR:
1645             case RPC_FC_SHORT:
1646                 param_type = RPC_FC_SHORT;
1647                 break;
1648             case RPC_FC_USHORT:
1649                 param_type = RPC_FC_USHORT;
1650                 break;
1651             case RPC_FC_LONG:
1652                 param_type = RPC_FC_LONG;
1653                 break;
1654             case RPC_FC_ULONG:
1655                 param_type = RPC_FC_ULONG;
1656                 break;
1657             default:
1658                 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1659                       get_basic_fc(correlation_variable));
1660             }
1661         }
1662         else if (type_get_type(correlation_variable) == TYPE_ENUM)
1663         {
1664             if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1665                 param_type = RPC_FC_LONG;
1666             else
1667                 param_type = RPC_FC_SHORT;
1668         }
1669         else if (type_get_type(correlation_variable) == TYPE_POINTER)
1670         {
1671             if (pointer_size == 8)
1672                 param_type = RPC_FC_HYPER;
1673             else
1674                 param_type = RPC_FC_LONG;
1675         }
1676         else
1677         {
1678             error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1679                   subexpr->u.sval);
1680             return 0;
1681         }
1682
1683         print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1684                    conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1685         print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1686                    operator_type ? string_of_type(operator_type) : "no operators");
1687         print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1688                    (unsigned short)offset, offset);
1689     }
1690     else if (!iface || is_interpreted_func( iface, current_func ))
1691     {
1692         unsigned int callback_offset = 0;
1693         struct expr_eval_routine *eval;
1694         int found = 0;
1695
1696         LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1697         {
1698             if (eval->cont_type == cont_type ||
1699                 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1700                  eval->iface == iface &&
1701                  eval->name && name && !strcmp(eval->name, name) &&
1702                  !compare_expr(eval->expr, expr)))
1703             {
1704                 found = 1;
1705                 break;
1706             }
1707             callback_offset++;
1708         }
1709
1710         if (!found)
1711         {
1712             eval = xmalloc (sizeof(*eval));
1713             eval->iface = iface;
1714             eval->cont_type = cont_type;
1715             eval->name = xstrdup( name );
1716             eval->baseoff = baseoff;
1717             eval->expr = expr;
1718             list_add_tail (&expr_eval_routines, &eval->entry);
1719         }
1720
1721         if (callback_offset > USHRT_MAX)
1722             error("Maximum number of callback routines reached\n");
1723
1724         print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1725         print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1726         print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1727     }
1728     else  /* output a dummy corr desc that isn't used */
1729     {
1730         print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1731         print_file(file, 2, "0x0,\n" );
1732         print_file(file, 2, "NdrFcShort(0x0),\n" );
1733     }
1734     return 4;
1735 }
1736
1737 /* return size and start offset of a data field based on current offset */
1738 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1739 {
1740     unsigned int align = 0;
1741     unsigned int size = type_memsize_and_alignment( type, &align );
1742
1743     *offset = ROUND_SIZE( *offset, align );
1744     return size;
1745 }
1746
1747 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1748 {
1749     unsigned int size = 0;
1750     unsigned int max_align;
1751     const var_t *v;
1752
1753     if (!fields) return 0;
1754     LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1755     {
1756         unsigned int falign = 0;
1757         unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1758         if (*align < falign) *align = falign;
1759         falign = clamp_align(falign);
1760         size = ROUND_SIZE(size, falign);
1761         size += fsize;
1762     }
1763
1764     max_align = clamp_align(*align);
1765     size = ROUND_SIZE(size, max_align);
1766
1767     return size;
1768 }
1769
1770 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1771 {
1772     unsigned int size, maxs = 0;
1773     unsigned int align = *pmaxa;
1774     const var_t *v;
1775
1776     if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1777     {
1778         /* we could have an empty default field with NULL type */
1779         if (v->type)
1780         {
1781             size = type_memsize_and_alignment(v->type, &align);
1782             if (maxs < size) maxs = size;
1783             if (*pmaxa < align) *pmaxa = align;
1784         }
1785     }
1786
1787     return maxs;
1788 }
1789
1790 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1791 {
1792     unsigned int size = 0;
1793
1794     switch (type_get_type(t))
1795     {
1796     case TYPE_BASIC:
1797         switch (get_basic_fc(t))
1798         {
1799         case RPC_FC_BYTE:
1800         case RPC_FC_CHAR:
1801         case RPC_FC_USMALL:
1802         case RPC_FC_SMALL:
1803             size = 1;
1804             if (size > *align) *align = size;
1805             break;
1806         case RPC_FC_WCHAR:
1807         case RPC_FC_USHORT:
1808         case RPC_FC_SHORT:
1809             size = 2;
1810             if (size > *align) *align = size;
1811             break;
1812         case RPC_FC_ULONG:
1813         case RPC_FC_LONG:
1814         case RPC_FC_ERROR_STATUS_T:
1815         case RPC_FC_FLOAT:
1816             size = 4;
1817             if (size > *align) *align = size;
1818             break;
1819         case RPC_FC_HYPER:
1820         case RPC_FC_DOUBLE:
1821             size = 8;
1822             if (size > *align) *align = size;
1823             break;
1824         case RPC_FC_INT3264:
1825         case RPC_FC_UINT3264:
1826         case RPC_FC_BIND_PRIMITIVE:
1827             assert( pointer_size );
1828             size = pointer_size;
1829             if (size > *align) *align = size;
1830             break;
1831         default:
1832             error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1833             size = 0;
1834         }
1835         break;
1836     case TYPE_ENUM:
1837         switch (get_enum_fc(t))
1838         {
1839         case RPC_FC_ENUM16:
1840         case RPC_FC_ENUM32:
1841             size = 4;
1842             if (size > *align) *align = size;
1843             break;
1844         default:
1845             error("type_memsize: Unknown enum type\n");
1846             size = 0;
1847         }
1848         break;
1849     case TYPE_STRUCT:
1850         size = fields_memsize(type_struct_get_fields(t), align);
1851         break;
1852     case TYPE_ENCAPSULATED_UNION:
1853         size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1854         break;
1855     case TYPE_UNION:
1856         size = union_memsize(type_union_get_cases(t), align);
1857         break;
1858     case TYPE_POINTER:
1859         assert( pointer_size );
1860         size = pointer_size;
1861         if (size > *align) *align = size;
1862         break;
1863     case TYPE_ARRAY:
1864         if (!type_array_is_decl_as_ptr(t))
1865         {
1866             if (is_conformant_array(t))
1867             {
1868                 type_memsize_and_alignment(type_array_get_element(t), align);
1869                 size = 0;
1870             }
1871             else
1872                 size = type_array_get_dim(t) *
1873                     type_memsize_and_alignment(type_array_get_element(t), align);
1874         }
1875         else /* declared as a pointer */
1876         {
1877             assert( pointer_size );
1878             size = pointer_size;
1879             if (size > *align) *align = size;
1880         }
1881         break;
1882     case TYPE_INTERFACE:
1883     case TYPE_ALIAS:
1884     case TYPE_VOID:
1885     case TYPE_COCLASS:
1886     case TYPE_MODULE:
1887     case TYPE_FUNCTION:
1888     case TYPE_BITFIELD:
1889         /* these types should not be encountered here due to language
1890          * restrictions (interface, void, coclass, module), logical
1891          * restrictions (alias - due to type_get_type call above) or
1892          * checking restrictions (function, bitfield). */
1893         assert(0);
1894     }
1895
1896     return size;
1897 }
1898
1899 unsigned int type_memsize(const type_t *t)
1900 {
1901     unsigned int align = 0;
1902     return type_memsize_and_alignment( t, &align );
1903 }
1904
1905 static unsigned int type_buffer_alignment(const type_t *t)
1906 {
1907     const var_list_t *fields;
1908     const var_t *var;
1909     unsigned int max = 0, align;
1910
1911     switch (type_get_type(t))
1912     {
1913     case TYPE_BASIC:
1914         switch (get_basic_fc(t))
1915         {
1916         case RPC_FC_BYTE:
1917         case RPC_FC_CHAR:
1918         case RPC_FC_USMALL:
1919         case RPC_FC_SMALL:
1920             return 1;
1921         case RPC_FC_WCHAR:
1922         case RPC_FC_USHORT:
1923         case RPC_FC_SHORT:
1924             return 2;
1925         case RPC_FC_ULONG:
1926         case RPC_FC_LONG:
1927         case RPC_FC_ERROR_STATUS_T:
1928         case RPC_FC_FLOAT:
1929         case RPC_FC_INT3264:
1930         case RPC_FC_UINT3264:
1931             return 4;
1932         case RPC_FC_HYPER:
1933         case RPC_FC_DOUBLE:
1934             return 8;
1935         default:
1936             error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1937         }
1938         break;
1939     case TYPE_ENUM:
1940         switch (get_enum_fc(t))
1941         {
1942         case RPC_FC_ENUM16:
1943             return 2;
1944         case RPC_FC_ENUM32:
1945             return 4;
1946         default:
1947             error("type_buffer_alignment: Unknown enum type\n");
1948         }
1949         break;
1950     case TYPE_STRUCT:
1951         if (!(fields = type_struct_get_fields(t))) break;
1952         LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1953         {
1954             if (!var->type) continue;
1955             align = type_buffer_alignment( var->type );
1956             if (max < align) max = align;
1957         }
1958         break;
1959     case TYPE_ENCAPSULATED_UNION:
1960         if (!(fields = type_encapsulated_union_get_fields(t))) break;
1961         LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1962         {
1963             if (!var->type) continue;
1964             align = type_buffer_alignment( var->type );
1965             if (max < align) max = align;
1966         }
1967         break;
1968     case TYPE_UNION:
1969         if (!(fields = type_union_get_cases(t))) break;
1970         LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1971         {
1972             if (!var->type) continue;
1973             align = type_buffer_alignment( var->type );
1974             if (max < align) max = align;
1975         }
1976         break;
1977     case TYPE_ARRAY:
1978         if (!type_array_is_decl_as_ptr(t))
1979             return type_buffer_alignment( type_array_get_element(t) );
1980         /* else fall through */
1981     case TYPE_POINTER:
1982         return 4;
1983     case TYPE_INTERFACE:
1984     case TYPE_ALIAS:
1985     case TYPE_VOID:
1986     case TYPE_COCLASS:
1987     case TYPE_MODULE:
1988     case TYPE_FUNCTION:
1989     case TYPE_BITFIELD:
1990         /* these types should not be encountered here due to language
1991          * restrictions (interface, void, coclass, module), logical
1992          * restrictions (alias - due to type_get_type call above) or
1993          * checking restrictions (function, bitfield). */
1994         assert(0);
1995     }
1996     return max;
1997 }
1998
1999 int is_full_pointer_function(const var_t *func)
2000 {
2001     const var_t *var;
2002     if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2003         return TRUE;
2004     if (!type_get_function_args(func->type))
2005         return FALSE;
2006     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2007         if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2008             return TRUE;
2009     return FALSE;
2010 }
2011
2012 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2013 {
2014     print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2015                    is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2016     fprintf(file, "\n");
2017 }
2018
2019 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2020 {
2021     print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2022     fprintf(file, "\n");
2023 }
2024
2025 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2026                                             const type_t *type,
2027                                             int toplevel_param,
2028                                             unsigned int offset,
2029                                             unsigned int *typeformat_offset)
2030 {
2031     unsigned int start_offset = *typeformat_offset;
2032     short reloff = offset - (*typeformat_offset + 2);
2033     int in_attr, out_attr;
2034     int pointer_type;
2035     unsigned char flags = 0;
2036
2037     pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2038
2039     in_attr = is_attr(attrs, ATTR_IN);
2040     out_attr = is_attr(attrs, ATTR_OUT);
2041     if (!in_attr && !out_attr) in_attr = 1;
2042
2043     if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2044         flags |= RPC_FC_P_ONSTACK;
2045
2046     if (is_ptr(type))
2047     {
2048         type_t *ref = type_pointer_get_ref(type);
2049         if(is_declptr(ref) && !is_user_type(ref))
2050             flags |= RPC_FC_P_DEREF;
2051     }
2052
2053     print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2054                pointer_type,
2055                flags,
2056                string_of_type(pointer_type));
2057     if (file)
2058     {
2059         if (flags & RPC_FC_P_ONSTACK)
2060             fprintf(file, " [allocated_on_stack]");
2061         if (flags & RPC_FC_P_DEREF)
2062             fprintf(file, " [pointer_deref]");
2063         fprintf(file, " */\n");
2064     }
2065
2066     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2067     *typeformat_offset += 4;
2068
2069     return start_offset;
2070 }
2071
2072 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
2073 {
2074     unsigned char fc;
2075     unsigned char pointer_fc;
2076     const type_t *ref;
2077     int in_attr = is_attr(attrs, ATTR_IN);
2078     int out_attr = is_attr(attrs, ATTR_OUT);
2079     unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
2080
2081     /* for historical reasons, write_simple_pointer also handled string types,
2082      * but no longer does. catch bad uses of the function with this check */
2083     if (is_string_type(attrs, type))
2084         error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2085
2086     pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
2087
2088     ref = type_pointer_get_ref(type);
2089     if (type_get_type(ref) == TYPE_ENUM)
2090         fc = get_enum_fc(ref);
2091     else
2092         fc = get_basic_fc(ref);
2093
2094     if (out_attr && !in_attr)
2095         flags |= RPC_FC_P_ONSTACK;
2096
2097     print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2098                pointer_fc, flags, string_of_type(pointer_fc),
2099                flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
2100     print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2101     print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2102     return 4;
2103 }
2104
2105 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2106 {
2107     print_file(file, 0, "/* %u (", tfsoff);
2108     write_type_decl(file, t, NULL);
2109     print_file(file, 0, ") */\n");
2110 }
2111
2112 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2113                                       type_t *type, unsigned int ref_offset,
2114                                       int toplevel_param,
2115                                       unsigned int *typestring_offset)
2116 {
2117     unsigned int offset = *typestring_offset;
2118     type_t *ref = type_pointer_get_ref(type);
2119
2120     print_start_tfs_comment(file, type, offset);
2121     update_tfsoff(type, offset, file);
2122
2123     switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2124     {
2125     case TGT_BASIC:
2126     case TGT_ENUM:
2127         *typestring_offset += write_simple_pointer(file, attrs, type,
2128                                                    toplevel_param);
2129         break;
2130     default:
2131         if (ref_offset)
2132             write_nonsimple_pointer(file, attrs, type,
2133                                     toplevel_param,
2134                                     ref_offset,
2135                                     typestring_offset);
2136         break;
2137     }
2138
2139     return offset;
2140 }
2141
2142 static int processed(const type_t *type)
2143 {
2144     return type->typestring_offset && !type->tfswrite;
2145 }
2146
2147 static int user_type_has_variable_size(const type_t *t)
2148 {
2149     if (is_ptr(t))
2150         return TRUE;
2151     else if (type_get_type(t) == TYPE_STRUCT)
2152     {
2153         switch (get_struct_fc(t))
2154         {
2155         case RPC_FC_PSTRUCT:
2156         case RPC_FC_CSTRUCT:
2157         case RPC_FC_CPSTRUCT:
2158         case RPC_FC_CVSTRUCT:
2159             return TRUE;
2160         }
2161     }
2162     /* Note: Since this only applies to user types, we can't have a conformant
2163        array here, and strings should get filed under pointer in this case.  */
2164     return FALSE;
2165 }
2166
2167 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2168 {
2169     unsigned int start, absoff, flags;
2170     const char *name = NULL;
2171     type_t *utype = get_user_type(type, &name);
2172     unsigned int usize = type_memsize(utype);
2173     unsigned int ualign = type_buffer_alignment(utype);
2174     unsigned int size = type_memsize(type);
2175     unsigned short funoff = user_type_offset(name);
2176     short reloff;
2177
2178     if (processed(type)) return type->typestring_offset;
2179
2180     guard_rec(type);
2181
2182     if(user_type_has_variable_size(utype)) usize = 0;
2183
2184     if (type_get_type(utype) == TYPE_BASIC ||
2185         type_get_type(utype) == TYPE_ENUM)
2186     {
2187         unsigned char fc;
2188
2189         if (type_get_type(utype) == TYPE_ENUM)
2190             fc = get_enum_fc(utype);
2191         else
2192             fc = get_basic_fc(utype);
2193
2194         absoff = *tfsoff;
2195         print_start_tfs_comment(file, utype, absoff);
2196         print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2197         print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2198         *tfsoff += 2;
2199     }
2200     else
2201     {
2202         if (!processed(utype))
2203             write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2204         absoff = utype->typestring_offset;
2205     }
2206
2207     if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
2208         flags = 0x40;
2209     else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
2210         flags = 0x80;
2211     else
2212         flags = 0;
2213
2214     start = *tfsoff;
2215     update_tfsoff(type, start, file);
2216     print_start_tfs_comment(file, type, start);
2217     print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
2218     print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2219                flags | (ualign - 1), ualign - 1, flags);
2220     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2221     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2222     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2223     *tfsoff += 8;
2224     reloff = absoff - *tfsoff;
2225     print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2226     *tfsoff += 2;
2227     return start;
2228 }
2229
2230 static void write_member_type(FILE *file, const type_t *cont,
2231                               int cont_is_complex, const attr_list_t *attrs,
2232                               const type_t *type, unsigned int *corroff,
2233                               unsigned int *tfsoff)
2234 {
2235     if (is_embedded_complex(type) && !is_conformant_array(type))
2236     {
2237         unsigned int absoff;
2238         short reloff;
2239
2240         if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2241         {
2242             absoff = *corroff;
2243             *corroff += 8;
2244         }
2245         else
2246         {
2247             absoff = type->typestring_offset;
2248         }
2249         reloff = absoff - (*tfsoff + 2);
2250
2251         print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2252         /* padding is represented using FC_STRUCTPAD* types, so presumably
2253          * this is left over in the format for historical purposes in MIDL
2254          * or rpcrt4. */
2255         print_file(file, 2, "0x0,\n");
2256         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2257                    reloff, reloff, absoff);
2258         *tfsoff += 4;
2259     }
2260     else if (is_ptr(type) || is_conformant_array(type))
2261     {
2262         unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
2263         print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2264         *tfsoff += 1;
2265     }
2266     else if (!write_base_type(file, type, tfsoff))
2267         error("Unsupported member type %d\n", type_get_type(type));
2268 }
2269
2270 static void write_array_element_type(FILE *file, const type_t *type,
2271                                      int cont_is_complex, unsigned int *tfsoff)
2272 {
2273     type_t *elem = type_array_get_element(type);
2274
2275     if (!is_embedded_complex(elem) && is_ptr(elem))
2276     {
2277         type_t *ref = type_pointer_get_ref(elem);
2278
2279         if (processed(ref))
2280         {
2281             write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
2282             return;
2283         }
2284         if (!is_string_type(NULL, elem) &&
2285             (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2286         {
2287             *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
2288             return;
2289         }
2290     }
2291     return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2292 }
2293
2294 static void write_end(FILE *file, unsigned int *tfsoff)
2295 {
2296     if (*tfsoff % 2 == 0)
2297     {
2298         print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
2299         *tfsoff += 1;
2300     }
2301     print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
2302     *tfsoff += 1;
2303 }
2304
2305 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2306 {
2307     unsigned int offset = 0;
2308     var_list_t *fs = type_struct_get_fields(type);
2309     var_t *f;
2310
2311     if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2312     {
2313         type_t *ft = f->type;
2314         unsigned int size = field_memsize( ft, &offset );
2315         if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2316         {
2317             short reloff;
2318             unsigned int absoff = ft->typestring_offset;
2319             if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2320                 absoff += 8; /* we already have a corr descr, skip it */
2321             reloff = absoff - (*tfsoff + 6);
2322             print_file(file, 0, "/* %d */\n", *tfsoff);
2323             print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2324             print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
2325             write_conf_or_var_desc(file, current_structure, offset, ft,
2326                                    get_attrp(f->attrs, ATTR_SWITCHIS));
2327             print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
2328                        reloff, reloff, absoff);
2329             *tfsoff += 8;
2330         }
2331         offset += size;
2332     }
2333 }
2334
2335 static int write_pointer_description_offsets(
2336     FILE *file, const attr_list_t *attrs, type_t *type,
2337     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2338     unsigned int *typestring_offset)
2339 {
2340     int written = 0;
2341
2342     if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2343         (is_array(type) && type_array_is_decl_as_ptr(type)))
2344     {
2345         if (offset_in_memory && offset_in_buffer)
2346         {
2347             unsigned int memsize;
2348
2349             /* pointer instance
2350              *
2351              * note that MSDN states that for pointer layouts in structures,
2352              * this is a negative offset from the end of the structure, but
2353              * this statement is incorrect. all offsets are positive */
2354             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2355             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2356
2357             memsize = type_memsize(type);
2358             *offset_in_memory += memsize;
2359             /* increment these separately as in the case of conformant (varying)
2360              * structures these start at different values */
2361             *offset_in_buffer += memsize;
2362         }
2363         *typestring_offset += 4;
2364
2365         if (is_ptr(type))
2366         {
2367             type_t *ref = type_pointer_get_ref(type);
2368
2369             if (is_string_type(attrs, type))
2370                 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
2371             else if (processed(ref))
2372                 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
2373             else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2374                 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
2375             else
2376                 error("write_pointer_description_offsets: type format string unknown\n");
2377         }
2378         else
2379         {
2380             unsigned int offset = type->typestring_offset;
2381             /* skip over the pointer that is written for strings, since a
2382              * pointer has to be written in-place here */
2383             if (is_string_type(attrs, type))
2384                 offset += 4;
2385             write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
2386         }
2387
2388         return 1;
2389     }
2390
2391     if (is_array(type))
2392     {
2393         return write_pointer_description_offsets(
2394             file, attrs, type_array_get_element(type), offset_in_memory,
2395             offset_in_buffer, typestring_offset);
2396     }
2397     else if (is_non_complex_struct(type))
2398     {
2399         /* otherwise search for interesting fields to parse */
2400         const var_t *v;
2401         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2402         {
2403             if (offset_in_memory && offset_in_buffer)
2404             {
2405                 unsigned int padding;
2406                 unsigned int align = 0;
2407                 type_memsize_and_alignment(v->type, &align);
2408                 padding = ROUNDING(*offset_in_memory, align);
2409                 *offset_in_memory += padding;
2410                 *offset_in_buffer += padding;
2411             }
2412             written += write_pointer_description_offsets(
2413                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2414                 typestring_offset);
2415         }
2416     }
2417     else
2418     {
2419         if (offset_in_memory && offset_in_buffer)
2420         {
2421             unsigned int memsize = type_memsize(type);
2422             *offset_in_memory += memsize;
2423             /* increment these separately as in the case of conformant (varying)
2424              * structures these start at different values */
2425             *offset_in_buffer += memsize;
2426         }
2427     }
2428
2429     return written;
2430 }
2431
2432 static int write_no_repeat_pointer_descriptions(
2433     FILE *file, const attr_list_t *attrs, type_t *type,
2434     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2435     unsigned int *typestring_offset)
2436 {
2437     int written = 0;
2438
2439     if (is_ptr(type) ||
2440         (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2441     {
2442         print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
2443         print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2444         *typestring_offset += 2;
2445
2446         return write_pointer_description_offsets(file, attrs, type,
2447                        offset_in_memory, offset_in_buffer, typestring_offset);
2448     }
2449
2450     if (is_non_complex_struct(type))
2451     {
2452         const var_t *v;
2453         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2454         {
2455             if (offset_in_memory && offset_in_buffer)
2456             {
2457                 unsigned int padding;
2458                 unsigned int align = 0;
2459                 type_memsize_and_alignment(v->type, &align);
2460                 padding = ROUNDING(*offset_in_memory, align);
2461                 *offset_in_memory += padding;
2462                 *offset_in_buffer += padding;
2463             }
2464             written += write_no_repeat_pointer_descriptions(
2465                 file, v->attrs, v->type,
2466                 offset_in_memory, offset_in_buffer, typestring_offset);
2467         }
2468     }
2469     else
2470     {
2471         unsigned int memsize = type_memsize(type);
2472         *offset_in_memory += memsize;
2473         /* increment these separately as in the case of conformant (varying)
2474          * structures these start at different values */
2475         *offset_in_buffer += memsize;
2476     }
2477
2478     return written;
2479 }
2480
2481 /* Note: if file is NULL return value is number of pointers to write, else
2482  * it is the number of type format characters written */
2483 static int write_fixed_array_pointer_descriptions(
2484     FILE *file, const attr_list_t *attrs, type_t *type,
2485     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2486     unsigned int *typestring_offset)
2487 {
2488     int pointer_count = 0;
2489
2490     if (type_get_type(type) == TYPE_ARRAY &&
2491         !type_array_has_conformance(type) && !type_array_has_variance(type))
2492     {
2493         unsigned int temp = 0;
2494         /* unfortunately, this needs to be done in two passes to avoid
2495          * writing out redundant FC_FIXED_REPEAT descriptions */
2496         pointer_count = write_pointer_description_offsets(
2497             NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2498         if (pointer_count > 0)
2499         {
2500             unsigned int increment_size;
2501             unsigned int offset_of_array_pointer_mem = 0;
2502             unsigned int offset_of_array_pointer_buf = 0;
2503
2504             increment_size = type_memsize(type_array_get_element(type));
2505
2506             print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2507             print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2508             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2509             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2510             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2511             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2512             *typestring_offset += 10;
2513
2514             pointer_count = write_pointer_description_offsets(
2515                 file, attrs, type, &offset_of_array_pointer_mem,
2516                 &offset_of_array_pointer_buf, typestring_offset);
2517         }
2518     }
2519     else if (type_get_type(type) == TYPE_STRUCT)
2520     {
2521         const var_t *v;
2522         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2523         {
2524             if (offset_in_memory && offset_in_buffer)
2525             {
2526                 unsigned int padding;
2527                 unsigned int align = 0;
2528                 type_memsize_and_alignment(v->type, &align);
2529                 padding = ROUNDING(*offset_in_memory, align);
2530                 *offset_in_memory += padding;
2531                 *offset_in_buffer += padding;
2532             }
2533             pointer_count += write_fixed_array_pointer_descriptions(
2534                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2535                 typestring_offset);
2536         }
2537     }
2538     else
2539     {
2540         if (offset_in_memory && offset_in_buffer)
2541         {
2542             unsigned int memsize;
2543             memsize = type_memsize(type);
2544             *offset_in_memory += memsize;
2545             /* increment these separately as in the case of conformant (varying)
2546              * structures these start at different values */
2547             *offset_in_buffer += memsize;
2548         }
2549     }
2550
2551     return pointer_count;
2552 }
2553
2554 /* Note: if file is NULL return value is number of pointers to write, else
2555  * it is the number of type format characters written */
2556 static int write_conformant_array_pointer_descriptions(
2557     FILE *file, const attr_list_t *attrs, type_t *type,
2558     unsigned int offset_in_memory, unsigned int *typestring_offset)
2559 {
2560     int pointer_count = 0;
2561
2562     if (is_conformant_array(type) && !type_array_has_variance(type))
2563     {
2564         unsigned int temp = 0;
2565         /* unfortunately, this needs to be done in two passes to avoid
2566          * writing out redundant FC_VARIABLE_REPEAT descriptions */
2567         pointer_count = write_pointer_description_offsets(
2568             NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2569         if (pointer_count > 0)
2570         {
2571             unsigned int increment_size;
2572             unsigned int offset_of_array_pointer_mem = offset_in_memory;
2573             unsigned int offset_of_array_pointer_buf = offset_in_memory;
2574
2575             increment_size = type_memsize(type_array_get_element(type));
2576
2577             if (increment_size > USHRT_MAX)
2578                 error("array size of %u bytes is too large\n", increment_size);
2579
2580             print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2581             print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2582             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2583             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2584             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2585             *typestring_offset += 8;
2586
2587             pointer_count = write_pointer_description_offsets(
2588                 file, attrs, type_array_get_element(type),
2589                 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2590                 typestring_offset);
2591         }
2592     }
2593
2594     return pointer_count;
2595 }
2596
2597 /* Note: if file is NULL return value is number of pointers to write, else
2598  * it is the number of type format characters written */
2599 static int write_varying_array_pointer_descriptions(
2600     FILE *file, const attr_list_t *attrs, type_t *type,
2601     unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2602     unsigned int *typestring_offset)
2603 {
2604     int pointer_count = 0;
2605
2606     if (is_array(type) && type_array_has_variance(type))
2607     {
2608         unsigned int temp = 0;
2609         /* unfortunately, this needs to be done in two passes to avoid
2610          * writing out redundant FC_VARIABLE_REPEAT descriptions */
2611         pointer_count = write_pointer_description_offsets(
2612             NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2613         if (pointer_count > 0)
2614         {
2615             unsigned int increment_size;
2616
2617             increment_size = type_memsize(type_array_get_element(type));
2618
2619             if (increment_size > USHRT_MAX)
2620                 error("array size of %u bytes is too large\n", increment_size);
2621
2622             print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2623             print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2624             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2625             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2626             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2627             *typestring_offset += 8;
2628
2629             pointer_count = write_pointer_description_offsets(
2630                 file, attrs, type_array_get_element(type), offset_in_memory,
2631                 offset_in_buffer, typestring_offset);
2632         }
2633     }
2634     else if (type_get_type(type) == TYPE_STRUCT)
2635     {
2636         const var_t *v;
2637         LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2638         {
2639             if (offset_in_memory && offset_in_buffer)
2640             {
2641                 unsigned int align = 0, padding;
2642
2643                 if (is_array(v->type) && type_array_has_variance(v->type))
2644                 {
2645                     *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2646                     /* skip over variance and offset in buffer */
2647                     *offset_in_buffer += 8;
2648                 }
2649
2650                 type_memsize_and_alignment(v->type, &align);
2651                 padding = ROUNDING(*offset_in_memory, align);
2652                 *offset_in_memory += padding;
2653                 *offset_in_buffer += padding;
2654             }
2655             pointer_count += write_varying_array_pointer_descriptions(
2656                 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2657                 typestring_offset);
2658         }
2659     }
2660     else
2661     {
2662         if (offset_in_memory && offset_in_buffer)
2663         {
2664             unsigned int memsize = type_memsize(type);
2665             *offset_in_memory += memsize;
2666             /* increment these separately as in the case of conformant (varying)
2667              * structures these start at different values */
2668             *offset_in_buffer += memsize;
2669         }
2670     }
2671
2672     return pointer_count;
2673 }
2674
2675 static void write_pointer_description(FILE *file, type_t *type,
2676                                       unsigned int *typestring_offset)
2677 {
2678     unsigned int offset_in_buffer;
2679     unsigned int offset_in_memory;
2680
2681     /* pass 1: search for single instance of a pointer (i.e. don't descend
2682      * into arrays) */
2683     if (!is_array(type))
2684     {
2685         offset_in_memory = 0;
2686         offset_in_buffer = 0;
2687         write_no_repeat_pointer_descriptions(
2688             file, NULL, type,
2689             &offset_in_memory, &offset_in_buffer, typestring_offset);
2690     }
2691
2692     /* pass 2: search for pointers in fixed arrays */
2693     offset_in_memory = 0;
2694     offset_in_buffer = 0;
2695     write_fixed_array_pointer_descriptions(
2696         file, NULL, type,
2697         &offset_in_memory, &offset_in_buffer, typestring_offset);
2698
2699     /* pass 3: search for pointers in conformant only arrays (but don't descend
2700      * into conformant varying or varying arrays) */
2701     if (is_conformant_array(type) &&
2702         (type_array_is_decl_as_ptr(type) || !current_structure))
2703         write_conformant_array_pointer_descriptions(
2704             file, NULL, type, 0, typestring_offset);
2705     else if (type_get_type(type) == TYPE_STRUCT &&
2706              get_struct_fc(type) == RPC_FC_CPSTRUCT)
2707     {
2708         type_t *carray = find_array_or_string_in_struct(type)->type;
2709         write_conformant_array_pointer_descriptions( file, NULL, carray,
2710                                                      type_memsize(type), typestring_offset);
2711     }
2712
2713     /* pass 4: search for pointers in varying arrays */
2714     offset_in_memory = 0;
2715     offset_in_buffer = 0;
2716     write_varying_array_pointer_descriptions(
2717             file, NULL, type,
2718             &offset_in_memory, &offset_in_buffer, typestring_offset);
2719 }
2720
2721 int is_declptr(const type_t *t)
2722 {
2723   return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2724 }
2725
2726 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2727                                      type_t *type, int toplevel_param,
2728                                      const char *name, unsigned int *typestring_offset)
2729 {
2730     unsigned int start_offset;
2731     unsigned char rtype;
2732     type_t *elem_type;
2733     int is_processed = processed(type);
2734
2735     start_offset = *typestring_offset;
2736
2737     if (is_declptr(type))
2738     {
2739         unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2740         int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2741         if (!pointer_type)
2742             pointer_type = RPC_FC_RP;
2743         print_start_tfs_comment(file, type, *typestring_offset);
2744         print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2745                    pointer_type, flag, string_of_type(pointer_type),
2746                    flag ? " [simple_pointer]" : "");
2747         *typestring_offset += 2;
2748         if (!flag)
2749         {
2750             print_file(file, 2, "NdrFcShort(0x2),\n");
2751             *typestring_offset += 2;
2752         }
2753         is_processed = FALSE;
2754     }
2755
2756     if (is_array(type))
2757         elem_type = type_array_get_element(type);
2758     else
2759         elem_type = type_pointer_get_ref(type);
2760
2761     if (type_get_type(elem_type) != TYPE_BASIC)
2762     {
2763         error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2764         return start_offset;
2765     }
2766
2767     rtype = get_basic_fc(elem_type);
2768     if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2769     {
2770         error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2771         return start_offset;
2772     }
2773
2774     if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2775     {
2776         unsigned int dim = type_array_get_dim(type);
2777
2778         if (is_processed) return start_offset;
2779
2780         /* FIXME: multi-dimensional array */
2781         if (0xffffu < dim)
2782             error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2783                   name, 0xffffu, dim - 0xffffu);
2784
2785         if (rtype == RPC_FC_WCHAR)
2786             WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2787         else
2788             WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2789         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2790         *typestring_offset += 2;
2791
2792         print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2793         *typestring_offset += 2;
2794
2795         update_tfsoff(type, start_offset, file);
2796         return start_offset;
2797     }
2798     else if (is_conformant_array(type))
2799     {
2800         if (rtype == RPC_FC_WCHAR)
2801             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2802         else
2803             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2804         print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2805         *typestring_offset += 2;
2806
2807         *typestring_offset += write_conf_or_var_desc(
2808             file, current_structure,
2809             (!type_array_is_decl_as_ptr(type) && current_structure
2810              ? type_memsize(current_structure)
2811              : 0),
2812             type, type_array_get_conformance(type));
2813
2814         update_tfsoff(type, start_offset, file);
2815         return start_offset;
2816     }
2817     else
2818     {
2819         if (is_processed) return start_offset;
2820
2821         if (rtype == RPC_FC_WCHAR)
2822             WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2823         else
2824             WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2825         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2826         *typestring_offset += 2;
2827
2828         update_tfsoff(type, start_offset, file);
2829         return start_offset;
2830     }
2831 }
2832
2833 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2834                                     const char *name, unsigned int *typestring_offset)
2835 {
2836     const expr_t *length_is = type_array_get_variance(type);
2837     const expr_t *size_is = type_array_get_conformance(type);
2838     unsigned int align;
2839     unsigned int size;
2840     unsigned int start_offset;
2841     unsigned char fc;
2842     int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2843     unsigned int baseoff
2844         = !type_array_is_decl_as_ptr(type) && current_structure
2845         ? type_memsize(current_structure)
2846         : 0;
2847
2848     if (!pointer_type)
2849         pointer_type = RPC_FC_RP;
2850
2851     write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2852
2853     size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2854     align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2855     fc = get_array_fc(type);
2856
2857     start_offset = *typestring_offset;
2858     update_tfsoff(type, start_offset, file);
2859     print_start_tfs_comment(file, type, start_offset);
2860     print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2861     print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2862     *typestring_offset += 2;
2863
2864     align = 0;
2865     if (fc != RPC_FC_BOGUS_ARRAY)
2866     {
2867         if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2868         {
2869             print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2870             *typestring_offset += 4;
2871         }
2872         else
2873         {
2874             print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2875             *typestring_offset += 2;
2876         }
2877
2878         if (is_conformant_array(type))
2879             *typestring_offset
2880                 += write_conf_or_var_desc(file, current_structure, baseoff,
2881                                           type, size_is);
2882
2883         if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2884         {
2885             unsigned int elsize = type_memsize(type_array_get_element(type));
2886             unsigned int dim = type_array_get_dim(type);
2887
2888             if (fc == RPC_FC_LGVARRAY)
2889             {
2890                 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2891                 *typestring_offset += 4;
2892             }
2893             else
2894             {
2895                 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2896                 *typestring_offset += 2;
2897             }
2898
2899             print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2900             *typestring_offset += 2;
2901         }
2902
2903         if (length_is)
2904             *typestring_offset
2905                 += write_conf_or_var_desc(file, current_structure, baseoff,
2906                                           type, length_is);
2907
2908         if (type_has_pointers(type_array_get_element(type)) &&
2909             (type_array_is_decl_as_ptr(type) || !current_structure))
2910         {
2911             print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2912             print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2913             *typestring_offset += 2;
2914             write_pointer_description(file, type, typestring_offset);
2915             print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2916             *typestring_offset += 1;
2917         }
2918
2919         write_array_element_type(file, type, FALSE, typestring_offset);
2920         write_end(file, typestring_offset);
2921     }
2922     else
2923     {
2924         unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2925         print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2926         *typestring_offset += 2;
2927         *typestring_offset
2928             += write_conf_or_var_desc(file, current_structure, baseoff,
2929                                       type, size_is);
2930         *typestring_offset
2931             += write_conf_or_var_desc(file, current_structure, baseoff,
2932                                       type, length_is);
2933
2934         write_array_element_type(file, type, TRUE, typestring_offset);
2935         write_end(file, typestring_offset);
2936     }
2937
2938     return start_offset;
2939 }
2940
2941 static const var_t *find_array_or_string_in_struct(const type_t *type)
2942 {
2943     const var_list_t *fields = type_struct_get_fields(type);
2944     const var_t *last_field;
2945     const type_t *ft;
2946
2947     if (!fields || list_empty(fields))
2948         return NULL;
2949
2950     last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2951     ft = last_field->type;
2952
2953     if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2954         return last_field;
2955
2956     if (type_get_type(ft) == TYPE_STRUCT)
2957         return find_array_or_string_in_struct(ft);
2958     else
2959         return NULL;
2960 }
2961
2962 static void write_struct_members(FILE *file, const type_t *type,
2963                                  int is_complex, unsigned int *corroff,
2964                                  unsigned int *typestring_offset)
2965 {
2966     const var_t *field;
2967     unsigned short offset = 0;
2968     unsigned int salign = 1;
2969     int padding;
2970     var_list_t *fields = type_struct_get_fields(type);
2971
2972     if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2973     {
2974         type_t *ft = field->type;
2975         unsigned int align = 0;
2976         unsigned int size = type_memsize_and_alignment(ft, &align);
2977         align = clamp_align(align);
2978         if (salign < align) salign = align;
2979
2980         if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2981         {
2982             if ((align - 1) & offset)
2983             {
2984                 unsigned char fc = 0;
2985                 switch (align)
2986                 {
2987                 case 2:
2988                     fc = RPC_FC_ALIGNM2;
2989                     break;
2990                 case 4:
2991                     fc = RPC_FC_ALIGNM4;
2992                     break;
2993                 case 8:
2994                     fc = RPC_FC_ALIGNM8;
2995                     break;
2996                 default:
2997                     error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2998                 }
2999                 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3000                 offset = ROUND_SIZE(offset, align);
3001                 *typestring_offset += 1;
3002             }
3003             write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3004                               typestring_offset);
3005             offset += size;
3006         }
3007     }
3008
3009     padding = ROUNDING(offset, salign);
3010     if (padding)
3011     {
3012         print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3013                    RPC_FC_STRUCTPAD1 + padding - 1,
3014                    padding);
3015         *typestring_offset += 1;
3016     }
3017
3018     write_end(file, typestring_offset);
3019 }
3020
3021 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3022                                      const char *name, unsigned int *tfsoff)
3023 {
3024     const type_t *save_current_structure = current_structure;
3025     unsigned int total_size;
3026     const var_t *array;
3027     unsigned int start_offset;
3028     unsigned int align;
3029     unsigned int corroff;
3030     var_t *f;
3031     unsigned char fc = get_struct_fc(type);
3032     var_list_t *fields = type_struct_get_fields(type);
3033
3034     if (processed(type)) return type->typestring_offset;
3035
3036     guard_rec(type);
3037     current_structure = type;
3038
3039     total_size = type_memsize(type);
3040     align = type_buffer_alignment(type);
3041     if (total_size > USHRT_MAX)
3042         error("structure size for %s exceeds %d bytes by %d bytes\n",
3043               name, USHRT_MAX, total_size - USHRT_MAX);
3044
3045     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3046         write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3047
3048     array = find_array_or_string_in_struct(type);
3049     if (array && !processed(array->type))
3050     {
3051         if(is_string_type(array->attrs, array->type))
3052             write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff);
3053         else
3054             write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3055     }
3056
3057     corroff = *tfsoff;
3058     write_descriptors(file, type, tfsoff);
3059
3060     start_offset = *tfsoff;
3061     update_tfsoff(type, start_offset, file);
3062     print_start_tfs_comment(file, type, start_offset);
3063     print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3064     print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3065     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3066     *tfsoff += 4;
3067
3068     if (array)
3069     {
3070         unsigned int absoff = array->type->typestring_offset;
3071         short reloff = absoff - *tfsoff;
3072         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3073                    reloff, reloff, absoff);
3074         *tfsoff += 2;
3075     }
3076     else if (fc == RPC_FC_BOGUS_STRUCT)
3077     {
3078         print_file(file, 2, "NdrFcShort(0x0),\n");
3079         *tfsoff += 2;
3080     }
3081
3082     if (fc == RPC_FC_BOGUS_STRUCT)
3083     {
3084         /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3085            nothing is written to file yet.  On the actual writing pass,
3086            this will have been updated.  */
3087         unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3088         int reloff = absoff - *tfsoff;
3089         assert( reloff >= 0 );
3090         print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3091                    (unsigned short)reloff, reloff, absoff);
3092         *tfsoff += 2;
3093     }
3094     else if ((fc == RPC_FC_PSTRUCT) ||
3095              (fc == RPC_FC_CPSTRUCT) ||
3096              (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
3097     {
3098         print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
3099         print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
3100         *tfsoff += 2;
3101         write_pointer_description(file, type, tfsoff);
3102         print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
3103         *tfsoff += 1;
3104     }
3105
3106     write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
3107                          tfsoff);
3108
3109     if (fc == RPC_FC_BOGUS_STRUCT)
3110     {
3111         const var_t *f;
3112
3113         type->ptrdesc = *tfsoff;
3114         if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3115         {
3116             type_t *ft = f->type;
3117             switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3118             {
3119             case TGT_POINTER:
3120                 if (is_string_type(f->attrs, ft))
3121                     write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
3122                 else
3123                     write_pointer_tfs(file, f->attrs, ft,
3124                                       type_pointer_get_ref(ft)->typestring_offset,
3125                                       FALSE, tfsoff);
3126                 break;
3127             case TGT_ARRAY:
3128                 if (type_array_is_decl_as_ptr(ft))
3129                 {
3130                     unsigned int offset;
3131
3132                     print_file(file, 0, "/* %d */\n", *tfsoff);
3133
3134                     offset = ft->typestring_offset;
3135                     /* skip over the pointer that is written for strings, since a
3136                      * pointer has to be written in-place here */
3137                     if (is_string_type(f->attrs, ft))
3138                         offset += 4;
3139                     write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
3140                 }
3141                 break;
3142             default:
3143                 break;
3144             }
3145         }
3146         if (type->ptrdesc == *tfsoff)
3147             type->ptrdesc = 0;
3148     }
3149
3150     current_structure = save_current_structure;
3151     return start_offset;
3152 }
3153
3154 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3155 {
3156     if (t == NULL)
3157     {
3158         print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3159     }
3160     else
3161     {
3162         if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3163         {
3164             unsigned char fc;
3165             if (type_get_type(t) == TYPE_BASIC)
3166                 fc = get_basic_fc(t);
3167             else
3168                 fc = get_enum_fc(t);
3169             print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3170                        fc, string_of_type(fc));
3171         }
3172         else if (t->typestring_offset)
3173         {
3174             short reloff = t->typestring_offset - *tfsoff;
3175             print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3176                        reloff, reloff, t->typestring_offset);
3177         }
3178         else
3179             error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3180     }
3181
3182     *tfsoff += 2;
3183 }
3184
3185 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3186                                     type_t *type, unsigned int *tfsoff)
3187 {
3188     unsigned int start_offset;
3189     unsigned int size;
3190     var_list_t *fields;
3191     unsigned int nbranch = 0;
3192     type_t *deftype = NULL;
3193     short nodeftype = 0xffff;
3194     var_t *f;
3195
3196     if (processed(type) &&
3197         (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3198         return type->typestring_offset;
3199
3200     guard_rec(type);
3201
3202     size = type_memsize(type);
3203
3204     fields = type_union_get_cases(type);
3205
3206     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3207     {
3208         expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3209         if (cases)
3210             nbranch += list_count(cases);
3211         if (f->type)
3212             write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3213     }
3214
3215     start_offset = *tfsoff;
3216     update_tfsoff(type, start_offset, file);
3217     print_start_tfs_comment(file, type, start_offset);
3218     if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3219     {
3220         const var_t *sv = type_union_get_switch_value(type);
3221         const type_t *st = sv->type;
3222         unsigned char fc;
3223
3224         if (type_get_type(st) == TYPE_BASIC)
3225         {
3226             fc = get_basic_fc(st);
3227             switch (fc)
3228             {
3229             case RPC_FC_CHAR:
3230             case RPC_FC_SMALL:
3231             case RPC_FC_BYTE:
3232             case RPC_FC_USMALL:
3233             case RPC_FC_WCHAR:
3234             case RPC_FC_SHORT:
3235             case RPC_FC_USHORT:
3236             case RPC_FC_LONG:
3237             case RPC_FC_ULONG:
3238                 break;
3239             default:
3240                 fc = 0;
3241                 error("union switch type must be an integer, char, or enum\n");
3242             }
3243         }
3244         else if (type_get_type(st) == TYPE_ENUM)
3245             fc = get_enum_fc(st);
3246         else
3247             error("union switch type must be an integer, char, or enum\n");
3248
3249         print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
3250         print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3251                    0x40 | fc, string_of_type(fc));
3252         *tfsoff += 2;
3253     }
3254     else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3255     {
3256         const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3257         const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3258         unsigned char fc;
3259
3260         if (type_get_type(st) == TYPE_BASIC)
3261         {
3262             fc = get_basic_fc(st);
3263             switch (fc)
3264             {
3265             case RPC_FC_CHAR:
3266             case RPC_FC_SMALL:
3267             case RPC_FC_USMALL:
3268             case RPC_FC_SHORT:
3269             case RPC_FC_USHORT:
3270             case RPC_FC_LONG:
3271             case RPC_FC_ULONG:
3272             case RPC_FC_ENUM16:
3273             case RPC_FC_ENUM32:
3274                 break;
3275             default:
3276                 fc = 0;
3277                 error("union switch type must be an integer, char, or enum\n");
3278             }
3279         }
3280         else if (type_get_type(st) == TYPE_ENUM)
3281             fc = get_enum_fc(st);
3282         else
3283             error("union switch type must be an integer, char, or enum\n");
3284
3285         print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
3286         print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3287                    fc, string_of_type(fc));
3288         *tfsoff += 2;
3289         *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3290         print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3291         *tfsoff += 2;
3292         print_file(file, 0, "/* %u */\n", *tfsoff);
3293     }
3294
3295     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3296     print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3297     *tfsoff += 4;
3298
3299     if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3300     {
3301         type_t *ft = f->type;
3302         expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3303         int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3304         expr_t *c;
3305
3306         if (cases == NULL && !deflt)
3307             error("union field %s with neither case nor default attribute\n", f->name);
3308
3309         if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3310         {
3311             /* MIDL doesn't check for duplicate cases, even though that seems
3312                like a reasonable thing to do, it just dumps them to the TFS
3313                like we're going to do here.  */
3314             print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3315             *tfsoff += 4;
3316             write_branch_type(file, ft, tfsoff);
3317         }
3318
3319         /* MIDL allows multiple default branches, even though that seems
3320            illogical, it just chooses the last one, which is what we will
3321            do.  */
3322         if (deflt)
3323         {
3324             deftype = ft;
3325             nodeftype = 0;
3326         }
3327     }
3328
3329     if (deftype)
3330     {
3331         write_branch_type(file, deftype, tfsoff);
3332     }
3333     else
3334     {
3335         print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3336         *tfsoff += 2;
3337     }
3338
3339     return start_offset;
3340 }
3341
3342 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3343                                  unsigned int *typeformat_offset)
3344 {
3345     unsigned int i;
3346     unsigned int start_offset = *typeformat_offset;
3347     expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3348
3349     if (!iid && processed(type)) return type->typestring_offset;
3350
3351     print_start_tfs_comment(file, type, start_offset);
3352     update_tfsoff(type, start_offset, file);
3353
3354     if (iid)
3355     {
3356         print_file(file, 2, "0x2f,  /* FC_IP */\n");
3357         print_file(file, 2, "0x5c,  /* FC_PAD */\n");
3358         *typeformat_offset
3359             += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3360     }
3361     else
3362     {
3363         const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3364         const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3365
3366         if (! uuid)
3367             error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3368
3369         print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3370         print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3371         print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3372         print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3373         print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3374         for (i = 0; i < 8; ++i)
3375             print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3376
3377         if (file)
3378             fprintf(file, "\n");
3379
3380         *typeformat_offset += 18;
3381     }
3382     return start_offset;
3383 }
3384
3385 static unsigned int write_contexthandle_tfs(FILE *file,
3386                                             const attr_list_t *attrs,
3387                                             type_t *type,
3388                                             int toplevel_param,
3389                                             unsigned int *typeformat_offset)
3390 {
3391     unsigned int start_offset = *typeformat_offset;
3392     unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3393
3394     print_start_tfs_comment(file, type, start_offset);
3395
3396     if (flags & 0x80)  /* via ptr */
3397     {
3398         int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3399         if (!pointer_type) pointer_type = RPC_FC_RP;
3400         *typeformat_offset += 4;
3401         print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3402         print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3403         print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3404     }
3405
3406     print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT);
3407     print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3408     /* return and can't be null values overlap */
3409     if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3410         print_file(file, 0, "can't be null, ");
3411     if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3412         print_file(file, 0, "serialize, ");
3413     if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
3414         print_file(file, 0, "no serialize, ");
3415     if (flags & NDR_STRICT_CONTEXT_HANDLE)
3416         print_file(file, 0, "strict, ");
3417     if ((flags & 0x21) == 0x20)
3418         print_file(file, 0, "out, ");
3419     if ((flags & 0x21) == 0x21)
3420         print_file(file, 0, "return, ");
3421     if (flags & 0x40)
3422         print_file(file, 0, "in, ");
3423     if (flags & 0x80)
3424         print_file(file, 0, "via ptr, ");
3425     print_file(file, 0, "*/\n");
3426     print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3427     print_file(file, 2, "0, /* FIXME: param num */\n");
3428     *typeformat_offset += 4;
3429
3430     update_tfsoff( type, start_offset, file );
3431     return start_offset;
3432 }
3433
3434 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3435                                     type_t *type, expr_list_t *range_list,
3436                                     unsigned int *typeformat_offset)
3437 {
3438     unsigned char fc;
3439     unsigned int start_offset = *typeformat_offset;
3440     const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3441     const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3442
3443     if (type_get_type(type) == TYPE_BASIC)
3444         fc = get_basic_fc(type);
3445     else
3446         fc = get_enum_fc(type);
3447
3448     /* fc must fit in lower 4-bits of 8-bit field below */
3449     assert(fc <= 0xf);
3450
3451     print_file(file, 0, "/* %u */\n", *typeformat_offset);
3452     print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3453     print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3454     print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3455     print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3456     update_tfsoff( type, start_offset, file );
3457     *typeformat_offset += 10;
3458
3459     return start_offset;
3460 }
3461
3462 static unsigned int write_type_tfs(FILE *file, int indent,
3463                                    const attr_list_t *attrs, type_t *type,
3464                                    const char *name,
3465                                    enum type_context context,
3466                                    unsigned int *typeformat_offset)
3467 {
3468     unsigned int offset;
3469
3470     switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3471     {
3472     case TGT_CTXT_HANDLE:
3473     case TGT_CTXT_HANDLE_POINTER:
3474         return write_contexthandle_tfs(file, attrs, type,
3475                                        context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3476     case TGT_USER_TYPE:
3477         return write_user_tfs(file, type, typeformat_offset);
3478     case TGT_STRING:
3479         return write_string_tfs(file, attrs, type,
3480                                 context == TYPE_CONTEXT_TOPLEVELPARAM,
3481                                 name, typeformat_offset);
3482     case TGT_ARRAY:
3483     {
3484         unsigned int off;
3485         /* conformant and pointer arrays are handled specially */
3486         if ((context != TYPE_CONTEXT_CONTAINER &&
3487              context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3488             !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3489             off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3490         else
3491             off = 0;
3492         if (context != TYPE_CONTEXT_CONTAINER &&
3493             context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3494         {
3495             int ptr_type;
3496             ptr_type = get_pointer_fc(type, attrs,
3497                                       context == TYPE_CONTEXT_TOPLEVELPARAM);
3498             if (ptr_type != RPC_FC_RP || type_array_is_decl_as_ptr(type))
3499             {
3500                 unsigned int absoff = type->typestring_offset;
3501                 short reloff = absoff - (*typeformat_offset + 2);
3502                 off = *typeformat_offset;
3503                 print_file(file, 0, "/* %d */\n", off);
3504                 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3505                            string_of_type(ptr_type));
3506                 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3507                            reloff, reloff, absoff);
3508                 if (ptr_type != RPC_FC_RP) update_tfsoff( type, off, file );
3509                 *typeformat_offset += 4;
3510             }
3511             type->details.array.ptr_tfsoff = off;
3512         }
3513         return off;
3514     }
3515     case TGT_STRUCT:
3516         return write_struct_tfs(file, type, name, typeformat_offset);
3517     case TGT_UNION:
3518         return write_union_tfs(file, attrs, type, typeformat_offset);
3519     case TGT_ENUM:
3520     case TGT_BASIC:
3521         /* nothing to do */
3522         return 0;
3523     case TGT_RANGE:
3524     {
3525         expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3526         if (!range_list)
3527             range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3528         return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3529     }
3530     case TGT_IFACE_POINTER:
3531         return write_ip_tfs(file, attrs, type, typeformat_offset);
3532     case TGT_POINTER:
3533     {
3534         enum type_context ref_context;
3535         if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3536             ref_context = TYPE_CONTEXT_PARAM;
3537         else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3538             ref_context = TYPE_CONTEXT_CONTAINER;
3539         else
3540             ref_context = context;
3541         offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3542                                  ref_context, typeformat_offset);
3543         if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3544             return 0;
3545         return write_pointer_tfs(file, attrs, type, offset,
3546                                  context == TYPE_CONTEXT_TOPLEVELPARAM,
3547                                  typeformat_offset);
3548     }
3549     case TGT_INVALID:
3550         break;
3551     }
3552     error("invalid type %s for var %s\n", type->name, name);
3553     return 0;
3554 }
3555
3556 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3557                                 const char *name, int write_ptr, unsigned int *tfsoff)
3558 {
3559     return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3560 }
3561
3562 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3563                                       type_pred_t pred, unsigned int *typeformat_offset)
3564 {
3565     const var_t *var;
3566     const statement_t *stmt;
3567
3568     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3569     {
3570         const type_t *iface;
3571         const statement_t *stmt_func;
3572
3573         if (stmt->type == STMT_LIBRARY)
3574         {
3575             process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
3576             continue;
3577         }
3578         else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3579             continue;
3580
3581         iface = stmt->u.type;
3582         if (!pred(iface))
3583             continue;
3584
3585         current_iface = iface;
3586         STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3587         {
3588             const var_t *func = stmt_func->u.var;
3589             current_func = func;
3590             if (is_local(func->attrs)) continue;
3591
3592             if (!is_void(type_function_get_rettype(func->type)))
3593             {
3594                 write_type_tfs( file, 2, func->attrs, type_function_get_rettype(func->type),
3595                                 func->name, TYPE_CONTEXT_PARAM, typeformat_offset);
3596             }
3597
3598             if (type_get_function_args(func->type))
3599                 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3600                     write_type_tfs( file, 2, var->attrs, var->type, var->name,
3601                                     TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset );
3602         }
3603     }
3604
3605     return *typeformat_offset + 1;
3606 }
3607
3608 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3609 {
3610     unsigned int typeformat_offset = 2;
3611
3612     return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3613 }
3614
3615
3616 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3617 {
3618     int indent = 0;
3619
3620     print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3621     print_file(file, indent, "{\n");
3622     indent++;
3623     print_file(file, indent, "0,\n");
3624     print_file(file, indent, "{\n");
3625     indent++;
3626     print_file(file, indent, "NdrFcShort(0x0),\n");
3627
3628     set_all_tfswrite(TRUE);
3629     process_tfs(file, stmts, pred);
3630
3631     print_file(file, indent, "0x0\n");
3632     indent--;
3633     print_file(file, indent, "}\n");
3634     indent--;
3635     print_file(file, indent, "};\n");
3636     print_file(file, indent, "\n");
3637 }
3638
3639 static unsigned int get_required_buffer_size_type(
3640     const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3641 {
3642     *alignment = 0;
3643     switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3644     {
3645     case TGT_USER_TYPE:
3646     {
3647         const char *uname;
3648         const type_t *utype = get_user_type(type, &uname);
3649         return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3650     }
3651     case TGT_BASIC:
3652         switch (get_basic_fc(type))
3653         {
3654         case RPC_FC_BYTE:
3655         case RPC_FC_CHAR:
3656         case RPC_FC_USMALL:
3657         case RPC_FC_SMALL:
3658             *alignment = 4;
3659             return 1;
3660
3661         case RPC_FC_WCHAR:
3662         case RPC_FC_USHORT:
3663         case RPC_FC_SHORT:
3664             *alignment = 4;
3665             return 2;
3666
3667         case RPC_FC_ULONG:
3668         case RPC_FC_LONG:
3669         case RPC_FC_FLOAT:
3670         case RPC_FC_ERROR_STATUS_T:
3671             *alignment = 4;
3672             return 4;
3673
3674         case RPC_FC_HYPER:
3675         case RPC_FC_DOUBLE:
3676             *alignment = 8;
3677             return 8;
3678
3679         case RPC_FC_INT3264:
3680         case RPC_FC_UINT3264:
3681             assert( pointer_size );
3682             *alignment = pointer_size;
3683             return pointer_size;
3684
3685         case RPC_FC_IGNORE:
3686         case RPC_FC_BIND_PRIMITIVE:
3687             return 0;
3688
3689         default:
3690             error("get_required_buffer_size: unknown basic type 0x%02x\n",
3691                   get_basic_fc(type));
3692             return 0;
3693         }
3694         break;
3695
3696     case TGT_ENUM:
3697         switch (get_enum_fc(type))
3698         {
3699         case RPC_FC_ENUM32:
3700             *alignment = 4;
3701             return 4;
3702         case RPC_FC_ENUM16:
3703             *alignment = 4;
3704             return 2;
3705         }
3706         break;
3707
3708     case TGT_STRUCT:
3709         if (get_struct_fc(type) == RPC_FC_STRUCT)
3710         {
3711             if (!type_struct_get_fields(type)) return 0;
3712             return fields_memsize(type_struct_get_fields(type), alignment);
3713         }
3714         break;
3715
3716     case TGT_POINTER:
3717         {
3718             unsigned int size, align;
3719             const type_t *ref = type_pointer_get_ref(type);
3720             if (is_string_type( attrs, ref )) break;
3721             if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3722             if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3723             {
3724                 size += 4 + align;
3725                 align = 4;
3726             }
3727             *alignment = align;
3728             return size;
3729         }
3730
3731     case TGT_ARRAY:
3732         if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3733         {
3734             switch (get_array_fc(type))
3735             {
3736             case RPC_FC_SMFARRAY:
3737             case RPC_FC_LGFARRAY:
3738                 return type_array_get_dim(type) *
3739                     get_required_buffer_size_type(type_array_get_element(type), name,
3740                                                   NULL, FALSE, alignment);
3741             }
3742         }
3743         break;
3744
3745     default:
3746         break;
3747     }
3748     return 0;
3749 }
3750
3751 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3752 {
3753     int in_attr = is_attr(var->attrs, ATTR_IN);
3754     int out_attr = is_attr(var->attrs, ATTR_OUT);
3755
3756     if (!in_attr && !out_attr)
3757         in_attr = 1;
3758
3759     *alignment = 0;
3760
3761     if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3762         pass == PASS_RETURN)
3763     {
3764         if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3765         {
3766             *alignment = 4;
3767             return 20;
3768         }
3769
3770         if (!is_string_type(var->attrs, var->type))
3771             return get_required_buffer_size_type(var->type, var->name,
3772                                                  var->attrs, TRUE, alignment);
3773     }
3774     return 0;
3775 }
3776
3777 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3778 {
3779     const var_t *var;
3780     unsigned int total_size = 0, alignment;
3781
3782     if (type_get_function_args(func->type))
3783     {
3784         LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3785         {
3786             total_size += get_required_buffer_size(var, &alignment, pass);
3787             total_size += alignment;
3788         }
3789     }
3790
3791     if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3792     {
3793         var_t v = *func;
3794         v.type = type_function_get_rettype(func->type);
3795         total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3796         total_size += alignment;
3797     }
3798     return total_size;
3799 }
3800
3801 static void print_phase_function(FILE *file, int indent, const char *type,
3802                                  const char *local_var_prefix, enum remoting_phase phase,
3803                                  const var_t *var, unsigned int type_offset)
3804 {
3805     const char *function;
3806     switch (phase)
3807     {
3808     case PHASE_BUFFERSIZE:
3809         function = "BufferSize";
3810         break;
3811     case PHASE_MARSHAL:
3812         function = "Marshall";
3813         break;
3814     case PHASE_UNMARSHAL:
3815         function = "Unmarshall";
3816         break;
3817     case PHASE_FREE:
3818         function = "Free";
3819         break;
3820     default:
3821         assert(0);
3822         return;
3823     }
3824
3825     print_file(file, indent, "Ndr%s%s(\n", type, function);
3826     indent++;
3827     print_file(file, indent, "&__frame->_StubMsg,\n");
3828     print_file(file, indent, "%s%s%s%s%s,\n",
3829                (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3830                (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3831                local_var_prefix,
3832                (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3833                var->name);
3834     print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3835                type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3836     if (phase == PHASE_UNMARSHAL)
3837         print_file(file, indent, "0);\n");
3838     indent--;
3839 }
3840
3841 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3842                           enum remoting_phase phase, enum pass pass, const var_t *var,
3843                           const char *varname)
3844 {
3845     type_t *type = var->type;
3846     unsigned int alignment = 0;
3847
3848     /* no work to do for other phases, buffer sizing is done elsewhere */
3849     if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3850         return;
3851
3852     if (type_get_type(type) == TYPE_ENUM ||
3853         (type_get_type(type) == TYPE_BASIC &&
3854          type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3855          pointer_size != 4))
3856     {
3857         unsigned char fc;
3858
3859         if (type_get_type(type) == TYPE_ENUM)
3860             fc = get_enum_fc(type);
3861         else
3862             fc = get_basic_fc(type);
3863
3864         if (phase == PHASE_MARSHAL)
3865             print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3866         else
3867             print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3868         print_file(file, indent+1, "&__frame->_StubMsg,\n");
3869         print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3870                    local_var_prefix,
3871                    var->name);
3872         print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3873     }
3874     else
3875     {
3876         const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3877         switch (get_basic_fc(ref))
3878         {
3879         case RPC_FC_BYTE:
3880         case RPC_FC_CHAR:
3881         case RPC_FC_SMALL:
3882         case RPC_FC_USMALL:
3883             alignment = 1;
3884             break;
3885
3886         case RPC_FC_WCHAR:
3887         case RPC_FC_USHORT:
3888         case RPC_FC_SHORT:
3889             alignment = 2;
3890             break;
3891
3892         case RPC_FC_ULONG:
3893         case RPC_FC_LONG:
3894         case RPC_FC_FLOAT:
3895         case RPC_FC_ERROR_STATUS_T:
3896         /* pointer_size must be 4 if we got here in these two cases */
3897         case RPC_FC_INT3264:
3898         case RPC_FC_UINT3264:
3899             alignment = 4;
3900             break;
3901
3902         case RPC_FC_HYPER:
3903         case RPC_FC_DOUBLE:
3904             alignment = 8;
3905             break;
3906
3907         case RPC_FC_IGNORE:
3908         case RPC_FC_BIND_PRIMITIVE:
3909             /* no marshalling needed */
3910             return;
3911
3912         default:
3913             error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3914                   var->name, get_basic_fc(ref));
3915         }
3916
3917         if (phase == PHASE_MARSHAL && alignment > 1)
3918             print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3919         print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3920                     alignment - 1, alignment - 1);
3921
3922         if (phase == PHASE_MARSHAL)
3923         {
3924             print_file(file, indent, "*(");
3925             write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3926             if (is_ptr(type))
3927                 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3928             else
3929                 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3930             fprintf(file, "%s%s", local_var_prefix, varname);
3931             fprintf(file, ";\n");
3932         }
3933         else if (phase == PHASE_UNMARSHAL)
3934         {
3935             print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3936             write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3937             fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3938             print_file(file, indent, "{\n");
3939             print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3940             print_file(file, indent, "}\n");
3941             print_file(file, indent, "%s%s%s",
3942                        (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3943                        local_var_prefix, varname);
3944             if (pass == PASS_IN && is_ptr(type))
3945                 fprintf(file, " = (");
3946             else
3947                 fprintf(file, " = *(");
3948             write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3949             fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3950         }
3951
3952         print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3953         write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3954         fprintf(file, ");\n");
3955     }
3956 }
3957
3958 /* returns whether the MaxCount, Offset or ActualCount members need to be
3959  * filled in for the specified phase */
3960 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3961 {
3962     return (phase != PHASE_UNMARSHAL);
3963 }
3964
3965 expr_t *get_size_is_expr(const type_t *t, const char *name)
3966 {
3967     expr_t *x = NULL;
3968
3969     for ( ; is_array(t); t = type_array_get_element(t))
3970         if (type_array_has_conformance(t) &&
3971             type_array_get_conformance(t)->type != EXPR_VOID)
3972         {
3973             if (!x)
3974                 x = type_array_get_conformance(t);
3975             else
3976                 error("%s: multidimensional conformant"
3977                       " arrays not supported at the top level\n",
3978                       name);
3979         }
3980
3981     return x;
3982 }
3983
3984 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3985                                               enum remoting_phase phase, const var_t *var)
3986 {
3987     const type_t *type = var->type;
3988     /* get fundamental type for the argument */
3989     for (;;)
3990     {
3991         switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3992         {
3993         case TGT_ARRAY:
3994             if (is_conformance_needed_for_phase(phase))
3995             {
3996                 if (type_array_has_conformance(type) &&
3997                     type_array_get_conformance(type)->type != EXPR_VOID)
3998                 {
3999                     print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4000                     write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4001                     fprintf(file, ";\n\n");
4002                 }
4003                 if (type_array_has_variance(type))
4004                 {
4005                     print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4006                     print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4007                     write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4008                     fprintf(file, ";\n\n");
4009                 }
4010             }
4011             break;
4012         case TGT_UNION:
4013             if (type_get_type(type) == TYPE_UNION &&
4014                 is_conformance_needed_for_phase(phase))
4015             {
4016                 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4017                 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4018                 fprintf(file, ";\n\n");
4019             }
4020             break;
4021         case TGT_IFACE_POINTER:
4022         {
4023             expr_t *iid;
4024
4025             if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4026             {
4027                 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4028                 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4029                 fprintf( file, ";\n\n" );
4030             }
4031             break;
4032         }
4033         case TGT_POINTER:
4034             type = type_pointer_get_ref(type);
4035             continue;
4036         case TGT_INVALID:
4037         case TGT_USER_TYPE:
4038         case TGT_CTXT_HANDLE:
4039         case TGT_CTXT_HANDLE_POINTER:
4040         case TGT_STRING:
4041         case TGT_BASIC:
4042         case TGT_ENUM:
4043         case TGT_STRUCT:
4044         case TGT_RANGE:
4045             break;
4046         }
4047         break;
4048     }
4049 }
4050
4051 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4052                                enum pass pass, enum remoting_phase phase, const var_t *var)
4053 {
4054     int in_attr, out_attr, pointer_type;
4055     const char *type_str = NULL;
4056     const type_t *type = var->type;
4057     unsigned int alignment, start_offset = type->typestring_offset;
4058
4059     if (is_ptr(type) || is_array(type))
4060         pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4061     else
4062         pointer_type = 0;
4063
4064     in_attr = is_attr(var->attrs, ATTR_IN);
4065     out_attr = is_attr(var->attrs, ATTR_OUT);
4066     if (!in_attr && !out_attr)
4067         in_attr = 1;
4068
4069     if (phase != PHASE_FREE)
4070         switch (pass)
4071         {
4072         case PASS_IN:
4073             if (!in_attr) return;
4074             break;
4075         case PASS_OUT:
4076             if (!out_attr) return;
4077             break;
4078         case PASS_RETURN:
4079             break;
4080         }
4081
4082     if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4083
4084     write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
4085
4086     switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4087     {
4088     case TGT_CTXT_HANDLE:
4089     case TGT_CTXT_HANDLE_POINTER:
4090         if (phase == PHASE_MARSHAL)
4091         {
4092             if (pass == PASS_IN)
4093             {
4094                 /* if the context_handle attribute appears in the chain of types
4095                  * without pointers being followed, then the context handle must
4096                  * be direct, otherwise it is a pointer */
4097                 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
4098                 print_file(file, indent, "NdrClientContextMarshall(\n");
4099                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4100                 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
4101                 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4102             }
4103             else
4104             {
4105                 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4106                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4107                 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4108                 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4109                 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4110             }
4111         }
4112         else if (phase == PHASE_UNMARSHAL)
4113         {
4114             if (pass == PASS_OUT)
4115             {
4116                 if (!in_attr)
4117                     print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4118                 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4119                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4120                 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4121                 print_file(file, indent + 1, "__frame->_Handle);\n");
4122             }
4123             else
4124             {
4125                 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4126                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4127                 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4128             }
4129         }
4130         break;
4131     case TGT_USER_TYPE:
4132         print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4133         break;
4134     case TGT_STRING:
4135         if (phase == PHASE_FREE || pass == PASS_RETURN ||
4136             pointer_type != RPC_FC_RP)
4137         {
4138             if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
4139                 !in_attr && is_conformant_array(type))
4140             {
4141                 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4142                 indent++;
4143                 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4144             }
4145             /* strings returned are assumed to be global and hence don't
4146              * need freeing */
4147             else if (is_declptr(type) &&
4148                      !(phase == PHASE_FREE && pass == PASS_RETURN))
4149                 print_phase_function(file, indent, "Pointer", local_var_prefix,
4150                                      phase, var, start_offset);
4151         }
4152         else
4153         {
4154             unsigned int real_start_offset = start_offset;
4155             /* skip over pointer description straight to string description */
4156             if (is_declptr(type))
4157             {
4158                 if (is_conformant_array(type))
4159                     real_start_offset += 4;
4160                 else
4161                     real_start_offset += 2;
4162             }
4163             if (is_array(type) && !is_conformant_array(type))
4164                 print_phase_function(file, indent, "NonConformantString",
4165                                      local_var_prefix, phase, var,
4166                                      real_start_offset);
4167             else
4168                 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4169                                      phase, var, real_start_offset);
4170         }
4171         break;
4172     case TGT_ARRAY:
4173     {
4174         unsigned char tc = get_array_fc(type);
4175         const char *array_type = NULL;
4176
4177         /* We already have the size_is expression since it's at the
4178            top level, but do checks for multidimensional conformant
4179            arrays.  When we handle them, we'll need to extend this
4180            function to return a list, and then we'll actually use
4181            the return value.  */
4182         get_size_is_expr(type, var->name);
4183
4184         switch (tc)
4185         {
4186         case RPC_FC_SMFARRAY:
4187         case RPC_FC_LGFARRAY:
4188             array_type = "FixedArray";
4189             break;
4190         case RPC_FC_SMVARRAY:
4191         case RPC_FC_LGVARRAY:
4192             array_type = "VaryingArray";
4193             break;
4194         case RPC_FC_CARRAY:
4195             array_type = "ConformantArray";
4196             break;
4197         case RPC_FC_CVARRAY:
4198             array_type = "ConformantVaryingArray";
4199             break;
4200         case RPC_FC_BOGUS_ARRAY:
4201             array_type = "ComplexArray";
4202             break;
4203         }
4204
4205         if (pointer_type != RPC_FC_RP) array_type = "Pointer";
4206         print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4207         if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
4208         {
4209             /* these are all unmarshalled by allocating memory */
4210             if (tc == RPC_FC_BOGUS_ARRAY ||
4211                 tc == RPC_FC_CVARRAY ||
4212                 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
4213                 (tc == RPC_FC_CARRAY && !in_attr))
4214             {
4215                 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4216                 indent++;
4217                 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4218             }
4219         }
4220         break;
4221     }
4222     case TGT_BASIC:
4223         print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4224         break;
4225     case TGT_ENUM:
4226         print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4227         break;
4228     case TGT_RANGE:
4229         print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4230         /* Note: this goes beyond what MIDL does - it only supports arguments
4231          * with the [range] attribute in Oicf mode */
4232         if (phase == PHASE_UNMARSHAL)
4233         {
4234             const expr_t *range_min;
4235             const expr_t *range_max;
4236             expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4237             if (!range_list)
4238                 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4239             range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4240             range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4241
4242             print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4243             write_type_decl(file, var->type, NULL);
4244             fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4245             write_type_decl(file, var->type, NULL);
4246             fprintf(file, ")0x%x))\n", range_max->cval);
4247             print_file(file, indent, "{\n");
4248             print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4249             print_file(file, indent, "}\n");
4250         }
4251         break;
4252     case TGT_STRUCT:
4253         switch (get_struct_fc(type))
4254         {
4255         case RPC_FC_STRUCT:
4256             if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4257                 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4258             break;
4259         case RPC_FC_PSTRUCT:
4260             print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4261             break;
4262         case RPC_FC_CSTRUCT:
4263         case RPC_FC_CPSTRUCT:
4264             print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4265             break;
4266         case RPC_FC_CVSTRUCT:
4267             print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4268             break;
4269         case RPC_FC_BOGUS_STRUCT:
4270             print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4271             break;
4272         default:
4273             error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4274         }
4275         break;
4276     case TGT_UNION:
4277     {
4278         const char *union_type = NULL;
4279
4280         if (type_get_type(type) == TYPE_UNION)
4281             union_type = "NonEncapsulatedUnion";
4282         else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4283             union_type = "EncapsulatedUnion";
4284
4285         print_phase_function(file, indent, union_type, local_var_prefix,
4286                              phase, var, start_offset);
4287         break;
4288     }
4289     case TGT_POINTER:
4290     {
4291         const type_t *ref = type_pointer_get_ref(type);
4292         if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4293         {
4294         case TGT_BASIC:
4295             print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4296             break;
4297         case TGT_ENUM:
4298             /* base types have known sizes, so don't need a sizing pass
4299              * and don't have any memory to free and so don't need a
4300              * freeing pass */
4301             if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4302                 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4303             break;
4304         case TGT_STRUCT:
4305             switch (get_struct_fc(ref))
4306             {
4307             case RPC_FC_STRUCT:
4308                 /* simple structs have known sizes, so don't need a sizing
4309                  * pass and don't have any memory to free and so don't
4310                  * need a freeing pass */
4311                 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4312                     type_str = "SimpleStruct";
4313                 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4314                 {
4315                     print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4316                     indent++;
4317                     print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4318                     indent--;
4319                 }
4320                 break;
4321             case RPC_FC_PSTRUCT:
4322                 type_str = "SimpleStruct";
4323                 break;
4324             case RPC_FC_CSTRUCT:
4325             case RPC_FC_CPSTRUCT:
4326                 type_str = "ConformantStruct";
4327                 break;
4328             case RPC_FC_CVSTRUCT:
4329                 type_str = "ConformantVaryingStruct";
4330                 break;
4331             case RPC_FC_BOGUS_STRUCT:
4332                 type_str = "ComplexStruct";
4333                 break;
4334             default:
4335                 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4336             }
4337
4338             if (type_str)
4339             {
4340                 if (phase == PHASE_FREE)
4341                     type_str = "Pointer";
4342                 else
4343                     start_offset = ref->typestring_offset;
4344                 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4345             }
4346             break;
4347         case TGT_UNION:
4348             if (phase == PHASE_FREE)
4349                 type_str = "Pointer";
4350             else
4351             {
4352                 if (type_get_type(ref) == TYPE_UNION)
4353                     type_str = "NonEncapsulatedUnion";
4354                 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4355                     type_str = "EncapsulatedUnion";
4356
4357                 start_offset = ref->typestring_offset;
4358             }
4359
4360             print_phase_function(file, indent, type_str, local_var_prefix,
4361                                  phase, var, start_offset);
4362             break;
4363         case TGT_USER_TYPE:
4364             if (phase != PHASE_FREE)
4365             {
4366                 type_str = "UserMarshal";
4367                 start_offset = ref->typestring_offset;
4368             }
4369             else type_str = "Pointer";
4370
4371             print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4372             break;
4373         case TGT_STRING:
4374         case TGT_POINTER:
4375         case TGT_ARRAY:
4376         case TGT_RANGE:
4377         case TGT_IFACE_POINTER:
4378         case TGT_CTXT_HANDLE:
4379         case TGT_CTXT_HANDLE_POINTER:
4380             print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4381             break;
4382         case TGT_INVALID:
4383             assert(0);
4384             break;
4385         }
4386         else
4387             print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4388         break;
4389     }
4390     case TGT_IFACE_POINTER:
4391         print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4392         break;
4393     case TGT_INVALID:
4394         assert(0);
4395         break;
4396     }
4397     fprintf(file, "\n");
4398 }
4399
4400 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4401                               enum pass pass, enum remoting_phase phase)
4402 {
4403     if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4404     {
4405         unsigned int size = get_function_buffer_size( func, pass );
4406         print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4407     }
4408
4409     if (pass == PASS_RETURN)
4410     {
4411         var_t var;
4412         var = *func;
4413         var.type = type_function_get_rettype(func->type);
4414         var.name = xstrdup( "_RetVal" );
4415         write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
4416         free( var.name );
4417     }
4418     else
4419     {
4420         const var_t *var;
4421         if (!type_get_function_args(func->type))
4422             return;
4423         LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4424             write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4425     }
4426 }
4427
4428
4429 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4430 {
4431     unsigned int offset = 0;
4432     write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4433     return offset;
4434 }
4435
4436 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4437 {
4438     const statement_t *stmt;
4439     unsigned int size = 1;
4440
4441     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4442     {
4443         const type_t *iface;
4444         const statement_t *stmt_func;
4445
4446         if (stmt->type == STMT_LIBRARY)
4447         {
4448             size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
4449             continue;
4450         }
4451         else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4452             continue;
4453
4454         iface = stmt->u.type;
4455         if (!pred(iface))
4456             continue;
4457
4458         STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4459         {
4460             const var_t *func = stmt_func->u.var;
4461             if (!is_local(func->attrs))
4462                 size += get_size_procformatstring_func( iface, func );
4463         }
4464     }
4465     return size;
4466 }
4467
4468 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4469 {
4470     set_all_tfswrite(FALSE);
4471     return process_tfs(NULL, stmts, pred);
4472 }
4473
4474 void declare_stub_args( FILE *file, int indent, const var_t *func )
4475 {
4476     int in_attr, out_attr;
4477     int i = 0;
4478     const var_t *var;
4479
4480     /* declare return value '_RetVal' */
4481     if (!is_void(type_function_get_rettype(func->type)))
4482     {
4483         print_file(file, indent, "%s", "");
4484         write_type_decl_left(file, type_function_get_rettype(func->type));
4485         fprintf(file, " _RetVal;\n");
4486     }
4487
4488     if (!type_get_function_args(func->type))
4489         return;
4490
4491     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4492     {
4493         in_attr = is_attr(var->attrs, ATTR_IN);
4494         out_attr = is_attr(var->attrs, ATTR_OUT);
4495         if (!out_attr && !in_attr)
4496             in_attr = 1;
4497
4498         if (is_context_handle(var->type))
4499             print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4500         else
4501         {
4502             if (!in_attr && !is_conformant_array(var->type))
4503             {
4504                 type_t *type_to_print;
4505                 char name[16];
4506                 print_file(file, indent, "%s", "");
4507                 if (type_get_type(var->type) == TYPE_ARRAY &&
4508                     !type_array_is_decl_as_ptr(var->type))
4509                     type_to_print = var->type;
4510                 else
4511                     type_to_print = type_pointer_get_ref(var->type);
4512                 sprintf(name, "_W%u", i++);
4513                 write_type_decl(file, type_to_print, name);
4514                 fprintf(file, ";\n");
4515             }
4516
4517             print_file(file, indent, "%s", "");
4518             write_type_decl_left(file, var->type);
4519             fprintf(file, " ");
4520             if (type_get_type(var->type) == TYPE_ARRAY &&
4521                 !type_array_is_decl_as_ptr(var->type)) {
4522                 fprintf(file, "(*%s)", var->name);
4523             } else
4524                 fprintf(file, "%s", var->name);
4525             write_type_right(file, var->type, FALSE);
4526             fprintf(file, ";\n");
4527
4528             if (decl_indirect(var->type))
4529                 print_file(file, indent, "void *_p_%s;\n", var->name);
4530         }
4531     }
4532 }
4533
4534
4535 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4536 {
4537     int in_attr, out_attr;
4538     int i = 0, sep = 0;
4539     const var_t *var;
4540
4541     if (!type_get_function_args(func->type))
4542         return;
4543
4544     LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4545     {
4546         in_attr = is_attr(var->attrs, ATTR_IN);
4547         out_attr = is_attr(var->attrs, ATTR_OUT);
4548         if (!out_attr && !in_attr)
4549             in_attr = 1;
4550
4551         if (!in_attr)
4552         {
4553             print_file(file, indent, "%s%s", local_var_prefix, var->name);
4554
4555             switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4556             {
4557             case TGT_CTXT_HANDLE_POINTER:
4558                 fprintf(file, " = NdrContextHandleInitialize(\n");
4559                 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4560                 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4561                            var->type->typestring_offset);
4562                 break;
4563             case TGT_ARRAY:
4564                 if (type_array_has_conformance(var->type))
4565                 {
4566                     unsigned int size;
4567                     type_t *type;
4568
4569                     fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4570                     for (type = var->type;
4571                          is_array(type) && type_array_has_conformance(type);
4572                          type = type_array_get_element(type))
4573                     {
4574                         write_expr(file, type_array_get_conformance(type), TRUE,
4575                                    TRUE, NULL, NULL, local_var_prefix);
4576                         fprintf(file, " * ");
4577                     }
4578                     size = type_memsize(type);
4579                     fprintf(file, "%u);\n", size);
4580
4581                     print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4582                     for (type = var->type;
4583                          is_array(type) && type_array_has_conformance(type);
4584                          type = type_array_get_element(type))
4585                     {
4586                         write_expr(file, type_array_get_conformance(type), TRUE,
4587                                    TRUE, NULL, NULL, local_var_prefix);
4588                         fprintf(file, " * ");
4589                     }
4590                     size = type_memsize(type);
4591                     fprintf(file, "%u);\n", size);
4592                 }
4593                 else
4594                     fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4595                 break;
4596             case TGT_POINTER:
4597                 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4598                 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
4599                 {
4600                 case TGT_BASIC:
4601                 case TGT_ENUM:
4602                 case TGT_POINTER:
4603                 case TGT_RANGE:
4604                 case TGT_IFACE_POINTER:
4605                     print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4606                     break;
4607                 case TGT_USER_TYPE:
4608                     print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4609                                local_var_prefix, i, local_var_prefix, i);
4610                     break;
4611                 case TGT_STRUCT:
4612                 case TGT_UNION:
4613                 case TGT_ARRAY:
4614                 case TGT_CTXT_HANDLE:
4615                 case TGT_CTXT_HANDLE_POINTER:
4616                 case TGT_INVALID:
4617                 case TGT_STRING:
4618                     /* not initialised */
4619                     break;
4620                 }
4621                 i++;
4622                 break;
4623             default:
4624                 break;
4625             }
4626
4627             sep = 1;
4628         }
4629     }
4630     if (sep)
4631         fprintf(file, "\n");
4632 }
4633
4634
4635 int write_expr_eval_routines(FILE *file, const char *iface)
4636 {
4637     static const char *var_name = "pS";
4638     static const char *var_name_expr = "pS->";
4639     int result = 0;
4640     struct expr_eval_routine *eval;
4641     unsigned short callback_offset = 0;
4642
4643     LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4644     {
4645         const char *name = eval->name;
4646         result = 1;
4647
4648         print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4649                    iface, name, callback_offset);
4650         print_file(file, 0, "{\n");
4651         if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4652         {
4653             const var_list_t *args = type_get_function_args( eval->cont_type );
4654             const var_t *arg;
4655
4656             print_file(file, 1, "struct _PARAM_STRUCT\n" );
4657             print_file(file, 1, "{\n" );
4658             if (is_object( eval->iface )) print_file(file, 2, "%s *This;\n", eval->iface->name );
4659
4660             if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4661             {
4662                 print_file(file, 2, "%s", "");
4663                 write_type_left( file, (type_t *)arg->type, TRUE );
4664                 if (needs_space_after( arg->type )) fputc( ' ', file );
4665                 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4666                 /* FIXME: should check for large args being passed by pointer */
4667                 if (is_array( arg->type ) || is_ptr( arg->type ) || type_memsize( arg->type ) == pointer_size)
4668                     fprintf( file, "%s;\n", arg->name );
4669                 else
4670                     fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4671             }
4672             print_file(file, 1, "} *pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop;\n" );
4673         }
4674         else
4675         {
4676             print_file(file, 1, "%s", "");
4677             write_type_left(file, (type_t *)eval->cont_type, TRUE);
4678             fprintf(file, " *%s = (", var_name);
4679             write_type_left(file, (type_t *)eval->cont_type, TRUE);
4680             fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4681         }
4682         print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4683         print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4684         write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4685         fprintf(file, ";\n");
4686         print_file(file, 0, "}\n\n");
4687         callback_offset++;
4688     }
4689     return result;
4690 }
4691
4692 void write_expr_eval_routine_list(FILE *file, const char *iface)
4693 {
4694     struct expr_eval_routine *eval;
4695     struct expr_eval_routine *cursor;
4696     unsigned short callback_offset = 0;
4697
4698     fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4699     fprintf(file, "{\n");
4700
4701     LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4702     {
4703         print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, eval->name, callback_offset);
4704         callback_offset++;
4705         list_remove(&eval->entry);
4706         free(eval->name);
4707         free(eval);
4708     }
4709
4710     fprintf(file, "};\n\n");
4711 }
4712
4713 void write_user_quad_list(FILE *file)
4714 {
4715     user_type_t *ut;
4716
4717     if (list_empty(&user_type_list))
4718         return;
4719
4720     fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4721     fprintf(file, "{\n");
4722     LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4723     {
4724         const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4725         print_file(file, 1, "{\n");
4726         print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4727         print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4728         print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4729         print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4730         print_file(file, 1, "}%s\n", sep);
4731     }
4732     fprintf(file, "};\n\n");
4733 }
4734
4735 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4736 {
4737     const struct str_list_entry_t *endpoint;
4738     const char *p;
4739
4740     /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4741     print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4742     LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4743     {
4744         print_file( f, 1, "{ (const unsigned char *)\"" );
4745         for (p = endpoint->str; *p && *p != ':'; p++)
4746         {
4747             if (*p == '"' || *p == '\\') fputc( '\\', f );
4748             fputc( *p, f );
4749         }
4750         if (!*p) goto error;
4751         if (p[1] != '[') goto error;
4752
4753         fprintf( f, "\", (const unsigned char *)\"" );
4754         for (p += 2; *p && *p != ']'; p++)
4755         {
4756             if (*p == '"' || *p == '\\') fputc( '\\', f );
4757             fputc( *p, f );
4758         }
4759         if (*p != ']') goto error;
4760         fprintf( f, "\" },\n" );
4761     }
4762     print_file( f, 0, "};\n\n" );
4763     return;
4764
4765 error:
4766     error("Invalid endpoint syntax '%s'\n", endpoint->str);
4767 }
4768
4769 void write_exceptions( FILE *file )
4770 {
4771     fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4772     fprintf( file, "\n");
4773     fprintf( file, "#include \"wine/exception.h\"\n");
4774     fprintf( file, "#undef RpcTryExcept\n");
4775     fprintf( file, "#undef RpcExcept\n");
4776     fprintf( file, "#undef RpcEndExcept\n");
4777     fprintf( file, "#undef RpcTryFinally\n");
4778     fprintf( file, "#undef RpcFinally\n");
4779     fprintf( file, "#undef RpcEndFinally\n");
4780     fprintf( file, "#undef RpcExceptionCode\n");
4781     fprintf( file, "#undef RpcAbnormalTermination\n");
4782     fprintf( file, "\n");
4783     fprintf( file, "struct __exception_frame;\n");
4784     fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4785     fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4786     fprintf( file, "\n");
4787     fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4788     fprintf( file, "    EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4789     fprintf( file, "    __filter_func                 filter; \\\n");
4790     fprintf( file, "    __finally_func                finally; \\\n");
4791     fprintf( file, "    sigjmp_buf                    jmp; \\\n");
4792     fprintf( file, "    DWORD                         code; \\\n");
4793     fprintf( file, "    unsigned char                 abnormal_termination; \\\n");
4794     fprintf( file, "    unsigned char                 filter_level; \\\n");
4795     fprintf( file, "    unsigned char                 finally_level;\n");
4796     fprintf( file, "\n");
4797     fprintf( file, "struct __exception_frame\n{\n");
4798     fprintf( file, "    __DECL_EXCEPTION_FRAME\n");
4799     fprintf( file, "};\n");
4800     fprintf( file, "\n");
4801     fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4802     fprintf( file, "{\n");
4803     fprintf( file, "    struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4804     fprintf( file, "    if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4805     fprintf( file, "    {\n");
4806     fprintf( file, "        exc_frame->abnormal_termination = 1;\n");
4807     fprintf( file, "        exc_frame->finally( exc_frame );\n");
4808     fprintf( file, "        __wine_pop_frame( &exc_frame->frame );\n");
4809     fprintf( file, "    }\n");
4810     fprintf( file, "    exc_frame->filter_level = 0;\n");
4811     fprintf( file, "    siglongjmp( exc_frame->jmp, 1 );\n");
4812     fprintf( file, "}\n");
4813     fprintf( file, "\n");
4814     fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4815     fprintf( file, "                                       EXCEPTION_REGISTRATION_RECORD *frame,\n");
4816     fprintf( file, "                                       CONTEXT *context,\n");
4817     fprintf( file, "                                       EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4818     fprintf( file, "{\n");
4819     fprintf( file, "    struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4820     fprintf( file, "\n");
4821     fprintf( file, "    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4822     fprintf( file, "    {\n" );
4823     fprintf( file, "        if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4824     fprintf( file, "        {\n" );
4825     fprintf( file, "            exc_frame->abnormal_termination = 1;\n");
4826     fprintf( file, "            exc_frame->finally( exc_frame );\n");
4827     fprintf( file, "        }\n" );
4828     fprintf( file, "        return ExceptionContinueSearch;\n");
4829     fprintf( file, "    }\n" );
4830     fprintf( file, "    exc_frame->code = record->ExceptionCode;\n");
4831     fprintf( file, "    if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4832     fprintf( file, "        __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4833     fprintf( file, "    return ExceptionContinueSearch;\n");
4834     fprintf( file, "}\n");
4835     fprintf( file, "\n");
4836     fprintf( file, "#define RpcTryExcept \\\n");
4837     fprintf( file, "    if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4838     fprintf( file, "    { \\\n");
4839     fprintf( file, "        if (!__frame->finally_level) \\\n" );
4840     fprintf( file, "            __wine_push_frame( &__frame->frame ); \\\n");
4841     fprintf( file, "        __frame->filter_level = __frame->finally_level + 1;\n" );
4842     fprintf( file, "\n");
4843     fprintf( file, "#define RpcExcept(expr) \\\n");
4844     fprintf( file, "        if (!__frame->finally_level) \\\n" );
4845     fprintf( file, "            __wine_pop_frame( &__frame->frame ); \\\n");
4846     fprintf( file, "        __frame->filter_level = 0; \\\n" );
4847     fprintf( file, "    } \\\n");
4848     fprintf( file, "    else \\\n");
4849     fprintf( file, "\n");
4850     fprintf( file, "#define RpcEndExcept\n");
4851     fprintf( file, "\n");
4852     fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4853     fprintf( file, "\n");
4854     fprintf( file, "#define RpcTryFinally \\\n");
4855     fprintf( file, "    if (!__frame->filter_level) \\\n");
4856     fprintf( file, "        __wine_push_frame( &__frame->frame ); \\\n");
4857     fprintf( file, "    __frame->finally_level = __frame->filter_level + 1;\n");
4858     fprintf( file, "\n");
4859     fprintf( file, "#define RpcFinally \\\n");
4860     fprintf( file, "    if (!__frame->filter_level) \\\n");
4861     fprintf( file, "        __wine_pop_frame( &__frame->frame ); \\\n");
4862     fprintf( file, "    __frame->finally_level = 0;\n");
4863     fprintf( file, "\n");
4864     fprintf( file, "#define RpcEndFinally\n");
4865     fprintf( file, "\n");
4866     fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4867     fprintf( file, "\n");
4868     fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4869     fprintf( file, "    do { \\\n");
4870     fprintf( file, "        __frame->frame.Handler = __widl_exception_handler; \\\n");
4871     fprintf( file, "        __frame->filter = (__filter_func)(filter_func); \\\n" );
4872     fprintf( file, "        __frame->finally = (__finally_func)(finally_func); \\\n");
4873     fprintf( file, "        __frame->abnormal_termination = 0; \\\n");
4874     fprintf( file, "        __frame->filter_level = 0; \\\n");
4875     fprintf( file, "        __frame->finally_level = 0; \\\n");
4876     fprintf( file, "    } while (0)\n");
4877     fprintf( file, "\n");
4878     fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4879     fprintf( file, "\n");
4880     fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4881     fprintf( file, "    do { (void)(filter_func); } while(0)\n");
4882     fprintf( file, "\n");
4883     fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4884     fprintf( file, "    DWORD code;\n");
4885     fprintf( file, "\n");
4886     fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");
4887 }