winecrt0: Add entry point code for stand-alone 16-bit executables.
[wine/multimedia.git] / tools / widl / typegen.c
blobdead790a3e0319d5c2a92b2ca71d446ebc224b15
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 && t->declarray)
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->declarray))
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 (t->declarray)
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) || (!type->declarray && is_conformant_array(type)))
1483 unsigned int memsize;
1485 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1486 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1488 /* pointer instance */
1489 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1490 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1491 *typestring_offset += 6;
1493 if (is_ptr(type))
1495 if (is_string_type(type->attrs, type))
1496 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1497 else
1498 write_pointer_tfs(file, type, typestring_offset);
1500 else
1502 unsigned absoff = type->typestring_offset;
1503 short reloff = absoff - (*typestring_offset + 2);
1504 /* FIXME: get pointer attributes from field */
1505 print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
1506 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1507 reloff, reloff, absoff);
1508 *typestring_offset += 4;
1511 align = 0;
1512 memsize = type_memsize(type, &align);
1513 *offset_in_memory += memsize;
1514 /* increment these separately as in the case of conformant (varying)
1515 * structures these start at different values */
1516 *offset_in_buffer += memsize;
1518 return 1;
1521 if (is_non_complex_struct(type))
1523 const var_t *v;
1524 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1526 if (offset_in_memory && offset_in_buffer)
1528 unsigned int padding;
1529 align = 0;
1530 type_memsize(v->type, &align);
1531 padding = ROUNDING(*offset_in_memory, align);
1532 *offset_in_memory += padding;
1533 *offset_in_buffer += padding;
1535 written += write_no_repeat_pointer_descriptions(
1536 file, v->type,
1537 offset_in_memory, offset_in_buffer, typestring_offset);
1540 else
1542 unsigned int memsize;
1543 align = 0;
1544 memsize = type_memsize(type, &align);
1545 *offset_in_memory += memsize;
1546 /* increment these separately as in the case of conformant (varying)
1547 * structures these start at different values */
1548 *offset_in_buffer += memsize;
1551 return written;
1554 static int write_pointer_description_offsets(
1555 FILE *file, const attr_list_t *attrs, type_t *type,
1556 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1557 unsigned int *typestring_offset)
1559 int written = 0;
1560 unsigned int align;
1562 if (is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE)
1564 type_t *ref = type_pointer_get_ref(type);
1566 if (offset_in_memory && offset_in_buffer)
1568 unsigned int memsize;
1570 /* pointer instance */
1571 /* FIXME: sometimes from end of structure, sometimes from beginning */
1572 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1573 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1575 align = 0;
1576 memsize = type_memsize(type, &align);
1577 *offset_in_memory += memsize;
1578 /* increment these separately as in the case of conformant (varying)
1579 * structures these start at different values */
1580 *offset_in_buffer += memsize;
1582 *typestring_offset += 4;
1584 if (is_string_type(attrs, type))
1585 write_string_tfs(file, NULL, type, NULL, typestring_offset);
1586 else if (processed(ref) || type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1587 write_pointer_tfs(file, type, typestring_offset);
1588 else
1589 error("write_pointer_description_offsets: type format string unknown\n");
1591 return 1;
1594 if (is_array(type))
1596 return write_pointer_description_offsets(
1597 file, attrs, type_array_get_element(type), offset_in_memory,
1598 offset_in_buffer, typestring_offset);
1600 else if (is_non_complex_struct(type))
1602 /* otherwise search for interesting fields to parse */
1603 const var_t *v;
1604 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1606 if (offset_in_memory && offset_in_buffer)
1608 unsigned int padding;
1609 align = 0;
1610 type_memsize(v->type, &align);
1611 padding = ROUNDING(*offset_in_memory, align);
1612 *offset_in_memory += padding;
1613 *offset_in_buffer += padding;
1615 written += write_pointer_description_offsets(
1616 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1617 typestring_offset);
1620 else
1622 if (offset_in_memory && offset_in_buffer)
1624 unsigned int memsize;
1625 align = 0;
1626 memsize = type_memsize(type, &align);
1627 *offset_in_memory += memsize;
1628 /* increment these separately as in the case of conformant (varying)
1629 * structures these start at different values */
1630 *offset_in_buffer += memsize;
1634 return written;
1637 /* Note: if file is NULL return value is number of pointers to write, else
1638 * it is the number of type format characters written */
1639 static int write_fixed_array_pointer_descriptions(
1640 FILE *file, const attr_list_t *attrs, type_t *type,
1641 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1642 unsigned int *typestring_offset)
1644 unsigned int align;
1645 int pointer_count = 0;
1647 if (type_get_type(type) == TYPE_ARRAY &&
1648 !type_array_has_conformance(type) && !type_array_has_variance(type))
1650 unsigned int temp = 0;
1651 /* unfortunately, this needs to be done in two passes to avoid
1652 * writing out redundant FC_FIXED_REPEAT descriptions */
1653 pointer_count = write_pointer_description_offsets(
1654 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1655 if (pointer_count > 0)
1657 unsigned int increment_size;
1658 unsigned int offset_of_array_pointer_mem = 0;
1659 unsigned int offset_of_array_pointer_buf = 0;
1661 align = 0;
1662 increment_size = type_memsize(type_array_get_element(type), &align);
1664 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1665 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1666 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1667 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1668 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1669 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1670 *typestring_offset += 10;
1672 pointer_count = write_pointer_description_offsets(
1673 file, attrs, type, &offset_of_array_pointer_mem,
1674 &offset_of_array_pointer_buf, typestring_offset);
1677 else if (type_get_type(type) == TYPE_STRUCT)
1679 const var_t *v;
1680 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1682 if (offset_in_memory && offset_in_buffer)
1684 unsigned int padding;
1685 align = 0;
1686 type_memsize(v->type, &align);
1687 padding = ROUNDING(*offset_in_memory, align);
1688 *offset_in_memory += padding;
1689 *offset_in_buffer += padding;
1691 pointer_count += write_fixed_array_pointer_descriptions(
1692 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1693 typestring_offset);
1696 else
1698 if (offset_in_memory && offset_in_buffer)
1700 unsigned int memsize;
1701 align = 0;
1702 memsize = type_memsize(type, &align);
1703 *offset_in_memory += memsize;
1704 /* increment these separately as in the case of conformant (varying)
1705 * structures these start at different values */
1706 *offset_in_buffer += memsize;
1710 return pointer_count;
1713 /* Note: if file is NULL return value is number of pointers to write, else
1714 * it is the number of type format characters written */
1715 static int write_conformant_array_pointer_descriptions(
1716 FILE *file, const attr_list_t *attrs, type_t *type,
1717 unsigned int offset_in_memory, unsigned int *typestring_offset)
1719 unsigned int align;
1720 int pointer_count = 0;
1722 if (is_conformant_array(type) && !type_array_has_variance(type))
1724 unsigned int temp = 0;
1725 /* unfortunately, this needs to be done in two passes to avoid
1726 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1727 pointer_count = write_pointer_description_offsets(
1728 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1729 if (pointer_count > 0)
1731 unsigned int increment_size;
1732 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1733 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1735 align = 0;
1736 increment_size = type_memsize(type_array_get_element(type), &align);
1738 if (increment_size > USHRT_MAX)
1739 error("array size of %u bytes is too large\n", increment_size);
1741 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1742 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1743 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1744 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1745 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1746 *typestring_offset += 8;
1748 pointer_count = write_pointer_description_offsets(
1749 file, attrs, type_array_get_element(type),
1750 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1751 typestring_offset);
1755 return pointer_count;
1758 /* Note: if file is NULL return value is number of pointers to write, else
1759 * it is the number of type format characters written */
1760 static int write_varying_array_pointer_descriptions(
1761 FILE *file, const attr_list_t *attrs, type_t *type,
1762 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1763 unsigned int *typestring_offset)
1765 unsigned int align;
1766 int pointer_count = 0;
1768 if (is_array(type) && type_array_has_variance(type))
1770 unsigned int temp = 0;
1771 /* unfortunately, this needs to be done in two passes to avoid
1772 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1773 pointer_count = write_pointer_description_offsets(
1774 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1775 if (pointer_count > 0)
1777 unsigned int increment_size;
1779 align = 0;
1780 increment_size = type_memsize(type_array_get_element(type), &align);
1782 if (increment_size > USHRT_MAX)
1783 error("array size of %u bytes is too large\n", increment_size);
1785 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1786 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1787 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1788 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1789 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1790 *typestring_offset += 8;
1792 pointer_count = write_pointer_description_offsets(
1793 file, attrs, type, offset_in_memory,
1794 offset_in_buffer, typestring_offset);
1797 else if (type_get_type(type) == TYPE_STRUCT)
1799 const var_t *v;
1800 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1802 if (offset_in_memory && offset_in_buffer)
1804 unsigned int padding;
1806 if (is_array(v->type) && type_array_has_variance(v->type))
1808 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1809 /* skip over variance and offset in buffer */
1810 *offset_in_buffer += 8;
1813 align = 0;
1814 type_memsize(v->type, &align);
1815 padding = ROUNDING(*offset_in_memory, align);
1816 *offset_in_memory += padding;
1817 *offset_in_buffer += padding;
1819 pointer_count += write_varying_array_pointer_descriptions(
1820 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1821 typestring_offset);
1824 else
1826 if (offset_in_memory && offset_in_buffer)
1828 unsigned int memsize;
1829 align = 0;
1830 memsize = type_memsize(type, &align);
1831 *offset_in_memory += memsize;
1832 /* increment these separately as in the case of conformant (varying)
1833 * structures these start at different values */
1834 *offset_in_buffer += memsize;
1838 return pointer_count;
1841 static void write_pointer_description(FILE *file, type_t *type,
1842 unsigned int *typestring_offset)
1844 unsigned int offset_in_buffer;
1845 unsigned int offset_in_memory;
1847 /* pass 1: search for single instance of a pointer (i.e. don't descend
1848 * into arrays) */
1849 if (!is_array(type))
1851 offset_in_memory = 0;
1852 offset_in_buffer = 0;
1853 write_no_repeat_pointer_descriptions(
1854 file, type,
1855 &offset_in_memory, &offset_in_buffer, typestring_offset);
1858 /* pass 2: search for pointers in fixed arrays */
1859 offset_in_memory = 0;
1860 offset_in_buffer = 0;
1861 write_fixed_array_pointer_descriptions(
1862 file, NULL, type,
1863 &offset_in_memory, &offset_in_buffer, typestring_offset);
1865 /* pass 3: search for pointers in conformant only arrays (but don't descend
1866 * into conformant varying or varying arrays) */
1867 if ((!type->declarray || !current_structure) && is_conformant_array(type))
1868 write_conformant_array_pointer_descriptions(
1869 file, NULL, type, 0, typestring_offset);
1870 else if (type_get_type(type) == TYPE_STRUCT &&
1871 get_struct_fc(type) == RPC_FC_CPSTRUCT)
1873 unsigned int align = 0;
1874 type_t *carray = find_array_or_string_in_struct(type)->type;
1875 write_conformant_array_pointer_descriptions(
1876 file, NULL, carray,
1877 type_memsize(type, &align),
1878 typestring_offset);
1881 /* pass 4: search for pointers in varying arrays */
1882 offset_in_memory = 0;
1883 offset_in_buffer = 0;
1884 write_varying_array_pointer_descriptions(
1885 file, NULL, type,
1886 &offset_in_memory, &offset_in_buffer, typestring_offset);
1889 int is_declptr(const type_t *t)
1891 return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
1894 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1895 type_t *type,
1896 const char *name, unsigned int *typestring_offset)
1898 unsigned int start_offset;
1899 unsigned char rtype;
1900 type_t *elem_type;
1902 if (is_declptr(type))
1904 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1905 int pointer_type = is_ptr(type) ? get_pointer_fc(type) : get_attrv(attrs, ATTR_POINTERTYPE);
1906 if (!pointer_type)
1907 pointer_type = RPC_FC_RP;
1908 print_start_tfs_comment(file, type, *typestring_offset);
1909 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1910 pointer_type, flag, string_of_type(pointer_type),
1911 flag ? " [simple_pointer]" : "");
1912 *typestring_offset += 2;
1913 if (!flag)
1915 print_file(file, 2, "NdrFcShort(0x2),\n");
1916 *typestring_offset += 2;
1920 start_offset = *typestring_offset;
1921 update_tfsoff(type, start_offset, file);
1923 if (is_array(type))
1924 elem_type = type_array_get_element(type);
1925 else
1926 elem_type = type_pointer_get_ref(type);
1928 if (type_get_type(elem_type) != TYPE_BASIC)
1930 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
1931 return start_offset;
1934 rtype = type_basic_get_fc(elem_type);
1935 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
1937 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1938 return start_offset;
1941 if (type->declarray && !is_conformant_array(type))
1943 unsigned int dim = type_array_get_dim(type);
1945 /* FIXME: multi-dimensional array */
1946 if (0xffffu < dim)
1947 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
1948 name, 0xffffu, dim - 0xffffu);
1950 if (rtype == RPC_FC_WCHAR)
1951 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
1952 else
1953 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
1954 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1955 *typestring_offset += 2;
1957 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
1958 *typestring_offset += 2;
1960 return start_offset;
1962 else if (is_conformant_array(type))
1964 unsigned int align = 0;
1966 if (rtype == RPC_FC_WCHAR)
1967 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1968 else
1969 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1970 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1971 *typestring_offset += 2;
1973 *typestring_offset += write_conf_or_var_desc(
1974 file, current_structure,
1975 (type->declarray && current_structure
1976 ? type_memsize(current_structure, &align)
1977 : 0),
1978 type, type_array_get_conformance(type));
1980 return start_offset;
1982 else
1984 if (rtype == RPC_FC_WCHAR)
1985 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1986 else
1987 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1988 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1989 *typestring_offset += 2;
1991 return start_offset;
1995 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1996 const char *name, unsigned int *typestring_offset)
1998 const expr_t *length_is = type_array_get_variance(type);
1999 const expr_t *size_is = type_array_get_conformance(type);
2000 unsigned int align = 0;
2001 unsigned int size;
2002 unsigned int start_offset;
2003 unsigned char fc;
2004 int has_pointer;
2005 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2006 unsigned int baseoff
2007 = type->declarray && current_structure
2008 ? type_memsize(current_structure, &align)
2009 : 0;
2011 if (!pointer_type)
2012 pointer_type = RPC_FC_RP;
2014 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2015 has_pointer = TRUE;
2016 else
2017 has_pointer = type_has_pointers(type_array_get_element(type));
2019 align = 0;
2020 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2021 fc = get_array_fc(type);
2023 start_offset = *typestring_offset;
2024 update_tfsoff(type, start_offset, file);
2025 print_start_tfs_comment(file, type, start_offset);
2026 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2027 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2028 *typestring_offset += 2;
2030 align = 0;
2031 if (fc != RPC_FC_BOGUS_ARRAY)
2033 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2035 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2036 *typestring_offset += 4;
2038 else
2040 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2041 *typestring_offset += 2;
2044 if (is_conformant_array(type))
2045 *typestring_offset
2046 += write_conf_or_var_desc(file, current_structure, baseoff,
2047 type, size_is);
2049 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2051 unsigned int elalign = 0;
2052 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2053 unsigned int dim = type_array_get_dim(type);
2055 if (fc == RPC_FC_LGVARRAY)
2057 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2058 *typestring_offset += 4;
2060 else
2062 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2063 *typestring_offset += 2;
2066 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2067 *typestring_offset += 2;
2070 if (length_is)
2071 *typestring_offset
2072 += write_conf_or_var_desc(file, current_structure, baseoff,
2073 type, length_is);
2075 if (has_pointer && (!type->declarray || !current_structure))
2077 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2078 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2079 *typestring_offset += 2;
2080 write_pointer_description(file, type, typestring_offset);
2081 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2082 *typestring_offset += 1;
2085 write_member_type(file, type, FALSE, NULL, type_array_get_element(type), NULL, typestring_offset);
2086 write_end(file, typestring_offset);
2088 else
2090 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2091 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2092 *typestring_offset += 2;
2093 *typestring_offset
2094 += write_conf_or_var_desc(file, current_structure, baseoff,
2095 type, size_is);
2096 *typestring_offset
2097 += write_conf_or_var_desc(file, current_structure, baseoff,
2098 type, length_is);
2099 write_member_type(file, type, TRUE, NULL, type_array_get_element(type), NULL, typestring_offset);
2100 write_end(file, typestring_offset);
2103 return start_offset;
2106 static const var_t *find_array_or_string_in_struct(const type_t *type)
2108 const var_list_t *fields = type_struct_get_fields(type);
2109 const var_t *last_field;
2110 const type_t *ft;
2112 if (!fields || list_empty(fields))
2113 return NULL;
2115 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2116 ft = last_field->type;
2118 if (ft->declarray && is_conformant_array(ft))
2119 return last_field;
2121 if (type_get_type(ft) == TYPE_STRUCT)
2122 return find_array_or_string_in_struct(ft);
2123 else
2124 return NULL;
2127 static void write_struct_members(FILE *file, const type_t *type,
2128 int is_complex, unsigned int *corroff,
2129 unsigned int *typestring_offset)
2131 const var_t *field;
2132 unsigned short offset = 0;
2133 int salign = -1;
2134 int padding;
2135 var_list_t *fields = type_struct_get_fields(type);
2137 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2139 type_t *ft = field->type;
2140 if (!ft->declarray || !is_conformant_array(ft))
2142 unsigned int align = 0;
2143 unsigned int size = type_memsize(ft, &align);
2144 if (salign == -1)
2145 salign = align;
2146 if ((align - 1) & offset)
2148 unsigned char fc = 0;
2149 switch (align)
2151 case 4:
2152 fc = RPC_FC_ALIGNM4;
2153 break;
2154 case 8:
2155 fc = RPC_FC_ALIGNM8;
2156 break;
2157 default:
2158 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2160 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2161 offset = ROUND_SIZE(offset, align);
2162 *typestring_offset += 1;
2164 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2165 typestring_offset);
2166 offset += size;
2170 padding = ROUNDING(offset, salign);
2171 if (padding)
2173 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2174 RPC_FC_STRUCTPAD1 + padding - 1,
2175 padding);
2176 *typestring_offset += 1;
2179 write_end(file, typestring_offset);
2182 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2183 const char *name, unsigned int *tfsoff)
2185 const type_t *save_current_structure = current_structure;
2186 unsigned int total_size;
2187 const var_t *array;
2188 unsigned int start_offset;
2189 unsigned int array_offset;
2190 int has_pointers = 0;
2191 unsigned int align = 0;
2192 unsigned int corroff;
2193 var_t *f;
2194 unsigned char fc = get_struct_fc(type);
2195 var_list_t *fields = type_struct_get_fields(type);
2197 guard_rec(type);
2198 current_structure = type;
2200 total_size = type_memsize(type, &align);
2201 if (total_size > USHRT_MAX)
2202 error("structure size for %s exceeds %d bytes by %d bytes\n",
2203 name, USHRT_MAX, total_size - USHRT_MAX);
2205 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2206 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2207 FALSE, tfsoff);
2208 if (!has_pointers) has_pointers = type_has_pointers(type);
2210 array = find_array_or_string_in_struct(type);
2211 if (array && !processed(array->type))
2212 array_offset
2213 = is_string_type(array->attrs, array->type)
2214 ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
2215 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2217 corroff = *tfsoff;
2218 write_descriptors(file, type, tfsoff);
2220 start_offset = *tfsoff;
2221 update_tfsoff(type, start_offset, file);
2222 print_start_tfs_comment(file, type, start_offset);
2223 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2224 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2225 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2226 *tfsoff += 4;
2228 if (array)
2230 unsigned int absoff = array->type->typestring_offset;
2231 short reloff = absoff - *tfsoff;
2232 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2233 reloff, reloff, absoff);
2234 *tfsoff += 2;
2236 else if (fc == RPC_FC_BOGUS_STRUCT)
2238 print_file(file, 2, "NdrFcShort(0x0),\n");
2239 *tfsoff += 2;
2242 if (fc == RPC_FC_BOGUS_STRUCT)
2244 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2245 nothing is written to file yet. On the actual writing pass,
2246 this will have been updated. */
2247 unsigned int absoff = type_get_real_type(type)->ptrdesc ?
2248 type_get_real_type(type)->ptrdesc : *tfsoff;
2249 int reloff = absoff - *tfsoff;
2250 assert( reloff >= 0 );
2251 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2252 reloff, reloff, absoff);
2253 *tfsoff += 2;
2255 else if ((fc == RPC_FC_PSTRUCT) ||
2256 (fc == RPC_FC_CPSTRUCT) ||
2257 (fc == RPC_FC_CVSTRUCT && has_pointers))
2259 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2260 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2261 *tfsoff += 2;
2262 write_pointer_description(file, type, tfsoff);
2263 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2264 *tfsoff += 1;
2267 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2268 tfsoff);
2270 if (fc == RPC_FC_BOGUS_STRUCT)
2272 const var_t *f;
2274 type_get_real_type(type)->ptrdesc = *tfsoff;
2275 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2277 type_t *ft = f->type;
2278 if (is_ptr(ft))
2280 if (is_string_type(f->attrs, ft))
2281 write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
2282 else
2283 write_pointer_tfs(file, ft, tfsoff);
2285 else if (!ft->declarray && is_conformant_array(ft))
2287 unsigned int absoff = ft->typestring_offset;
2288 short reloff = absoff - (*tfsoff + 2);
2289 int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
2290 /* FIXME: We need to store pointer attributes for arrays
2291 so we don't lose pointer_default info. */
2292 if (ptr_type == 0)
2293 ptr_type = RPC_FC_UP;
2294 print_file(file, 0, "/* %d */\n", *tfsoff);
2295 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2296 string_of_type(ptr_type));
2297 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2298 reloff, reloff, absoff);
2299 *tfsoff += 4;
2302 if (type_get_real_type(type)->ptrdesc == *tfsoff)
2303 type_get_real_type(type)->ptrdesc = 0;
2306 current_structure = save_current_structure;
2307 return start_offset;
2310 static unsigned int write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
2311 unsigned char flags, unsigned int offset,
2312 unsigned int *typeformat_offset)
2314 unsigned int start_offset = *typeformat_offset;
2315 short reloff = offset - (*typeformat_offset + 2);
2316 int in_attr, out_attr;
2317 in_attr = is_attr(attrs, ATTR_IN);
2318 out_attr = is_attr(attrs, ATTR_OUT);
2319 if (!in_attr && !out_attr) in_attr = 1;
2321 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2322 flags |= 0x04;
2324 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2325 pointer_type,
2326 flags,
2327 string_of_type(pointer_type));
2328 if (file)
2330 if (flags & 0x04)
2331 fprintf(file, " [allocated_on_stack]");
2332 if (flags & 0x10)
2333 fprintf(file, " [pointer_deref]");
2334 fprintf(file, " */\n");
2337 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", reloff, offset);
2338 *typeformat_offset += 4;
2340 return start_offset;
2343 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2345 if (t == NULL)
2347 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2349 else
2351 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2353 unsigned char fc;
2354 if (type_get_type(t) == TYPE_BASIC)
2355 fc = type_basic_get_fc(t);
2356 else
2357 fc = get_enum_fc(t);
2358 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2359 fc, string_of_type(fc));
2361 else if (t->typestring_offset)
2363 short reloff = t->typestring_offset - *tfsoff;
2364 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2365 reloff, reloff, t->typestring_offset);
2367 else
2368 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2371 *tfsoff += 2;
2374 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2376 unsigned int align;
2377 unsigned int start_offset;
2378 unsigned int size;
2379 var_list_t *fields;
2380 unsigned int nbranch = 0;
2381 type_t *deftype = NULL;
2382 short nodeftype = 0xffff;
2383 var_t *f;
2385 guard_rec(type);
2387 align = 0;
2388 size = type_memsize(type, &align);
2390 fields = type_union_get_cases(type);
2392 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2394 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2395 if (cases)
2396 nbranch += list_count(cases);
2397 if (f->type)
2398 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2401 start_offset = *tfsoff;
2402 update_tfsoff(type, start_offset, file);
2403 print_start_tfs_comment(file, type, start_offset);
2404 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2406 const var_t *sv = type_union_get_switch_value(type);
2407 const type_t *st = sv->type;
2408 unsigned char fc;
2410 if (type_get_type(st) == TYPE_BASIC)
2412 switch (type_basic_get_fc(st))
2414 case RPC_FC_CHAR:
2415 case RPC_FC_SMALL:
2416 case RPC_FC_BYTE:
2417 case RPC_FC_USMALL:
2418 case RPC_FC_WCHAR:
2419 case RPC_FC_SHORT:
2420 case RPC_FC_USHORT:
2421 case RPC_FC_LONG:
2422 case RPC_FC_ULONG:
2423 fc = type_basic_get_fc(st);
2424 break;
2425 default:
2426 fc = 0;
2427 error("union switch type must be an integer, char, or enum\n");
2430 else if (type_get_type(st) == TYPE_ENUM)
2431 fc = get_enum_fc(st);
2432 else
2433 error("union switch type must be an integer, char, or enum\n");
2435 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2436 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2437 0x40 | fc, string_of_type(fc));
2438 *tfsoff += 2;
2440 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2442 static const expr_t dummy_expr; /* FIXME */
2443 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2444 unsigned char fc;
2446 if (type_get_type(st) == TYPE_BASIC)
2448 switch (type_basic_get_fc(st))
2450 case RPC_FC_CHAR:
2451 case RPC_FC_SMALL:
2452 case RPC_FC_USMALL:
2453 case RPC_FC_SHORT:
2454 case RPC_FC_USHORT:
2455 case RPC_FC_LONG:
2456 case RPC_FC_ULONG:
2457 case RPC_FC_ENUM16:
2458 case RPC_FC_ENUM32:
2459 fc = type_basic_get_fc(st);
2460 break;
2461 default:
2462 fc = 0;
2463 error("union switch type must be an integer, char, or enum\n");
2466 else if (type_get_type(st) == TYPE_ENUM)
2467 fc = get_enum_fc(st);
2468 else
2469 error("union switch type must be an integer, char, or enum\n");
2471 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2472 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2473 fc, string_of_type(fc));
2474 *tfsoff += 2;
2476 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2477 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2478 *tfsoff += 2;
2481 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2482 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2483 *tfsoff += 4;
2485 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2487 type_t *ft = f->type;
2488 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2489 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2490 expr_t *c;
2492 if (cases == NULL && !deflt)
2493 error("union field %s with neither case nor default attribute\n", f->name);
2495 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2497 /* MIDL doesn't check for duplicate cases, even though that seems
2498 like a reasonable thing to do, it just dumps them to the TFS
2499 like we're going to do here. */
2500 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2501 *tfsoff += 4;
2502 write_branch_type(file, ft, tfsoff);
2505 /* MIDL allows multiple default branches, even though that seems
2506 illogical, it just chooses the last one, which is what we will
2507 do. */
2508 if (deflt)
2510 deftype = ft;
2511 nodeftype = 0;
2515 if (deftype)
2517 write_branch_type(file, deftype, tfsoff);
2519 else
2521 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2522 *tfsoff += 2;
2525 return start_offset;
2528 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2529 unsigned int *typeformat_offset)
2531 unsigned int i;
2532 unsigned int start_offset = *typeformat_offset;
2533 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2535 if (iid)
2537 print_file(file, 2, "0x2f, /* FC_IP */\n");
2538 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2539 *typeformat_offset
2540 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2542 else
2544 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2545 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2547 if (! uuid)
2548 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2550 update_tfsoff(type, start_offset, file);
2551 print_start_tfs_comment(file, type, start_offset);
2552 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2553 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2554 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2555 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2556 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2557 for (i = 0; i < 8; ++i)
2558 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2560 if (file)
2561 fprintf(file, "\n");
2563 *typeformat_offset += 18;
2565 return start_offset;
2568 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2569 const var_t *var,
2570 unsigned int *typeformat_offset)
2572 unsigned int start_offset = *typeformat_offset;
2573 unsigned char flags = 0;
2575 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2576 flags |= NDR_STRICT_CONTEXT_HANDLE;
2578 if (is_ptr(type))
2579 flags |= 0x80;
2580 if (is_attr(var->attrs, ATTR_IN))
2582 flags |= 0x40;
2583 if (!is_attr(var->attrs, ATTR_OUT))
2584 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2586 if (is_attr(var->attrs, ATTR_OUT))
2587 flags |= 0x20;
2589 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2590 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2591 /* return and can't be null values overlap */
2592 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2593 print_file(file, 0, "can't be null, ");
2594 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2595 print_file(file, 0, "serialize, ");
2596 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2597 print_file(file, 0, "no serialize, ");
2598 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2599 print_file(file, 0, "strict, ");
2600 if ((flags & 0x21) == 0x20)
2601 print_file(file, 0, "out, ");
2602 if ((flags & 0x21) == 0x21)
2603 print_file(file, 0, "return, ");
2604 if (flags & 0x40)
2605 print_file(file, 0, "in, ");
2606 if (flags & 0x80)
2607 print_file(file, 0, "via ptr, ");
2608 print_file(file, 0, "*/\n");
2609 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2610 print_file(file, 2, "0, /* FIXME: param num */\n");
2611 *typeformat_offset += 4;
2613 return start_offset;
2616 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2617 type_t *type, const var_t *var,
2618 unsigned int *typeformat_offset)
2620 unsigned int offset;
2622 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2624 case TGT_CTXT_HANDLE:
2625 case TGT_CTXT_HANDLE_POINTER:
2626 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2627 case TGT_USER_TYPE:
2628 write_user_tfs(file, type, typeformat_offset);
2629 return type->typestring_offset;
2630 case TGT_STRING:
2631 return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2632 case TGT_ARRAY:
2634 int ptr_type;
2635 unsigned int off;
2636 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2637 ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2638 /* Top level pointers to conformant arrays may be handled specially
2639 since we can bypass the pointer, but if the array is buried
2640 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2641 always need to write the pointer. */
2642 if (!ptr_type && var->type != type)
2643 /* FIXME: This should use pointer_default, but the information
2644 isn't kept around for arrays. */
2645 ptr_type = RPC_FC_UP;
2646 if (ptr_type && ptr_type != RPC_FC_RP)
2648 unsigned int absoff = type->typestring_offset;
2649 short reloff = absoff - (*typeformat_offset + 2);
2650 off = *typeformat_offset;
2651 print_file(file, 0, "/* %d */\n", off);
2652 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2653 string_of_type(ptr_type));
2654 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2655 reloff, reloff, absoff);
2656 *typeformat_offset += 4;
2658 return off;
2660 case TGT_STRUCT:
2661 if (processed(type)) return type->typestring_offset;
2662 return write_struct_tfs(file, type, var->name, typeformat_offset);
2663 case TGT_UNION:
2664 if (processed(type)) return type->typestring_offset;
2665 return write_union_tfs(file, type, typeformat_offset);
2666 case TGT_ENUM:
2667 case TGT_BASIC:
2668 /* nothing to do */
2669 return 0;
2670 case TGT_IFACE_POINTER:
2671 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2672 case TGT_POINTER:
2673 if (last_ptr(type))
2675 size_t start_offset = *typeformat_offset;
2676 int in_attr = is_attr(var->attrs, ATTR_IN);
2677 int out_attr = is_attr(var->attrs, ATTR_OUT);
2678 const type_t *ref = type_pointer_get_ref(type);
2680 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2682 /* special case for pointers to base types */
2683 case TGT_BASIC:
2684 case TGT_ENUM:
2686 unsigned char fc;
2688 if (type_get_type(ref) == TYPE_ENUM)
2689 fc = get_enum_fc(ref);
2690 else
2691 fc = type_basic_get_fc(ref);
2693 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2694 get_pointer_fc(type),
2695 (!in_attr && out_attr) ? 0x0C : 0x08,
2696 string_of_type(get_pointer_fc(type)),
2697 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2698 print_file(file, indent, "0x%02x, /* %s */\n",
2699 fc, string_of_type(fc));
2700 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2701 *typeformat_offset += 4;
2702 return start_offset;
2704 default:
2705 break;
2709 offset = write_typeformatstring_var(file, indent, func,
2710 type_pointer_get_ref(type), var,
2711 typeformat_offset);
2712 if (file)
2713 fprintf(file, "/* %2u */\n", *typeformat_offset);
2714 return write_pointer_only_tfs(file, var->attrs, get_pointer_fc(type),
2715 !last_ptr(type) ? 0x10 : 0,
2716 offset, typeformat_offset);
2717 case TGT_INVALID:
2718 break;
2720 error("invalid type %s for var %s\n", type->name, var->name);
2721 return 0;
2724 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2725 const char *name, int write_ptr, unsigned int *tfsoff)
2727 int retmask = 0;
2729 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2731 case TGT_USER_TYPE:
2732 write_user_tfs(file, type, tfsoff);
2733 break;
2734 case TGT_STRING:
2735 write_string_tfs(file, attrs, type, name, tfsoff);
2736 break;
2737 case TGT_IFACE_POINTER:
2738 write_ip_tfs(file, attrs, type, tfsoff);
2739 break;
2740 case TGT_POINTER:
2742 type_t *ref = type_pointer_get_ref(type);
2744 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2745 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2747 if (write_ptr)
2748 write_pointer_tfs(file, type, tfsoff);
2750 retmask |= 1;
2751 break;
2753 case TGT_ARRAY:
2754 /* conformant arrays and strings are handled specially */
2755 if (!type->declarray || !is_conformant_array(type))
2757 write_array_tfs(file, attrs, type, name, tfsoff);
2758 if (is_conformant_array(type))
2759 retmask |= 1;
2761 break;
2762 case TGT_STRUCT:
2763 if (!processed(type))
2764 write_struct_tfs(file, type, name, tfsoff);
2765 break;
2766 case TGT_UNION:
2767 if (!processed(type))
2768 write_union_tfs(file, type, tfsoff);
2769 break;
2770 case TGT_ENUM:
2771 case TGT_BASIC:
2772 /* nothing to do */
2773 break;
2774 case TGT_CTXT_HANDLE:
2775 case TGT_CTXT_HANDLE_POINTER:
2776 case TGT_INVALID:
2777 error("invalid type %s for var %s\n", type->name, name);
2778 break;
2781 return retmask;
2784 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2785 type_pred_t pred, unsigned int *typeformat_offset)
2787 const var_t *var;
2788 const statement_t *stmt;
2790 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2792 const type_t *iface;
2793 const statement_t *stmt_func;
2795 if (stmt->type == STMT_LIBRARY)
2797 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2798 continue;
2800 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2801 continue;
2803 iface = stmt->u.type;
2804 if (!pred(iface))
2805 continue;
2807 current_iface = iface;
2808 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2810 const var_t *func = stmt_func->u.var;
2811 if (is_local(func->attrs)) continue;
2813 if (!is_void(type_function_get_rettype(func->type)))
2815 var_t v = *func;
2816 v.type = type_function_get_rettype(func->type);
2817 update_tfsoff(type_function_get_rettype(func->type),
2818 write_typeformatstring_var(
2819 file, 2, NULL,
2820 type_function_get_rettype(func->type),
2821 &v, typeformat_offset),
2822 file);
2825 current_func = func;
2826 if (type_get_function_args(func->type))
2827 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2828 update_tfsoff(
2829 var->type,
2830 write_typeformatstring_var(
2831 file, 2, func, var->type, var,
2832 typeformat_offset),
2833 file);
2837 return *typeformat_offset + 1;
2840 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2842 unsigned int typeformat_offset = 2;
2844 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2848 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2850 int indent = 0;
2852 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2853 print_file(file, indent, "{\n");
2854 indent++;
2855 print_file(file, indent, "0,\n");
2856 print_file(file, indent, "{\n");
2857 indent++;
2858 print_file(file, indent, "NdrFcShort(0x0),\n");
2860 set_all_tfswrite(TRUE);
2861 process_tfs(file, stmts, pred);
2863 print_file(file, indent, "0x0\n");
2864 indent--;
2865 print_file(file, indent, "}\n");
2866 indent--;
2867 print_file(file, indent, "};\n");
2868 print_file(file, indent, "\n");
2871 static unsigned int get_required_buffer_size_type(
2872 const type_t *type, const char *name, unsigned int *alignment)
2874 *alignment = 0;
2875 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2877 case TGT_USER_TYPE:
2879 const char *uname;
2880 const type_t *utype = get_user_type(type, &uname);
2881 return get_required_buffer_size_type(utype, uname, alignment);
2883 case TGT_BASIC:
2884 switch (type_basic_get_fc(type))
2886 case RPC_FC_BYTE:
2887 case RPC_FC_CHAR:
2888 case RPC_FC_USMALL:
2889 case RPC_FC_SMALL:
2890 *alignment = 4;
2891 return 1;
2893 case RPC_FC_WCHAR:
2894 case RPC_FC_USHORT:
2895 case RPC_FC_SHORT:
2896 *alignment = 4;
2897 return 2;
2899 case RPC_FC_ULONG:
2900 case RPC_FC_LONG:
2901 case RPC_FC_FLOAT:
2902 case RPC_FC_ERROR_STATUS_T:
2903 *alignment = 4;
2904 return 4;
2906 case RPC_FC_HYPER:
2907 case RPC_FC_DOUBLE:
2908 *alignment = 8;
2909 return 8;
2911 case RPC_FC_IGNORE:
2912 case RPC_FC_BIND_PRIMITIVE:
2913 return 0;
2915 default:
2916 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2917 type_basic_get_fc(type));
2918 return 0;
2920 break;
2922 case TGT_ENUM:
2923 switch (get_enum_fc(type))
2925 case RPC_FC_ENUM32:
2926 *alignment = 4;
2927 return 4;
2928 case RPC_FC_ENUM16:
2929 *alignment = 4;
2930 return 2;
2932 break;
2934 case TGT_STRUCT:
2935 if (get_struct_fc(type) == RPC_FC_STRUCT)
2937 if (!type_struct_get_fields(type)) return 0;
2938 return fields_memsize(type_struct_get_fields(type), alignment);
2940 break;
2942 case TGT_POINTER:
2943 if (get_pointer_fc(type) == RPC_FC_RP)
2945 const type_t *ref = type_pointer_get_ref(type);
2946 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2948 case TGT_BASIC:
2949 case TGT_ENUM:
2950 return get_required_buffer_size_type( ref, name, alignment );
2951 case TGT_STRUCT:
2952 if (get_struct_fc(ref) == RPC_FC_STRUCT)
2953 return get_required_buffer_size_type( ref, name, alignment );
2954 break;
2955 case TGT_USER_TYPE:
2956 case TGT_CTXT_HANDLE:
2957 case TGT_CTXT_HANDLE_POINTER:
2958 case TGT_STRING:
2959 case TGT_POINTER:
2960 case TGT_ARRAY:
2961 case TGT_IFACE_POINTER:
2962 case TGT_UNION:
2963 case TGT_INVALID:
2964 break;
2967 break;
2969 case TGT_ARRAY:
2970 /* FIXME: depends on pointer type */
2971 return type_array_get_dim(type) *
2972 get_required_buffer_size_type(type_array_get_element(type), name, alignment);
2974 default:
2975 break;
2977 return 0;
2980 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2982 int in_attr = is_attr(var->attrs, ATTR_IN);
2983 int out_attr = is_attr(var->attrs, ATTR_OUT);
2985 if (!in_attr && !out_attr)
2986 in_attr = 1;
2988 *alignment = 0;
2990 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
2991 pass == PASS_RETURN)
2993 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
2995 *alignment = 4;
2996 return 20;
2999 if (!is_string_type(var->attrs, var->type))
3000 return get_required_buffer_size_type(var->type, var->name,
3001 alignment);
3003 return 0;
3006 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3008 const var_t *var;
3009 unsigned int total_size = 0, alignment;
3011 if (type_get_function_args(func->type))
3013 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3015 total_size += get_required_buffer_size(var, &alignment, pass);
3016 total_size += alignment;
3020 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3022 var_t v = *func;
3023 v.type = type_function_get_rettype(func->type);
3024 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3025 total_size += alignment;
3027 return total_size;
3030 static void print_phase_function(FILE *file, int indent, const char *type,
3031 const char *local_var_prefix, enum remoting_phase phase,
3032 const var_t *var, unsigned int type_offset)
3034 const char *function;
3035 switch (phase)
3037 case PHASE_BUFFERSIZE:
3038 function = "BufferSize";
3039 break;
3040 case PHASE_MARSHAL:
3041 function = "Marshall";
3042 break;
3043 case PHASE_UNMARSHAL:
3044 function = "Unmarshall";
3045 break;
3046 case PHASE_FREE:
3047 function = "Free";
3048 break;
3049 default:
3050 assert(0);
3051 return;
3054 print_file(file, indent, "Ndr%s%s(\n", type, function);
3055 indent++;
3056 print_file(file, indent, "&__frame->_StubMsg,\n");
3057 print_file(file, indent, "%s%s%s%s%s,\n",
3058 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3059 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3060 local_var_prefix,
3061 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3062 var->name);
3063 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3064 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3065 if (phase == PHASE_UNMARSHAL)
3066 print_file(file, indent, "0);\n");
3067 indent--;
3070 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3071 enum remoting_phase phase, enum pass pass, const var_t *var,
3072 const char *varname)
3074 type_t *type = var->type;
3075 unsigned int size;
3076 unsigned int alignment = 0;
3077 const type_t *ref;
3079 /* no work to do for other phases, buffer sizing is done elsewhere */
3080 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3081 return;
3083 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3084 if (type_get_type(ref) == TYPE_ENUM)
3086 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3088 size = 4;
3089 alignment = 4;
3091 else /* RPC_FC_ENUM16 */
3093 size = 2;
3094 alignment = 2;
3097 else
3099 switch (type_basic_get_fc(ref))
3101 case RPC_FC_BYTE:
3102 case RPC_FC_CHAR:
3103 case RPC_FC_SMALL:
3104 case RPC_FC_USMALL:
3105 size = 1;
3106 alignment = 1;
3107 break;
3109 case RPC_FC_WCHAR:
3110 case RPC_FC_USHORT:
3111 case RPC_FC_SHORT:
3112 size = 2;
3113 alignment = 2;
3114 break;
3116 case RPC_FC_ULONG:
3117 case RPC_FC_LONG:
3118 case RPC_FC_FLOAT:
3119 case RPC_FC_ERROR_STATUS_T:
3120 size = 4;
3121 alignment = 4;
3122 break;
3124 case RPC_FC_HYPER:
3125 case RPC_FC_DOUBLE:
3126 size = 8;
3127 alignment = 8;
3128 break;
3130 case RPC_FC_IGNORE:
3131 case RPC_FC_BIND_PRIMITIVE:
3132 /* no marshalling needed */
3133 return;
3135 default:
3136 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3137 var->name, type_basic_get_fc(ref));
3138 size = 0;
3142 if (phase == PHASE_MARSHAL)
3143 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3144 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3145 alignment - 1, alignment - 1);
3147 if (phase == PHASE_MARSHAL)
3149 print_file(file, indent, "*(");
3150 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3151 if (is_ptr(type))
3152 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3153 else
3154 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3155 fprintf(file, "%s%s", local_var_prefix, varname);
3156 fprintf(file, ";\n");
3158 else if (phase == PHASE_UNMARSHAL)
3160 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3161 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3162 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3163 print_file(file, indent, "{\n");
3164 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3165 print_file(file, indent, "}\n");
3166 print_file(file, indent, "%s%s%s",
3167 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3168 local_var_prefix, varname);
3169 if (pass == PASS_IN && is_ptr(type))
3170 fprintf(file, " = (");
3171 else
3172 fprintf(file, " = *(");
3173 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3174 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3177 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3178 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3179 fprintf(file, ");\n");
3182 /* returns whether the MaxCount, Offset or ActualCount members need to be
3183 * filled in for the specified phase */
3184 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3186 return (phase != PHASE_UNMARSHAL);
3189 expr_t *get_size_is_expr(const type_t *t, const char *name)
3191 expr_t *x = NULL;
3193 for ( ; is_array(t); t = type_array_get_element(t))
3194 if (type_array_has_conformance(t))
3196 if (!x)
3197 x = type_array_get_conformance(t);
3198 else
3199 error("%s: multidimensional conformant"
3200 " arrays not supported at the top level\n",
3201 name);
3204 return x;
3207 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3208 enum remoting_phase phase, const var_t *var)
3210 const type_t *type = var->type;
3211 /* get fundamental type for the argument */
3212 for (;;)
3214 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3215 break;
3216 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3217 break;
3218 else if (type_is_alias(type))
3219 type = type_alias_get_aliasee(type);
3220 else if (is_array(type) || is_string_type(var->attrs, type))
3222 if (is_conformance_needed_for_phase(phase) && is_array(type))
3224 if (type_array_has_conformance(type))
3226 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3227 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3228 fprintf(file, ";\n\n");
3230 if (type_array_has_variance(type))
3232 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3233 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3234 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3235 fprintf(file, ";\n\n");
3238 break;
3240 else if (type_get_type(type) == TYPE_UNION)
3242 if (is_conformance_needed_for_phase(phase))
3244 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3245 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3246 fprintf(file, ";\n\n");
3248 break;
3250 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3252 expr_t *iid;
3254 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3256 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3257 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3258 fprintf( file, ";\n\n" );
3260 break;
3262 else if (is_ptr(type))
3263 type = type_pointer_get_ref(type);
3264 else
3265 break;
3269 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3270 enum pass pass, enum remoting_phase phase, const var_t *var)
3272 int in_attr, out_attr, pointer_type;
3273 const type_t *type = var->type;
3274 unsigned int start_offset = type->typestring_offset;
3276 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
3277 if (!pointer_type)
3278 pointer_type = RPC_FC_RP;
3280 in_attr = is_attr(var->attrs, ATTR_IN);
3281 out_attr = is_attr(var->attrs, ATTR_OUT);
3282 if (!in_attr && !out_attr)
3283 in_attr = 1;
3285 if (phase != PHASE_FREE)
3286 switch (pass)
3288 case PASS_IN:
3289 if (!in_attr) return;
3290 break;
3291 case PASS_OUT:
3292 if (!out_attr) return;
3293 break;
3294 case PASS_RETURN:
3295 break;
3298 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3300 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3302 case TGT_CTXT_HANDLE:
3303 case TGT_CTXT_HANDLE_POINTER:
3304 if (phase == PHASE_MARSHAL)
3306 if (pass == PASS_IN)
3308 /* if the context_handle attribute appears in the chain of types
3309 * without pointers being followed, then the context handle must
3310 * be direct, otherwise it is a pointer */
3311 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3312 print_file(file, indent, "NdrClientContextMarshall(\n");
3313 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3314 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3315 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3317 else
3319 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3320 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3321 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3322 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3323 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3326 else if (phase == PHASE_UNMARSHAL)
3328 if (pass == PASS_OUT)
3330 if (!in_attr)
3331 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3332 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3333 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3334 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3335 print_file(file, indent + 1, "__frame->_Handle);\n");
3337 else
3339 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3340 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3341 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3344 break;
3345 case TGT_USER_TYPE:
3346 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3347 break;
3348 case TGT_STRING:
3349 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3350 pointer_type != RPC_FC_RP)
3352 unsigned int ptr_start_offset = (start_offset - (is_conformant_array(type) ? 4 : 2));
3353 print_phase_function(file, indent, "Pointer", local_var_prefix,
3354 phase, var, ptr_start_offset);
3356 else
3358 if (is_array(type) && !is_conformant_array(type))
3359 print_phase_function(file, indent, "NonConformantString",
3360 local_var_prefix, phase, var,
3361 start_offset);
3362 else
3363 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3364 phase, var, start_offset);
3366 break;
3367 case TGT_ARRAY:
3369 unsigned char tc = get_array_fc(type);
3370 const char *array_type = "FixedArray";
3372 /* We already have the size_is expression since it's at the
3373 top level, but do checks for multidimensional conformant
3374 arrays. When we handle them, we'll need to extend this
3375 function to return a list, and then we'll actually use
3376 the return value. */
3377 get_size_is_expr(type, var->name);
3379 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3381 array_type = "VaryingArray";
3383 else if (tc == RPC_FC_CARRAY)
3385 array_type = "ConformantArray";
3387 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3389 array_type = (tc == RPC_FC_BOGUS_ARRAY
3390 ? "ComplexArray"
3391 : "ConformantVaryingArray");
3394 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3395 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3396 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3398 /* these are all unmarshalled by allocating memory */
3399 if (tc == RPC_FC_BOGUS_ARRAY ||
3400 tc == RPC_FC_CVARRAY ||
3401 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3402 (tc == RPC_FC_CARRAY && !in_attr))
3404 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3405 indent++;
3406 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3409 break;
3411 case TGT_BASIC:
3412 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3413 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3414 break;
3415 case TGT_ENUM:
3416 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3418 if (phase == PHASE_MARSHAL)
3419 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3420 else
3421 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3422 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3423 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3424 local_var_prefix,
3425 var->name);
3426 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3428 break;
3429 case TGT_STRUCT:
3430 switch (get_struct_fc(type))
3432 case RPC_FC_STRUCT:
3433 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3434 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3435 break;
3436 case RPC_FC_PSTRUCT:
3437 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3438 break;
3439 case RPC_FC_CSTRUCT:
3440 case RPC_FC_CPSTRUCT:
3441 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3442 break;
3443 case RPC_FC_CVSTRUCT:
3444 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3445 break;
3446 case RPC_FC_BOGUS_STRUCT:
3447 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3448 break;
3449 default:
3450 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3452 break;
3453 case TGT_UNION:
3455 const char *union_type = NULL;
3457 if (type_get_type(type) == TYPE_UNION)
3458 union_type = "NonEncapsulatedUnion";
3459 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3460 union_type = "EncapsulatedUnion";
3462 print_phase_function(file, indent, union_type, local_var_prefix,
3463 phase, var, start_offset);
3464 break;
3466 case TGT_POINTER:
3468 const type_t *ref = type_pointer_get_ref(type);
3469 if (get_pointer_fc(type) == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3471 case TYPE_BASIC:
3472 /* base types have known sizes, so don't need a sizing pass
3473 * and don't have any memory to free and so don't need a
3474 * freeing pass */
3475 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3476 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3477 break;
3478 case TYPE_ENUM:
3479 /* base types have known sizes, so don't need a sizing pass
3480 * and don't have any memory to free and so don't need a
3481 * freeing pass */
3482 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3483 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3484 break;
3485 case TYPE_STRUCT:
3487 const char *struct_type = NULL;
3488 switch (get_struct_fc(ref))
3490 case RPC_FC_STRUCT:
3491 /* simple structs have known sizes, so don't need a sizing
3492 * pass and don't have any memory to free and so don't
3493 * need a freeing pass */
3494 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3495 struct_type = "SimpleStruct";
3496 break;
3497 case RPC_FC_PSTRUCT:
3498 struct_type = "SimpleStruct";
3499 break;
3500 case RPC_FC_CSTRUCT:
3501 case RPC_FC_CPSTRUCT:
3502 struct_type = "ConformantStruct";
3503 break;
3504 case RPC_FC_CVSTRUCT:
3505 struct_type = "ConformantVaryingStruct";
3506 break;
3507 case RPC_FC_BOGUS_STRUCT:
3508 struct_type = "ComplexStruct";
3509 break;
3510 default:
3511 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3514 if (struct_type)
3516 if (phase == PHASE_FREE)
3517 struct_type = "Pointer";
3518 else
3519 start_offset = ref->typestring_offset;
3520 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3522 break;
3524 case TYPE_UNION:
3525 case TYPE_ENCAPSULATED_UNION:
3527 const char *union_type = NULL;
3528 if (phase == PHASE_FREE)
3529 union_type = "Pointer";
3530 else
3532 if (type_get_type(ref) == TYPE_UNION)
3533 union_type = "NonEncapsulatedUnion";
3534 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3535 union_type = "EncapsulatedUnion";
3537 start_offset = ref->typestring_offset;
3540 print_phase_function(file, indent, union_type, local_var_prefix,
3541 phase, var, start_offset);
3542 break;
3544 case TYPE_POINTER:
3545 case TYPE_ARRAY:
3546 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3547 break;
3548 case TYPE_VOID:
3549 case TYPE_ALIAS:
3550 case TYPE_MODULE:
3551 case TYPE_COCLASS:
3552 case TYPE_FUNCTION:
3553 case TYPE_INTERFACE:
3554 assert(0);
3555 break;
3557 else
3558 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3559 break;
3561 case TGT_IFACE_POINTER:
3562 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3563 break;
3564 case TGT_INVALID:
3565 assert(0);
3566 break;
3568 fprintf(file, "\n");
3571 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3572 enum pass pass, enum remoting_phase phase)
3574 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3576 unsigned int size = get_function_buffer_size( func, pass );
3577 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3580 if (pass == PASS_RETURN)
3582 var_t var;
3583 var = *func;
3584 var.type = type_function_get_rettype(func->type);
3585 var.name = xstrdup( "_RetVal" );
3586 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3587 free( var.name );
3589 else
3591 const var_t *var;
3592 if (!type_get_function_args(func->type))
3593 return;
3594 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3595 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3600 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3602 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3606 unsigned int get_size_procformatstring_func(const var_t *func)
3608 const var_t *var;
3609 unsigned int size = 0;
3611 /* argument list size */
3612 if (type_get_function_args(func->type))
3613 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3614 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3616 /* return value size */
3617 if (is_void(type_function_get_rettype(func->type)))
3618 size += 2; /* FC_END and FC_PAD */
3619 else
3620 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3622 return size;
3625 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3627 const statement_t *stmt;
3628 unsigned int size = 1;
3630 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3632 const type_t *iface;
3633 const statement_t *stmt_func;
3635 if (stmt->type == STMT_LIBRARY)
3637 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3638 continue;
3640 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3641 continue;
3643 iface = stmt->u.type;
3644 if (!pred(iface))
3645 continue;
3647 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3649 const var_t *func = stmt_func->u.var;
3650 if (!is_local(func->attrs))
3651 size += get_size_procformatstring_func( func );
3654 return size;
3657 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3659 set_all_tfswrite(FALSE);
3660 return process_tfs(NULL, stmts, pred);
3663 void declare_stub_args( FILE *file, int indent, const var_t *func )
3665 int in_attr, out_attr;
3666 int i = 0;
3667 const var_t *var;
3669 /* declare return value '_RetVal' */
3670 if (!is_void(type_function_get_rettype(func->type)))
3672 print_file(file, indent, "%s", "");
3673 write_type_decl_left(file, type_function_get_rettype(func->type));
3674 fprintf(file, " _RetVal;\n");
3677 if (!type_get_function_args(func->type))
3678 return;
3680 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3682 int is_string = is_string_type(var->attrs, var->type);
3684 in_attr = is_attr(var->attrs, ATTR_IN);
3685 out_attr = is_attr(var->attrs, ATTR_OUT);
3686 if (!out_attr && !in_attr)
3687 in_attr = 1;
3689 if (is_context_handle(var->type))
3690 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3691 else
3693 if (!in_attr && !is_conformant_array(var->type) && !is_string)
3695 type_t *type_to_print;
3696 char name[16];
3697 print_file(file, indent, "%s", "");
3698 if (var->type->declarray)
3699 type_to_print = var->type;
3700 else
3701 type_to_print = type_pointer_get_ref(var->type);
3702 sprintf(name, "_W%u", i++);
3703 write_type_decl(file, type_to_print, name);
3704 fprintf(file, ";\n");
3707 print_file(file, indent, "%s", "");
3708 write_type_decl_left(file, var->type);
3709 fprintf(file, " ");
3710 if (var->type->declarray) {
3711 fprintf(file, "(*%s)", var->name);
3712 } else
3713 fprintf(file, "%s", var->name);
3714 write_type_right(file, var->type, FALSE);
3715 fprintf(file, ";\n");
3717 if (decl_indirect(var->type))
3718 print_file(file, indent, "void *_p_%s;\n", var->name);
3724 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3726 int in_attr, out_attr;
3727 int i = 0, sep = 0;
3728 const var_t *var;
3730 if (!type_get_function_args(func->type))
3731 return;
3733 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3735 int is_string = is_string_type(var->attrs, var->type);
3736 in_attr = is_attr(var->attrs, ATTR_IN);
3737 out_attr = is_attr(var->attrs, ATTR_OUT);
3738 if (!out_attr && !in_attr)
3739 in_attr = 1;
3741 if (!in_attr)
3743 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3745 if (is_context_handle(var->type))
3747 fprintf(file, " = NdrContextHandleInitialize(\n");
3748 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3749 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3750 var->type->typestring_offset);
3752 else if (is_array(var->type) &&
3753 type_array_has_conformance(var->type))
3755 unsigned int size, align = 0;
3756 type_t *type = var->type;
3758 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3759 for ( ;
3760 is_array(type) && type_array_has_conformance(type);
3761 type = type_array_get_element(type))
3763 write_expr(file, type_array_get_conformance(type), TRUE,
3764 TRUE, NULL, NULL, local_var_prefix);
3765 fprintf(file, " * ");
3767 size = type_memsize(type, &align);
3768 fprintf(file, "%u);\n", size);
3770 else if (!is_string)
3772 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3773 if (is_ptr(var->type) && !last_ptr(var->type))
3774 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3775 i++;
3778 sep = 1;
3781 if (sep)
3782 fprintf(file, "\n");
3786 int write_expr_eval_routines(FILE *file, const char *iface)
3788 static const char *var_name = "pS";
3789 static const char *var_name_expr = "pS->";
3790 int result = 0;
3791 struct expr_eval_routine *eval;
3792 unsigned short callback_offset = 0;
3794 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3796 const char *name = eval->structure->name;
3797 result = 1;
3799 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3800 iface, name, callback_offset);
3801 print_file(file, 0, "{\n");
3802 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3803 name, var_name, name, eval->baseoff);
3804 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3805 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3806 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3807 fprintf(file, ";\n");
3808 print_file(file, 0, "}\n\n");
3809 callback_offset++;
3811 return result;
3814 void write_expr_eval_routine_list(FILE *file, const char *iface)
3816 struct expr_eval_routine *eval;
3817 struct expr_eval_routine *cursor;
3818 unsigned short callback_offset = 0;
3820 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3821 fprintf(file, "{\n");
3823 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3825 const char *name = eval->structure->name;
3826 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3827 callback_offset++;
3828 list_remove(&eval->entry);
3829 free(eval);
3832 fprintf(file, "};\n\n");
3835 void write_user_quad_list(FILE *file)
3837 user_type_t *ut;
3839 if (list_empty(&user_type_list))
3840 return;
3842 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3843 fprintf(file, "{\n");
3844 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3846 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3847 print_file(file, 1, "{\n");
3848 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3849 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3850 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3851 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3852 print_file(file, 1, "}%s\n", sep);
3854 fprintf(file, "};\n\n");
3857 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3859 const struct str_list_entry_t *endpoint;
3860 const char *p;
3862 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3863 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3864 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3866 print_file( f, 1, "{ (const unsigned char *)\"" );
3867 for (p = endpoint->str; *p && *p != ':'; p++)
3869 if (*p == '"' || *p == '\\') fputc( '\\', f );
3870 fputc( *p, f );
3872 if (!*p) goto error;
3873 if (p[1] != '[') goto error;
3875 fprintf( f, "\", (const unsigned char *)\"" );
3876 for (p += 2; *p && *p != ']'; p++)
3878 if (*p == '"' || *p == '\\') fputc( '\\', f );
3879 fputc( *p, f );
3881 if (*p != ']') goto error;
3882 fprintf( f, "\" },\n" );
3884 print_file( f, 0, "};\n\n" );
3885 return;
3887 error:
3888 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3891 void write_exceptions( FILE *file )
3893 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3894 fprintf( file, "\n");
3895 fprintf( file, "#include \"wine/exception.h\"\n");
3896 fprintf( file, "#undef RpcTryExcept\n");
3897 fprintf( file, "#undef RpcExcept\n");
3898 fprintf( file, "#undef RpcEndExcept\n");
3899 fprintf( file, "#undef RpcTryFinally\n");
3900 fprintf( file, "#undef RpcFinally\n");
3901 fprintf( file, "#undef RpcEndFinally\n");
3902 fprintf( file, "#undef RpcExceptionCode\n");
3903 fprintf( file, "#undef RpcAbnormalTermination\n");
3904 fprintf( file, "\n");
3905 fprintf( file, "struct __exception_frame;\n");
3906 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3907 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3908 fprintf( file, "\n");
3909 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3910 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3911 fprintf( file, " __filter_func filter; \\\n");
3912 fprintf( file, " __finally_func finally; \\\n");
3913 fprintf( file, " sigjmp_buf jmp; \\\n");
3914 fprintf( file, " DWORD code; \\\n");
3915 fprintf( file, " unsigned char abnormal_termination; \\\n");
3916 fprintf( file, " unsigned char filter_level; \\\n");
3917 fprintf( file, " unsigned char finally_level;\n");
3918 fprintf( file, "\n");
3919 fprintf( file, "struct __exception_frame\n{\n");
3920 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3921 fprintf( file, "};\n");
3922 fprintf( file, "\n");
3923 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3924 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3925 fprintf( file, " CONTEXT *context,\n");
3926 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3927 fprintf( file, "{\n");
3928 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3929 fprintf( file, "\n");
3930 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3931 fprintf( file, " {\n" );
3932 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3933 fprintf( file, " {\n" );
3934 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3935 fprintf( file, " exc_frame->finally( exc_frame );\n");
3936 fprintf( file, " }\n" );
3937 fprintf( file, " return ExceptionContinueSearch;\n");
3938 fprintf( file, " }\n" );
3939 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3940 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3941 fprintf( file, " {\n");
3942 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3943 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3944 fprintf( file, " {\n");
3945 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3946 fprintf( file, " exc_frame->finally( exc_frame );\n");
3947 fprintf( file, " __wine_pop_frame( frame );\n");
3948 fprintf( file, " }\n");
3949 fprintf( file, " exc_frame->filter_level = 0;\n");
3950 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3951 fprintf( file, " }\n");
3952 fprintf( file, " return ExceptionContinueSearch;\n");
3953 fprintf( file, "}\n");
3954 fprintf( file, "\n");
3955 fprintf( file, "#define RpcTryExcept \\\n");
3956 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3957 fprintf( file, " { \\\n");
3958 fprintf( file, " if (!__frame->finally_level) \\\n" );
3959 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3960 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
3961 fprintf( file, "\n");
3962 fprintf( file, "#define RpcExcept(expr) \\\n");
3963 fprintf( file, " if (!__frame->finally_level) \\\n" );
3964 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3965 fprintf( file, " __frame->filter_level = 0; \\\n" );
3966 fprintf( file, " } \\\n");
3967 fprintf( file, " else \\\n");
3968 fprintf( file, "\n");
3969 fprintf( file, "#define RpcEndExcept\n");
3970 fprintf( file, "\n");
3971 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
3972 fprintf( file, "\n");
3973 fprintf( file, "#define RpcTryFinally \\\n");
3974 fprintf( file, " if (!__frame->filter_level) \\\n");
3975 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
3976 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
3977 fprintf( file, "\n");
3978 fprintf( file, "#define RpcFinally \\\n");
3979 fprintf( file, " if (!__frame->filter_level) \\\n");
3980 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
3981 fprintf( file, " __frame->finally_level = 0;\n");
3982 fprintf( file, "\n");
3983 fprintf( file, "#define RpcEndFinally\n");
3984 fprintf( file, "\n");
3985 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3986 fprintf( file, "\n");
3987 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3988 fprintf( file, " do { \\\n");
3989 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3990 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3991 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
3992 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
3993 fprintf( file, " __frame->filter_level = 0; \\\n");
3994 fprintf( file, " __frame->finally_level = 0; \\\n");
3995 fprintf( file, " } while (0)\n");
3996 fprintf( file, "\n");
3997 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3998 fprintf( file, "\n");
3999 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4000 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4001 fprintf( file, "\n");
4002 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4003 fprintf( file, " DWORD code;\n");
4004 fprintf( file, "\n");
4005 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");