push 8079c4124d1355f652d7dbd6f1862eb95d83e2de
[wine/hacks.git] / tools / widl / typegen.c
blobce4d3d7da801ade63516b03ac806559f0d37814e
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const var_t *current_func;
50 static const type_t *current_structure;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *structure;
58 unsigned int baseoff;
59 const expr_t *expr;
62 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
63 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65 const char *name, int write_ptr, unsigned int *tfsoff);
66 static const var_t *find_array_or_string_in_struct(const type_t *type);
67 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
68 type_t *type, int toplevel_param,
69 const char *name, unsigned int *typestring_offset);
71 const char *string_of_type(unsigned char type)
73 switch (type)
75 case RPC_FC_BYTE: return "FC_BYTE";
76 case RPC_FC_CHAR: return "FC_CHAR";
77 case RPC_FC_SMALL: return "FC_SMALL";
78 case RPC_FC_USMALL: return "FC_USMALL";
79 case RPC_FC_WCHAR: return "FC_WCHAR";
80 case RPC_FC_SHORT: return "FC_SHORT";
81 case RPC_FC_USHORT: return "FC_USHORT";
82 case RPC_FC_LONG: return "FC_LONG";
83 case RPC_FC_ULONG: return "FC_ULONG";
84 case RPC_FC_FLOAT: return "FC_FLOAT";
85 case RPC_FC_HYPER: return "FC_HYPER";
86 case RPC_FC_DOUBLE: return "FC_DOUBLE";
87 case RPC_FC_ENUM16: return "FC_ENUM16";
88 case RPC_FC_ENUM32: return "FC_ENUM32";
89 case RPC_FC_IGNORE: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP: return "FC_RP";
92 case RPC_FC_UP: return "FC_UP";
93 case RPC_FC_OP: return "FC_OP";
94 case RPC_FC_FP: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY: return "FC_CARRAY";
108 case RPC_FC_CVARRAY: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112 case RPC_FC_POINTER: return "FC_POINTER";
113 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING: return "FC_CSTRING";
116 case RPC_FC_WSTRING: return "FC_WSTRING";
117 default:
118 error("string_of_type: unknown type 0x%02x\n", type);
119 return NULL;
123 unsigned char get_basic_fc(const type_t *type)
125 int sign = type_basic_get_sign(type);
126 switch (type_basic_get_type(type))
128 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
129 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
130 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
131 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
132 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
133 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
134 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
135 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
136 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
137 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
138 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
139 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
140 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
141 default: return 0;
145 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
147 const type_t *t;
148 int pointer_type;
150 assert(is_ptr(type) || is_array(type));
152 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
153 if (pointer_type)
154 return pointer_type;
156 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
158 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
159 if (pointer_type)
160 return pointer_type;
163 if (toplevel_param)
164 return RPC_FC_RP;
165 else if (is_ptr(type))
166 return type_pointer_get_default_fc(type);
167 else
168 return type_array_get_ptr_default_fc(type);
171 static unsigned char get_enum_fc(const type_t *type)
173 assert(type_get_type(type) == TYPE_ENUM);
174 if (is_aliaschain_attr(type, ATTR_V1ENUM))
175 return RPC_FC_ENUM32;
176 else
177 return RPC_FC_ENUM16;
180 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
182 if (is_user_type(type))
183 return TGT_USER_TYPE;
185 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
186 return TGT_CTXT_HANDLE;
188 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
189 return TGT_STRING;
191 switch (type_get_type(type))
193 case TYPE_BASIC:
194 return TGT_BASIC;
195 case TYPE_ENUM:
196 return TGT_ENUM;
197 case TYPE_POINTER:
198 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
199 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
200 return TGT_IFACE_POINTER;
201 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
202 return TGT_CTXT_HANDLE_POINTER;
203 else
204 return TGT_POINTER;
205 case TYPE_STRUCT:
206 return TGT_STRUCT;
207 case TYPE_ENCAPSULATED_UNION:
208 case TYPE_UNION:
209 return TGT_UNION;
210 case TYPE_ARRAY:
211 return TGT_ARRAY;
212 case TYPE_FUNCTION:
213 case TYPE_COCLASS:
214 case TYPE_INTERFACE:
215 case TYPE_MODULE:
216 case TYPE_VOID:
217 case TYPE_ALIAS:
218 break;
220 return TGT_INVALID;
223 unsigned char get_struct_fc(const type_t *type)
225 int has_pointer = 0;
226 int has_conformance = 0;
227 int has_variance = 0;
228 var_t *field;
229 var_list_t *fields;
231 fields = type_struct_get_fields(type);
233 if (get_padding(fields))
234 return RPC_FC_BOGUS_STRUCT;
236 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
238 type_t *t = field->type;
239 enum typegen_type typegen_type;
241 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
243 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
245 if (is_string_type(field->attrs, field->type))
247 if (is_conformant_array(t))
248 has_conformance = 1;
249 has_variance = 1;
250 continue;
253 if (is_array(type_array_get_element(field->type)))
254 return RPC_FC_BOGUS_STRUCT;
256 if (type_array_has_conformance(field->type))
258 has_conformance = 1;
259 if (list_next(fields, &field->entry))
260 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
261 field->name);
263 if (type_array_has_variance(t))
264 has_variance = 1;
266 t = type_array_get_element(t);
267 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
270 switch (typegen_type)
272 case TGT_USER_TYPE:
273 case TGT_IFACE_POINTER:
274 return RPC_FC_BOGUS_STRUCT;
275 case TGT_BASIC:
276 break;
277 case TGT_ENUM:
278 if (get_enum_fc(t) == RPC_FC_ENUM16)
279 return RPC_FC_BOGUS_STRUCT;
280 break;
281 case TGT_POINTER:
282 case TGT_ARRAY:
283 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
284 return RPC_FC_BOGUS_STRUCT;
285 has_pointer = 1;
286 break;
287 case TGT_UNION:
288 return RPC_FC_BOGUS_STRUCT;
289 case TGT_STRUCT:
291 unsigned char fc = get_struct_fc(t);
292 switch (fc)
294 case RPC_FC_STRUCT:
295 break;
296 case RPC_FC_CVSTRUCT:
297 has_conformance = 1;
298 has_variance = 1;
299 has_pointer = 1;
300 break;
302 case RPC_FC_CPSTRUCT:
303 has_conformance = 1;
304 if (list_next( fields, &field->entry ))
305 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
306 field->name);
307 has_pointer = 1;
308 break;
310 case RPC_FC_CSTRUCT:
311 has_conformance = 1;
312 if (list_next( fields, &field->entry ))
313 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
314 field->name);
315 break;
317 case RPC_FC_PSTRUCT:
318 has_pointer = 1;
319 break;
321 default:
322 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
323 /* fallthru - treat it as complex */
325 /* as soon as we see one of these these members, it's bogus... */
326 case RPC_FC_BOGUS_STRUCT:
327 return RPC_FC_BOGUS_STRUCT;
329 break;
331 case TGT_STRING:
332 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
333 case TGT_INVALID:
334 case TGT_CTXT_HANDLE:
335 case TGT_CTXT_HANDLE_POINTER:
336 /* checking after parsing should mean that we don't get here. if we do,
337 * it's a checker bug */
338 assert(0);
342 if( has_variance )
344 if ( has_conformance )
345 return RPC_FC_CVSTRUCT;
346 else
347 return RPC_FC_BOGUS_STRUCT;
349 if( has_conformance && has_pointer )
350 return RPC_FC_CPSTRUCT;
351 if( has_conformance )
352 return RPC_FC_CSTRUCT;
353 if( has_pointer )
354 return RPC_FC_PSTRUCT;
355 return RPC_FC_STRUCT;
358 unsigned char get_array_fc(const type_t *type)
360 unsigned char fc;
361 const expr_t *size_is;
362 const type_t *elem_type;
364 elem_type = type_array_get_element(type);
365 size_is = type_array_get_conformance(type);
367 if (!size_is)
369 unsigned int align = 0;
370 unsigned int size = type_memsize(elem_type, &align);
371 if (size * type_array_get_dim(type) > 0xffffuL)
372 fc = RPC_FC_LGFARRAY;
373 else
374 fc = RPC_FC_SMFARRAY;
376 else
377 fc = RPC_FC_CARRAY;
379 if (type_array_has_variance(type))
381 if (fc == RPC_FC_SMFARRAY)
382 fc = RPC_FC_SMVARRAY;
383 else if (fc == RPC_FC_LGFARRAY)
384 fc = RPC_FC_LGVARRAY;
385 else if (fc == RPC_FC_CARRAY)
386 fc = RPC_FC_CVARRAY;
389 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
391 case TGT_USER_TYPE:
392 fc = RPC_FC_BOGUS_ARRAY;
393 break;
394 case TGT_STRUCT:
395 switch (get_struct_fc(elem_type))
397 case RPC_FC_BOGUS_STRUCT:
398 fc = RPC_FC_BOGUS_ARRAY;
399 break;
401 break;
402 case TGT_ENUM:
403 /* is 16-bit enum - if so, wire size differs from mem size and so
404 * the array cannot be block copied, which means the array is complex */
405 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
406 fc = RPC_FC_BOGUS_ARRAY;
407 break;
408 case TGT_UNION:
409 case TGT_IFACE_POINTER:
410 fc = RPC_FC_BOGUS_ARRAY;
411 break;
412 case TGT_POINTER:
413 /* ref pointers cannot just be block copied. unique pointers to
414 * interfaces need special treatment. either case means the array is
415 * complex */
416 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP)
417 fc = RPC_FC_BOGUS_ARRAY;
418 break;
419 case TGT_BASIC:
420 case TGT_CTXT_HANDLE:
421 case TGT_CTXT_HANDLE_POINTER:
422 case TGT_STRING:
423 case TGT_INVALID:
424 case TGT_ARRAY:
425 /* nothing to do for everything else */
426 break;
429 return fc;
432 int is_struct(unsigned char type)
434 switch (type)
436 case RPC_FC_STRUCT:
437 case RPC_FC_PSTRUCT:
438 case RPC_FC_CSTRUCT:
439 case RPC_FC_CPSTRUCT:
440 case RPC_FC_CVSTRUCT:
441 case RPC_FC_BOGUS_STRUCT:
442 return 1;
443 default:
444 return 0;
448 static int is_non_complex_struct(const type_t *type)
450 return (type_get_type(type) == TYPE_STRUCT &&
451 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
454 static int type_has_pointers(const type_t *type)
456 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
458 case TGT_USER_TYPE:
459 return FALSE;
460 case TGT_POINTER:
461 return TRUE;
462 case TGT_ARRAY:
463 /* FIXME: array can be pointer */
464 return type_has_pointers(type_array_get_element(type));
465 case TGT_STRUCT:
467 var_list_t *fields = type_struct_get_fields(type);
468 const var_t *field;
469 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
471 if (type_has_pointers(field->type))
472 return TRUE;
474 break;
476 case TGT_UNION:
478 var_list_t *fields;
479 const var_t *field;
480 fields = type_union_get_cases(type);
481 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
483 if (field->type && type_has_pointers(field->type))
484 return TRUE;
486 break;
488 case TGT_CTXT_HANDLE:
489 case TGT_CTXT_HANDLE_POINTER:
490 case TGT_STRING:
491 case TGT_IFACE_POINTER:
492 case TGT_BASIC:
493 case TGT_ENUM:
494 case TGT_INVALID:
495 break;
498 return FALSE;
501 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
502 int toplevel_param)
504 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
506 case TGT_USER_TYPE:
507 return FALSE;
508 case TGT_POINTER:
509 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
510 return TRUE;
511 else
512 return FALSE;
513 case TGT_ARRAY:
514 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
515 return TRUE;
516 else
517 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
518 case TGT_STRUCT:
520 var_list_t *fields = type_struct_get_fields(type);
521 const var_t *field;
522 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
524 if (type_has_full_pointer(field->type, field->attrs, FALSE))
525 return TRUE;
527 break;
529 case TGT_UNION:
531 var_list_t *fields;
532 const var_t *field;
533 fields = type_union_get_cases(type);
534 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
536 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
537 return TRUE;
539 break;
541 case TGT_CTXT_HANDLE:
542 case TGT_CTXT_HANDLE_POINTER:
543 case TGT_STRING:
544 case TGT_IFACE_POINTER:
545 case TGT_BASIC:
546 case TGT_ENUM:
547 case TGT_INVALID:
548 break;
551 return FALSE;
554 static unsigned short user_type_offset(const char *name)
556 user_type_t *ut;
557 unsigned short off = 0;
558 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
560 if (strcmp(name, ut->name) == 0)
561 return off;
562 ++off;
564 error("user_type_offset: couldn't find type (%s)\n", name);
565 return 0;
568 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
570 type->typestring_offset = offset;
571 if (file) type->tfswrite = FALSE;
574 static void guard_rec(type_t *type)
576 /* types that contain references to themselves (like a linked list),
577 need to be shielded from infinite recursion when writing embedded
578 types */
579 if (type->typestring_offset)
580 type->tfswrite = FALSE;
581 else
582 type->typestring_offset = 1;
585 static type_t *get_user_type(const type_t *t, const char **pname)
587 for (;;)
589 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
590 if (ut)
592 if (pname)
593 *pname = t->name;
594 return ut;
597 if (type_is_alias(t))
598 t = type_alias_get_aliasee(t);
599 else
600 return 0;
604 int is_user_type(const type_t *t)
606 return get_user_type(t, NULL) != NULL;
609 static int is_embedded_complex(const type_t *type)
611 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
613 case TGT_USER_TYPE:
614 case TGT_STRUCT:
615 case TGT_UNION:
616 case TGT_ARRAY:
617 case TGT_IFACE_POINTER:
618 return TRUE;
619 default:
620 return FALSE;
624 static const char *get_context_handle_type_name(const type_t *type)
626 const type_t *t;
627 for (t = type;
628 is_ptr(t) || type_is_alias(t);
629 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
630 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
631 return t->name;
632 assert(0);
633 return NULL;
636 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
637 do { \
638 if (file) \
639 fprintf(file, "/* %2u */\n", typestring_offset); \
640 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
642 while (0)
644 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
645 static void print_file(FILE *file, int indent, const char *format, ...)
647 va_list va;
648 va_start(va, format);
649 print(file, indent, format, va);
650 va_end(va);
653 void print(FILE *file, int indent, const char *format, va_list va)
655 if (file)
657 if (format[0] != '\n')
658 while (0 < indent--)
659 fprintf(file, " ");
660 vfprintf(file, format, va);
665 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
667 if (decl_indirect(t))
669 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
670 local_var_prefix, n, local_var_prefix, n);
671 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
673 else if (is_ptr(t) || is_array(t))
674 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
677 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
679 const var_t *var;
681 if (!is_void(type_function_get_rettype(func->type)))
682 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
684 if (!type_get_function_args(func->type))
685 return;
687 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
688 write_var_init(file, indent, var->type, var->name, local_var_prefix);
690 fprintf(file, "\n");
693 static void write_formatdesc(FILE *f, int indent, const char *str)
695 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
696 print_file(f, indent, "{\n");
697 print_file(f, indent + 1, "short Pad;\n");
698 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
699 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
700 print_file(f, indent, "\n");
703 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
705 clear_all_offsets();
707 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
708 get_size_typeformatstring(stmts, pred));
710 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
711 get_size_procformatstring(stmts, pred));
713 fprintf(f, "\n");
714 write_formatdesc(f, indent, "TYPE");
715 write_formatdesc(f, indent, "PROC");
716 fprintf(f, "\n");
717 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
718 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
719 print_file(f, indent, "\n");
722 int decl_indirect(const type_t *t)
724 if (is_user_type(t))
725 return TRUE;
726 return (type_get_type(t) != TYPE_BASIC &&
727 type_get_type(t) != TYPE_ENUM &&
728 type_get_type(t) != TYPE_POINTER &&
729 type_get_type(t) != TYPE_ARRAY);
732 static unsigned int write_procformatstring_type(FILE *file, int indent,
733 const char *name,
734 const type_t *type,
735 const attr_list_t *attrs,
736 int is_return)
738 unsigned int size;
740 int is_in = is_attr(attrs, ATTR_IN);
741 int is_out = is_attr(attrs, ATTR_OUT);
743 if (!is_in && !is_out) is_in = TRUE;
745 if (type_get_type(type) == TYPE_BASIC ||
746 type_get_type(type) == TYPE_ENUM)
748 unsigned char fc;
750 if (is_return)
751 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
752 else
753 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
755 if (type_get_type(type) == TYPE_ENUM)
757 fc = get_enum_fc(type);
759 else
761 fc = get_basic_fc(type);
763 if (fc == RPC_FC_BIND_PRIMITIVE)
764 fc = RPC_FC_IGNORE;
767 print_file(file, indent, "0x%02x, /* %s */\n",
768 fc, string_of_type(fc));
769 size = 2; /* includes param type prefix */
771 else
773 if (is_return)
774 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
775 else if (is_in && is_out)
776 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
777 else if (is_out)
778 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
779 else
780 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
782 print_file(file, indent, "0x01,\n");
783 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
784 size = 4; /* includes param type prefix */
786 return size;
789 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
791 const statement_t *stmt;
792 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
794 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
796 const statement_t *stmt_func;
797 if (!pred(stmt->u.type))
798 continue;
799 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
801 const var_t *func = stmt_func->u.var;
802 if (is_local(func->attrs)) continue;
803 /* emit argument data */
804 if (type_get_function_args(func->type))
806 const var_t *var;
807 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
808 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
811 /* emit return value data */
812 if (is_void(type_function_get_rettype(func->type)))
814 print_file(file, indent, "0x5b, /* FC_END */\n");
815 print_file(file, indent, "0x5c, /* FC_PAD */\n");
817 else
818 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
821 else if (stmt->type == STMT_LIBRARY)
822 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
826 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
828 int indent = 0;
830 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
831 print_file(file, indent, "{\n");
832 indent++;
833 print_file(file, indent, "0,\n");
834 print_file(file, indent, "{\n");
835 indent++;
837 write_procformatstring_stmts(file, indent, stmts, pred);
839 print_file(file, indent, "0x0\n");
840 indent--;
841 print_file(file, indent, "}\n");
842 indent--;
843 print_file(file, indent, "};\n");
844 print_file(file, indent, "\n");
847 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
849 unsigned char fc;
851 if (type_get_type(type) == TYPE_BASIC)
852 fc = get_basic_fc(type);
853 else if (type_get_type(type) == TYPE_ENUM)
854 fc = get_enum_fc(type);
855 else
856 return 0;
858 if (convert_to_signed_type)
860 switch(fc)
862 case RPC_FC_USMALL:
863 fc = RPC_FC_SMALL;
864 break;
865 case RPC_FC_USHORT:
866 fc = RPC_FC_SHORT;
867 break;
868 case RPC_FC_ULONG:
869 fc = RPC_FC_LONG;
870 break;
874 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
875 *typestring_offset += 1;
876 return 1;
879 /* write conformance / variance descriptor */
880 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
881 unsigned int baseoff, const type_t *type,
882 const expr_t *expr)
884 unsigned char operator_type = 0;
885 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
886 const char *conftype_string = "";
887 const char *operator_string = "no operators";
888 const expr_t *subexpr;
890 if (!expr)
892 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
893 return 4;
896 if (!structure)
898 /* Top-level conformance calculations are done inline. */
899 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
900 RPC_FC_TOP_LEVEL_CONFORMANCE);
901 print_file (file, 2, "0x0,\n");
902 print_file (file, 2, "NdrFcShort(0x0),\n");
903 return 4;
906 if (expr->is_const)
908 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
909 error("write_conf_or_var_desc: constant value %ld is greater than "
910 "the maximum constant size of %d\n", expr->cval,
911 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
913 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
914 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
915 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
916 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
918 return 4;
921 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
923 conftype = RPC_FC_POINTER_CONFORMANCE;
924 conftype_string = "field pointer, ";
927 subexpr = expr;
928 switch (subexpr->type)
930 case EXPR_PPTR:
931 subexpr = subexpr->ref;
932 operator_type = RPC_FC_DEREFERENCE;
933 operator_string = "FC_DEREFERENCE";
934 break;
935 case EXPR_DIV:
936 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
938 subexpr = subexpr->ref;
939 operator_type = RPC_FC_DIV_2;
940 operator_string = "FC_DIV_2";
942 break;
943 case EXPR_MUL:
944 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
946 subexpr = subexpr->ref;
947 operator_type = RPC_FC_MULT_2;
948 operator_string = "FC_MULT_2";
950 break;
951 case EXPR_SUB:
952 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
954 subexpr = subexpr->ref;
955 operator_type = RPC_FC_SUB_1;
956 operator_string = "FC_SUB_1";
958 break;
959 case EXPR_ADD:
960 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
962 subexpr = subexpr->ref;
963 operator_type = RPC_FC_ADD_1;
964 operator_string = "FC_ADD_1";
966 break;
967 default:
968 break;
971 if (subexpr->type == EXPR_IDENTIFIER)
973 const type_t *correlation_variable = NULL;
974 unsigned char param_type = 0;
975 unsigned int offset = 0;
976 const var_t *var;
977 var_list_t *fields = type_struct_get_fields(structure);
979 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
981 unsigned int align = 0;
982 /* FIXME: take alignment into account */
983 if (var->name && !strcmp(var->name, subexpr->u.sval))
985 correlation_variable = var->type;
986 break;
988 offset += type_memsize(var->type, &align);
990 if (!correlation_variable)
991 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
992 subexpr->u.sval);
994 correlation_variable = expr_resolve_type(NULL, structure, expr);
996 offset -= baseoff;
998 if (type_get_type(correlation_variable) == TYPE_BASIC)
1000 switch (get_basic_fc(correlation_variable))
1002 case RPC_FC_CHAR:
1003 case RPC_FC_SMALL:
1004 param_type = RPC_FC_SMALL;
1005 break;
1006 case RPC_FC_BYTE:
1007 case RPC_FC_USMALL:
1008 param_type = RPC_FC_USMALL;
1009 break;
1010 case RPC_FC_WCHAR:
1011 case RPC_FC_SHORT:
1012 param_type = RPC_FC_SHORT;
1013 break;
1014 case RPC_FC_USHORT:
1015 param_type = RPC_FC_USHORT;
1016 break;
1017 case RPC_FC_LONG:
1018 param_type = RPC_FC_LONG;
1019 break;
1020 case RPC_FC_ULONG:
1021 param_type = RPC_FC_ULONG;
1022 break;
1023 default:
1024 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1025 get_basic_fc(correlation_variable));
1028 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1030 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1031 param_type = RPC_FC_LONG;
1032 else
1033 param_type = RPC_FC_SHORT;
1035 else
1037 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1038 subexpr->u.sval);
1039 return 0;
1042 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1043 conftype | param_type, conftype_string, string_of_type(param_type));
1044 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1045 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1046 offset, offset);
1048 else
1050 unsigned int callback_offset = 0;
1051 struct expr_eval_routine *eval;
1052 int found = 0;
1054 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1056 if (!strcmp (eval->structure->name, structure->name)
1057 && !compare_expr (eval->expr, expr))
1059 found = 1;
1060 break;
1062 callback_offset++;
1065 if (!found)
1067 eval = xmalloc (sizeof(*eval));
1068 eval->structure = structure;
1069 eval->baseoff = baseoff;
1070 eval->expr = expr;
1071 list_add_tail (&expr_eval_routines, &eval->entry);
1074 if (callback_offset > USHRT_MAX)
1075 error("Maximum number of callback routines reached\n");
1077 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1078 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1079 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1081 return 4;
1084 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1086 int have_align = FALSE;
1087 unsigned int size = 0;
1088 const var_t *v;
1090 if (!fields) return 0;
1091 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1093 unsigned int falign = 0;
1094 unsigned int fsize = type_memsize(v->type, &falign);
1095 if (!have_align)
1097 *align = falign;
1098 have_align = TRUE;
1100 size = ROUND_SIZE(size, falign);
1101 size += fsize;
1104 size = ROUND_SIZE(size, *align);
1105 return size;
1108 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1110 unsigned int size, maxs = 0;
1111 unsigned int align = *pmaxa;
1112 const var_t *v;
1114 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1116 /* we could have an empty default field with NULL type */
1117 if (v->type)
1119 size = type_memsize(v->type, &align);
1120 if (maxs < size) maxs = size;
1121 if (*pmaxa < align) *pmaxa = align;
1125 return maxs;
1128 int get_padding(const var_list_t *fields)
1130 unsigned short offset = 0;
1131 int salign = -1;
1132 const var_t *f;
1134 if (!fields)
1135 return 0;
1137 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1139 type_t *ft = f->type;
1140 unsigned int align = 0;
1141 unsigned int size = type_memsize(ft, &align);
1142 if (salign == -1)
1143 salign = align;
1144 offset = ROUND_SIZE(offset, align);
1145 offset += size;
1148 return ROUNDING(offset, salign);
1151 unsigned int type_memsize(const type_t *t, unsigned int *align)
1153 unsigned int size = 0;
1155 switch (type_get_type(t))
1157 case TYPE_BASIC:
1158 switch (get_basic_fc(t))
1160 case RPC_FC_BYTE:
1161 case RPC_FC_CHAR:
1162 case RPC_FC_USMALL:
1163 case RPC_FC_SMALL:
1164 size = 1;
1165 if (size > *align) *align = size;
1166 break;
1167 case RPC_FC_WCHAR:
1168 case RPC_FC_USHORT:
1169 case RPC_FC_SHORT:
1170 size = 2;
1171 if (size > *align) *align = size;
1172 break;
1173 case RPC_FC_ULONG:
1174 case RPC_FC_LONG:
1175 case RPC_FC_ERROR_STATUS_T:
1176 case RPC_FC_FLOAT:
1177 size = 4;
1178 if (size > *align) *align = size;
1179 break;
1180 case RPC_FC_HYPER:
1181 case RPC_FC_DOUBLE:
1182 size = 8;
1183 if (size > *align) *align = size;
1184 break;
1185 default:
1186 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1187 size = 0;
1189 break;
1190 case TYPE_ENUM:
1191 switch (get_enum_fc(t))
1193 case RPC_FC_ENUM32:
1194 size = 4;
1195 if (size > *align) *align = size;
1196 break;
1197 case RPC_FC_ENUM16:
1198 size = 2;
1199 if (size > *align) *align = size;
1200 break;
1201 default:
1202 error("type_memsize: Unknown enum type\n");
1203 size = 0;
1205 break;
1206 case TYPE_STRUCT:
1207 size = fields_memsize(type_struct_get_fields(t), align);
1208 break;
1209 case TYPE_ENCAPSULATED_UNION:
1210 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1211 break;
1212 case TYPE_UNION:
1213 size = union_memsize(type_union_get_cases(t), align);
1214 break;
1215 case TYPE_POINTER:
1216 assert( pointer_size );
1217 size = pointer_size;
1218 if (size > *align) *align = size;
1219 break;
1220 case TYPE_ARRAY:
1221 if (!type_array_is_decl_as_ptr(t))
1223 if (is_conformant_array(t))
1225 type_memsize(type_array_get_element(t), align);
1226 size = 0;
1228 else
1229 size = type_array_get_dim(t) *
1230 type_memsize(type_array_get_element(t), align);
1232 else /* declared as a pointer */
1234 assert( pointer_size );
1235 size = pointer_size;
1236 if (size > *align) *align = size;
1238 break;
1239 case TYPE_INTERFACE:
1240 case TYPE_ALIAS:
1241 case TYPE_VOID:
1242 case TYPE_COCLASS:
1243 case TYPE_MODULE:
1244 case TYPE_FUNCTION:
1245 /* these types should not be encountered here due to language
1246 * restrictions (interface, void, coclass, module), logical
1247 * restrictions (alias - due to type_get_type call above) or
1248 * checking restrictions (function). */
1249 assert(0);
1252 return size;
1255 int is_full_pointer_function(const var_t *func)
1257 const var_t *var;
1258 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1259 return TRUE;
1260 if (!type_get_function_args(func->type))
1261 return FALSE;
1262 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1263 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1264 return TRUE;
1265 return FALSE;
1268 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1270 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1271 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1272 fprintf(file, "\n");
1275 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1277 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1278 fprintf(file, "\n");
1281 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1282 const type_t *type,
1283 int toplevel_param,
1284 unsigned int offset,
1285 unsigned int *typeformat_offset)
1287 unsigned int start_offset = *typeformat_offset;
1288 short reloff = offset - (*typeformat_offset + 2);
1289 int in_attr, out_attr;
1290 int pointer_type;
1291 unsigned char flags = 0;
1293 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1295 in_attr = is_attr(attrs, ATTR_IN);
1296 out_attr = is_attr(attrs, ATTR_OUT);
1297 if (!in_attr && !out_attr) in_attr = 1;
1299 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1300 flags |= RPC_FC_P_ONSTACK;
1302 if (is_ptr(type) && !last_ptr(type))
1303 flags |= RPC_FC_P_DEREF;
1305 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1306 pointer_type,
1307 flags,
1308 string_of_type(pointer_type));
1309 if (file)
1311 if (flags & RPC_FC_P_ONSTACK)
1312 fprintf(file, " [allocated_on_stack]");
1313 if (flags & RPC_FC_P_DEREF)
1314 fprintf(file, " [pointer_deref]");
1315 fprintf(file, " */\n");
1318 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1319 *typeformat_offset += 4;
1321 return start_offset;
1324 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1326 unsigned char fc;
1327 unsigned char pointer_fc;
1328 const type_t *ref;
1330 /* for historical reasons, write_simple_pointer also handled string types,
1331 * but no longer does. catch bad uses of the function with this check */
1332 if (is_string_type(attrs, type))
1333 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1335 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1337 ref = type_pointer_get_ref(type);
1338 if (type_get_type(ref) == TYPE_ENUM)
1339 fc = get_enum_fc(ref);
1340 else
1341 fc = get_basic_fc(ref);
1343 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1344 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1345 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1346 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1347 return 4;
1350 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1352 print_file(file, 0, "/* %u (", tfsoff);
1353 write_type_decl(file, t, NULL);
1354 print_file(file, 0, ") */\n");
1357 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1358 type_t *type, int toplevel_param,
1359 unsigned int *typestring_offset)
1361 unsigned int offset = *typestring_offset;
1362 type_t *ref = type_pointer_get_ref(type);
1364 print_start_tfs_comment(file, type, offset);
1365 update_tfsoff(type, offset, file);
1367 if (ref->typestring_offset)
1368 write_nonsimple_pointer(file, attrs, type,
1369 toplevel_param,
1370 type_pointer_get_ref(type)->typestring_offset,
1371 typestring_offset);
1372 else if (type_get_type(ref) == TYPE_BASIC ||
1373 type_get_type(ref) == TYPE_ENUM)
1374 *typestring_offset += write_simple_pointer(file, attrs, type,
1375 toplevel_param);
1377 return offset;
1380 static int processed(const type_t *type)
1382 return type->typestring_offset && !type->tfswrite;
1385 static int user_type_has_variable_size(const type_t *t)
1387 if (is_ptr(t))
1388 return TRUE;
1389 else if (type_get_type(t) == TYPE_STRUCT)
1391 switch (get_struct_fc(t))
1393 case RPC_FC_PSTRUCT:
1394 case RPC_FC_CSTRUCT:
1395 case RPC_FC_CPSTRUCT:
1396 case RPC_FC_CVSTRUCT:
1397 return TRUE;
1400 /* Note: Since this only applies to user types, we can't have a conformant
1401 array here, and strings should get filed under pointer in this case. */
1402 return FALSE;
1405 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1407 unsigned int start, absoff, flags;
1408 unsigned int align = 0, ualign = 0;
1409 const char *name = NULL;
1410 type_t *utype = get_user_type(type, &name);
1411 unsigned int usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
1412 unsigned int size = type_memsize(type, &align);
1413 unsigned short funoff = user_type_offset(name);
1414 short reloff;
1416 guard_rec(type);
1418 if (type_get_type(utype) == TYPE_BASIC ||
1419 type_get_type(utype) == TYPE_ENUM)
1421 unsigned char fc;
1423 if (type_get_type(utype) == TYPE_ENUM)
1424 fc = get_enum_fc(utype);
1425 else
1426 fc = get_basic_fc(utype);
1428 absoff = *tfsoff;
1429 print_start_tfs_comment(file, utype, absoff);
1430 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1431 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1432 *tfsoff += 2;
1434 else
1436 if (!processed(utype))
1437 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1438 absoff = utype->typestring_offset;
1441 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1442 flags = 0x40;
1443 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1444 flags = 0x80;
1445 else
1446 flags = 0;
1448 start = *tfsoff;
1449 update_tfsoff(type, start, file);
1450 print_start_tfs_comment(file, type, start);
1451 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1452 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1453 flags | (align - 1), align - 1, flags);
1454 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1455 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1456 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1457 *tfsoff += 8;
1458 reloff = absoff - *tfsoff;
1459 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1460 *tfsoff += 2;
1463 static void write_member_type(FILE *file, const type_t *cont,
1464 int cont_is_complex, const attr_list_t *attrs,
1465 const type_t *type, unsigned int *corroff,
1466 unsigned int *tfsoff)
1468 if (is_embedded_complex(type) && !is_conformant_array(type))
1470 unsigned int absoff;
1471 short reloff;
1473 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1475 absoff = *corroff;
1476 *corroff += 8;
1478 else
1480 absoff = type->typestring_offset;
1482 reloff = absoff - (*tfsoff + 2);
1484 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1485 /* FIXME: actually compute necessary padding */
1486 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1487 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1488 reloff, reloff, absoff);
1489 *tfsoff += 4;
1491 else if (is_ptr(type) || is_conformant_array(type))
1493 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1494 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1495 *tfsoff += 1;
1497 else if (!write_base_type(file, type, TRUE, tfsoff))
1498 error("Unsupported member type %d\n", type_get_type(type));
1501 static void write_end(FILE *file, unsigned int *tfsoff)
1503 if (*tfsoff % 2 == 0)
1505 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1506 *tfsoff += 1;
1508 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1509 *tfsoff += 1;
1512 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1514 unsigned int offset = 0;
1515 var_list_t *fs = type_struct_get_fields(type);
1516 var_t *f;
1518 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1520 unsigned int align = 0;
1521 type_t *ft = f->type;
1522 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1524 unsigned int absoff = ft->typestring_offset;
1525 short reloff = absoff - (*tfsoff + 6);
1526 print_file(file, 0, "/* %d */\n", *tfsoff);
1527 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1528 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1529 write_conf_or_var_desc(file, current_structure, offset, ft,
1530 get_attrp(f->attrs, ATTR_SWITCHIS));
1531 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1532 reloff, reloff, absoff);
1533 *tfsoff += 8;
1536 /* FIXME: take alignment into account */
1537 offset += type_memsize(ft, &align);
1541 static int write_no_repeat_pointer_descriptions(
1542 FILE *file, const attr_list_t *attrs, type_t *type,
1543 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1544 unsigned int *typestring_offset)
1546 int written = 0;
1547 unsigned int align;
1549 if (is_ptr(type) ||
1550 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1552 unsigned int memsize;
1554 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1555 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1557 /* pointer instance */
1558 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1559 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1560 *typestring_offset += 6;
1562 if (is_ptr(type))
1564 if (is_string_type(attrs, type))
1565 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1566 else
1567 write_pointer_tfs(file, attrs, type, FALSE, typestring_offset);
1569 else
1571 unsigned int offset = type->typestring_offset;
1572 /* skip over the pointer that is written for strings, since a
1573 * pointer has to be written in-place here */
1574 if (is_string_type(attrs, type))
1575 offset += 4;
1576 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1579 align = 0;
1580 memsize = type_memsize(type, &align);
1581 *offset_in_memory += memsize;
1582 /* increment these separately as in the case of conformant (varying)
1583 * structures these start at different values */
1584 *offset_in_buffer += memsize;
1586 return 1;
1589 if (is_non_complex_struct(type))
1591 const var_t *v;
1592 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1594 if (offset_in_memory && offset_in_buffer)
1596 unsigned int padding;
1597 align = 0;
1598 type_memsize(v->type, &align);
1599 padding = ROUNDING(*offset_in_memory, align);
1600 *offset_in_memory += padding;
1601 *offset_in_buffer += padding;
1603 written += write_no_repeat_pointer_descriptions(
1604 file, v->attrs, v->type,
1605 offset_in_memory, offset_in_buffer, typestring_offset);
1608 else
1610 unsigned int memsize;
1611 align = 0;
1612 memsize = type_memsize(type, &align);
1613 *offset_in_memory += memsize;
1614 /* increment these separately as in the case of conformant (varying)
1615 * structures these start at different values */
1616 *offset_in_buffer += memsize;
1619 return written;
1622 static int write_pointer_description_offsets(
1623 FILE *file, const attr_list_t *attrs, type_t *type,
1624 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1625 unsigned int *typestring_offset)
1627 int written = 0;
1628 unsigned int align;
1630 if (is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE)
1632 type_t *ref = type_pointer_get_ref(type);
1634 if (offset_in_memory && offset_in_buffer)
1636 unsigned int memsize;
1638 /* pointer instance */
1639 /* FIXME: sometimes from end of structure, sometimes from beginning */
1640 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1641 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1643 align = 0;
1644 memsize = type_memsize(type, &align);
1645 *offset_in_memory += memsize;
1646 /* increment these separately as in the case of conformant (varying)
1647 * structures these start at different values */
1648 *offset_in_buffer += memsize;
1650 *typestring_offset += 4;
1652 if (is_string_type(attrs, type))
1653 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1654 else if (processed(ref) || type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1655 write_pointer_tfs(file, attrs, type, FALSE, typestring_offset);
1656 else
1657 error("write_pointer_description_offsets: type format string unknown\n");
1659 return 1;
1662 if (is_array(type))
1664 return write_pointer_description_offsets(
1665 file, attrs, type_array_get_element(type), offset_in_memory,
1666 offset_in_buffer, typestring_offset);
1668 else if (is_non_complex_struct(type))
1670 /* otherwise search for interesting fields to parse */
1671 const var_t *v;
1672 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1674 if (offset_in_memory && offset_in_buffer)
1676 unsigned int padding;
1677 align = 0;
1678 type_memsize(v->type, &align);
1679 padding = ROUNDING(*offset_in_memory, align);
1680 *offset_in_memory += padding;
1681 *offset_in_buffer += padding;
1683 written += write_pointer_description_offsets(
1684 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1685 typestring_offset);
1688 else
1690 if (offset_in_memory && offset_in_buffer)
1692 unsigned int memsize;
1693 align = 0;
1694 memsize = type_memsize(type, &align);
1695 *offset_in_memory += memsize;
1696 /* increment these separately as in the case of conformant (varying)
1697 * structures these start at different values */
1698 *offset_in_buffer += memsize;
1702 return written;
1705 /* Note: if file is NULL return value is number of pointers to write, else
1706 * it is the number of type format characters written */
1707 static int write_fixed_array_pointer_descriptions(
1708 FILE *file, const attr_list_t *attrs, type_t *type,
1709 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1710 unsigned int *typestring_offset)
1712 unsigned int align;
1713 int pointer_count = 0;
1715 if (type_get_type(type) == TYPE_ARRAY &&
1716 !type_array_has_conformance(type) && !type_array_has_variance(type))
1718 unsigned int temp = 0;
1719 /* unfortunately, this needs to be done in two passes to avoid
1720 * writing out redundant FC_FIXED_REPEAT descriptions */
1721 pointer_count = write_pointer_description_offsets(
1722 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1723 if (pointer_count > 0)
1725 unsigned int increment_size;
1726 unsigned int offset_of_array_pointer_mem = 0;
1727 unsigned int offset_of_array_pointer_buf = 0;
1729 align = 0;
1730 increment_size = type_memsize(type_array_get_element(type), &align);
1732 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1733 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1734 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1735 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1736 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1737 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1738 *typestring_offset += 10;
1740 pointer_count = write_pointer_description_offsets(
1741 file, attrs, type, &offset_of_array_pointer_mem,
1742 &offset_of_array_pointer_buf, typestring_offset);
1745 else if (type_get_type(type) == TYPE_STRUCT)
1747 const var_t *v;
1748 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1750 if (offset_in_memory && offset_in_buffer)
1752 unsigned int padding;
1753 align = 0;
1754 type_memsize(v->type, &align);
1755 padding = ROUNDING(*offset_in_memory, align);
1756 *offset_in_memory += padding;
1757 *offset_in_buffer += padding;
1759 pointer_count += write_fixed_array_pointer_descriptions(
1760 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1761 typestring_offset);
1764 else
1766 if (offset_in_memory && offset_in_buffer)
1768 unsigned int memsize;
1769 align = 0;
1770 memsize = type_memsize(type, &align);
1771 *offset_in_memory += memsize;
1772 /* increment these separately as in the case of conformant (varying)
1773 * structures these start at different values */
1774 *offset_in_buffer += memsize;
1778 return pointer_count;
1781 /* Note: if file is NULL return value is number of pointers to write, else
1782 * it is the number of type format characters written */
1783 static int write_conformant_array_pointer_descriptions(
1784 FILE *file, const attr_list_t *attrs, type_t *type,
1785 unsigned int offset_in_memory, unsigned int *typestring_offset)
1787 unsigned int align;
1788 int pointer_count = 0;
1790 if (is_conformant_array(type) && !type_array_has_variance(type))
1792 unsigned int temp = 0;
1793 /* unfortunately, this needs to be done in two passes to avoid
1794 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1795 pointer_count = write_pointer_description_offsets(
1796 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1797 if (pointer_count > 0)
1799 unsigned int increment_size;
1800 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1801 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1803 align = 0;
1804 increment_size = type_memsize(type_array_get_element(type), &align);
1806 if (increment_size > USHRT_MAX)
1807 error("array size of %u bytes is too large\n", increment_size);
1809 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1810 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1811 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1812 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1813 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1814 *typestring_offset += 8;
1816 pointer_count = write_pointer_description_offsets(
1817 file, attrs, type_array_get_element(type),
1818 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1819 typestring_offset);
1823 return pointer_count;
1826 /* Note: if file is NULL return value is number of pointers to write, else
1827 * it is the number of type format characters written */
1828 static int write_varying_array_pointer_descriptions(
1829 FILE *file, const attr_list_t *attrs, type_t *type,
1830 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1831 unsigned int *typestring_offset)
1833 unsigned int align;
1834 int pointer_count = 0;
1836 if (is_array(type) && type_array_has_variance(type))
1838 unsigned int temp = 0;
1839 /* unfortunately, this needs to be done in two passes to avoid
1840 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1841 pointer_count = write_pointer_description_offsets(
1842 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1843 if (pointer_count > 0)
1845 unsigned int increment_size;
1847 align = 0;
1848 increment_size = type_memsize(type_array_get_element(type), &align);
1850 if (increment_size > USHRT_MAX)
1851 error("array size of %u bytes is too large\n", increment_size);
1853 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1854 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1855 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1856 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1857 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1858 *typestring_offset += 8;
1860 pointer_count = write_pointer_description_offsets(
1861 file, attrs, type, offset_in_memory,
1862 offset_in_buffer, typestring_offset);
1865 else if (type_get_type(type) == TYPE_STRUCT)
1867 const var_t *v;
1868 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1870 if (offset_in_memory && offset_in_buffer)
1872 unsigned int padding;
1874 if (is_array(v->type) && type_array_has_variance(v->type))
1876 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1877 /* skip over variance and offset in buffer */
1878 *offset_in_buffer += 8;
1881 align = 0;
1882 type_memsize(v->type, &align);
1883 padding = ROUNDING(*offset_in_memory, align);
1884 *offset_in_memory += padding;
1885 *offset_in_buffer += padding;
1887 pointer_count += write_varying_array_pointer_descriptions(
1888 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1889 typestring_offset);
1892 else
1894 if (offset_in_memory && offset_in_buffer)
1896 unsigned int memsize;
1897 align = 0;
1898 memsize = type_memsize(type, &align);
1899 *offset_in_memory += memsize;
1900 /* increment these separately as in the case of conformant (varying)
1901 * structures these start at different values */
1902 *offset_in_buffer += memsize;
1906 return pointer_count;
1909 static void write_pointer_description(FILE *file, type_t *type,
1910 unsigned int *typestring_offset)
1912 unsigned int offset_in_buffer;
1913 unsigned int offset_in_memory;
1915 /* pass 1: search for single instance of a pointer (i.e. don't descend
1916 * into arrays) */
1917 if (!is_array(type))
1919 offset_in_memory = 0;
1920 offset_in_buffer = 0;
1921 write_no_repeat_pointer_descriptions(
1922 file, NULL, type,
1923 &offset_in_memory, &offset_in_buffer, typestring_offset);
1926 /* pass 2: search for pointers in fixed arrays */
1927 offset_in_memory = 0;
1928 offset_in_buffer = 0;
1929 write_fixed_array_pointer_descriptions(
1930 file, NULL, type,
1931 &offset_in_memory, &offset_in_buffer, typestring_offset);
1933 /* pass 3: search for pointers in conformant only arrays (but don't descend
1934 * into conformant varying or varying arrays) */
1935 if (is_conformant_array(type) &&
1936 (type_array_is_decl_as_ptr(type) || !current_structure))
1937 write_conformant_array_pointer_descriptions(
1938 file, NULL, type, 0, typestring_offset);
1939 else if (type_get_type(type) == TYPE_STRUCT &&
1940 get_struct_fc(type) == RPC_FC_CPSTRUCT)
1942 unsigned int align = 0;
1943 type_t *carray = find_array_or_string_in_struct(type)->type;
1944 write_conformant_array_pointer_descriptions(
1945 file, NULL, carray,
1946 type_memsize(type, &align),
1947 typestring_offset);
1950 /* pass 4: search for pointers in varying arrays */
1951 offset_in_memory = 0;
1952 offset_in_buffer = 0;
1953 write_varying_array_pointer_descriptions(
1954 file, NULL, type,
1955 &offset_in_memory, &offset_in_buffer, typestring_offset);
1958 int is_declptr(const type_t *t)
1960 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
1963 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
1964 type_t *type, int toplevel_param,
1965 const char *name, unsigned int *typestring_offset)
1967 unsigned int start_offset;
1968 unsigned char rtype;
1969 type_t *elem_type;
1971 start_offset = *typestring_offset;
1972 update_tfsoff(type, start_offset, file);
1974 if (is_declptr(type))
1976 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
1977 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1978 if (!pointer_type)
1979 pointer_type = RPC_FC_RP;
1980 print_start_tfs_comment(file, type, *typestring_offset);
1981 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1982 pointer_type, flag, string_of_type(pointer_type),
1983 flag ? " [simple_pointer]" : "");
1984 *typestring_offset += 2;
1985 if (!flag)
1987 print_file(file, 2, "NdrFcShort(0x2),\n");
1988 *typestring_offset += 2;
1992 if (is_array(type))
1993 elem_type = type_array_get_element(type);
1994 else
1995 elem_type = type_pointer_get_ref(type);
1997 if (type_get_type(elem_type) != TYPE_BASIC)
1999 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2000 return start_offset;
2003 rtype = get_basic_fc(elem_type);
2004 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2006 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2007 return start_offset;
2010 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2012 unsigned int dim = type_array_get_dim(type);
2014 /* FIXME: multi-dimensional array */
2015 if (0xffffu < dim)
2016 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2017 name, 0xffffu, dim - 0xffffu);
2019 if (rtype == RPC_FC_WCHAR)
2020 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2021 else
2022 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2023 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2024 *typestring_offset += 2;
2026 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2027 *typestring_offset += 2;
2029 return start_offset;
2031 else if (is_conformant_array(type))
2033 unsigned int align = 0;
2035 if (rtype == RPC_FC_WCHAR)
2036 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2037 else
2038 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2039 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2040 *typestring_offset += 2;
2042 *typestring_offset += write_conf_or_var_desc(
2043 file, current_structure,
2044 (!type_array_is_decl_as_ptr(type) && current_structure
2045 ? type_memsize(current_structure, &align)
2046 : 0),
2047 type, type_array_get_conformance(type));
2049 return start_offset;
2051 else
2053 if (rtype == RPC_FC_WCHAR)
2054 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2055 else
2056 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2057 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2058 *typestring_offset += 2;
2060 return start_offset;
2064 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2065 const char *name, unsigned int *typestring_offset)
2067 const expr_t *length_is = type_array_get_variance(type);
2068 const expr_t *size_is = type_array_get_conformance(type);
2069 unsigned int align = 0;
2070 unsigned int size;
2071 unsigned int start_offset;
2072 unsigned char fc;
2073 int has_pointer;
2074 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2075 unsigned int baseoff
2076 = !type_array_is_decl_as_ptr(type) && current_structure
2077 ? type_memsize(current_structure, &align)
2078 : 0;
2080 if (!pointer_type)
2081 pointer_type = RPC_FC_RP;
2083 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2084 has_pointer = TRUE;
2085 else
2086 has_pointer = type_has_pointers(type_array_get_element(type));
2088 align = 0;
2089 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2090 fc = get_array_fc(type);
2092 start_offset = *typestring_offset;
2093 update_tfsoff(type, start_offset, file);
2094 print_start_tfs_comment(file, type, start_offset);
2095 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2096 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2097 *typestring_offset += 2;
2099 align = 0;
2100 if (fc != RPC_FC_BOGUS_ARRAY)
2102 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2104 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2105 *typestring_offset += 4;
2107 else
2109 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2110 *typestring_offset += 2;
2113 if (is_conformant_array(type))
2114 *typestring_offset
2115 += write_conf_or_var_desc(file, current_structure, baseoff,
2116 type, size_is);
2118 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2120 unsigned int elalign = 0;
2121 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2122 unsigned int dim = type_array_get_dim(type);
2124 if (fc == RPC_FC_LGVARRAY)
2126 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2127 *typestring_offset += 4;
2129 else
2131 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2132 *typestring_offset += 2;
2135 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2136 *typestring_offset += 2;
2139 if (length_is)
2140 *typestring_offset
2141 += write_conf_or_var_desc(file, current_structure, baseoff,
2142 type, length_is);
2144 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2146 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2147 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2148 *typestring_offset += 2;
2149 write_pointer_description(file, type, typestring_offset);
2150 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2151 *typestring_offset += 1;
2154 write_member_type(file, type, FALSE, NULL, type_array_get_element(type), NULL, typestring_offset);
2155 write_end(file, typestring_offset);
2157 else
2159 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2160 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2161 *typestring_offset += 2;
2162 *typestring_offset
2163 += write_conf_or_var_desc(file, current_structure, baseoff,
2164 type, size_is);
2165 *typestring_offset
2166 += write_conf_or_var_desc(file, current_structure, baseoff,
2167 type, length_is);
2168 write_member_type(file, type, TRUE, NULL, type_array_get_element(type), NULL, typestring_offset);
2169 write_end(file, typestring_offset);
2172 return start_offset;
2175 static const var_t *find_array_or_string_in_struct(const type_t *type)
2177 const var_list_t *fields = type_struct_get_fields(type);
2178 const var_t *last_field;
2179 const type_t *ft;
2181 if (!fields || list_empty(fields))
2182 return NULL;
2184 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2185 ft = last_field->type;
2187 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2188 return last_field;
2190 if (type_get_type(ft) == TYPE_STRUCT)
2191 return find_array_or_string_in_struct(ft);
2192 else
2193 return NULL;
2196 static void write_struct_members(FILE *file, const type_t *type,
2197 int is_complex, unsigned int *corroff,
2198 unsigned int *typestring_offset)
2200 const var_t *field;
2201 unsigned short offset = 0;
2202 int salign = -1;
2203 int padding;
2204 var_list_t *fields = type_struct_get_fields(type);
2206 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2208 type_t *ft = field->type;
2209 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2211 unsigned int align = 0;
2212 unsigned int size = type_memsize(ft, &align);
2213 if (salign == -1)
2214 salign = align;
2215 if ((align - 1) & offset)
2217 unsigned char fc = 0;
2218 switch (align)
2220 case 4:
2221 fc = RPC_FC_ALIGNM4;
2222 break;
2223 case 8:
2224 fc = RPC_FC_ALIGNM8;
2225 break;
2226 default:
2227 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2229 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2230 offset = ROUND_SIZE(offset, align);
2231 *typestring_offset += 1;
2233 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2234 typestring_offset);
2235 offset += size;
2239 padding = ROUNDING(offset, salign);
2240 if (padding)
2242 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2243 RPC_FC_STRUCTPAD1 + padding - 1,
2244 padding);
2245 *typestring_offset += 1;
2248 write_end(file, typestring_offset);
2251 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2252 const char *name, unsigned int *tfsoff)
2254 const type_t *save_current_structure = current_structure;
2255 unsigned int total_size;
2256 const var_t *array;
2257 unsigned int start_offset;
2258 unsigned int array_offset;
2259 int has_pointers = 0;
2260 unsigned int align = 0;
2261 unsigned int corroff;
2262 var_t *f;
2263 unsigned char fc = get_struct_fc(type);
2264 var_list_t *fields = type_struct_get_fields(type);
2266 guard_rec(type);
2267 current_structure = type;
2269 total_size = type_memsize(type, &align);
2270 if (total_size > USHRT_MAX)
2271 error("structure size for %s exceeds %d bytes by %d bytes\n",
2272 name, USHRT_MAX, total_size - USHRT_MAX);
2274 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2275 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2276 FALSE, tfsoff);
2277 if (!has_pointers) has_pointers = type_has_pointers(type);
2279 array = find_array_or_string_in_struct(type);
2280 if (array && !processed(array->type))
2281 array_offset
2282 = is_string_type(array->attrs, array->type)
2283 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2284 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2286 corroff = *tfsoff;
2287 write_descriptors(file, type, tfsoff);
2289 start_offset = *tfsoff;
2290 update_tfsoff(type, start_offset, file);
2291 print_start_tfs_comment(file, type, start_offset);
2292 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2293 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2294 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2295 *tfsoff += 4;
2297 if (array)
2299 unsigned int absoff = array->type->typestring_offset;
2300 short reloff = absoff - *tfsoff;
2301 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2302 reloff, reloff, absoff);
2303 *tfsoff += 2;
2305 else if (fc == RPC_FC_BOGUS_STRUCT)
2307 print_file(file, 2, "NdrFcShort(0x0),\n");
2308 *tfsoff += 2;
2311 if (fc == RPC_FC_BOGUS_STRUCT)
2313 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2314 nothing is written to file yet. On the actual writing pass,
2315 this will have been updated. */
2316 unsigned int absoff = type_get_real_type(type)->ptrdesc ?
2317 type_get_real_type(type)->ptrdesc : *tfsoff;
2318 int reloff = absoff - *tfsoff;
2319 assert( reloff >= 0 );
2320 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2321 reloff, reloff, absoff);
2322 *tfsoff += 2;
2324 else if ((fc == RPC_FC_PSTRUCT) ||
2325 (fc == RPC_FC_CPSTRUCT) ||
2326 (fc == RPC_FC_CVSTRUCT && has_pointers))
2328 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2329 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2330 *tfsoff += 2;
2331 write_pointer_description(file, type, tfsoff);
2332 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2333 *tfsoff += 1;
2336 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2337 tfsoff);
2339 if (fc == RPC_FC_BOGUS_STRUCT)
2341 const var_t *f;
2343 type_get_real_type(type)->ptrdesc = *tfsoff;
2344 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2346 type_t *ft = f->type;
2347 if (is_ptr(ft))
2349 if (is_string_type(f->attrs, ft))
2350 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2351 else
2352 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2354 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2356 unsigned int offset;
2358 print_file(file, 0, "/* %d */\n", *tfsoff);
2360 offset = ft->typestring_offset;
2361 /* skip over the pointer that is written for strings, since a
2362 * pointer has to be written in-place here */
2363 if (is_string_type(f->attrs, ft))
2364 offset += 4;
2365 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2368 if (type_get_real_type(type)->ptrdesc == *tfsoff)
2369 type_get_real_type(type)->ptrdesc = 0;
2372 current_structure = save_current_structure;
2373 return start_offset;
2376 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2378 if (t == NULL)
2380 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2382 else
2384 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2386 unsigned char fc;
2387 if (type_get_type(t) == TYPE_BASIC)
2388 fc = get_basic_fc(t);
2389 else
2390 fc = get_enum_fc(t);
2391 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2392 fc, string_of_type(fc));
2394 else if (t->typestring_offset)
2396 short reloff = t->typestring_offset - *tfsoff;
2397 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2398 reloff, reloff, t->typestring_offset);
2400 else
2401 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2404 *tfsoff += 2;
2407 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2409 unsigned int align;
2410 unsigned int start_offset;
2411 unsigned int size;
2412 var_list_t *fields;
2413 unsigned int nbranch = 0;
2414 type_t *deftype = NULL;
2415 short nodeftype = 0xffff;
2416 var_t *f;
2418 guard_rec(type);
2420 align = 0;
2421 size = type_memsize(type, &align);
2423 fields = type_union_get_cases(type);
2425 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2427 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2428 if (cases)
2429 nbranch += list_count(cases);
2430 if (f->type)
2431 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2434 start_offset = *tfsoff;
2435 update_tfsoff(type, start_offset, file);
2436 print_start_tfs_comment(file, type, start_offset);
2437 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2439 const var_t *sv = type_union_get_switch_value(type);
2440 const type_t *st = sv->type;
2441 unsigned char fc;
2443 if (type_get_type(st) == TYPE_BASIC)
2445 switch (get_basic_fc(st))
2447 case RPC_FC_CHAR:
2448 case RPC_FC_SMALL:
2449 case RPC_FC_BYTE:
2450 case RPC_FC_USMALL:
2451 case RPC_FC_WCHAR:
2452 case RPC_FC_SHORT:
2453 case RPC_FC_USHORT:
2454 case RPC_FC_LONG:
2455 case RPC_FC_ULONG:
2456 fc = get_basic_fc(st);
2457 break;
2458 default:
2459 fc = 0;
2460 error("union switch type must be an integer, char, or enum\n");
2463 else if (type_get_type(st) == TYPE_ENUM)
2464 fc = get_enum_fc(st);
2465 else
2466 error("union switch type must be an integer, char, or enum\n");
2468 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2469 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2470 0x40 | fc, string_of_type(fc));
2471 *tfsoff += 2;
2473 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2475 static const expr_t dummy_expr; /* FIXME */
2476 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2477 unsigned char fc;
2479 if (type_get_type(st) == TYPE_BASIC)
2481 switch (get_basic_fc(st))
2483 case RPC_FC_CHAR:
2484 case RPC_FC_SMALL:
2485 case RPC_FC_USMALL:
2486 case RPC_FC_SHORT:
2487 case RPC_FC_USHORT:
2488 case RPC_FC_LONG:
2489 case RPC_FC_ULONG:
2490 case RPC_FC_ENUM16:
2491 case RPC_FC_ENUM32:
2492 fc = get_basic_fc(st);
2493 break;
2494 default:
2495 fc = 0;
2496 error("union switch type must be an integer, char, or enum\n");
2499 else if (type_get_type(st) == TYPE_ENUM)
2500 fc = get_enum_fc(st);
2501 else
2502 error("union switch type must be an integer, char, or enum\n");
2504 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2505 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2506 fc, string_of_type(fc));
2507 *tfsoff += 2;
2509 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2510 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2511 *tfsoff += 2;
2514 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2515 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2516 *tfsoff += 4;
2518 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2520 type_t *ft = f->type;
2521 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2522 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2523 expr_t *c;
2525 if (cases == NULL && !deflt)
2526 error("union field %s with neither case nor default attribute\n", f->name);
2528 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2530 /* MIDL doesn't check for duplicate cases, even though that seems
2531 like a reasonable thing to do, it just dumps them to the TFS
2532 like we're going to do here. */
2533 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2534 *tfsoff += 4;
2535 write_branch_type(file, ft, tfsoff);
2538 /* MIDL allows multiple default branches, even though that seems
2539 illogical, it just chooses the last one, which is what we will
2540 do. */
2541 if (deflt)
2543 deftype = ft;
2544 nodeftype = 0;
2548 if (deftype)
2550 write_branch_type(file, deftype, tfsoff);
2552 else
2554 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2555 *tfsoff += 2;
2558 return start_offset;
2561 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2562 unsigned int *typeformat_offset)
2564 unsigned int i;
2565 unsigned int start_offset = *typeformat_offset;
2566 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2568 if (iid)
2570 print_file(file, 2, "0x2f, /* FC_IP */\n");
2571 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2572 *typeformat_offset
2573 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2575 else
2577 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2578 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2580 if (! uuid)
2581 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2583 update_tfsoff(type, start_offset, file);
2584 print_start_tfs_comment(file, type, start_offset);
2585 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2586 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2587 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2588 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2589 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2590 for (i = 0; i < 8; ++i)
2591 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2593 if (file)
2594 fprintf(file, "\n");
2596 *typeformat_offset += 18;
2598 return start_offset;
2601 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2602 const var_t *var,
2603 unsigned int *typeformat_offset)
2605 unsigned int start_offset = *typeformat_offset;
2606 unsigned char flags = 0;
2608 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2609 flags |= NDR_STRICT_CONTEXT_HANDLE;
2611 if (is_ptr(type))
2612 flags |= 0x80;
2613 if (is_attr(var->attrs, ATTR_IN))
2615 flags |= 0x40;
2616 if (!is_attr(var->attrs, ATTR_OUT))
2617 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2619 if (is_attr(var->attrs, ATTR_OUT))
2620 flags |= 0x20;
2622 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2623 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2624 /* return and can't be null values overlap */
2625 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2626 print_file(file, 0, "can't be null, ");
2627 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2628 print_file(file, 0, "serialize, ");
2629 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2630 print_file(file, 0, "no serialize, ");
2631 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2632 print_file(file, 0, "strict, ");
2633 if ((flags & 0x21) == 0x20)
2634 print_file(file, 0, "out, ");
2635 if ((flags & 0x21) == 0x21)
2636 print_file(file, 0, "return, ");
2637 if (flags & 0x40)
2638 print_file(file, 0, "in, ");
2639 if (flags & 0x80)
2640 print_file(file, 0, "via ptr, ");
2641 print_file(file, 0, "*/\n");
2642 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2643 print_file(file, 2, "0, /* FIXME: param num */\n");
2644 *typeformat_offset += 4;
2646 return start_offset;
2649 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2650 type_t *type, const var_t *var,
2651 int toplevel_param,
2652 unsigned int *typeformat_offset)
2654 unsigned int offset;
2656 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2658 case TGT_CTXT_HANDLE:
2659 case TGT_CTXT_HANDLE_POINTER:
2660 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2661 case TGT_USER_TYPE:
2662 write_user_tfs(file, type, typeformat_offset);
2663 return type->typestring_offset;
2664 case TGT_STRING:
2665 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2666 case TGT_ARRAY:
2668 int ptr_type;
2669 unsigned int off;
2670 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2671 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2672 if (ptr_type != RPC_FC_RP)
2674 unsigned int absoff = type->typestring_offset;
2675 short reloff = absoff - (*typeformat_offset + 2);
2676 off = *typeformat_offset;
2677 print_file(file, 0, "/* %d */\n", off);
2678 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2679 string_of_type(ptr_type));
2680 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2681 reloff, reloff, absoff);
2682 *typeformat_offset += 4;
2684 return off;
2686 case TGT_STRUCT:
2687 if (processed(type)) return type->typestring_offset;
2688 return write_struct_tfs(file, type, var->name, typeformat_offset);
2689 case TGT_UNION:
2690 if (processed(type)) return type->typestring_offset;
2691 return write_union_tfs(file, type, typeformat_offset);
2692 case TGT_ENUM:
2693 case TGT_BASIC:
2694 /* nothing to do */
2695 return 0;
2696 case TGT_IFACE_POINTER:
2697 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2698 case TGT_POINTER:
2699 if (last_ptr(type))
2701 size_t start_offset = *typeformat_offset;
2702 int in_attr = is_attr(var->attrs, ATTR_IN);
2703 int out_attr = is_attr(var->attrs, ATTR_OUT);
2704 const type_t *ref = type_pointer_get_ref(type);
2706 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2708 /* special case for pointers to base types */
2709 case TGT_BASIC:
2710 case TGT_ENUM:
2712 unsigned char fc;
2714 if (type_get_type(ref) == TYPE_ENUM)
2715 fc = get_enum_fc(ref);
2716 else
2717 fc = get_basic_fc(ref);
2719 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2720 get_pointer_fc(type, var->attrs, toplevel_param),
2721 (!in_attr && out_attr) ? 0x0C : 0x08,
2722 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2723 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2724 print_file(file, indent, "0x%02x, /* %s */\n",
2725 fc, string_of_type(fc));
2726 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2727 *typeformat_offset += 4;
2728 return start_offset;
2730 default:
2731 break;
2735 offset = write_typeformatstring_var(file, indent, func,
2736 type_pointer_get_ref(type), var,
2737 FALSE, typeformat_offset);
2738 if (file)
2739 fprintf(file, "/* %2u */\n", *typeformat_offset);
2740 return write_nonsimple_pointer(file, var->attrs, type,
2741 toplevel_param,
2742 offset, typeformat_offset);
2743 case TGT_INVALID:
2744 break;
2746 error("invalid type %s for var %s\n", type->name, var->name);
2747 return 0;
2750 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2751 const char *name, int write_ptr, unsigned int *tfsoff)
2753 int retmask = 0;
2755 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2757 case TGT_USER_TYPE:
2758 write_user_tfs(file, type, tfsoff);
2759 break;
2760 case TGT_STRING:
2761 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2762 break;
2763 case TGT_IFACE_POINTER:
2764 write_ip_tfs(file, attrs, type, tfsoff);
2765 break;
2766 case TGT_POINTER:
2768 type_t *ref = type_pointer_get_ref(type);
2770 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2771 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2773 if (write_ptr)
2774 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2776 retmask |= 1;
2777 break;
2779 case TGT_ARRAY:
2780 /* conformant arrays and strings are handled specially */
2781 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2783 write_array_tfs(file, attrs, type, name, tfsoff);
2784 if (is_conformant_array(type))
2785 retmask |= 1;
2787 break;
2788 case TGT_STRUCT:
2789 if (!processed(type))
2790 write_struct_tfs(file, type, name, tfsoff);
2791 break;
2792 case TGT_UNION:
2793 if (!processed(type))
2794 write_union_tfs(file, type, tfsoff);
2795 break;
2796 case TGT_ENUM:
2797 case TGT_BASIC:
2798 /* nothing to do */
2799 break;
2800 case TGT_CTXT_HANDLE:
2801 case TGT_CTXT_HANDLE_POINTER:
2802 case TGT_INVALID:
2803 error("invalid type %s for var %s\n", type->name, name);
2804 break;
2807 return retmask;
2810 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2811 type_pred_t pred, unsigned int *typeformat_offset)
2813 const var_t *var;
2814 const statement_t *stmt;
2816 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2818 const type_t *iface;
2819 const statement_t *stmt_func;
2821 if (stmt->type == STMT_LIBRARY)
2823 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2824 continue;
2826 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2827 continue;
2829 iface = stmt->u.type;
2830 if (!pred(iface))
2831 continue;
2833 current_iface = iface;
2834 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2836 const var_t *func = stmt_func->u.var;
2837 if (is_local(func->attrs)) continue;
2839 if (!is_void(type_function_get_rettype(func->type)))
2841 var_t v = *func;
2842 v.type = type_function_get_rettype(func->type);
2843 update_tfsoff(type_function_get_rettype(func->type),
2844 write_typeformatstring_var(
2845 file, 2, NULL,
2846 type_function_get_rettype(func->type),
2847 &v, TRUE, typeformat_offset),
2848 file);
2851 current_func = func;
2852 if (type_get_function_args(func->type))
2853 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2854 update_tfsoff(
2855 var->type,
2856 write_typeformatstring_var(
2857 file, 2, func, var->type, var,
2858 TRUE, typeformat_offset),
2859 file);
2863 return *typeformat_offset + 1;
2866 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2868 unsigned int typeformat_offset = 2;
2870 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2874 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2876 int indent = 0;
2878 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2879 print_file(file, indent, "{\n");
2880 indent++;
2881 print_file(file, indent, "0,\n");
2882 print_file(file, indent, "{\n");
2883 indent++;
2884 print_file(file, indent, "NdrFcShort(0x0),\n");
2886 set_all_tfswrite(TRUE);
2887 process_tfs(file, stmts, pred);
2889 print_file(file, indent, "0x0\n");
2890 indent--;
2891 print_file(file, indent, "}\n");
2892 indent--;
2893 print_file(file, indent, "};\n");
2894 print_file(file, indent, "\n");
2897 static unsigned int get_required_buffer_size_type(
2898 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2900 *alignment = 0;
2901 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2903 case TGT_USER_TYPE:
2905 const char *uname;
2906 const type_t *utype = get_user_type(type, &uname);
2907 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
2909 case TGT_BASIC:
2910 switch (get_basic_fc(type))
2912 case RPC_FC_BYTE:
2913 case RPC_FC_CHAR:
2914 case RPC_FC_USMALL:
2915 case RPC_FC_SMALL:
2916 *alignment = 4;
2917 return 1;
2919 case RPC_FC_WCHAR:
2920 case RPC_FC_USHORT:
2921 case RPC_FC_SHORT:
2922 *alignment = 4;
2923 return 2;
2925 case RPC_FC_ULONG:
2926 case RPC_FC_LONG:
2927 case RPC_FC_FLOAT:
2928 case RPC_FC_ERROR_STATUS_T:
2929 *alignment = 4;
2930 return 4;
2932 case RPC_FC_HYPER:
2933 case RPC_FC_DOUBLE:
2934 *alignment = 8;
2935 return 8;
2937 case RPC_FC_IGNORE:
2938 case RPC_FC_BIND_PRIMITIVE:
2939 return 0;
2941 default:
2942 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2943 get_basic_fc(type));
2944 return 0;
2946 break;
2948 case TGT_ENUM:
2949 switch (get_enum_fc(type))
2951 case RPC_FC_ENUM32:
2952 *alignment = 4;
2953 return 4;
2954 case RPC_FC_ENUM16:
2955 *alignment = 4;
2956 return 2;
2958 break;
2960 case TGT_STRUCT:
2961 if (get_struct_fc(type) == RPC_FC_STRUCT)
2963 if (!type_struct_get_fields(type)) return 0;
2964 return fields_memsize(type_struct_get_fields(type), alignment);
2966 break;
2968 case TGT_POINTER:
2969 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
2971 const type_t *ref = type_pointer_get_ref(type);
2972 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2974 case TGT_BASIC:
2975 case TGT_ENUM:
2976 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
2977 case TGT_STRUCT:
2978 if (get_struct_fc(ref) == RPC_FC_STRUCT)
2979 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
2980 break;
2981 case TGT_USER_TYPE:
2982 case TGT_CTXT_HANDLE:
2983 case TGT_CTXT_HANDLE_POINTER:
2984 case TGT_STRING:
2985 case TGT_POINTER:
2986 case TGT_ARRAY:
2987 case TGT_IFACE_POINTER:
2988 case TGT_UNION:
2989 case TGT_INVALID:
2990 break;
2993 break;
2995 case TGT_ARRAY:
2996 /* FIXME: depends on pointer type */
2997 return type_array_get_dim(type) *
2998 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3000 default:
3001 break;
3003 return 0;
3006 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3008 int in_attr = is_attr(var->attrs, ATTR_IN);
3009 int out_attr = is_attr(var->attrs, ATTR_OUT);
3011 if (!in_attr && !out_attr)
3012 in_attr = 1;
3014 *alignment = 0;
3016 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3017 pass == PASS_RETURN)
3019 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3021 *alignment = 4;
3022 return 20;
3025 if (!is_string_type(var->attrs, var->type))
3026 return get_required_buffer_size_type(var->type, var->name,
3027 var->attrs, TRUE, alignment);
3029 return 0;
3032 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3034 const var_t *var;
3035 unsigned int total_size = 0, alignment;
3037 if (type_get_function_args(func->type))
3039 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3041 total_size += get_required_buffer_size(var, &alignment, pass);
3042 total_size += alignment;
3046 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3048 var_t v = *func;
3049 v.type = type_function_get_rettype(func->type);
3050 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3051 total_size += alignment;
3053 return total_size;
3056 static void print_phase_function(FILE *file, int indent, const char *type,
3057 const char *local_var_prefix, enum remoting_phase phase,
3058 const var_t *var, unsigned int type_offset)
3060 const char *function;
3061 switch (phase)
3063 case PHASE_BUFFERSIZE:
3064 function = "BufferSize";
3065 break;
3066 case PHASE_MARSHAL:
3067 function = "Marshall";
3068 break;
3069 case PHASE_UNMARSHAL:
3070 function = "Unmarshall";
3071 break;
3072 case PHASE_FREE:
3073 function = "Free";
3074 break;
3075 default:
3076 assert(0);
3077 return;
3080 print_file(file, indent, "Ndr%s%s(\n", type, function);
3081 indent++;
3082 print_file(file, indent, "&__frame->_StubMsg,\n");
3083 print_file(file, indent, "%s%s%s%s%s,\n",
3084 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3085 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3086 local_var_prefix,
3087 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3088 var->name);
3089 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3090 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3091 if (phase == PHASE_UNMARSHAL)
3092 print_file(file, indent, "0);\n");
3093 indent--;
3096 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3097 enum remoting_phase phase, enum pass pass, const var_t *var,
3098 const char *varname)
3100 type_t *type = var->type;
3101 unsigned int size;
3102 unsigned int alignment = 0;
3103 const type_t *ref;
3105 /* no work to do for other phases, buffer sizing is done elsewhere */
3106 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3107 return;
3109 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3110 if (type_get_type(ref) == TYPE_ENUM)
3112 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3114 size = 4;
3115 alignment = 4;
3117 else /* RPC_FC_ENUM16 */
3119 size = 2;
3120 alignment = 2;
3123 else
3125 switch (get_basic_fc(ref))
3127 case RPC_FC_BYTE:
3128 case RPC_FC_CHAR:
3129 case RPC_FC_SMALL:
3130 case RPC_FC_USMALL:
3131 size = 1;
3132 alignment = 1;
3133 break;
3135 case RPC_FC_WCHAR:
3136 case RPC_FC_USHORT:
3137 case RPC_FC_SHORT:
3138 size = 2;
3139 alignment = 2;
3140 break;
3142 case RPC_FC_ULONG:
3143 case RPC_FC_LONG:
3144 case RPC_FC_FLOAT:
3145 case RPC_FC_ERROR_STATUS_T:
3146 size = 4;
3147 alignment = 4;
3148 break;
3150 case RPC_FC_HYPER:
3151 case RPC_FC_DOUBLE:
3152 size = 8;
3153 alignment = 8;
3154 break;
3156 case RPC_FC_IGNORE:
3157 case RPC_FC_BIND_PRIMITIVE:
3158 /* no marshalling needed */
3159 return;
3161 default:
3162 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3163 var->name, get_basic_fc(ref));
3164 size = 0;
3168 if (phase == PHASE_MARSHAL)
3169 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3170 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3171 alignment - 1, alignment - 1);
3173 if (phase == PHASE_MARSHAL)
3175 print_file(file, indent, "*(");
3176 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3177 if (is_ptr(type))
3178 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3179 else
3180 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3181 fprintf(file, "%s%s", local_var_prefix, varname);
3182 fprintf(file, ";\n");
3184 else if (phase == PHASE_UNMARSHAL)
3186 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3187 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3188 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3189 print_file(file, indent, "{\n");
3190 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3191 print_file(file, indent, "}\n");
3192 print_file(file, indent, "%s%s%s",
3193 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3194 local_var_prefix, varname);
3195 if (pass == PASS_IN && is_ptr(type))
3196 fprintf(file, " = (");
3197 else
3198 fprintf(file, " = *(");
3199 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3200 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3203 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3204 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3205 fprintf(file, ");\n");
3208 /* returns whether the MaxCount, Offset or ActualCount members need to be
3209 * filled in for the specified phase */
3210 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3212 return (phase != PHASE_UNMARSHAL);
3215 expr_t *get_size_is_expr(const type_t *t, const char *name)
3217 expr_t *x = NULL;
3219 for ( ; is_array(t); t = type_array_get_element(t))
3220 if (type_array_has_conformance(t))
3222 if (!x)
3223 x = type_array_get_conformance(t);
3224 else
3225 error("%s: multidimensional conformant"
3226 " arrays not supported at the top level\n",
3227 name);
3230 return x;
3233 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3234 enum remoting_phase phase, const var_t *var)
3236 const type_t *type = var->type;
3237 /* get fundamental type for the argument */
3238 for (;;)
3240 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3241 break;
3242 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3243 break;
3244 else if (type_is_alias(type))
3245 type = type_alias_get_aliasee(type);
3246 else if (is_array(type) || is_string_type(var->attrs, type))
3248 if (is_conformance_needed_for_phase(phase) && is_array(type))
3250 if (type_array_has_conformance(type))
3252 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3253 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3254 fprintf(file, ";\n\n");
3256 if (type_array_has_variance(type))
3258 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3259 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3260 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3261 fprintf(file, ";\n\n");
3264 break;
3266 else if (type_get_type(type) == TYPE_UNION)
3268 if (is_conformance_needed_for_phase(phase))
3270 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3271 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3272 fprintf(file, ";\n\n");
3274 break;
3276 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3278 expr_t *iid;
3280 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3282 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3283 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3284 fprintf( file, ";\n\n" );
3286 break;
3288 else if (is_ptr(type))
3289 type = type_pointer_get_ref(type);
3290 else
3291 break;
3295 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3296 enum pass pass, enum remoting_phase phase, const var_t *var)
3298 int in_attr, out_attr, pointer_type;
3299 const type_t *type = var->type;
3300 unsigned int start_offset = type->typestring_offset;
3302 if (is_ptr(type) || is_array(type))
3303 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3304 else
3305 pointer_type = 0;
3307 in_attr = is_attr(var->attrs, ATTR_IN);
3308 out_attr = is_attr(var->attrs, ATTR_OUT);
3309 if (!in_attr && !out_attr)
3310 in_attr = 1;
3312 if (phase != PHASE_FREE)
3313 switch (pass)
3315 case PASS_IN:
3316 if (!in_attr) return;
3317 break;
3318 case PASS_OUT:
3319 if (!out_attr) return;
3320 break;
3321 case PASS_RETURN:
3322 break;
3325 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3327 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3329 case TGT_CTXT_HANDLE:
3330 case TGT_CTXT_HANDLE_POINTER:
3331 if (phase == PHASE_MARSHAL)
3333 if (pass == PASS_IN)
3335 /* if the context_handle attribute appears in the chain of types
3336 * without pointers being followed, then the context handle must
3337 * be direct, otherwise it is a pointer */
3338 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3339 print_file(file, indent, "NdrClientContextMarshall(\n");
3340 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3341 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3342 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3344 else
3346 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3347 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3348 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3349 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3350 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3353 else if (phase == PHASE_UNMARSHAL)
3355 if (pass == PASS_OUT)
3357 if (!in_attr)
3358 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3359 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3360 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3361 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3362 print_file(file, indent + 1, "__frame->_Handle);\n");
3364 else
3366 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3367 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3368 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3371 break;
3372 case TGT_USER_TYPE:
3373 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3374 break;
3375 case TGT_STRING:
3376 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3377 pointer_type != RPC_FC_RP)
3379 /* strings returned are assumed to be global and hence don't
3380 * need freeing */
3381 if (phase != PHASE_FREE || pass != PASS_RETURN)
3382 print_phase_function(file, indent, "Pointer", local_var_prefix,
3383 phase, var, start_offset);
3385 else
3387 unsigned int real_start_offset = start_offset;
3388 /* skip over pointer description straight to string description */
3389 if (is_declptr(type))
3391 if (is_conformant_array(type))
3392 real_start_offset += 4;
3393 else
3394 real_start_offset += 2;
3396 if (is_array(type) && !is_conformant_array(type))
3397 print_phase_function(file, indent, "NonConformantString",
3398 local_var_prefix, phase, var,
3399 real_start_offset);
3400 else
3401 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3402 phase, var, real_start_offset);
3404 break;
3405 case TGT_ARRAY:
3407 unsigned char tc = get_array_fc(type);
3408 const char *array_type = "FixedArray";
3410 /* We already have the size_is expression since it's at the
3411 top level, but do checks for multidimensional conformant
3412 arrays. When we handle them, we'll need to extend this
3413 function to return a list, and then we'll actually use
3414 the return value. */
3415 get_size_is_expr(type, var->name);
3417 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3419 array_type = "VaryingArray";
3421 else if (tc == RPC_FC_CARRAY)
3423 array_type = "ConformantArray";
3425 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3427 array_type = (tc == RPC_FC_BOGUS_ARRAY
3428 ? "ComplexArray"
3429 : "ConformantVaryingArray");
3432 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3433 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3434 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3436 /* these are all unmarshalled by allocating memory */
3437 if (tc == RPC_FC_BOGUS_ARRAY ||
3438 tc == RPC_FC_CVARRAY ||
3439 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3440 (tc == RPC_FC_CARRAY && !in_attr))
3442 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3443 indent++;
3444 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3447 break;
3449 case TGT_BASIC:
3450 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3451 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3452 break;
3453 case TGT_ENUM:
3454 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3456 if (phase == PHASE_MARSHAL)
3457 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3458 else
3459 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3460 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3461 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3462 local_var_prefix,
3463 var->name);
3464 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3466 break;
3467 case TGT_STRUCT:
3468 switch (get_struct_fc(type))
3470 case RPC_FC_STRUCT:
3471 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3472 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3473 break;
3474 case RPC_FC_PSTRUCT:
3475 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3476 break;
3477 case RPC_FC_CSTRUCT:
3478 case RPC_FC_CPSTRUCT:
3479 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3480 break;
3481 case RPC_FC_CVSTRUCT:
3482 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3483 break;
3484 case RPC_FC_BOGUS_STRUCT:
3485 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3486 break;
3487 default:
3488 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3490 break;
3491 case TGT_UNION:
3493 const char *union_type = NULL;
3495 if (type_get_type(type) == TYPE_UNION)
3496 union_type = "NonEncapsulatedUnion";
3497 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3498 union_type = "EncapsulatedUnion";
3500 print_phase_function(file, indent, union_type, local_var_prefix,
3501 phase, var, start_offset);
3502 break;
3504 case TGT_POINTER:
3506 const type_t *ref = type_pointer_get_ref(type);
3507 if (pointer_type == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3509 case TYPE_BASIC:
3510 /* base types have known sizes, so don't need a sizing pass
3511 * and don't have any memory to free and so don't need a
3512 * freeing pass */
3513 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3514 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3515 break;
3516 case TYPE_ENUM:
3517 /* base types have known sizes, so don't need a sizing pass
3518 * and don't have any memory to free and so don't need a
3519 * freeing pass */
3520 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3521 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3522 break;
3523 case TYPE_STRUCT:
3525 const char *struct_type = NULL;
3526 switch (get_struct_fc(ref))
3528 case RPC_FC_STRUCT:
3529 /* simple structs have known sizes, so don't need a sizing
3530 * pass and don't have any memory to free and so don't
3531 * need a freeing pass */
3532 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3533 struct_type = "SimpleStruct";
3534 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3536 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3537 indent++;
3538 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3539 indent--;
3541 break;
3542 case RPC_FC_PSTRUCT:
3543 struct_type = "SimpleStruct";
3544 break;
3545 case RPC_FC_CSTRUCT:
3546 case RPC_FC_CPSTRUCT:
3547 struct_type = "ConformantStruct";
3548 break;
3549 case RPC_FC_CVSTRUCT:
3550 struct_type = "ConformantVaryingStruct";
3551 break;
3552 case RPC_FC_BOGUS_STRUCT:
3553 struct_type = "ComplexStruct";
3554 break;
3555 default:
3556 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3559 if (struct_type)
3561 if (phase == PHASE_FREE)
3562 struct_type = "Pointer";
3563 else
3564 start_offset = ref->typestring_offset;
3565 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3567 break;
3569 case TYPE_UNION:
3570 case TYPE_ENCAPSULATED_UNION:
3572 const char *union_type = NULL;
3573 if (phase == PHASE_FREE)
3574 union_type = "Pointer";
3575 else
3577 if (type_get_type(ref) == TYPE_UNION)
3578 union_type = "NonEncapsulatedUnion";
3579 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3580 union_type = "EncapsulatedUnion";
3582 start_offset = ref->typestring_offset;
3585 print_phase_function(file, indent, union_type, local_var_prefix,
3586 phase, var, start_offset);
3587 break;
3589 case TYPE_POINTER:
3590 case TYPE_ARRAY:
3591 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3592 break;
3593 case TYPE_VOID:
3594 case TYPE_ALIAS:
3595 case TYPE_MODULE:
3596 case TYPE_COCLASS:
3597 case TYPE_FUNCTION:
3598 case TYPE_INTERFACE:
3599 assert(0);
3600 break;
3602 else
3603 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3604 break;
3606 case TGT_IFACE_POINTER:
3607 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3608 break;
3609 case TGT_INVALID:
3610 assert(0);
3611 break;
3613 fprintf(file, "\n");
3616 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3617 enum pass pass, enum remoting_phase phase)
3619 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3621 unsigned int size = get_function_buffer_size( func, pass );
3622 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3625 if (pass == PASS_RETURN)
3627 var_t var;
3628 var = *func;
3629 var.type = type_function_get_rettype(func->type);
3630 var.name = xstrdup( "_RetVal" );
3631 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3632 free( var.name );
3634 else
3636 const var_t *var;
3637 if (!type_get_function_args(func->type))
3638 return;
3639 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3640 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3645 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3647 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3651 unsigned int get_size_procformatstring_func(const var_t *func)
3653 const var_t *var;
3654 unsigned int size = 0;
3656 /* argument list size */
3657 if (type_get_function_args(func->type))
3658 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3659 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3661 /* return value size */
3662 if (is_void(type_function_get_rettype(func->type)))
3663 size += 2; /* FC_END and FC_PAD */
3664 else
3665 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3667 return size;
3670 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3672 const statement_t *stmt;
3673 unsigned int size = 1;
3675 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3677 const type_t *iface;
3678 const statement_t *stmt_func;
3680 if (stmt->type == STMT_LIBRARY)
3682 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3683 continue;
3685 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3686 continue;
3688 iface = stmt->u.type;
3689 if (!pred(iface))
3690 continue;
3692 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3694 const var_t *func = stmt_func->u.var;
3695 if (!is_local(func->attrs))
3696 size += get_size_procformatstring_func( func );
3699 return size;
3702 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3704 set_all_tfswrite(FALSE);
3705 return process_tfs(NULL, stmts, pred);
3708 void declare_stub_args( FILE *file, int indent, const var_t *func )
3710 int in_attr, out_attr;
3711 int i = 0;
3712 const var_t *var;
3714 /* declare return value '_RetVal' */
3715 if (!is_void(type_function_get_rettype(func->type)))
3717 print_file(file, indent, "%s", "");
3718 write_type_decl_left(file, type_function_get_rettype(func->type));
3719 fprintf(file, " _RetVal;\n");
3722 if (!type_get_function_args(func->type))
3723 return;
3725 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3727 int is_string = is_string_type(var->attrs, var->type);
3729 in_attr = is_attr(var->attrs, ATTR_IN);
3730 out_attr = is_attr(var->attrs, ATTR_OUT);
3731 if (!out_attr && !in_attr)
3732 in_attr = 1;
3734 if (is_context_handle(var->type))
3735 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3736 else
3738 if (!in_attr && !is_conformant_array(var->type) && !is_string)
3740 type_t *type_to_print;
3741 char name[16];
3742 print_file(file, indent, "%s", "");
3743 if (type_get_type(var->type) == TYPE_ARRAY &&
3744 !type_array_is_decl_as_ptr(var->type))
3745 type_to_print = var->type;
3746 else
3747 type_to_print = type_pointer_get_ref(var->type);
3748 sprintf(name, "_W%u", i++);
3749 write_type_decl(file, type_to_print, name);
3750 fprintf(file, ";\n");
3753 print_file(file, indent, "%s", "");
3754 write_type_decl_left(file, var->type);
3755 fprintf(file, " ");
3756 if (type_get_type(var->type) == TYPE_ARRAY &&
3757 !type_array_is_decl_as_ptr(var->type)) {
3758 fprintf(file, "(*%s)", var->name);
3759 } else
3760 fprintf(file, "%s", var->name);
3761 write_type_right(file, var->type, FALSE);
3762 fprintf(file, ";\n");
3764 if (decl_indirect(var->type))
3765 print_file(file, indent, "void *_p_%s;\n", var->name);
3771 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3773 int in_attr, out_attr;
3774 int i = 0, sep = 0;
3775 const var_t *var;
3777 if (!type_get_function_args(func->type))
3778 return;
3780 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3782 int is_string = is_string_type(var->attrs, var->type);
3783 in_attr = is_attr(var->attrs, ATTR_IN);
3784 out_attr = is_attr(var->attrs, ATTR_OUT);
3785 if (!out_attr && !in_attr)
3786 in_attr = 1;
3788 if (!in_attr)
3790 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3792 if (is_context_handle(var->type))
3794 fprintf(file, " = NdrContextHandleInitialize(\n");
3795 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3796 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3797 var->type->typestring_offset);
3799 else if (is_array(var->type) &&
3800 type_array_has_conformance(var->type))
3802 unsigned int size, align = 0;
3803 type_t *type = var->type;
3805 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3806 for ( ;
3807 is_array(type) && type_array_has_conformance(type);
3808 type = type_array_get_element(type))
3810 write_expr(file, type_array_get_conformance(type), TRUE,
3811 TRUE, NULL, NULL, local_var_prefix);
3812 fprintf(file, " * ");
3814 size = type_memsize(type, &align);
3815 fprintf(file, "%u);\n", size);
3817 else if (!is_string)
3819 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3820 if (is_ptr(var->type) && !last_ptr(var->type))
3821 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3822 i++;
3825 sep = 1;
3828 if (sep)
3829 fprintf(file, "\n");
3833 int write_expr_eval_routines(FILE *file, const char *iface)
3835 static const char *var_name = "pS";
3836 static const char *var_name_expr = "pS->";
3837 int result = 0;
3838 struct expr_eval_routine *eval;
3839 unsigned short callback_offset = 0;
3841 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3843 const char *name = eval->structure->name;
3844 result = 1;
3846 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3847 iface, name, callback_offset);
3848 print_file(file, 0, "{\n");
3849 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3850 name, var_name, name, eval->baseoff);
3851 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3852 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3853 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3854 fprintf(file, ";\n");
3855 print_file(file, 0, "}\n\n");
3856 callback_offset++;
3858 return result;
3861 void write_expr_eval_routine_list(FILE *file, const char *iface)
3863 struct expr_eval_routine *eval;
3864 struct expr_eval_routine *cursor;
3865 unsigned short callback_offset = 0;
3867 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3868 fprintf(file, "{\n");
3870 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3872 const char *name = eval->structure->name;
3873 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3874 callback_offset++;
3875 list_remove(&eval->entry);
3876 free(eval);
3879 fprintf(file, "};\n\n");
3882 void write_user_quad_list(FILE *file)
3884 user_type_t *ut;
3886 if (list_empty(&user_type_list))
3887 return;
3889 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3890 fprintf(file, "{\n");
3891 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3893 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3894 print_file(file, 1, "{\n");
3895 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3896 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3897 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3898 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3899 print_file(file, 1, "}%s\n", sep);
3901 fprintf(file, "};\n\n");
3904 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3906 const struct str_list_entry_t *endpoint;
3907 const char *p;
3909 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3910 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3911 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
3913 print_file( f, 1, "{ (const unsigned char *)\"" );
3914 for (p = endpoint->str; *p && *p != ':'; p++)
3916 if (*p == '"' || *p == '\\') fputc( '\\', f );
3917 fputc( *p, f );
3919 if (!*p) goto error;
3920 if (p[1] != '[') goto error;
3922 fprintf( f, "\", (const unsigned char *)\"" );
3923 for (p += 2; *p && *p != ']'; p++)
3925 if (*p == '"' || *p == '\\') fputc( '\\', f );
3926 fputc( *p, f );
3928 if (*p != ']') goto error;
3929 fprintf( f, "\" },\n" );
3931 print_file( f, 0, "};\n\n" );
3932 return;
3934 error:
3935 error("Invalid endpoint syntax '%s'\n", endpoint->str);
3938 void write_exceptions( FILE *file )
3940 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3941 fprintf( file, "\n");
3942 fprintf( file, "#include \"wine/exception.h\"\n");
3943 fprintf( file, "#undef RpcTryExcept\n");
3944 fprintf( file, "#undef RpcExcept\n");
3945 fprintf( file, "#undef RpcEndExcept\n");
3946 fprintf( file, "#undef RpcTryFinally\n");
3947 fprintf( file, "#undef RpcFinally\n");
3948 fprintf( file, "#undef RpcEndFinally\n");
3949 fprintf( file, "#undef RpcExceptionCode\n");
3950 fprintf( file, "#undef RpcAbnormalTermination\n");
3951 fprintf( file, "\n");
3952 fprintf( file, "struct __exception_frame;\n");
3953 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3954 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3955 fprintf( file, "\n");
3956 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
3957 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3958 fprintf( file, " __filter_func filter; \\\n");
3959 fprintf( file, " __finally_func finally; \\\n");
3960 fprintf( file, " sigjmp_buf jmp; \\\n");
3961 fprintf( file, " DWORD code; \\\n");
3962 fprintf( file, " unsigned char abnormal_termination; \\\n");
3963 fprintf( file, " unsigned char filter_level; \\\n");
3964 fprintf( file, " unsigned char finally_level;\n");
3965 fprintf( file, "\n");
3966 fprintf( file, "struct __exception_frame\n{\n");
3967 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
3968 fprintf( file, "};\n");
3969 fprintf( file, "\n");
3970 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3971 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3972 fprintf( file, " CONTEXT *context,\n");
3973 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3974 fprintf( file, "{\n");
3975 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3976 fprintf( file, "\n");
3977 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3978 fprintf( file, " {\n" );
3979 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3980 fprintf( file, " {\n" );
3981 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3982 fprintf( file, " exc_frame->finally( exc_frame );\n");
3983 fprintf( file, " }\n" );
3984 fprintf( file, " return ExceptionContinueSearch;\n");
3985 fprintf( file, " }\n" );
3986 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
3987 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3988 fprintf( file, " {\n");
3989 fprintf( file, " __wine_rtl_unwind( frame, record );\n");
3990 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3991 fprintf( file, " {\n");
3992 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
3993 fprintf( file, " exc_frame->finally( exc_frame );\n");
3994 fprintf( file, " __wine_pop_frame( frame );\n");
3995 fprintf( file, " }\n");
3996 fprintf( file, " exc_frame->filter_level = 0;\n");
3997 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
3998 fprintf( file, " }\n");
3999 fprintf( file, " return ExceptionContinueSearch;\n");
4000 fprintf( file, "}\n");
4001 fprintf( file, "\n");
4002 fprintf( file, "#define RpcTryExcept \\\n");
4003 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4004 fprintf( file, " { \\\n");
4005 fprintf( file, " if (!__frame->finally_level) \\\n" );
4006 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4007 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4008 fprintf( file, "\n");
4009 fprintf( file, "#define RpcExcept(expr) \\\n");
4010 fprintf( file, " if (!__frame->finally_level) \\\n" );
4011 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4012 fprintf( file, " __frame->filter_level = 0; \\\n" );
4013 fprintf( file, " } \\\n");
4014 fprintf( file, " else \\\n");
4015 fprintf( file, "\n");
4016 fprintf( file, "#define RpcEndExcept\n");
4017 fprintf( file, "\n");
4018 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4019 fprintf( file, "\n");
4020 fprintf( file, "#define RpcTryFinally \\\n");
4021 fprintf( file, " if (!__frame->filter_level) \\\n");
4022 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4023 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4024 fprintf( file, "\n");
4025 fprintf( file, "#define RpcFinally \\\n");
4026 fprintf( file, " if (!__frame->filter_level) \\\n");
4027 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4028 fprintf( file, " __frame->finally_level = 0;\n");
4029 fprintf( file, "\n");
4030 fprintf( file, "#define RpcEndFinally\n");
4031 fprintf( file, "\n");
4032 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4033 fprintf( file, "\n");
4034 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4035 fprintf( file, " do { \\\n");
4036 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4037 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4038 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4039 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4040 fprintf( file, " __frame->filter_level = 0; \\\n");
4041 fprintf( file, " __frame->finally_level = 0; \\\n");
4042 fprintf( file, " } while (0)\n");
4043 fprintf( file, "\n");
4044 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4045 fprintf( file, "\n");
4046 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4047 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4048 fprintf( file, "\n");
4049 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4050 fprintf( file, " DWORD code;\n");
4051 fprintf( file, "\n");
4052 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");