push 8b07bf1f08b23b9893a622b47d2be359556765b1
[wine/hacks.git] / tools / widl / typegen.c
blobbcf2bf5bfc4d856b95981a3b6ae9c9f1bec154f4
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 field_memsize(const type_t *type, unsigned int *offset);
63 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
64 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
65 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
66 const char *name, int write_ptr, unsigned int *tfsoff);
67 static const var_t *find_array_or_string_in_struct(const type_t *type);
68 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
69 type_t *type, int toplevel_param,
70 const char *name, unsigned int *typestring_offset);
72 const char *string_of_type(unsigned char type)
74 switch (type)
76 case RPC_FC_BYTE: return "FC_BYTE";
77 case RPC_FC_CHAR: return "FC_CHAR";
78 case RPC_FC_SMALL: return "FC_SMALL";
79 case RPC_FC_USMALL: return "FC_USMALL";
80 case RPC_FC_WCHAR: return "FC_WCHAR";
81 case RPC_FC_SHORT: return "FC_SHORT";
82 case RPC_FC_USHORT: return "FC_USHORT";
83 case RPC_FC_LONG: return "FC_LONG";
84 case RPC_FC_ULONG: return "FC_ULONG";
85 case RPC_FC_FLOAT: return "FC_FLOAT";
86 case RPC_FC_HYPER: return "FC_HYPER";
87 case RPC_FC_DOUBLE: return "FC_DOUBLE";
88 case RPC_FC_ENUM16: return "FC_ENUM16";
89 case RPC_FC_ENUM32: return "FC_ENUM32";
90 case RPC_FC_IGNORE: return "FC_IGNORE";
91 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
92 case RPC_FC_RP: return "FC_RP";
93 case RPC_FC_UP: return "FC_UP";
94 case RPC_FC_OP: return "FC_OP";
95 case RPC_FC_FP: return "FC_FP";
96 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
97 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
98 case RPC_FC_STRUCT: return "FC_STRUCT";
99 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
100 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
101 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
102 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
103 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
104 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
105 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
106 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
107 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
108 case RPC_FC_CARRAY: return "FC_CARRAY";
109 case RPC_FC_CVARRAY: return "FC_CVARRAY";
110 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
111 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
112 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
113 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
114 case RPC_FC_POINTER: return "FC_POINTER";
115 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
116 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
117 case RPC_FC_CSTRING: return "FC_CSTRING";
118 case RPC_FC_WSTRING: return "FC_WSTRING";
119 default:
120 error("string_of_type: unknown type 0x%02x\n", type);
121 return NULL;
125 unsigned char get_basic_fc(const type_t *type)
127 int sign = type_basic_get_sign(type);
128 switch (type_basic_get_type(type))
130 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
131 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
132 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
133 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
134 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
135 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
136 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
137 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
138 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
139 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
140 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
141 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
142 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
143 default: return 0;
147 static inline unsigned int clamp_align(unsigned int align)
149 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
150 if(align > packing) align = packing;
151 return align;
154 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
156 const type_t *t;
157 int pointer_type;
159 assert(is_ptr(type) || is_array(type));
161 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
162 if (pointer_type)
163 return pointer_type;
165 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
167 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
168 if (pointer_type)
169 return pointer_type;
172 if (toplevel_param)
173 return RPC_FC_RP;
174 else if (is_ptr(type))
175 return type_pointer_get_default_fc(type);
176 else
177 return type_array_get_ptr_default_fc(type);
180 static unsigned char get_enum_fc(const type_t *type)
182 assert(type_get_type(type) == TYPE_ENUM);
183 if (is_aliaschain_attr(type, ATTR_V1ENUM))
184 return RPC_FC_ENUM32;
185 else
186 return RPC_FC_ENUM16;
189 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
191 if (is_user_type(type))
192 return TGT_USER_TYPE;
194 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
195 return TGT_CTXT_HANDLE;
197 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
198 return TGT_STRING;
200 switch (type_get_type(type))
202 case TYPE_BASIC:
203 return TGT_BASIC;
204 case TYPE_ENUM:
205 return TGT_ENUM;
206 case TYPE_POINTER:
207 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
208 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
209 return TGT_IFACE_POINTER;
210 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
211 return TGT_CTXT_HANDLE_POINTER;
212 else
213 return TGT_POINTER;
214 case TYPE_STRUCT:
215 return TGT_STRUCT;
216 case TYPE_ENCAPSULATED_UNION:
217 case TYPE_UNION:
218 return TGT_UNION;
219 case TYPE_ARRAY:
220 return TGT_ARRAY;
221 case TYPE_FUNCTION:
222 case TYPE_COCLASS:
223 case TYPE_INTERFACE:
224 case TYPE_MODULE:
225 case TYPE_VOID:
226 case TYPE_ALIAS:
227 break;
229 return TGT_INVALID;
232 static type_t *get_user_type(const type_t *t, const char **pname);
234 static int type_contains_iface(const type_t *type)
236 enum typegen_type typegen_type;
237 var_list_t *fields;
238 const var_t *field;
240 typegen_type = typegen_detect_type(type, type->attrs, TDT_IGNORE_STRINGS);
242 switch(typegen_type)
244 case TGT_USER_TYPE:
245 return type_contains_iface(get_user_type(type, NULL));
247 case TGT_BASIC:
248 case TGT_ENUM:
249 return FALSE;
251 case TGT_POINTER:
252 return type_contains_iface(type_pointer_get_ref(type));
254 case TGT_ARRAY:
255 return type_contains_iface(type_array_get_element(type));
257 case TGT_IFACE_POINTER:
258 return TRUE;
260 case TGT_STRUCT:
261 fields = type_struct_get_fields(type);
262 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
264 if(type_contains_iface(field->type))
265 return TRUE;
267 return FALSE;
269 case TGT_UNION:
270 fields = type_union_get_cases(type);
271 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
273 if(field->type && type_contains_iface(field->type))
274 return TRUE;
276 return FALSE;
278 case TGT_STRING:
279 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
280 case TGT_INVALID:
281 case TGT_CTXT_HANDLE:
282 case TGT_CTXT_HANDLE_POINTER:
283 /* checking after parsing should mean that we don't get here. if we do,
284 * it's a checker bug */
285 assert(0);
287 return FALSE;
290 unsigned char get_struct_fc(const type_t *type)
292 int has_pointer = 0;
293 int has_conformance = 0;
294 int has_variance = 0;
295 var_t *field;
296 var_list_t *fields;
298 fields = type_struct_get_fields(type);
300 if (get_padding(fields))
301 return RPC_FC_BOGUS_STRUCT;
303 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
305 type_t *t = field->type;
306 enum typegen_type typegen_type;
308 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
310 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
312 if (is_string_type(field->attrs, field->type))
314 if (is_conformant_array(t))
315 has_conformance = 1;
316 has_variance = 1;
317 continue;
320 if (is_array(type_array_get_element(field->type)))
321 return RPC_FC_BOGUS_STRUCT;
323 if (type_array_has_conformance(field->type))
325 has_conformance = 1;
326 if (list_next(fields, &field->entry))
327 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
328 field->name);
330 if (type_array_has_variance(t))
331 has_variance = 1;
333 t = type_array_get_element(t);
334 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
337 switch (typegen_type)
339 case TGT_USER_TYPE:
340 case TGT_IFACE_POINTER:
341 return RPC_FC_BOGUS_STRUCT;
342 case TGT_BASIC:
343 break;
344 case TGT_ENUM:
345 if (get_enum_fc(t) == RPC_FC_ENUM16)
346 return RPC_FC_BOGUS_STRUCT;
347 break;
348 case TGT_ARRAY:
349 if(type_contains_iface(type_array_get_element(t)))
350 return RPC_FC_BOGUS_STRUCT;
351 case TGT_POINTER:
352 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
353 return RPC_FC_BOGUS_STRUCT;
354 has_pointer = 1;
355 break;
356 case TGT_UNION:
357 return RPC_FC_BOGUS_STRUCT;
358 case TGT_STRUCT:
360 unsigned char fc = get_struct_fc(t);
361 switch (fc)
363 case RPC_FC_STRUCT:
364 break;
365 case RPC_FC_CVSTRUCT:
366 has_conformance = 1;
367 has_variance = 1;
368 has_pointer = 1;
369 break;
371 case RPC_FC_CPSTRUCT:
372 has_conformance = 1;
373 if (list_next( fields, &field->entry ))
374 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
375 field->name);
376 has_pointer = 1;
377 break;
379 case RPC_FC_CSTRUCT:
380 has_conformance = 1;
381 if (list_next( fields, &field->entry ))
382 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
383 field->name);
384 break;
386 case RPC_FC_PSTRUCT:
387 has_pointer = 1;
388 break;
390 default:
391 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
392 /* fallthru - treat it as complex */
394 /* as soon as we see one of these these members, it's bogus... */
395 case RPC_FC_BOGUS_STRUCT:
396 return RPC_FC_BOGUS_STRUCT;
398 break;
400 case TGT_STRING:
401 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
402 case TGT_INVALID:
403 case TGT_CTXT_HANDLE:
404 case TGT_CTXT_HANDLE_POINTER:
405 /* checking after parsing should mean that we don't get here. if we do,
406 * it's a checker bug */
407 assert(0);
411 if( has_variance )
413 if ( has_conformance )
414 return RPC_FC_CVSTRUCT;
415 else
416 return RPC_FC_BOGUS_STRUCT;
418 if( has_conformance && has_pointer )
419 return RPC_FC_CPSTRUCT;
420 if( has_conformance )
421 return RPC_FC_CSTRUCT;
422 if( has_pointer )
423 return RPC_FC_PSTRUCT;
424 return RPC_FC_STRUCT;
427 unsigned char get_array_fc(const type_t *type)
429 unsigned char fc;
430 const expr_t *size_is;
431 const type_t *elem_type;
433 elem_type = type_array_get_element(type);
434 size_is = type_array_get_conformance(type);
436 if (!size_is)
438 unsigned int align = 0;
439 unsigned int size = type_memsize(elem_type, &align);
440 if (size * type_array_get_dim(type) > 0xffffuL)
441 fc = RPC_FC_LGFARRAY;
442 else
443 fc = RPC_FC_SMFARRAY;
445 else
446 fc = RPC_FC_CARRAY;
448 if (type_array_has_variance(type))
450 if (fc == RPC_FC_SMFARRAY)
451 fc = RPC_FC_SMVARRAY;
452 else if (fc == RPC_FC_LGFARRAY)
453 fc = RPC_FC_LGVARRAY;
454 else if (fc == RPC_FC_CARRAY)
455 fc = RPC_FC_CVARRAY;
458 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
460 case TGT_USER_TYPE:
461 fc = RPC_FC_BOGUS_ARRAY;
462 break;
463 case TGT_STRUCT:
464 switch (get_struct_fc(elem_type))
466 case RPC_FC_BOGUS_STRUCT:
467 fc = RPC_FC_BOGUS_ARRAY;
468 break;
470 break;
471 case TGT_ENUM:
472 /* is 16-bit enum - if so, wire size differs from mem size and so
473 * the array cannot be block copied, which means the array is complex */
474 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
475 fc = RPC_FC_BOGUS_ARRAY;
476 break;
477 case TGT_UNION:
478 case TGT_IFACE_POINTER:
479 fc = RPC_FC_BOGUS_ARRAY;
480 break;
481 case TGT_POINTER:
482 /* ref pointers cannot just be block copied. unique pointers to
483 * interfaces need special treatment. either case means the array is
484 * complex */
485 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP)
486 fc = RPC_FC_BOGUS_ARRAY;
487 break;
488 case TGT_BASIC:
489 case TGT_CTXT_HANDLE:
490 case TGT_CTXT_HANDLE_POINTER:
491 case TGT_STRING:
492 case TGT_INVALID:
493 case TGT_ARRAY:
494 /* nothing to do for everything else */
495 break;
498 return fc;
501 int is_struct(unsigned char type)
503 switch (type)
505 case RPC_FC_STRUCT:
506 case RPC_FC_PSTRUCT:
507 case RPC_FC_CSTRUCT:
508 case RPC_FC_CPSTRUCT:
509 case RPC_FC_CVSTRUCT:
510 case RPC_FC_BOGUS_STRUCT:
511 return 1;
512 default:
513 return 0;
517 static int is_non_complex_struct(const type_t *type)
519 return (type_get_type(type) == TYPE_STRUCT &&
520 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
523 static int type_has_pointers(const type_t *type)
525 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
527 case TGT_USER_TYPE:
528 return FALSE;
529 case TGT_POINTER:
530 return TRUE;
531 case TGT_ARRAY:
532 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
533 case TGT_STRUCT:
535 var_list_t *fields = type_struct_get_fields(type);
536 const var_t *field;
537 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
539 if (type_has_pointers(field->type))
540 return TRUE;
542 break;
544 case TGT_UNION:
546 var_list_t *fields;
547 const var_t *field;
548 fields = type_union_get_cases(type);
549 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
551 if (field->type && type_has_pointers(field->type))
552 return TRUE;
554 break;
556 case TGT_CTXT_HANDLE:
557 case TGT_CTXT_HANDLE_POINTER:
558 case TGT_STRING:
559 case TGT_IFACE_POINTER:
560 case TGT_BASIC:
561 case TGT_ENUM:
562 case TGT_INVALID:
563 break;
566 return FALSE;
569 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
570 int toplevel_param)
572 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
574 case TGT_USER_TYPE:
575 return FALSE;
576 case TGT_POINTER:
577 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
578 return TRUE;
579 else
580 return FALSE;
581 case TGT_ARRAY:
582 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
583 return TRUE;
584 else
585 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
586 case TGT_STRUCT:
588 var_list_t *fields = type_struct_get_fields(type);
589 const var_t *field;
590 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
592 if (type_has_full_pointer(field->type, field->attrs, FALSE))
593 return TRUE;
595 break;
597 case TGT_UNION:
599 var_list_t *fields;
600 const var_t *field;
601 fields = type_union_get_cases(type);
602 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
604 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
605 return TRUE;
607 break;
609 case TGT_CTXT_HANDLE:
610 case TGT_CTXT_HANDLE_POINTER:
611 case TGT_STRING:
612 case TGT_IFACE_POINTER:
613 case TGT_BASIC:
614 case TGT_ENUM:
615 case TGT_INVALID:
616 break;
619 return FALSE;
622 static unsigned short user_type_offset(const char *name)
624 user_type_t *ut;
625 unsigned short off = 0;
626 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
628 if (strcmp(name, ut->name) == 0)
629 return off;
630 ++off;
632 error("user_type_offset: couldn't find type (%s)\n", name);
633 return 0;
636 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
638 type->typestring_offset = offset;
639 if (file) type->tfswrite = FALSE;
642 static void guard_rec(type_t *type)
644 /* types that contain references to themselves (like a linked list),
645 need to be shielded from infinite recursion when writing embedded
646 types */
647 if (type->typestring_offset)
648 type->tfswrite = FALSE;
649 else
650 type->typestring_offset = 1;
653 static type_t *get_user_type(const type_t *t, const char **pname)
655 for (;;)
657 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
658 if (ut)
660 if (pname)
661 *pname = t->name;
662 return ut;
665 if (type_is_alias(t))
666 t = type_alias_get_aliasee(t);
667 else
668 return 0;
672 int is_user_type(const type_t *t)
674 return get_user_type(t, NULL) != NULL;
677 static int is_embedded_complex(const type_t *type)
679 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
681 case TGT_USER_TYPE:
682 case TGT_STRUCT:
683 case TGT_UNION:
684 case TGT_ARRAY:
685 case TGT_IFACE_POINTER:
686 return TRUE;
687 default:
688 return FALSE;
692 static const char *get_context_handle_type_name(const type_t *type)
694 const type_t *t;
695 for (t = type;
696 is_ptr(t) || type_is_alias(t);
697 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
698 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
699 return t->name;
700 assert(0);
701 return NULL;
704 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
705 do { \
706 if (file) \
707 fprintf(file, "/* %2u */\n", typestring_offset); \
708 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
710 while (0)
712 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
713 static void print_file(FILE *file, int indent, const char *format, ...)
715 va_list va;
716 va_start(va, format);
717 print(file, indent, format, va);
718 va_end(va);
721 void print(FILE *file, int indent, const char *format, va_list va)
723 if (file)
725 if (format[0] != '\n')
726 while (0 < indent--)
727 fprintf(file, " ");
728 vfprintf(file, format, va);
733 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
735 if (decl_indirect(t))
737 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
738 local_var_prefix, n, local_var_prefix, n);
739 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
741 else if (is_ptr(t) || is_array(t))
742 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
745 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
747 const var_t *var;
749 if (!is_void(type_function_get_rettype(func->type)))
750 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
752 if (!type_get_function_args(func->type))
753 return;
755 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
756 write_var_init(file, indent, var->type, var->name, local_var_prefix);
758 fprintf(file, "\n");
761 static void write_formatdesc(FILE *f, int indent, const char *str)
763 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
764 print_file(f, indent, "{\n");
765 print_file(f, indent + 1, "short Pad;\n");
766 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
767 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
768 print_file(f, indent, "\n");
771 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
773 clear_all_offsets();
775 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
776 get_size_typeformatstring(stmts, pred));
778 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
779 get_size_procformatstring(stmts, pred));
781 fprintf(f, "\n");
782 write_formatdesc(f, indent, "TYPE");
783 write_formatdesc(f, indent, "PROC");
784 fprintf(f, "\n");
785 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
786 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
787 print_file(f, indent, "\n");
790 int decl_indirect(const type_t *t)
792 if (is_user_type(t))
793 return TRUE;
794 return (type_get_type(t) != TYPE_BASIC &&
795 type_get_type(t) != TYPE_ENUM &&
796 type_get_type(t) != TYPE_POINTER &&
797 type_get_type(t) != TYPE_ARRAY);
800 static unsigned int write_procformatstring_type(FILE *file, int indent,
801 const char *name,
802 const type_t *type,
803 const attr_list_t *attrs,
804 int is_return)
806 unsigned int size;
808 int is_in = is_attr(attrs, ATTR_IN);
809 int is_out = is_attr(attrs, ATTR_OUT);
811 if (!is_in && !is_out) is_in = TRUE;
813 if (type_get_type(type) == TYPE_BASIC ||
814 type_get_type(type) == TYPE_ENUM)
816 unsigned char fc;
818 if (is_return)
819 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
820 else
821 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
823 if (type_get_type(type) == TYPE_ENUM)
825 fc = get_enum_fc(type);
827 else
829 fc = get_basic_fc(type);
831 if (fc == RPC_FC_BIND_PRIMITIVE)
832 fc = RPC_FC_IGNORE;
835 print_file(file, indent, "0x%02x, /* %s */\n",
836 fc, string_of_type(fc));
837 size = 2; /* includes param type prefix */
839 else
841 if (is_return)
842 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
843 else if (is_in && is_out)
844 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
845 else if (is_out)
846 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
847 else
848 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
850 print_file(file, indent, "0x01,\n");
851 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
852 size = 4; /* includes param type prefix */
854 return size;
857 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
859 const statement_t *stmt;
860 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
862 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
864 const statement_t *stmt_func;
865 if (!pred(stmt->u.type))
866 continue;
867 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
869 const var_t *func = stmt_func->u.var;
870 if (is_local(func->attrs)) continue;
871 /* emit argument data */
872 if (type_get_function_args(func->type))
874 const var_t *var;
875 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
876 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
879 /* emit return value data */
880 if (is_void(type_function_get_rettype(func->type)))
882 print_file(file, indent, "0x5b, /* FC_END */\n");
883 print_file(file, indent, "0x5c, /* FC_PAD */\n");
885 else
886 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
889 else if (stmt->type == STMT_LIBRARY)
890 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
894 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
896 int indent = 0;
898 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
899 print_file(file, indent, "{\n");
900 indent++;
901 print_file(file, indent, "0,\n");
902 print_file(file, indent, "{\n");
903 indent++;
905 write_procformatstring_stmts(file, indent, stmts, pred);
907 print_file(file, indent, "0x0\n");
908 indent--;
909 print_file(file, indent, "}\n");
910 indent--;
911 print_file(file, indent, "};\n");
912 print_file(file, indent, "\n");
915 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
917 unsigned char fc;
919 if (type_get_type(type) == TYPE_BASIC)
920 fc = get_basic_fc(type);
921 else if (type_get_type(type) == TYPE_ENUM)
922 fc = get_enum_fc(type);
923 else
924 return 0;
926 if (convert_to_signed_type)
928 switch(fc)
930 case RPC_FC_USMALL:
931 fc = RPC_FC_SMALL;
932 break;
933 case RPC_FC_USHORT:
934 fc = RPC_FC_SHORT;
935 break;
936 case RPC_FC_ULONG:
937 fc = RPC_FC_LONG;
938 break;
942 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
943 *typestring_offset += 1;
944 return 1;
947 /* write conformance / variance descriptor */
948 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
949 unsigned int baseoff, const type_t *type,
950 const expr_t *expr)
952 unsigned char operator_type = 0;
953 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
954 const char *conftype_string = "";
955 const char *operator_string = "no operators";
956 const expr_t *subexpr;
958 if (!expr)
960 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
961 return 4;
964 if (!structure)
966 /* Top-level conformance calculations are done inline. */
967 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
968 RPC_FC_TOP_LEVEL_CONFORMANCE);
969 print_file (file, 2, "0x0,\n");
970 print_file (file, 2, "NdrFcShort(0x0),\n");
971 return 4;
974 if (expr->is_const)
976 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
977 error("write_conf_or_var_desc: constant value %ld is greater than "
978 "the maximum constant size of %d\n", expr->cval,
979 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
981 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
982 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
983 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
984 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
986 return 4;
989 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
991 conftype = RPC_FC_POINTER_CONFORMANCE;
992 conftype_string = "field pointer, ";
995 subexpr = expr;
996 switch (subexpr->type)
998 case EXPR_PPTR:
999 subexpr = subexpr->ref;
1000 operator_type = RPC_FC_DEREFERENCE;
1001 operator_string = "FC_DEREFERENCE";
1002 break;
1003 case EXPR_DIV:
1004 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1006 subexpr = subexpr->ref;
1007 operator_type = RPC_FC_DIV_2;
1008 operator_string = "FC_DIV_2";
1010 break;
1011 case EXPR_MUL:
1012 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1014 subexpr = subexpr->ref;
1015 operator_type = RPC_FC_MULT_2;
1016 operator_string = "FC_MULT_2";
1018 break;
1019 case EXPR_SUB:
1020 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1022 subexpr = subexpr->ref;
1023 operator_type = RPC_FC_SUB_1;
1024 operator_string = "FC_SUB_1";
1026 break;
1027 case EXPR_ADD:
1028 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1030 subexpr = subexpr->ref;
1031 operator_type = RPC_FC_ADD_1;
1032 operator_string = "FC_ADD_1";
1034 break;
1035 default:
1036 break;
1039 if (subexpr->type == EXPR_IDENTIFIER)
1041 const type_t *correlation_variable = NULL;
1042 unsigned char param_type = 0;
1043 unsigned int offset = 0;
1044 const var_t *var;
1045 var_list_t *fields = type_struct_get_fields(structure);
1047 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1049 unsigned int size = field_memsize( var->type, &offset );
1050 if (var->name && !strcmp(var->name, subexpr->u.sval))
1052 correlation_variable = var->type;
1053 break;
1055 offset += size;
1057 if (!correlation_variable)
1058 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1059 subexpr->u.sval);
1061 correlation_variable = expr_resolve_type(NULL, structure, expr);
1063 offset -= baseoff;
1065 if (type_get_type(correlation_variable) == TYPE_BASIC)
1067 switch (get_basic_fc(correlation_variable))
1069 case RPC_FC_CHAR:
1070 case RPC_FC_SMALL:
1071 param_type = RPC_FC_SMALL;
1072 break;
1073 case RPC_FC_BYTE:
1074 case RPC_FC_USMALL:
1075 param_type = RPC_FC_USMALL;
1076 break;
1077 case RPC_FC_WCHAR:
1078 case RPC_FC_SHORT:
1079 param_type = RPC_FC_SHORT;
1080 break;
1081 case RPC_FC_USHORT:
1082 param_type = RPC_FC_USHORT;
1083 break;
1084 case RPC_FC_LONG:
1085 param_type = RPC_FC_LONG;
1086 break;
1087 case RPC_FC_ULONG:
1088 param_type = RPC_FC_ULONG;
1089 break;
1090 default:
1091 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1092 get_basic_fc(correlation_variable));
1095 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1097 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1098 param_type = RPC_FC_LONG;
1099 else
1100 param_type = RPC_FC_SHORT;
1102 else
1104 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1105 subexpr->u.sval);
1106 return 0;
1109 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1110 conftype | param_type, conftype_string, string_of_type(param_type));
1111 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1112 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1113 offset, offset);
1115 else
1117 unsigned int callback_offset = 0;
1118 struct expr_eval_routine *eval;
1119 int found = 0;
1121 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1123 if (!strcmp (eval->structure->name, structure->name)
1124 && !compare_expr (eval->expr, expr))
1126 found = 1;
1127 break;
1129 callback_offset++;
1132 if (!found)
1134 eval = xmalloc (sizeof(*eval));
1135 eval->structure = structure;
1136 eval->baseoff = baseoff;
1137 eval->expr = expr;
1138 list_add_tail (&expr_eval_routines, &eval->entry);
1141 if (callback_offset > USHRT_MAX)
1142 error("Maximum number of callback routines reached\n");
1144 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1145 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1146 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1148 return 4;
1151 /* return size and start offset of a data field based on current offset */
1152 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1154 unsigned int align = 0;
1155 unsigned int size = type_memsize( type, &align );
1157 *offset = ROUND_SIZE( *offset, align );
1158 return size;
1161 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1163 unsigned int size = 0;
1164 unsigned int max_align;
1165 const var_t *v;
1167 if (!fields) return 0;
1168 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1170 unsigned int falign = 0;
1171 unsigned int fsize = type_memsize(v->type, &falign);
1172 if (*align < falign) *align = falign;
1173 falign = clamp_align(falign);
1174 size = ROUND_SIZE(size, falign);
1175 size += fsize;
1178 max_align = clamp_align(*align);
1179 size = ROUND_SIZE(size, max_align);
1181 return size;
1184 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1186 unsigned int size, maxs = 0;
1187 unsigned int align = *pmaxa;
1188 const var_t *v;
1190 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1192 /* we could have an empty default field with NULL type */
1193 if (v->type)
1195 size = type_memsize(v->type, &align);
1196 if (maxs < size) maxs = size;
1197 if (*pmaxa < align) *pmaxa = align;
1201 return maxs;
1204 int get_padding(const var_list_t *fields)
1206 unsigned short offset = 0;
1207 unsigned int salign = 1;
1208 const var_t *f;
1210 if (!fields)
1211 return 0;
1213 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1215 type_t *ft = f->type;
1216 unsigned int align = 0;
1217 unsigned int size = type_memsize(ft, &align);
1218 align = clamp_align(align);
1219 if (align > salign) salign = align;
1220 offset = ROUND_SIZE(offset, align);
1221 offset += size;
1224 return ROUNDING(offset, salign);
1227 unsigned int type_memsize(const type_t *t, unsigned int *align)
1229 unsigned int size = 0;
1231 switch (type_get_type(t))
1233 case TYPE_BASIC:
1234 switch (get_basic_fc(t))
1236 case RPC_FC_BYTE:
1237 case RPC_FC_CHAR:
1238 case RPC_FC_USMALL:
1239 case RPC_FC_SMALL:
1240 size = 1;
1241 if (size > *align) *align = size;
1242 break;
1243 case RPC_FC_WCHAR:
1244 case RPC_FC_USHORT:
1245 case RPC_FC_SHORT:
1246 size = 2;
1247 if (size > *align) *align = size;
1248 break;
1249 case RPC_FC_ULONG:
1250 case RPC_FC_LONG:
1251 case RPC_FC_ERROR_STATUS_T:
1252 case RPC_FC_FLOAT:
1253 size = 4;
1254 if (size > *align) *align = size;
1255 break;
1256 case RPC_FC_HYPER:
1257 case RPC_FC_DOUBLE:
1258 size = 8;
1259 if (size > *align) *align = size;
1260 break;
1261 default:
1262 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1263 size = 0;
1265 break;
1266 case TYPE_ENUM:
1267 switch (get_enum_fc(t))
1269 case RPC_FC_ENUM16:
1270 case RPC_FC_ENUM32:
1271 size = 4;
1272 if (size > *align) *align = size;
1273 break;
1274 default:
1275 error("type_memsize: Unknown enum type\n");
1276 size = 0;
1278 break;
1279 case TYPE_STRUCT:
1280 size = fields_memsize(type_struct_get_fields(t), align);
1281 break;
1282 case TYPE_ENCAPSULATED_UNION:
1283 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1284 break;
1285 case TYPE_UNION:
1286 size = union_memsize(type_union_get_cases(t), align);
1287 break;
1288 case TYPE_POINTER:
1289 assert( pointer_size );
1290 size = pointer_size;
1291 if (size > *align) *align = size;
1292 break;
1293 case TYPE_ARRAY:
1294 if (!type_array_is_decl_as_ptr(t))
1296 if (is_conformant_array(t))
1298 type_memsize(type_array_get_element(t), align);
1299 size = 0;
1301 else
1302 size = type_array_get_dim(t) *
1303 type_memsize(type_array_get_element(t), align);
1305 else /* declared as a pointer */
1307 assert( pointer_size );
1308 size = pointer_size;
1309 if (size > *align) *align = size;
1311 break;
1312 case TYPE_INTERFACE:
1313 case TYPE_ALIAS:
1314 case TYPE_VOID:
1315 case TYPE_COCLASS:
1316 case TYPE_MODULE:
1317 case TYPE_FUNCTION:
1318 /* these types should not be encountered here due to language
1319 * restrictions (interface, void, coclass, module), logical
1320 * restrictions (alias - due to type_get_type call above) or
1321 * checking restrictions (function). */
1322 assert(0);
1325 return size;
1328 int is_full_pointer_function(const var_t *func)
1330 const var_t *var;
1331 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1332 return TRUE;
1333 if (!type_get_function_args(func->type))
1334 return FALSE;
1335 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1336 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1337 return TRUE;
1338 return FALSE;
1341 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1343 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1344 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1345 fprintf(file, "\n");
1348 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1350 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1351 fprintf(file, "\n");
1354 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1355 const type_t *type,
1356 int toplevel_param,
1357 unsigned int offset,
1358 unsigned int *typeformat_offset)
1360 unsigned int start_offset = *typeformat_offset;
1361 short reloff = offset - (*typeformat_offset + 2);
1362 int in_attr, out_attr;
1363 int pointer_type;
1364 unsigned char flags = 0;
1366 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1368 in_attr = is_attr(attrs, ATTR_IN);
1369 out_attr = is_attr(attrs, ATTR_OUT);
1370 if (!in_attr && !out_attr) in_attr = 1;
1372 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1373 flags |= RPC_FC_P_ONSTACK;
1375 if (is_ptr(type) && !last_ptr(type))
1376 flags |= RPC_FC_P_DEREF;
1378 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1379 pointer_type,
1380 flags,
1381 string_of_type(pointer_type));
1382 if (file)
1384 if (flags & RPC_FC_P_ONSTACK)
1385 fprintf(file, " [allocated_on_stack]");
1386 if (flags & RPC_FC_P_DEREF)
1387 fprintf(file, " [pointer_deref]");
1388 fprintf(file, " */\n");
1391 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1392 *typeformat_offset += 4;
1394 return start_offset;
1397 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1399 unsigned char fc;
1400 unsigned char pointer_fc;
1401 const type_t *ref;
1403 /* for historical reasons, write_simple_pointer also handled string types,
1404 * but no longer does. catch bad uses of the function with this check */
1405 if (is_string_type(attrs, type))
1406 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1408 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1410 ref = type_pointer_get_ref(type);
1411 if (type_get_type(ref) == TYPE_ENUM)
1412 fc = get_enum_fc(ref);
1413 else
1414 fc = get_basic_fc(ref);
1416 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1417 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1418 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1419 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1420 return 4;
1423 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1425 print_file(file, 0, "/* %u (", tfsoff);
1426 write_type_decl(file, t, NULL);
1427 print_file(file, 0, ") */\n");
1430 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1431 type_t *type, int toplevel_param,
1432 unsigned int *typestring_offset)
1434 unsigned int offset = *typestring_offset;
1435 type_t *ref = type_pointer_get_ref(type);
1437 print_start_tfs_comment(file, type, offset);
1438 update_tfsoff(type, offset, file);
1440 if (ref->typestring_offset)
1441 write_nonsimple_pointer(file, attrs, type,
1442 toplevel_param,
1443 type_pointer_get_ref(type)->typestring_offset,
1444 typestring_offset);
1445 else if (type_get_type(ref) == TYPE_BASIC ||
1446 type_get_type(ref) == TYPE_ENUM)
1447 *typestring_offset += write_simple_pointer(file, attrs, type,
1448 toplevel_param);
1450 return offset;
1453 static int processed(const type_t *type)
1455 return type->typestring_offset && !type->tfswrite;
1458 static int user_type_has_variable_size(const type_t *t)
1460 if (is_ptr(t))
1461 return TRUE;
1462 else if (type_get_type(t) == TYPE_STRUCT)
1464 switch (get_struct_fc(t))
1466 case RPC_FC_PSTRUCT:
1467 case RPC_FC_CSTRUCT:
1468 case RPC_FC_CPSTRUCT:
1469 case RPC_FC_CVSTRUCT:
1470 return TRUE;
1473 /* Note: Since this only applies to user types, we can't have a conformant
1474 array here, and strings should get filed under pointer in this case. */
1475 return FALSE;
1478 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1480 unsigned int start, absoff, flags;
1481 unsigned int align = 0, ualign = 0;
1482 const char *name = NULL;
1483 type_t *utype = get_user_type(type, &name);
1484 unsigned int usize = type_memsize(utype, &ualign);
1485 unsigned int size = type_memsize(type, &align);
1486 unsigned short funoff = user_type_offset(name);
1487 short reloff;
1489 guard_rec(type);
1491 if(user_type_has_variable_size(utype)) usize = 0;
1493 if (type_get_type(utype) == TYPE_BASIC ||
1494 type_get_type(utype) == TYPE_ENUM)
1496 unsigned char fc;
1498 if (type_get_type(utype) == TYPE_ENUM)
1499 fc = get_enum_fc(utype);
1500 else
1501 fc = get_basic_fc(utype);
1503 absoff = *tfsoff;
1504 print_start_tfs_comment(file, utype, absoff);
1505 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1506 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1507 *tfsoff += 2;
1509 else
1511 if (!processed(utype))
1512 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1513 absoff = utype->typestring_offset;
1516 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1517 flags = 0x40;
1518 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1519 flags = 0x80;
1520 else
1521 flags = 0;
1523 start = *tfsoff;
1524 update_tfsoff(type, start, file);
1525 print_start_tfs_comment(file, type, start);
1526 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1527 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1528 flags | (ualign - 1), ualign - 1, flags);
1529 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1530 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1531 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1532 *tfsoff += 8;
1533 reloff = absoff - *tfsoff;
1534 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1535 *tfsoff += 2;
1538 static void write_member_type(FILE *file, const type_t *cont,
1539 int cont_is_complex, const attr_list_t *attrs,
1540 const type_t *type, unsigned int *corroff,
1541 unsigned int *tfsoff)
1543 if (is_embedded_complex(type) && !is_conformant_array(type))
1545 unsigned int absoff;
1546 short reloff;
1548 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1550 absoff = *corroff;
1551 *corroff += 8;
1553 else
1555 absoff = type->typestring_offset;
1557 reloff = absoff - (*tfsoff + 2);
1559 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1560 /* FIXME: actually compute necessary padding */
1561 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1562 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1563 reloff, reloff, absoff);
1564 *tfsoff += 4;
1566 else if (is_ptr(type) || is_conformant_array(type))
1568 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1569 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1570 *tfsoff += 1;
1572 else if (!write_base_type(file, type, TRUE, tfsoff))
1573 error("Unsupported member type %d\n", type_get_type(type));
1576 static void write_end(FILE *file, unsigned int *tfsoff)
1578 if (*tfsoff % 2 == 0)
1580 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1581 *tfsoff += 1;
1583 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1584 *tfsoff += 1;
1587 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1589 unsigned int offset = 0;
1590 var_list_t *fs = type_struct_get_fields(type);
1591 var_t *f;
1593 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1595 type_t *ft = f->type;
1596 unsigned int size = field_memsize( ft, &offset );
1597 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1599 short reloff;
1600 unsigned int absoff = ft->typestring_offset;
1601 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1602 absoff += 8; /* we already have a corr descr, skip it */
1603 reloff = absoff - (*tfsoff + 6);
1604 print_file(file, 0, "/* %d */\n", *tfsoff);
1605 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1606 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1607 write_conf_or_var_desc(file, current_structure, offset, ft,
1608 get_attrp(f->attrs, ATTR_SWITCHIS));
1609 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1610 reloff, reloff, absoff);
1611 *tfsoff += 8;
1613 offset += size;
1617 static int write_pointer_description_offsets(
1618 FILE *file, const attr_list_t *attrs, type_t *type,
1619 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1620 unsigned int *typestring_offset)
1622 int written = 0;
1623 unsigned int align;
1625 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1626 (is_array(type) && type_array_is_decl_as_ptr(type)))
1628 if (offset_in_memory && offset_in_buffer)
1630 unsigned int memsize;
1632 /* pointer instance */
1633 /* FIXME: sometimes from end of structure, sometimes from beginning */
1634 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1635 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1637 align = 0;
1638 memsize = type_memsize(type, &align);
1639 *offset_in_memory += memsize;
1640 /* increment these separately as in the case of conformant (varying)
1641 * structures these start at different values */
1642 *offset_in_buffer += memsize;
1644 *typestring_offset += 4;
1646 if (is_ptr(type))
1648 type_t *ref = type_pointer_get_ref(type);
1650 if (is_string_type(attrs, type))
1651 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1652 else if (processed(ref))
1653 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1654 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1655 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1656 else
1657 error("write_pointer_description_offsets: type format string unknown\n");
1659 else
1661 unsigned int offset = type->typestring_offset;
1662 /* skip over the pointer that is written for strings, since a
1663 * pointer has to be written in-place here */
1664 if (is_string_type(attrs, type))
1665 offset += 4;
1666 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1669 return 1;
1672 if (is_array(type))
1674 return write_pointer_description_offsets(
1675 file, attrs, type_array_get_element(type), offset_in_memory,
1676 offset_in_buffer, typestring_offset);
1678 else if (is_non_complex_struct(type))
1680 /* otherwise search for interesting fields to parse */
1681 const var_t *v;
1682 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1684 if (offset_in_memory && offset_in_buffer)
1686 unsigned int padding;
1687 align = 0;
1688 type_memsize(v->type, &align);
1689 padding = ROUNDING(*offset_in_memory, align);
1690 *offset_in_memory += padding;
1691 *offset_in_buffer += padding;
1693 written += write_pointer_description_offsets(
1694 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1695 typestring_offset);
1698 else
1700 if (offset_in_memory && offset_in_buffer)
1702 unsigned int memsize;
1703 align = 0;
1704 memsize = type_memsize(type, &align);
1705 *offset_in_memory += memsize;
1706 /* increment these separately as in the case of conformant (varying)
1707 * structures these start at different values */
1708 *offset_in_buffer += memsize;
1712 return written;
1715 static int write_no_repeat_pointer_descriptions(
1716 FILE *file, const attr_list_t *attrs, type_t *type,
1717 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1718 unsigned int *typestring_offset)
1720 int written = 0;
1721 unsigned int align;
1723 if (is_ptr(type) ||
1724 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1726 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1727 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1728 *typestring_offset += 2;
1730 return write_pointer_description_offsets(file, attrs, type,
1731 offset_in_memory, offset_in_buffer, typestring_offset);
1734 if (is_non_complex_struct(type))
1736 const var_t *v;
1737 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1739 if (offset_in_memory && offset_in_buffer)
1741 unsigned int padding;
1742 align = 0;
1743 type_memsize(v->type, &align);
1744 padding = ROUNDING(*offset_in_memory, align);
1745 *offset_in_memory += padding;
1746 *offset_in_buffer += padding;
1748 written += write_no_repeat_pointer_descriptions(
1749 file, v->attrs, v->type,
1750 offset_in_memory, offset_in_buffer, typestring_offset);
1753 else
1755 unsigned int memsize;
1756 align = 0;
1757 memsize = type_memsize(type, &align);
1758 *offset_in_memory += memsize;
1759 /* increment these separately as in the case of conformant (varying)
1760 * structures these start at different values */
1761 *offset_in_buffer += memsize;
1764 return written;
1767 /* Note: if file is NULL return value is number of pointers to write, else
1768 * it is the number of type format characters written */
1769 static int write_fixed_array_pointer_descriptions(
1770 FILE *file, const attr_list_t *attrs, type_t *type,
1771 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1772 unsigned int *typestring_offset)
1774 unsigned int align;
1775 int pointer_count = 0;
1777 if (type_get_type(type) == TYPE_ARRAY &&
1778 !type_array_has_conformance(type) && !type_array_has_variance(type))
1780 unsigned int temp = 0;
1781 /* unfortunately, this needs to be done in two passes to avoid
1782 * writing out redundant FC_FIXED_REPEAT descriptions */
1783 pointer_count = write_pointer_description_offsets(
1784 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1785 if (pointer_count > 0)
1787 unsigned int increment_size;
1788 unsigned int offset_of_array_pointer_mem = 0;
1789 unsigned int offset_of_array_pointer_buf = 0;
1791 align = 0;
1792 increment_size = type_memsize(type_array_get_element(type), &align);
1794 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1795 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1796 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1797 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1798 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1799 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1800 *typestring_offset += 10;
1802 pointer_count = write_pointer_description_offsets(
1803 file, attrs, type, &offset_of_array_pointer_mem,
1804 &offset_of_array_pointer_buf, typestring_offset);
1807 else if (type_get_type(type) == TYPE_STRUCT)
1809 const var_t *v;
1810 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1812 if (offset_in_memory && offset_in_buffer)
1814 unsigned int padding;
1815 align = 0;
1816 type_memsize(v->type, &align);
1817 padding = ROUNDING(*offset_in_memory, align);
1818 *offset_in_memory += padding;
1819 *offset_in_buffer += padding;
1821 pointer_count += write_fixed_array_pointer_descriptions(
1822 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1823 typestring_offset);
1826 else
1828 if (offset_in_memory && offset_in_buffer)
1830 unsigned int memsize;
1831 align = 0;
1832 memsize = type_memsize(type, &align);
1833 *offset_in_memory += memsize;
1834 /* increment these separately as in the case of conformant (varying)
1835 * structures these start at different values */
1836 *offset_in_buffer += memsize;
1840 return pointer_count;
1843 /* Note: if file is NULL return value is number of pointers to write, else
1844 * it is the number of type format characters written */
1845 static int write_conformant_array_pointer_descriptions(
1846 FILE *file, const attr_list_t *attrs, type_t *type,
1847 unsigned int offset_in_memory, unsigned int *typestring_offset)
1849 unsigned int align;
1850 int pointer_count = 0;
1852 if (is_conformant_array(type) && !type_array_has_variance(type))
1854 unsigned int temp = 0;
1855 /* unfortunately, this needs to be done in two passes to avoid
1856 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1857 pointer_count = write_pointer_description_offsets(
1858 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1859 if (pointer_count > 0)
1861 unsigned int increment_size;
1862 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1863 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1865 align = 0;
1866 increment_size = type_memsize(type_array_get_element(type), &align);
1868 if (increment_size > USHRT_MAX)
1869 error("array size of %u bytes is too large\n", increment_size);
1871 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1872 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1873 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1874 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1875 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1876 *typestring_offset += 8;
1878 pointer_count = write_pointer_description_offsets(
1879 file, attrs, type_array_get_element(type),
1880 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1881 typestring_offset);
1885 return pointer_count;
1888 /* Note: if file is NULL return value is number of pointers to write, else
1889 * it is the number of type format characters written */
1890 static int write_varying_array_pointer_descriptions(
1891 FILE *file, const attr_list_t *attrs, type_t *type,
1892 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1893 unsigned int *typestring_offset)
1895 unsigned int align;
1896 int pointer_count = 0;
1898 if (is_array(type) && type_array_has_variance(type))
1900 unsigned int temp = 0;
1901 /* unfortunately, this needs to be done in two passes to avoid
1902 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1903 pointer_count = write_pointer_description_offsets(
1904 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1905 if (pointer_count > 0)
1907 unsigned int increment_size;
1909 align = 0;
1910 increment_size = type_memsize(type_array_get_element(type), &align);
1912 if (increment_size > USHRT_MAX)
1913 error("array size of %u bytes is too large\n", increment_size);
1915 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1916 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1917 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1918 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1919 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1920 *typestring_offset += 8;
1922 pointer_count = write_pointer_description_offsets(
1923 file, attrs, type_array_get_element(type), offset_in_memory,
1924 offset_in_buffer, typestring_offset);
1927 else if (type_get_type(type) == TYPE_STRUCT)
1929 const var_t *v;
1930 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1932 if (offset_in_memory && offset_in_buffer)
1934 unsigned int padding;
1936 if (is_array(v->type) && type_array_has_variance(v->type))
1938 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1939 /* skip over variance and offset in buffer */
1940 *offset_in_buffer += 8;
1943 align = 0;
1944 type_memsize(v->type, &align);
1945 padding = ROUNDING(*offset_in_memory, align);
1946 *offset_in_memory += padding;
1947 *offset_in_buffer += padding;
1949 pointer_count += write_varying_array_pointer_descriptions(
1950 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1951 typestring_offset);
1954 else
1956 if (offset_in_memory && offset_in_buffer)
1958 unsigned int memsize;
1959 align = 0;
1960 memsize = type_memsize(type, &align);
1961 *offset_in_memory += memsize;
1962 /* increment these separately as in the case of conformant (varying)
1963 * structures these start at different values */
1964 *offset_in_buffer += memsize;
1968 return pointer_count;
1971 static void write_pointer_description(FILE *file, type_t *type,
1972 unsigned int *typestring_offset)
1974 unsigned int offset_in_buffer;
1975 unsigned int offset_in_memory;
1977 /* pass 1: search for single instance of a pointer (i.e. don't descend
1978 * into arrays) */
1979 if (!is_array(type))
1981 offset_in_memory = 0;
1982 offset_in_buffer = 0;
1983 write_no_repeat_pointer_descriptions(
1984 file, NULL, type,
1985 &offset_in_memory, &offset_in_buffer, typestring_offset);
1988 /* pass 2: search for pointers in fixed arrays */
1989 offset_in_memory = 0;
1990 offset_in_buffer = 0;
1991 write_fixed_array_pointer_descriptions(
1992 file, NULL, type,
1993 &offset_in_memory, &offset_in_buffer, typestring_offset);
1995 /* pass 3: search for pointers in conformant only arrays (but don't descend
1996 * into conformant varying or varying arrays) */
1997 if (is_conformant_array(type) &&
1998 (type_array_is_decl_as_ptr(type) || !current_structure))
1999 write_conformant_array_pointer_descriptions(
2000 file, NULL, type, 0, typestring_offset);
2001 else if (type_get_type(type) == TYPE_STRUCT &&
2002 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2004 unsigned int align = 0;
2005 type_t *carray = find_array_or_string_in_struct(type)->type;
2006 write_conformant_array_pointer_descriptions(
2007 file, NULL, carray,
2008 type_memsize(type, &align),
2009 typestring_offset);
2012 /* pass 4: search for pointers in varying arrays */
2013 offset_in_memory = 0;
2014 offset_in_buffer = 0;
2015 write_varying_array_pointer_descriptions(
2016 file, NULL, type,
2017 &offset_in_memory, &offset_in_buffer, typestring_offset);
2020 int is_declptr(const type_t *t)
2022 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2025 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2026 type_t *type, int toplevel_param,
2027 const char *name, unsigned int *typestring_offset)
2029 unsigned int start_offset;
2030 unsigned char rtype;
2031 type_t *elem_type;
2033 start_offset = *typestring_offset;
2034 update_tfsoff(type, start_offset, file);
2036 if (is_declptr(type))
2038 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2039 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2040 if (!pointer_type)
2041 pointer_type = RPC_FC_RP;
2042 print_start_tfs_comment(file, type, *typestring_offset);
2043 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2044 pointer_type, flag, string_of_type(pointer_type),
2045 flag ? " [simple_pointer]" : "");
2046 *typestring_offset += 2;
2047 if (!flag)
2049 print_file(file, 2, "NdrFcShort(0x2),\n");
2050 *typestring_offset += 2;
2054 if (is_array(type))
2055 elem_type = type_array_get_element(type);
2056 else
2057 elem_type = type_pointer_get_ref(type);
2059 if (type_get_type(elem_type) != TYPE_BASIC)
2061 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2062 return start_offset;
2065 rtype = get_basic_fc(elem_type);
2066 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2068 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2069 return start_offset;
2072 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2074 unsigned int dim = type_array_get_dim(type);
2076 /* FIXME: multi-dimensional array */
2077 if (0xffffu < dim)
2078 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2079 name, 0xffffu, dim - 0xffffu);
2081 if (rtype == RPC_FC_WCHAR)
2082 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2083 else
2084 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2085 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2086 *typestring_offset += 2;
2088 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2089 *typestring_offset += 2;
2091 return start_offset;
2093 else if (is_conformant_array(type))
2095 unsigned int align = 0;
2097 if (rtype == RPC_FC_WCHAR)
2098 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2099 else
2100 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2101 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2102 *typestring_offset += 2;
2104 *typestring_offset += write_conf_or_var_desc(
2105 file, current_structure,
2106 (!type_array_is_decl_as_ptr(type) && current_structure
2107 ? type_memsize(current_structure, &align)
2108 : 0),
2109 type, type_array_get_conformance(type));
2111 return start_offset;
2113 else
2115 if (rtype == RPC_FC_WCHAR)
2116 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2117 else
2118 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2119 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2120 *typestring_offset += 2;
2122 return start_offset;
2126 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2127 const char *name, unsigned int *typestring_offset)
2129 const expr_t *length_is = type_array_get_variance(type);
2130 const expr_t *size_is = type_array_get_conformance(type);
2131 unsigned int align = 0;
2132 unsigned int size;
2133 unsigned int start_offset;
2134 unsigned char fc;
2135 int has_pointer;
2136 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2137 unsigned int baseoff
2138 = !type_array_is_decl_as_ptr(type) && current_structure
2139 ? type_memsize(current_structure, &align)
2140 : 0;
2142 if (!pointer_type)
2143 pointer_type = RPC_FC_RP;
2145 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2146 has_pointer = TRUE;
2147 else
2148 has_pointer = type_has_pointers(type_array_get_element(type));
2150 align = 0;
2151 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2152 fc = get_array_fc(type);
2154 start_offset = *typestring_offset;
2155 update_tfsoff(type, start_offset, file);
2156 print_start_tfs_comment(file, type, start_offset);
2157 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2158 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2159 *typestring_offset += 2;
2161 align = 0;
2162 if (fc != RPC_FC_BOGUS_ARRAY)
2164 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2166 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2167 *typestring_offset += 4;
2169 else
2171 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2172 *typestring_offset += 2;
2175 if (is_conformant_array(type))
2176 *typestring_offset
2177 += write_conf_or_var_desc(file, current_structure, baseoff,
2178 type, size_is);
2180 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2182 unsigned int elalign = 0;
2183 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2184 unsigned int dim = type_array_get_dim(type);
2186 if (fc == RPC_FC_LGVARRAY)
2188 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2189 *typestring_offset += 4;
2191 else
2193 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2194 *typestring_offset += 2;
2197 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2198 *typestring_offset += 2;
2201 if (length_is)
2202 *typestring_offset
2203 += write_conf_or_var_desc(file, current_structure, baseoff,
2204 type, length_is);
2206 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2208 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2209 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2210 *typestring_offset += 2;
2211 write_pointer_description(file, type, typestring_offset);
2212 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2213 *typestring_offset += 1;
2216 write_member_type(file, type, FALSE, NULL, type_array_get_element(type), NULL, typestring_offset);
2217 write_end(file, typestring_offset);
2219 else
2221 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2222 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2223 *typestring_offset += 2;
2224 *typestring_offset
2225 += write_conf_or_var_desc(file, current_structure, baseoff,
2226 type, size_is);
2227 *typestring_offset
2228 += write_conf_or_var_desc(file, current_structure, baseoff,
2229 type, length_is);
2230 write_member_type(file, type, TRUE, NULL, type_array_get_element(type), NULL, typestring_offset);
2231 write_end(file, typestring_offset);
2234 return start_offset;
2237 static const var_t *find_array_or_string_in_struct(const type_t *type)
2239 const var_list_t *fields = type_struct_get_fields(type);
2240 const var_t *last_field;
2241 const type_t *ft;
2243 if (!fields || list_empty(fields))
2244 return NULL;
2246 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2247 ft = last_field->type;
2249 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2250 return last_field;
2252 if (type_get_type(ft) == TYPE_STRUCT)
2253 return find_array_or_string_in_struct(ft);
2254 else
2255 return NULL;
2258 static void write_struct_members(FILE *file, const type_t *type,
2259 int is_complex, unsigned int *corroff,
2260 unsigned int *typestring_offset)
2262 const var_t *field;
2263 unsigned short offset = 0;
2264 unsigned int salign = 1;
2265 int padding;
2266 var_list_t *fields = type_struct_get_fields(type);
2268 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2270 type_t *ft = field->type;
2271 unsigned int align = 0;
2272 unsigned int size = type_memsize(ft, &align);
2273 align = clamp_align(align);
2274 if (salign < align) salign = align;
2276 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2278 if ((align - 1) & offset)
2280 unsigned char fc = 0;
2281 switch (align)
2283 case 2:
2284 fc = RPC_FC_ALIGNM2;
2285 break;
2286 case 4:
2287 fc = RPC_FC_ALIGNM4;
2288 break;
2289 case 8:
2290 fc = RPC_FC_ALIGNM8;
2291 break;
2292 default:
2293 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2295 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2296 offset = ROUND_SIZE(offset, align);
2297 *typestring_offset += 1;
2299 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2300 typestring_offset);
2301 offset += size;
2305 padding = ROUNDING(offset, salign);
2306 if (padding)
2308 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2309 RPC_FC_STRUCTPAD1 + padding - 1,
2310 padding);
2311 *typestring_offset += 1;
2314 write_end(file, typestring_offset);
2317 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2318 const char *name, unsigned int *tfsoff)
2320 const type_t *save_current_structure = current_structure;
2321 unsigned int total_size;
2322 const var_t *array;
2323 unsigned int start_offset;
2324 unsigned int array_offset;
2325 int has_pointers = 0;
2326 unsigned int align = 0;
2327 unsigned int corroff;
2328 var_t *f;
2329 unsigned char fc = get_struct_fc(type);
2330 var_list_t *fields = type_struct_get_fields(type);
2332 guard_rec(type);
2333 current_structure = type;
2335 total_size = type_memsize(type, &align);
2336 if (total_size > USHRT_MAX)
2337 error("structure size for %s exceeds %d bytes by %d bytes\n",
2338 name, USHRT_MAX, total_size - USHRT_MAX);
2340 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2341 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2342 FALSE, tfsoff);
2343 if (!has_pointers) has_pointers = type_has_pointers(type);
2345 array = find_array_or_string_in_struct(type);
2346 if (array && !processed(array->type))
2347 array_offset
2348 = is_string_type(array->attrs, array->type)
2349 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2350 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2352 corroff = *tfsoff;
2353 write_descriptors(file, type, tfsoff);
2355 start_offset = *tfsoff;
2356 update_tfsoff(type, start_offset, file);
2357 print_start_tfs_comment(file, type, start_offset);
2358 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2359 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2360 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2361 *tfsoff += 4;
2363 if (array)
2365 unsigned int absoff = array->type->typestring_offset;
2366 short reloff = absoff - *tfsoff;
2367 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2368 reloff, reloff, absoff);
2369 *tfsoff += 2;
2371 else if (fc == RPC_FC_BOGUS_STRUCT)
2373 print_file(file, 2, "NdrFcShort(0x0),\n");
2374 *tfsoff += 2;
2377 if (fc == RPC_FC_BOGUS_STRUCT)
2379 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2380 nothing is written to file yet. On the actual writing pass,
2381 this will have been updated. */
2382 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2383 int reloff = absoff - *tfsoff;
2384 assert( reloff >= 0 );
2385 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2386 reloff, reloff, absoff);
2387 *tfsoff += 2;
2389 else if ((fc == RPC_FC_PSTRUCT) ||
2390 (fc == RPC_FC_CPSTRUCT) ||
2391 (fc == RPC_FC_CVSTRUCT && has_pointers))
2393 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2394 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2395 *tfsoff += 2;
2396 write_pointer_description(file, type, tfsoff);
2397 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2398 *tfsoff += 1;
2401 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2402 tfsoff);
2404 if (fc == RPC_FC_BOGUS_STRUCT)
2406 const var_t *f;
2408 type->ptrdesc = *tfsoff;
2409 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2411 type_t *ft = f->type;
2412 if (is_ptr(ft))
2414 if (is_string_type(f->attrs, ft))
2415 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2416 else
2417 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2419 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2421 unsigned int offset;
2423 print_file(file, 0, "/* %d */\n", *tfsoff);
2425 offset = ft->typestring_offset;
2426 /* skip over the pointer that is written for strings, since a
2427 * pointer has to be written in-place here */
2428 if (is_string_type(f->attrs, ft))
2429 offset += 4;
2430 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2433 if (type->ptrdesc == *tfsoff)
2434 type->ptrdesc = 0;
2437 current_structure = save_current_structure;
2438 return start_offset;
2441 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2443 if (t == NULL)
2445 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2447 else
2449 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2451 unsigned char fc;
2452 if (type_get_type(t) == TYPE_BASIC)
2453 fc = get_basic_fc(t);
2454 else
2455 fc = get_enum_fc(t);
2456 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2457 fc, string_of_type(fc));
2459 else if (t->typestring_offset)
2461 short reloff = t->typestring_offset - *tfsoff;
2462 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2463 reloff, reloff, t->typestring_offset);
2465 else
2466 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2469 *tfsoff += 2;
2472 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2474 unsigned int align;
2475 unsigned int start_offset;
2476 unsigned int size;
2477 var_list_t *fields;
2478 unsigned int nbranch = 0;
2479 type_t *deftype = NULL;
2480 short nodeftype = 0xffff;
2481 var_t *f;
2483 guard_rec(type);
2485 align = 0;
2486 size = type_memsize(type, &align);
2488 fields = type_union_get_cases(type);
2490 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2492 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2493 if (cases)
2494 nbranch += list_count(cases);
2495 if (f->type)
2496 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2499 start_offset = *tfsoff;
2500 update_tfsoff(type, start_offset, file);
2501 print_start_tfs_comment(file, type, start_offset);
2502 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2504 const var_t *sv = type_union_get_switch_value(type);
2505 const type_t *st = sv->type;
2506 unsigned char fc;
2508 if (type_get_type(st) == TYPE_BASIC)
2510 switch (get_basic_fc(st))
2512 case RPC_FC_CHAR:
2513 case RPC_FC_SMALL:
2514 case RPC_FC_BYTE:
2515 case RPC_FC_USMALL:
2516 case RPC_FC_WCHAR:
2517 case RPC_FC_SHORT:
2518 case RPC_FC_USHORT:
2519 case RPC_FC_LONG:
2520 case RPC_FC_ULONG:
2521 fc = get_basic_fc(st);
2522 break;
2523 default:
2524 fc = 0;
2525 error("union switch type must be an integer, char, or enum\n");
2528 else if (type_get_type(st) == TYPE_ENUM)
2529 fc = get_enum_fc(st);
2530 else
2531 error("union switch type must be an integer, char, or enum\n");
2533 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2534 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2535 0x40 | fc, string_of_type(fc));
2536 *tfsoff += 2;
2538 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2540 static const expr_t dummy_expr; /* FIXME */
2541 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2542 unsigned char fc;
2544 if (type_get_type(st) == TYPE_BASIC)
2546 switch (get_basic_fc(st))
2548 case RPC_FC_CHAR:
2549 case RPC_FC_SMALL:
2550 case RPC_FC_USMALL:
2551 case RPC_FC_SHORT:
2552 case RPC_FC_USHORT:
2553 case RPC_FC_LONG:
2554 case RPC_FC_ULONG:
2555 case RPC_FC_ENUM16:
2556 case RPC_FC_ENUM32:
2557 fc = get_basic_fc(st);
2558 break;
2559 default:
2560 fc = 0;
2561 error("union switch type must be an integer, char, or enum\n");
2564 else if (type_get_type(st) == TYPE_ENUM)
2565 fc = get_enum_fc(st);
2566 else
2567 error("union switch type must be an integer, char, or enum\n");
2569 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2570 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2571 fc, string_of_type(fc));
2572 *tfsoff += 2;
2574 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2575 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2576 *tfsoff += 2;
2577 print_file(file, 0, "/* %u */\n", *tfsoff);
2580 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2581 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2582 *tfsoff += 4;
2584 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2586 type_t *ft = f->type;
2587 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2588 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2589 expr_t *c;
2591 if (cases == NULL && !deflt)
2592 error("union field %s with neither case nor default attribute\n", f->name);
2594 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2596 /* MIDL doesn't check for duplicate cases, even though that seems
2597 like a reasonable thing to do, it just dumps them to the TFS
2598 like we're going to do here. */
2599 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2600 *tfsoff += 4;
2601 write_branch_type(file, ft, tfsoff);
2604 /* MIDL allows multiple default branches, even though that seems
2605 illogical, it just chooses the last one, which is what we will
2606 do. */
2607 if (deflt)
2609 deftype = ft;
2610 nodeftype = 0;
2614 if (deftype)
2616 write_branch_type(file, deftype, tfsoff);
2618 else
2620 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2621 *tfsoff += 2;
2624 return start_offset;
2627 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2628 unsigned int *typeformat_offset)
2630 unsigned int i;
2631 unsigned int start_offset = *typeformat_offset;
2632 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2634 if (iid)
2636 print_file(file, 2, "0x2f, /* FC_IP */\n");
2637 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2638 *typeformat_offset
2639 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2641 else
2643 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2644 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2646 if (! uuid)
2647 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2649 update_tfsoff(type, start_offset, file);
2650 print_start_tfs_comment(file, type, start_offset);
2651 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2652 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2653 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2654 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2655 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2656 for (i = 0; i < 8; ++i)
2657 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2659 if (file)
2660 fprintf(file, "\n");
2662 *typeformat_offset += 18;
2664 return start_offset;
2667 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2668 const var_t *var,
2669 unsigned int *typeformat_offset)
2671 unsigned int start_offset = *typeformat_offset;
2672 unsigned char flags = 0;
2674 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2675 flags |= NDR_STRICT_CONTEXT_HANDLE;
2677 if (is_ptr(type))
2678 flags |= 0x80;
2679 if (is_attr(var->attrs, ATTR_IN))
2681 flags |= 0x40;
2682 if (!is_attr(var->attrs, ATTR_OUT))
2683 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2685 if (is_attr(var->attrs, ATTR_OUT))
2686 flags |= 0x20;
2688 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2689 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2690 /* return and can't be null values overlap */
2691 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2692 print_file(file, 0, "can't be null, ");
2693 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2694 print_file(file, 0, "serialize, ");
2695 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2696 print_file(file, 0, "no serialize, ");
2697 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2698 print_file(file, 0, "strict, ");
2699 if ((flags & 0x21) == 0x20)
2700 print_file(file, 0, "out, ");
2701 if ((flags & 0x21) == 0x21)
2702 print_file(file, 0, "return, ");
2703 if (flags & 0x40)
2704 print_file(file, 0, "in, ");
2705 if (flags & 0x80)
2706 print_file(file, 0, "via ptr, ");
2707 print_file(file, 0, "*/\n");
2708 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2709 print_file(file, 2, "0, /* FIXME: param num */\n");
2710 *typeformat_offset += 4;
2712 return start_offset;
2715 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2716 type_t *type, const var_t *var,
2717 int toplevel_param,
2718 unsigned int *typeformat_offset)
2720 unsigned int offset;
2722 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2724 case TGT_CTXT_HANDLE:
2725 case TGT_CTXT_HANDLE_POINTER:
2726 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2727 case TGT_USER_TYPE:
2728 write_user_tfs(file, type, typeformat_offset);
2729 return type->typestring_offset;
2730 case TGT_STRING:
2731 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2732 case TGT_ARRAY:
2734 int ptr_type;
2735 unsigned int off;
2736 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2737 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2738 if (ptr_type != RPC_FC_RP)
2740 unsigned int absoff = type->typestring_offset;
2741 short reloff = absoff - (*typeformat_offset + 2);
2742 off = *typeformat_offset;
2743 print_file(file, 0, "/* %d */\n", off);
2744 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2745 string_of_type(ptr_type));
2746 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2747 reloff, reloff, absoff);
2748 *typeformat_offset += 4;
2750 return off;
2752 case TGT_STRUCT:
2753 if (processed(type)) return type->typestring_offset;
2754 return write_struct_tfs(file, type, var->name, typeformat_offset);
2755 case TGT_UNION:
2756 if (processed(type)) return type->typestring_offset;
2757 return write_union_tfs(file, type, typeformat_offset);
2758 case TGT_ENUM:
2759 case TGT_BASIC:
2760 /* nothing to do */
2761 return 0;
2762 case TGT_IFACE_POINTER:
2763 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2764 case TGT_POINTER:
2765 if (last_ptr(type))
2767 size_t start_offset = *typeformat_offset;
2768 int in_attr = is_attr(var->attrs, ATTR_IN);
2769 int out_attr = is_attr(var->attrs, ATTR_OUT);
2770 const type_t *ref = type_pointer_get_ref(type);
2772 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2774 /* special case for pointers to base types */
2775 case TGT_BASIC:
2776 case TGT_ENUM:
2778 unsigned char fc;
2780 if (type_get_type(ref) == TYPE_ENUM)
2781 fc = get_enum_fc(ref);
2782 else
2783 fc = get_basic_fc(ref);
2785 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2786 get_pointer_fc(type, var->attrs, toplevel_param),
2787 (!in_attr && out_attr) ? 0x0C : 0x08,
2788 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2789 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2790 print_file(file, indent, "0x%02x, /* %s */\n",
2791 fc, string_of_type(fc));
2792 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2793 *typeformat_offset += 4;
2794 return start_offset;
2796 default:
2797 break;
2801 offset = write_typeformatstring_var(file, indent, func,
2802 type_pointer_get_ref(type), var,
2803 FALSE, typeformat_offset);
2804 if (file)
2805 fprintf(file, "/* %2u */\n", *typeformat_offset);
2806 return write_nonsimple_pointer(file, var->attrs, type,
2807 toplevel_param,
2808 offset, typeformat_offset);
2809 case TGT_INVALID:
2810 break;
2812 error("invalid type %s for var %s\n", type->name, var->name);
2813 return 0;
2816 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2817 const char *name, int write_ptr, unsigned int *tfsoff)
2819 int retmask = 0;
2821 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2823 case TGT_USER_TYPE:
2824 write_user_tfs(file, type, tfsoff);
2825 break;
2826 case TGT_STRING:
2827 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2828 break;
2829 case TGT_IFACE_POINTER:
2830 write_ip_tfs(file, attrs, type, tfsoff);
2831 break;
2832 case TGT_POINTER:
2834 type_t *ref = type_pointer_get_ref(type);
2836 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2837 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2839 if (write_ptr)
2840 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2842 retmask |= 1;
2843 break;
2845 case TGT_ARRAY:
2846 /* conformant arrays and strings are handled specially */
2847 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2849 write_array_tfs(file, attrs, type, name, tfsoff);
2850 if (is_conformant_array(type))
2851 retmask |= 1;
2853 break;
2854 case TGT_STRUCT:
2855 if (!processed(type))
2856 write_struct_tfs(file, type, name, tfsoff);
2857 break;
2858 case TGT_UNION:
2859 if (!processed(type))
2860 write_union_tfs(file, type, tfsoff);
2861 break;
2862 case TGT_ENUM:
2863 case TGT_BASIC:
2864 /* nothing to do */
2865 break;
2866 case TGT_CTXT_HANDLE:
2867 case TGT_CTXT_HANDLE_POINTER:
2868 case TGT_INVALID:
2869 error("invalid type %s for var %s\n", type->name, name);
2870 break;
2873 return retmask;
2876 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2877 type_pred_t pred, unsigned int *typeformat_offset)
2879 const var_t *var;
2880 const statement_t *stmt;
2882 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2884 const type_t *iface;
2885 const statement_t *stmt_func;
2887 if (stmt->type == STMT_LIBRARY)
2889 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2890 continue;
2892 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2893 continue;
2895 iface = stmt->u.type;
2896 if (!pred(iface))
2897 continue;
2899 current_iface = iface;
2900 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2902 const var_t *func = stmt_func->u.var;
2903 if (is_local(func->attrs)) continue;
2905 if (!is_void(type_function_get_rettype(func->type)))
2907 var_t v = *func;
2908 v.type = type_function_get_rettype(func->type);
2909 update_tfsoff(type_function_get_rettype(func->type),
2910 write_typeformatstring_var(
2911 file, 2, NULL,
2912 type_function_get_rettype(func->type),
2913 &v, FALSE, typeformat_offset),
2914 file);
2917 current_func = func;
2918 if (type_get_function_args(func->type))
2919 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2920 update_tfsoff(
2921 var->type,
2922 write_typeformatstring_var(
2923 file, 2, func, var->type, var,
2924 TRUE, typeformat_offset),
2925 file);
2929 return *typeformat_offset + 1;
2932 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2934 unsigned int typeformat_offset = 2;
2936 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2940 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2942 int indent = 0;
2944 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2945 print_file(file, indent, "{\n");
2946 indent++;
2947 print_file(file, indent, "0,\n");
2948 print_file(file, indent, "{\n");
2949 indent++;
2950 print_file(file, indent, "NdrFcShort(0x0),\n");
2952 set_all_tfswrite(TRUE);
2953 process_tfs(file, stmts, pred);
2955 print_file(file, indent, "0x0\n");
2956 indent--;
2957 print_file(file, indent, "}\n");
2958 indent--;
2959 print_file(file, indent, "};\n");
2960 print_file(file, indent, "\n");
2963 static unsigned int get_required_buffer_size_type(
2964 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2966 *alignment = 0;
2967 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
2969 case TGT_USER_TYPE:
2971 const char *uname;
2972 const type_t *utype = get_user_type(type, &uname);
2973 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
2975 case TGT_BASIC:
2976 switch (get_basic_fc(type))
2978 case RPC_FC_BYTE:
2979 case RPC_FC_CHAR:
2980 case RPC_FC_USMALL:
2981 case RPC_FC_SMALL:
2982 *alignment = 4;
2983 return 1;
2985 case RPC_FC_WCHAR:
2986 case RPC_FC_USHORT:
2987 case RPC_FC_SHORT:
2988 *alignment = 4;
2989 return 2;
2991 case RPC_FC_ULONG:
2992 case RPC_FC_LONG:
2993 case RPC_FC_FLOAT:
2994 case RPC_FC_ERROR_STATUS_T:
2995 *alignment = 4;
2996 return 4;
2998 case RPC_FC_HYPER:
2999 case RPC_FC_DOUBLE:
3000 *alignment = 8;
3001 return 8;
3003 case RPC_FC_IGNORE:
3004 case RPC_FC_BIND_PRIMITIVE:
3005 return 0;
3007 default:
3008 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3009 get_basic_fc(type));
3010 return 0;
3012 break;
3014 case TGT_ENUM:
3015 switch (get_enum_fc(type))
3017 case RPC_FC_ENUM32:
3018 *alignment = 4;
3019 return 4;
3020 case RPC_FC_ENUM16:
3021 *alignment = 4;
3022 return 2;
3024 break;
3026 case TGT_STRUCT:
3027 if (get_struct_fc(type) == RPC_FC_STRUCT)
3029 if (!type_struct_get_fields(type)) return 0;
3030 return fields_memsize(type_struct_get_fields(type), alignment);
3032 break;
3034 case TGT_POINTER:
3035 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3037 const type_t *ref = type_pointer_get_ref(type);
3038 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3040 case TGT_BASIC:
3041 case TGT_ENUM:
3042 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3043 case TGT_STRUCT:
3044 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3045 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3046 break;
3047 case TGT_USER_TYPE:
3048 case TGT_CTXT_HANDLE:
3049 case TGT_CTXT_HANDLE_POINTER:
3050 case TGT_STRING:
3051 case TGT_POINTER:
3052 case TGT_ARRAY:
3053 case TGT_IFACE_POINTER:
3054 case TGT_UNION:
3055 case TGT_INVALID:
3056 break;
3059 break;
3061 case TGT_ARRAY:
3062 /* FIXME: depends on pointer type */
3063 return type_array_get_dim(type) *
3064 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3066 default:
3067 break;
3069 return 0;
3072 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3074 int in_attr = is_attr(var->attrs, ATTR_IN);
3075 int out_attr = is_attr(var->attrs, ATTR_OUT);
3077 if (!in_attr && !out_attr)
3078 in_attr = 1;
3080 *alignment = 0;
3082 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3083 pass == PASS_RETURN)
3085 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3087 *alignment = 4;
3088 return 20;
3091 if (!is_string_type(var->attrs, var->type))
3092 return get_required_buffer_size_type(var->type, var->name,
3093 var->attrs, TRUE, alignment);
3095 return 0;
3098 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3100 const var_t *var;
3101 unsigned int total_size = 0, alignment;
3103 if (type_get_function_args(func->type))
3105 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3107 total_size += get_required_buffer_size(var, &alignment, pass);
3108 total_size += alignment;
3112 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3114 var_t v = *func;
3115 v.type = type_function_get_rettype(func->type);
3116 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3117 total_size += alignment;
3119 return total_size;
3122 static void print_phase_function(FILE *file, int indent, const char *type,
3123 const char *local_var_prefix, enum remoting_phase phase,
3124 const var_t *var, unsigned int type_offset)
3126 const char *function;
3127 switch (phase)
3129 case PHASE_BUFFERSIZE:
3130 function = "BufferSize";
3131 break;
3132 case PHASE_MARSHAL:
3133 function = "Marshall";
3134 break;
3135 case PHASE_UNMARSHAL:
3136 function = "Unmarshall";
3137 break;
3138 case PHASE_FREE:
3139 function = "Free";
3140 break;
3141 default:
3142 assert(0);
3143 return;
3146 print_file(file, indent, "Ndr%s%s(\n", type, function);
3147 indent++;
3148 print_file(file, indent, "&__frame->_StubMsg,\n");
3149 print_file(file, indent, "%s%s%s%s%s,\n",
3150 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3151 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3152 local_var_prefix,
3153 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3154 var->name);
3155 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3156 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3157 if (phase == PHASE_UNMARSHAL)
3158 print_file(file, indent, "0);\n");
3159 indent--;
3162 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3163 enum remoting_phase phase, enum pass pass, const var_t *var,
3164 const char *varname)
3166 type_t *type = var->type;
3167 unsigned int size;
3168 unsigned int alignment = 0;
3169 const type_t *ref;
3171 /* no work to do for other phases, buffer sizing is done elsewhere */
3172 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3173 return;
3175 ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3176 if (type_get_type(ref) == TYPE_ENUM)
3178 if (get_enum_fc(ref) == RPC_FC_ENUM32)
3180 size = 4;
3181 alignment = 4;
3183 else /* RPC_FC_ENUM16 */
3185 size = 2;
3186 alignment = 2;
3189 else
3191 switch (get_basic_fc(ref))
3193 case RPC_FC_BYTE:
3194 case RPC_FC_CHAR:
3195 case RPC_FC_SMALL:
3196 case RPC_FC_USMALL:
3197 size = 1;
3198 alignment = 1;
3199 break;
3201 case RPC_FC_WCHAR:
3202 case RPC_FC_USHORT:
3203 case RPC_FC_SHORT:
3204 size = 2;
3205 alignment = 2;
3206 break;
3208 case RPC_FC_ULONG:
3209 case RPC_FC_LONG:
3210 case RPC_FC_FLOAT:
3211 case RPC_FC_ERROR_STATUS_T:
3212 size = 4;
3213 alignment = 4;
3214 break;
3216 case RPC_FC_HYPER:
3217 case RPC_FC_DOUBLE:
3218 size = 8;
3219 alignment = 8;
3220 break;
3222 case RPC_FC_IGNORE:
3223 case RPC_FC_BIND_PRIMITIVE:
3224 /* no marshalling needed */
3225 return;
3227 default:
3228 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3229 var->name, get_basic_fc(ref));
3230 size = 0;
3234 if (phase == PHASE_MARSHAL)
3235 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3236 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3237 alignment - 1, alignment - 1);
3239 if (phase == PHASE_MARSHAL)
3241 print_file(file, indent, "*(");
3242 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3243 if (is_ptr(type))
3244 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3245 else
3246 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3247 fprintf(file, "%s%s", local_var_prefix, varname);
3248 fprintf(file, ";\n");
3250 else if (phase == PHASE_UNMARSHAL)
3252 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3253 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3254 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3255 print_file(file, indent, "{\n");
3256 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3257 print_file(file, indent, "}\n");
3258 print_file(file, indent, "%s%s%s",
3259 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3260 local_var_prefix, varname);
3261 if (pass == PASS_IN && is_ptr(type))
3262 fprintf(file, " = (");
3263 else
3264 fprintf(file, " = *(");
3265 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3266 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3269 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3270 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3271 fprintf(file, ");\n");
3274 /* returns whether the MaxCount, Offset or ActualCount members need to be
3275 * filled in for the specified phase */
3276 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3278 return (phase != PHASE_UNMARSHAL);
3281 expr_t *get_size_is_expr(const type_t *t, const char *name)
3283 expr_t *x = NULL;
3285 for ( ; is_array(t); t = type_array_get_element(t))
3286 if (type_array_has_conformance(t))
3288 if (!x)
3289 x = type_array_get_conformance(t);
3290 else
3291 error("%s: multidimensional conformant"
3292 " arrays not supported at the top level\n",
3293 name);
3296 return x;
3299 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3300 enum remoting_phase phase, const var_t *var)
3302 const type_t *type = var->type;
3303 /* get fundamental type for the argument */
3304 for (;;)
3306 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3307 break;
3308 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3309 break;
3310 else if (type_is_alias(type))
3311 type = type_alias_get_aliasee(type);
3312 else if (is_array(type))
3314 if (is_conformance_needed_for_phase(phase) && is_array(type))
3316 if (type_array_has_conformance(type))
3318 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3319 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3320 fprintf(file, ";\n\n");
3322 if (type_array_has_variance(type))
3324 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3325 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3326 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3327 fprintf(file, ";\n\n");
3330 break;
3332 else if (type_get_type(type) == TYPE_UNION)
3334 if (is_conformance_needed_for_phase(phase))
3336 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3337 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3338 fprintf(file, ";\n\n");
3340 break;
3342 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3344 expr_t *iid;
3346 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3348 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3349 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3350 fprintf( file, ";\n\n" );
3352 break;
3354 else if (is_ptr(type))
3355 type = type_pointer_get_ref(type);
3356 else
3357 break;
3361 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3362 enum pass pass, enum remoting_phase phase, const var_t *var)
3364 int in_attr, out_attr, pointer_type;
3365 const type_t *type = var->type;
3366 unsigned int start_offset = type->typestring_offset;
3368 if (is_ptr(type) || is_array(type))
3369 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3370 else
3371 pointer_type = 0;
3373 in_attr = is_attr(var->attrs, ATTR_IN);
3374 out_attr = is_attr(var->attrs, ATTR_OUT);
3375 if (!in_attr && !out_attr)
3376 in_attr = 1;
3378 if (phase != PHASE_FREE)
3379 switch (pass)
3381 case PASS_IN:
3382 if (!in_attr) return;
3383 break;
3384 case PASS_OUT:
3385 if (!out_attr) return;
3386 break;
3387 case PASS_RETURN:
3388 break;
3391 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3393 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3395 case TGT_CTXT_HANDLE:
3396 case TGT_CTXT_HANDLE_POINTER:
3397 if (phase == PHASE_MARSHAL)
3399 if (pass == PASS_IN)
3401 /* if the context_handle attribute appears in the chain of types
3402 * without pointers being followed, then the context handle must
3403 * be direct, otherwise it is a pointer */
3404 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3405 print_file(file, indent, "NdrClientContextMarshall(\n");
3406 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3407 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3408 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3410 else
3412 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3413 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3414 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3415 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3416 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3419 else if (phase == PHASE_UNMARSHAL)
3421 if (pass == PASS_OUT)
3423 if (!in_attr)
3424 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3425 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3426 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3427 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3428 print_file(file, indent + 1, "__frame->_Handle);\n");
3430 else
3432 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3433 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3434 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3437 break;
3438 case TGT_USER_TYPE:
3439 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3440 break;
3441 case TGT_STRING:
3442 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3443 pointer_type != RPC_FC_RP)
3445 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3446 !in_attr && is_conformant_array(type))
3448 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3449 indent++;
3450 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3452 /* strings returned are assumed to be global and hence don't
3453 * need freeing */
3454 else if (is_declptr(type) &&
3455 !(phase == PHASE_FREE && pass == PASS_RETURN))
3456 print_phase_function(file, indent, "Pointer", local_var_prefix,
3457 phase, var, start_offset);
3459 else
3461 unsigned int real_start_offset = start_offset;
3462 /* skip over pointer description straight to string description */
3463 if (is_declptr(type))
3465 if (is_conformant_array(type))
3466 real_start_offset += 4;
3467 else
3468 real_start_offset += 2;
3470 if (is_array(type) && !is_conformant_array(type))
3471 print_phase_function(file, indent, "NonConformantString",
3472 local_var_prefix, phase, var,
3473 real_start_offset);
3474 else
3475 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3476 phase, var, real_start_offset);
3478 break;
3479 case TGT_ARRAY:
3481 unsigned char tc = get_array_fc(type);
3482 const char *array_type = "FixedArray";
3484 /* We already have the size_is expression since it's at the
3485 top level, but do checks for multidimensional conformant
3486 arrays. When we handle them, we'll need to extend this
3487 function to return a list, and then we'll actually use
3488 the return value. */
3489 get_size_is_expr(type, var->name);
3491 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3493 array_type = "VaryingArray";
3495 else if (tc == RPC_FC_CARRAY)
3497 array_type = "ConformantArray";
3499 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3501 array_type = (tc == RPC_FC_BOGUS_ARRAY
3502 ? "ComplexArray"
3503 : "ConformantVaryingArray");
3506 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3507 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3508 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3510 /* these are all unmarshalled by allocating memory */
3511 if (tc == RPC_FC_BOGUS_ARRAY ||
3512 tc == RPC_FC_CVARRAY ||
3513 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3514 (tc == RPC_FC_CARRAY && !in_attr))
3516 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3517 indent++;
3518 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3521 break;
3523 case TGT_BASIC:
3524 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3525 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3526 break;
3527 case TGT_ENUM:
3528 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3530 if (phase == PHASE_MARSHAL)
3531 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3532 else
3533 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3534 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3535 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3536 local_var_prefix,
3537 var->name);
3538 print_file(file, indent+1, "0x%02x /* %s */);\n", get_enum_fc(type), string_of_type(get_enum_fc(type)));
3540 break;
3541 case TGT_STRUCT:
3542 switch (get_struct_fc(type))
3544 case RPC_FC_STRUCT:
3545 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3546 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3547 break;
3548 case RPC_FC_PSTRUCT:
3549 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3550 break;
3551 case RPC_FC_CSTRUCT:
3552 case RPC_FC_CPSTRUCT:
3553 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3554 break;
3555 case RPC_FC_CVSTRUCT:
3556 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3557 break;
3558 case RPC_FC_BOGUS_STRUCT:
3559 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3560 break;
3561 default:
3562 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3564 break;
3565 case TGT_UNION:
3567 const char *union_type = NULL;
3569 if (type_get_type(type) == TYPE_UNION)
3570 union_type = "NonEncapsulatedUnion";
3571 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3572 union_type = "EncapsulatedUnion";
3574 print_phase_function(file, indent, union_type, local_var_prefix,
3575 phase, var, start_offset);
3576 break;
3578 case TGT_POINTER:
3580 const type_t *ref = type_pointer_get_ref(type);
3581 if (pointer_type == RPC_FC_RP && !is_user_type(ref)) switch (type_get_type(ref))
3583 case TYPE_BASIC:
3584 /* base types have known sizes, so don't need a sizing pass
3585 * and don't have any memory to free and so don't need a
3586 * freeing pass */
3587 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3588 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3589 break;
3590 case TYPE_ENUM:
3591 /* base types have known sizes, so don't need a sizing pass
3592 * and don't have any memory to free and so don't need a
3593 * freeing pass */
3594 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3595 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3596 break;
3597 case TYPE_STRUCT:
3599 const char *struct_type = NULL;
3600 switch (get_struct_fc(ref))
3602 case RPC_FC_STRUCT:
3603 /* simple structs have known sizes, so don't need a sizing
3604 * pass and don't have any memory to free and so don't
3605 * need a freeing pass */
3606 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3607 struct_type = "SimpleStruct";
3608 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3610 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3611 indent++;
3612 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3613 indent--;
3615 break;
3616 case RPC_FC_PSTRUCT:
3617 struct_type = "SimpleStruct";
3618 break;
3619 case RPC_FC_CSTRUCT:
3620 case RPC_FC_CPSTRUCT:
3621 struct_type = "ConformantStruct";
3622 break;
3623 case RPC_FC_CVSTRUCT:
3624 struct_type = "ConformantVaryingStruct";
3625 break;
3626 case RPC_FC_BOGUS_STRUCT:
3627 struct_type = "ComplexStruct";
3628 break;
3629 default:
3630 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3633 if (struct_type)
3635 if (phase == PHASE_FREE)
3636 struct_type = "Pointer";
3637 else
3638 start_offset = ref->typestring_offset;
3639 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3641 break;
3643 case TYPE_UNION:
3644 case TYPE_ENCAPSULATED_UNION:
3646 const char *union_type = NULL;
3647 if (phase == PHASE_FREE)
3648 union_type = "Pointer";
3649 else
3651 if (type_get_type(ref) == TYPE_UNION)
3652 union_type = "NonEncapsulatedUnion";
3653 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3654 union_type = "EncapsulatedUnion";
3656 start_offset = ref->typestring_offset;
3659 print_phase_function(file, indent, union_type, local_var_prefix,
3660 phase, var, start_offset);
3661 break;
3663 case TYPE_POINTER:
3664 case TYPE_ARRAY:
3665 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3666 break;
3667 case TYPE_VOID:
3668 case TYPE_ALIAS:
3669 case TYPE_MODULE:
3670 case TYPE_COCLASS:
3671 case TYPE_FUNCTION:
3672 case TYPE_INTERFACE:
3673 assert(0);
3674 break;
3676 else
3677 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3678 break;
3680 case TGT_IFACE_POINTER:
3681 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3682 break;
3683 case TGT_INVALID:
3684 assert(0);
3685 break;
3687 fprintf(file, "\n");
3690 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3691 enum pass pass, enum remoting_phase phase)
3693 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3695 unsigned int size = get_function_buffer_size( func, pass );
3696 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3699 if (pass == PASS_RETURN)
3701 var_t var;
3702 var = *func;
3703 var.type = type_function_get_rettype(func->type);
3704 var.name = xstrdup( "_RetVal" );
3705 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3706 free( var.name );
3708 else
3710 const var_t *var;
3711 if (!type_get_function_args(func->type))
3712 return;
3713 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3714 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3719 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3721 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3725 unsigned int get_size_procformatstring_func(const var_t *func)
3727 const var_t *var;
3728 unsigned int size = 0;
3730 /* argument list size */
3731 if (type_get_function_args(func->type))
3732 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3733 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3735 /* return value size */
3736 if (is_void(type_function_get_rettype(func->type)))
3737 size += 2; /* FC_END and FC_PAD */
3738 else
3739 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3741 return size;
3744 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3746 const statement_t *stmt;
3747 unsigned int size = 1;
3749 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3751 const type_t *iface;
3752 const statement_t *stmt_func;
3754 if (stmt->type == STMT_LIBRARY)
3756 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3757 continue;
3759 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3760 continue;
3762 iface = stmt->u.type;
3763 if (!pred(iface))
3764 continue;
3766 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3768 const var_t *func = stmt_func->u.var;
3769 if (!is_local(func->attrs))
3770 size += get_size_procformatstring_func( func );
3773 return size;
3776 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3778 set_all_tfswrite(FALSE);
3779 return process_tfs(NULL, stmts, pred);
3782 void declare_stub_args( FILE *file, int indent, const var_t *func )
3784 int in_attr, out_attr;
3785 int i = 0;
3786 const var_t *var;
3788 /* declare return value '_RetVal' */
3789 if (!is_void(type_function_get_rettype(func->type)))
3791 print_file(file, indent, "%s", "");
3792 write_type_decl_left(file, type_function_get_rettype(func->type));
3793 fprintf(file, " _RetVal;\n");
3796 if (!type_get_function_args(func->type))
3797 return;
3799 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3801 in_attr = is_attr(var->attrs, ATTR_IN);
3802 out_attr = is_attr(var->attrs, ATTR_OUT);
3803 if (!out_attr && !in_attr)
3804 in_attr = 1;
3806 if (is_context_handle(var->type))
3807 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3808 else
3810 if (!in_attr && !is_conformant_array(var->type))
3812 type_t *type_to_print;
3813 char name[16];
3814 print_file(file, indent, "%s", "");
3815 if (type_get_type(var->type) == TYPE_ARRAY &&
3816 !type_array_is_decl_as_ptr(var->type))
3817 type_to_print = var->type;
3818 else
3819 type_to_print = type_pointer_get_ref(var->type);
3820 sprintf(name, "_W%u", i++);
3821 write_type_decl(file, type_to_print, name);
3822 fprintf(file, ";\n");
3825 print_file(file, indent, "%s", "");
3826 write_type_decl_left(file, var->type);
3827 fprintf(file, " ");
3828 if (type_get_type(var->type) == TYPE_ARRAY &&
3829 !type_array_is_decl_as_ptr(var->type)) {
3830 fprintf(file, "(*%s)", var->name);
3831 } else
3832 fprintf(file, "%s", var->name);
3833 write_type_right(file, var->type, FALSE);
3834 fprintf(file, ";\n");
3836 if (decl_indirect(var->type))
3837 print_file(file, indent, "void *_p_%s;\n", var->name);
3843 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3845 int in_attr, out_attr;
3846 int i = 0, sep = 0;
3847 const var_t *var;
3849 if (!type_get_function_args(func->type))
3850 return;
3852 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3854 in_attr = is_attr(var->attrs, ATTR_IN);
3855 out_attr = is_attr(var->attrs, ATTR_OUT);
3856 if (!out_attr && !in_attr)
3857 in_attr = 1;
3859 if (!in_attr)
3861 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3863 if (is_context_handle(var->type))
3865 fprintf(file, " = NdrContextHandleInitialize(\n");
3866 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3867 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3868 var->type->typestring_offset);
3870 else if (is_array(var->type) &&
3871 type_array_has_conformance(var->type))
3873 unsigned int size, align = 0;
3874 type_t *type = var->type;
3876 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3877 for ( ;
3878 is_array(type) && type_array_has_conformance(type);
3879 type = type_array_get_element(type))
3881 write_expr(file, type_array_get_conformance(type), TRUE,
3882 TRUE, NULL, NULL, local_var_prefix);
3883 fprintf(file, " * ");
3885 size = type_memsize(type, &align);
3886 fprintf(file, "%u);\n", size);
3888 else
3890 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3891 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3893 case TGT_BASIC:
3894 case TGT_ENUM:
3895 case TGT_POINTER:
3896 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3897 break;
3898 case TGT_STRUCT:
3899 case TGT_UNION:
3900 case TGT_USER_TYPE:
3901 case TGT_IFACE_POINTER:
3902 case TGT_ARRAY:
3903 case TGT_CTXT_HANDLE:
3904 case TGT_CTXT_HANDLE_POINTER:
3905 case TGT_INVALID:
3906 case TGT_STRING:
3907 /* not initialised */
3908 break;
3910 i++;
3913 sep = 1;
3916 if (sep)
3917 fprintf(file, "\n");
3921 int write_expr_eval_routines(FILE *file, const char *iface)
3923 static const char *var_name = "pS";
3924 static const char *var_name_expr = "pS->";
3925 int result = 0;
3926 struct expr_eval_routine *eval;
3927 unsigned short callback_offset = 0;
3929 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3931 const char *name = eval->structure->name;
3932 result = 1;
3934 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3935 iface, name, callback_offset);
3936 print_file(file, 0, "{\n");
3937 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3938 name, var_name, name, eval->baseoff);
3939 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3940 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3941 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3942 fprintf(file, ";\n");
3943 print_file(file, 0, "}\n\n");
3944 callback_offset++;
3946 return result;
3949 void write_expr_eval_routine_list(FILE *file, const char *iface)
3951 struct expr_eval_routine *eval;
3952 struct expr_eval_routine *cursor;
3953 unsigned short callback_offset = 0;
3955 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3956 fprintf(file, "{\n");
3958 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
3960 const char *name = eval->structure->name;
3961 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3962 callback_offset++;
3963 list_remove(&eval->entry);
3964 free(eval);
3967 fprintf(file, "};\n\n");
3970 void write_user_quad_list(FILE *file)
3972 user_type_t *ut;
3974 if (list_empty(&user_type_list))
3975 return;
3977 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3978 fprintf(file, "{\n");
3979 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
3981 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
3982 print_file(file, 1, "{\n");
3983 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
3984 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
3985 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
3986 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3987 print_file(file, 1, "}%s\n", sep);
3989 fprintf(file, "};\n\n");
3992 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
3994 const struct str_list_entry_t *endpoint;
3995 const char *p;
3997 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3998 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
3999 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4001 print_file( f, 1, "{ (const unsigned char *)\"" );
4002 for (p = endpoint->str; *p && *p != ':'; p++)
4004 if (*p == '"' || *p == '\\') fputc( '\\', f );
4005 fputc( *p, f );
4007 if (!*p) goto error;
4008 if (p[1] != '[') goto error;
4010 fprintf( f, "\", (const unsigned char *)\"" );
4011 for (p += 2; *p && *p != ']'; p++)
4013 if (*p == '"' || *p == '\\') fputc( '\\', f );
4014 fputc( *p, f );
4016 if (*p != ']') goto error;
4017 fprintf( f, "\" },\n" );
4019 print_file( f, 0, "};\n\n" );
4020 return;
4022 error:
4023 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4026 void write_exceptions( FILE *file )
4028 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4029 fprintf( file, "\n");
4030 fprintf( file, "#include \"wine/exception.h\"\n");
4031 fprintf( file, "#undef RpcTryExcept\n");
4032 fprintf( file, "#undef RpcExcept\n");
4033 fprintf( file, "#undef RpcEndExcept\n");
4034 fprintf( file, "#undef RpcTryFinally\n");
4035 fprintf( file, "#undef RpcFinally\n");
4036 fprintf( file, "#undef RpcEndFinally\n");
4037 fprintf( file, "#undef RpcExceptionCode\n");
4038 fprintf( file, "#undef RpcAbnormalTermination\n");
4039 fprintf( file, "\n");
4040 fprintf( file, "struct __exception_frame;\n");
4041 fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
4042 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4043 fprintf( file, "\n");
4044 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4045 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4046 fprintf( file, " __filter_func filter; \\\n");
4047 fprintf( file, " __finally_func finally; \\\n");
4048 fprintf( file, " sigjmp_buf jmp; \\\n");
4049 fprintf( file, " DWORD code; \\\n");
4050 fprintf( file, " unsigned char abnormal_termination; \\\n");
4051 fprintf( file, " unsigned char filter_level; \\\n");
4052 fprintf( file, " unsigned char finally_level;\n");
4053 fprintf( file, "\n");
4054 fprintf( file, "struct __exception_frame\n{\n");
4055 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4056 fprintf( file, "};\n");
4057 fprintf( file, "\n");
4058 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4059 fprintf( file, "{\n");
4060 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4061 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4062 fprintf( file, " {\n");
4063 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4064 fprintf( file, " exc_frame->finally( exc_frame );\n");
4065 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4066 fprintf( file, " }\n");
4067 fprintf( file, " exc_frame->filter_level = 0;\n");
4068 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4069 fprintf( file, "}\n");
4070 fprintf( file, "\n");
4071 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4072 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4073 fprintf( file, " CONTEXT *context,\n");
4074 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4075 fprintf( file, "{\n");
4076 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4077 fprintf( file, "\n");
4078 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4079 fprintf( file, " {\n" );
4080 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4081 fprintf( file, " {\n" );
4082 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4083 fprintf( file, " exc_frame->finally( exc_frame );\n");
4084 fprintf( file, " }\n" );
4085 fprintf( file, " return ExceptionContinueSearch;\n");
4086 fprintf( file, " }\n" );
4087 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4088 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4089 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4090 fprintf( file, " return ExceptionContinueSearch;\n");
4091 fprintf( file, "}\n");
4092 fprintf( file, "\n");
4093 fprintf( file, "#define RpcTryExcept \\\n");
4094 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4095 fprintf( file, " { \\\n");
4096 fprintf( file, " if (!__frame->finally_level) \\\n" );
4097 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4098 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4099 fprintf( file, "\n");
4100 fprintf( file, "#define RpcExcept(expr) \\\n");
4101 fprintf( file, " if (!__frame->finally_level) \\\n" );
4102 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4103 fprintf( file, " __frame->filter_level = 0; \\\n" );
4104 fprintf( file, " } \\\n");
4105 fprintf( file, " else \\\n");
4106 fprintf( file, "\n");
4107 fprintf( file, "#define RpcEndExcept\n");
4108 fprintf( file, "\n");
4109 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4110 fprintf( file, "\n");
4111 fprintf( file, "#define RpcTryFinally \\\n");
4112 fprintf( file, " if (!__frame->filter_level) \\\n");
4113 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4114 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4115 fprintf( file, "\n");
4116 fprintf( file, "#define RpcFinally \\\n");
4117 fprintf( file, " if (!__frame->filter_level) \\\n");
4118 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4119 fprintf( file, " __frame->finally_level = 0;\n");
4120 fprintf( file, "\n");
4121 fprintf( file, "#define RpcEndFinally\n");
4122 fprintf( file, "\n");
4123 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4124 fprintf( file, "\n");
4125 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4126 fprintf( file, " do { \\\n");
4127 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4128 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4129 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4130 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4131 fprintf( file, " __frame->filter_level = 0; \\\n");
4132 fprintf( file, " __frame->finally_level = 0; \\\n");
4133 fprintf( file, " } while (0)\n");
4134 fprintf( file, "\n");
4135 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4136 fprintf( file, "\n");
4137 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4138 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4139 fprintf( file, "\n");
4140 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4141 fprintf( file, " DWORD code;\n");
4142 fprintf( file, "\n");
4143 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");