widl: Move declarray property to array_details.
[wine/multimedia.git] / tools / widl / typegen.c
blobae46338c0079432ee888bba61ed06cf163ab4c8b
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,
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_pointer_fc(const type_t *type)
125 assert(is_ptr(type));
126 /* FIXME: see corresponding hack in set_type - we shouldn't be getting
127 * the pointer type from an alias, rather determining it from the
128 * position */
129 return type->type;
132 static unsigned char get_enum_fc(const type_t *type)
134 assert(type_get_type(type) == TYPE_ENUM);
135 if (is_aliaschain_attr(type, ATTR_V1ENUM))
136 return RPC_FC_ENUM32;
137 else
138 return RPC_FC_ENUM16;
141 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
143 if (is_user_type(type))
144 return TGT_USER_TYPE;
146 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
147 return TGT_CTXT_HANDLE;
149 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
150 return TGT_STRING;
152 switch (type_get_type(type))
154 case TYPE_BASIC:
155 return TGT_BASIC;
156 case TYPE_ENUM:
157 return TGT_ENUM;
158 case TYPE_POINTER:
159 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
160 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
161 return TGT_IFACE_POINTER;
162 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
163 return TGT_CTXT_HANDLE_POINTER;
164 else
165 return TGT_POINTER;
166 case TYPE_STRUCT:
167 return TGT_STRUCT;
168 case TYPE_ENCAPSULATED_UNION:
169 case TYPE_UNION:
170 return TGT_UNION;
171 case TYPE_ARRAY:
172 return TGT_ARRAY;
173 case TYPE_FUNCTION:
174 case TYPE_COCLASS:
175 case TYPE_INTERFACE:
176 case TYPE_MODULE:
177 case TYPE_VOID:
178 case TYPE_ALIAS:
179 break;
181 return TGT_INVALID;
184 unsigned char get_struct_fc(const type_t *type)
186 int has_pointer = 0;
187 int has_conformance = 0;
188 int has_variance = 0;
189 var_t *field;
190 var_list_t *fields;
192 fields = type_struct_get_fields(type);
194 if (get_padding(fields))
195 return RPC_FC_BOGUS_STRUCT;
197 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
199 type_t *t = field->type;
200 enum typegen_type typegen_type;
202 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
204 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
206 if (is_string_type(field->attrs, field->type))
208 if (is_conformant_array(t))
209 has_conformance = 1;
210 has_variance = 1;
211 continue;
214 if (is_array(type_array_get_element(field->type)))
215 return RPC_FC_BOGUS_STRUCT;
217 if (type_array_has_conformance(field->type))
219 has_conformance = 1;
220 if (list_next(fields, &field->entry))
221 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
222 field->name);
224 if (type_array_has_variance(t))
225 has_variance = 1;
227 t = type_array_get_element(t);
228 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
231 switch (typegen_type)
233 case TGT_USER_TYPE:
234 case TGT_IFACE_POINTER:
235 return RPC_FC_BOGUS_STRUCT;
236 case TGT_BASIC:
237 break;
238 case TGT_ENUM:
239 if (get_enum_fc(t) == RPC_FC_ENUM16)
240 return RPC_FC_BOGUS_STRUCT;
241 break;
242 case TGT_POINTER:
243 if (get_pointer_fc(t) == RPC_FC_RP || pointer_size != 4)
244 return RPC_FC_BOGUS_STRUCT;
245 has_pointer = 1;
246 break;
247 case TGT_ARRAY:
249 unsigned int ptr_type = get_attrv(field->attrs, ATTR_POINTERTYPE);
250 if (!ptr_type || ptr_type == RPC_FC_RP)
251 return RPC_FC_BOGUS_STRUCT;
252 else if (pointer_size != 4)
253 return RPC_FC_BOGUS_STRUCT;
254 has_pointer = 1;
255 break;
257 case TGT_UNION:
258 return RPC_FC_BOGUS_STRUCT;
259 case TGT_STRUCT:
261 unsigned char fc = get_struct_fc(t);
262 switch (fc)
264 case RPC_FC_STRUCT:
265 break;
266 case RPC_FC_CVSTRUCT:
267 has_conformance = 1;
268 has_variance = 1;
269 has_pointer = 1;
270 break;
272 case RPC_FC_CPSTRUCT:
273 has_conformance = 1;
274 if (list_next( fields, &field->entry ))
275 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
276 field->name);
277 has_pointer = 1;
278 break;
280 case RPC_FC_CSTRUCT:
281 has_conformance = 1;
282 if (list_next( fields, &field->entry ))
283 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
284 field->name);
285 break;
287 case RPC_FC_PSTRUCT:
288 has_pointer = 1;
289 break;
291 default:
292 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
293 /* fallthru - treat it as complex */
295 /* as soon as we see one of these these members, it's bogus... */
296 case RPC_FC_BOGUS_STRUCT:
297 return RPC_FC_BOGUS_STRUCT;
299 break;
301 case TGT_STRING:
302 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
303 case TGT_INVALID:
304 case TGT_CTXT_HANDLE:
305 case TGT_CTXT_HANDLE_POINTER:
306 /* checking after parsing should mean that we don't get here. if we do,
307 * it's a checker bug */
308 assert(0);
312 if( has_variance )
314 if ( has_conformance )
315 return RPC_FC_CVSTRUCT;
316 else
317 return RPC_FC_BOGUS_STRUCT;
319 if( has_conformance && has_pointer )
320 return RPC_FC_CPSTRUCT;
321 if( has_conformance )
322 return RPC_FC_CSTRUCT;
323 if( has_pointer )
324 return RPC_FC_PSTRUCT;
325 return RPC_FC_STRUCT;
328 unsigned char get_array_fc(const type_t *type)
330 unsigned char fc;
331 const expr_t *size_is;
332 const type_t *elem_type;
334 elem_type = type_array_get_element(type);
335 size_is = type_array_get_conformance(type);
337 if (!size_is)
339 unsigned int align = 0;
340 unsigned int size = type_memsize(elem_type, &align);
341 if (size * type_array_get_dim(type) > 0xffffuL)
342 fc = RPC_FC_LGFARRAY;
343 else
344 fc = RPC_FC_SMFARRAY;
346 else
347 fc = RPC_FC_CARRAY;
349 if (type_array_has_variance(type))
351 if (fc == RPC_FC_SMFARRAY)
352 fc = RPC_FC_SMVARRAY;
353 else if (fc == RPC_FC_LGFARRAY)
354 fc = RPC_FC_LGVARRAY;
355 else if (fc == RPC_FC_CARRAY)
356 fc = RPC_FC_CVARRAY;
359 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
361 case TGT_USER_TYPE:
362 fc = RPC_FC_BOGUS_ARRAY;
363 break;
364 case TGT_STRUCT:
365 switch (get_struct_fc(elem_type))
367 case RPC_FC_BOGUS_STRUCT:
368 fc = RPC_FC_BOGUS_ARRAY;
369 break;
371 break;
372 case TGT_ENUM:
373 /* is 16-bit enum - if so, wire size differs from mem size and so
374 * the array cannot be block copied, which means the array is complex */
375 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
376 fc = RPC_FC_BOGUS_ARRAY;
377 break;
378 case TGT_UNION:
379 case TGT_IFACE_POINTER:
380 fc = RPC_FC_BOGUS_ARRAY;
381 break;
382 case TGT_POINTER:
383 /* ref pointers cannot just be block copied. unique pointers to
384 * interfaces need special treatment. either case means the array is
385 * complex */
386 if (get_pointer_fc(elem_type) == RPC_FC_RP)
387 fc = RPC_FC_BOGUS_ARRAY;
388 break;
389 case TGT_BASIC:
390 case TGT_CTXT_HANDLE:
391 case TGT_CTXT_HANDLE_POINTER:
392 case TGT_STRING:
393 case TGT_INVALID:
394 case TGT_ARRAY:
395 /* nothing to do for everything else */
396 break;
399 return fc;
402 int is_struct(unsigned char type)
404 switch (type)
406 case RPC_FC_STRUCT:
407 case RPC_FC_PSTRUCT:
408 case RPC_FC_CSTRUCT:
409 case RPC_FC_CPSTRUCT:
410 case RPC_FC_CVSTRUCT:
411 case RPC_FC_BOGUS_STRUCT:
412 return 1;
413 default:
414 return 0;
418 static int is_non_complex_struct(const type_t *type)
420 return (type_get_type(type) == TYPE_STRUCT &&
421 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
424 static int type_has_pointers(const type_t *type)
426 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
428 case TGT_USER_TYPE:
429 return FALSE;
430 case TGT_POINTER:
431 return TRUE;
432 case TGT_ARRAY:
433 /* FIXME: array can be pointer */
434 return type_has_pointers(type_array_get_element(type));
435 case TGT_STRUCT:
437 var_list_t *fields = type_struct_get_fields(type);
438 const var_t *field;
439 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
441 if (type_has_pointers(field->type))
442 return TRUE;
444 break;
446 case TGT_UNION:
448 var_list_t *fields;
449 const var_t *field;
450 fields = type_union_get_cases(type);
451 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
453 if (field->type && type_has_pointers(field->type))
454 return TRUE;
456 break;
458 case TGT_CTXT_HANDLE:
459 case TGT_CTXT_HANDLE_POINTER:
460 case TGT_STRING:
461 case TGT_IFACE_POINTER:
462 case TGT_BASIC:
463 case TGT_ENUM:
464 case TGT_INVALID:
465 break;
468 return FALSE;
471 static int type_has_full_pointer(const type_t *type)
473 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
475 case TGT_USER_TYPE:
476 return FALSE;
477 case TGT_POINTER:
478 if (get_pointer_fc(type) == RPC_FC_FP)
479 return TRUE;
480 else
481 return FALSE;
482 case TGT_ARRAY:
483 /* FIXME: array can be full pointer */
484 return type_has_full_pointer(type_array_get_element(type));
485 case TGT_STRUCT:
487 var_list_t *fields = type_struct_get_fields(type);
488 const var_t *field;
489 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
491 if (type_has_full_pointer(field->type))
492 return TRUE;
494 break;
496 case TGT_UNION:
498 var_list_t *fields;
499 const var_t *field;
500 fields = type_union_get_cases(type);
501 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
503 if (field->type && type_has_full_pointer(field->type))
504 return TRUE;
506 break;
508 case TGT_CTXT_HANDLE:
509 case TGT_CTXT_HANDLE_POINTER:
510 case TGT_STRING:
511 case TGT_IFACE_POINTER:
512 case TGT_BASIC:
513 case TGT_ENUM:
514 case TGT_INVALID:
515 break;
518 return FALSE;
521 static unsigned short user_type_offset(const char *name)
523 user_type_t *ut;
524 unsigned short off = 0;
525 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
527 if (strcmp(name, ut->name) == 0)
528 return off;
529 ++off;
531 error("user_type_offset: couldn't find type (%s)\n", name);
532 return 0;
535 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
537 type->typestring_offset = offset;
538 if (file) type->tfswrite = FALSE;
541 static void guard_rec(type_t *type)
543 /* types that contain references to themselves (like a linked list),
544 need to be shielded from infinite recursion when writing embedded
545 types */
546 if (type->typestring_offset)
547 type->tfswrite = FALSE;
548 else
549 type->typestring_offset = 1;
552 static type_t *get_user_type(const type_t *t, const char **pname)
554 for (;;)
556 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
557 if (ut)
559 if (pname)
560 *pname = t->name;
561 return ut;
564 if (type_is_alias(t))
565 t = type_alias_get_aliasee(t);
566 else
567 return 0;
571 int is_user_type(const type_t *t)
573 return get_user_type(t, NULL) != NULL;
576 static int is_embedded_complex(const type_t *type)
578 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
580 case TGT_USER_TYPE:
581 case TGT_STRUCT:
582 case TGT_UNION:
583 case TGT_ARRAY:
584 case TGT_IFACE_POINTER:
585 return TRUE;
586 default:
587 return FALSE;
591 static const char *get_context_handle_type_name(const type_t *type)
593 const type_t *t;
594 for (t = type;
595 is_ptr(t) || type_is_alias(t);
596 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
597 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
598 return t->name;
599 assert(0);
600 return NULL;
603 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
604 do { \
605 if (file) \
606 fprintf(file, "/* %2u */\n", typestring_offset); \
607 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
609 while (0)
611 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
612 static void print_file(FILE *file, int indent, const char *format, ...)
614 va_list va;
615 va_start(va, format);
616 print(file, indent, format, va);
617 va_end(va);
620 void print(FILE *file, int indent, const char *format, va_list va)
622 if (file)
624 if (format[0] != '\n')
625 while (0 < indent--)
626 fprintf(file, " ");
627 vfprintf(file, format, va);
632 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
634 if (decl_indirect(t))
636 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
637 local_var_prefix, n, local_var_prefix, n);
638 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
640 else if (is_ptr(t) || is_array(t))
641 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
644 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
646 const var_t *var;
648 if (!is_void(type_function_get_rettype(func->type)))
649 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
651 if (!type_get_function_args(func->type))
652 return;
654 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
655 write_var_init(file, indent, var->type, var->name, local_var_prefix);
657 fprintf(file, "\n");
660 static void write_formatdesc(FILE *f, int indent, const char *str)
662 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
663 print_file(f, indent, "{\n");
664 print_file(f, indent + 1, "short Pad;\n");
665 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
666 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
667 print_file(f, indent, "\n");
670 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
672 clear_all_offsets();
674 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
675 get_size_typeformatstring(stmts, pred));
677 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
678 get_size_procformatstring(stmts, pred));
680 fprintf(f, "\n");
681 write_formatdesc(f, indent, "TYPE");
682 write_formatdesc(f, indent, "PROC");
683 fprintf(f, "\n");
684 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
685 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
686 print_file(f, indent, "\n");
689 int decl_indirect(const type_t *t)
691 if (is_user_type(t))
692 return TRUE;
693 return (type_get_type(t) != TYPE_BASIC &&
694 type_get_type(t) != TYPE_ENUM &&
695 type_get_type(t) != TYPE_POINTER &&
696 type_get_type(t) != TYPE_ARRAY);
699 static unsigned int write_procformatstring_type(FILE *file, int indent,
700 const char *name,
701 const type_t *type,
702 const attr_list_t *attrs,
703 int is_return)
705 unsigned int size;
707 int is_in = is_attr(attrs, ATTR_IN);
708 int is_out = is_attr(attrs, ATTR_OUT);
710 if (!is_in && !is_out) is_in = TRUE;
712 if (type_get_type(type) == TYPE_BASIC ||
713 type_get_type(type) == TYPE_ENUM)
715 unsigned char fc;
717 if (is_return)
718 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
719 else
720 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
722 if (type_get_type(type) == TYPE_ENUM)
724 fc = get_enum_fc(type);
726 else
728 fc = type_basic_get_fc(type);
730 if (fc == RPC_FC_BIND_PRIMITIVE)
731 fc = RPC_FC_IGNORE;
734 print_file(file, indent, "0x%02x, /* %s */\n",
735 fc, string_of_type(fc));
736 size = 2; /* includes param type prefix */
738 else
740 if (is_return)
741 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
742 else if (is_in && is_out)
743 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
744 else if (is_out)
745 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
746 else
747 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
749 print_file(file, indent, "0x01,\n");
750 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
751 size = 4; /* includes param type prefix */
753 return size;
756 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
758 const statement_t *stmt;
759 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
761 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
763 const statement_t *stmt_func;
764 if (!pred(stmt->u.type))
765 continue;
766 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
768 const var_t *func = stmt_func->u.var;
769 if (is_local(func->attrs)) continue;
770 /* emit argument data */
771 if (type_get_function_args(func->type))
773 const var_t *var;
774 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
775 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
778 /* emit return value data */
779 if (is_void(type_function_get_rettype(func->type)))
781 print_file(file, indent, "0x5b, /* FC_END */\n");
782 print_file(file, indent, "0x5c, /* FC_PAD */\n");
784 else
785 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
788 else if (stmt->type == STMT_LIBRARY)
789 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
793 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
795 int indent = 0;
797 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
798 print_file(file, indent, "{\n");
799 indent++;
800 print_file(file, indent, "0,\n");
801 print_file(file, indent, "{\n");
802 indent++;
804 write_procformatstring_stmts(file, indent, stmts, pred);
806 print_file(file, indent, "0x0\n");
807 indent--;
808 print_file(file, indent, "}\n");
809 indent--;
810 print_file(file, indent, "};\n");
811 print_file(file, indent, "\n");
814 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
816 unsigned char fc;
818 if (type_get_type(type) == TYPE_BASIC)
819 fc = type_basic_get_fc(type);
820 else if (type_get_type(type) == TYPE_ENUM)
821 fc = get_enum_fc(type);
822 else
823 return 0;
825 if (convert_to_signed_type)
827 switch(fc)
829 case RPC_FC_USMALL:
830 fc = RPC_FC_SMALL;
831 break;
832 case RPC_FC_USHORT:
833 fc = RPC_FC_SHORT;
834 break;
835 case RPC_FC_ULONG:
836 fc = RPC_FC_LONG;
837 break;
841 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
842 *typestring_offset += 1;
843 return 1;
846 /* write conformance / variance descriptor */
847 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
848 unsigned int baseoff, const type_t *type,
849 const expr_t *expr)
851 unsigned char operator_type = 0;
852 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
853 const char *conftype_string = "";
854 const char *operator_string = "no operators";
855 const expr_t *subexpr;
857 if (!expr)
859 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
860 return 4;
863 if (!structure)
865 /* Top-level conformance calculations are done inline. */
866 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
867 RPC_FC_TOP_LEVEL_CONFORMANCE);
868 print_file (file, 2, "0x0,\n");
869 print_file (file, 2, "NdrFcShort(0x0),\n");
870 return 4;
873 if (expr->is_const)
875 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
876 error("write_conf_or_var_desc: constant value %ld is greater than "
877 "the maximum constant size of %d\n", expr->cval,
878 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
880 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
881 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
882 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
883 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
885 return 4;
888 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
890 conftype = RPC_FC_POINTER_CONFORMANCE;
891 conftype_string = "field pointer, ";
894 subexpr = expr;
895 switch (subexpr->type)
897 case EXPR_PPTR:
898 subexpr = subexpr->ref;
899 operator_type = RPC_FC_DEREFERENCE;
900 operator_string = "FC_DEREFERENCE";
901 break;
902 case EXPR_DIV:
903 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
905 subexpr = subexpr->ref;
906 operator_type = RPC_FC_DIV_2;
907 operator_string = "FC_DIV_2";
909 break;
910 case EXPR_MUL:
911 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
913 subexpr = subexpr->ref;
914 operator_type = RPC_FC_MULT_2;
915 operator_string = "FC_MULT_2";
917 break;
918 case EXPR_SUB:
919 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
921 subexpr = subexpr->ref;
922 operator_type = RPC_FC_SUB_1;
923 operator_string = "FC_SUB_1";
925 break;
926 case EXPR_ADD:
927 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
929 subexpr = subexpr->ref;
930 operator_type = RPC_FC_ADD_1;
931 operator_string = "FC_ADD_1";
933 break;
934 default:
935 break;
938 if (subexpr->type == EXPR_IDENTIFIER)
940 const type_t *correlation_variable = NULL;
941 unsigned char param_type = 0;
942 unsigned int offset = 0;
943 const var_t *var;
944 var_list_t *fields = type_struct_get_fields(structure);
946 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
948 unsigned int align = 0;
949 /* FIXME: take alignment into account */
950 if (var->name && !strcmp(var->name, subexpr->u.sval))
952 correlation_variable = var->type;
953 break;
955 offset += type_memsize(var->type, &align);
957 if (!correlation_variable)
958 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
959 subexpr->u.sval);
961 correlation_variable = expr_resolve_type(NULL, structure, expr);
963 offset -= baseoff;
965 if (type_get_type(correlation_variable) == TYPE_BASIC)
967 switch (type_basic_get_fc(correlation_variable))
969 case RPC_FC_CHAR:
970 case RPC_FC_SMALL:
971 param_type = RPC_FC_SMALL;
972 break;
973 case RPC_FC_BYTE:
974 case RPC_FC_USMALL:
975 param_type = RPC_FC_USMALL;
976 break;
977 case RPC_FC_WCHAR:
978 case RPC_FC_SHORT:
979 param_type = RPC_FC_SHORT;
980 break;
981 case RPC_FC_USHORT:
982 param_type = RPC_FC_USHORT;
983 break;
984 case RPC_FC_LONG:
985 param_type = RPC_FC_LONG;
986 break;
987 case RPC_FC_ULONG:
988 param_type = RPC_FC_ULONG;
989 break;
990 default:
991 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
992 type_basic_get_fc(correlation_variable));
995 else if (type_get_type(correlation_variable) == TYPE_ENUM)
997 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
998 param_type = RPC_FC_LONG;
999 else
1000 param_type = RPC_FC_SHORT;
1002 else
1004 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1005 subexpr->u.sval);
1006 return 0;
1009 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1010 conftype | param_type, conftype_string, string_of_type(param_type));
1011 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1012 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1013 offset, offset);
1015 else
1017 unsigned int callback_offset = 0;
1018 struct expr_eval_routine *eval;
1019 int found = 0;
1021 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1023 if (!strcmp (eval->structure->name, structure->name)
1024 && !compare_expr (eval->expr, expr))
1026 found = 1;
1027 break;
1029 callback_offset++;
1032 if (!found)
1034 eval = xmalloc (sizeof(*eval));
1035 eval->structure = structure;
1036 eval->baseoff = baseoff;
1037 eval->expr = expr;
1038 list_add_tail (&expr_eval_routines, &eval->entry);
1041 if (callback_offset > USHRT_MAX)
1042 error("Maximum number of callback routines reached\n");
1044 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1045 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1046 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1048 return 4;
1051 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1053 int have_align = FALSE;
1054 unsigned int size = 0;
1055 const var_t *v;
1057 if (!fields) return 0;
1058 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1060 unsigned int falign = 0;
1061 unsigned int fsize = type_memsize(v->type, &falign);
1062 if (!have_align)
1064 *align = falign;
1065 have_align = TRUE;
1067 size = ROUND_SIZE(size, falign);
1068 size += fsize;
1071 size = ROUND_SIZE(size, *align);
1072 return size;
1075 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1077 unsigned int size, maxs = 0;
1078 unsigned int align = *pmaxa;
1079 const var_t *v;
1081 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1083 /* we could have an empty default field with NULL type */
1084 if (v->type)
1086 size = type_memsize(v->type, &align);
1087 if (maxs < size) maxs = size;
1088 if (*pmaxa < align) *pmaxa = align;
1092 return maxs;
1095 int get_padding(const var_list_t *fields)
1097 unsigned short offset = 0;
1098 int salign = -1;
1099 const var_t *f;
1101 if (!fields)
1102 return 0;
1104 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1106 type_t *ft = f->type;
1107 unsigned int align = 0;
1108 unsigned int size = type_memsize(ft, &align);
1109 if (salign == -1)
1110 salign = align;
1111 offset = ROUND_SIZE(offset, align);
1112 offset += size;
1115 return ROUNDING(offset, salign);
1118 unsigned int type_memsize(const type_t *t, unsigned int *align)
1120 unsigned int size = 0;
1122 switch (type_get_type(t))
1124 case TYPE_BASIC:
1125 switch (type_basic_get_fc(t))
1127 case RPC_FC_BYTE:
1128 case RPC_FC_CHAR:
1129 case RPC_FC_USMALL:
1130 case RPC_FC_SMALL:
1131 size = 1;
1132 if (size > *align) *align = size;
1133 break;
1134 case RPC_FC_WCHAR:
1135 case RPC_FC_USHORT:
1136 case RPC_FC_SHORT:
1137 size = 2;
1138 if (size > *align) *align = size;
1139 break;
1140 case RPC_FC_ULONG:
1141 case RPC_FC_LONG:
1142 case RPC_FC_ERROR_STATUS_T:
1143 case RPC_FC_FLOAT:
1144 size = 4;
1145 if (size > *align) *align = size;
1146 break;
1147 case RPC_FC_HYPER:
1148 case RPC_FC_DOUBLE:
1149 size = 8;
1150 if (size > *align) *align = size;
1151 break;
1152 default:
1153 error("type_memsize: Unknown type 0x%x\n", type_basic_get_fc(t));
1154 size = 0;
1156 break;
1157 case TYPE_ENUM:
1158 switch (get_enum_fc(t))
1160 case RPC_FC_ENUM32:
1161 size = 4;
1162 if (size > *align) *align = size;
1163 break;
1164 case RPC_FC_ENUM16:
1165 size = 2;
1166 if (size > *align) *align = size;
1167 break;
1168 default:
1169 error("type_memsize: Unknown enum type\n");
1170 size = 0;
1172 break;
1173 case TYPE_STRUCT:
1174 size = fields_memsize(type_struct_get_fields(t), align);
1175 break;
1176 case TYPE_ENCAPSULATED_UNION:
1177 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1178 break;
1179 case TYPE_UNION:
1180 size = union_memsize(type_union_get_cases(t), align);
1181 break;
1182 case TYPE_POINTER:
1183 assert( pointer_size );
1184 size = pointer_size;
1185 if (size > *align) *align = size;
1186 break;
1187 case TYPE_ARRAY:
1188 if (!type_array_is_decl_as_ptr(t))
1190 if (is_conformant_array(t))
1192 type_memsize(type_array_get_element(t), align);
1193 size = 0;
1195 else
1196 size = type_array_get_dim(t) *
1197 type_memsize(type_array_get_element(t), align);
1199 else /* declared as a pointer */
1201 assert( pointer_size );
1202 size = pointer_size;
1203 if (size > *align) *align = size;
1205 break;
1206 case TYPE_INTERFACE:
1207 case TYPE_ALIAS:
1208 case TYPE_VOID:
1209 case TYPE_COCLASS:
1210 case TYPE_MODULE:
1211 case TYPE_FUNCTION:
1212 /* these types should not be encountered here due to language
1213 * restrictions (interface, void, coclass, module), logical
1214 * restrictions (alias - due to type_get_type call above) or
1215 * checking restrictions (function). */
1216 assert(0);
1219 return size;
1222 int is_full_pointer_function(const var_t *func)
1224 const var_t *var;
1225 if (type_has_full_pointer(type_function_get_rettype(func->type)))
1226 return TRUE;
1227 if (!type_get_function_args(func->type))
1228 return FALSE;
1229 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1230 if (type_has_full_pointer( var->type ))
1231 return TRUE;
1232 return FALSE;
1235 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1237 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1238 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1239 fprintf(file, "\n");
1242 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1244 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1245 fprintf(file, "\n");
1248 static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, unsigned int offset)
1250 short absoff = type_pointer_get_ref(type)->typestring_offset;
1251 short reloff = absoff - (offset + 2);
1252 int ptr_attr = is_ptr(type_pointer_get_ref(type)) ? 0x10 : 0x0;
1253 unsigned char pointer_fc = get_pointer_fc(type);
1255 print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
1256 pointer_fc, ptr_attr, string_of_type(pointer_fc));
1257 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1258 reloff, reloff, absoff);
1259 return 4;
1262 static unsigned int write_simple_pointer(FILE *file, const type_t *type)
1264 unsigned char fc;
1265 unsigned char pointer_fc;
1266 const type_t *ref;
1268 /* for historical reasons, write_simple_pointer also handled string types,
1269 * but no longer does. catch bad uses of the function with this check */
1270 if (is_string_type(type->attrs, type))
1271 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1273 pointer_fc = get_pointer_fc(type);
1275 ref = type_pointer_get_ref(type);
1276 if (type_get_type(ref) == TYPE_ENUM)
1277 fc = get_enum_fc(ref);
1278 else
1279 fc = type_basic_get_fc(ref);
1281 print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1282 pointer_fc, string_of_type(pointer_fc));
1283 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1284 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1285 return 4;
1288 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1290 print_file(file, 0, "/* %u (", tfsoff);
1291 write_type_decl(file, t, NULL);
1292 print_file(file, 0, ") */\n");
1295 static unsigned int write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
1297 unsigned int offset = *typestring_offset;
1298 type_t *ref = type_pointer_get_ref(type);
1300 print_start_tfs_comment(file, type, offset);
1301 update_tfsoff(type, offset, file);
1303 if (ref->typestring_offset)
1304 *typestring_offset += write_nonsimple_pointer(file, type, offset);
1305 else if (type_get_type(ref) == TYPE_BASIC ||
1306 type_get_type(ref) == TYPE_ENUM)
1307 *typestring_offset += write_simple_pointer(file, type);
1309 return offset;
1312 static int processed(const type_t *type)
1314 return type->typestring_offset && !type->tfswrite;
1317 static int user_type_has_variable_size(const type_t *t)
1319 if (is_ptr(t))
1320 return TRUE;
1321 else if (type_get_type(t) == TYPE_STRUCT)
1323 switch (get_struct_fc(t))
1325 case RPC_FC_PSTRUCT:
1326 case RPC_FC_CSTRUCT:
1327 case RPC_FC_CPSTRUCT:
1328 case RPC_FC_CVSTRUCT:
1329 return TRUE;
1332 /* Note: Since this only applies to user types, we can't have a conformant
1333 array here, and strings should get filed under pointer in this case. */
1334 return FALSE;
1337 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1339 unsigned int start, absoff, flags;
1340 unsigned int align = 0, ualign = 0;
1341 const char *name = NULL;
1342 type_t *utype = get_user_type(type, &name);
1343 unsigned int usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1344 unsigned int size = type_memsize(type, &align);
1345 unsigned short funoff = user_type_offset(name);
1346 short reloff;
1348 guard_rec(type);
1350 if (type_get_type(utype) == TYPE_BASIC ||
1351 type_get_type(utype) == TYPE_ENUM)
1353 unsigned char fc;
1355 if (type_get_type(utype) == TYPE_ENUM)
1356 fc = get_enum_fc(utype);
1357 else
1358 fc = type_basic_get_fc(utype);
1360 absoff = *tfsoff;
1361 print_start_tfs_comment(file, utype, absoff);
1362 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1363 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1364 *tfsoff += 2;
1366 else
1368 if (!processed(utype))
1369 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1370 absoff = utype->typestring_offset;
1373 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype) == RPC_FC_RP)
1374 flags = 0x40;
1375 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype) == RPC_FC_UP)
1376 flags = 0x80;
1377 else
1378 flags = 0;
1380 start = *tfsoff;
1381 update_tfsoff(type, start, file);
1382 print_start_tfs_comment(file, type, start);
1383 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1384 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1385 flags | (align - 1), align - 1, flags);
1386 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1387 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1388 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1389 *tfsoff += 8;
1390 reloff = absoff - *tfsoff;
1391 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1392 *tfsoff += 2;
1395 static void write_member_type(FILE *file, const type_t *cont,
1396 int cont_is_complex, const attr_list_t *attrs,
1397 const type_t *type, unsigned int *corroff,
1398 unsigned int *tfsoff)
1400 if (is_embedded_complex(type) && !is_conformant_array(type))
1402 unsigned int absoff;
1403 short reloff;
1405 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1407 absoff = *corroff;
1408 *corroff += 8;
1410 else
1412 absoff = type->typestring_offset;
1414 reloff = absoff - (*tfsoff + 2);
1416 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1417 /* FIXME: actually compute necessary padding */
1418 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1419 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1420 reloff, reloff, absoff);
1421 *tfsoff += 4;
1423 else if (is_ptr(type) || is_conformant_array(type))
1425 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1426 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1427 *tfsoff += 1;
1429 else if (!write_base_type(file, type, TRUE, tfsoff))
1430 error("Unsupported member type %d\n", type_get_type(type));
1433 static void write_end(FILE *file, unsigned int *tfsoff)
1435 if (*tfsoff % 2 == 0)
1437 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1438 *tfsoff += 1;
1440 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1441 *tfsoff += 1;
1444 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1446 unsigned int offset = 0;
1447 var_list_t *fs = type_struct_get_fields(type);
1448 var_t *f;
1450 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1452 unsigned int align = 0;
1453 type_t *ft = f->type;
1454 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1456 unsigned int absoff = ft->typestring_offset;
1457 short reloff = absoff - (*tfsoff + 6);
1458 print_file(file, 0, "/* %d */\n", *tfsoff);
1459 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1460 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1461 write_conf_or_var_desc(file, current_structure, offset, ft,
1462 get_attrp(f->attrs, ATTR_SWITCHIS));
1463 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1464 reloff, reloff, absoff);
1465 *tfsoff += 8;
1468 /* FIXME: take alignment into account */
1469 offset += type_memsize(ft, &align);
1473 static int write_no_repeat_pointer_descriptions(
1474 FILE *file, type_t *type,
1475 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1476 unsigned int *typestring_offset)
1478 int written = 0;
1479 unsigned int align;
1481 if (is_ptr(type) ||
1482 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1484 unsigned int memsize;
1486 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1487 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1489 /* pointer instance */
1490 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1491 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1492 *typestring_offset += 6;
1494 if (is_ptr(type))
1496 if (is_string_type(type->attrs, type))
1497 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1498 else
1499 write_pointer_tfs(file, type, typestring_offset);
1501 else
1503 unsigned absoff = type->typestring_offset;
1504 short reloff = absoff - (*typestring_offset + 2);
1505 /* FIXME: get pointer attributes from field */
1506 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1507 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1508 reloff, reloff, absoff);
1509 *typestring_offset += 4;
1512 align = 0;
1513 memsize = type_memsize(type, &align);
1514 *offset_in_memory += memsize;
1515 /* increment these separately as in the case of conformant (varying)
1516 * structures these start at different values */
1517 *offset_in_buffer += memsize;
1519 return 1;
1522 if (is_non_complex_struct(type))
1524 const var_t *v;
1525 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1527 if (offset_in_memory && offset_in_buffer)
1529 unsigned int padding;
1530 align = 0;
1531 type_memsize(v->type, &align);
1532 padding = ROUNDING(*offset_in_memory, align);
1533 *offset_in_memory += padding;
1534 *offset_in_buffer += padding;
1536 written += write_no_repeat_pointer_descriptions(
1537 file, v->type,
1538 offset_in_memory, offset_in_buffer, typestring_offset);
1541 else
1543 unsigned int memsize;
1544 align = 0;
1545 memsize = type_memsize(type, &align);
1546 *offset_in_memory += memsize;
1547 /* increment these separately as in the case of conformant (varying)
1548 * structures these start at different values */
1549 *offset_in_buffer += memsize;
1552 return written;
1555 static int write_pointer_description_offsets(
1556 FILE *file, const attr_list_t *attrs, type_t *type,
1557 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1558 unsigned int *typestring_offset)
1560 int written = 0;
1561 unsigned int align;
1563 if (is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE)
1565 type_t *ref = type_pointer_get_ref(type);
1567 if (offset_in_memory && offset_in_buffer)
1569 unsigned int memsize;
1571 /* pointer instance */
1572 /* FIXME: sometimes from end of structure, sometimes from beginning */
1573 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1574 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1576 align = 0;
1577 memsize = type_memsize(type, &align);
1578 *offset_in_memory += memsize;
1579 /* increment these separately as in the case of conformant (varying)
1580 * structures these start at different values */
1581 *offset_in_buffer += memsize;
1583 *typestring_offset += 4;
1585 if (is_string_type(attrs, type))
1586 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1587 else if (processed(ref) || type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1588 write_pointer_tfs(file, type, typestring_offset);
1589 else
1590 error("write_pointer_description_offsets: type format string unknown\n");
1592 return 1;
1595 if (is_array(type))
1597 return write_pointer_description_offsets(
1598 file, attrs, type_array_get_element(type), offset_in_memory,
1599 offset_in_buffer, typestring_offset);
1601 else if (is_non_complex_struct(type))
1603 /* otherwise search for interesting fields to parse */
1604 const var_t *v;
1605 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1607 if (offset_in_memory && offset_in_buffer)
1609 unsigned int padding;
1610 align = 0;
1611 type_memsize(v->type, &align);
1612 padding = ROUNDING(*offset_in_memory, align);
1613 *offset_in_memory += padding;
1614 *offset_in_buffer += padding;
1616 written += write_pointer_description_offsets(
1617 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1618 typestring_offset);
1621 else
1623 if (offset_in_memory && offset_in_buffer)
1625 unsigned int memsize;
1626 align = 0;
1627 memsize = type_memsize(type, &align);
1628 *offset_in_memory += memsize;
1629 /* increment these separately as in the case of conformant (varying)
1630 * structures these start at different values */
1631 *offset_in_buffer += memsize;
1635 return written;
1638 /* Note: if file is NULL return value is number of pointers to write, else
1639 * it is the number of type format characters written */
1640 static int write_fixed_array_pointer_descriptions(
1641 FILE *file, const attr_list_t *attrs, type_t *type,
1642 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1643 unsigned int *typestring_offset)
1645 unsigned int align;
1646 int pointer_count = 0;
1648 if (type_get_type(type) == TYPE_ARRAY &&
1649 !type_array_has_conformance(type) && !type_array_has_variance(type))
1651 unsigned int temp = 0;
1652 /* unfortunately, this needs to be done in two passes to avoid
1653 * writing out redundant FC_FIXED_REPEAT descriptions */
1654 pointer_count = write_pointer_description_offsets(
1655 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1656 if (pointer_count > 0)
1658 unsigned int increment_size;
1659 unsigned int offset_of_array_pointer_mem = 0;
1660 unsigned int offset_of_array_pointer_buf = 0;
1662 align = 0;
1663 increment_size = type_memsize(type_array_get_element(type), &align);
1665 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1666 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1667 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1668 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1669 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1670 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1671 *typestring_offset += 10;
1673 pointer_count = write_pointer_description_offsets(
1674 file, attrs, type, &offset_of_array_pointer_mem,
1675 &offset_of_array_pointer_buf, typestring_offset);
1678 else if (type_get_type(type) == TYPE_STRUCT)
1680 const var_t *v;
1681 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1683 if (offset_in_memory && offset_in_buffer)
1685 unsigned int padding;
1686 align = 0;
1687 type_memsize(v->type, &align);
1688 padding = ROUNDING(*offset_in_memory, align);
1689 *offset_in_memory += padding;
1690 *offset_in_buffer += padding;
1692 pointer_count += write_fixed_array_pointer_descriptions(
1693 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1694 typestring_offset);
1697 else
1699 if (offset_in_memory && offset_in_buffer)
1701 unsigned int memsize;
1702 align = 0;
1703 memsize = type_memsize(type, &align);
1704 *offset_in_memory += memsize;
1705 /* increment these separately as in the case of conformant (varying)
1706 * structures these start at different values */
1707 *offset_in_buffer += memsize;
1711 return pointer_count;
1714 /* Note: if file is NULL return value is number of pointers to write, else
1715 * it is the number of type format characters written */
1716 static int write_conformant_array_pointer_descriptions(
1717 FILE *file, const attr_list_t *attrs, type_t *type,
1718 unsigned int offset_in_memory, unsigned int *typestring_offset)
1720 unsigned int align;
1721 int pointer_count = 0;
1723 if (is_conformant_array(type) && !type_array_has_variance(type))
1725 unsigned int temp = 0;
1726 /* unfortunately, this needs to be done in two passes to avoid
1727 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1728 pointer_count = write_pointer_description_offsets(
1729 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1730 if (pointer_count > 0)
1732 unsigned int increment_size;
1733 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1734 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1736 align = 0;
1737 increment_size = type_memsize(type_array_get_element(type), &align);
1739 if (increment_size > USHRT_MAX)
1740 error("array size of %u bytes is too large\n", increment_size);
1742 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1743 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1744 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1745 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1746 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1747 *typestring_offset += 8;
1749 pointer_count = write_pointer_description_offsets(
1750 file, attrs, type_array_get_element(type),
1751 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1752 typestring_offset);
1756 return pointer_count;
1759 /* Note: if file is NULL return value is number of pointers to write, else
1760 * it is the number of type format characters written */
1761 static int write_varying_array_pointer_descriptions(
1762 FILE *file, const attr_list_t *attrs, type_t *type,
1763 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1764 unsigned int *typestring_offset)
1766 unsigned int align;
1767 int pointer_count = 0;
1769 if (is_array(type) && type_array_has_variance(type))
1771 unsigned int temp = 0;
1772 /* unfortunately, this needs to be done in two passes to avoid
1773 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1774 pointer_count = write_pointer_description_offsets(
1775 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1776 if (pointer_count > 0)
1778 unsigned int increment_size;
1780 align = 0;
1781 increment_size = type_memsize(type_array_get_element(type), &align);
1783 if (increment_size > USHRT_MAX)
1784 error("array size of %u bytes is too large\n", increment_size);
1786 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1787 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1788 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1789 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1790 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1791 *typestring_offset += 8;
1793 pointer_count = write_pointer_description_offsets(
1794 file, attrs, type, offset_in_memory,
1795 offset_in_buffer, typestring_offset);
1798 else if (type_get_type(type) == TYPE_STRUCT)
1800 const var_t *v;
1801 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1803 if (offset_in_memory && offset_in_buffer)
1805 unsigned int padding;
1807 if (is_array(v->type) && type_array_has_variance(v->type))
1809 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1810 /* skip over variance and offset in buffer */
1811 *offset_in_buffer += 8;
1814 align = 0;
1815 type_memsize(v->type, &align);
1816 padding = ROUNDING(*offset_in_memory, align);
1817 *offset_in_memory += padding;
1818 *offset_in_buffer += padding;
1820 pointer_count += write_varying_array_pointer_descriptions(
1821 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1822 typestring_offset);
1825 else
1827 if (offset_in_memory && offset_in_buffer)
1829 unsigned int memsize;
1830 align = 0;
1831 memsize = type_memsize(type, &align);
1832 *offset_in_memory += memsize;
1833 /* increment these separately as in the case of conformant (varying)
1834 * structures these start at different values */
1835 *offset_in_buffer += memsize;
1839 return pointer_count;
1842 static void write_pointer_description(FILE *file, type_t *type,
1843 unsigned int *typestring_offset)
1845 unsigned int offset_in_buffer;
1846 unsigned int offset_in_memory;
1848 /* pass 1: search for single instance of a pointer (i.e. don't descend
1849 * into arrays) */
1850 if (!is_array(type))
1852 offset_in_memory = 0;
1853 offset_in_buffer = 0;
1854 write_no_repeat_pointer_descriptions(
1855 file, type,
1856 &offset_in_memory, &offset_in_buffer, typestring_offset);
1859 /* pass 2: search for pointers in fixed arrays */
1860 offset_in_memory = 0;
1861 offset_in_buffer = 0;
1862 write_fixed_array_pointer_descriptions(
1863 file, NULL, type,
1864 &offset_in_memory, &offset_in_buffer, typestring_offset);
1866 /* pass 3: search for pointers in conformant only arrays (but don't descend
1867 * into conformant varying or varying arrays) */
1868 if (is_conformant_array(type) &&
1869 (type_array_is_decl_as_ptr(type) || !current_structure))
1870 write_conformant_array_pointer_descriptions(
1871 file, NULL, type, 0, typestring_offset);
1872 else if (type_get_type(type) == TYPE_STRUCT &&
1873 get_struct_fc(type) == RPC_FC_CPSTRUCT)
1875 unsigned int align = 0;
1876 type_t *carray = find_array_or_string_in_struct(type)->type;
1877 write_conformant_array_pointer_descriptions(
1878 file, NULL, carray,
1879 type_memsize(type, &align),
1880 typestring_offset);
1883 /* pass 4: search for pointers in varying arrays */
1884 offset_in_memory = 0;
1885 offset_in_buffer = 0;
1886 write_varying_array_pointer_descriptions(
1887 file, NULL, type,
1888 &offset_in_memory, &offset_in_buffer, typestring_offset);
1891 int is_declptr(const type_t *t)
1893 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
1896 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1897 type_t *type,
1898 const char *name, unsigned int *typestring_offset)
1900 unsigned int start_offset;
1901 unsigned char rtype;
1902 type_t *elem_type;
1904 if (is_declptr(type))
1906 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1907 int pointer_type = is_ptr(type) ? get_pointer_fc(type) : get_attrv(attrs, ATTR_POINTERTYPE);
1908 if (!pointer_type)
1909 pointer_type = RPC_FC_RP;
1910 print_start_tfs_comment(file, type, *typestring_offset);
1911 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1912 pointer_type, flag, string_of_type(pointer_type),
1913 flag ? " [simple_pointer]" : "");
1914 *typestring_offset += 2;
1915 if (!flag)
1917 print_file(file, 2, "NdrFcShort(0x2),\n");
1918 *typestring_offset += 2;
1922 start_offset = *typestring_offset;
1923 update_tfsoff(type, start_offset, file);
1925 if (is_array(type))
1926 elem_type = type_array_get_element(type);
1927 else
1928 elem_type = type_pointer_get_ref(type);
1930 if (type_get_type(elem_type) != TYPE_BASIC)
1932 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
1933 return start_offset;
1936 rtype = type_basic_get_fc(elem_type);
1937 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1939 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1940 return start_offset;
1943 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
1945 unsigned int dim = type_array_get_dim(type);
1947 /* FIXME: multi-dimensional array */
1948 if (0xffffu < dim)
1949 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
1950 name, 0xffffu, dim - 0xffffu);
1952 if (rtype == RPC_FC_WCHAR)
1953 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1954 else
1955 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1956 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1957 *typestring_offset += 2;
1959 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
1960 *typestring_offset += 2;
1962 return start_offset;
1964 else if (is_conformant_array(type))
1966 unsigned int align = 0;
1968 if (rtype == RPC_FC_WCHAR)
1969 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1970 else
1971 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1972 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1973 *typestring_offset += 2;
1975 *typestring_offset += write_conf_or_var_desc(
1976 file, current_structure,
1977 (!type_array_is_decl_as_ptr(type) && current_structure
1978 ? type_memsize(current_structure, &align)
1979 : 0),
1980 type, type_array_get_conformance(type));
1982 return start_offset;
1984 else
1986 if (rtype == RPC_FC_WCHAR)
1987 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1988 else
1989 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1990 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1991 *typestring_offset += 2;
1993 return start_offset;
1997 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1998 const char *name, unsigned int *typestring_offset)
2000 const expr_t *length_is = type_array_get_variance(type);
2001 const expr_t *size_is = type_array_get_conformance(type);
2002 unsigned int align = 0;
2003 unsigned int size;
2004 unsigned int start_offset;
2005 unsigned char fc;
2006 int has_pointer;
2007 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2008 unsigned int baseoff
2009 = !type_array_is_decl_as_ptr(type) && current_structure
2010 ? type_memsize(current_structure, &align)
2011 : 0;
2013 if (!pointer_type)
2014 pointer_type = RPC_FC_RP;
2016 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2017 has_pointer = TRUE;
2018 else
2019 has_pointer = type_has_pointers(type_array_get_element(type));
2021 align = 0;
2022 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2023 fc = get_array_fc(type);
2025 start_offset = *typestring_offset;
2026 update_tfsoff(type, start_offset, file);
2027 print_start_tfs_comment(file, type, start_offset);
2028 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2029 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2030 *typestring_offset += 2;
2032 align = 0;
2033 if (fc != RPC_FC_BOGUS_ARRAY)
2035 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2037 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2038 *typestring_offset += 4;
2040 else
2042 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2043 *typestring_offset += 2;
2046 if (is_conformant_array(type))
2047 *typestring_offset
2048 += write_conf_or_var_desc(file, current_structure, baseoff,
2049 type, size_is);
2051 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2053 unsigned int elalign = 0;
2054 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2055 unsigned int dim = type_array_get_dim(type);
2057 if (fc == RPC_FC_LGVARRAY)
2059 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2060 *typestring_offset += 4;
2062 else
2064 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2065 *typestring_offset += 2;
2068 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2069 *typestring_offset += 2;
2072 if (length_is)
2073 *typestring_offset
2074 += write_conf_or_var_desc(file, current_structure, baseoff,
2075 type, length_is);
2077 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2079 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2080 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2081 *typestring_offset += 2;
2082 write_pointer_description(file, type, typestring_offset);
2083 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2084 *typestring_offset += 1;
2087 write_member_type(file, type, FALSE, NULL, type_array_get_element(type), NULL, typestring_offset);
2088 write_end(file, typestring_offset);
2090 else
2092 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2093 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2094 *typestring_offset += 2;
2095 *typestring_offset
2096 += write_conf_or_var_desc(file, current_structure, baseoff,
2097 type, size_is);
2098 *typestring_offset
2099 += write_conf_or_var_desc(file, current_structure, baseoff,
2100 type, length_is);
2101 write_member_type(file, type, TRUE, NULL, type_array_get_element(type), NULL, typestring_offset);
2102 write_end(file, typestring_offset);
2105 return start_offset;
2108 static const var_t *find_array_or_string_in_struct(const type_t *type)
2110 const var_list_t *fields = type_struct_get_fields(type);
2111 const var_t *last_field;
2112 const type_t *ft;
2114 if (!fields || list_empty(fields))
2115 return NULL;
2117 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2118 ft = last_field->type;
2120 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2121 return last_field;
2123 if (type_get_type(ft) == TYPE_STRUCT)
2124 return find_array_or_string_in_struct(ft);
2125 else
2126 return NULL;
2129 static void write_struct_members(FILE *file, const type_t *type,
2130 int is_complex, unsigned int *corroff,
2131 unsigned int *typestring_offset)
2133 const var_t *field;
2134 unsigned short offset = 0;
2135 int salign = -1;
2136 int padding;
2137 var_list_t *fields = type_struct_get_fields(type);
2139 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2141 type_t *ft = field->type;
2142 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2144 unsigned int align = 0;
2145 unsigned int size = type_memsize(ft, &align);
2146 if (salign == -1)
2147 salign = align;
2148 if ((align - 1) & offset)
2150 unsigned char fc = 0;
2151 switch (align)
2153 case 4:
2154 fc = RPC_FC_ALIGNM4;
2155 break;
2156 case 8:
2157 fc = RPC_FC_ALIGNM8;
2158 break;
2159 default:
2160 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2162 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2163 offset = ROUND_SIZE(offset, align);
2164 *typestring_offset += 1;
2166 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2167 typestring_offset);
2168 offset += size;
2172 padding = ROUNDING(offset, salign);
2173 if (padding)
2175 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2176 RPC_FC_STRUCTPAD1 + padding - 1,
2177 padding);
2178 *typestring_offset += 1;
2181 write_end(file, typestring_offset);
2184 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2185 const char *name, unsigned int *tfsoff)
2187 const type_t *save_current_structure = current_structure;
2188 unsigned int total_size;
2189 const var_t *array;
2190 unsigned int start_offset;
2191 unsigned int array_offset;
2192 int has_pointers = 0;
2193 unsigned int align = 0;
2194 unsigned int corroff;
2195 var_t *f;
2196 unsigned char fc = get_struct_fc(type);
2197 var_list_t *fields = type_struct_get_fields(type);
2199 guard_rec(type);
2200 current_structure = type;
2202 total_size = type_memsize(type, &align);
2203 if (total_size > USHRT_MAX)
2204 error("structure size for %s exceeds %d bytes by %d bytes\n",
2205 name, USHRT_MAX, total_size - USHRT_MAX);
2207 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2208 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2209 FALSE, tfsoff);
2210 if (!has_pointers) has_pointers = type_has_pointers(type);
2212 array = find_array_or_string_in_struct(type);
2213 if (array && !processed(array->type))
2214 array_offset
2215 = is_string_type(array->attrs, array->type)
2216 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2217 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2219 corroff = *tfsoff;
2220 write_descriptors(file, type, tfsoff);
2222 start_offset = *tfsoff;
2223 update_tfsoff(type, start_offset, file);
2224 print_start_tfs_comment(file, type, start_offset);
2225 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2226 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2227 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2228 *tfsoff += 4;
2230 if (array)
2232 unsigned int absoff = array->type->typestring_offset;
2233 short reloff = absoff - *tfsoff;
2234 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2235 reloff, reloff, absoff);
2236 *tfsoff += 2;
2238 else if (fc == RPC_FC_BOGUS_STRUCT)
2240 print_file(file, 2, "NdrFcShort(0x0),\n");
2241 *tfsoff += 2;
2244 if (fc == RPC_FC_BOGUS_STRUCT)
2246 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2247 nothing is written to file yet. On the actual writing pass,
2248 this will have been updated. */
2249 unsigned int absoff = type_get_real_type(type)->ptrdesc ?
2250 type_get_real_type(type)->ptrdesc : *tfsoff;
2251 int reloff = absoff - *tfsoff;
2252 assert( reloff >= 0 );
2253 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2254 reloff, reloff, absoff);
2255 *tfsoff += 2;
2257 else if ((fc == RPC_FC_PSTRUCT) ||
2258 (fc == RPC_FC_CPSTRUCT) ||
2259 (fc == RPC_FC_CVSTRUCT && has_pointers))
2261 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2262 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2263 *tfsoff += 2;
2264 write_pointer_description(file, type, tfsoff);
2265 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2266 *tfsoff += 1;
2269 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2270 tfsoff);
2272 if (fc == RPC_FC_BOGUS_STRUCT)
2274 const var_t *f;
2276 type_get_real_type(type)->ptrdesc = *tfsoff;
2277 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2279 type_t *ft = f->type;
2280 if (is_ptr(ft))
2282 if (is_string_type(f->attrs, ft))
2283 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2284 else
2285 write_pointer_tfs(file, ft, tfsoff);
2287 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2289 unsigned int absoff = ft->typestring_offset;
2290 short reloff = absoff - (*tfsoff + 2);
2291 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2292 /* FIXME: We need to store pointer attributes for arrays
2293 so we don't lose pointer_default info. */
2294 if (ptr_type == 0)
2295 ptr_type = RPC_FC_UP;
2296 print_file(file, 0, "/* %d */\n", *tfsoff);
2297 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2298 string_of_type(ptr_type));
2299 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2300 reloff, reloff, absoff);
2301 *tfsoff += 4;
2304 if (type_get_real_type(type)->ptrdesc == *tfsoff)
2305 type_get_real_type(type)->ptrdesc = 0;
2308 current_structure = save_current_structure;
2309 return start_offset;
2312 static unsigned int write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2313 unsigned char flags, unsigned int offset,
2314 unsigned int *typeformat_offset)
2316 unsigned int start_offset = *typeformat_offset;
2317 short reloff = offset - (*typeformat_offset + 2);
2318 int in_attr, out_attr;
2319 in_attr = is_attr(attrs, ATTR_IN);
2320 out_attr = is_attr(attrs, ATTR_OUT);
2321 if (!in_attr && !out_attr) in_attr = 1;
2323 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2324 flags |= 0x04;
2326 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2327 pointer_type,
2328 flags,
2329 string_of_type(pointer_type));
2330 if (file)
2332 if (flags & 0x04)
2333 fprintf(file, " [allocated_on_stack]");
2334 if (flags & 0x10)
2335 fprintf(file, " [pointer_deref]");
2336 fprintf(file, " */\n");
2339 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", reloff, offset);
2340 *typeformat_offset += 4;
2342 return start_offset;
2345 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2347 if (t == NULL)
2349 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2351 else
2353 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2355 unsigned char fc;
2356 if (type_get_type(t) == TYPE_BASIC)
2357 fc = type_basic_get_fc(t);
2358 else
2359 fc = get_enum_fc(t);
2360 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2361 fc, string_of_type(fc));
2363 else if (t->typestring_offset)
2365 short reloff = t->typestring_offset - *tfsoff;
2366 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2367 reloff, reloff, t->typestring_offset);
2369 else
2370 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2373 *tfsoff += 2;
2376 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2378 unsigned int align;
2379 unsigned int start_offset;
2380 unsigned int size;
2381 var_list_t *fields;
2382 unsigned int nbranch = 0;
2383 type_t *deftype = NULL;
2384 short nodeftype = 0xffff;
2385 var_t *f;
2387 guard_rec(type);
2389 align = 0;
2390 size = type_memsize(type, &align);
2392 fields = type_union_get_cases(type);
2394 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2396 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2397 if (cases)
2398 nbranch += list_count(cases);
2399 if (f->type)
2400 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2403 start_offset = *tfsoff;
2404 update_tfsoff(type, start_offset, file);
2405 print_start_tfs_comment(file, type, start_offset);
2406 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2408 const var_t *sv = type_union_get_switch_value(type);
2409 const type_t *st = sv->type;
2410 unsigned char fc;
2412 if (type_get_type(st) == TYPE_BASIC)
2414 switch (type_basic_get_fc(st))
2416 case RPC_FC_CHAR:
2417 case RPC_FC_SMALL:
2418 case RPC_FC_BYTE:
2419 case RPC_FC_USMALL:
2420 case RPC_FC_WCHAR:
2421 case RPC_FC_SHORT:
2422 case RPC_FC_USHORT:
2423 case RPC_FC_LONG:
2424 case RPC_FC_ULONG:
2425 fc = type_basic_get_fc(st);
2426 break;
2427 default:
2428 fc = 0;
2429 error("union switch type must be an integer, char, or enum\n");
2432 else if (type_get_type(st) == TYPE_ENUM)
2433 fc = get_enum_fc(st);
2434 else
2435 error("union switch type must be an integer, char, or enum\n");
2437 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2438 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2439 0x40 | fc, string_of_type(fc));
2440 *tfsoff += 2;
2442 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2444 static const expr_t dummy_expr; /* FIXME */
2445 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2446 unsigned char fc;
2448 if (type_get_type(st) == TYPE_BASIC)
2450 switch (type_basic_get_fc(st))
2452 case RPC_FC_CHAR:
2453 case RPC_FC_SMALL:
2454 case RPC_FC_USMALL:
2455 case RPC_FC_SHORT:
2456 case RPC_FC_USHORT:
2457 case RPC_FC_LONG:
2458 case RPC_FC_ULONG:
2459 case RPC_FC_ENUM16:
2460 case RPC_FC_ENUM32:
2461 fc = type_basic_get_fc(st);
2462 break;
2463 default:
2464 fc = 0;
2465 error("union switch type must be an integer, char, or enum\n");
2468 else if (type_get_type(st) == TYPE_ENUM)
2469 fc = get_enum_fc(st);
2470 else
2471 error("union switch type must be an integer, char, or enum\n");
2473 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2474 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2475 fc, string_of_type(fc));
2476 *tfsoff += 2;
2478 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2479 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2480 *tfsoff += 2;
2483 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2484 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2485 *tfsoff += 4;
2487 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2489 type_t *ft = f->type;
2490 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2491 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2492 expr_t *c;
2494 if (cases == NULL && !deflt)
2495 error("union field %s with neither case nor default attribute\n", f->name);
2497 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2499 /* MIDL doesn't check for duplicate cases, even though that seems
2500 like a reasonable thing to do, it just dumps them to the TFS
2501 like we're going to do here. */
2502 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2503 *tfsoff += 4;
2504 write_branch_type(file, ft, tfsoff);
2507 /* MIDL allows multiple default branches, even though that seems
2508 illogical, it just chooses the last one, which is what we will
2509 do. */
2510 if (deflt)
2512 deftype = ft;
2513 nodeftype = 0;
2517 if (deftype)
2519 write_branch_type(file, deftype, tfsoff);
2521 else
2523 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2524 *tfsoff += 2;
2527 return start_offset;
2530 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2531 unsigned int *typeformat_offset)
2533 unsigned int i;
2534 unsigned int start_offset = *typeformat_offset;
2535 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2537 if (iid)
2539 print_file(file, 2, "0x2f, /* FC_IP */\n");
2540 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2541 *typeformat_offset
2542 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2544 else
2546 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2547 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2549 if (! uuid)
2550 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2552 update_tfsoff(type, start_offset, file);
2553 print_start_tfs_comment(file, type, start_offset);
2554 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2555 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2556 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2557 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2558 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2559 for (i = 0; i < 8; ++i)
2560 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2562 if (file)
2563 fprintf(file, "\n");
2565 *typeformat_offset += 18;
2567 return start_offset;
2570 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2571 const var_t *var,
2572 unsigned int *typeformat_offset)
2574 unsigned int start_offset = *typeformat_offset;
2575 unsigned char flags = 0;
2577 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2578 flags |= NDR_STRICT_CONTEXT_HANDLE;
2580 if (is_ptr(type))
2581 flags |= 0x80;
2582 if (is_attr(var->attrs, ATTR_IN))
2584 flags |= 0x40;
2585 if (!is_attr(var->attrs, ATTR_OUT))
2586 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2588 if (is_attr(var->attrs, ATTR_OUT))
2589 flags |= 0x20;
2591 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2592 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2593 /* return and can't be null values overlap */
2594 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2595 print_file(file, 0, "can't be null, ");
2596 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2597 print_file(file, 0, "serialize, ");
2598 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2599 print_file(file, 0, "no serialize, ");
2600 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2601 print_file(file, 0, "strict, ");
2602 if ((flags & 0x21) == 0x20)
2603 print_file(file, 0, "out, ");
2604 if ((flags & 0x21) == 0x21)
2605 print_file(file, 0, "return, ");
2606 if (flags & 0x40)
2607 print_file(file, 0, "in, ");
2608 if (flags & 0x80)
2609 print_file(file, 0, "via ptr, ");
2610 print_file(file, 0, "*/\n");
2611 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2612 print_file(file, 2, "0, /* FIXME: param num */\n");
2613 *typeformat_offset += 4;
2615 return start_offset;
2618 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2619 type_t *type, const var_t *var,
2620 unsigned int *typeformat_offset)
2622 unsigned int offset;
2624 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2626 case TGT_CTXT_HANDLE:
2627 case TGT_CTXT_HANDLE_POINTER:
2628 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2629 case TGT_USER_TYPE:
2630 write_user_tfs(file, type, typeformat_offset);
2631 return type->typestring_offset;
2632 case TGT_STRING:
2633 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2634 case TGT_ARRAY:
2636 int ptr_type;
2637 unsigned int off;
2638 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2639 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2640 /* Top level pointers to conformant arrays may be handled specially
2641 since we can bypass the pointer, but if the array is buried
2642 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2643 always need to write the pointer. */
2644 if (!ptr_type && var->type != type)
2645 /* FIXME: This should use pointer_default, but the information
2646 isn't kept around for arrays. */
2647 ptr_type = RPC_FC_UP;
2648 if (ptr_type && ptr_type != RPC_FC_RP)
2650 unsigned int absoff = type->typestring_offset;
2651 short reloff = absoff - (*typeformat_offset + 2);
2652 off = *typeformat_offset;
2653 print_file(file, 0, "/* %d */\n", off);
2654 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2655 string_of_type(ptr_type));
2656 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2657 reloff, reloff, absoff);
2658 *typeformat_offset += 4;
2660 return off;
2662 case TGT_STRUCT:
2663 if (processed(type)) return type->typestring_offset;
2664 return write_struct_tfs(file, type, var->name, typeformat_offset);
2665 case TGT_UNION:
2666 if (processed(type)) return type->typestring_offset;
2667 return write_union_tfs(file, type, typeformat_offset);
2668 case TGT_ENUM:
2669 case TGT_BASIC:
2670 /* nothing to do */
2671 return 0;
2672 case TGT_IFACE_POINTER:
2673 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2674 case TGT_POINTER:
2675 if (last_ptr(type))
2677 size_t start_offset = *typeformat_offset;
2678 int in_attr = is_attr(var->attrs, ATTR_IN);
2679 int out_attr = is_attr(var->attrs, ATTR_OUT);
2680 const type_t *ref = type_pointer_get_ref(type);
2682 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2684 /* special case for pointers to base types */
2685 case TGT_BASIC:
2686 case TGT_ENUM:
2688 unsigned char fc;
2690 if (type_get_type(ref) == TYPE_ENUM)
2691 fc = get_enum_fc(ref);
2692 else
2693 fc = type_basic_get_fc(ref);
2695 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2696 get_pointer_fc(type),
2697 (!in_attr && out_attr) ? 0x0C : 0x08,
2698 string_of_type(get_pointer_fc(type)),
2699 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2700 print_file(file, indent, "0x%02x, /* %s */\n",
2701 fc, string_of_type(fc));
2702 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2703 *typeformat_offset += 4;
2704 return start_offset;
2706 default:
2707 break;
2711 offset = write_typeformatstring_var(file, indent, func,
2712 type_pointer_get_ref(type), var,
2713 typeformat_offset);
2714 if (file)
2715 fprintf(file, "/* %2u */\n", *typeformat_offset);
2716 return write_pointer_only_tfs(file, var->attrs, get_pointer_fc(type),
2717 !last_ptr(type) ? 0x10 : 0,
2718 offset, typeformat_offset);
2719 case TGT_INVALID:
2720 break;
2722 error("invalid type %s for var %s\n", type->name, var->name);
2723 return 0;
2726 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2727 const char *name, int write_ptr, unsigned int *tfsoff)
2729 int retmask = 0;
2731 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2733 case TGT_USER_TYPE:
2734 write_user_tfs(file, type, tfsoff);
2735 break;
2736 case TGT_STRING:
2737 write_string_tfs(file, attrs, type, name, tfsoff);
2738 break;
2739 case TGT_IFACE_POINTER:
2740 write_ip_tfs(file, attrs, type, tfsoff);
2741 break;
2742 case TGT_POINTER:
2744 type_t *ref = type_pointer_get_ref(type);
2746 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2747 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2749 if (write_ptr)
2750 write_pointer_tfs(file, type, tfsoff);
2752 retmask |= 1;
2753 break;
2755 case TGT_ARRAY:
2756 /* conformant arrays and strings are handled specially */
2757 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2759 write_array_tfs(file, attrs, type, name, tfsoff);
2760 if (is_conformant_array(type))
2761 retmask |= 1;
2763 break;
2764 case TGT_STRUCT:
2765 if (!processed(type))
2766 write_struct_tfs(file, type, name, tfsoff);
2767 break;
2768 case TGT_UNION:
2769 if (!processed(type))
2770 write_union_tfs(file, type, tfsoff);
2771 break;
2772 case TGT_ENUM:
2773 case TGT_BASIC:
2774 /* nothing to do */
2775 break;
2776 case TGT_CTXT_HANDLE:
2777 case TGT_CTXT_HANDLE_POINTER:
2778 case TGT_INVALID:
2779 error("invalid type %s for var %s\n", type->name, name);
2780 break;
2783 return retmask;
2786 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2787 type_pred_t pred, unsigned int *typeformat_offset)
2789 const var_t *var;
2790 const statement_t *stmt;
2792 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2794 const type_t *iface;
2795 const statement_t *stmt_func;
2797 if (stmt->type == STMT_LIBRARY)
2799 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2800 continue;
2802 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2803 continue;
2805 iface = stmt->u.type;
2806 if (!pred(iface))
2807 continue;
2809 current_iface = iface;
2810 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2812 const var_t *func = stmt_func->u.var;
2813 if (is_local(func->attrs)) continue;
2815 if (!is_void(type_function_get_rettype(func->type)))
2817 var_t v = *func;
2818 v.type = type_function_get_rettype(func->type);
2819 update_tfsoff(type_function_get_rettype(func->type),
2820 write_typeformatstring_var(
2821 file, 2, NULL,
2822 type_function_get_rettype(func->type),
2823 &v, typeformat_offset),
2824 file);
2827 current_func = func;
2828 if (type_get_function_args(func->type))
2829 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2830 update_tfsoff(
2831 var->type,
2832 write_typeformatstring_var(
2833 file, 2, func, var->type, var,
2834 typeformat_offset),
2835 file);
2839 return *typeformat_offset + 1;
2842 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2844 unsigned int typeformat_offset = 2;
2846 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2850 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2852 int indent = 0;
2854 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2855 print_file(file, indent, "{\n");
2856 indent++;
2857 print_file(file, indent, "0,\n");
2858 print_file(file, indent, "{\n");
2859 indent++;
2860 print_file(file, indent, "NdrFcShort(0x0),\n");
2862 set_all_tfswrite(TRUE);
2863 process_tfs(file, stmts, pred);
2865 print_file(file, indent, "0x0\n");
2866 indent--;
2867 print_file(file, indent, "}\n");
2868 indent--;
2869 print_file(file, indent, "};\n");
2870 print_file(file, indent, "\n");
2873 static unsigned int get_required_buffer_size_type(
2874 const type_t *type, const char *name, unsigned int *alignment)
2876 *alignment = 0;
2877 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2879 case TGT_USER_TYPE:
2881 const char *uname;
2882 const type_t *utype = get_user_type(type, &uname);
2883 return get_required_buffer_size_type(utype, uname, alignment);
2885 case TGT_BASIC:
2886 switch (type_basic_get_fc(type))
2888 case RPC_FC_BYTE:
2889 case RPC_FC_CHAR:
2890 case RPC_FC_USMALL:
2891 case RPC_FC_SMALL:
2892 *alignment = 4;
2893 return 1;
2895 case RPC_FC_WCHAR:
2896 case RPC_FC_USHORT:
2897 case RPC_FC_SHORT:
2898 *alignment = 4;
2899 return 2;
2901 case RPC_FC_ULONG:
2902 case RPC_FC_LONG:
2903 case RPC_FC_FLOAT:
2904 case RPC_FC_ERROR_STATUS_T:
2905 *alignment = 4;
2906 return 4;
2908 case RPC_FC_HYPER:
2909 case RPC_FC_DOUBLE:
2910 *alignment = 8;
2911 return 8;
2913 case RPC_FC_IGNORE:
2914 case RPC_FC_BIND_PRIMITIVE:
2915 return 0;
2917 default:
2918 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2919 type_basic_get_fc(type));
2920 return 0;
2922 break;
2924 case TGT_ENUM:
2925 switch (get_enum_fc(type))
2927 case RPC_FC_ENUM32:
2928 *alignment = 4;
2929 return 4;
2930 case RPC_FC_ENUM16:
2931 *alignment = 4;
2932 return 2;
2934 break;
2936 case TGT_STRUCT:
2937 if (get_struct_fc(type) == RPC_FC_STRUCT)
2939 if (!type_struct_get_fields(type)) return 0;
2940 return fields_memsize(type_struct_get_fields(type), alignment);
2942 break;
2944 case TGT_POINTER:
2945 if (get_pointer_fc(type) == RPC_FC_RP)
2947 const type_t *ref = type_pointer_get_ref(type);
2948 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2950 case TGT_BASIC:
2951 case TGT_ENUM:
2952 return get_required_buffer_size_type( ref, name, alignment );
2953 case TGT_STRUCT:
2954 if (get_struct_fc(ref) == RPC_FC_STRUCT)
2955 return get_required_buffer_size_type( ref, name, alignment );
2956 break;
2957 case TGT_USER_TYPE:
2958 case TGT_CTXT_HANDLE:
2959 case TGT_CTXT_HANDLE_POINTER:
2960 case TGT_STRING:
2961 case TGT_POINTER:
2962 case TGT_ARRAY:
2963 case TGT_IFACE_POINTER:
2964 case TGT_UNION:
2965 case TGT_INVALID:
2966 break;
2969 break;
2971 case TGT_ARRAY:
2972 /* FIXME: depends on pointer type */
2973 return type_array_get_dim(type) *
2974 get_required_buffer_size_type(type_array_get_element(type), name, alignment);
2976 default:
2977 break;
2979 return 0;
2982 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2984 int in_attr = is_attr(var->attrs, ATTR_IN);
2985 int out_attr = is_attr(var->attrs, ATTR_OUT);
2987 if (!in_attr && !out_attr)
2988 in_attr = 1;
2990 *alignment = 0;
2992 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2993 pass == PASS_RETURN)
2995 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2997 *alignment = 4;
2998 return 20;
3001 if (!is_string_type(var->attrs, var->type))
3002 return get_required_buffer_size_type(var->type, var->name,
3003 alignment);
3005 return 0;
3008 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3010 const var_t *var;
3011 unsigned int total_size = 0, alignment;
3013 if (type_get_function_args(func->type))
3015 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3017 total_size += get_required_buffer_size(var, &alignment, pass);
3018 total_size += alignment;
3022 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3024 var_t v = *func;
3025 v.type = type_function_get_rettype(func->type);
3026 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3027 total_size += alignment;
3029 return total_size;
3032 static void print_phase_function(FILE *file, int indent, const char *type,
3033 const char *local_var_prefix, enum remoting_phase phase,
3034 const var_t *var, unsigned int type_offset)
3036 const char *function;
3037 switch (phase)
3039 case PHASE_BUFFERSIZE:
3040 function = "BufferSize";
3041 break;
3042 case PHASE_MARSHAL:
3043 function = "Marshall";
3044 break;
3045 case PHASE_UNMARSHAL:
3046 function = "Unmarshall";
3047 break;
3048 case PHASE_FREE:
3049 function = "Free";
3050 break;
3051 default:
3052 assert(0);
3053 return;
3056 print_file(file, indent, "Ndr%s%s(\n", type, function);
3057 indent++;
3058 print_file(file, indent, "&__frame->_StubMsg,\n");
3059 print_file(file, indent, "%s%s%s%s%s,\n",
3060 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3061 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3062 local_var_prefix,
3063 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3064 var->name);
3065 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3066 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3067 if (phase == PHASE_UNMARSHAL)
3068 print_file(file, indent, "0);\n");
3069 indent--;
3072 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3073 enum remoting_phase phase, enum pass pass, const var_t *var,
3074 const char *varname)
3076 type_t *type = var->type;
3077 unsigned int size;
3078 unsigned int alignment = 0;
3079 const type_t *ref;
3081 /* no work to do for other phases, buffer sizing is done elsewhere */
3082 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3083 return;
3085 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3086 if (type_get_type(ref) == TYPE_ENUM)
3088 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3090 size = 4;
3091 alignment = 4;
3093 else /* RPC_FC_ENUM16 */
3095 size = 2;
3096 alignment = 2;
3099 else
3101 switch (type_basic_get_fc(ref))
3103 case RPC_FC_BYTE:
3104 case RPC_FC_CHAR:
3105 case RPC_FC_SMALL:
3106 case RPC_FC_USMALL:
3107 size = 1;
3108 alignment = 1;
3109 break;
3111 case RPC_FC_WCHAR:
3112 case RPC_FC_USHORT:
3113 case RPC_FC_SHORT:
3114 size = 2;
3115 alignment = 2;
3116 break;
3118 case RPC_FC_ULONG:
3119 case RPC_FC_LONG:
3120 case RPC_FC_FLOAT:
3121 case RPC_FC_ERROR_STATUS_T:
3122 size = 4;
3123 alignment = 4;
3124 break;
3126 case RPC_FC_HYPER:
3127 case RPC_FC_DOUBLE:
3128 size = 8;
3129 alignment = 8;
3130 break;
3132 case RPC_FC_IGNORE:
3133 case RPC_FC_BIND_PRIMITIVE:
3134 /* no marshalling needed */
3135 return;
3137 default:
3138 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3139 var->name, type_basic_get_fc(ref));
3140 size = 0;
3144 if (phase == PHASE_MARSHAL)
3145 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3146 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3147 alignment - 1, alignment - 1);
3149 if (phase == PHASE_MARSHAL)
3151 print_file(file, indent, "*(");
3152 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3153 if (is_ptr(type))
3154 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3155 else
3156 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3157 fprintf(file, "%s%s", local_var_prefix, varname);
3158 fprintf(file, ";\n");
3160 else if (phase == PHASE_UNMARSHAL)
3162 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3163 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3164 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3165 print_file(file, indent, "{\n");
3166 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3167 print_file(file, indent, "}\n");
3168 print_file(file, indent, "%s%s%s",
3169 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3170 local_var_prefix, varname);
3171 if (pass == PASS_IN && is_ptr(type))
3172 fprintf(file, " = (");
3173 else
3174 fprintf(file, " = *(");
3175 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3176 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3179 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3180 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3181 fprintf(file, ");\n");
3184 /* returns whether the MaxCount, Offset or ActualCount members need to be
3185 * filled in for the specified phase */
3186 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3188 return (phase != PHASE_UNMARSHAL);
3191 expr_t *get_size_is_expr(const type_t *t, const char *name)
3193 expr_t *x = NULL;
3195 for ( ; is_array(t); t = type_array_get_element(t))
3196 if (type_array_has_conformance(t))
3198 if (!x)
3199 x = type_array_get_conformance(t);
3200 else
3201 error("%s: multidimensional conformant"
3202 " arrays not supported at the top level\n",
3203 name);
3206 return x;
3209 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3210 enum remoting_phase phase, const var_t *var)
3212 const type_t *type = var->type;
3213 /* get fundamental type for the argument */
3214 for (;;)
3216 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3217 break;
3218 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3219 break;
3220 else if (type_is_alias(type))
3221 type = type_alias_get_aliasee(type);
3222 else if (is_array(type) || is_string_type(var->attrs, type))
3224 if (is_conformance_needed_for_phase(phase) && is_array(type))
3226 if (type_array_has_conformance(type))
3228 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3229 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3230 fprintf(file, ";\n\n");
3232 if (type_array_has_variance(type))
3234 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3235 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3236 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3237 fprintf(file, ";\n\n");
3240 break;
3242 else if (type_get_type(type) == TYPE_UNION)
3244 if (is_conformance_needed_for_phase(phase))
3246 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3247 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3248 fprintf(file, ";\n\n");
3250 break;
3252 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3254 expr_t *iid;
3256 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3258 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3259 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3260 fprintf( file, ";\n\n" );
3262 break;
3264 else if (is_ptr(type))
3265 type = type_pointer_get_ref(type);
3266 else
3267 break;
3271 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3272 enum pass pass, enum remoting_phase phase, const var_t *var)
3274 int in_attr, out_attr, pointer_type;
3275 const type_t *type = var->type;
3276 unsigned int start_offset = type->typestring_offset;
3278 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3279 if (!pointer_type)
3280 pointer_type = RPC_FC_RP;
3282 in_attr = is_attr(var->attrs, ATTR_IN);
3283 out_attr = is_attr(var->attrs, ATTR_OUT);
3284 if (!in_attr && !out_attr)
3285 in_attr = 1;
3287 if (phase != PHASE_FREE)
3288 switch (pass)
3290 case PASS_IN:
3291 if (!in_attr) return;
3292 break;
3293 case PASS_OUT:
3294 if (!out_attr) return;
3295 break;
3296 case PASS_RETURN:
3297 break;
3300 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3302 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3304 case TGT_CTXT_HANDLE:
3305 case TGT_CTXT_HANDLE_POINTER:
3306 if (phase == PHASE_MARSHAL)
3308 if (pass == PASS_IN)
3310 /* if the context_handle attribute appears in the chain of types
3311 * without pointers being followed, then the context handle must
3312 * be direct, otherwise it is a pointer */
3313 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3314 print_file(file, indent, "NdrClientContextMarshall(\n");
3315 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3316 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3317 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3319 else
3321 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3322 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3323 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3324 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3325 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3328 else if (phase == PHASE_UNMARSHAL)
3330 if (pass == PASS_OUT)
3332 if (!in_attr)
3333 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3334 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3335 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3336 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3337 print_file(file, indent + 1, "__frame->_Handle);\n");
3339 else
3341 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3342 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3343 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3346 break;
3347 case TGT_USER_TYPE:
3348 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3349 break;
3350 case TGT_STRING:
3351 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3352 pointer_type != RPC_FC_RP)
3354 unsigned int ptr_start_offset = (start_offset - (is_conformant_array(type) ? 4 : 2));
3355 print_phase_function(file, indent, "Pointer", local_var_prefix,
3356 phase, var, ptr_start_offset);
3358 else
3360 if (is_array(type) && !is_conformant_array(type))
3361 print_phase_function(file, indent, "NonConformantString",
3362 local_var_prefix, phase, var,
3363 start_offset);
3364 else
3365 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3366 phase, var, start_offset);
3368 break;
3369 case TGT_ARRAY:
3371 unsigned char tc = get_array_fc(type);
3372 const char *array_type = "FixedArray";
3374 /* We already have the size_is expression since it's at the
3375 top level, but do checks for multidimensional conformant
3376 arrays. When we handle them, we'll need to extend this
3377 function to return a list, and then we'll actually use
3378 the return value. */
3379 get_size_is_expr(type, var->name);
3381 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3383 array_type = "VaryingArray";
3385 else if (tc == RPC_FC_CARRAY)
3387 array_type = "ConformantArray";
3389 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3391 array_type = (tc == RPC_FC_BOGUS_ARRAY
3392 ? "ComplexArray"
3393 : "ConformantVaryingArray");
3396 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3397 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3398 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3400 /* these are all unmarshalled by allocating memory */
3401 if (tc == RPC_FC_BOGUS_ARRAY ||
3402 tc == RPC_FC_CVARRAY ||
3403 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3404 (tc == RPC_FC_CARRAY && !in_attr))
3406 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3407 indent++;
3408 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3411 break;
3413 case TGT_BASIC:
3414 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3415 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3416 break;
3417 case TGT_ENUM:
3418 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3420 if (phase == PHASE_MARSHAL)
3421 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3422 else
3423 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3424 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3425 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3426 local_var_prefix,
3427 var->name);
3428 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3430 break;
3431 case TGT_STRUCT:
3432 switch (get_struct_fc(type))
3434 case RPC_FC_STRUCT:
3435 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3436 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3437 break;
3438 case RPC_FC_PSTRUCT:
3439 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3440 break;
3441 case RPC_FC_CSTRUCT:
3442 case RPC_FC_CPSTRUCT:
3443 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3444 break;
3445 case RPC_FC_CVSTRUCT:
3446 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3447 break;
3448 case RPC_FC_BOGUS_STRUCT:
3449 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3450 break;
3451 default:
3452 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3454 break;
3455 case TGT_UNION:
3457 const char *union_type = NULL;
3459 if (type_get_type(type) == TYPE_UNION)
3460 union_type = "NonEncapsulatedUnion";
3461 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3462 union_type = "EncapsulatedUnion";
3464 print_phase_function(file, indent, union_type, local_var_prefix,
3465 phase, var, start_offset);
3466 break;
3468 case TGT_POINTER:
3470 const type_t *ref = type_pointer_get_ref(type);
3471 if (get_pointer_fc(type) == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3473 case TYPE_BASIC:
3474 /* base types have known sizes, so don't need a sizing pass
3475 * and don't have any memory to free and so don't need a
3476 * freeing pass */
3477 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3478 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3479 break;
3480 case TYPE_ENUM:
3481 /* base types have known sizes, so don't need a sizing pass
3482 * and don't have any memory to free and so don't need a
3483 * freeing pass */
3484 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3485 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3486 break;
3487 case TYPE_STRUCT:
3489 const char *struct_type = NULL;
3490 switch (get_struct_fc(ref))
3492 case RPC_FC_STRUCT:
3493 /* simple structs have known sizes, so don't need a sizing
3494 * pass and don't have any memory to free and so don't
3495 * need a freeing pass */
3496 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3497 struct_type = "SimpleStruct";
3498 break;
3499 case RPC_FC_PSTRUCT:
3500 struct_type = "SimpleStruct";
3501 break;
3502 case RPC_FC_CSTRUCT:
3503 case RPC_FC_CPSTRUCT:
3504 struct_type = "ConformantStruct";
3505 break;
3506 case RPC_FC_CVSTRUCT:
3507 struct_type = "ConformantVaryingStruct";
3508 break;
3509 case RPC_FC_BOGUS_STRUCT:
3510 struct_type = "ComplexStruct";
3511 break;
3512 default:
3513 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3516 if (struct_type)
3518 if (phase == PHASE_FREE)
3519 struct_type = "Pointer";
3520 else
3521 start_offset = ref->typestring_offset;
3522 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3524 break;
3526 case TYPE_UNION:
3527 case TYPE_ENCAPSULATED_UNION:
3529 const char *union_type = NULL;
3530 if (phase == PHASE_FREE)
3531 union_type = "Pointer";
3532 else
3534 if (type_get_type(ref) == TYPE_UNION)
3535 union_type = "NonEncapsulatedUnion";
3536 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3537 union_type = "EncapsulatedUnion";
3539 start_offset = ref->typestring_offset;
3542 print_phase_function(file, indent, union_type, local_var_prefix,
3543 phase, var, start_offset);
3544 break;
3546 case TYPE_POINTER:
3547 case TYPE_ARRAY:
3548 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3549 break;
3550 case TYPE_VOID:
3551 case TYPE_ALIAS:
3552 case TYPE_MODULE:
3553 case TYPE_COCLASS:
3554 case TYPE_FUNCTION:
3555 case TYPE_INTERFACE:
3556 assert(0);
3557 break;
3559 else
3560 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3561 break;
3563 case TGT_IFACE_POINTER:
3564 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3565 break;
3566 case TGT_INVALID:
3567 assert(0);
3568 break;
3570 fprintf(file, "\n");
3573 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3574 enum pass pass, enum remoting_phase phase)
3576 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3578 unsigned int size = get_function_buffer_size( func, pass );
3579 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3582 if (pass == PASS_RETURN)
3584 var_t var;
3585 var = *func;
3586 var.type = type_function_get_rettype(func->type);
3587 var.name = xstrdup( "_RetVal" );
3588 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3589 free( var.name );
3591 else
3593 const var_t *var;
3594 if (!type_get_function_args(func->type))
3595 return;
3596 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3597 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3602 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3604 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3608 unsigned int get_size_procformatstring_func(const var_t *func)
3610 const var_t *var;
3611 unsigned int size = 0;
3613 /* argument list size */
3614 if (type_get_function_args(func->type))
3615 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3616 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3618 /* return value size */
3619 if (is_void(type_function_get_rettype(func->type)))
3620 size += 2; /* FC_END and FC_PAD */
3621 else
3622 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3624 return size;
3627 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3629 const statement_t *stmt;
3630 unsigned int size = 1;
3632 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3634 const type_t *iface;
3635 const statement_t *stmt_func;
3637 if (stmt->type == STMT_LIBRARY)
3639 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3640 continue;
3642 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3643 continue;
3645 iface = stmt->u.type;
3646 if (!pred(iface))
3647 continue;
3649 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3651 const var_t *func = stmt_func->u.var;
3652 if (!is_local(func->attrs))
3653 size += get_size_procformatstring_func( func );
3656 return size;
3659 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3661 set_all_tfswrite(FALSE);
3662 return process_tfs(NULL, stmts, pred);
3665 void declare_stub_args( FILE *file, int indent, const var_t *func )
3667 int in_attr, out_attr;
3668 int i = 0;
3669 const var_t *var;
3671 /* declare return value '_RetVal' */
3672 if (!is_void(type_function_get_rettype(func->type)))
3674 print_file(file, indent, "%s", "");
3675 write_type_decl_left(file, type_function_get_rettype(func->type));
3676 fprintf(file, " _RetVal;\n");
3679 if (!type_get_function_args(func->type))
3680 return;
3682 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3684 int is_string = is_string_type(var->attrs, var->type);
3686 in_attr = is_attr(var->attrs, ATTR_IN);
3687 out_attr = is_attr(var->attrs, ATTR_OUT);
3688 if (!out_attr && !in_attr)
3689 in_attr = 1;
3691 if (is_context_handle(var->type))
3692 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3693 else
3695 if (!in_attr && !is_conformant_array(var->type) && !is_string)
3697 type_t *type_to_print;
3698 char name[16];
3699 print_file(file, indent, "%s", "");
3700 if (type_get_type(var->type) == TYPE_ARRAY &&
3701 !type_array_is_decl_as_ptr(var->type))
3702 type_to_print = var->type;
3703 else
3704 type_to_print = type_pointer_get_ref(var->type);
3705 sprintf(name, "_W%u", i++);
3706 write_type_decl(file, type_to_print, name);
3707 fprintf(file, ";\n");
3710 print_file(file, indent, "%s", "");
3711 write_type_decl_left(file, var->type);
3712 fprintf(file, " ");
3713 if (type_get_type(var->type) == TYPE_ARRAY &&
3714 !type_array_is_decl_as_ptr(var->type)) {
3715 fprintf(file, "(*%s)", var->name);
3716 } else
3717 fprintf(file, "%s", var->name);
3718 write_type_right(file, var->type, FALSE);
3719 fprintf(file, ";\n");
3721 if (decl_indirect(var->type))
3722 print_file(file, indent, "void *_p_%s;\n", var->name);
3728 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3730 int in_attr, out_attr;
3731 int i = 0, sep = 0;
3732 const var_t *var;
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 int is_string = is_string_type(var->attrs, var->type);
3740 in_attr = is_attr(var->attrs, ATTR_IN);
3741 out_attr = is_attr(var->attrs, ATTR_OUT);
3742 if (!out_attr && !in_attr)
3743 in_attr = 1;
3745 if (!in_attr)
3747 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3749 if (is_context_handle(var->type))
3751 fprintf(file, " = NdrContextHandleInitialize(\n");
3752 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3753 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3754 var->type->typestring_offset);
3756 else if (is_array(var->type) &&
3757 type_array_has_conformance(var->type))
3759 unsigned int size, align = 0;
3760 type_t *type = var->type;
3762 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3763 for ( ;
3764 is_array(type) && type_array_has_conformance(type);
3765 type = type_array_get_element(type))
3767 write_expr(file, type_array_get_conformance(type), TRUE,
3768 TRUE, NULL, NULL, local_var_prefix);
3769 fprintf(file, " * ");
3771 size = type_memsize(type, &align);
3772 fprintf(file, "%u);\n", size);
3774 else if (!is_string)
3776 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3777 if (is_ptr(var->type) && !last_ptr(var->type))
3778 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3779 i++;
3782 sep = 1;
3785 if (sep)
3786 fprintf(file, "\n");
3790 int write_expr_eval_routines(FILE *file, const char *iface)
3792 static const char *var_name = "pS";
3793 static const char *var_name_expr = "pS->";
3794 int result = 0;
3795 struct expr_eval_routine *eval;
3796 unsigned short callback_offset = 0;
3798 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3800 const char *name = eval->structure->name;
3801 result = 1;
3803 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3804 iface, name, callback_offset);
3805 print_file(file, 0, "{\n");
3806 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3807 name, var_name, name, eval->baseoff);
3808 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3809 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3810 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3811 fprintf(file, ";\n");
3812 print_file(file, 0, "}\n\n");
3813 callback_offset++;
3815 return result;
3818 void write_expr_eval_routine_list(FILE *file, const char *iface)
3820 struct expr_eval_routine *eval;
3821 struct expr_eval_routine *cursor;
3822 unsigned short callback_offset = 0;
3824 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3825 fprintf(file, "{\n");
3827 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3829 const char *name = eval->structure->name;
3830 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3831 callback_offset++;
3832 list_remove(&eval->entry);
3833 free(eval);
3836 fprintf(file, "};\n\n");
3839 void write_user_quad_list(FILE *file)
3841 user_type_t *ut;
3843 if (list_empty(&user_type_list))
3844 return;
3846 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3847 fprintf(file, "{\n");
3848 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3850 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3851 print_file(file, 1, "{\n");
3852 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3853 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3854 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3855 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3856 print_file(file, 1, "}%s\n", sep);
3858 fprintf(file, "};\n\n");
3861 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3863 const struct str_list_entry_t *endpoint;
3864 const char *p;
3866 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3867 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3868 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3870 print_file( f, 1, "{ (const unsigned char *)\"" );
3871 for (p = endpoint->str; *p && *p != ':'; p++)
3873 if (*p == '"' || *p == '\\') fputc( '\\', f );
3874 fputc( *p, f );
3876 if (!*p) goto error;
3877 if (p[1] != '[') goto error;
3879 fprintf( f, "\", (const unsigned char *)\"" );
3880 for (p += 2; *p && *p != ']'; p++)
3882 if (*p == '"' || *p == '\\') fputc( '\\', f );
3883 fputc( *p, f );
3885 if (*p != ']') goto error;
3886 fprintf( f, "\" },\n" );
3888 print_file( f, 0, "};\n\n" );
3889 return;
3891 error:
3892 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3895 void write_exceptions( FILE *file )
3897 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3898 fprintf( file, "\n");
3899 fprintf( file, "#include \"wine/exception.h\"\n");
3900 fprintf( file, "#undef RpcTryExcept\n");
3901 fprintf( file, "#undef RpcExcept\n");
3902 fprintf( file, "#undef RpcEndExcept\n");
3903 fprintf( file, "#undef RpcTryFinally\n");
3904 fprintf( file, "#undef RpcFinally\n");
3905 fprintf( file, "#undef RpcEndFinally\n");
3906 fprintf( file, "#undef RpcExceptionCode\n");
3907 fprintf( file, "#undef RpcAbnormalTermination\n");
3908 fprintf( file, "\n");
3909 fprintf( file, "struct __exception_frame;\n");
3910 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3911 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3912 fprintf( file, "\n");
3913 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3914 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3915 fprintf( file, " __filter_func filter; \\\n");
3916 fprintf( file, " __finally_func finally; \\\n");
3917 fprintf( file, " sigjmp_buf jmp; \\\n");
3918 fprintf( file, " DWORD code; \\\n");
3919 fprintf( file, " unsigned char abnormal_termination; \\\n");
3920 fprintf( file, " unsigned char filter_level; \\\n");
3921 fprintf( file, " unsigned char finally_level;\n");
3922 fprintf( file, "\n");
3923 fprintf( file, "struct __exception_frame\n{\n");
3924 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3925 fprintf( file, "};\n");
3926 fprintf( file, "\n");
3927 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3928 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3929 fprintf( file, " CONTEXT *context,\n");
3930 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3931 fprintf( file, "{\n");
3932 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3933 fprintf( file, "\n");
3934 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3935 fprintf( file, " {\n" );
3936 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3937 fprintf( file, " {\n" );
3938 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3939 fprintf( file, " exc_frame->finally( exc_frame );\n");
3940 fprintf( file, " }\n" );
3941 fprintf( file, " return ExceptionContinueSearch;\n");
3942 fprintf( file, " }\n" );
3943 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3944 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3945 fprintf( file, " {\n");
3946 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3947 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3948 fprintf( file, " {\n");
3949 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3950 fprintf( file, " exc_frame->finally( exc_frame );\n");
3951 fprintf( file, " __wine_pop_frame( frame );\n");
3952 fprintf( file, " }\n");
3953 fprintf( file, " exc_frame->filter_level = 0;\n");
3954 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3955 fprintf( file, " }\n");
3956 fprintf( file, " return ExceptionContinueSearch;\n");
3957 fprintf( file, "}\n");
3958 fprintf( file, "\n");
3959 fprintf( file, "#define RpcTryExcept \\\n");
3960 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3961 fprintf( file, " { \\\n");
3962 fprintf( file, " if (!__frame->finally_level) \\\n" );
3963 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3964 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3965 fprintf( file, "\n");
3966 fprintf( file, "#define RpcExcept(expr) \\\n");
3967 fprintf( file, " if (!__frame->finally_level) \\\n" );
3968 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3969 fprintf( file, " __frame->filter_level = 0; \\\n" );
3970 fprintf( file, " } \\\n");
3971 fprintf( file, " else \\\n");
3972 fprintf( file, "\n");
3973 fprintf( file, "#define RpcEndExcept\n");
3974 fprintf( file, "\n");
3975 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3976 fprintf( file, "\n");
3977 fprintf( file, "#define RpcTryFinally \\\n");
3978 fprintf( file, " if (!__frame->filter_level) \\\n");
3979 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3980 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3981 fprintf( file, "\n");
3982 fprintf( file, "#define RpcFinally \\\n");
3983 fprintf( file, " if (!__frame->filter_level) \\\n");
3984 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3985 fprintf( file, " __frame->finally_level = 0;\n");
3986 fprintf( file, "\n");
3987 fprintf( file, "#define RpcEndFinally\n");
3988 fprintf( file, "\n");
3989 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3990 fprintf( file, "\n");
3991 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3992 fprintf( file, " do { \\\n");
3993 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3994 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3995 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3996 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3997 fprintf( file, " __frame->filter_level = 0; \\\n");
3998 fprintf( file, " __frame->finally_level = 0; \\\n");
3999 fprintf( file, " } while (0)\n");
4000 fprintf( file, "\n");
4001 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4002 fprintf( file, "\n");
4003 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4004 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4005 fprintf( file, "\n");
4006 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4007 fprintf( file, " DWORD code;\n");
4008 fprintf( file, "\n");
4009 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");