widl: Skip the existing correlation descriptor on unencapsulated unions when generati...
[wine/wine-gecko.git] / tools / widl / typegen.c
blob861dada6f64bf597e0ecfa08010c10a5433d00c6
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
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>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
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)))
49 static const var_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *structure;
58 unsigned int baseoff;
59 const expr_t *expr;
62 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
63 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65 const char *name, int write_ptr, unsigned int *tfsoff);
66 static const var_t *find_array_or_string_in_struct(const type_t *type);
67 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
68 type_t *type, int toplevel_param,
69 const char *name, unsigned int *typestring_offset);
71 const char *string_of_type(unsigned char type)
73 switch (type)
75 case RPC_FC_BYTE: return "FC_BYTE";
76 case RPC_FC_CHAR: return "FC_CHAR";
77 case RPC_FC_SMALL: return "FC_SMALL";
78 case RPC_FC_USMALL: return "FC_USMALL";
79 case RPC_FC_WCHAR: return "FC_WCHAR";
80 case RPC_FC_SHORT: return "FC_SHORT";
81 case RPC_FC_USHORT: return "FC_USHORT";
82 case RPC_FC_LONG: return "FC_LONG";
83 case RPC_FC_ULONG: return "FC_ULONG";
84 case RPC_FC_FLOAT: return "FC_FLOAT";
85 case RPC_FC_HYPER: return "FC_HYPER";
86 case RPC_FC_DOUBLE: return "FC_DOUBLE";
87 case RPC_FC_ENUM16: return "FC_ENUM16";
88 case RPC_FC_ENUM32: return "FC_ENUM32";
89 case RPC_FC_IGNORE: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP: return "FC_RP";
92 case RPC_FC_UP: return "FC_UP";
93 case RPC_FC_OP: return "FC_OP";
94 case RPC_FC_FP: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY: return "FC_CARRAY";
108 case RPC_FC_CVARRAY: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112 case RPC_FC_POINTER: return "FC_POINTER";
113 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING: return "FC_CSTRING";
116 case RPC_FC_WSTRING: return "FC_WSTRING";
117 default:
118 error("string_of_type: unknown type 0x%02x\n", type);
119 return NULL;
123 unsigned char get_basic_fc(const type_t *type)
125 int sign = type_basic_get_sign(type);
126 switch (type_basic_get_type(type))
128 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
129 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
130 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
131 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
132 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
133 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
134 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
135 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
136 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
137 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
138 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
139 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
140 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
141 default: return 0;
145 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
147 const type_t *t;
148 int pointer_type;
150 assert(is_ptr(type) || is_array(type));
152 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
153 if (pointer_type)
154 return pointer_type;
156 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
158 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
159 if (pointer_type)
160 return pointer_type;
163 if (toplevel_param)
164 return RPC_FC_RP;
165 else if (is_ptr(type))
166 return type_pointer_get_default_fc(type);
167 else
168 return type_array_get_ptr_default_fc(type);
171 static unsigned char get_enum_fc(const type_t *type)
173 assert(type_get_type(type) == TYPE_ENUM);
174 if (is_aliaschain_attr(type, ATTR_V1ENUM))
175 return RPC_FC_ENUM32;
176 else
177 return RPC_FC_ENUM16;
180 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
182 if (is_user_type(type))
183 return TGT_USER_TYPE;
185 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
186 return TGT_CTXT_HANDLE;
188 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
189 return TGT_STRING;
191 switch (type_get_type(type))
193 case TYPE_BASIC:
194 return TGT_BASIC;
195 case TYPE_ENUM:
196 return TGT_ENUM;
197 case TYPE_POINTER:
198 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
199 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
200 return TGT_IFACE_POINTER;
201 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
202 return TGT_CTXT_HANDLE_POINTER;
203 else
204 return TGT_POINTER;
205 case TYPE_STRUCT:
206 return TGT_STRUCT;
207 case TYPE_ENCAPSULATED_UNION:
208 case TYPE_UNION:
209 return TGT_UNION;
210 case TYPE_ARRAY:
211 return TGT_ARRAY;
212 case TYPE_FUNCTION:
213 case TYPE_COCLASS:
214 case TYPE_INTERFACE:
215 case TYPE_MODULE:
216 case TYPE_VOID:
217 case TYPE_ALIAS:
218 break;
220 return TGT_INVALID;
223 unsigned char get_struct_fc(const type_t *type)
225 int has_pointer = 0;
226 int has_conformance = 0;
227 int has_variance = 0;
228 var_t *field;
229 var_list_t *fields;
231 fields = type_struct_get_fields(type);
233 if (get_padding(fields))
234 return RPC_FC_BOGUS_STRUCT;
236 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
238 type_t *t = field->type;
239 enum typegen_type typegen_type;
241 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
243 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
245 if (is_string_type(field->attrs, field->type))
247 if (is_conformant_array(t))
248 has_conformance = 1;
249 has_variance = 1;
250 continue;
253 if (is_array(type_array_get_element(field->type)))
254 return RPC_FC_BOGUS_STRUCT;
256 if (type_array_has_conformance(field->type))
258 has_conformance = 1;
259 if (list_next(fields, &field->entry))
260 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
261 field->name);
263 if (type_array_has_variance(t))
264 has_variance = 1;
266 t = type_array_get_element(t);
267 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
270 switch (typegen_type)
272 case TGT_USER_TYPE:
273 case TGT_IFACE_POINTER:
274 return RPC_FC_BOGUS_STRUCT;
275 case TGT_BASIC:
276 break;
277 case TGT_ENUM:
278 if (get_enum_fc(t) == RPC_FC_ENUM16)
279 return RPC_FC_BOGUS_STRUCT;
280 break;
281 case TGT_POINTER:
282 case TGT_ARRAY:
283 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
284 return RPC_FC_BOGUS_STRUCT;
285 has_pointer = 1;
286 break;
287 case TGT_UNION:
288 return RPC_FC_BOGUS_STRUCT;
289 case TGT_STRUCT:
291 unsigned char fc = get_struct_fc(t);
292 switch (fc)
294 case RPC_FC_STRUCT:
295 break;
296 case RPC_FC_CVSTRUCT:
297 has_conformance = 1;
298 has_variance = 1;
299 has_pointer = 1;
300 break;
302 case RPC_FC_CPSTRUCT:
303 has_conformance = 1;
304 if (list_next( fields, &field->entry ))
305 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
306 field->name);
307 has_pointer = 1;
308 break;
310 case RPC_FC_CSTRUCT:
311 has_conformance = 1;
312 if (list_next( fields, &field->entry ))
313 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
314 field->name);
315 break;
317 case RPC_FC_PSTRUCT:
318 has_pointer = 1;
319 break;
321 default:
322 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
323 /* fallthru - treat it as complex */
325 /* as soon as we see one of these these members, it's bogus... */
326 case RPC_FC_BOGUS_STRUCT:
327 return RPC_FC_BOGUS_STRUCT;
329 break;
331 case TGT_STRING:
332 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
333 case TGT_INVALID:
334 case TGT_CTXT_HANDLE:
335 case TGT_CTXT_HANDLE_POINTER:
336 /* checking after parsing should mean that we don't get here. if we do,
337 * it's a checker bug */
338 assert(0);
342 if( has_variance )
344 if ( has_conformance )
345 return RPC_FC_CVSTRUCT;
346 else
347 return RPC_FC_BOGUS_STRUCT;
349 if( has_conformance && has_pointer )
350 return RPC_FC_CPSTRUCT;
351 if( has_conformance )
352 return RPC_FC_CSTRUCT;
353 if( has_pointer )
354 return RPC_FC_PSTRUCT;
355 return RPC_FC_STRUCT;
358 unsigned char get_array_fc(const type_t *type)
360 unsigned char fc;
361 const expr_t *size_is;
362 const type_t *elem_type;
364 elem_type = type_array_get_element(type);
365 size_is = type_array_get_conformance(type);
367 if (!size_is)
369 unsigned int align = 0;
370 unsigned int size = type_memsize(elem_type, &align);
371 if (size * type_array_get_dim(type) > 0xffffuL)
372 fc = RPC_FC_LGFARRAY;
373 else
374 fc = RPC_FC_SMFARRAY;
376 else
377 fc = RPC_FC_CARRAY;
379 if (type_array_has_variance(type))
381 if (fc == RPC_FC_SMFARRAY)
382 fc = RPC_FC_SMVARRAY;
383 else if (fc == RPC_FC_LGFARRAY)
384 fc = RPC_FC_LGVARRAY;
385 else if (fc == RPC_FC_CARRAY)
386 fc = RPC_FC_CVARRAY;
389 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
391 case TGT_USER_TYPE:
392 fc = RPC_FC_BOGUS_ARRAY;
393 break;
394 case TGT_STRUCT:
395 switch (get_struct_fc(elem_type))
397 case RPC_FC_BOGUS_STRUCT:
398 fc = RPC_FC_BOGUS_ARRAY;
399 break;
401 break;
402 case TGT_ENUM:
403 /* is 16-bit enum - if so, wire size differs from mem size and so
404 * the array cannot be block copied, which means the array is complex */
405 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
406 fc = RPC_FC_BOGUS_ARRAY;
407 break;
408 case TGT_UNION:
409 case TGT_IFACE_POINTER:
410 fc = RPC_FC_BOGUS_ARRAY;
411 break;
412 case TGT_POINTER:
413 /* ref pointers cannot just be block copied. unique pointers to
414 * interfaces need special treatment. either case means the array is
415 * complex */
416 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP)
417 fc = RPC_FC_BOGUS_ARRAY;
418 break;
419 case TGT_BASIC:
420 case TGT_CTXT_HANDLE:
421 case TGT_CTXT_HANDLE_POINTER:
422 case TGT_STRING:
423 case TGT_INVALID:
424 case TGT_ARRAY:
425 /* nothing to do for everything else */
426 break;
429 return fc;
432 int is_struct(unsigned char type)
434 switch (type)
436 case RPC_FC_STRUCT:
437 case RPC_FC_PSTRUCT:
438 case RPC_FC_CSTRUCT:
439 case RPC_FC_CPSTRUCT:
440 case RPC_FC_CVSTRUCT:
441 case RPC_FC_BOGUS_STRUCT:
442 return 1;
443 default:
444 return 0;
448 static int is_non_complex_struct(const type_t *type)
450 return (type_get_type(type) == TYPE_STRUCT &&
451 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
454 static int type_has_pointers(const type_t *type)
456 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
458 case TGT_USER_TYPE:
459 return FALSE;
460 case TGT_POINTER:
461 return TRUE;
462 case TGT_ARRAY:
463 /* FIXME: array can be pointer */
464 return type_has_pointers(type_array_get_element(type));
465 case TGT_STRUCT:
467 var_list_t *fields = type_struct_get_fields(type);
468 const var_t *field;
469 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
471 if (type_has_pointers(field->type))
472 return TRUE;
474 break;
476 case TGT_UNION:
478 var_list_t *fields;
479 const var_t *field;
480 fields = type_union_get_cases(type);
481 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
483 if (field->type && type_has_pointers(field->type))
484 return TRUE;
486 break;
488 case TGT_CTXT_HANDLE:
489 case TGT_CTXT_HANDLE_POINTER:
490 case TGT_STRING:
491 case TGT_IFACE_POINTER:
492 case TGT_BASIC:
493 case TGT_ENUM:
494 case TGT_INVALID:
495 break;
498 return FALSE;
501 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
502 int toplevel_param)
504 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
506 case TGT_USER_TYPE:
507 return FALSE;
508 case TGT_POINTER:
509 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
510 return TRUE;
511 else
512 return FALSE;
513 case TGT_ARRAY:
514 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
515 return TRUE;
516 else
517 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
518 case TGT_STRUCT:
520 var_list_t *fields = type_struct_get_fields(type);
521 const var_t *field;
522 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
524 if (type_has_full_pointer(field->type, field->attrs, FALSE))
525 return TRUE;
527 break;
529 case TGT_UNION:
531 var_list_t *fields;
532 const var_t *field;
533 fields = type_union_get_cases(type);
534 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
536 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
537 return TRUE;
539 break;
541 case TGT_CTXT_HANDLE:
542 case TGT_CTXT_HANDLE_POINTER:
543 case TGT_STRING:
544 case TGT_IFACE_POINTER:
545 case TGT_BASIC:
546 case TGT_ENUM:
547 case TGT_INVALID:
548 break;
551 return FALSE;
554 static unsigned short user_type_offset(const char *name)
556 user_type_t *ut;
557 unsigned short off = 0;
558 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
560 if (strcmp(name, ut->name) == 0)
561 return off;
562 ++off;
564 error("user_type_offset: couldn't find type (%s)\n", name);
565 return 0;
568 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
570 type->typestring_offset = offset;
571 if (file) type->tfswrite = FALSE;
574 static void guard_rec(type_t *type)
576 /* types that contain references to themselves (like a linked list),
577 need to be shielded from infinite recursion when writing embedded
578 types */
579 if (type->typestring_offset)
580 type->tfswrite = FALSE;
581 else
582 type->typestring_offset = 1;
585 static type_t *get_user_type(const type_t *t, const char **pname)
587 for (;;)
589 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
590 if (ut)
592 if (pname)
593 *pname = t->name;
594 return ut;
597 if (type_is_alias(t))
598 t = type_alias_get_aliasee(t);
599 else
600 return 0;
604 int is_user_type(const type_t *t)
606 return get_user_type(t, NULL) != NULL;
609 static int is_embedded_complex(const type_t *type)
611 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
613 case TGT_USER_TYPE:
614 case TGT_STRUCT:
615 case TGT_UNION:
616 case TGT_ARRAY:
617 case TGT_IFACE_POINTER:
618 return TRUE;
619 default:
620 return FALSE;
624 static const char *get_context_handle_type_name(const type_t *type)
626 const type_t *t;
627 for (t = type;
628 is_ptr(t) || type_is_alias(t);
629 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
630 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
631 return t->name;
632 assert(0);
633 return NULL;
636 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
637 do { \
638 if (file) \
639 fprintf(file, "/* %2u */\n", typestring_offset); \
640 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
642 while (0)
644 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
645 static void print_file(FILE *file, int indent, const char *format, ...)
647 va_list va;
648 va_start(va, format);
649 print(file, indent, format, va);
650 va_end(va);
653 void print(FILE *file, int indent, const char *format, va_list va)
655 if (file)
657 if (format[0] != '\n')
658 while (0 < indent--)
659 fprintf(file, " ");
660 vfprintf(file, format, va);
665 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
667 if (decl_indirect(t))
669 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
670 local_var_prefix, n, local_var_prefix, n);
671 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
673 else if (is_ptr(t) || is_array(t))
674 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
677 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
679 const var_t *var;
681 if (!is_void(type_function_get_rettype(func->type)))
682 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
684 if (!type_get_function_args(func->type))
685 return;
687 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
688 write_var_init(file, indent, var->type, var->name, local_var_prefix);
690 fprintf(file, "\n");
693 static void write_formatdesc(FILE *f, int indent, const char *str)
695 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
696 print_file(f, indent, "{\n");
697 print_file(f, indent + 1, "short Pad;\n");
698 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
699 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
700 print_file(f, indent, "\n");
703 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
705 clear_all_offsets();
707 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
708 get_size_typeformatstring(stmts, pred));
710 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
711 get_size_procformatstring(stmts, pred));
713 fprintf(f, "\n");
714 write_formatdesc(f, indent, "TYPE");
715 write_formatdesc(f, indent, "PROC");
716 fprintf(f, "\n");
717 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
718 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
719 print_file(f, indent, "\n");
722 int decl_indirect(const type_t *t)
724 if (is_user_type(t))
725 return TRUE;
726 return (type_get_type(t) != TYPE_BASIC &&
727 type_get_type(t) != TYPE_ENUM &&
728 type_get_type(t) != TYPE_POINTER &&
729 type_get_type(t) != TYPE_ARRAY);
732 static unsigned int write_procformatstring_type(FILE *file, int indent,
733 const char *name,
734 const type_t *type,
735 const attr_list_t *attrs,
736 int is_return)
738 unsigned int size;
740 int is_in = is_attr(attrs, ATTR_IN);
741 int is_out = is_attr(attrs, ATTR_OUT);
743 if (!is_in && !is_out) is_in = TRUE;
745 if (type_get_type(type) == TYPE_BASIC ||
746 type_get_type(type) == TYPE_ENUM)
748 unsigned char fc;
750 if (is_return)
751 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
752 else
753 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
755 if (type_get_type(type) == TYPE_ENUM)
757 fc = get_enum_fc(type);
759 else
761 fc = get_basic_fc(type);
763 if (fc == RPC_FC_BIND_PRIMITIVE)
764 fc = RPC_FC_IGNORE;
767 print_file(file, indent, "0x%02x, /* %s */\n",
768 fc, string_of_type(fc));
769 size = 2; /* includes param type prefix */
771 else
773 if (is_return)
774 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
775 else if (is_in && is_out)
776 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
777 else if (is_out)
778 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
779 else
780 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
782 print_file(file, indent, "0x01,\n");
783 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
784 size = 4; /* includes param type prefix */
786 return size;
789 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
791 const statement_t *stmt;
792 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
794 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
796 const statement_t *stmt_func;
797 if (!pred(stmt->u.type))
798 continue;
799 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
801 const var_t *func = stmt_func->u.var;
802 if (is_local(func->attrs)) continue;
803 /* emit argument data */
804 if (type_get_function_args(func->type))
806 const var_t *var;
807 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
808 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
811 /* emit return value data */
812 if (is_void(type_function_get_rettype(func->type)))
814 print_file(file, indent, "0x5b, /* FC_END */\n");
815 print_file(file, indent, "0x5c, /* FC_PAD */\n");
817 else
818 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
821 else if (stmt->type == STMT_LIBRARY)
822 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
826 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
828 int indent = 0;
830 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
831 print_file(file, indent, "{\n");
832 indent++;
833 print_file(file, indent, "0,\n");
834 print_file(file, indent, "{\n");
835 indent++;
837 write_procformatstring_stmts(file, indent, stmts, pred);
839 print_file(file, indent, "0x0\n");
840 indent--;
841 print_file(file, indent, "}\n");
842 indent--;
843 print_file(file, indent, "};\n");
844 print_file(file, indent, "\n");
847 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
849 unsigned char fc;
851 if (type_get_type(type) == TYPE_BASIC)
852 fc = get_basic_fc(type);
853 else if (type_get_type(type) == TYPE_ENUM)
854 fc = get_enum_fc(type);
855 else
856 return 0;
858 if (convert_to_signed_type)
860 switch(fc)
862 case RPC_FC_USMALL:
863 fc = RPC_FC_SMALL;
864 break;
865 case RPC_FC_USHORT:
866 fc = RPC_FC_SHORT;
867 break;
868 case RPC_FC_ULONG:
869 fc = RPC_FC_LONG;
870 break;
874 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
875 *typestring_offset += 1;
876 return 1;
879 /* write conformance / variance descriptor */
880 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
881 unsigned int baseoff, const type_t *type,
882 const expr_t *expr)
884 unsigned char operator_type = 0;
885 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
886 const char *conftype_string = "";
887 const char *operator_string = "no operators";
888 const expr_t *subexpr;
890 if (!expr)
892 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
893 return 4;
896 if (!structure)
898 /* Top-level conformance calculations are done inline. */
899 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
900 RPC_FC_TOP_LEVEL_CONFORMANCE);
901 print_file (file, 2, "0x0,\n");
902 print_file (file, 2, "NdrFcShort(0x0),\n");
903 return 4;
906 if (expr->is_const)
908 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
909 error("write_conf_or_var_desc: constant value %ld is greater than "
910 "the maximum constant size of %d\n", expr->cval,
911 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
913 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
914 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
915 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
916 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
918 return 4;
921 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
923 conftype = RPC_FC_POINTER_CONFORMANCE;
924 conftype_string = "field pointer, ";
927 subexpr = expr;
928 switch (subexpr->type)
930 case EXPR_PPTR:
931 subexpr = subexpr->ref;
932 operator_type = RPC_FC_DEREFERENCE;
933 operator_string = "FC_DEREFERENCE";
934 break;
935 case EXPR_DIV:
936 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
938 subexpr = subexpr->ref;
939 operator_type = RPC_FC_DIV_2;
940 operator_string = "FC_DIV_2";
942 break;
943 case EXPR_MUL:
944 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
946 subexpr = subexpr->ref;
947 operator_type = RPC_FC_MULT_2;
948 operator_string = "FC_MULT_2";
950 break;
951 case EXPR_SUB:
952 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
954 subexpr = subexpr->ref;
955 operator_type = RPC_FC_SUB_1;
956 operator_string = "FC_SUB_1";
958 break;
959 case EXPR_ADD:
960 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
962 subexpr = subexpr->ref;
963 operator_type = RPC_FC_ADD_1;
964 operator_string = "FC_ADD_1";
966 break;
967 default:
968 break;
971 if (subexpr->type == EXPR_IDENTIFIER)
973 const type_t *correlation_variable = NULL;
974 unsigned char param_type = 0;
975 unsigned int offset = 0;
976 const var_t *var;
977 var_list_t *fields = type_struct_get_fields(structure);
979 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
981 unsigned int align = 0;
982 /* FIXME: take alignment into account */
983 if (var->name && !strcmp(var->name, subexpr->u.sval))
985 correlation_variable = var->type;
986 break;
988 offset += type_memsize(var->type, &align);
990 if (!correlation_variable)
991 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
992 subexpr->u.sval);
994 correlation_variable = expr_resolve_type(NULL, structure, expr);
996 offset -= baseoff;
998 if (type_get_type(correlation_variable) == TYPE_BASIC)
1000 switch (get_basic_fc(correlation_variable))
1002 case RPC_FC_CHAR:
1003 case RPC_FC_SMALL:
1004 param_type = RPC_FC_SMALL;
1005 break;
1006 case RPC_FC_BYTE:
1007 case RPC_FC_USMALL:
1008 param_type = RPC_FC_USMALL;
1009 break;
1010 case RPC_FC_WCHAR:
1011 case RPC_FC_SHORT:
1012 param_type = RPC_FC_SHORT;
1013 break;
1014 case RPC_FC_USHORT:
1015 param_type = RPC_FC_USHORT;
1016 break;
1017 case RPC_FC_LONG:
1018 param_type = RPC_FC_LONG;
1019 break;
1020 case RPC_FC_ULONG:
1021 param_type = RPC_FC_ULONG;
1022 break;
1023 default:
1024 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1025 get_basic_fc(correlation_variable));
1028 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1030 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1031 param_type = RPC_FC_LONG;
1032 else
1033 param_type = RPC_FC_SHORT;
1035 else
1037 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1038 subexpr->u.sval);
1039 return 0;
1042 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1043 conftype | param_type, conftype_string, string_of_type(param_type));
1044 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1045 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1046 offset, offset);
1048 else
1050 unsigned int callback_offset = 0;
1051 struct expr_eval_routine *eval;
1052 int found = 0;
1054 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1056 if (!strcmp (eval->structure->name, structure->name)
1057 && !compare_expr (eval->expr, expr))
1059 found = 1;
1060 break;
1062 callback_offset++;
1065 if (!found)
1067 eval = xmalloc (sizeof(*eval));
1068 eval->structure = structure;
1069 eval->baseoff = baseoff;
1070 eval->expr = expr;
1071 list_add_tail (&expr_eval_routines, &eval->entry);
1074 if (callback_offset > USHRT_MAX)
1075 error("Maximum number of callback routines reached\n");
1077 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1078 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1079 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1081 return 4;
1084 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1086 int have_align = FALSE;
1087 unsigned int size = 0;
1088 const var_t *v;
1090 if (!fields) return 0;
1091 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1093 unsigned int falign = 0;
1094 unsigned int fsize = type_memsize(v->type, &falign);
1095 if (!have_align)
1097 *align = falign;
1098 have_align = TRUE;
1100 size = ROUND_SIZE(size, falign);
1101 size += fsize;
1104 size = ROUND_SIZE(size, *align);
1105 return size;
1108 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1110 unsigned int size, maxs = 0;
1111 unsigned int align = *pmaxa;
1112 const var_t *v;
1114 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1116 /* we could have an empty default field with NULL type */
1117 if (v->type)
1119 size = type_memsize(v->type, &align);
1120 if (maxs < size) maxs = size;
1121 if (*pmaxa < align) *pmaxa = align;
1125 return maxs;
1128 int get_padding(const var_list_t *fields)
1130 unsigned short offset = 0;
1131 int salign = -1;
1132 const var_t *f;
1134 if (!fields)
1135 return 0;
1137 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1139 type_t *ft = f->type;
1140 unsigned int align = 0;
1141 unsigned int size = type_memsize(ft, &align);
1142 if (salign == -1)
1143 salign = align;
1144 offset = ROUND_SIZE(offset, align);
1145 offset += size;
1148 return ROUNDING(offset, salign);
1151 unsigned int type_memsize(const type_t *t, unsigned int *align)
1153 unsigned int size = 0;
1155 switch (type_get_type(t))
1157 case TYPE_BASIC:
1158 switch (get_basic_fc(t))
1160 case RPC_FC_BYTE:
1161 case RPC_FC_CHAR:
1162 case RPC_FC_USMALL:
1163 case RPC_FC_SMALL:
1164 size = 1;
1165 if (size > *align) *align = size;
1166 break;
1167 case RPC_FC_WCHAR:
1168 case RPC_FC_USHORT:
1169 case RPC_FC_SHORT:
1170 size = 2;
1171 if (size > *align) *align = size;
1172 break;
1173 case RPC_FC_ULONG:
1174 case RPC_FC_LONG:
1175 case RPC_FC_ERROR_STATUS_T:
1176 case RPC_FC_FLOAT:
1177 size = 4;
1178 if (size > *align) *align = size;
1179 break;
1180 case RPC_FC_HYPER:
1181 case RPC_FC_DOUBLE:
1182 size = 8;
1183 if (size > *align) *align = size;
1184 break;
1185 default:
1186 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1187 size = 0;
1189 break;
1190 case TYPE_ENUM:
1191 switch (get_enum_fc(t))
1193 case RPC_FC_ENUM32:
1194 size = 4;
1195 if (size > *align) *align = size;
1196 break;
1197 case RPC_FC_ENUM16:
1198 size = 2;
1199 if (size > *align) *align = size;
1200 break;
1201 default:
1202 error("type_memsize: Unknown enum type\n");
1203 size = 0;
1205 break;
1206 case TYPE_STRUCT:
1207 size = fields_memsize(type_struct_get_fields(t), align);
1208 break;
1209 case TYPE_ENCAPSULATED_UNION:
1210 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1211 break;
1212 case TYPE_UNION:
1213 size = union_memsize(type_union_get_cases(t), align);
1214 break;
1215 case TYPE_POINTER:
1216 assert( pointer_size );
1217 size = pointer_size;
1218 if (size > *align) *align = size;
1219 break;
1220 case TYPE_ARRAY:
1221 if (!type_array_is_decl_as_ptr(t))
1223 if (is_conformant_array(t))
1225 type_memsize(type_array_get_element(t), align);
1226 size = 0;
1228 else
1229 size = type_array_get_dim(t) *
1230 type_memsize(type_array_get_element(t), align);
1232 else /* declared as a pointer */
1234 assert( pointer_size );
1235 size = pointer_size;
1236 if (size > *align) *align = size;
1238 break;
1239 case TYPE_INTERFACE:
1240 case TYPE_ALIAS:
1241 case TYPE_VOID:
1242 case TYPE_COCLASS:
1243 case TYPE_MODULE:
1244 case TYPE_FUNCTION:
1245 /* these types should not be encountered here due to language
1246 * restrictions (interface, void, coclass, module), logical
1247 * restrictions (alias - due to type_get_type call above) or
1248 * checking restrictions (function). */
1249 assert(0);
1252 return size;
1255 int is_full_pointer_function(const var_t *func)
1257 const var_t *var;
1258 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1259 return TRUE;
1260 if (!type_get_function_args(func->type))
1261 return FALSE;
1262 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1263 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1264 return TRUE;
1265 return FALSE;
1268 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1270 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1271 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1272 fprintf(file, "\n");
1275 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1277 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1278 fprintf(file, "\n");
1281 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1282 const type_t *type,
1283 int toplevel_param,
1284 unsigned int offset,
1285 unsigned int *typeformat_offset)
1287 unsigned int start_offset = *typeformat_offset;
1288 short reloff = offset - (*typeformat_offset + 2);
1289 int in_attr, out_attr;
1290 int pointer_type;
1291 unsigned char flags = 0;
1293 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1295 in_attr = is_attr(attrs, ATTR_IN);
1296 out_attr = is_attr(attrs, ATTR_OUT);
1297 if (!in_attr && !out_attr) in_attr = 1;
1299 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1300 flags |= RPC_FC_P_ONSTACK;
1302 if (is_ptr(type) && !last_ptr(type))
1303 flags |= RPC_FC_P_DEREF;
1305 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1306 pointer_type,
1307 flags,
1308 string_of_type(pointer_type));
1309 if (file)
1311 if (flags & RPC_FC_P_ONSTACK)
1312 fprintf(file, " [allocated_on_stack]");
1313 if (flags & RPC_FC_P_DEREF)
1314 fprintf(file, " [pointer_deref]");
1315 fprintf(file, " */\n");
1318 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1319 *typeformat_offset += 4;
1321 return start_offset;
1324 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1326 unsigned char fc;
1327 unsigned char pointer_fc;
1328 const type_t *ref;
1330 /* for historical reasons, write_simple_pointer also handled string types,
1331 * but no longer does. catch bad uses of the function with this check */
1332 if (is_string_type(attrs, type))
1333 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1335 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1337 ref = type_pointer_get_ref(type);
1338 if (type_get_type(ref) == TYPE_ENUM)
1339 fc = get_enum_fc(ref);
1340 else
1341 fc = get_basic_fc(ref);
1343 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1344 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1345 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1346 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1347 return 4;
1350 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1352 print_file(file, 0, "/* %u (", tfsoff);
1353 write_type_decl(file, t, NULL);
1354 print_file(file, 0, ") */\n");
1357 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1358 type_t *type, int toplevel_param,
1359 unsigned int *typestring_offset)
1361 unsigned int offset = *typestring_offset;
1362 type_t *ref = type_pointer_get_ref(type);
1364 print_start_tfs_comment(file, type, offset);
1365 update_tfsoff(type, offset, file);
1367 if (ref->typestring_offset)
1368 write_nonsimple_pointer(file, attrs, type,
1369 toplevel_param,
1370 type_pointer_get_ref(type)->typestring_offset,
1371 typestring_offset);
1372 else if (type_get_type(ref) == TYPE_BASIC ||
1373 type_get_type(ref) == TYPE_ENUM)
1374 *typestring_offset += write_simple_pointer(file, attrs, type,
1375 toplevel_param);
1377 return offset;
1380 static int processed(const type_t *type)
1382 return type->typestring_offset && !type->tfswrite;
1385 static int user_type_has_variable_size(const type_t *t)
1387 if (is_ptr(t))
1388 return TRUE;
1389 else if (type_get_type(t) == TYPE_STRUCT)
1391 switch (get_struct_fc(t))
1393 case RPC_FC_PSTRUCT:
1394 case RPC_FC_CSTRUCT:
1395 case RPC_FC_CPSTRUCT:
1396 case RPC_FC_CVSTRUCT:
1397 return TRUE;
1400 /* Note: Since this only applies to user types, we can't have a conformant
1401 array here, and strings should get filed under pointer in this case. */
1402 return FALSE;
1405 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1407 unsigned int start, absoff, flags;
1408 unsigned int align = 0, ualign = 0;
1409 const char *name = NULL;
1410 type_t *utype = get_user_type(type, &name);
1411 unsigned int usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1412 unsigned int size = type_memsize(type, &align);
1413 unsigned short funoff = user_type_offset(name);
1414 short reloff;
1416 guard_rec(type);
1418 if (type_get_type(utype) == TYPE_BASIC ||
1419 type_get_type(utype) == TYPE_ENUM)
1421 unsigned char fc;
1423 if (type_get_type(utype) == TYPE_ENUM)
1424 fc = get_enum_fc(utype);
1425 else
1426 fc = get_basic_fc(utype);
1428 absoff = *tfsoff;
1429 print_start_tfs_comment(file, utype, absoff);
1430 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1431 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1432 *tfsoff += 2;
1434 else
1436 if (!processed(utype))
1437 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1438 absoff = utype->typestring_offset;
1441 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1442 flags = 0x40;
1443 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1444 flags = 0x80;
1445 else
1446 flags = 0;
1448 start = *tfsoff;
1449 update_tfsoff(type, start, file);
1450 print_start_tfs_comment(file, type, start);
1451 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1452 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1453 flags | (align - 1), align - 1, flags);
1454 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1455 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1456 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1457 *tfsoff += 8;
1458 reloff = absoff - *tfsoff;
1459 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1460 *tfsoff += 2;
1463 static void write_member_type(FILE *file, const type_t *cont,
1464 int cont_is_complex, const attr_list_t *attrs,
1465 const type_t *type, unsigned int *corroff,
1466 unsigned int *tfsoff)
1468 if (is_embedded_complex(type) && !is_conformant_array(type))
1470 unsigned int absoff;
1471 short reloff;
1473 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1475 absoff = *corroff;
1476 *corroff += 8;
1478 else
1480 absoff = type->typestring_offset;
1482 reloff = absoff - (*tfsoff + 2);
1484 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1485 /* FIXME: actually compute necessary padding */
1486 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1487 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1488 reloff, reloff, absoff);
1489 *tfsoff += 4;
1491 else if (is_ptr(type) || is_conformant_array(type))
1493 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1494 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1495 *tfsoff += 1;
1497 else if (!write_base_type(file, type, TRUE, tfsoff))
1498 error("Unsupported member type %d\n", type_get_type(type));
1501 static void write_end(FILE *file, unsigned int *tfsoff)
1503 if (*tfsoff % 2 == 0)
1505 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1506 *tfsoff += 1;
1508 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1509 *tfsoff += 1;
1512 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1514 unsigned int offset = 0;
1515 var_list_t *fs = type_struct_get_fields(type);
1516 var_t *f;
1518 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1520 unsigned int align = 0;
1521 type_t *ft = f->type;
1522 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1524 short reloff;
1525 unsigned int absoff = ft->typestring_offset;
1526 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1527 absoff += 8; /* we already have a corr descr, skip it */
1528 reloff = absoff - (*tfsoff + 6);
1529 print_file(file, 0, "/* %d */\n", *tfsoff);
1530 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1531 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1532 write_conf_or_var_desc(file, current_structure, offset, ft,
1533 get_attrp(f->attrs, ATTR_SWITCHIS));
1534 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1535 reloff, reloff, absoff);
1536 *tfsoff += 8;
1539 /* FIXME: take alignment into account */
1540 offset += type_memsize(ft, &align);
1544 static int write_no_repeat_pointer_descriptions(
1545 FILE *file, const attr_list_t *attrs, type_t *type,
1546 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1547 unsigned int *typestring_offset)
1549 int written = 0;
1550 unsigned int align;
1552 if (is_ptr(type) ||
1553 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1555 unsigned int memsize;
1557 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1558 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1560 /* pointer instance */
1561 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1562 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1563 *typestring_offset += 6;
1565 if (is_ptr(type))
1567 if (is_string_type(attrs, type))
1568 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1569 else
1570 write_pointer_tfs(file, attrs, type, FALSE, typestring_offset);
1572 else
1574 unsigned int offset = type->typestring_offset;
1575 /* skip over the pointer that is written for strings, since a
1576 * pointer has to be written in-place here */
1577 if (is_string_type(attrs, type))
1578 offset += 4;
1579 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1582 align = 0;
1583 memsize = type_memsize(type, &align);
1584 *offset_in_memory += memsize;
1585 /* increment these separately as in the case of conformant (varying)
1586 * structures these start at different values */
1587 *offset_in_buffer += memsize;
1589 return 1;
1592 if (is_non_complex_struct(type))
1594 const var_t *v;
1595 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1597 if (offset_in_memory && offset_in_buffer)
1599 unsigned int padding;
1600 align = 0;
1601 type_memsize(v->type, &align);
1602 padding = ROUNDING(*offset_in_memory, align);
1603 *offset_in_memory += padding;
1604 *offset_in_buffer += padding;
1606 written += write_no_repeat_pointer_descriptions(
1607 file, v->attrs, v->type,
1608 offset_in_memory, offset_in_buffer, typestring_offset);
1611 else
1613 unsigned int memsize;
1614 align = 0;
1615 memsize = type_memsize(type, &align);
1616 *offset_in_memory += memsize;
1617 /* increment these separately as in the case of conformant (varying)
1618 * structures these start at different values */
1619 *offset_in_buffer += memsize;
1622 return written;
1625 static int write_pointer_description_offsets(
1626 FILE *file, const attr_list_t *attrs, type_t *type,
1627 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1628 unsigned int *typestring_offset)
1630 int written = 0;
1631 unsigned int align;
1633 if (is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE)
1635 type_t *ref = type_pointer_get_ref(type);
1637 if (offset_in_memory && offset_in_buffer)
1639 unsigned int memsize;
1641 /* pointer instance */
1642 /* FIXME: sometimes from end of structure, sometimes from beginning */
1643 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1644 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1646 align = 0;
1647 memsize = type_memsize(type, &align);
1648 *offset_in_memory += memsize;
1649 /* increment these separately as in the case of conformant (varying)
1650 * structures these start at different values */
1651 *offset_in_buffer += memsize;
1653 *typestring_offset += 4;
1655 if (is_string_type(attrs, type))
1656 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1657 else if (processed(ref) || type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1658 write_pointer_tfs(file, attrs, type, FALSE, typestring_offset);
1659 else
1660 error("write_pointer_description_offsets: type format string unknown\n");
1662 return 1;
1665 if (is_array(type))
1667 return write_pointer_description_offsets(
1668 file, attrs, type_array_get_element(type), offset_in_memory,
1669 offset_in_buffer, typestring_offset);
1671 else if (is_non_complex_struct(type))
1673 /* otherwise search for interesting fields to parse */
1674 const var_t *v;
1675 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1677 if (offset_in_memory && offset_in_buffer)
1679 unsigned int padding;
1680 align = 0;
1681 type_memsize(v->type, &align);
1682 padding = ROUNDING(*offset_in_memory, align);
1683 *offset_in_memory += padding;
1684 *offset_in_buffer += padding;
1686 written += write_pointer_description_offsets(
1687 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1688 typestring_offset);
1691 else
1693 if (offset_in_memory && offset_in_buffer)
1695 unsigned int memsize;
1696 align = 0;
1697 memsize = type_memsize(type, &align);
1698 *offset_in_memory += memsize;
1699 /* increment these separately as in the case of conformant (varying)
1700 * structures these start at different values */
1701 *offset_in_buffer += memsize;
1705 return written;
1708 /* Note: if file is NULL return value is number of pointers to write, else
1709 * it is the number of type format characters written */
1710 static int write_fixed_array_pointer_descriptions(
1711 FILE *file, const attr_list_t *attrs, type_t *type,
1712 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1713 unsigned int *typestring_offset)
1715 unsigned int align;
1716 int pointer_count = 0;
1718 if (type_get_type(type) == TYPE_ARRAY &&
1719 !type_array_has_conformance(type) && !type_array_has_variance(type))
1721 unsigned int temp = 0;
1722 /* unfortunately, this needs to be done in two passes to avoid
1723 * writing out redundant FC_FIXED_REPEAT descriptions */
1724 pointer_count = write_pointer_description_offsets(
1725 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1726 if (pointer_count > 0)
1728 unsigned int increment_size;
1729 unsigned int offset_of_array_pointer_mem = 0;
1730 unsigned int offset_of_array_pointer_buf = 0;
1732 align = 0;
1733 increment_size = type_memsize(type_array_get_element(type), &align);
1735 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1736 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1737 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1738 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1739 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1740 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1741 *typestring_offset += 10;
1743 pointer_count = write_pointer_description_offsets(
1744 file, attrs, type, &offset_of_array_pointer_mem,
1745 &offset_of_array_pointer_buf, typestring_offset);
1748 else if (type_get_type(type) == TYPE_STRUCT)
1750 const var_t *v;
1751 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1753 if (offset_in_memory && offset_in_buffer)
1755 unsigned int padding;
1756 align = 0;
1757 type_memsize(v->type, &align);
1758 padding = ROUNDING(*offset_in_memory, align);
1759 *offset_in_memory += padding;
1760 *offset_in_buffer += padding;
1762 pointer_count += write_fixed_array_pointer_descriptions(
1763 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1764 typestring_offset);
1767 else
1769 if (offset_in_memory && offset_in_buffer)
1771 unsigned int memsize;
1772 align = 0;
1773 memsize = type_memsize(type, &align);
1774 *offset_in_memory += memsize;
1775 /* increment these separately as in the case of conformant (varying)
1776 * structures these start at different values */
1777 *offset_in_buffer += memsize;
1781 return pointer_count;
1784 /* Note: if file is NULL return value is number of pointers to write, else
1785 * it is the number of type format characters written */
1786 static int write_conformant_array_pointer_descriptions(
1787 FILE *file, const attr_list_t *attrs, type_t *type,
1788 unsigned int offset_in_memory, unsigned int *typestring_offset)
1790 unsigned int align;
1791 int pointer_count = 0;
1793 if (is_conformant_array(type) && !type_array_has_variance(type))
1795 unsigned int temp = 0;
1796 /* unfortunately, this needs to be done in two passes to avoid
1797 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1798 pointer_count = write_pointer_description_offsets(
1799 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1800 if (pointer_count > 0)
1802 unsigned int increment_size;
1803 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1804 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1806 align = 0;
1807 increment_size = type_memsize(type_array_get_element(type), &align);
1809 if (increment_size > USHRT_MAX)
1810 error("array size of %u bytes is too large\n", increment_size);
1812 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1813 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1814 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1815 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1816 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1817 *typestring_offset += 8;
1819 pointer_count = write_pointer_description_offsets(
1820 file, attrs, type_array_get_element(type),
1821 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1822 typestring_offset);
1826 return pointer_count;
1829 /* Note: if file is NULL return value is number of pointers to write, else
1830 * it is the number of type format characters written */
1831 static int write_varying_array_pointer_descriptions(
1832 FILE *file, const attr_list_t *attrs, type_t *type,
1833 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1834 unsigned int *typestring_offset)
1836 unsigned int align;
1837 int pointer_count = 0;
1839 if (is_array(type) && type_array_has_variance(type))
1841 unsigned int temp = 0;
1842 /* unfortunately, this needs to be done in two passes to avoid
1843 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1844 pointer_count = write_pointer_description_offsets(
1845 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1846 if (pointer_count > 0)
1848 unsigned int increment_size;
1850 align = 0;
1851 increment_size = type_memsize(type_array_get_element(type), &align);
1853 if (increment_size > USHRT_MAX)
1854 error("array size of %u bytes is too large\n", increment_size);
1856 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1857 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1858 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1859 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1860 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1861 *typestring_offset += 8;
1863 pointer_count = write_pointer_description_offsets(
1864 file, attrs, type, offset_in_memory,
1865 offset_in_buffer, typestring_offset);
1868 else if (type_get_type(type) == TYPE_STRUCT)
1870 const var_t *v;
1871 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1873 if (offset_in_memory && offset_in_buffer)
1875 unsigned int padding;
1877 if (is_array(v->type) && type_array_has_variance(v->type))
1879 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1880 /* skip over variance and offset in buffer */
1881 *offset_in_buffer += 8;
1884 align = 0;
1885 type_memsize(v->type, &align);
1886 padding = ROUNDING(*offset_in_memory, align);
1887 *offset_in_memory += padding;
1888 *offset_in_buffer += padding;
1890 pointer_count += write_varying_array_pointer_descriptions(
1891 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1892 typestring_offset);
1895 else
1897 if (offset_in_memory && offset_in_buffer)
1899 unsigned int memsize;
1900 align = 0;
1901 memsize = type_memsize(type, &align);
1902 *offset_in_memory += memsize;
1903 /* increment these separately as in the case of conformant (varying)
1904 * structures these start at different values */
1905 *offset_in_buffer += memsize;
1909 return pointer_count;
1912 static void write_pointer_description(FILE *file, type_t *type,
1913 unsigned int *typestring_offset)
1915 unsigned int offset_in_buffer;
1916 unsigned int offset_in_memory;
1918 /* pass 1: search for single instance of a pointer (i.e. don't descend
1919 * into arrays) */
1920 if (!is_array(type))
1922 offset_in_memory = 0;
1923 offset_in_buffer = 0;
1924 write_no_repeat_pointer_descriptions(
1925 file, NULL, type,
1926 &offset_in_memory, &offset_in_buffer, typestring_offset);
1929 /* pass 2: search for pointers in fixed arrays */
1930 offset_in_memory = 0;
1931 offset_in_buffer = 0;
1932 write_fixed_array_pointer_descriptions(
1933 file, NULL, type,
1934 &offset_in_memory, &offset_in_buffer, typestring_offset);
1936 /* pass 3: search for pointers in conformant only arrays (but don't descend
1937 * into conformant varying or varying arrays) */
1938 if (is_conformant_array(type) &&
1939 (type_array_is_decl_as_ptr(type) || !current_structure))
1940 write_conformant_array_pointer_descriptions(
1941 file, NULL, type, 0, typestring_offset);
1942 else if (type_get_type(type) == TYPE_STRUCT &&
1943 get_struct_fc(type) == RPC_FC_CPSTRUCT)
1945 unsigned int align = 0;
1946 type_t *carray = find_array_or_string_in_struct(type)->type;
1947 write_conformant_array_pointer_descriptions(
1948 file, NULL, carray,
1949 type_memsize(type, &align),
1950 typestring_offset);
1953 /* pass 4: search for pointers in varying arrays */
1954 offset_in_memory = 0;
1955 offset_in_buffer = 0;
1956 write_varying_array_pointer_descriptions(
1957 file, NULL, type,
1958 &offset_in_memory, &offset_in_buffer, typestring_offset);
1961 int is_declptr(const type_t *t)
1963 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
1966 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1967 type_t *type, int toplevel_param,
1968 const char *name, unsigned int *typestring_offset)
1970 unsigned int start_offset;
1971 unsigned char rtype;
1972 type_t *elem_type;
1974 start_offset = *typestring_offset;
1975 update_tfsoff(type, start_offset, file);
1977 if (is_declptr(type))
1979 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1980 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1981 if (!pointer_type)
1982 pointer_type = RPC_FC_RP;
1983 print_start_tfs_comment(file, type, *typestring_offset);
1984 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1985 pointer_type, flag, string_of_type(pointer_type),
1986 flag ? " [simple_pointer]" : "");
1987 *typestring_offset += 2;
1988 if (!flag)
1990 print_file(file, 2, "NdrFcShort(0x2),\n");
1991 *typestring_offset += 2;
1995 if (is_array(type))
1996 elem_type = type_array_get_element(type);
1997 else
1998 elem_type = type_pointer_get_ref(type);
2000 if (type_get_type(elem_type) != TYPE_BASIC)
2002 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2003 return start_offset;
2006 rtype = get_basic_fc(elem_type);
2007 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2009 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2010 return start_offset;
2013 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2015 unsigned int dim = type_array_get_dim(type);
2017 /* FIXME: multi-dimensional array */
2018 if (0xffffu < dim)
2019 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2020 name, 0xffffu, dim - 0xffffu);
2022 if (rtype == RPC_FC_WCHAR)
2023 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2024 else
2025 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2026 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2027 *typestring_offset += 2;
2029 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2030 *typestring_offset += 2;
2032 return start_offset;
2034 else if (is_conformant_array(type))
2036 unsigned int align = 0;
2038 if (rtype == RPC_FC_WCHAR)
2039 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2040 else
2041 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2042 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2043 *typestring_offset += 2;
2045 *typestring_offset += write_conf_or_var_desc(
2046 file, current_structure,
2047 (!type_array_is_decl_as_ptr(type) && current_structure
2048 ? type_memsize(current_structure, &align)
2049 : 0),
2050 type, type_array_get_conformance(type));
2052 return start_offset;
2054 else
2056 if (rtype == RPC_FC_WCHAR)
2057 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2058 else
2059 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2060 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2061 *typestring_offset += 2;
2063 return start_offset;
2067 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2068 const char *name, unsigned int *typestring_offset)
2070 const expr_t *length_is = type_array_get_variance(type);
2071 const expr_t *size_is = type_array_get_conformance(type);
2072 unsigned int align = 0;
2073 unsigned int size;
2074 unsigned int start_offset;
2075 unsigned char fc;
2076 int has_pointer;
2077 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2078 unsigned int baseoff
2079 = !type_array_is_decl_as_ptr(type) && current_structure
2080 ? type_memsize(current_structure, &align)
2081 : 0;
2083 if (!pointer_type)
2084 pointer_type = RPC_FC_RP;
2086 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2087 has_pointer = TRUE;
2088 else
2089 has_pointer = type_has_pointers(type_array_get_element(type));
2091 align = 0;
2092 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2093 fc = get_array_fc(type);
2095 start_offset = *typestring_offset;
2096 update_tfsoff(type, start_offset, file);
2097 print_start_tfs_comment(file, type, start_offset);
2098 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2099 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2100 *typestring_offset += 2;
2102 align = 0;
2103 if (fc != RPC_FC_BOGUS_ARRAY)
2105 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2107 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2108 *typestring_offset += 4;
2110 else
2112 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2113 *typestring_offset += 2;
2116 if (is_conformant_array(type))
2117 *typestring_offset
2118 += write_conf_or_var_desc(file, current_structure, baseoff,
2119 type, size_is);
2121 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2123 unsigned int elalign = 0;
2124 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2125 unsigned int dim = type_array_get_dim(type);
2127 if (fc == RPC_FC_LGVARRAY)
2129 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2130 *typestring_offset += 4;
2132 else
2134 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2135 *typestring_offset += 2;
2138 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2139 *typestring_offset += 2;
2142 if (length_is)
2143 *typestring_offset
2144 += write_conf_or_var_desc(file, current_structure, baseoff,
2145 type, length_is);
2147 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2149 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2150 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2151 *typestring_offset += 2;
2152 write_pointer_description(file, type, typestring_offset);
2153 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2154 *typestring_offset += 1;
2157 write_member_type(file, type, FALSE, NULL, type_array_get_element(type), NULL, typestring_offset);
2158 write_end(file, typestring_offset);
2160 else
2162 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2163 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2164 *typestring_offset += 2;
2165 *typestring_offset
2166 += write_conf_or_var_desc(file, current_structure, baseoff,
2167 type, size_is);
2168 *typestring_offset
2169 += write_conf_or_var_desc(file, current_structure, baseoff,
2170 type, length_is);
2171 write_member_type(file, type, TRUE, NULL, type_array_get_element(type), NULL, typestring_offset);
2172 write_end(file, typestring_offset);
2175 return start_offset;
2178 static const var_t *find_array_or_string_in_struct(const type_t *type)
2180 const var_list_t *fields = type_struct_get_fields(type);
2181 const var_t *last_field;
2182 const type_t *ft;
2184 if (!fields || list_empty(fields))
2185 return NULL;
2187 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2188 ft = last_field->type;
2190 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2191 return last_field;
2193 if (type_get_type(ft) == TYPE_STRUCT)
2194 return find_array_or_string_in_struct(ft);
2195 else
2196 return NULL;
2199 static void write_struct_members(FILE *file, const type_t *type,
2200 int is_complex, unsigned int *corroff,
2201 unsigned int *typestring_offset)
2203 const var_t *field;
2204 unsigned short offset = 0;
2205 int salign = -1;
2206 int padding;
2207 var_list_t *fields = type_struct_get_fields(type);
2209 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2211 type_t *ft = field->type;
2212 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2214 unsigned int align = 0;
2215 unsigned int size = type_memsize(ft, &align);
2216 if (salign == -1)
2217 salign = align;
2218 if ((align - 1) & offset)
2220 unsigned char fc = 0;
2221 switch (align)
2223 case 4:
2224 fc = RPC_FC_ALIGNM4;
2225 break;
2226 case 8:
2227 fc = RPC_FC_ALIGNM8;
2228 break;
2229 default:
2230 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2232 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2233 offset = ROUND_SIZE(offset, align);
2234 *typestring_offset += 1;
2236 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2237 typestring_offset);
2238 offset += size;
2242 padding = ROUNDING(offset, salign);
2243 if (padding)
2245 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2246 RPC_FC_STRUCTPAD1 + padding - 1,
2247 padding);
2248 *typestring_offset += 1;
2251 write_end(file, typestring_offset);
2254 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2255 const char *name, unsigned int *tfsoff)
2257 const type_t *save_current_structure = current_structure;
2258 unsigned int total_size;
2259 const var_t *array;
2260 unsigned int start_offset;
2261 unsigned int array_offset;
2262 int has_pointers = 0;
2263 unsigned int align = 0;
2264 unsigned int corroff;
2265 var_t *f;
2266 unsigned char fc = get_struct_fc(type);
2267 var_list_t *fields = type_struct_get_fields(type);
2269 guard_rec(type);
2270 current_structure = type;
2272 total_size = type_memsize(type, &align);
2273 if (total_size > USHRT_MAX)
2274 error("structure size for %s exceeds %d bytes by %d bytes\n",
2275 name, USHRT_MAX, total_size - USHRT_MAX);
2277 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2278 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2279 FALSE, tfsoff);
2280 if (!has_pointers) has_pointers = type_has_pointers(type);
2282 array = find_array_or_string_in_struct(type);
2283 if (array && !processed(array->type))
2284 array_offset
2285 = is_string_type(array->attrs, array->type)
2286 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2287 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2289 corroff = *tfsoff;
2290 write_descriptors(file, type, tfsoff);
2292 start_offset = *tfsoff;
2293 update_tfsoff(type, start_offset, file);
2294 print_start_tfs_comment(file, type, start_offset);
2295 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2296 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2297 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2298 *tfsoff += 4;
2300 if (array)
2302 unsigned int absoff = array->type->typestring_offset;
2303 short reloff = absoff - *tfsoff;
2304 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2305 reloff, reloff, absoff);
2306 *tfsoff += 2;
2308 else if (fc == RPC_FC_BOGUS_STRUCT)
2310 print_file(file, 2, "NdrFcShort(0x0),\n");
2311 *tfsoff += 2;
2314 if (fc == RPC_FC_BOGUS_STRUCT)
2316 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2317 nothing is written to file yet. On the actual writing pass,
2318 this will have been updated. */
2319 unsigned int absoff = type_get_real_type(type)->ptrdesc ?
2320 type_get_real_type(type)->ptrdesc : *tfsoff;
2321 int reloff = absoff - *tfsoff;
2322 assert( reloff >= 0 );
2323 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2324 reloff, reloff, absoff);
2325 *tfsoff += 2;
2327 else if ((fc == RPC_FC_PSTRUCT) ||
2328 (fc == RPC_FC_CPSTRUCT) ||
2329 (fc == RPC_FC_CVSTRUCT && has_pointers))
2331 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2332 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2333 *tfsoff += 2;
2334 write_pointer_description(file, type, tfsoff);
2335 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2336 *tfsoff += 1;
2339 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2340 tfsoff);
2342 if (fc == RPC_FC_BOGUS_STRUCT)
2344 const var_t *f;
2346 type_get_real_type(type)->ptrdesc = *tfsoff;
2347 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2349 type_t *ft = f->type;
2350 if (is_ptr(ft))
2352 if (is_string_type(f->attrs, ft))
2353 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2354 else
2355 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2357 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2359 unsigned int offset;
2361 print_file(file, 0, "/* %d */\n", *tfsoff);
2363 offset = ft->typestring_offset;
2364 /* skip over the pointer that is written for strings, since a
2365 * pointer has to be written in-place here */
2366 if (is_string_type(f->attrs, ft))
2367 offset += 4;
2368 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2371 if (type_get_real_type(type)->ptrdesc == *tfsoff)
2372 type_get_real_type(type)->ptrdesc = 0;
2375 current_structure = save_current_structure;
2376 return start_offset;
2379 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2381 if (t == NULL)
2383 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2385 else
2387 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2389 unsigned char fc;
2390 if (type_get_type(t) == TYPE_BASIC)
2391 fc = get_basic_fc(t);
2392 else
2393 fc = get_enum_fc(t);
2394 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2395 fc, string_of_type(fc));
2397 else if (t->typestring_offset)
2399 short reloff = t->typestring_offset - *tfsoff;
2400 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2401 reloff, reloff, t->typestring_offset);
2403 else
2404 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2407 *tfsoff += 2;
2410 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2412 unsigned int align;
2413 unsigned int start_offset;
2414 unsigned int size;
2415 var_list_t *fields;
2416 unsigned int nbranch = 0;
2417 type_t *deftype = NULL;
2418 short nodeftype = 0xffff;
2419 var_t *f;
2421 guard_rec(type);
2423 align = 0;
2424 size = type_memsize(type, &align);
2426 fields = type_union_get_cases(type);
2428 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2430 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2431 if (cases)
2432 nbranch += list_count(cases);
2433 if (f->type)
2434 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2437 start_offset = *tfsoff;
2438 update_tfsoff(type, start_offset, file);
2439 print_start_tfs_comment(file, type, start_offset);
2440 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2442 const var_t *sv = type_union_get_switch_value(type);
2443 const type_t *st = sv->type;
2444 unsigned char fc;
2446 if (type_get_type(st) == TYPE_BASIC)
2448 switch (get_basic_fc(st))
2450 case RPC_FC_CHAR:
2451 case RPC_FC_SMALL:
2452 case RPC_FC_BYTE:
2453 case RPC_FC_USMALL:
2454 case RPC_FC_WCHAR:
2455 case RPC_FC_SHORT:
2456 case RPC_FC_USHORT:
2457 case RPC_FC_LONG:
2458 case RPC_FC_ULONG:
2459 fc = get_basic_fc(st);
2460 break;
2461 default:
2462 fc = 0;
2463 error("union switch type must be an integer, char, or enum\n");
2466 else if (type_get_type(st) == TYPE_ENUM)
2467 fc = get_enum_fc(st);
2468 else
2469 error("union switch type must be an integer, char, or enum\n");
2471 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2472 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2473 0x40 | fc, string_of_type(fc));
2474 *tfsoff += 2;
2476 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2478 static const expr_t dummy_expr; /* FIXME */
2479 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2480 unsigned char fc;
2482 if (type_get_type(st) == TYPE_BASIC)
2484 switch (get_basic_fc(st))
2486 case RPC_FC_CHAR:
2487 case RPC_FC_SMALL:
2488 case RPC_FC_USMALL:
2489 case RPC_FC_SHORT:
2490 case RPC_FC_USHORT:
2491 case RPC_FC_LONG:
2492 case RPC_FC_ULONG:
2493 case RPC_FC_ENUM16:
2494 case RPC_FC_ENUM32:
2495 fc = get_basic_fc(st);
2496 break;
2497 default:
2498 fc = 0;
2499 error("union switch type must be an integer, char, or enum\n");
2502 else if (type_get_type(st) == TYPE_ENUM)
2503 fc = get_enum_fc(st);
2504 else
2505 error("union switch type must be an integer, char, or enum\n");
2507 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2508 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2509 fc, string_of_type(fc));
2510 *tfsoff += 2;
2512 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2513 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2514 *tfsoff += 2;
2515 print_file(file, 0, "/* %u */\n", *tfsoff);
2518 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2519 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2520 *tfsoff += 4;
2522 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2524 type_t *ft = f->type;
2525 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2526 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2527 expr_t *c;
2529 if (cases == NULL && !deflt)
2530 error("union field %s with neither case nor default attribute\n", f->name);
2532 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2534 /* MIDL doesn't check for duplicate cases, even though that seems
2535 like a reasonable thing to do, it just dumps them to the TFS
2536 like we're going to do here. */
2537 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2538 *tfsoff += 4;
2539 write_branch_type(file, ft, tfsoff);
2542 /* MIDL allows multiple default branches, even though that seems
2543 illogical, it just chooses the last one, which is what we will
2544 do. */
2545 if (deflt)
2547 deftype = ft;
2548 nodeftype = 0;
2552 if (deftype)
2554 write_branch_type(file, deftype, tfsoff);
2556 else
2558 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2559 *tfsoff += 2;
2562 return start_offset;
2565 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2566 unsigned int *typeformat_offset)
2568 unsigned int i;
2569 unsigned int start_offset = *typeformat_offset;
2570 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2572 if (iid)
2574 print_file(file, 2, "0x2f, /* FC_IP */\n");
2575 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2576 *typeformat_offset
2577 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2579 else
2581 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2582 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2584 if (! uuid)
2585 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2587 update_tfsoff(type, start_offset, file);
2588 print_start_tfs_comment(file, type, start_offset);
2589 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2590 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2591 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2592 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2593 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2594 for (i = 0; i < 8; ++i)
2595 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2597 if (file)
2598 fprintf(file, "\n");
2600 *typeformat_offset += 18;
2602 return start_offset;
2605 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2606 const var_t *var,
2607 unsigned int *typeformat_offset)
2609 unsigned int start_offset = *typeformat_offset;
2610 unsigned char flags = 0;
2612 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2613 flags |= NDR_STRICT_CONTEXT_HANDLE;
2615 if (is_ptr(type))
2616 flags |= 0x80;
2617 if (is_attr(var->attrs, ATTR_IN))
2619 flags |= 0x40;
2620 if (!is_attr(var->attrs, ATTR_OUT))
2621 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2623 if (is_attr(var->attrs, ATTR_OUT))
2624 flags |= 0x20;
2626 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2627 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2628 /* return and can't be null values overlap */
2629 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2630 print_file(file, 0, "can't be null, ");
2631 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2632 print_file(file, 0, "serialize, ");
2633 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2634 print_file(file, 0, "no serialize, ");
2635 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2636 print_file(file, 0, "strict, ");
2637 if ((flags & 0x21) == 0x20)
2638 print_file(file, 0, "out, ");
2639 if ((flags & 0x21) == 0x21)
2640 print_file(file, 0, "return, ");
2641 if (flags & 0x40)
2642 print_file(file, 0, "in, ");
2643 if (flags & 0x80)
2644 print_file(file, 0, "via ptr, ");
2645 print_file(file, 0, "*/\n");
2646 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2647 print_file(file, 2, "0, /* FIXME: param num */\n");
2648 *typeformat_offset += 4;
2650 return start_offset;
2653 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2654 type_t *type, const var_t *var,
2655 int toplevel_param,
2656 unsigned int *typeformat_offset)
2658 unsigned int offset;
2660 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2662 case TGT_CTXT_HANDLE:
2663 case TGT_CTXT_HANDLE_POINTER:
2664 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2665 case TGT_USER_TYPE:
2666 write_user_tfs(file, type, typeformat_offset);
2667 return type->typestring_offset;
2668 case TGT_STRING:
2669 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2670 case TGT_ARRAY:
2672 int ptr_type;
2673 unsigned int off;
2674 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2675 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2676 if (ptr_type != RPC_FC_RP)
2678 unsigned int absoff = type->typestring_offset;
2679 short reloff = absoff - (*typeformat_offset + 2);
2680 off = *typeformat_offset;
2681 print_file(file, 0, "/* %d */\n", off);
2682 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2683 string_of_type(ptr_type));
2684 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2685 reloff, reloff, absoff);
2686 *typeformat_offset += 4;
2688 return off;
2690 case TGT_STRUCT:
2691 if (processed(type)) return type->typestring_offset;
2692 return write_struct_tfs(file, type, var->name, typeformat_offset);
2693 case TGT_UNION:
2694 if (processed(type)) return type->typestring_offset;
2695 return write_union_tfs(file, type, typeformat_offset);
2696 case TGT_ENUM:
2697 case TGT_BASIC:
2698 /* nothing to do */
2699 return 0;
2700 case TGT_IFACE_POINTER:
2701 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2702 case TGT_POINTER:
2703 if (last_ptr(type))
2705 size_t start_offset = *typeformat_offset;
2706 int in_attr = is_attr(var->attrs, ATTR_IN);
2707 int out_attr = is_attr(var->attrs, ATTR_OUT);
2708 const type_t *ref = type_pointer_get_ref(type);
2710 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2712 /* special case for pointers to base types */
2713 case TGT_BASIC:
2714 case TGT_ENUM:
2716 unsigned char fc;
2718 if (type_get_type(ref) == TYPE_ENUM)
2719 fc = get_enum_fc(ref);
2720 else
2721 fc = get_basic_fc(ref);
2723 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2724 get_pointer_fc(type, var->attrs, toplevel_param),
2725 (!in_attr && out_attr) ? 0x0C : 0x08,
2726 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2727 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2728 print_file(file, indent, "0x%02x, /* %s */\n",
2729 fc, string_of_type(fc));
2730 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2731 *typeformat_offset += 4;
2732 return start_offset;
2734 default:
2735 break;
2739 offset = write_typeformatstring_var(file, indent, func,
2740 type_pointer_get_ref(type), var,
2741 FALSE, typeformat_offset);
2742 if (file)
2743 fprintf(file, "/* %2u */\n", *typeformat_offset);
2744 return write_nonsimple_pointer(file, var->attrs, type,
2745 toplevel_param,
2746 offset, typeformat_offset);
2747 case TGT_INVALID:
2748 break;
2750 error("invalid type %s for var %s\n", type->name, var->name);
2751 return 0;
2754 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2755 const char *name, int write_ptr, unsigned int *tfsoff)
2757 int retmask = 0;
2759 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2761 case TGT_USER_TYPE:
2762 write_user_tfs(file, type, tfsoff);
2763 break;
2764 case TGT_STRING:
2765 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2766 break;
2767 case TGT_IFACE_POINTER:
2768 write_ip_tfs(file, attrs, type, tfsoff);
2769 break;
2770 case TGT_POINTER:
2772 type_t *ref = type_pointer_get_ref(type);
2774 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2775 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2777 if (write_ptr)
2778 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2780 retmask |= 1;
2781 break;
2783 case TGT_ARRAY:
2784 /* conformant arrays and strings are handled specially */
2785 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2787 write_array_tfs(file, attrs, type, name, tfsoff);
2788 if (is_conformant_array(type))
2789 retmask |= 1;
2791 break;
2792 case TGT_STRUCT:
2793 if (!processed(type))
2794 write_struct_tfs(file, type, name, tfsoff);
2795 break;
2796 case TGT_UNION:
2797 if (!processed(type))
2798 write_union_tfs(file, type, tfsoff);
2799 break;
2800 case TGT_ENUM:
2801 case TGT_BASIC:
2802 /* nothing to do */
2803 break;
2804 case TGT_CTXT_HANDLE:
2805 case TGT_CTXT_HANDLE_POINTER:
2806 case TGT_INVALID:
2807 error("invalid type %s for var %s\n", type->name, name);
2808 break;
2811 return retmask;
2814 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2815 type_pred_t pred, unsigned int *typeformat_offset)
2817 const var_t *var;
2818 const statement_t *stmt;
2820 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2822 const type_t *iface;
2823 const statement_t *stmt_func;
2825 if (stmt->type == STMT_LIBRARY)
2827 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2828 continue;
2830 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2831 continue;
2833 iface = stmt->u.type;
2834 if (!pred(iface))
2835 continue;
2837 current_iface = iface;
2838 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2840 const var_t *func = stmt_func->u.var;
2841 if (is_local(func->attrs)) continue;
2843 if (!is_void(type_function_get_rettype(func->type)))
2845 var_t v = *func;
2846 v.type = type_function_get_rettype(func->type);
2847 update_tfsoff(type_function_get_rettype(func->type),
2848 write_typeformatstring_var(
2849 file, 2, NULL,
2850 type_function_get_rettype(func->type),
2851 &v, FALSE, typeformat_offset),
2852 file);
2855 current_func = func;
2856 if (type_get_function_args(func->type))
2857 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2858 update_tfsoff(
2859 var->type,
2860 write_typeformatstring_var(
2861 file, 2, func, var->type, var,
2862 TRUE, typeformat_offset),
2863 file);
2867 return *typeformat_offset + 1;
2870 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2872 unsigned int typeformat_offset = 2;
2874 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2878 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2880 int indent = 0;
2882 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2883 print_file(file, indent, "{\n");
2884 indent++;
2885 print_file(file, indent, "0,\n");
2886 print_file(file, indent, "{\n");
2887 indent++;
2888 print_file(file, indent, "NdrFcShort(0x0),\n");
2890 set_all_tfswrite(TRUE);
2891 process_tfs(file, stmts, pred);
2893 print_file(file, indent, "0x0\n");
2894 indent--;
2895 print_file(file, indent, "}\n");
2896 indent--;
2897 print_file(file, indent, "};\n");
2898 print_file(file, indent, "\n");
2901 static unsigned int get_required_buffer_size_type(
2902 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2904 *alignment = 0;
2905 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2907 case TGT_USER_TYPE:
2909 const char *uname;
2910 const type_t *utype = get_user_type(type, &uname);
2911 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
2913 case TGT_BASIC:
2914 switch (get_basic_fc(type))
2916 case RPC_FC_BYTE:
2917 case RPC_FC_CHAR:
2918 case RPC_FC_USMALL:
2919 case RPC_FC_SMALL:
2920 *alignment = 4;
2921 return 1;
2923 case RPC_FC_WCHAR:
2924 case RPC_FC_USHORT:
2925 case RPC_FC_SHORT:
2926 *alignment = 4;
2927 return 2;
2929 case RPC_FC_ULONG:
2930 case RPC_FC_LONG:
2931 case RPC_FC_FLOAT:
2932 case RPC_FC_ERROR_STATUS_T:
2933 *alignment = 4;
2934 return 4;
2936 case RPC_FC_HYPER:
2937 case RPC_FC_DOUBLE:
2938 *alignment = 8;
2939 return 8;
2941 case RPC_FC_IGNORE:
2942 case RPC_FC_BIND_PRIMITIVE:
2943 return 0;
2945 default:
2946 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2947 get_basic_fc(type));
2948 return 0;
2950 break;
2952 case TGT_ENUM:
2953 switch (get_enum_fc(type))
2955 case RPC_FC_ENUM32:
2956 *alignment = 4;
2957 return 4;
2958 case RPC_FC_ENUM16:
2959 *alignment = 4;
2960 return 2;
2962 break;
2964 case TGT_STRUCT:
2965 if (get_struct_fc(type) == RPC_FC_STRUCT)
2967 if (!type_struct_get_fields(type)) return 0;
2968 return fields_memsize(type_struct_get_fields(type), alignment);
2970 break;
2972 case TGT_POINTER:
2973 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
2975 const type_t *ref = type_pointer_get_ref(type);
2976 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2978 case TGT_BASIC:
2979 case TGT_ENUM:
2980 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
2981 case TGT_STRUCT:
2982 if (get_struct_fc(ref) == RPC_FC_STRUCT)
2983 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
2984 break;
2985 case TGT_USER_TYPE:
2986 case TGT_CTXT_HANDLE:
2987 case TGT_CTXT_HANDLE_POINTER:
2988 case TGT_STRING:
2989 case TGT_POINTER:
2990 case TGT_ARRAY:
2991 case TGT_IFACE_POINTER:
2992 case TGT_UNION:
2993 case TGT_INVALID:
2994 break;
2997 break;
2999 case TGT_ARRAY:
3000 /* FIXME: depends on pointer type */
3001 return type_array_get_dim(type) *
3002 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3004 default:
3005 break;
3007 return 0;
3010 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3012 int in_attr = is_attr(var->attrs, ATTR_IN);
3013 int out_attr = is_attr(var->attrs, ATTR_OUT);
3015 if (!in_attr && !out_attr)
3016 in_attr = 1;
3018 *alignment = 0;
3020 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3021 pass == PASS_RETURN)
3023 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3025 *alignment = 4;
3026 return 20;
3029 if (!is_string_type(var->attrs, var->type))
3030 return get_required_buffer_size_type(var->type, var->name,
3031 var->attrs, TRUE, alignment);
3033 return 0;
3036 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3038 const var_t *var;
3039 unsigned int total_size = 0, alignment;
3041 if (type_get_function_args(func->type))
3043 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3045 total_size += get_required_buffer_size(var, &alignment, pass);
3046 total_size += alignment;
3050 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3052 var_t v = *func;
3053 v.type = type_function_get_rettype(func->type);
3054 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3055 total_size += alignment;
3057 return total_size;
3060 static void print_phase_function(FILE *file, int indent, const char *type,
3061 const char *local_var_prefix, enum remoting_phase phase,
3062 const var_t *var, unsigned int type_offset)
3064 const char *function;
3065 switch (phase)
3067 case PHASE_BUFFERSIZE:
3068 function = "BufferSize";
3069 break;
3070 case PHASE_MARSHAL:
3071 function = "Marshall";
3072 break;
3073 case PHASE_UNMARSHAL:
3074 function = "Unmarshall";
3075 break;
3076 case PHASE_FREE:
3077 function = "Free";
3078 break;
3079 default:
3080 assert(0);
3081 return;
3084 print_file(file, indent, "Ndr%s%s(\n", type, function);
3085 indent++;
3086 print_file(file, indent, "&__frame->_StubMsg,\n");
3087 print_file(file, indent, "%s%s%s%s%s,\n",
3088 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3089 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3090 local_var_prefix,
3091 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3092 var->name);
3093 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3094 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3095 if (phase == PHASE_UNMARSHAL)
3096 print_file(file, indent, "0);\n");
3097 indent--;
3100 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3101 enum remoting_phase phase, enum pass pass, const var_t *var,
3102 const char *varname)
3104 type_t *type = var->type;
3105 unsigned int size;
3106 unsigned int alignment = 0;
3107 const type_t *ref;
3109 /* no work to do for other phases, buffer sizing is done elsewhere */
3110 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3111 return;
3113 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3114 if (type_get_type(ref) == TYPE_ENUM)
3116 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3118 size = 4;
3119 alignment = 4;
3121 else /* RPC_FC_ENUM16 */
3123 size = 2;
3124 alignment = 2;
3127 else
3129 switch (get_basic_fc(ref))
3131 case RPC_FC_BYTE:
3132 case RPC_FC_CHAR:
3133 case RPC_FC_SMALL:
3134 case RPC_FC_USMALL:
3135 size = 1;
3136 alignment = 1;
3137 break;
3139 case RPC_FC_WCHAR:
3140 case RPC_FC_USHORT:
3141 case RPC_FC_SHORT:
3142 size = 2;
3143 alignment = 2;
3144 break;
3146 case RPC_FC_ULONG:
3147 case RPC_FC_LONG:
3148 case RPC_FC_FLOAT:
3149 case RPC_FC_ERROR_STATUS_T:
3150 size = 4;
3151 alignment = 4;
3152 break;
3154 case RPC_FC_HYPER:
3155 case RPC_FC_DOUBLE:
3156 size = 8;
3157 alignment = 8;
3158 break;
3160 case RPC_FC_IGNORE:
3161 case RPC_FC_BIND_PRIMITIVE:
3162 /* no marshalling needed */
3163 return;
3165 default:
3166 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3167 var->name, get_basic_fc(ref));
3168 size = 0;
3172 if (phase == PHASE_MARSHAL)
3173 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3174 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3175 alignment - 1, alignment - 1);
3177 if (phase == PHASE_MARSHAL)
3179 print_file(file, indent, "*(");
3180 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3181 if (is_ptr(type))
3182 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3183 else
3184 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3185 fprintf(file, "%s%s", local_var_prefix, varname);
3186 fprintf(file, ";\n");
3188 else if (phase == PHASE_UNMARSHAL)
3190 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3191 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3192 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3193 print_file(file, indent, "{\n");
3194 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3195 print_file(file, indent, "}\n");
3196 print_file(file, indent, "%s%s%s",
3197 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3198 local_var_prefix, varname);
3199 if (pass == PASS_IN && is_ptr(type))
3200 fprintf(file, " = (");
3201 else
3202 fprintf(file, " = *(");
3203 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3204 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3207 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3208 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3209 fprintf(file, ");\n");
3212 /* returns whether the MaxCount, Offset or ActualCount members need to be
3213 * filled in for the specified phase */
3214 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3216 return (phase != PHASE_UNMARSHAL);
3219 expr_t *get_size_is_expr(const type_t *t, const char *name)
3221 expr_t *x = NULL;
3223 for ( ; is_array(t); t = type_array_get_element(t))
3224 if (type_array_has_conformance(t))
3226 if (!x)
3227 x = type_array_get_conformance(t);
3228 else
3229 error("%s: multidimensional conformant"
3230 " arrays not supported at the top level\n",
3231 name);
3234 return x;
3237 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3238 enum remoting_phase phase, const var_t *var)
3240 const type_t *type = var->type;
3241 /* get fundamental type for the argument */
3242 for (;;)
3244 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3245 break;
3246 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3247 break;
3248 else if (type_is_alias(type))
3249 type = type_alias_get_aliasee(type);
3250 else if (is_array(type))
3252 if (is_conformance_needed_for_phase(phase) && is_array(type))
3254 if (type_array_has_conformance(type))
3256 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3257 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3258 fprintf(file, ";\n\n");
3260 if (type_array_has_variance(type))
3262 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3263 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3264 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3265 fprintf(file, ";\n\n");
3268 break;
3270 else if (type_get_type(type) == TYPE_UNION)
3272 if (is_conformance_needed_for_phase(phase))
3274 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3275 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3276 fprintf(file, ";\n\n");
3278 break;
3280 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3282 expr_t *iid;
3284 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3286 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3287 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3288 fprintf( file, ";\n\n" );
3290 break;
3292 else if (is_ptr(type))
3293 type = type_pointer_get_ref(type);
3294 else
3295 break;
3299 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3300 enum pass pass, enum remoting_phase phase, const var_t *var)
3302 int in_attr, out_attr, pointer_type;
3303 const type_t *type = var->type;
3304 unsigned int start_offset = type->typestring_offset;
3306 if (is_ptr(type) || is_array(type))
3307 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3308 else
3309 pointer_type = 0;
3311 in_attr = is_attr(var->attrs, ATTR_IN);
3312 out_attr = is_attr(var->attrs, ATTR_OUT);
3313 if (!in_attr && !out_attr)
3314 in_attr = 1;
3316 if (phase != PHASE_FREE)
3317 switch (pass)
3319 case PASS_IN:
3320 if (!in_attr) return;
3321 break;
3322 case PASS_OUT:
3323 if (!out_attr) return;
3324 break;
3325 case PASS_RETURN:
3326 break;
3329 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3331 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3333 case TGT_CTXT_HANDLE:
3334 case TGT_CTXT_HANDLE_POINTER:
3335 if (phase == PHASE_MARSHAL)
3337 if (pass == PASS_IN)
3339 /* if the context_handle attribute appears in the chain of types
3340 * without pointers being followed, then the context handle must
3341 * be direct, otherwise it is a pointer */
3342 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3343 print_file(file, indent, "NdrClientContextMarshall(\n");
3344 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3345 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3346 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3348 else
3350 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3351 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3352 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3353 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3354 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3357 else if (phase == PHASE_UNMARSHAL)
3359 if (pass == PASS_OUT)
3361 if (!in_attr)
3362 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3363 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3364 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3365 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3366 print_file(file, indent + 1, "__frame->_Handle);\n");
3368 else
3370 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3371 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3372 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3375 break;
3376 case TGT_USER_TYPE:
3377 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3378 break;
3379 case TGT_STRING:
3380 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3381 pointer_type != RPC_FC_RP)
3383 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3384 !in_attr && is_conformant_array(type))
3386 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3387 indent++;
3388 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3390 /* strings returned are assumed to be global and hence don't
3391 * need freeing */
3392 else if (is_declptr(type) &&
3393 !(phase == PHASE_FREE && pass == PASS_RETURN))
3394 print_phase_function(file, indent, "Pointer", local_var_prefix,
3395 phase, var, start_offset);
3397 else
3399 unsigned int real_start_offset = start_offset;
3400 /* skip over pointer description straight to string description */
3401 if (is_declptr(type))
3403 if (is_conformant_array(type))
3404 real_start_offset += 4;
3405 else
3406 real_start_offset += 2;
3408 if (is_array(type) && !is_conformant_array(type))
3409 print_phase_function(file, indent, "NonConformantString",
3410 local_var_prefix, phase, var,
3411 real_start_offset);
3412 else
3413 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3414 phase, var, real_start_offset);
3416 break;
3417 case TGT_ARRAY:
3419 unsigned char tc = get_array_fc(type);
3420 const char *array_type = "FixedArray";
3422 /* We already have the size_is expression since it's at the
3423 top level, but do checks for multidimensional conformant
3424 arrays. When we handle them, we'll need to extend this
3425 function to return a list, and then we'll actually use
3426 the return value. */
3427 get_size_is_expr(type, var->name);
3429 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3431 array_type = "VaryingArray";
3433 else if (tc == RPC_FC_CARRAY)
3435 array_type = "ConformantArray";
3437 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3439 array_type = (tc == RPC_FC_BOGUS_ARRAY
3440 ? "ComplexArray"
3441 : "ConformantVaryingArray");
3444 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3445 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3446 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3448 /* these are all unmarshalled by allocating memory */
3449 if (tc == RPC_FC_BOGUS_ARRAY ||
3450 tc == RPC_FC_CVARRAY ||
3451 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3452 (tc == RPC_FC_CARRAY && !in_attr))
3454 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3455 indent++;
3456 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3459 break;
3461 case TGT_BASIC:
3462 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3463 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3464 break;
3465 case TGT_ENUM:
3466 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3468 if (phase == PHASE_MARSHAL)
3469 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3470 else
3471 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3472 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3473 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3474 local_var_prefix,
3475 var->name);
3476 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3478 break;
3479 case TGT_STRUCT:
3480 switch (get_struct_fc(type))
3482 case RPC_FC_STRUCT:
3483 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3484 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3485 break;
3486 case RPC_FC_PSTRUCT:
3487 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3488 break;
3489 case RPC_FC_CSTRUCT:
3490 case RPC_FC_CPSTRUCT:
3491 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3492 break;
3493 case RPC_FC_CVSTRUCT:
3494 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3495 break;
3496 case RPC_FC_BOGUS_STRUCT:
3497 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3498 break;
3499 default:
3500 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3502 break;
3503 case TGT_UNION:
3505 const char *union_type = NULL;
3507 if (type_get_type(type) == TYPE_UNION)
3508 union_type = "NonEncapsulatedUnion";
3509 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3510 union_type = "EncapsulatedUnion";
3512 print_phase_function(file, indent, union_type, local_var_prefix,
3513 phase, var, start_offset);
3514 break;
3516 case TGT_POINTER:
3518 const type_t *ref = type_pointer_get_ref(type);
3519 if (pointer_type == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3521 case TYPE_BASIC:
3522 /* base types have known sizes, so don't need a sizing pass
3523 * and don't have any memory to free and so don't need a
3524 * freeing pass */
3525 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3526 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3527 break;
3528 case TYPE_ENUM:
3529 /* base types have known sizes, so don't need a sizing pass
3530 * and don't have any memory to free and so don't need a
3531 * freeing pass */
3532 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3533 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3534 break;
3535 case TYPE_STRUCT:
3537 const char *struct_type = NULL;
3538 switch (get_struct_fc(ref))
3540 case RPC_FC_STRUCT:
3541 /* simple structs have known sizes, so don't need a sizing
3542 * pass and don't have any memory to free and so don't
3543 * need a freeing pass */
3544 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3545 struct_type = "SimpleStruct";
3546 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3548 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3549 indent++;
3550 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3551 indent--;
3553 break;
3554 case RPC_FC_PSTRUCT:
3555 struct_type = "SimpleStruct";
3556 break;
3557 case RPC_FC_CSTRUCT:
3558 case RPC_FC_CPSTRUCT:
3559 struct_type = "ConformantStruct";
3560 break;
3561 case RPC_FC_CVSTRUCT:
3562 struct_type = "ConformantVaryingStruct";
3563 break;
3564 case RPC_FC_BOGUS_STRUCT:
3565 struct_type = "ComplexStruct";
3566 break;
3567 default:
3568 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3571 if (struct_type)
3573 if (phase == PHASE_FREE)
3574 struct_type = "Pointer";
3575 else
3576 start_offset = ref->typestring_offset;
3577 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3579 break;
3581 case TYPE_UNION:
3582 case TYPE_ENCAPSULATED_UNION:
3584 const char *union_type = NULL;
3585 if (phase == PHASE_FREE)
3586 union_type = "Pointer";
3587 else
3589 if (type_get_type(ref) == TYPE_UNION)
3590 union_type = "NonEncapsulatedUnion";
3591 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3592 union_type = "EncapsulatedUnion";
3594 start_offset = ref->typestring_offset;
3597 print_phase_function(file, indent, union_type, local_var_prefix,
3598 phase, var, start_offset);
3599 break;
3601 case TYPE_POINTER:
3602 case TYPE_ARRAY:
3603 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3604 break;
3605 case TYPE_VOID:
3606 case TYPE_ALIAS:
3607 case TYPE_MODULE:
3608 case TYPE_COCLASS:
3609 case TYPE_FUNCTION:
3610 case TYPE_INTERFACE:
3611 assert(0);
3612 break;
3614 else
3615 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3616 break;
3618 case TGT_IFACE_POINTER:
3619 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3620 break;
3621 case TGT_INVALID:
3622 assert(0);
3623 break;
3625 fprintf(file, "\n");
3628 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3629 enum pass pass, enum remoting_phase phase)
3631 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3633 unsigned int size = get_function_buffer_size( func, pass );
3634 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3637 if (pass == PASS_RETURN)
3639 var_t var;
3640 var = *func;
3641 var.type = type_function_get_rettype(func->type);
3642 var.name = xstrdup( "_RetVal" );
3643 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3644 free( var.name );
3646 else
3648 const var_t *var;
3649 if (!type_get_function_args(func->type))
3650 return;
3651 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3652 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3657 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3659 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3663 unsigned int get_size_procformatstring_func(const var_t *func)
3665 const var_t *var;
3666 unsigned int size = 0;
3668 /* argument list size */
3669 if (type_get_function_args(func->type))
3670 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3671 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3673 /* return value size */
3674 if (is_void(type_function_get_rettype(func->type)))
3675 size += 2; /* FC_END and FC_PAD */
3676 else
3677 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3679 return size;
3682 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3684 const statement_t *stmt;
3685 unsigned int size = 1;
3687 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3689 const type_t *iface;
3690 const statement_t *stmt_func;
3692 if (stmt->type == STMT_LIBRARY)
3694 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3695 continue;
3697 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3698 continue;
3700 iface = stmt->u.type;
3701 if (!pred(iface))
3702 continue;
3704 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3706 const var_t *func = stmt_func->u.var;
3707 if (!is_local(func->attrs))
3708 size += get_size_procformatstring_func( func );
3711 return size;
3714 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3716 set_all_tfswrite(FALSE);
3717 return process_tfs(NULL, stmts, pred);
3720 void declare_stub_args( FILE *file, int indent, const var_t *func )
3722 int in_attr, out_attr;
3723 int i = 0;
3724 const var_t *var;
3726 /* declare return value '_RetVal' */
3727 if (!is_void(type_function_get_rettype(func->type)))
3729 print_file(file, indent, "%s", "");
3730 write_type_decl_left(file, type_function_get_rettype(func->type));
3731 fprintf(file, " _RetVal;\n");
3734 if (!type_get_function_args(func->type))
3735 return;
3737 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3739 in_attr = is_attr(var->attrs, ATTR_IN);
3740 out_attr = is_attr(var->attrs, ATTR_OUT);
3741 if (!out_attr && !in_attr)
3742 in_attr = 1;
3744 if (is_context_handle(var->type))
3745 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3746 else
3748 if (!in_attr && !is_conformant_array(var->type))
3750 type_t *type_to_print;
3751 char name[16];
3752 print_file(file, indent, "%s", "");
3753 if (type_get_type(var->type) == TYPE_ARRAY &&
3754 !type_array_is_decl_as_ptr(var->type))
3755 type_to_print = var->type;
3756 else
3757 type_to_print = type_pointer_get_ref(var->type);
3758 sprintf(name, "_W%u", i++);
3759 write_type_decl(file, type_to_print, name);
3760 fprintf(file, ";\n");
3763 print_file(file, indent, "%s", "");
3764 write_type_decl_left(file, var->type);
3765 fprintf(file, " ");
3766 if (type_get_type(var->type) == TYPE_ARRAY &&
3767 !type_array_is_decl_as_ptr(var->type)) {
3768 fprintf(file, "(*%s)", var->name);
3769 } else
3770 fprintf(file, "%s", var->name);
3771 write_type_right(file, var->type, FALSE);
3772 fprintf(file, ";\n");
3774 if (decl_indirect(var->type))
3775 print_file(file, indent, "void *_p_%s;\n", var->name);
3781 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3783 int in_attr, out_attr;
3784 int i = 0, sep = 0;
3785 const var_t *var;
3787 if (!type_get_function_args(func->type))
3788 return;
3790 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3792 in_attr = is_attr(var->attrs, ATTR_IN);
3793 out_attr = is_attr(var->attrs, ATTR_OUT);
3794 if (!out_attr && !in_attr)
3795 in_attr = 1;
3797 if (!in_attr)
3799 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3801 if (is_context_handle(var->type))
3803 fprintf(file, " = NdrContextHandleInitialize(\n");
3804 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3805 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3806 var->type->typestring_offset);
3808 else if (is_array(var->type) &&
3809 type_array_has_conformance(var->type))
3811 unsigned int size, align = 0;
3812 type_t *type = var->type;
3814 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3815 for ( ;
3816 is_array(type) && type_array_has_conformance(type);
3817 type = type_array_get_element(type))
3819 write_expr(file, type_array_get_conformance(type), TRUE,
3820 TRUE, NULL, NULL, local_var_prefix);
3821 fprintf(file, " * ");
3823 size = type_memsize(type, &align);
3824 fprintf(file, "%u);\n", size);
3826 else
3828 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3829 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3831 case TGT_BASIC:
3832 case TGT_ENUM:
3833 case TGT_POINTER:
3834 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3835 break;
3836 case TGT_STRUCT:
3837 case TGT_UNION:
3838 case TGT_USER_TYPE:
3839 case TGT_IFACE_POINTER:
3840 case TGT_ARRAY:
3841 case TGT_CTXT_HANDLE:
3842 case TGT_CTXT_HANDLE_POINTER:
3843 case TGT_INVALID:
3844 case TGT_STRING:
3845 /* not initialised */
3846 break;
3848 i++;
3851 sep = 1;
3854 if (sep)
3855 fprintf(file, "\n");
3859 int write_expr_eval_routines(FILE *file, const char *iface)
3861 static const char *var_name = "pS";
3862 static const char *var_name_expr = "pS->";
3863 int result = 0;
3864 struct expr_eval_routine *eval;
3865 unsigned short callback_offset = 0;
3867 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3869 const char *name = eval->structure->name;
3870 result = 1;
3872 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3873 iface, name, callback_offset);
3874 print_file(file, 0, "{\n");
3875 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3876 name, var_name, name, eval->baseoff);
3877 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3878 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3879 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3880 fprintf(file, ";\n");
3881 print_file(file, 0, "}\n\n");
3882 callback_offset++;
3884 return result;
3887 void write_expr_eval_routine_list(FILE *file, const char *iface)
3889 struct expr_eval_routine *eval;
3890 struct expr_eval_routine *cursor;
3891 unsigned short callback_offset = 0;
3893 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3894 fprintf(file, "{\n");
3896 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3898 const char *name = eval->structure->name;
3899 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3900 callback_offset++;
3901 list_remove(&eval->entry);
3902 free(eval);
3905 fprintf(file, "};\n\n");
3908 void write_user_quad_list(FILE *file)
3910 user_type_t *ut;
3912 if (list_empty(&user_type_list))
3913 return;
3915 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3916 fprintf(file, "{\n");
3917 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3919 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3920 print_file(file, 1, "{\n");
3921 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3922 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3923 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3924 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3925 print_file(file, 1, "}%s\n", sep);
3927 fprintf(file, "};\n\n");
3930 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3932 const struct str_list_entry_t *endpoint;
3933 const char *p;
3935 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3936 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3937 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3939 print_file( f, 1, "{ (const unsigned char *)\"" );
3940 for (p = endpoint->str; *p && *p != ':'; p++)
3942 if (*p == '"' || *p == '\\') fputc( '\\', f );
3943 fputc( *p, f );
3945 if (!*p) goto error;
3946 if (p[1] != '[') goto error;
3948 fprintf( f, "\", (const unsigned char *)\"" );
3949 for (p += 2; *p && *p != ']'; p++)
3951 if (*p == '"' || *p == '\\') fputc( '\\', f );
3952 fputc( *p, f );
3954 if (*p != ']') goto error;
3955 fprintf( f, "\" },\n" );
3957 print_file( f, 0, "};\n\n" );
3958 return;
3960 error:
3961 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3964 void write_exceptions( FILE *file )
3966 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3967 fprintf( file, "\n");
3968 fprintf( file, "#include \"wine/exception.h\"\n");
3969 fprintf( file, "#undef RpcTryExcept\n");
3970 fprintf( file, "#undef RpcExcept\n");
3971 fprintf( file, "#undef RpcEndExcept\n");
3972 fprintf( file, "#undef RpcTryFinally\n");
3973 fprintf( file, "#undef RpcFinally\n");
3974 fprintf( file, "#undef RpcEndFinally\n");
3975 fprintf( file, "#undef RpcExceptionCode\n");
3976 fprintf( file, "#undef RpcAbnormalTermination\n");
3977 fprintf( file, "\n");
3978 fprintf( file, "struct __exception_frame;\n");
3979 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3980 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3981 fprintf( file, "\n");
3982 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3983 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3984 fprintf( file, " __filter_func filter; \\\n");
3985 fprintf( file, " __finally_func finally; \\\n");
3986 fprintf( file, " sigjmp_buf jmp; \\\n");
3987 fprintf( file, " DWORD code; \\\n");
3988 fprintf( file, " unsigned char abnormal_termination; \\\n");
3989 fprintf( file, " unsigned char filter_level; \\\n");
3990 fprintf( file, " unsigned char finally_level;\n");
3991 fprintf( file, "\n");
3992 fprintf( file, "struct __exception_frame\n{\n");
3993 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3994 fprintf( file, "};\n");
3995 fprintf( file, "\n");
3996 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
3997 fprintf( file, "{\n");
3998 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
3999 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4000 fprintf( file, " {\n");
4001 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4002 fprintf( file, " exc_frame->finally( exc_frame );\n");
4003 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4004 fprintf( file, " }\n");
4005 fprintf( file, " exc_frame->filter_level = 0;\n");
4006 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4007 fprintf( file, "}\n");
4008 fprintf( file, "\n");
4009 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4010 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4011 fprintf( file, " CONTEXT *context,\n");
4012 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4013 fprintf( file, "{\n");
4014 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4015 fprintf( file, "\n");
4016 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4017 fprintf( file, " {\n" );
4018 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4019 fprintf( file, " {\n" );
4020 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4021 fprintf( file, " exc_frame->finally( exc_frame );\n");
4022 fprintf( file, " }\n" );
4023 fprintf( file, " return ExceptionContinueSearch;\n");
4024 fprintf( file, " }\n" );
4025 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4026 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4027 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4028 fprintf( file, " return ExceptionContinueSearch;\n");
4029 fprintf( file, "}\n");
4030 fprintf( file, "\n");
4031 fprintf( file, "#define RpcTryExcept \\\n");
4032 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4033 fprintf( file, " { \\\n");
4034 fprintf( file, " if (!__frame->finally_level) \\\n" );
4035 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4036 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4037 fprintf( file, "\n");
4038 fprintf( file, "#define RpcExcept(expr) \\\n");
4039 fprintf( file, " if (!__frame->finally_level) \\\n" );
4040 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4041 fprintf( file, " __frame->filter_level = 0; \\\n" );
4042 fprintf( file, " } \\\n");
4043 fprintf( file, " else \\\n");
4044 fprintf( file, "\n");
4045 fprintf( file, "#define RpcEndExcept\n");
4046 fprintf( file, "\n");
4047 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4048 fprintf( file, "\n");
4049 fprintf( file, "#define RpcTryFinally \\\n");
4050 fprintf( file, " if (!__frame->filter_level) \\\n");
4051 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4052 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4053 fprintf( file, "\n");
4054 fprintf( file, "#define RpcFinally \\\n");
4055 fprintf( file, " if (!__frame->filter_level) \\\n");
4056 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4057 fprintf( file, " __frame->finally_level = 0;\n");
4058 fprintf( file, "\n");
4059 fprintf( file, "#define RpcEndFinally\n");
4060 fprintf( file, "\n");
4061 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4062 fprintf( file, "\n");
4063 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4064 fprintf( file, " do { \\\n");
4065 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4066 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4067 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4068 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4069 fprintf( file, " __frame->filter_level = 0; \\\n");
4070 fprintf( file, " __frame->finally_level = 0; \\\n");
4071 fprintf( file, " } while (0)\n");
4072 fprintf( file, "\n");
4073 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4074 fprintf( file, "\n");
4075 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4076 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4077 fprintf( file, "\n");
4078 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4079 fprintf( file, " DWORD code;\n");
4080 fprintf( file, "\n");
4081 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");