Release 1.1.37.
[wine/gsoc-2012-control.git] / tools / widl / typegen.c
blobc35020c90ad59c738e2756e110602f2b78eca780
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 static 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 case RPC_FC_INT3264: return "FC_INT3264";
120 case RPC_FC_UINT3264: return "FC_UINT3264";
121 default:
122 error("string_of_type: unknown type 0x%02x\n", type);
123 return NULL;
127 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
129 const type_t *t = type;
130 for (;;)
132 if (is_attr(t->attrs, attr))
133 return get_attrp(t->attrs, attr);
134 else if (type_is_alias(t))
135 t = type_alias_get_aliasee(t);
136 else return NULL;
140 unsigned char get_basic_fc(const type_t *type)
142 int sign = type_basic_get_sign(type);
143 switch (type_basic_get_type(type))
145 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
146 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
147 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
148 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
149 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
150 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
151 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
152 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
153 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
154 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
155 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
156 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
157 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
158 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
160 return 0;
163 static inline unsigned int clamp_align(unsigned int align)
165 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
166 if(align > packing) align = packing;
167 return align;
170 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
172 const type_t *t;
173 int pointer_type;
175 assert(is_ptr(type) || is_array(type));
177 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
178 if (pointer_type)
179 return pointer_type;
181 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
183 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
184 if (pointer_type)
185 return pointer_type;
188 if (toplevel_param)
189 return RPC_FC_RP;
190 else if (is_ptr(type))
191 return type_pointer_get_default_fc(type);
192 else
193 return type_array_get_ptr_default_fc(type);
196 static unsigned char get_enum_fc(const type_t *type)
198 assert(type_get_type(type) == TYPE_ENUM);
199 if (is_aliaschain_attr(type, ATTR_V1ENUM))
200 return RPC_FC_ENUM32;
201 else
202 return RPC_FC_ENUM16;
205 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
207 if (is_user_type(type))
208 return TGT_USER_TYPE;
210 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
211 return TGT_CTXT_HANDLE;
213 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
214 return TGT_STRING;
216 switch (type_get_type(type))
218 case TYPE_BASIC:
219 if (!(flags & TDT_IGNORE_RANGES) &&
220 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
221 return TGT_RANGE;
222 return TGT_BASIC;
223 case TYPE_ENUM:
224 if (!(flags & TDT_IGNORE_RANGES) &&
225 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
226 return TGT_RANGE;
227 return TGT_ENUM;
228 case TYPE_POINTER:
229 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
230 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
231 return TGT_IFACE_POINTER;
232 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
233 return TGT_CTXT_HANDLE_POINTER;
234 else
235 return TGT_POINTER;
236 case TYPE_STRUCT:
237 return TGT_STRUCT;
238 case TYPE_ENCAPSULATED_UNION:
239 case TYPE_UNION:
240 return TGT_UNION;
241 case TYPE_ARRAY:
242 return TGT_ARRAY;
243 case TYPE_FUNCTION:
244 case TYPE_COCLASS:
245 case TYPE_INTERFACE:
246 case TYPE_MODULE:
247 case TYPE_VOID:
248 case TYPE_ALIAS:
249 case TYPE_BITFIELD:
250 break;
252 return TGT_INVALID;
255 unsigned char get_struct_fc(const type_t *type)
257 int has_pointer = 0;
258 int has_conformance = 0;
259 int has_variance = 0;
260 var_t *field;
261 var_list_t *fields;
263 fields = type_struct_get_fields(type);
265 if (get_padding(fields))
266 return RPC_FC_BOGUS_STRUCT;
268 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
270 type_t *t = field->type;
271 enum typegen_type typegen_type;
273 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
275 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
277 if (is_string_type(field->attrs, field->type))
279 if (is_conformant_array(t))
280 has_conformance = 1;
281 has_variance = 1;
282 continue;
285 if (is_array(type_array_get_element(field->type)))
286 return RPC_FC_BOGUS_STRUCT;
288 if (type_array_has_conformance(field->type))
290 has_conformance = 1;
291 if (list_next(fields, &field->entry))
292 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
293 field->name);
295 if (type_array_has_variance(t))
296 has_variance = 1;
298 t = type_array_get_element(t);
299 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
302 switch (typegen_type)
304 case TGT_USER_TYPE:
305 case TGT_IFACE_POINTER:
306 return RPC_FC_BOGUS_STRUCT;
307 case TGT_BASIC:
308 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
309 return RPC_FC_BOGUS_STRUCT;
310 break;
311 case TGT_ENUM:
312 if (get_enum_fc(t) == RPC_FC_ENUM16)
313 return RPC_FC_BOGUS_STRUCT;
314 break;
315 case TGT_POINTER:
316 case TGT_ARRAY:
317 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
318 return RPC_FC_BOGUS_STRUCT;
319 has_pointer = 1;
320 break;
321 case TGT_UNION:
322 return RPC_FC_BOGUS_STRUCT;
323 case TGT_STRUCT:
325 unsigned char fc = get_struct_fc(t);
326 switch (fc)
328 case RPC_FC_STRUCT:
329 break;
330 case RPC_FC_CVSTRUCT:
331 has_conformance = 1;
332 has_variance = 1;
333 has_pointer = 1;
334 break;
336 case RPC_FC_CPSTRUCT:
337 has_conformance = 1;
338 if (list_next( fields, &field->entry ))
339 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
340 field->name);
341 has_pointer = 1;
342 break;
344 case RPC_FC_CSTRUCT:
345 has_conformance = 1;
346 if (list_next( fields, &field->entry ))
347 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
348 field->name);
349 break;
351 case RPC_FC_PSTRUCT:
352 has_pointer = 1;
353 break;
355 default:
356 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
357 /* fallthru - treat it as complex */
359 /* as soon as we see one of these these members, it's bogus... */
360 case RPC_FC_BOGUS_STRUCT:
361 return RPC_FC_BOGUS_STRUCT;
363 break;
365 case TGT_RANGE:
366 return RPC_FC_BOGUS_STRUCT;
367 case TGT_STRING:
368 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
369 case TGT_INVALID:
370 case TGT_CTXT_HANDLE:
371 case TGT_CTXT_HANDLE_POINTER:
372 /* checking after parsing should mean that we don't get here. if we do,
373 * it's a checker bug */
374 assert(0);
378 if( has_variance )
380 if ( has_conformance )
381 return RPC_FC_CVSTRUCT;
382 else
383 return RPC_FC_BOGUS_STRUCT;
385 if( has_conformance && has_pointer )
386 return RPC_FC_CPSTRUCT;
387 if( has_conformance )
388 return RPC_FC_CSTRUCT;
389 if( has_pointer )
390 return RPC_FC_PSTRUCT;
391 return RPC_FC_STRUCT;
394 static unsigned char get_array_fc(const type_t *type)
396 unsigned char fc;
397 const expr_t *size_is;
398 const type_t *elem_type;
400 elem_type = type_array_get_element(type);
401 size_is = type_array_get_conformance(type);
403 if (!size_is)
405 unsigned int align = 0;
406 unsigned int size = type_memsize(elem_type, &align);
407 if (size * type_array_get_dim(type) > 0xffffuL)
408 fc = RPC_FC_LGFARRAY;
409 else
410 fc = RPC_FC_SMFARRAY;
412 else
413 fc = RPC_FC_CARRAY;
415 if (type_array_has_variance(type))
417 if (fc == RPC_FC_SMFARRAY)
418 fc = RPC_FC_SMVARRAY;
419 else if (fc == RPC_FC_LGFARRAY)
420 fc = RPC_FC_LGVARRAY;
421 else if (fc == RPC_FC_CARRAY)
422 fc = RPC_FC_CVARRAY;
425 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
427 case TGT_USER_TYPE:
428 fc = RPC_FC_BOGUS_ARRAY;
429 break;
430 case TGT_BASIC:
431 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
432 pointer_size != 4)
433 fc = RPC_FC_BOGUS_ARRAY;
434 break;
435 case TGT_STRUCT:
436 switch (get_struct_fc(elem_type))
438 case RPC_FC_BOGUS_STRUCT:
439 fc = RPC_FC_BOGUS_ARRAY;
440 break;
442 break;
443 case TGT_ENUM:
444 /* is 16-bit enum - if so, wire size differs from mem size and so
445 * the array cannot be block copied, which means the array is complex */
446 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
447 fc = RPC_FC_BOGUS_ARRAY;
448 break;
449 case TGT_UNION:
450 case TGT_IFACE_POINTER:
451 fc = RPC_FC_BOGUS_ARRAY;
452 break;
453 case TGT_POINTER:
454 /* ref pointers cannot just be block copied. unique pointers to
455 * interfaces need special treatment. either case means the array is
456 * complex */
457 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
458 fc = RPC_FC_BOGUS_ARRAY;
459 break;
460 case TGT_RANGE:
461 fc = RPC_FC_BOGUS_ARRAY;
462 break;
463 case TGT_CTXT_HANDLE:
464 case TGT_CTXT_HANDLE_POINTER:
465 case TGT_STRING:
466 case TGT_INVALID:
467 case TGT_ARRAY:
468 /* nothing to do for everything else */
469 break;
472 return fc;
475 int is_struct(unsigned char type)
477 switch (type)
479 case RPC_FC_STRUCT:
480 case RPC_FC_PSTRUCT:
481 case RPC_FC_CSTRUCT:
482 case RPC_FC_CPSTRUCT:
483 case RPC_FC_CVSTRUCT:
484 case RPC_FC_BOGUS_STRUCT:
485 return 1;
486 default:
487 return 0;
491 static int is_non_complex_struct(const type_t *type)
493 return (type_get_type(type) == TYPE_STRUCT &&
494 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
497 static int type_has_pointers(const type_t *type)
499 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
501 case TGT_USER_TYPE:
502 return FALSE;
503 case TGT_POINTER:
504 return TRUE;
505 case TGT_ARRAY:
506 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
507 case TGT_STRUCT:
509 var_list_t *fields = type_struct_get_fields(type);
510 const var_t *field;
511 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
513 if (type_has_pointers(field->type))
514 return TRUE;
516 break;
518 case TGT_UNION:
520 var_list_t *fields;
521 const var_t *field;
522 fields = type_union_get_cases(type);
523 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
525 if (field->type && type_has_pointers(field->type))
526 return TRUE;
528 break;
530 case TGT_CTXT_HANDLE:
531 case TGT_CTXT_HANDLE_POINTER:
532 case TGT_STRING:
533 case TGT_IFACE_POINTER:
534 case TGT_BASIC:
535 case TGT_ENUM:
536 case TGT_RANGE:
537 case TGT_INVALID:
538 break;
541 return FALSE;
544 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
545 int toplevel_param)
547 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
549 case TGT_USER_TYPE:
550 return FALSE;
551 case TGT_POINTER:
552 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
553 return TRUE;
554 else
555 return FALSE;
556 case TGT_ARRAY:
557 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
558 return TRUE;
559 else
560 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
561 case TGT_STRUCT:
563 var_list_t *fields = type_struct_get_fields(type);
564 const var_t *field;
565 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
567 if (type_has_full_pointer(field->type, field->attrs, FALSE))
568 return TRUE;
570 break;
572 case TGT_UNION:
574 var_list_t *fields;
575 const var_t *field;
576 fields = type_union_get_cases(type);
577 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
579 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
580 return TRUE;
582 break;
584 case TGT_CTXT_HANDLE:
585 case TGT_CTXT_HANDLE_POINTER:
586 case TGT_STRING:
587 case TGT_IFACE_POINTER:
588 case TGT_BASIC:
589 case TGT_ENUM:
590 case TGT_RANGE:
591 case TGT_INVALID:
592 break;
595 return FALSE;
598 static unsigned short user_type_offset(const char *name)
600 user_type_t *ut;
601 unsigned short off = 0;
602 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
604 if (strcmp(name, ut->name) == 0)
605 return off;
606 ++off;
608 error("user_type_offset: couldn't find type (%s)\n", name);
609 return 0;
612 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
614 type->typestring_offset = offset;
615 if (file) type->tfswrite = FALSE;
618 static void guard_rec(type_t *type)
620 /* types that contain references to themselves (like a linked list),
621 need to be shielded from infinite recursion when writing embedded
622 types */
623 if (type->typestring_offset)
624 type->tfswrite = FALSE;
625 else
626 type->typestring_offset = 1;
629 static type_t *get_user_type(const type_t *t, const char **pname)
631 for (;;)
633 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
634 if (ut)
636 if (pname)
637 *pname = t->name;
638 return ut;
641 if (type_is_alias(t))
642 t = type_alias_get_aliasee(t);
643 else
644 return NULL;
648 int is_user_type(const type_t *t)
650 return get_user_type(t, NULL) != NULL;
653 static int is_embedded_complex(const type_t *type)
655 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
657 case TGT_USER_TYPE:
658 case TGT_STRUCT:
659 case TGT_UNION:
660 case TGT_ARRAY:
661 case TGT_IFACE_POINTER:
662 return TRUE;
663 default:
664 return FALSE;
668 static const char *get_context_handle_type_name(const type_t *type)
670 const type_t *t;
671 for (t = type;
672 is_ptr(t) || type_is_alias(t);
673 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
674 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
675 return t->name;
676 assert(0);
677 return NULL;
680 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
681 do { \
682 if (file) \
683 fprintf(file, "/* %2u */\n", typestring_offset); \
684 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
686 while (0)
688 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
689 static void print_file(FILE *file, int indent, const char *format, ...)
691 va_list va;
692 va_start(va, format);
693 print(file, indent, format, va);
694 va_end(va);
697 void print(FILE *file, int indent, const char *format, va_list va)
699 if (file)
701 if (format[0] != '\n')
702 while (0 < indent--)
703 fprintf(file, " ");
704 vfprintf(file, format, va);
709 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
711 if (decl_indirect(t))
713 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
714 local_var_prefix, n, local_var_prefix, n);
715 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
717 else if (is_ptr(t) || is_array(t))
718 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
721 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
723 const var_t *var;
725 if (!is_void(type_function_get_rettype(func->type)))
726 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
728 if (!type_get_function_args(func->type))
729 return;
731 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
732 write_var_init(file, indent, var->type, var->name, local_var_prefix);
734 fprintf(file, "\n");
737 static void write_formatdesc(FILE *f, int indent, const char *str)
739 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
740 print_file(f, indent, "{\n");
741 print_file(f, indent + 1, "short Pad;\n");
742 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
743 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
744 print_file(f, indent, "\n");
747 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
749 clear_all_offsets();
751 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
752 get_size_typeformatstring(stmts, pred));
754 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
755 get_size_procformatstring(stmts, pred));
757 fprintf(f, "\n");
758 write_formatdesc(f, indent, "TYPE");
759 write_formatdesc(f, indent, "PROC");
760 fprintf(f, "\n");
761 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
762 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
763 print_file(f, indent, "\n");
766 int decl_indirect(const type_t *t)
768 if (is_user_type(t))
769 return TRUE;
770 return (type_get_type(t) != TYPE_BASIC &&
771 type_get_type(t) != TYPE_ENUM &&
772 type_get_type(t) != TYPE_POINTER &&
773 type_get_type(t) != TYPE_ARRAY);
776 static unsigned int write_procformatstring_type(FILE *file, int indent,
777 const char *name,
778 const type_t *type,
779 const attr_list_t *attrs,
780 int is_return)
782 unsigned int size;
784 int is_in = is_attr(attrs, ATTR_IN);
785 int is_out = is_attr(attrs, ATTR_OUT);
787 if (!is_in && !is_out) is_in = TRUE;
789 if (type_get_type(type) == TYPE_BASIC ||
790 type_get_type(type) == TYPE_ENUM)
792 unsigned char fc;
794 if (is_return)
795 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
796 else
797 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
799 if (type_get_type(type) == TYPE_ENUM)
801 fc = get_enum_fc(type);
803 else
805 fc = get_basic_fc(type);
807 if (fc == RPC_FC_BIND_PRIMITIVE)
808 fc = RPC_FC_IGNORE;
811 print_file(file, indent, "0x%02x, /* %s */\n",
812 fc, string_of_type(fc));
813 size = 2; /* includes param type prefix */
815 else
817 if (is_return)
818 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
819 else if (is_in && is_out)
820 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
821 else if (is_out)
822 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
823 else
824 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
826 print_file(file, indent, "0x01,\n");
827 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
828 size = 4; /* includes param type prefix */
830 return size;
833 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
835 const statement_t *stmt;
836 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
838 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
840 const statement_t *stmt_func;
841 if (!pred(stmt->u.type))
842 continue;
843 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
845 const var_t *func = stmt_func->u.var;
846 if (is_local(func->attrs)) continue;
847 /* emit argument data */
848 if (type_get_function_args(func->type))
850 const var_t *var;
851 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
852 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
855 /* emit return value data */
856 if (is_void(type_function_get_rettype(func->type)))
858 print_file(file, indent, "0x5b, /* FC_END */\n");
859 print_file(file, indent, "0x5c, /* FC_PAD */\n");
861 else
862 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
865 else if (stmt->type == STMT_LIBRARY)
866 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
870 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
872 int indent = 0;
874 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
875 print_file(file, indent, "{\n");
876 indent++;
877 print_file(file, indent, "0,\n");
878 print_file(file, indent, "{\n");
879 indent++;
881 write_procformatstring_stmts(file, indent, stmts, pred);
883 print_file(file, indent, "0x0\n");
884 indent--;
885 print_file(file, indent, "}\n");
886 indent--;
887 print_file(file, indent, "};\n");
888 print_file(file, indent, "\n");
891 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
893 unsigned char fc;
895 if (type_get_type(type) == TYPE_BASIC)
896 fc = get_basic_fc(type);
897 else if (type_get_type(type) == TYPE_ENUM)
898 fc = get_enum_fc(type);
899 else
900 return 0;
902 if (convert_to_signed_type)
904 switch(fc)
906 case RPC_FC_USMALL:
907 fc = RPC_FC_SMALL;
908 break;
909 case RPC_FC_USHORT:
910 fc = RPC_FC_SHORT;
911 break;
912 case RPC_FC_ULONG:
913 fc = RPC_FC_LONG;
914 break;
918 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
919 *typestring_offset += 1;
920 return 1;
923 /* write conformance / variance descriptor */
924 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
925 unsigned int baseoff, const type_t *type,
926 const expr_t *expr)
928 unsigned char operator_type = 0;
929 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
930 const char *conftype_string = "";
931 const char *operator_string = "no operators";
932 const expr_t *subexpr;
934 if (!expr)
936 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
937 return 4;
940 if (!structure)
942 /* Top-level conformance calculations are done inline. */
943 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
944 RPC_FC_TOP_LEVEL_CONFORMANCE);
945 print_file (file, 2, "0x0,\n");
946 print_file (file, 2, "NdrFcShort(0x0),\n");
947 return 4;
950 if (expr->is_const)
952 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
953 error("write_conf_or_var_desc: constant value %ld is greater than "
954 "the maximum constant size of %d\n", expr->cval,
955 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
957 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
958 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
959 print_file(file, 2, "0x%lx,\n", expr->cval >> 16);
960 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
962 return 4;
965 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
967 conftype = RPC_FC_POINTER_CONFORMANCE;
968 conftype_string = "field pointer, ";
971 subexpr = expr;
972 switch (subexpr->type)
974 case EXPR_PPTR:
975 subexpr = subexpr->ref;
976 operator_type = RPC_FC_DEREFERENCE;
977 operator_string = "FC_DEREFERENCE";
978 break;
979 case EXPR_DIV:
980 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
982 subexpr = subexpr->ref;
983 operator_type = RPC_FC_DIV_2;
984 operator_string = "FC_DIV_2";
986 break;
987 case EXPR_MUL:
988 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
990 subexpr = subexpr->ref;
991 operator_type = RPC_FC_MULT_2;
992 operator_string = "FC_MULT_2";
994 break;
995 case EXPR_SUB:
996 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
998 subexpr = subexpr->ref;
999 operator_type = RPC_FC_SUB_1;
1000 operator_string = "FC_SUB_1";
1002 break;
1003 case EXPR_ADD:
1004 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1006 subexpr = subexpr->ref;
1007 operator_type = RPC_FC_ADD_1;
1008 operator_string = "FC_ADD_1";
1010 break;
1011 default:
1012 break;
1015 if (subexpr->type == EXPR_IDENTIFIER)
1017 const type_t *correlation_variable = NULL;
1018 unsigned char param_type = 0;
1019 unsigned int offset = 0;
1020 const var_t *var;
1021 var_list_t *fields = type_struct_get_fields(structure);
1023 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1025 unsigned int size = field_memsize( var->type, &offset );
1026 if (var->name && !strcmp(var->name, subexpr->u.sval))
1028 correlation_variable = var->type;
1029 break;
1031 offset += size;
1033 if (!correlation_variable)
1034 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1035 subexpr->u.sval);
1037 correlation_variable = expr_resolve_type(NULL, structure, expr);
1039 offset -= baseoff;
1041 if (type_get_type(correlation_variable) == TYPE_BASIC)
1043 switch (get_basic_fc(correlation_variable))
1045 case RPC_FC_CHAR:
1046 case RPC_FC_SMALL:
1047 param_type = RPC_FC_SMALL;
1048 break;
1049 case RPC_FC_BYTE:
1050 case RPC_FC_USMALL:
1051 param_type = RPC_FC_USMALL;
1052 break;
1053 case RPC_FC_WCHAR:
1054 case RPC_FC_SHORT:
1055 param_type = RPC_FC_SHORT;
1056 break;
1057 case RPC_FC_USHORT:
1058 param_type = RPC_FC_USHORT;
1059 break;
1060 case RPC_FC_LONG:
1061 param_type = RPC_FC_LONG;
1062 break;
1063 case RPC_FC_ULONG:
1064 param_type = RPC_FC_ULONG;
1065 break;
1066 default:
1067 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1068 get_basic_fc(correlation_variable));
1071 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1073 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1074 param_type = RPC_FC_LONG;
1075 else
1076 param_type = RPC_FC_SHORT;
1078 else
1080 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1081 subexpr->u.sval);
1082 return 0;
1085 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1086 conftype | param_type, conftype_string, string_of_type(param_type));
1087 print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
1088 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1089 offset, offset);
1091 else
1093 unsigned int callback_offset = 0;
1094 struct expr_eval_routine *eval;
1095 int found = 0;
1097 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1099 if (!strcmp (eval->structure->name, structure->name)
1100 && !compare_expr (eval->expr, expr))
1102 found = 1;
1103 break;
1105 callback_offset++;
1108 if (!found)
1110 eval = xmalloc (sizeof(*eval));
1111 eval->structure = structure;
1112 eval->baseoff = baseoff;
1113 eval->expr = expr;
1114 list_add_tail (&expr_eval_routines, &eval->entry);
1117 if (callback_offset > USHRT_MAX)
1118 error("Maximum number of callback routines reached\n");
1120 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1121 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1122 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1124 return 4;
1127 /* return size and start offset of a data field based on current offset */
1128 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1130 unsigned int align = 0;
1131 unsigned int size = type_memsize( type, &align );
1133 *offset = ROUND_SIZE( *offset, align );
1134 return size;
1137 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1139 unsigned int size = 0;
1140 unsigned int max_align;
1141 const var_t *v;
1143 if (!fields) return 0;
1144 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1146 unsigned int falign = 0;
1147 unsigned int fsize = type_memsize(v->type, &falign);
1148 if (*align < falign) *align = falign;
1149 falign = clamp_align(falign);
1150 size = ROUND_SIZE(size, falign);
1151 size += fsize;
1154 max_align = clamp_align(*align);
1155 size = ROUND_SIZE(size, max_align);
1157 return size;
1160 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1162 unsigned int size, maxs = 0;
1163 unsigned int align = *pmaxa;
1164 const var_t *v;
1166 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1168 /* we could have an empty default field with NULL type */
1169 if (v->type)
1171 size = type_memsize(v->type, &align);
1172 if (maxs < size) maxs = size;
1173 if (*pmaxa < align) *pmaxa = align;
1177 return maxs;
1180 int get_padding(const var_list_t *fields)
1182 unsigned short offset = 0;
1183 unsigned int salign = 1;
1184 const var_t *f;
1186 if (!fields)
1187 return 0;
1189 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
1191 type_t *ft = f->type;
1192 unsigned int align = 0;
1193 unsigned int size = type_memsize(ft, &align);
1194 align = clamp_align(align);
1195 if (align > salign) salign = align;
1196 offset = ROUND_SIZE(offset, align);
1197 offset += size;
1200 return ROUNDING(offset, salign);
1203 unsigned int type_memsize(const type_t *t, unsigned int *align)
1205 unsigned int size = 0;
1207 switch (type_get_type(t))
1209 case TYPE_BASIC:
1210 switch (get_basic_fc(t))
1212 case RPC_FC_BYTE:
1213 case RPC_FC_CHAR:
1214 case RPC_FC_USMALL:
1215 case RPC_FC_SMALL:
1216 size = 1;
1217 if (size > *align) *align = size;
1218 break;
1219 case RPC_FC_WCHAR:
1220 case RPC_FC_USHORT:
1221 case RPC_FC_SHORT:
1222 size = 2;
1223 if (size > *align) *align = size;
1224 break;
1225 case RPC_FC_ULONG:
1226 case RPC_FC_LONG:
1227 case RPC_FC_ERROR_STATUS_T:
1228 case RPC_FC_FLOAT:
1229 size = 4;
1230 if (size > *align) *align = size;
1231 break;
1232 case RPC_FC_HYPER:
1233 case RPC_FC_DOUBLE:
1234 size = 8;
1235 if (size > *align) *align = size;
1236 break;
1237 case RPC_FC_INT3264:
1238 case RPC_FC_UINT3264:
1239 assert( pointer_size );
1240 size = pointer_size;
1241 if (size > *align) *align = size;
1242 break;
1243 default:
1244 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1245 size = 0;
1247 break;
1248 case TYPE_ENUM:
1249 switch (get_enum_fc(t))
1251 case RPC_FC_ENUM16:
1252 case RPC_FC_ENUM32:
1253 size = 4;
1254 if (size > *align) *align = size;
1255 break;
1256 default:
1257 error("type_memsize: Unknown enum type\n");
1258 size = 0;
1260 break;
1261 case TYPE_STRUCT:
1262 size = fields_memsize(type_struct_get_fields(t), align);
1263 break;
1264 case TYPE_ENCAPSULATED_UNION:
1265 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1266 break;
1267 case TYPE_UNION:
1268 size = union_memsize(type_union_get_cases(t), align);
1269 break;
1270 case TYPE_POINTER:
1271 assert( pointer_size );
1272 size = pointer_size;
1273 if (size > *align) *align = size;
1274 break;
1275 case TYPE_ARRAY:
1276 if (!type_array_is_decl_as_ptr(t))
1278 if (is_conformant_array(t))
1280 type_memsize(type_array_get_element(t), align);
1281 size = 0;
1283 else
1284 size = type_array_get_dim(t) *
1285 type_memsize(type_array_get_element(t), align);
1287 else /* declared as a pointer */
1289 assert( pointer_size );
1290 size = pointer_size;
1291 if (size > *align) *align = size;
1293 break;
1294 case TYPE_INTERFACE:
1295 case TYPE_ALIAS:
1296 case TYPE_VOID:
1297 case TYPE_COCLASS:
1298 case TYPE_MODULE:
1299 case TYPE_FUNCTION:
1300 case TYPE_BITFIELD:
1301 /* these types should not be encountered here due to language
1302 * restrictions (interface, void, coclass, module), logical
1303 * restrictions (alias - due to type_get_type call above) or
1304 * checking restrictions (function, bitfield). */
1305 assert(0);
1308 return size;
1311 int is_full_pointer_function(const var_t *func)
1313 const var_t *var;
1314 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1315 return TRUE;
1316 if (!type_get_function_args(func->type))
1317 return FALSE;
1318 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1319 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1320 return TRUE;
1321 return FALSE;
1324 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1326 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1327 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1328 fprintf(file, "\n");
1331 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1333 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1334 fprintf(file, "\n");
1337 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1338 const type_t *type,
1339 int toplevel_param,
1340 unsigned int offset,
1341 unsigned int *typeformat_offset)
1343 unsigned int start_offset = *typeformat_offset;
1344 short reloff = offset - (*typeformat_offset + 2);
1345 int in_attr, out_attr;
1346 int pointer_type;
1347 unsigned char flags = 0;
1349 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1351 in_attr = is_attr(attrs, ATTR_IN);
1352 out_attr = is_attr(attrs, ATTR_OUT);
1353 if (!in_attr && !out_attr) in_attr = 1;
1355 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1356 flags |= RPC_FC_P_ONSTACK;
1358 if (is_ptr(type) && !last_ptr(type))
1359 flags |= RPC_FC_P_DEREF;
1361 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1362 pointer_type,
1363 flags,
1364 string_of_type(pointer_type));
1365 if (file)
1367 if (flags & RPC_FC_P_ONSTACK)
1368 fprintf(file, " [allocated_on_stack]");
1369 if (flags & RPC_FC_P_DEREF)
1370 fprintf(file, " [pointer_deref]");
1371 fprintf(file, " */\n");
1374 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1375 *typeformat_offset += 4;
1377 return start_offset;
1380 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1382 unsigned char fc;
1383 unsigned char pointer_fc;
1384 const type_t *ref;
1386 /* for historical reasons, write_simple_pointer also handled string types,
1387 * but no longer does. catch bad uses of the function with this check */
1388 if (is_string_type(attrs, type))
1389 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1391 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1393 ref = type_pointer_get_ref(type);
1394 if (type_get_type(ref) == TYPE_ENUM)
1395 fc = get_enum_fc(ref);
1396 else
1397 fc = get_basic_fc(ref);
1399 print_file(file, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1400 pointer_fc, RPC_FC_P_SIMPLEPOINTER, string_of_type(pointer_fc));
1401 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1402 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1403 return 4;
1406 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1408 print_file(file, 0, "/* %u (", tfsoff);
1409 write_type_decl(file, t, NULL);
1410 print_file(file, 0, ") */\n");
1413 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1414 type_t *type, int toplevel_param,
1415 unsigned int *typestring_offset)
1417 unsigned int offset = *typestring_offset;
1418 type_t *ref = type_pointer_get_ref(type);
1420 print_start_tfs_comment(file, type, offset);
1421 update_tfsoff(type, offset, file);
1423 if (ref->typestring_offset)
1424 write_nonsimple_pointer(file, attrs, type,
1425 toplevel_param,
1426 type_pointer_get_ref(type)->typestring_offset,
1427 typestring_offset);
1428 else if (type_get_type(ref) == TYPE_BASIC ||
1429 type_get_type(ref) == TYPE_ENUM)
1430 *typestring_offset += write_simple_pointer(file, attrs, type,
1431 toplevel_param);
1433 return offset;
1436 static int processed(const type_t *type)
1438 return type->typestring_offset && !type->tfswrite;
1441 static int user_type_has_variable_size(const type_t *t)
1443 if (is_ptr(t))
1444 return TRUE;
1445 else if (type_get_type(t) == TYPE_STRUCT)
1447 switch (get_struct_fc(t))
1449 case RPC_FC_PSTRUCT:
1450 case RPC_FC_CSTRUCT:
1451 case RPC_FC_CPSTRUCT:
1452 case RPC_FC_CVSTRUCT:
1453 return TRUE;
1456 /* Note: Since this only applies to user types, we can't have a conformant
1457 array here, and strings should get filed under pointer in this case. */
1458 return FALSE;
1461 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1463 unsigned int start, absoff, flags;
1464 unsigned int align = 0, ualign = 0;
1465 const char *name = NULL;
1466 type_t *utype = get_user_type(type, &name);
1467 unsigned int usize = type_memsize(utype, &ualign);
1468 unsigned int size = type_memsize(type, &align);
1469 unsigned short funoff = user_type_offset(name);
1470 short reloff;
1472 guard_rec(type);
1474 if(user_type_has_variable_size(utype)) usize = 0;
1476 if (type_get_type(utype) == TYPE_BASIC ||
1477 type_get_type(utype) == TYPE_ENUM)
1479 unsigned char fc;
1481 if (type_get_type(utype) == TYPE_ENUM)
1482 fc = get_enum_fc(utype);
1483 else
1484 fc = get_basic_fc(utype);
1486 absoff = *tfsoff;
1487 print_start_tfs_comment(file, utype, absoff);
1488 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1489 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1490 *tfsoff += 2;
1492 else
1494 if (!processed(utype))
1495 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1496 absoff = utype->typestring_offset;
1499 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1500 flags = 0x40;
1501 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1502 flags = 0x80;
1503 else
1504 flags = 0;
1506 start = *tfsoff;
1507 update_tfsoff(type, start, file);
1508 print_start_tfs_comment(file, type, start);
1509 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1510 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1511 flags | (ualign - 1), ualign - 1, flags);
1512 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1513 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1514 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1515 *tfsoff += 8;
1516 reloff = absoff - *tfsoff;
1517 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1518 *tfsoff += 2;
1521 static void write_member_type(FILE *file, const type_t *cont,
1522 int cont_is_complex, const attr_list_t *attrs,
1523 const type_t *type, unsigned int *corroff,
1524 unsigned int *tfsoff)
1526 if (is_embedded_complex(type) && !is_conformant_array(type))
1528 unsigned int absoff;
1529 short reloff;
1531 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1533 absoff = *corroff;
1534 *corroff += 8;
1536 else
1538 absoff = type->typestring_offset;
1540 reloff = absoff - (*tfsoff + 2);
1542 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1543 /* FIXME: actually compute necessary padding */
1544 print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
1545 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1546 reloff, reloff, absoff);
1547 *tfsoff += 4;
1549 else if (is_ptr(type) || is_conformant_array(type))
1551 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1552 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1553 *tfsoff += 1;
1555 else if (!write_base_type(file, type, TRUE, tfsoff))
1556 error("Unsupported member type %d\n", type_get_type(type));
1559 static void write_array_element_type(FILE *file, const type_t *type,
1560 int cont_is_complex, unsigned int *tfsoff)
1562 type_t *elem = type_array_get_element(type);
1564 if (!is_embedded_complex(elem) && is_ptr(elem))
1566 type_t *ref = type_pointer_get_ref(elem);
1568 if (processed(ref))
1570 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1571 return;
1573 if (!is_string_type(NULL, elem) &&
1574 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1576 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1577 return;
1580 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1583 static void write_end(FILE *file, unsigned int *tfsoff)
1585 if (*tfsoff % 2 == 0)
1587 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1588 *tfsoff += 1;
1590 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1591 *tfsoff += 1;
1594 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1596 unsigned int offset = 0;
1597 var_list_t *fs = type_struct_get_fields(type);
1598 var_t *f;
1600 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1602 type_t *ft = f->type;
1603 unsigned int size = field_memsize( ft, &offset );
1604 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1606 short reloff;
1607 unsigned int absoff = ft->typestring_offset;
1608 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1609 absoff += 8; /* we already have a corr descr, skip it */
1610 reloff = absoff - (*tfsoff + 6);
1611 print_file(file, 0, "/* %d */\n", *tfsoff);
1612 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1613 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1614 write_conf_or_var_desc(file, current_structure, offset, ft,
1615 get_attrp(f->attrs, ATTR_SWITCHIS));
1616 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1617 reloff, reloff, absoff);
1618 *tfsoff += 8;
1620 offset += size;
1624 static int write_pointer_description_offsets(
1625 FILE *file, const attr_list_t *attrs, type_t *type,
1626 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1627 unsigned int *typestring_offset)
1629 int written = 0;
1630 unsigned int align;
1632 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1633 (is_array(type) && type_array_is_decl_as_ptr(type)))
1635 if (offset_in_memory && offset_in_buffer)
1637 unsigned int memsize;
1639 /* pointer instance */
1640 /* FIXME: sometimes from end of structure, sometimes from beginning */
1641 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1642 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1644 align = 0;
1645 memsize = type_memsize(type, &align);
1646 *offset_in_memory += memsize;
1647 /* increment these separately as in the case of conformant (varying)
1648 * structures these start at different values */
1649 *offset_in_buffer += memsize;
1651 *typestring_offset += 4;
1653 if (is_ptr(type))
1655 type_t *ref = type_pointer_get_ref(type);
1657 if (is_string_type(attrs, type))
1658 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1659 else if (processed(ref))
1660 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1661 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1662 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1663 else
1664 error("write_pointer_description_offsets: type format string unknown\n");
1666 else
1668 unsigned int offset = type->typestring_offset;
1669 /* skip over the pointer that is written for strings, since a
1670 * pointer has to be written in-place here */
1671 if (is_string_type(attrs, type))
1672 offset += 4;
1673 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1676 return 1;
1679 if (is_array(type))
1681 return write_pointer_description_offsets(
1682 file, attrs, type_array_get_element(type), offset_in_memory,
1683 offset_in_buffer, typestring_offset);
1685 else if (is_non_complex_struct(type))
1687 /* otherwise search for interesting fields to parse */
1688 const var_t *v;
1689 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1691 if (offset_in_memory && offset_in_buffer)
1693 unsigned int padding;
1694 align = 0;
1695 type_memsize(v->type, &align);
1696 padding = ROUNDING(*offset_in_memory, align);
1697 *offset_in_memory += padding;
1698 *offset_in_buffer += padding;
1700 written += write_pointer_description_offsets(
1701 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1702 typestring_offset);
1705 else
1707 if (offset_in_memory && offset_in_buffer)
1709 unsigned int memsize;
1710 align = 0;
1711 memsize = type_memsize(type, &align);
1712 *offset_in_memory += memsize;
1713 /* increment these separately as in the case of conformant (varying)
1714 * structures these start at different values */
1715 *offset_in_buffer += memsize;
1719 return written;
1722 static int write_no_repeat_pointer_descriptions(
1723 FILE *file, const attr_list_t *attrs, type_t *type,
1724 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1725 unsigned int *typestring_offset)
1727 int written = 0;
1728 unsigned int align;
1730 if (is_ptr(type) ||
1731 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1733 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1734 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1735 *typestring_offset += 2;
1737 return write_pointer_description_offsets(file, attrs, type,
1738 offset_in_memory, offset_in_buffer, typestring_offset);
1741 if (is_non_complex_struct(type))
1743 const var_t *v;
1744 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1746 if (offset_in_memory && offset_in_buffer)
1748 unsigned int padding;
1749 align = 0;
1750 type_memsize(v->type, &align);
1751 padding = ROUNDING(*offset_in_memory, align);
1752 *offset_in_memory += padding;
1753 *offset_in_buffer += padding;
1755 written += write_no_repeat_pointer_descriptions(
1756 file, v->attrs, v->type,
1757 offset_in_memory, offset_in_buffer, typestring_offset);
1760 else
1762 unsigned int memsize;
1763 align = 0;
1764 memsize = type_memsize(type, &align);
1765 *offset_in_memory += memsize;
1766 /* increment these separately as in the case of conformant (varying)
1767 * structures these start at different values */
1768 *offset_in_buffer += memsize;
1771 return written;
1774 /* Note: if file is NULL return value is number of pointers to write, else
1775 * it is the number of type format characters written */
1776 static int write_fixed_array_pointer_descriptions(
1777 FILE *file, const attr_list_t *attrs, type_t *type,
1778 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1779 unsigned int *typestring_offset)
1781 unsigned int align;
1782 int pointer_count = 0;
1784 if (type_get_type(type) == TYPE_ARRAY &&
1785 !type_array_has_conformance(type) && !type_array_has_variance(type))
1787 unsigned int temp = 0;
1788 /* unfortunately, this needs to be done in two passes to avoid
1789 * writing out redundant FC_FIXED_REPEAT descriptions */
1790 pointer_count = write_pointer_description_offsets(
1791 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1792 if (pointer_count > 0)
1794 unsigned int increment_size;
1795 unsigned int offset_of_array_pointer_mem = 0;
1796 unsigned int offset_of_array_pointer_buf = 0;
1798 align = 0;
1799 increment_size = type_memsize(type_array_get_element(type), &align);
1801 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1802 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1803 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1804 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1805 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1806 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1807 *typestring_offset += 10;
1809 pointer_count = write_pointer_description_offsets(
1810 file, attrs, type, &offset_of_array_pointer_mem,
1811 &offset_of_array_pointer_buf, typestring_offset);
1814 else if (type_get_type(type) == TYPE_STRUCT)
1816 const var_t *v;
1817 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1819 if (offset_in_memory && offset_in_buffer)
1821 unsigned int padding;
1822 align = 0;
1823 type_memsize(v->type, &align);
1824 padding = ROUNDING(*offset_in_memory, align);
1825 *offset_in_memory += padding;
1826 *offset_in_buffer += padding;
1828 pointer_count += write_fixed_array_pointer_descriptions(
1829 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1830 typestring_offset);
1833 else
1835 if (offset_in_memory && offset_in_buffer)
1837 unsigned int memsize;
1838 align = 0;
1839 memsize = type_memsize(type, &align);
1840 *offset_in_memory += memsize;
1841 /* increment these separately as in the case of conformant (varying)
1842 * structures these start at different values */
1843 *offset_in_buffer += memsize;
1847 return pointer_count;
1850 /* Note: if file is NULL return value is number of pointers to write, else
1851 * it is the number of type format characters written */
1852 static int write_conformant_array_pointer_descriptions(
1853 FILE *file, const attr_list_t *attrs, type_t *type,
1854 unsigned int offset_in_memory, unsigned int *typestring_offset)
1856 unsigned int align;
1857 int pointer_count = 0;
1859 if (is_conformant_array(type) && !type_array_has_variance(type))
1861 unsigned int temp = 0;
1862 /* unfortunately, this needs to be done in two passes to avoid
1863 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1864 pointer_count = write_pointer_description_offsets(
1865 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1866 if (pointer_count > 0)
1868 unsigned int increment_size;
1869 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1870 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1872 align = 0;
1873 increment_size = type_memsize(type_array_get_element(type), &align);
1875 if (increment_size > USHRT_MAX)
1876 error("array size of %u bytes is too large\n", increment_size);
1878 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1879 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1880 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1881 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1882 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1883 *typestring_offset += 8;
1885 pointer_count = write_pointer_description_offsets(
1886 file, attrs, type_array_get_element(type),
1887 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1888 typestring_offset);
1892 return pointer_count;
1895 /* Note: if file is NULL return value is number of pointers to write, else
1896 * it is the number of type format characters written */
1897 static int write_varying_array_pointer_descriptions(
1898 FILE *file, const attr_list_t *attrs, type_t *type,
1899 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1900 unsigned int *typestring_offset)
1902 unsigned int align;
1903 int pointer_count = 0;
1905 if (is_array(type) && type_array_has_variance(type))
1907 unsigned int temp = 0;
1908 /* unfortunately, this needs to be done in two passes to avoid
1909 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1910 pointer_count = write_pointer_description_offsets(
1911 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1912 if (pointer_count > 0)
1914 unsigned int increment_size;
1916 align = 0;
1917 increment_size = type_memsize(type_array_get_element(type), &align);
1919 if (increment_size > USHRT_MAX)
1920 error("array size of %u bytes is too large\n", increment_size);
1922 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1923 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1924 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1925 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1926 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1927 *typestring_offset += 8;
1929 pointer_count = write_pointer_description_offsets(
1930 file, attrs, type_array_get_element(type), offset_in_memory,
1931 offset_in_buffer, typestring_offset);
1934 else if (type_get_type(type) == TYPE_STRUCT)
1936 const var_t *v;
1937 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1939 if (offset_in_memory && offset_in_buffer)
1941 unsigned int padding;
1943 if (is_array(v->type) && type_array_has_variance(v->type))
1945 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1946 /* skip over variance and offset in buffer */
1947 *offset_in_buffer += 8;
1950 align = 0;
1951 type_memsize(v->type, &align);
1952 padding = ROUNDING(*offset_in_memory, align);
1953 *offset_in_memory += padding;
1954 *offset_in_buffer += padding;
1956 pointer_count += write_varying_array_pointer_descriptions(
1957 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1958 typestring_offset);
1961 else
1963 if (offset_in_memory && offset_in_buffer)
1965 unsigned int memsize;
1966 align = 0;
1967 memsize = type_memsize(type, &align);
1968 *offset_in_memory += memsize;
1969 /* increment these separately as in the case of conformant (varying)
1970 * structures these start at different values */
1971 *offset_in_buffer += memsize;
1975 return pointer_count;
1978 static void write_pointer_description(FILE *file, type_t *type,
1979 unsigned int *typestring_offset)
1981 unsigned int offset_in_buffer;
1982 unsigned int offset_in_memory;
1984 /* pass 1: search for single instance of a pointer (i.e. don't descend
1985 * into arrays) */
1986 if (!is_array(type))
1988 offset_in_memory = 0;
1989 offset_in_buffer = 0;
1990 write_no_repeat_pointer_descriptions(
1991 file, NULL, type,
1992 &offset_in_memory, &offset_in_buffer, typestring_offset);
1995 /* pass 2: search for pointers in fixed arrays */
1996 offset_in_memory = 0;
1997 offset_in_buffer = 0;
1998 write_fixed_array_pointer_descriptions(
1999 file, NULL, type,
2000 &offset_in_memory, &offset_in_buffer, typestring_offset);
2002 /* pass 3: search for pointers in conformant only arrays (but don't descend
2003 * into conformant varying or varying arrays) */
2004 if (is_conformant_array(type) &&
2005 (type_array_is_decl_as_ptr(type) || !current_structure))
2006 write_conformant_array_pointer_descriptions(
2007 file, NULL, type, 0, typestring_offset);
2008 else if (type_get_type(type) == TYPE_STRUCT &&
2009 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2011 unsigned int align = 0;
2012 type_t *carray = find_array_or_string_in_struct(type)->type;
2013 write_conformant_array_pointer_descriptions(
2014 file, NULL, carray,
2015 type_memsize(type, &align),
2016 typestring_offset);
2019 /* pass 4: search for pointers in varying arrays */
2020 offset_in_memory = 0;
2021 offset_in_buffer = 0;
2022 write_varying_array_pointer_descriptions(
2023 file, NULL, type,
2024 &offset_in_memory, &offset_in_buffer, typestring_offset);
2027 int is_declptr(const type_t *t)
2029 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2032 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2033 type_t *type, int toplevel_param,
2034 const char *name, unsigned int *typestring_offset)
2036 unsigned int start_offset;
2037 unsigned char rtype;
2038 type_t *elem_type;
2040 start_offset = *typestring_offset;
2041 update_tfsoff(type, start_offset, file);
2043 if (is_declptr(type))
2045 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2046 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2047 if (!pointer_type)
2048 pointer_type = RPC_FC_RP;
2049 print_start_tfs_comment(file, type, *typestring_offset);
2050 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2051 pointer_type, flag, string_of_type(pointer_type),
2052 flag ? " [simple_pointer]" : "");
2053 *typestring_offset += 2;
2054 if (!flag)
2056 print_file(file, 2, "NdrFcShort(0x2),\n");
2057 *typestring_offset += 2;
2061 if (is_array(type))
2062 elem_type = type_array_get_element(type);
2063 else
2064 elem_type = type_pointer_get_ref(type);
2066 if (type_get_type(elem_type) != TYPE_BASIC)
2068 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2069 return start_offset;
2072 rtype = get_basic_fc(elem_type);
2073 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2075 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2076 return start_offset;
2079 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2081 unsigned int dim = type_array_get_dim(type);
2083 /* FIXME: multi-dimensional array */
2084 if (0xffffu < dim)
2085 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2086 name, 0xffffu, dim - 0xffffu);
2088 if (rtype == RPC_FC_WCHAR)
2089 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2090 else
2091 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2092 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2093 *typestring_offset += 2;
2095 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2096 *typestring_offset += 2;
2098 return start_offset;
2100 else if (is_conformant_array(type))
2102 unsigned int align = 0;
2104 if (rtype == RPC_FC_WCHAR)
2105 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2106 else
2107 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2108 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2109 *typestring_offset += 2;
2111 *typestring_offset += write_conf_or_var_desc(
2112 file, current_structure,
2113 (!type_array_is_decl_as_ptr(type) && current_structure
2114 ? type_memsize(current_structure, &align)
2115 : 0),
2116 type, type_array_get_conformance(type));
2118 return start_offset;
2120 else
2122 if (rtype == RPC_FC_WCHAR)
2123 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2124 else
2125 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2126 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2127 *typestring_offset += 2;
2129 return start_offset;
2133 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2134 const char *name, unsigned int *typestring_offset)
2136 const expr_t *length_is = type_array_get_variance(type);
2137 const expr_t *size_is = type_array_get_conformance(type);
2138 unsigned int align = 0;
2139 unsigned int size;
2140 unsigned int start_offset;
2141 unsigned char fc;
2142 int has_pointer;
2143 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2144 unsigned int baseoff
2145 = !type_array_is_decl_as_ptr(type) && current_structure
2146 ? type_memsize(current_structure, &align)
2147 : 0;
2149 if (!pointer_type)
2150 pointer_type = RPC_FC_RP;
2152 if (write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset))
2153 has_pointer = TRUE;
2154 else
2155 has_pointer = type_has_pointers(type_array_get_element(type));
2157 align = 0;
2158 size = type_memsize((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2159 fc = get_array_fc(type);
2161 start_offset = *typestring_offset;
2162 update_tfsoff(type, start_offset, file);
2163 print_start_tfs_comment(file, type, start_offset);
2164 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2165 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2166 *typestring_offset += 2;
2168 align = 0;
2169 if (fc != RPC_FC_BOGUS_ARRAY)
2171 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2173 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2174 *typestring_offset += 4;
2176 else
2178 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2179 *typestring_offset += 2;
2182 if (is_conformant_array(type))
2183 *typestring_offset
2184 += write_conf_or_var_desc(file, current_structure, baseoff,
2185 type, size_is);
2187 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2189 unsigned int elalign = 0;
2190 unsigned int elsize = type_memsize(type_array_get_element(type), &elalign);
2191 unsigned int dim = type_array_get_dim(type);
2193 if (fc == RPC_FC_LGVARRAY)
2195 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2196 *typestring_offset += 4;
2198 else
2200 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2201 *typestring_offset += 2;
2204 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2205 *typestring_offset += 2;
2208 if (length_is)
2209 *typestring_offset
2210 += write_conf_or_var_desc(file, current_structure, baseoff,
2211 type, length_is);
2213 if (has_pointer && (type_array_is_decl_as_ptr(type) || !current_structure))
2215 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2216 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2217 *typestring_offset += 2;
2218 write_pointer_description(file, type, typestring_offset);
2219 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2220 *typestring_offset += 1;
2223 write_array_element_type(file, type, FALSE, typestring_offset);
2224 write_end(file, typestring_offset);
2226 else
2228 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2229 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2230 *typestring_offset += 2;
2231 *typestring_offset
2232 += write_conf_or_var_desc(file, current_structure, baseoff,
2233 type, size_is);
2234 *typestring_offset
2235 += write_conf_or_var_desc(file, current_structure, baseoff,
2236 type, length_is);
2238 write_array_element_type(file, type, TRUE, typestring_offset);
2239 write_end(file, typestring_offset);
2242 return start_offset;
2245 static const var_t *find_array_or_string_in_struct(const type_t *type)
2247 const var_list_t *fields = type_struct_get_fields(type);
2248 const var_t *last_field;
2249 const type_t *ft;
2251 if (!fields || list_empty(fields))
2252 return NULL;
2254 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2255 ft = last_field->type;
2257 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2258 return last_field;
2260 if (type_get_type(ft) == TYPE_STRUCT)
2261 return find_array_or_string_in_struct(ft);
2262 else
2263 return NULL;
2266 static void write_struct_members(FILE *file, const type_t *type,
2267 int is_complex, unsigned int *corroff,
2268 unsigned int *typestring_offset)
2270 const var_t *field;
2271 unsigned short offset = 0;
2272 unsigned int salign = 1;
2273 int padding;
2274 var_list_t *fields = type_struct_get_fields(type);
2276 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2278 type_t *ft = field->type;
2279 unsigned int align = 0;
2280 unsigned int size = type_memsize(ft, &align);
2281 align = clamp_align(align);
2282 if (salign < align) salign = align;
2284 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2286 if ((align - 1) & offset)
2288 unsigned char fc = 0;
2289 switch (align)
2291 case 2:
2292 fc = RPC_FC_ALIGNM2;
2293 break;
2294 case 4:
2295 fc = RPC_FC_ALIGNM4;
2296 break;
2297 case 8:
2298 fc = RPC_FC_ALIGNM8;
2299 break;
2300 default:
2301 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2303 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2304 offset = ROUND_SIZE(offset, align);
2305 *typestring_offset += 1;
2307 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2308 typestring_offset);
2309 offset += size;
2313 padding = ROUNDING(offset, salign);
2314 if (padding)
2316 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2317 RPC_FC_STRUCTPAD1 + padding - 1,
2318 padding);
2319 *typestring_offset += 1;
2322 write_end(file, typestring_offset);
2325 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2326 const char *name, unsigned int *tfsoff)
2328 const type_t *save_current_structure = current_structure;
2329 unsigned int total_size;
2330 const var_t *array;
2331 unsigned int start_offset;
2332 unsigned int array_offset;
2333 int has_pointers = 0;
2334 unsigned int align = 0;
2335 unsigned int corroff;
2336 var_t *f;
2337 unsigned char fc = get_struct_fc(type);
2338 var_list_t *fields = type_struct_get_fields(type);
2340 guard_rec(type);
2341 current_structure = type;
2343 total_size = type_memsize(type, &align);
2344 if (total_size > USHRT_MAX)
2345 error("structure size for %s exceeds %d bytes by %d bytes\n",
2346 name, USHRT_MAX, total_size - USHRT_MAX);
2348 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2349 has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
2350 FALSE, tfsoff);
2351 if (!has_pointers) has_pointers = type_has_pointers(type);
2353 array = find_array_or_string_in_struct(type);
2354 if (array && !processed(array->type))
2355 array_offset
2356 = is_string_type(array->attrs, array->type)
2357 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2358 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2360 corroff = *tfsoff;
2361 write_descriptors(file, type, tfsoff);
2363 start_offset = *tfsoff;
2364 update_tfsoff(type, start_offset, file);
2365 print_start_tfs_comment(file, type, start_offset);
2366 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2367 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2368 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2369 *tfsoff += 4;
2371 if (array)
2373 unsigned int absoff = array->type->typestring_offset;
2374 short reloff = absoff - *tfsoff;
2375 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2376 reloff, reloff, absoff);
2377 *tfsoff += 2;
2379 else if (fc == RPC_FC_BOGUS_STRUCT)
2381 print_file(file, 2, "NdrFcShort(0x0),\n");
2382 *tfsoff += 2;
2385 if (fc == RPC_FC_BOGUS_STRUCT)
2387 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2388 nothing is written to file yet. On the actual writing pass,
2389 this will have been updated. */
2390 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2391 int reloff = absoff - *tfsoff;
2392 assert( reloff >= 0 );
2393 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2394 reloff, reloff, absoff);
2395 *tfsoff += 2;
2397 else if ((fc == RPC_FC_PSTRUCT) ||
2398 (fc == RPC_FC_CPSTRUCT) ||
2399 (fc == RPC_FC_CVSTRUCT && has_pointers))
2401 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2402 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2403 *tfsoff += 2;
2404 write_pointer_description(file, type, tfsoff);
2405 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2406 *tfsoff += 1;
2409 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2410 tfsoff);
2412 if (fc == RPC_FC_BOGUS_STRUCT)
2414 const var_t *f;
2416 type->ptrdesc = *tfsoff;
2417 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2419 type_t *ft = f->type;
2420 if (is_ptr(ft))
2422 if (is_string_type(f->attrs, ft))
2423 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2424 else
2425 write_pointer_tfs(file, f->attrs, ft, FALSE, tfsoff);
2427 else if (type_get_type(ft) == TYPE_ARRAY && type_array_is_decl_as_ptr(ft))
2429 unsigned int offset;
2431 print_file(file, 0, "/* %d */\n", *tfsoff);
2433 offset = ft->typestring_offset;
2434 /* skip over the pointer that is written for strings, since a
2435 * pointer has to be written in-place here */
2436 if (is_string_type(f->attrs, ft))
2437 offset += 4;
2438 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2441 if (type->ptrdesc == *tfsoff)
2442 type->ptrdesc = 0;
2445 current_structure = save_current_structure;
2446 return start_offset;
2449 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2451 if (t == NULL)
2453 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2455 else
2457 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2459 unsigned char fc;
2460 if (type_get_type(t) == TYPE_BASIC)
2461 fc = get_basic_fc(t);
2462 else
2463 fc = get_enum_fc(t);
2464 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2465 fc, string_of_type(fc));
2467 else if (t->typestring_offset)
2469 short reloff = t->typestring_offset - *tfsoff;
2470 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2471 reloff, reloff, t->typestring_offset);
2473 else
2474 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2477 *tfsoff += 2;
2480 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2482 unsigned int align;
2483 unsigned int start_offset;
2484 unsigned int size;
2485 var_list_t *fields;
2486 unsigned int nbranch = 0;
2487 type_t *deftype = NULL;
2488 short nodeftype = 0xffff;
2489 var_t *f;
2491 guard_rec(type);
2493 align = 0;
2494 size = type_memsize(type, &align);
2496 fields = type_union_get_cases(type);
2498 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2500 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2501 if (cases)
2502 nbranch += list_count(cases);
2503 if (f->type)
2504 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2507 start_offset = *tfsoff;
2508 update_tfsoff(type, start_offset, file);
2509 print_start_tfs_comment(file, type, start_offset);
2510 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2512 const var_t *sv = type_union_get_switch_value(type);
2513 const type_t *st = sv->type;
2514 unsigned char fc;
2516 if (type_get_type(st) == TYPE_BASIC)
2518 switch (get_basic_fc(st))
2520 case RPC_FC_CHAR:
2521 case RPC_FC_SMALL:
2522 case RPC_FC_BYTE:
2523 case RPC_FC_USMALL:
2524 case RPC_FC_WCHAR:
2525 case RPC_FC_SHORT:
2526 case RPC_FC_USHORT:
2527 case RPC_FC_LONG:
2528 case RPC_FC_ULONG:
2529 fc = get_basic_fc(st);
2530 break;
2531 default:
2532 fc = 0;
2533 error("union switch type must be an integer, char, or enum\n");
2536 else if (type_get_type(st) == TYPE_ENUM)
2537 fc = get_enum_fc(st);
2538 else
2539 error("union switch type must be an integer, char, or enum\n");
2541 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2542 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2543 0x40 | fc, string_of_type(fc));
2544 *tfsoff += 2;
2546 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2548 static const expr_t dummy_expr; /* FIXME */
2549 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2550 unsigned char fc;
2552 if (type_get_type(st) == TYPE_BASIC)
2554 switch (get_basic_fc(st))
2556 case RPC_FC_CHAR:
2557 case RPC_FC_SMALL:
2558 case RPC_FC_USMALL:
2559 case RPC_FC_SHORT:
2560 case RPC_FC_USHORT:
2561 case RPC_FC_LONG:
2562 case RPC_FC_ULONG:
2563 case RPC_FC_ENUM16:
2564 case RPC_FC_ENUM32:
2565 fc = get_basic_fc(st);
2566 break;
2567 default:
2568 fc = 0;
2569 error("union switch type must be an integer, char, or enum\n");
2572 else if (type_get_type(st) == TYPE_ENUM)
2573 fc = get_enum_fc(st);
2574 else
2575 error("union switch type must be an integer, char, or enum\n");
2577 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2578 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2579 fc, string_of_type(fc));
2580 *tfsoff += 2;
2582 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2583 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2584 *tfsoff += 2;
2585 print_file(file, 0, "/* %u */\n", *tfsoff);
2588 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2589 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2590 *tfsoff += 4;
2592 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2594 type_t *ft = f->type;
2595 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2596 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2597 expr_t *c;
2599 if (cases == NULL && !deflt)
2600 error("union field %s with neither case nor default attribute\n", f->name);
2602 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2604 /* MIDL doesn't check for duplicate cases, even though that seems
2605 like a reasonable thing to do, it just dumps them to the TFS
2606 like we're going to do here. */
2607 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c->cval, c->cval);
2608 *tfsoff += 4;
2609 write_branch_type(file, ft, tfsoff);
2612 /* MIDL allows multiple default branches, even though that seems
2613 illogical, it just chooses the last one, which is what we will
2614 do. */
2615 if (deflt)
2617 deftype = ft;
2618 nodeftype = 0;
2622 if (deftype)
2624 write_branch_type(file, deftype, tfsoff);
2626 else
2628 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2629 *tfsoff += 2;
2632 return start_offset;
2635 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2636 unsigned int *typeformat_offset)
2638 unsigned int i;
2639 unsigned int start_offset = *typeformat_offset;
2640 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2642 if (iid)
2644 print_file(file, 2, "0x2f, /* FC_IP */\n");
2645 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2646 *typeformat_offset
2647 += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2649 else
2651 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2652 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2654 if (! uuid)
2655 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2657 update_tfsoff(type, start_offset, file);
2658 print_start_tfs_comment(file, type, start_offset);
2659 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2660 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2661 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2662 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2663 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2664 for (i = 0; i < 8; ++i)
2665 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2667 if (file)
2668 fprintf(file, "\n");
2670 *typeformat_offset += 18;
2672 return start_offset;
2675 static unsigned int write_contexthandle_tfs(FILE *file, const type_t *type,
2676 const var_t *var,
2677 unsigned int *typeformat_offset)
2679 unsigned int start_offset = *typeformat_offset;
2680 unsigned char flags = 0;
2682 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2683 flags |= NDR_STRICT_CONTEXT_HANDLE;
2685 if (is_ptr(type))
2686 flags |= 0x80;
2687 if (is_attr(var->attrs, ATTR_IN))
2689 flags |= 0x40;
2690 if (!is_attr(var->attrs, ATTR_OUT))
2691 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2693 if (is_attr(var->attrs, ATTR_OUT))
2694 flags |= 0x20;
2696 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2697 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2698 /* return and can't be null values overlap */
2699 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2700 print_file(file, 0, "can't be null, ");
2701 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2702 print_file(file, 0, "serialize, ");
2703 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2704 print_file(file, 0, "no serialize, ");
2705 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2706 print_file(file, 0, "strict, ");
2707 if ((flags & 0x21) == 0x20)
2708 print_file(file, 0, "out, ");
2709 if ((flags & 0x21) == 0x21)
2710 print_file(file, 0, "return, ");
2711 if (flags & 0x40)
2712 print_file(file, 0, "in, ");
2713 if (flags & 0x80)
2714 print_file(file, 0, "via ptr, ");
2715 print_file(file, 0, "*/\n");
2716 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2717 print_file(file, 2, "0, /* FIXME: param num */\n");
2718 *typeformat_offset += 4;
2720 return start_offset;
2723 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
2724 type_t *type, expr_list_t *range_list,
2725 unsigned int *typeformat_offset)
2727 unsigned char fc;
2728 unsigned int start_offset = *typeformat_offset;
2729 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
2730 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
2732 if (type_get_type(type) == TYPE_BASIC)
2733 fc = get_basic_fc(type);
2734 else
2735 fc = get_enum_fc(type);
2737 /* fc must fit in lower 4-bits of 8-bit field below */
2738 assert(fc <= 0xf);
2740 print_file(file, 0, "/* %u */\n", *typeformat_offset);
2741 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
2742 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2743 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_min->cval, range_min->cval);
2744 print_file(file, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_max->cval, range_max->cval);
2745 *typeformat_offset += 10;
2747 return start_offset;
2750 static unsigned int write_typeformatstring_var(FILE *file, int indent, const var_t *func,
2751 type_t *type, const var_t *var,
2752 int toplevel_param,
2753 unsigned int *typeformat_offset)
2755 unsigned int offset;
2757 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
2759 case TGT_CTXT_HANDLE:
2760 case TGT_CTXT_HANDLE_POINTER:
2761 return write_contexthandle_tfs(file, type, var, typeformat_offset);
2762 case TGT_USER_TYPE:
2763 write_user_tfs(file, type, typeformat_offset);
2764 return type->typestring_offset;
2765 case TGT_STRING:
2766 return write_string_tfs(file, var->attrs, type, toplevel_param, var->name, typeformat_offset);
2767 case TGT_ARRAY:
2769 int ptr_type;
2770 unsigned int off;
2771 off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2772 ptr_type = get_pointer_fc(type, var->attrs, toplevel_param);
2773 if (ptr_type != RPC_FC_RP)
2775 unsigned int absoff = type->typestring_offset;
2776 short reloff = absoff - (*typeformat_offset + 2);
2777 off = *typeformat_offset;
2778 print_file(file, 0, "/* %d */\n", off);
2779 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2780 string_of_type(ptr_type));
2781 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2782 reloff, reloff, absoff);
2783 *typeformat_offset += 4;
2785 return off;
2787 case TGT_STRUCT:
2788 if (processed(type)) return type->typestring_offset;
2789 return write_struct_tfs(file, type, var->name, typeformat_offset);
2790 case TGT_UNION:
2791 if (processed(type)) return type->typestring_offset;
2792 return write_union_tfs(file, type, typeformat_offset);
2793 case TGT_ENUM:
2794 case TGT_BASIC:
2795 /* nothing to do */
2796 return 0;
2797 case TGT_RANGE:
2799 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
2800 if (!range_list)
2801 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
2802 return write_range_tfs(file, var->attrs, type, range_list, typeformat_offset);
2804 case TGT_IFACE_POINTER:
2805 return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2806 case TGT_POINTER:
2807 if (last_ptr(type))
2809 size_t start_offset = *typeformat_offset;
2810 int in_attr = is_attr(var->attrs, ATTR_IN);
2811 int out_attr = is_attr(var->attrs, ATTR_OUT);
2812 const type_t *ref = type_pointer_get_ref(type);
2814 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
2816 /* special case for pointers to base types */
2817 case TGT_BASIC:
2818 case TGT_ENUM:
2820 unsigned char fc;
2822 if (type_get_type(ref) == TYPE_ENUM)
2823 fc = get_enum_fc(ref);
2824 else
2825 fc = get_basic_fc(ref);
2827 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2828 get_pointer_fc(type, var->attrs, toplevel_param),
2829 (!in_attr && out_attr) ? 0x0C : 0x08,
2830 string_of_type(get_pointer_fc(type, var->attrs, toplevel_param)),
2831 (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
2832 print_file(file, indent, "0x%02x, /* %s */\n",
2833 fc, string_of_type(fc));
2834 print_file(file, indent, "0x5c, /* FC_PAD */\n");
2835 *typeformat_offset += 4;
2836 return start_offset;
2838 default:
2839 break;
2843 offset = write_typeformatstring_var(file, indent, func,
2844 type_pointer_get_ref(type), var,
2845 FALSE, typeformat_offset);
2846 if (file)
2847 fprintf(file, "/* %2u */\n", *typeformat_offset);
2848 return write_nonsimple_pointer(file, var->attrs, type,
2849 toplevel_param,
2850 offset, typeformat_offset);
2851 case TGT_INVALID:
2852 break;
2854 error("invalid type %s for var %s\n", type->name, var->name);
2855 return 0;
2858 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2859 const char *name, int write_ptr, unsigned int *tfsoff)
2861 int retmask = 0;
2863 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2865 case TGT_USER_TYPE:
2866 write_user_tfs(file, type, tfsoff);
2867 break;
2868 case TGT_STRING:
2869 write_string_tfs(file, attrs, type, FALSE, name, tfsoff);
2870 break;
2871 case TGT_IFACE_POINTER:
2872 write_ip_tfs(file, attrs, type, tfsoff);
2873 break;
2874 case TGT_POINTER:
2876 type_t *ref = type_pointer_get_ref(type);
2878 if (!processed(ref) && type_get_type(ref) != TYPE_BASIC)
2879 retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2881 if (write_ptr)
2882 write_pointer_tfs(file, attrs, type, FALSE, tfsoff);
2884 retmask |= 1;
2885 break;
2887 case TGT_ARRAY:
2888 /* conformant arrays and strings are handled specially */
2889 if (!is_conformant_array(type) || type_array_is_decl_as_ptr(type) )
2891 write_array_tfs(file, attrs, type, name, tfsoff);
2892 if (is_conformant_array(type))
2893 retmask |= 1;
2895 break;
2896 case TGT_STRUCT:
2897 if (!processed(type))
2898 write_struct_tfs(file, type, name, tfsoff);
2899 break;
2900 case TGT_UNION:
2901 if (!processed(type))
2902 write_union_tfs(file, type, tfsoff);
2903 break;
2904 case TGT_ENUM:
2905 case TGT_BASIC:
2906 /* nothing to do */
2907 break;
2908 case TGT_RANGE:
2910 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
2911 if (!range_list)
2912 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
2913 write_range_tfs(file, attrs, type, range_list, tfsoff);
2914 break;
2916 case TGT_CTXT_HANDLE:
2917 case TGT_CTXT_HANDLE_POINTER:
2918 case TGT_INVALID:
2919 error("invalid type %s for var %s\n", type->name, name);
2920 break;
2923 return retmask;
2926 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2927 type_pred_t pred, unsigned int *typeformat_offset)
2929 const var_t *var;
2930 const statement_t *stmt;
2932 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2934 const type_t *iface;
2935 const statement_t *stmt_func;
2937 if (stmt->type == STMT_LIBRARY)
2939 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2940 continue;
2942 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2943 continue;
2945 iface = stmt->u.type;
2946 if (!pred(iface))
2947 continue;
2949 current_iface = iface;
2950 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2952 const var_t *func = stmt_func->u.var;
2953 if (is_local(func->attrs)) continue;
2955 if (!is_void(type_function_get_rettype(func->type)))
2957 var_t v = *func;
2958 v.type = type_function_get_rettype(func->type);
2959 update_tfsoff(type_function_get_rettype(func->type),
2960 write_typeformatstring_var(
2961 file, 2, NULL,
2962 type_function_get_rettype(func->type),
2963 &v, FALSE, typeformat_offset),
2964 file);
2967 current_func = func;
2968 if (type_get_function_args(func->type))
2969 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2970 update_tfsoff(
2971 var->type,
2972 write_typeformatstring_var(
2973 file, 2, func, var->type, var,
2974 TRUE, typeformat_offset),
2975 file);
2979 return *typeformat_offset + 1;
2982 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2984 unsigned int typeformat_offset = 2;
2986 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2990 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2992 int indent = 0;
2994 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2995 print_file(file, indent, "{\n");
2996 indent++;
2997 print_file(file, indent, "0,\n");
2998 print_file(file, indent, "{\n");
2999 indent++;
3000 print_file(file, indent, "NdrFcShort(0x0),\n");
3002 set_all_tfswrite(TRUE);
3003 process_tfs(file, stmts, pred);
3005 print_file(file, indent, "0x0\n");
3006 indent--;
3007 print_file(file, indent, "}\n");
3008 indent--;
3009 print_file(file, indent, "};\n");
3010 print_file(file, indent, "\n");
3013 static unsigned int get_required_buffer_size_type(
3014 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3016 *alignment = 0;
3017 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3019 case TGT_USER_TYPE:
3021 const char *uname;
3022 const type_t *utype = get_user_type(type, &uname);
3023 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3025 case TGT_BASIC:
3026 switch (get_basic_fc(type))
3028 case RPC_FC_BYTE:
3029 case RPC_FC_CHAR:
3030 case RPC_FC_USMALL:
3031 case RPC_FC_SMALL:
3032 *alignment = 4;
3033 return 1;
3035 case RPC_FC_WCHAR:
3036 case RPC_FC_USHORT:
3037 case RPC_FC_SHORT:
3038 *alignment = 4;
3039 return 2;
3041 case RPC_FC_ULONG:
3042 case RPC_FC_LONG:
3043 case RPC_FC_FLOAT:
3044 case RPC_FC_ERROR_STATUS_T:
3045 *alignment = 4;
3046 return 4;
3048 case RPC_FC_HYPER:
3049 case RPC_FC_DOUBLE:
3050 *alignment = 8;
3051 return 8;
3053 case RPC_FC_INT3264:
3054 case RPC_FC_UINT3264:
3055 assert( pointer_size );
3056 *alignment = pointer_size;
3057 return pointer_size;
3059 case RPC_FC_IGNORE:
3060 case RPC_FC_BIND_PRIMITIVE:
3061 return 0;
3063 default:
3064 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3065 get_basic_fc(type));
3066 return 0;
3068 break;
3070 case TGT_ENUM:
3071 switch (get_enum_fc(type))
3073 case RPC_FC_ENUM32:
3074 *alignment = 4;
3075 return 4;
3076 case RPC_FC_ENUM16:
3077 *alignment = 4;
3078 return 2;
3080 break;
3082 case TGT_STRUCT:
3083 if (get_struct_fc(type) == RPC_FC_STRUCT)
3085 if (!type_struct_get_fields(type)) return 0;
3086 return fields_memsize(type_struct_get_fields(type), alignment);
3088 break;
3090 case TGT_POINTER:
3091 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3093 const type_t *ref = type_pointer_get_ref(type);
3094 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3096 case TGT_BASIC:
3097 case TGT_ENUM:
3098 case TGT_RANGE:
3099 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3100 case TGT_STRUCT:
3101 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3102 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3103 break;
3104 case TGT_USER_TYPE:
3105 case TGT_CTXT_HANDLE:
3106 case TGT_CTXT_HANDLE_POINTER:
3107 case TGT_STRING:
3108 case TGT_POINTER:
3109 case TGT_ARRAY:
3110 case TGT_IFACE_POINTER:
3111 case TGT_UNION:
3112 case TGT_INVALID:
3113 break;
3116 break;
3118 case TGT_ARRAY:
3119 /* FIXME: depends on pointer type */
3120 return type_array_get_dim(type) *
3121 get_required_buffer_size_type(type_array_get_element(type), name, NULL, FALSE, alignment);
3123 default:
3124 break;
3126 return 0;
3129 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3131 int in_attr = is_attr(var->attrs, ATTR_IN);
3132 int out_attr = is_attr(var->attrs, ATTR_OUT);
3134 if (!in_attr && !out_attr)
3135 in_attr = 1;
3137 *alignment = 0;
3139 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3140 pass == PASS_RETURN)
3142 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3144 *alignment = 4;
3145 return 20;
3148 if (!is_string_type(var->attrs, var->type))
3149 return get_required_buffer_size_type(var->type, var->name,
3150 var->attrs, TRUE, alignment);
3152 return 0;
3155 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3157 const var_t *var;
3158 unsigned int total_size = 0, alignment;
3160 if (type_get_function_args(func->type))
3162 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3164 total_size += get_required_buffer_size(var, &alignment, pass);
3165 total_size += alignment;
3169 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3171 var_t v = *func;
3172 v.type = type_function_get_rettype(func->type);
3173 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3174 total_size += alignment;
3176 return total_size;
3179 static void print_phase_function(FILE *file, int indent, const char *type,
3180 const char *local_var_prefix, enum remoting_phase phase,
3181 const var_t *var, unsigned int type_offset)
3183 const char *function;
3184 switch (phase)
3186 case PHASE_BUFFERSIZE:
3187 function = "BufferSize";
3188 break;
3189 case PHASE_MARSHAL:
3190 function = "Marshall";
3191 break;
3192 case PHASE_UNMARSHAL:
3193 function = "Unmarshall";
3194 break;
3195 case PHASE_FREE:
3196 function = "Free";
3197 break;
3198 default:
3199 assert(0);
3200 return;
3203 print_file(file, indent, "Ndr%s%s(\n", type, function);
3204 indent++;
3205 print_file(file, indent, "&__frame->_StubMsg,\n");
3206 print_file(file, indent, "%s%s%s%s%s,\n",
3207 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3208 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3209 local_var_prefix,
3210 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3211 var->name);
3212 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3213 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3214 if (phase == PHASE_UNMARSHAL)
3215 print_file(file, indent, "0);\n");
3216 indent--;
3219 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3220 enum remoting_phase phase, enum pass pass, const var_t *var,
3221 const char *varname)
3223 type_t *type = var->type;
3224 unsigned int size;
3225 unsigned int alignment = 0;
3227 /* no work to do for other phases, buffer sizing is done elsewhere */
3228 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3229 return;
3231 if (type_get_type(type) == TYPE_ENUM ||
3232 (type_get_type(type) == TYPE_BASIC &&
3233 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3234 pointer_size != 4))
3236 unsigned char fc;
3238 if (type_get_type(type) == TYPE_ENUM)
3239 fc = get_enum_fc(type);
3240 else
3241 fc = get_basic_fc(type);
3243 if (phase == PHASE_MARSHAL)
3244 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3245 else
3246 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3247 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3248 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3249 local_var_prefix,
3250 var->name);
3251 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3253 else
3255 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3256 switch (get_basic_fc(ref))
3258 case RPC_FC_BYTE:
3259 case RPC_FC_CHAR:
3260 case RPC_FC_SMALL:
3261 case RPC_FC_USMALL:
3262 size = 1;
3263 alignment = 1;
3264 break;
3266 case RPC_FC_WCHAR:
3267 case RPC_FC_USHORT:
3268 case RPC_FC_SHORT:
3269 size = 2;
3270 alignment = 2;
3271 break;
3273 case RPC_FC_ULONG:
3274 case RPC_FC_LONG:
3275 case RPC_FC_FLOAT:
3276 case RPC_FC_ERROR_STATUS_T:
3277 /* pointer_size must be 4 if we got here in these two cases */
3278 case RPC_FC_INT3264:
3279 case RPC_FC_UINT3264:
3280 size = 4;
3281 alignment = 4;
3282 break;
3284 case RPC_FC_HYPER:
3285 case RPC_FC_DOUBLE:
3286 size = 8;
3287 alignment = 8;
3288 break;
3290 case RPC_FC_IGNORE:
3291 case RPC_FC_BIND_PRIMITIVE:
3292 /* no marshalling needed */
3293 return;
3295 default:
3296 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3297 var->name, get_basic_fc(ref));
3298 size = 0;
3301 if (phase == PHASE_MARSHAL && alignment > 1)
3302 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3303 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3304 alignment - 1, alignment - 1);
3306 if (phase == PHASE_MARSHAL)
3308 print_file(file, indent, "*(");
3309 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3310 if (is_ptr(type))
3311 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3312 else
3313 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3314 fprintf(file, "%s%s", local_var_prefix, varname);
3315 fprintf(file, ";\n");
3317 else if (phase == PHASE_UNMARSHAL)
3319 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3320 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3321 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3322 print_file(file, indent, "{\n");
3323 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3324 print_file(file, indent, "}\n");
3325 print_file(file, indent, "%s%s%s",
3326 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3327 local_var_prefix, varname);
3328 if (pass == PASS_IN && is_ptr(type))
3329 fprintf(file, " = (");
3330 else
3331 fprintf(file, " = *(");
3332 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3333 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3336 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3337 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3338 fprintf(file, ");\n");
3342 /* returns whether the MaxCount, Offset or ActualCount members need to be
3343 * filled in for the specified phase */
3344 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3346 return (phase != PHASE_UNMARSHAL);
3349 expr_t *get_size_is_expr(const type_t *t, const char *name)
3351 expr_t *x = NULL;
3353 for ( ; is_array(t); t = type_array_get_element(t))
3354 if (type_array_has_conformance(t))
3356 if (!x)
3357 x = type_array_get_conformance(t);
3358 else
3359 error("%s: multidimensional conformant"
3360 " arrays not supported at the top level\n",
3361 name);
3364 return x;
3367 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3368 enum remoting_phase phase, const var_t *var)
3370 const type_t *type = var->type;
3371 /* get fundamental type for the argument */
3372 for (;;)
3374 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
3375 break;
3376 else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
3377 break;
3378 else if (type_is_alias(type))
3379 type = type_alias_get_aliasee(type);
3380 else if (is_array(type))
3382 if (is_conformance_needed_for_phase(phase) && is_array(type))
3384 if (type_array_has_conformance(type))
3386 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3387 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3388 fprintf(file, ";\n\n");
3390 if (type_array_has_variance(type))
3392 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3393 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3394 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3395 fprintf(file, ";\n\n");
3398 break;
3400 else if (type_get_type(type) == TYPE_UNION)
3402 if (is_conformance_needed_for_phase(phase))
3404 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3405 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3406 fprintf(file, ";\n\n");
3408 break;
3410 else if (type_get_type(type) == TYPE_INTERFACE || is_void(type))
3412 expr_t *iid;
3414 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3416 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3417 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3418 fprintf( file, ";\n\n" );
3420 break;
3422 else if (is_ptr(type))
3423 type = type_pointer_get_ref(type);
3424 else
3425 break;
3429 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3430 enum pass pass, enum remoting_phase phase, const var_t *var)
3432 int in_attr, out_attr, pointer_type;
3433 const type_t *type = var->type;
3434 unsigned int start_offset = type->typestring_offset;
3436 if (is_ptr(type) || is_array(type))
3437 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3438 else
3439 pointer_type = 0;
3441 in_attr = is_attr(var->attrs, ATTR_IN);
3442 out_attr = is_attr(var->attrs, ATTR_OUT);
3443 if (!in_attr && !out_attr)
3444 in_attr = 1;
3446 if (phase != PHASE_FREE)
3447 switch (pass)
3449 case PASS_IN:
3450 if (!in_attr) return;
3451 break;
3452 case PASS_OUT:
3453 if (!out_attr) return;
3454 break;
3455 case PASS_RETURN:
3456 break;
3459 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3461 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3463 case TGT_CTXT_HANDLE:
3464 case TGT_CTXT_HANDLE_POINTER:
3465 if (phase == PHASE_MARSHAL)
3467 if (pass == PASS_IN)
3469 /* if the context_handle attribute appears in the chain of types
3470 * without pointers being followed, then the context handle must
3471 * be direct, otherwise it is a pointer */
3472 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3473 print_file(file, indent, "NdrClientContextMarshall(\n");
3474 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3475 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3476 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3478 else
3480 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3481 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3482 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3483 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3484 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3487 else if (phase == PHASE_UNMARSHAL)
3489 if (pass == PASS_OUT)
3491 if (!in_attr)
3492 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3493 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3494 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3495 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3496 print_file(file, indent + 1, "__frame->_Handle);\n");
3498 else
3500 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3501 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3502 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3505 break;
3506 case TGT_USER_TYPE:
3507 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3508 break;
3509 case TGT_STRING:
3510 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3511 pointer_type != RPC_FC_RP)
3513 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3514 !in_attr && is_conformant_array(type))
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);
3520 /* strings returned are assumed to be global and hence don't
3521 * need freeing */
3522 else if (is_declptr(type) &&
3523 !(phase == PHASE_FREE && pass == PASS_RETURN))
3524 print_phase_function(file, indent, "Pointer", local_var_prefix,
3525 phase, var, start_offset);
3527 else
3529 unsigned int real_start_offset = start_offset;
3530 /* skip over pointer description straight to string description */
3531 if (is_declptr(type))
3533 if (is_conformant_array(type))
3534 real_start_offset += 4;
3535 else
3536 real_start_offset += 2;
3538 if (is_array(type) && !is_conformant_array(type))
3539 print_phase_function(file, indent, "NonConformantString",
3540 local_var_prefix, phase, var,
3541 real_start_offset);
3542 else
3543 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3544 phase, var, real_start_offset);
3546 break;
3547 case TGT_ARRAY:
3549 unsigned char tc = get_array_fc(type);
3550 const char *array_type = "FixedArray";
3552 /* We already have the size_is expression since it's at the
3553 top level, but do checks for multidimensional conformant
3554 arrays. When we handle them, we'll need to extend this
3555 function to return a list, and then we'll actually use
3556 the return value. */
3557 get_size_is_expr(type, var->name);
3559 if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
3561 array_type = "VaryingArray";
3563 else if (tc == RPC_FC_CARRAY)
3565 array_type = "ConformantArray";
3567 else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
3569 array_type = (tc == RPC_FC_BOGUS_ARRAY
3570 ? "ComplexArray"
3571 : "ConformantVaryingArray");
3574 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3575 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3576 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3578 /* these are all unmarshalled by allocating memory */
3579 if (tc == RPC_FC_BOGUS_ARRAY ||
3580 tc == RPC_FC_CVARRAY ||
3581 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3582 (tc == RPC_FC_CARRAY && !in_attr))
3584 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3585 indent++;
3586 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3589 break;
3591 case TGT_BASIC:
3592 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3593 break;
3594 case TGT_ENUM:
3595 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3596 break;
3597 case TGT_RANGE:
3598 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3599 /* Note: this goes beyond what MIDL does - it only supports arguments
3600 * with the [range] attribute in Oicf mode */
3601 if (phase == PHASE_UNMARSHAL)
3603 const expr_t *range_min;
3604 const expr_t *range_max;
3605 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
3606 if (!range_list)
3607 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3608 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3609 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3611 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
3612 write_type_decl(file, var->type, NULL);
3613 fprintf(file, ")0x%lx) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
3614 write_type_decl(file, var->type, NULL);
3615 fprintf(file, ")0x%lx))\n", range_max->cval);
3616 print_file(file, indent, "{\n");
3617 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
3618 print_file(file, indent, "}\n");
3620 break;
3621 case TGT_STRUCT:
3622 switch (get_struct_fc(type))
3624 case RPC_FC_STRUCT:
3625 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3626 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3627 break;
3628 case RPC_FC_PSTRUCT:
3629 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3630 break;
3631 case RPC_FC_CSTRUCT:
3632 case RPC_FC_CPSTRUCT:
3633 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3634 break;
3635 case RPC_FC_CVSTRUCT:
3636 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3637 break;
3638 case RPC_FC_BOGUS_STRUCT:
3639 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3640 break;
3641 default:
3642 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3644 break;
3645 case TGT_UNION:
3647 const char *union_type = NULL;
3649 if (type_get_type(type) == TYPE_UNION)
3650 union_type = "NonEncapsulatedUnion";
3651 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3652 union_type = "EncapsulatedUnion";
3654 print_phase_function(file, indent, union_type, local_var_prefix,
3655 phase, var, start_offset);
3656 break;
3658 case TGT_POINTER:
3660 const type_t *ref = type_pointer_get_ref(type);
3661 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3663 case TGT_BASIC:
3664 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3665 break;
3666 case TGT_ENUM:
3667 /* base types have known sizes, so don't need a sizing pass
3668 * and don't have any memory to free and so don't need a
3669 * freeing pass */
3670 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3671 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3672 break;
3673 case TGT_STRUCT:
3675 const char *struct_type = NULL;
3676 switch (get_struct_fc(ref))
3678 case RPC_FC_STRUCT:
3679 /* simple structs have known sizes, so don't need a sizing
3680 * pass and don't have any memory to free and so don't
3681 * need a freeing pass */
3682 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3683 struct_type = "SimpleStruct";
3684 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3686 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3687 indent++;
3688 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3689 indent--;
3691 break;
3692 case RPC_FC_PSTRUCT:
3693 struct_type = "SimpleStruct";
3694 break;
3695 case RPC_FC_CSTRUCT:
3696 case RPC_FC_CPSTRUCT:
3697 struct_type = "ConformantStruct";
3698 break;
3699 case RPC_FC_CVSTRUCT:
3700 struct_type = "ConformantVaryingStruct";
3701 break;
3702 case RPC_FC_BOGUS_STRUCT:
3703 struct_type = "ComplexStruct";
3704 break;
3705 default:
3706 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3709 if (struct_type)
3711 if (phase == PHASE_FREE)
3712 struct_type = "Pointer";
3713 else
3714 start_offset = ref->typestring_offset;
3715 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3717 break;
3719 case TGT_UNION:
3721 const char *union_type = NULL;
3722 if (phase == PHASE_FREE)
3723 union_type = "Pointer";
3724 else
3726 if (type_get_type(ref) == TYPE_UNION)
3727 union_type = "NonEncapsulatedUnion";
3728 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3729 union_type = "EncapsulatedUnion";
3731 start_offset = ref->typestring_offset;
3734 print_phase_function(file, indent, union_type, local_var_prefix,
3735 phase, var, start_offset);
3736 break;
3738 case TGT_STRING:
3739 case TGT_POINTER:
3740 case TGT_ARRAY:
3741 case TGT_RANGE:
3742 case TGT_IFACE_POINTER:
3743 case TGT_USER_TYPE:
3744 case TGT_CTXT_HANDLE:
3745 case TGT_CTXT_HANDLE_POINTER:
3746 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3747 break;
3748 case TGT_INVALID:
3749 assert(0);
3750 break;
3752 else
3753 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3754 break;
3756 case TGT_IFACE_POINTER:
3757 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3758 break;
3759 case TGT_INVALID:
3760 assert(0);
3761 break;
3763 fprintf(file, "\n");
3766 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3767 enum pass pass, enum remoting_phase phase)
3769 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3771 unsigned int size = get_function_buffer_size( func, pass );
3772 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3775 if (pass == PASS_RETURN)
3777 var_t var;
3778 var = *func;
3779 var.type = type_function_get_rettype(func->type);
3780 var.name = xstrdup( "_RetVal" );
3781 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3782 free( var.name );
3784 else
3786 const var_t *var;
3787 if (!type_get_function_args(func->type))
3788 return;
3789 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3790 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3795 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3797 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3801 unsigned int get_size_procformatstring_func(const var_t *func)
3803 const var_t *var;
3804 unsigned int size = 0;
3806 /* argument list size */
3807 if (type_get_function_args(func->type))
3808 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3809 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3811 /* return value size */
3812 if (is_void(type_function_get_rettype(func->type)))
3813 size += 2; /* FC_END and FC_PAD */
3814 else
3815 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3817 return size;
3820 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3822 const statement_t *stmt;
3823 unsigned int size = 1;
3825 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3827 const type_t *iface;
3828 const statement_t *stmt_func;
3830 if (stmt->type == STMT_LIBRARY)
3832 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3833 continue;
3835 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3836 continue;
3838 iface = stmt->u.type;
3839 if (!pred(iface))
3840 continue;
3842 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3844 const var_t *func = stmt_func->u.var;
3845 if (!is_local(func->attrs))
3846 size += get_size_procformatstring_func( func );
3849 return size;
3852 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3854 set_all_tfswrite(FALSE);
3855 return process_tfs(NULL, stmts, pred);
3858 void declare_stub_args( FILE *file, int indent, const var_t *func )
3860 int in_attr, out_attr;
3861 int i = 0;
3862 const var_t *var;
3864 /* declare return value '_RetVal' */
3865 if (!is_void(type_function_get_rettype(func->type)))
3867 print_file(file, indent, "%s", "");
3868 write_type_decl_left(file, type_function_get_rettype(func->type));
3869 fprintf(file, " _RetVal;\n");
3872 if (!type_get_function_args(func->type))
3873 return;
3875 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3877 in_attr = is_attr(var->attrs, ATTR_IN);
3878 out_attr = is_attr(var->attrs, ATTR_OUT);
3879 if (!out_attr && !in_attr)
3880 in_attr = 1;
3882 if (is_context_handle(var->type))
3883 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3884 else
3886 if (!in_attr && !is_conformant_array(var->type))
3888 type_t *type_to_print;
3889 char name[16];
3890 print_file(file, indent, "%s", "");
3891 if (type_get_type(var->type) == TYPE_ARRAY &&
3892 !type_array_is_decl_as_ptr(var->type))
3893 type_to_print = var->type;
3894 else
3895 type_to_print = type_pointer_get_ref(var->type);
3896 sprintf(name, "_W%u", i++);
3897 write_type_decl(file, type_to_print, name);
3898 fprintf(file, ";\n");
3901 print_file(file, indent, "%s", "");
3902 write_type_decl_left(file, var->type);
3903 fprintf(file, " ");
3904 if (type_get_type(var->type) == TYPE_ARRAY &&
3905 !type_array_is_decl_as_ptr(var->type)) {
3906 fprintf(file, "(*%s)", var->name);
3907 } else
3908 fprintf(file, "%s", var->name);
3909 write_type_right(file, var->type, FALSE);
3910 fprintf(file, ";\n");
3912 if (decl_indirect(var->type))
3913 print_file(file, indent, "void *_p_%s;\n", var->name);
3919 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3921 int in_attr, out_attr;
3922 int i = 0, sep = 0;
3923 const var_t *var;
3925 if (!type_get_function_args(func->type))
3926 return;
3928 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3930 in_attr = is_attr(var->attrs, ATTR_IN);
3931 out_attr = is_attr(var->attrs, ATTR_OUT);
3932 if (!out_attr && !in_attr)
3933 in_attr = 1;
3935 if (!in_attr)
3937 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3939 if (is_context_handle(var->type))
3941 fprintf(file, " = NdrContextHandleInitialize(\n");
3942 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3943 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3944 var->type->typestring_offset);
3946 else if (is_array(var->type) &&
3947 type_array_has_conformance(var->type))
3949 unsigned int size, align = 0;
3950 type_t *type = var->type;
3952 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3953 for ( ;
3954 is_array(type) && type_array_has_conformance(type);
3955 type = type_array_get_element(type))
3957 write_expr(file, type_array_get_conformance(type), TRUE,
3958 TRUE, NULL, NULL, local_var_prefix);
3959 fprintf(file, " * ");
3961 size = type_memsize(type, &align);
3962 fprintf(file, "%u);\n", size);
3964 else
3966 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3967 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3969 case TGT_BASIC:
3970 case TGT_ENUM:
3971 case TGT_POINTER:
3972 case TGT_RANGE:
3973 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3974 break;
3975 case TGT_STRUCT:
3976 case TGT_UNION:
3977 case TGT_USER_TYPE:
3978 case TGT_IFACE_POINTER:
3979 case TGT_ARRAY:
3980 case TGT_CTXT_HANDLE:
3981 case TGT_CTXT_HANDLE_POINTER:
3982 case TGT_INVALID:
3983 case TGT_STRING:
3984 /* not initialised */
3985 break;
3987 i++;
3990 sep = 1;
3993 if (sep)
3994 fprintf(file, "\n");
3998 int write_expr_eval_routines(FILE *file, const char *iface)
4000 static const char *var_name = "pS";
4001 static const char *var_name_expr = "pS->";
4002 int result = 0;
4003 struct expr_eval_routine *eval;
4004 unsigned short callback_offset = 0;
4006 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4008 const char *name = eval->structure->name;
4009 result = 1;
4011 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4012 iface, name, callback_offset);
4013 print_file(file, 0, "{\n");
4014 print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
4015 name, var_name, name, eval->baseoff);
4016 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4017 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4018 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
4019 fprintf(file, ";\n");
4020 print_file(file, 0, "}\n\n");
4021 callback_offset++;
4023 return result;
4026 void write_expr_eval_routine_list(FILE *file, const char *iface)
4028 struct expr_eval_routine *eval;
4029 struct expr_eval_routine *cursor;
4030 unsigned short callback_offset = 0;
4032 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4033 fprintf(file, "{\n");
4035 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4037 const char *name = eval->structure->name;
4038 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
4039 callback_offset++;
4040 list_remove(&eval->entry);
4041 free(eval);
4044 fprintf(file, "};\n\n");
4047 void write_user_quad_list(FILE *file)
4049 user_type_t *ut;
4051 if (list_empty(&user_type_list))
4052 return;
4054 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4055 fprintf(file, "{\n");
4056 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4058 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4059 print_file(file, 1, "{\n");
4060 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4061 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4062 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4063 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4064 print_file(file, 1, "}%s\n", sep);
4066 fprintf(file, "};\n\n");
4069 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4071 const struct str_list_entry_t *endpoint;
4072 const char *p;
4074 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4075 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4076 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4078 print_file( f, 1, "{ (const unsigned char *)\"" );
4079 for (p = endpoint->str; *p && *p != ':'; p++)
4081 if (*p == '"' || *p == '\\') fputc( '\\', f );
4082 fputc( *p, f );
4084 if (!*p) goto error;
4085 if (p[1] != '[') goto error;
4087 fprintf( f, "\", (const unsigned char *)\"" );
4088 for (p += 2; *p && *p != ']'; p++)
4090 if (*p == '"' || *p == '\\') fputc( '\\', f );
4091 fputc( *p, f );
4093 if (*p != ']') goto error;
4094 fprintf( f, "\" },\n" );
4096 print_file( f, 0, "};\n\n" );
4097 return;
4099 error:
4100 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4103 void write_exceptions( FILE *file )
4105 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4106 fprintf( file, "\n");
4107 fprintf( file, "#include \"wine/exception.h\"\n");
4108 fprintf( file, "#undef RpcTryExcept\n");
4109 fprintf( file, "#undef RpcExcept\n");
4110 fprintf( file, "#undef RpcEndExcept\n");
4111 fprintf( file, "#undef RpcTryFinally\n");
4112 fprintf( file, "#undef RpcFinally\n");
4113 fprintf( file, "#undef RpcEndFinally\n");
4114 fprintf( file, "#undef RpcExceptionCode\n");
4115 fprintf( file, "#undef RpcAbnormalTermination\n");
4116 fprintf( file, "\n");
4117 fprintf( file, "struct __exception_frame;\n");
4118 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4119 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4120 fprintf( file, "\n");
4121 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4122 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4123 fprintf( file, " __filter_func filter; \\\n");
4124 fprintf( file, " __finally_func finally; \\\n");
4125 fprintf( file, " sigjmp_buf jmp; \\\n");
4126 fprintf( file, " DWORD code; \\\n");
4127 fprintf( file, " unsigned char abnormal_termination; \\\n");
4128 fprintf( file, " unsigned char filter_level; \\\n");
4129 fprintf( file, " unsigned char finally_level;\n");
4130 fprintf( file, "\n");
4131 fprintf( file, "struct __exception_frame\n{\n");
4132 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4133 fprintf( file, "};\n");
4134 fprintf( file, "\n");
4135 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4136 fprintf( file, "{\n");
4137 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4138 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4139 fprintf( file, " {\n");
4140 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4141 fprintf( file, " exc_frame->finally( exc_frame );\n");
4142 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4143 fprintf( file, " }\n");
4144 fprintf( file, " exc_frame->filter_level = 0;\n");
4145 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4146 fprintf( file, "}\n");
4147 fprintf( file, "\n");
4148 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4149 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4150 fprintf( file, " CONTEXT *context,\n");
4151 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4152 fprintf( file, "{\n");
4153 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4154 fprintf( file, "\n");
4155 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4156 fprintf( file, " {\n" );
4157 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4158 fprintf( file, " {\n" );
4159 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4160 fprintf( file, " exc_frame->finally( exc_frame );\n");
4161 fprintf( file, " }\n" );
4162 fprintf( file, " return ExceptionContinueSearch;\n");
4163 fprintf( file, " }\n" );
4164 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4165 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4166 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4167 fprintf( file, " return ExceptionContinueSearch;\n");
4168 fprintf( file, "}\n");
4169 fprintf( file, "\n");
4170 fprintf( file, "#define RpcTryExcept \\\n");
4171 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4172 fprintf( file, " { \\\n");
4173 fprintf( file, " if (!__frame->finally_level) \\\n" );
4174 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4175 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4176 fprintf( file, "\n");
4177 fprintf( file, "#define RpcExcept(expr) \\\n");
4178 fprintf( file, " if (!__frame->finally_level) \\\n" );
4179 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4180 fprintf( file, " __frame->filter_level = 0; \\\n" );
4181 fprintf( file, " } \\\n");
4182 fprintf( file, " else \\\n");
4183 fprintf( file, "\n");
4184 fprintf( file, "#define RpcEndExcept\n");
4185 fprintf( file, "\n");
4186 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4187 fprintf( file, "\n");
4188 fprintf( file, "#define RpcTryFinally \\\n");
4189 fprintf( file, " if (!__frame->filter_level) \\\n");
4190 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4191 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4192 fprintf( file, "\n");
4193 fprintf( file, "#define RpcFinally \\\n");
4194 fprintf( file, " if (!__frame->filter_level) \\\n");
4195 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4196 fprintf( file, " __frame->finally_level = 0;\n");
4197 fprintf( file, "\n");
4198 fprintf( file, "#define RpcEndFinally\n");
4199 fprintf( file, "\n");
4200 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4201 fprintf( file, "\n");
4202 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4203 fprintf( file, " do { \\\n");
4204 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4205 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4206 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4207 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4208 fprintf( file, " __frame->filter_level = 0; \\\n");
4209 fprintf( file, " __frame->finally_level = 0; \\\n");
4210 fprintf( file, " } while (0)\n");
4211 fprintf( file, "\n");
4212 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4213 fprintf( file, "\n");
4214 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4215 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4216 fprintf( file, "\n");
4217 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4218 fprintf( file, " DWORD code;\n");
4219 fprintf( file, "\n");
4220 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");