widl: Add a type_memsize_and_alignment function to make it explicit where the alignme...
[wine/multimedia.git] / tools / widl / typegen.c
blobc3cfdc087b513c5851a2cfcc76607c352fb9a891
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 type_t *current_structure;
50 static const type_t *current_iface;
52 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
53 struct expr_eval_routine
55 struct list entry;
56 const type_t *structure;
57 unsigned int baseoff;
58 const expr_t *expr;
61 enum type_context
63 TYPE_CONTEXT_TOPLEVELPARAM,
64 TYPE_CONTEXT_PARAM,
65 TYPE_CONTEXT_CONTAINER,
66 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
69 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
70 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
71 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align);
72 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
73 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
74 const char *name, int write_ptr, unsigned int *tfsoff);
75 static const var_t *find_array_or_string_in_struct(const type_t *type);
76 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
77 type_t *type, int toplevel_param,
78 const char *name, unsigned int *typestring_offset);
80 static const char *string_of_type(unsigned char type)
82 switch (type)
84 case RPC_FC_BYTE: return "FC_BYTE";
85 case RPC_FC_CHAR: return "FC_CHAR";
86 case RPC_FC_SMALL: return "FC_SMALL";
87 case RPC_FC_USMALL: return "FC_USMALL";
88 case RPC_FC_WCHAR: return "FC_WCHAR";
89 case RPC_FC_SHORT: return "FC_SHORT";
90 case RPC_FC_USHORT: return "FC_USHORT";
91 case RPC_FC_LONG: return "FC_LONG";
92 case RPC_FC_ULONG: return "FC_ULONG";
93 case RPC_FC_FLOAT: return "FC_FLOAT";
94 case RPC_FC_HYPER: return "FC_HYPER";
95 case RPC_FC_DOUBLE: return "FC_DOUBLE";
96 case RPC_FC_ENUM16: return "FC_ENUM16";
97 case RPC_FC_ENUM32: return "FC_ENUM32";
98 case RPC_FC_IGNORE: return "FC_IGNORE";
99 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
100 case RPC_FC_RP: return "FC_RP";
101 case RPC_FC_UP: return "FC_UP";
102 case RPC_FC_OP: return "FC_OP";
103 case RPC_FC_FP: return "FC_FP";
104 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
105 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
106 case RPC_FC_STRUCT: return "FC_STRUCT";
107 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
108 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
109 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
110 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
111 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
112 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
113 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
114 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
115 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
116 case RPC_FC_CARRAY: return "FC_CARRAY";
117 case RPC_FC_CVARRAY: return "FC_CVARRAY";
118 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
119 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
120 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
121 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
122 case RPC_FC_POINTER: return "FC_POINTER";
123 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
124 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
125 case RPC_FC_CSTRING: return "FC_CSTRING";
126 case RPC_FC_WSTRING: return "FC_WSTRING";
127 case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
128 case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
129 case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
130 case RPC_FC_IP: return "FC_IP";
131 case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
132 case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
133 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
134 case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
135 case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
136 case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
137 case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
138 case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
139 case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
140 case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
141 case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
142 case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
143 case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
144 case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
145 case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
146 case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
147 case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
148 case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
149 case RPC_FC_PP: return "FC_PP";
150 case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
151 case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
152 case RPC_FC_DIV_2: return "FC_DIV_2";
153 case RPC_FC_MULT_2: return "FC_MULT_2";
154 case RPC_FC_ADD_1: return "FC_ADD_1";
155 case RPC_FC_SUB_1: return "FC_SUB_1";
156 case RPC_FC_CALLBACK: return "FC_CALLBACK";
157 case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
158 case RPC_FC_END: return "FC_END";
159 case RPC_FC_PAD: return "FC_PAD";
160 case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
161 case RPC_FC_RANGE: return "FC_RANGE";
162 case RPC_FC_INT3264: return "FC_INT3264";
163 case RPC_FC_UINT3264: return "FC_UINT3264";
164 default:
165 error("string_of_type: unknown type 0x%02x\n", type);
166 return NULL;
170 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
172 const type_t *t = type;
173 for (;;)
175 if (is_attr(t->attrs, attr))
176 return get_attrp(t->attrs, attr);
177 else if (type_is_alias(t))
178 t = type_alias_get_aliasee(t);
179 else return NULL;
183 unsigned char get_basic_fc(const type_t *type)
185 int sign = type_basic_get_sign(type);
186 switch (type_basic_get_type(type))
188 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
189 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
190 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
191 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
192 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
193 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
194 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
195 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
196 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
197 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
198 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
199 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
200 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
201 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
203 return 0;
206 static inline unsigned int clamp_align(unsigned int align)
208 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
209 if(align > packing) align = packing;
210 return align;
213 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
215 const type_t *t;
216 int pointer_type;
218 assert(is_ptr(type) || is_array(type));
220 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
221 if (pointer_type)
222 return pointer_type;
224 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
226 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
227 if (pointer_type)
228 return pointer_type;
231 if (toplevel_param)
232 return RPC_FC_RP;
233 else if (is_ptr(type))
234 return type_pointer_get_default_fc(type);
235 else
236 return type_array_get_ptr_default_fc(type);
239 static unsigned char get_enum_fc(const type_t *type)
241 assert(type_get_type(type) == TYPE_ENUM);
242 if (is_aliaschain_attr(type, ATTR_V1ENUM))
243 return RPC_FC_ENUM32;
244 else
245 return RPC_FC_ENUM16;
248 static type_t *get_user_type(const type_t *t, const char **pname)
250 for (;;)
252 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
253 if (ut)
255 if (pname)
256 *pname = t->name;
257 return ut;
260 if (type_is_alias(t))
261 t = type_alias_get_aliasee(t);
262 else
263 return NULL;
267 static int is_user_type(const type_t *t)
269 return get_user_type(t, NULL) != NULL;
272 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
274 if (is_user_type(type))
275 return TGT_USER_TYPE;
277 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
278 return TGT_CTXT_HANDLE;
280 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
281 return TGT_STRING;
283 switch (type_get_type(type))
285 case TYPE_BASIC:
286 if (!(flags & TDT_IGNORE_RANGES) &&
287 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
288 return TGT_RANGE;
289 return TGT_BASIC;
290 case TYPE_ENUM:
291 if (!(flags & TDT_IGNORE_RANGES) &&
292 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
293 return TGT_RANGE;
294 return TGT_ENUM;
295 case TYPE_POINTER:
296 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
297 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
298 return TGT_IFACE_POINTER;
299 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
300 return TGT_CTXT_HANDLE_POINTER;
301 else
302 return TGT_POINTER;
303 case TYPE_STRUCT:
304 return TGT_STRUCT;
305 case TYPE_ENCAPSULATED_UNION:
306 case TYPE_UNION:
307 return TGT_UNION;
308 case TYPE_ARRAY:
309 return TGT_ARRAY;
310 case TYPE_FUNCTION:
311 case TYPE_COCLASS:
312 case TYPE_INTERFACE:
313 case TYPE_MODULE:
314 case TYPE_VOID:
315 case TYPE_ALIAS:
316 case TYPE_BITFIELD:
317 break;
319 return TGT_INVALID;
322 static int get_padding(const var_list_t *fields)
324 unsigned short offset = 0;
325 unsigned int salign = 1;
326 const var_t *f;
328 if (!fields)
329 return 0;
331 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
333 type_t *ft = f->type;
334 unsigned int align = 0;
335 unsigned int size = type_memsize_and_alignment(ft, &align);
336 align = clamp_align(align);
337 if (align > salign) salign = align;
338 offset = ROUND_SIZE(offset, align);
339 offset += size;
342 return ROUNDING(offset, salign);
345 unsigned char get_struct_fc(const type_t *type)
347 int has_pointer = 0;
348 int has_conformance = 0;
349 int has_variance = 0;
350 var_t *field;
351 var_list_t *fields;
353 fields = type_struct_get_fields(type);
355 if (get_padding(fields))
356 return RPC_FC_BOGUS_STRUCT;
358 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
360 type_t *t = field->type;
361 enum typegen_type typegen_type;
363 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
365 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
367 if (is_string_type(field->attrs, field->type))
369 if (is_conformant_array(t))
370 has_conformance = 1;
371 has_variance = 1;
372 continue;
375 if (is_array(type_array_get_element(field->type)))
376 return RPC_FC_BOGUS_STRUCT;
378 if (type_array_has_conformance(field->type))
380 has_conformance = 1;
381 if (list_next(fields, &field->entry))
382 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
383 field->name);
385 if (type_array_has_variance(t))
386 has_variance = 1;
388 t = type_array_get_element(t);
389 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
392 switch (typegen_type)
394 case TGT_USER_TYPE:
395 case TGT_IFACE_POINTER:
396 return RPC_FC_BOGUS_STRUCT;
397 case TGT_BASIC:
398 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
399 return RPC_FC_BOGUS_STRUCT;
400 break;
401 case TGT_ENUM:
402 if (get_enum_fc(t) == RPC_FC_ENUM16)
403 return RPC_FC_BOGUS_STRUCT;
404 break;
405 case TGT_POINTER:
406 case TGT_ARRAY:
407 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
408 return RPC_FC_BOGUS_STRUCT;
409 has_pointer = 1;
410 break;
411 case TGT_UNION:
412 return RPC_FC_BOGUS_STRUCT;
413 case TGT_STRUCT:
415 unsigned char fc = get_struct_fc(t);
416 switch (fc)
418 case RPC_FC_STRUCT:
419 break;
420 case RPC_FC_CVSTRUCT:
421 has_conformance = 1;
422 has_variance = 1;
423 has_pointer = 1;
424 break;
426 case RPC_FC_CPSTRUCT:
427 has_conformance = 1;
428 if (list_next( fields, &field->entry ))
429 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
430 field->name);
431 has_pointer = 1;
432 break;
434 case RPC_FC_CSTRUCT:
435 has_conformance = 1;
436 if (list_next( fields, &field->entry ))
437 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
438 field->name);
439 break;
441 case RPC_FC_PSTRUCT:
442 has_pointer = 1;
443 break;
445 default:
446 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
447 /* fallthru - treat it as complex */
449 /* as soon as we see one of these these members, it's bogus... */
450 case RPC_FC_BOGUS_STRUCT:
451 return RPC_FC_BOGUS_STRUCT;
453 break;
455 case TGT_RANGE:
456 return RPC_FC_BOGUS_STRUCT;
457 case TGT_STRING:
458 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
459 case TGT_INVALID:
460 case TGT_CTXT_HANDLE:
461 case TGT_CTXT_HANDLE_POINTER:
462 /* checking after parsing should mean that we don't get here. if we do,
463 * it's a checker bug */
464 assert(0);
468 if( has_variance )
470 if ( has_conformance )
471 return RPC_FC_CVSTRUCT;
472 else
473 return RPC_FC_BOGUS_STRUCT;
475 if( has_conformance && has_pointer )
476 return RPC_FC_CPSTRUCT;
477 if( has_conformance )
478 return RPC_FC_CSTRUCT;
479 if( has_pointer )
480 return RPC_FC_PSTRUCT;
481 return RPC_FC_STRUCT;
484 static unsigned char get_array_fc(const type_t *type)
486 unsigned char fc;
487 const expr_t *size_is;
488 const type_t *elem_type;
490 elem_type = type_array_get_element(type);
491 size_is = type_array_get_conformance(type);
493 if (!size_is)
495 unsigned int size = type_memsize(elem_type);
496 if (size * type_array_get_dim(type) > 0xffffuL)
497 fc = RPC_FC_LGFARRAY;
498 else
499 fc = RPC_FC_SMFARRAY;
501 else
502 fc = RPC_FC_CARRAY;
504 if (type_array_has_variance(type))
506 if (fc == RPC_FC_SMFARRAY)
507 fc = RPC_FC_SMVARRAY;
508 else if (fc == RPC_FC_LGFARRAY)
509 fc = RPC_FC_LGVARRAY;
510 else if (fc == RPC_FC_CARRAY)
511 fc = RPC_FC_CVARRAY;
514 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
516 case TGT_USER_TYPE:
517 fc = RPC_FC_BOGUS_ARRAY;
518 break;
519 case TGT_BASIC:
520 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
521 pointer_size != 4)
522 fc = RPC_FC_BOGUS_ARRAY;
523 break;
524 case TGT_STRUCT:
525 switch (get_struct_fc(elem_type))
527 case RPC_FC_BOGUS_STRUCT:
528 fc = RPC_FC_BOGUS_ARRAY;
529 break;
531 break;
532 case TGT_ENUM:
533 /* is 16-bit enum - if so, wire size differs from mem size and so
534 * the array cannot be block copied, which means the array is complex */
535 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
536 fc = RPC_FC_BOGUS_ARRAY;
537 break;
538 case TGT_UNION:
539 case TGT_IFACE_POINTER:
540 fc = RPC_FC_BOGUS_ARRAY;
541 break;
542 case TGT_POINTER:
543 /* ref pointers cannot just be block copied. unique pointers to
544 * interfaces need special treatment. either case means the array is
545 * complex */
546 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
547 fc = RPC_FC_BOGUS_ARRAY;
548 break;
549 case TGT_RANGE:
550 fc = RPC_FC_BOGUS_ARRAY;
551 break;
552 case TGT_CTXT_HANDLE:
553 case TGT_CTXT_HANDLE_POINTER:
554 case TGT_STRING:
555 case TGT_INVALID:
556 case TGT_ARRAY:
557 /* nothing to do for everything else */
558 break;
561 return fc;
564 static int is_non_complex_struct(const type_t *type)
566 return (type_get_type(type) == TYPE_STRUCT &&
567 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
570 static int type_has_pointers(const type_t *type)
572 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
574 case TGT_USER_TYPE:
575 return FALSE;
576 case TGT_POINTER:
577 return TRUE;
578 case TGT_ARRAY:
579 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
580 case TGT_STRUCT:
582 var_list_t *fields = type_struct_get_fields(type);
583 const var_t *field;
584 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
586 if (type_has_pointers(field->type))
587 return TRUE;
589 break;
591 case TGT_UNION:
593 var_list_t *fields;
594 const var_t *field;
595 fields = type_union_get_cases(type);
596 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
598 if (field->type && type_has_pointers(field->type))
599 return TRUE;
601 break;
603 case TGT_CTXT_HANDLE:
604 case TGT_CTXT_HANDLE_POINTER:
605 case TGT_STRING:
606 case TGT_IFACE_POINTER:
607 case TGT_BASIC:
608 case TGT_ENUM:
609 case TGT_RANGE:
610 case TGT_INVALID:
611 break;
614 return FALSE;
617 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
618 int toplevel_param)
620 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
622 case TGT_USER_TYPE:
623 return FALSE;
624 case TGT_POINTER:
625 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
626 return TRUE;
627 else
628 return FALSE;
629 case TGT_ARRAY:
630 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
631 return TRUE;
632 else
633 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
634 case TGT_STRUCT:
636 var_list_t *fields = type_struct_get_fields(type);
637 const var_t *field;
638 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
640 if (type_has_full_pointer(field->type, field->attrs, FALSE))
641 return TRUE;
643 break;
645 case TGT_UNION:
647 var_list_t *fields;
648 const var_t *field;
649 fields = type_union_get_cases(type);
650 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
652 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
653 return TRUE;
655 break;
657 case TGT_CTXT_HANDLE:
658 case TGT_CTXT_HANDLE_POINTER:
659 case TGT_STRING:
660 case TGT_IFACE_POINTER:
661 case TGT_BASIC:
662 case TGT_ENUM:
663 case TGT_RANGE:
664 case TGT_INVALID:
665 break;
668 return FALSE;
671 static unsigned short user_type_offset(const char *name)
673 user_type_t *ut;
674 unsigned short off = 0;
675 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
677 if (strcmp(name, ut->name) == 0)
678 return off;
679 ++off;
681 error("user_type_offset: couldn't find type (%s)\n", name);
682 return 0;
685 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
687 type->typestring_offset = offset;
688 if (file) type->tfswrite = FALSE;
691 static void guard_rec(type_t *type)
693 /* types that contain references to themselves (like a linked list),
694 need to be shielded from infinite recursion when writing embedded
695 types */
696 if (type->typestring_offset)
697 type->tfswrite = FALSE;
698 else
699 type->typestring_offset = 1;
702 static int is_embedded_complex(const type_t *type)
704 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
706 case TGT_USER_TYPE:
707 case TGT_STRUCT:
708 case TGT_UNION:
709 case TGT_ARRAY:
710 case TGT_IFACE_POINTER:
711 return TRUE;
712 default:
713 return FALSE;
717 static const char *get_context_handle_type_name(const type_t *type)
719 const type_t *t;
720 for (t = type;
721 is_ptr(t) || type_is_alias(t);
722 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
723 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
724 return t->name;
725 assert(0);
726 return NULL;
729 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
730 do { \
731 if (file) \
732 fprintf(file, "/* %2u */\n", typestring_offset); \
733 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
735 while (0)
737 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
738 static void print_file(FILE *file, int indent, const char *format, ...)
740 va_list va;
741 va_start(va, format);
742 print(file, indent, format, va);
743 va_end(va);
746 void print(FILE *file, int indent, const char *format, va_list va)
748 if (file)
750 if (format[0] != '\n')
751 while (0 < indent--)
752 fprintf(file, " ");
753 vfprintf(file, format, va);
758 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
760 if (decl_indirect(t))
762 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
763 local_var_prefix, n, local_var_prefix, n);
764 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
766 else if (is_ptr(t) || is_array(t))
767 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
770 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
772 const var_t *var;
774 if (!is_void(type_function_get_rettype(func->type)))
775 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
777 if (!type_get_function_args(func->type))
778 return;
780 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
781 write_var_init(file, indent, var->type, var->name, local_var_prefix);
783 fprintf(file, "\n");
786 static void write_formatdesc(FILE *f, int indent, const char *str)
788 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
789 print_file(f, indent, "{\n");
790 print_file(f, indent + 1, "short Pad;\n");
791 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
792 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
793 print_file(f, indent, "\n");
796 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
798 clear_all_offsets();
800 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
801 get_size_typeformatstring(stmts, pred));
803 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
804 get_size_procformatstring(stmts, pred));
806 fprintf(f, "\n");
807 write_formatdesc(f, indent, "TYPE");
808 write_formatdesc(f, indent, "PROC");
809 fprintf(f, "\n");
810 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
811 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
812 print_file(f, indent, "\n");
815 int decl_indirect(const type_t *t)
817 if (is_user_type(t))
818 return TRUE;
819 return (type_get_type(t) != TYPE_BASIC &&
820 type_get_type(t) != TYPE_ENUM &&
821 type_get_type(t) != TYPE_POINTER &&
822 type_get_type(t) != TYPE_ARRAY);
825 static unsigned int write_procformatstring_type(FILE *file, int indent,
826 const char *name,
827 const type_t *type,
828 const attr_list_t *attrs,
829 int is_return)
831 unsigned int size;
833 int is_in = is_attr(attrs, ATTR_IN);
834 int is_out = is_attr(attrs, ATTR_OUT);
836 if (!is_in && !is_out) is_in = TRUE;
838 if (type_get_type(type) == TYPE_BASIC ||
839 type_get_type(type) == TYPE_ENUM)
841 unsigned char fc;
843 if (is_return)
844 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
845 else
846 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
848 if (type_get_type(type) == TYPE_ENUM)
850 fc = get_enum_fc(type);
852 else
854 fc = get_basic_fc(type);
856 if (fc == RPC_FC_BIND_PRIMITIVE)
857 fc = RPC_FC_IGNORE;
860 print_file(file, indent, "0x%02x, /* %s */\n",
861 fc, string_of_type(fc));
862 size = 2; /* includes param type prefix */
864 else
866 if (is_return)
867 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
868 else if (is_in && is_out)
869 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
870 else if (is_out)
871 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
872 else
873 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
875 print_file(file, indent, "0x01,\n");
876 print_file(file, indent, "NdrFcShort(0x%hx),\n", type->typestring_offset);
877 size = 4; /* includes param type prefix */
879 return size;
882 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
884 const statement_t *stmt;
885 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
887 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
889 const statement_t *stmt_func;
890 if (!pred(stmt->u.type))
891 continue;
892 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
894 const var_t *func = stmt_func->u.var;
895 if (is_local(func->attrs)) continue;
896 /* emit argument data */
897 if (type_get_function_args(func->type))
899 const var_t *var;
900 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
901 write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
904 /* emit return value data */
905 if (is_void(type_function_get_rettype(func->type)))
907 print_file(file, indent, "0x5b, /* FC_END */\n");
908 print_file(file, indent, "0x5c, /* FC_PAD */\n");
910 else
911 write_procformatstring_type(file, indent, "return value", type_function_get_rettype(func->type), NULL, TRUE);
914 else if (stmt->type == STMT_LIBRARY)
915 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
919 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
921 int indent = 0;
923 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
924 print_file(file, indent, "{\n");
925 indent++;
926 print_file(file, indent, "0,\n");
927 print_file(file, indent, "{\n");
928 indent++;
930 write_procformatstring_stmts(file, indent, stmts, pred);
932 print_file(file, indent, "0x0\n");
933 indent--;
934 print_file(file, indent, "}\n");
935 indent--;
936 print_file(file, indent, "};\n");
937 print_file(file, indent, "\n");
940 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
942 unsigned char fc;
944 if (type_get_type(type) == TYPE_BASIC)
945 fc = get_basic_fc(type);
946 else if (type_get_type(type) == TYPE_ENUM)
947 fc = get_enum_fc(type);
948 else
949 return 0;
951 if (convert_to_signed_type)
953 switch(fc)
955 case RPC_FC_USMALL:
956 fc = RPC_FC_SMALL;
957 break;
958 case RPC_FC_USHORT:
959 fc = RPC_FC_SHORT;
960 break;
961 case RPC_FC_ULONG:
962 fc = RPC_FC_LONG;
963 break;
967 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
968 *typestring_offset += 1;
969 return 1;
972 /* write conformance / variance descriptor */
973 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *structure,
974 unsigned int baseoff, const type_t *type,
975 const expr_t *expr)
977 unsigned char operator_type = 0;
978 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
979 const char *conftype_string = "";
980 const expr_t *subexpr;
982 if (!expr)
984 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
985 return 4;
988 if (expr->is_const)
990 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
991 error("write_conf_or_var_desc: constant value %d is greater than "
992 "the maximum constant size of %d\n", expr->cval,
993 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
995 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
996 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
997 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
998 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1000 return 4;
1003 if (!structure)
1005 /* Top-level conformance calculations are done inline. */
1006 print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
1007 RPC_FC_TOP_LEVEL_CONFORMANCE);
1008 print_file (file, 2, "0x0,\n");
1009 print_file (file, 2, "NdrFcShort(0x0),\n");
1010 return 4;
1013 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1015 conftype = RPC_FC_POINTER_CONFORMANCE;
1016 conftype_string = "field pointer, ";
1019 subexpr = expr;
1020 switch (subexpr->type)
1022 case EXPR_PPTR:
1023 subexpr = subexpr->ref;
1024 operator_type = RPC_FC_DEREFERENCE;
1025 break;
1026 case EXPR_DIV:
1027 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1029 subexpr = subexpr->ref;
1030 operator_type = RPC_FC_DIV_2;
1032 break;
1033 case EXPR_MUL:
1034 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1036 subexpr = subexpr->ref;
1037 operator_type = RPC_FC_MULT_2;
1039 break;
1040 case EXPR_SUB:
1041 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1043 subexpr = subexpr->ref;
1044 operator_type = RPC_FC_SUB_1;
1046 break;
1047 case EXPR_ADD:
1048 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1050 subexpr = subexpr->ref;
1051 operator_type = RPC_FC_ADD_1;
1053 break;
1054 default:
1055 break;
1058 if (subexpr->type == EXPR_IDENTIFIER)
1060 const type_t *correlation_variable = NULL;
1061 unsigned char param_type = 0;
1062 unsigned int offset = 0;
1063 const var_t *var;
1064 var_list_t *fields = type_struct_get_fields(structure);
1066 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1068 unsigned int size = field_memsize( var->type, &offset );
1069 if (var->name && !strcmp(var->name, subexpr->u.sval))
1071 correlation_variable = var->type;
1072 break;
1074 offset += size;
1076 if (!correlation_variable)
1077 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1078 subexpr->u.sval);
1080 correlation_variable = expr_resolve_type(NULL, structure, expr);
1082 offset -= baseoff;
1084 if (type_get_type(correlation_variable) == TYPE_BASIC)
1086 switch (get_basic_fc(correlation_variable))
1088 case RPC_FC_CHAR:
1089 case RPC_FC_SMALL:
1090 param_type = RPC_FC_SMALL;
1091 break;
1092 case RPC_FC_BYTE:
1093 case RPC_FC_USMALL:
1094 param_type = RPC_FC_USMALL;
1095 break;
1096 case RPC_FC_WCHAR:
1097 case RPC_FC_SHORT:
1098 param_type = RPC_FC_SHORT;
1099 break;
1100 case RPC_FC_USHORT:
1101 param_type = RPC_FC_USHORT;
1102 break;
1103 case RPC_FC_LONG:
1104 param_type = RPC_FC_LONG;
1105 break;
1106 case RPC_FC_ULONG:
1107 param_type = RPC_FC_ULONG;
1108 break;
1109 default:
1110 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1111 get_basic_fc(correlation_variable));
1114 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1116 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1117 param_type = RPC_FC_LONG;
1118 else
1119 param_type = RPC_FC_SHORT;
1121 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1123 if (pointer_size == 8)
1124 param_type = RPC_FC_HYPER;
1125 else
1126 param_type = RPC_FC_LONG;
1128 else
1130 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1131 subexpr->u.sval);
1132 return 0;
1135 print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
1136 conftype | param_type, conftype_string, string_of_type(param_type));
1137 print_file(file, 2, "0x%x, /* %s */\n", operator_type,
1138 operator_type ? string_of_type(operator_type) : "no operators");
1139 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1140 offset, offset);
1142 else
1144 unsigned int callback_offset = 0;
1145 struct expr_eval_routine *eval;
1146 int found = 0;
1148 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1150 if (eval->structure == structure ||
1151 (eval->structure->name && structure->name &&
1152 !strcmp(eval->structure->name, structure->name) &&
1153 !compare_expr(eval->expr, expr)))
1155 found = 1;
1156 break;
1158 callback_offset++;
1161 if (!found)
1163 eval = xmalloc (sizeof(*eval));
1164 eval->structure = structure;
1165 eval->baseoff = baseoff;
1166 eval->expr = expr;
1167 list_add_tail (&expr_eval_routines, &eval->entry);
1170 if (callback_offset > USHRT_MAX)
1171 error("Maximum number of callback routines reached\n");
1173 print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
1174 print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1175 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset, callback_offset);
1177 return 4;
1180 /* return size and start offset of a data field based on current offset */
1181 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1183 unsigned int align = 0;
1184 unsigned int size = type_memsize_and_alignment( type, &align );
1186 *offset = ROUND_SIZE( *offset, align );
1187 return size;
1190 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1192 unsigned int size = 0;
1193 unsigned int max_align;
1194 const var_t *v;
1196 if (!fields) return 0;
1197 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1199 unsigned int falign = 0;
1200 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1201 if (*align < falign) *align = falign;
1202 falign = clamp_align(falign);
1203 size = ROUND_SIZE(size, falign);
1204 size += fsize;
1207 max_align = clamp_align(*align);
1208 size = ROUND_SIZE(size, max_align);
1210 return size;
1213 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1215 unsigned int size, maxs = 0;
1216 unsigned int align = *pmaxa;
1217 const var_t *v;
1219 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1221 /* we could have an empty default field with NULL type */
1222 if (v->type)
1224 size = type_memsize_and_alignment(v->type, &align);
1225 if (maxs < size) maxs = size;
1226 if (*pmaxa < align) *pmaxa = align;
1230 return maxs;
1233 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1235 unsigned int size = 0;
1237 switch (type_get_type(t))
1239 case TYPE_BASIC:
1240 switch (get_basic_fc(t))
1242 case RPC_FC_BYTE:
1243 case RPC_FC_CHAR:
1244 case RPC_FC_USMALL:
1245 case RPC_FC_SMALL:
1246 size = 1;
1247 if (size > *align) *align = size;
1248 break;
1249 case RPC_FC_WCHAR:
1250 case RPC_FC_USHORT:
1251 case RPC_FC_SHORT:
1252 size = 2;
1253 if (size > *align) *align = size;
1254 break;
1255 case RPC_FC_ULONG:
1256 case RPC_FC_LONG:
1257 case RPC_FC_ERROR_STATUS_T:
1258 case RPC_FC_FLOAT:
1259 size = 4;
1260 if (size > *align) *align = size;
1261 break;
1262 case RPC_FC_HYPER:
1263 case RPC_FC_DOUBLE:
1264 size = 8;
1265 if (size > *align) *align = size;
1266 break;
1267 case RPC_FC_INT3264:
1268 case RPC_FC_UINT3264:
1269 assert( pointer_size );
1270 size = pointer_size;
1271 if (size > *align) *align = size;
1272 break;
1273 default:
1274 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1275 size = 0;
1277 break;
1278 case TYPE_ENUM:
1279 switch (get_enum_fc(t))
1281 case RPC_FC_ENUM16:
1282 case RPC_FC_ENUM32:
1283 size = 4;
1284 if (size > *align) *align = size;
1285 break;
1286 default:
1287 error("type_memsize: Unknown enum type\n");
1288 size = 0;
1290 break;
1291 case TYPE_STRUCT:
1292 size = fields_memsize(type_struct_get_fields(t), align);
1293 break;
1294 case TYPE_ENCAPSULATED_UNION:
1295 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1296 break;
1297 case TYPE_UNION:
1298 size = union_memsize(type_union_get_cases(t), align);
1299 break;
1300 case TYPE_POINTER:
1301 assert( pointer_size );
1302 size = pointer_size;
1303 if (size > *align) *align = size;
1304 break;
1305 case TYPE_ARRAY:
1306 if (!type_array_is_decl_as_ptr(t))
1308 if (is_conformant_array(t))
1310 type_memsize_and_alignment(type_array_get_element(t), align);
1311 size = 0;
1313 else
1314 size = type_array_get_dim(t) *
1315 type_memsize_and_alignment(type_array_get_element(t), align);
1317 else /* declared as a pointer */
1319 assert( pointer_size );
1320 size = pointer_size;
1321 if (size > *align) *align = size;
1323 break;
1324 case TYPE_INTERFACE:
1325 case TYPE_ALIAS:
1326 case TYPE_VOID:
1327 case TYPE_COCLASS:
1328 case TYPE_MODULE:
1329 case TYPE_FUNCTION:
1330 case TYPE_BITFIELD:
1331 /* these types should not be encountered here due to language
1332 * restrictions (interface, void, coclass, module), logical
1333 * restrictions (alias - due to type_get_type call above) or
1334 * checking restrictions (function, bitfield). */
1335 assert(0);
1338 return size;
1341 unsigned int type_memsize(const type_t *t)
1343 unsigned int align = 0;
1344 return type_memsize_and_alignment( t, &align );
1347 int is_full_pointer_function(const var_t *func)
1349 const var_t *var;
1350 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1351 return TRUE;
1352 if (!type_get_function_args(func->type))
1353 return FALSE;
1354 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1355 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1356 return TRUE;
1357 return FALSE;
1360 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1362 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1363 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1364 fprintf(file, "\n");
1367 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1369 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1370 fprintf(file, "\n");
1373 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1374 const type_t *type,
1375 int toplevel_param,
1376 unsigned int offset,
1377 unsigned int *typeformat_offset)
1379 unsigned int start_offset = *typeformat_offset;
1380 short reloff = offset - (*typeformat_offset + 2);
1381 int in_attr, out_attr;
1382 int pointer_type;
1383 unsigned char flags = 0;
1385 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1387 in_attr = is_attr(attrs, ATTR_IN);
1388 out_attr = is_attr(attrs, ATTR_OUT);
1389 if (!in_attr && !out_attr) in_attr = 1;
1391 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1392 flags |= RPC_FC_P_ONSTACK;
1394 if (is_ptr(type) && is_declptr(type_pointer_get_ref(type)))
1395 flags |= RPC_FC_P_DEREF;
1397 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1398 pointer_type,
1399 flags,
1400 string_of_type(pointer_type));
1401 if (file)
1403 if (flags & RPC_FC_P_ONSTACK)
1404 fprintf(file, " [allocated_on_stack]");
1405 if (flags & RPC_FC_P_DEREF)
1406 fprintf(file, " [pointer_deref]");
1407 fprintf(file, " */\n");
1410 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1411 *typeformat_offset += 4;
1413 return start_offset;
1416 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1418 unsigned char fc;
1419 unsigned char pointer_fc;
1420 const type_t *ref;
1421 int in_attr = is_attr(attrs, ATTR_IN);
1422 int out_attr = is_attr(attrs, ATTR_OUT);
1423 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
1425 /* for historical reasons, write_simple_pointer also handled string types,
1426 * but no longer does. catch bad uses of the function with this check */
1427 if (is_string_type(attrs, type))
1428 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1430 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1432 ref = type_pointer_get_ref(type);
1433 if (type_get_type(ref) == TYPE_ENUM)
1434 fc = get_enum_fc(ref);
1435 else
1436 fc = get_basic_fc(ref);
1438 if (out_attr && !in_attr)
1439 flags |= RPC_FC_P_ONSTACK;
1441 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
1442 pointer_fc, flags, string_of_type(pointer_fc),
1443 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
1444 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1445 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1446 return 4;
1449 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1451 print_file(file, 0, "/* %u (", tfsoff);
1452 write_type_decl(file, t, NULL);
1453 print_file(file, 0, ") */\n");
1456 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1457 type_t *type, unsigned int ref_offset,
1458 int toplevel_param,
1459 unsigned int *typestring_offset)
1461 unsigned int offset = *typestring_offset;
1462 type_t *ref = type_pointer_get_ref(type);
1464 print_start_tfs_comment(file, type, offset);
1465 update_tfsoff(type, offset, file);
1467 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
1469 case TGT_BASIC:
1470 case TGT_ENUM:
1471 *typestring_offset += write_simple_pointer(file, attrs, type,
1472 toplevel_param);
1473 break;
1474 default:
1475 if (ref_offset)
1476 write_nonsimple_pointer(file, attrs, type,
1477 toplevel_param,
1478 ref_offset,
1479 typestring_offset);
1480 break;
1483 return offset;
1486 static int processed(const type_t *type)
1488 return type->typestring_offset && !type->tfswrite;
1491 static int user_type_has_variable_size(const type_t *t)
1493 if (is_ptr(t))
1494 return TRUE;
1495 else if (type_get_type(t) == TYPE_STRUCT)
1497 switch (get_struct_fc(t))
1499 case RPC_FC_PSTRUCT:
1500 case RPC_FC_CSTRUCT:
1501 case RPC_FC_CPSTRUCT:
1502 case RPC_FC_CVSTRUCT:
1503 return TRUE;
1506 /* Note: Since this only applies to user types, we can't have a conformant
1507 array here, and strings should get filed under pointer in this case. */
1508 return FALSE;
1511 static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1513 unsigned int start, absoff, flags;
1514 unsigned int ualign = 0;
1515 const char *name = NULL;
1516 type_t *utype = get_user_type(type, &name);
1517 unsigned int usize = type_memsize_and_alignment(utype, &ualign);
1518 unsigned int size = type_memsize(type);
1519 unsigned short funoff = user_type_offset(name);
1520 short reloff;
1522 guard_rec(type);
1524 if(user_type_has_variable_size(utype)) usize = 0;
1526 if (type_get_type(utype) == TYPE_BASIC ||
1527 type_get_type(utype) == TYPE_ENUM)
1529 unsigned char fc;
1531 if (type_get_type(utype) == TYPE_ENUM)
1532 fc = get_enum_fc(utype);
1533 else
1534 fc = get_basic_fc(utype);
1536 absoff = *tfsoff;
1537 print_start_tfs_comment(file, utype, absoff);
1538 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1539 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1540 *tfsoff += 2;
1542 else
1544 if (!processed(utype))
1545 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1546 absoff = utype->typestring_offset;
1549 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1550 flags = 0x40;
1551 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1552 flags = 0x80;
1553 else
1554 flags = 0;
1556 start = *tfsoff;
1557 update_tfsoff(type, start, file);
1558 print_start_tfs_comment(file, type, start);
1559 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1560 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1561 flags | (ualign - 1), ualign - 1, flags);
1562 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1563 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
1564 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize, usize);
1565 *tfsoff += 8;
1566 reloff = absoff - *tfsoff;
1567 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1568 *tfsoff += 2;
1571 static void write_member_type(FILE *file, const type_t *cont,
1572 int cont_is_complex, const attr_list_t *attrs,
1573 const type_t *type, unsigned int *corroff,
1574 unsigned int *tfsoff)
1576 if (is_embedded_complex(type) && !is_conformant_array(type))
1578 unsigned int absoff;
1579 short reloff;
1581 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1583 absoff = *corroff;
1584 *corroff += 8;
1586 else
1588 absoff = type->typestring_offset;
1590 reloff = absoff - (*tfsoff + 2);
1592 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1593 /* padding is represented using FC_STRUCTPAD* types, so presumably
1594 * this is left over in the format for historical purposes in MIDL
1595 * or rpcrt4. */
1596 print_file(file, 2, "0x0,\n");
1597 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1598 reloff, reloff, absoff);
1599 *tfsoff += 4;
1601 else if (is_ptr(type) || is_conformant_array(type))
1603 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1604 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1605 *tfsoff += 1;
1607 else if (!write_base_type(file, type, TRUE, tfsoff))
1608 error("Unsupported member type %d\n", type_get_type(type));
1611 static void write_array_element_type(FILE *file, const type_t *type,
1612 int cont_is_complex, unsigned int *tfsoff)
1614 type_t *elem = type_array_get_element(type);
1616 if (!is_embedded_complex(elem) && is_ptr(elem))
1618 type_t *ref = type_pointer_get_ref(elem);
1620 if (processed(ref))
1622 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1623 return;
1625 if (!is_string_type(NULL, elem) &&
1626 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1628 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1629 return;
1632 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1635 static void write_end(FILE *file, unsigned int *tfsoff)
1637 if (*tfsoff % 2 == 0)
1639 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1640 *tfsoff += 1;
1642 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1643 *tfsoff += 1;
1646 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1648 unsigned int offset = 0;
1649 var_list_t *fs = type_struct_get_fields(type);
1650 var_t *f;
1652 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1654 type_t *ft = f->type;
1655 unsigned int size = field_memsize( ft, &offset );
1656 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1658 short reloff;
1659 unsigned int absoff = ft->typestring_offset;
1660 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1661 absoff += 8; /* we already have a corr descr, skip it */
1662 reloff = absoff - (*tfsoff + 6);
1663 print_file(file, 0, "/* %d */\n", *tfsoff);
1664 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1665 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1666 write_conf_or_var_desc(file, current_structure, offset, ft,
1667 get_attrp(f->attrs, ATTR_SWITCHIS));
1668 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1669 reloff, reloff, absoff);
1670 *tfsoff += 8;
1672 offset += size;
1676 static int write_pointer_description_offsets(
1677 FILE *file, const attr_list_t *attrs, type_t *type,
1678 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1679 unsigned int *typestring_offset)
1681 int written = 0;
1683 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1684 (is_array(type) && type_array_is_decl_as_ptr(type)))
1686 if (offset_in_memory && offset_in_buffer)
1688 unsigned int memsize;
1690 /* pointer instance
1692 * note that MSDN states that for pointer layouts in structures,
1693 * this is a negative offset from the end of the structure, but
1694 * this statement is incorrect. all offsets are positive */
1695 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
1696 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1698 memsize = type_memsize(type);
1699 *offset_in_memory += memsize;
1700 /* increment these separately as in the case of conformant (varying)
1701 * structures these start at different values */
1702 *offset_in_buffer += memsize;
1704 *typestring_offset += 4;
1706 if (is_ptr(type))
1708 type_t *ref = type_pointer_get_ref(type);
1710 if (is_string_type(attrs, type))
1711 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1712 else if (processed(ref))
1713 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1714 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1715 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1716 else
1717 error("write_pointer_description_offsets: type format string unknown\n");
1719 else
1721 unsigned int offset = type->typestring_offset;
1722 /* skip over the pointer that is written for strings, since a
1723 * pointer has to be written in-place here */
1724 if (is_string_type(attrs, type))
1725 offset += 4;
1726 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1729 return 1;
1732 if (is_array(type))
1734 return write_pointer_description_offsets(
1735 file, attrs, type_array_get_element(type), offset_in_memory,
1736 offset_in_buffer, typestring_offset);
1738 else if (is_non_complex_struct(type))
1740 /* otherwise search for interesting fields to parse */
1741 const var_t *v;
1742 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1744 if (offset_in_memory && offset_in_buffer)
1746 unsigned int padding;
1747 unsigned int align = 0;
1748 type_memsize_and_alignment(v->type, &align);
1749 padding = ROUNDING(*offset_in_memory, align);
1750 *offset_in_memory += padding;
1751 *offset_in_buffer += padding;
1753 written += write_pointer_description_offsets(
1754 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1755 typestring_offset);
1758 else
1760 if (offset_in_memory && offset_in_buffer)
1762 unsigned int memsize = type_memsize(type);
1763 *offset_in_memory += memsize;
1764 /* increment these separately as in the case of conformant (varying)
1765 * structures these start at different values */
1766 *offset_in_buffer += memsize;
1770 return written;
1773 static int write_no_repeat_pointer_descriptions(
1774 FILE *file, const attr_list_t *attrs, type_t *type,
1775 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1776 unsigned int *typestring_offset)
1778 int written = 0;
1780 if (is_ptr(type) ||
1781 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1783 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1784 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1785 *typestring_offset += 2;
1787 return write_pointer_description_offsets(file, attrs, type,
1788 offset_in_memory, offset_in_buffer, typestring_offset);
1791 if (is_non_complex_struct(type))
1793 const var_t *v;
1794 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1796 if (offset_in_memory && offset_in_buffer)
1798 unsigned int padding;
1799 unsigned int align = 0;
1800 type_memsize_and_alignment(v->type, &align);
1801 padding = ROUNDING(*offset_in_memory, align);
1802 *offset_in_memory += padding;
1803 *offset_in_buffer += padding;
1805 written += write_no_repeat_pointer_descriptions(
1806 file, v->attrs, v->type,
1807 offset_in_memory, offset_in_buffer, typestring_offset);
1810 else
1812 unsigned int memsize = type_memsize(type);
1813 *offset_in_memory += memsize;
1814 /* increment these separately as in the case of conformant (varying)
1815 * structures these start at different values */
1816 *offset_in_buffer += memsize;
1819 return written;
1822 /* Note: if file is NULL return value is number of pointers to write, else
1823 * it is the number of type format characters written */
1824 static int write_fixed_array_pointer_descriptions(
1825 FILE *file, const attr_list_t *attrs, type_t *type,
1826 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1827 unsigned int *typestring_offset)
1829 int pointer_count = 0;
1831 if (type_get_type(type) == TYPE_ARRAY &&
1832 !type_array_has_conformance(type) && !type_array_has_variance(type))
1834 unsigned int temp = 0;
1835 /* unfortunately, this needs to be done in two passes to avoid
1836 * writing out redundant FC_FIXED_REPEAT descriptions */
1837 pointer_count = write_pointer_description_offsets(
1838 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1839 if (pointer_count > 0)
1841 unsigned int increment_size;
1842 unsigned int offset_of_array_pointer_mem = 0;
1843 unsigned int offset_of_array_pointer_buf = 0;
1845 increment_size = type_memsize(type_array_get_element(type));
1847 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
1848 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1849 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type), type_array_get_dim(type));
1850 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1851 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1852 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1853 *typestring_offset += 10;
1855 pointer_count = write_pointer_description_offsets(
1856 file, attrs, type, &offset_of_array_pointer_mem,
1857 &offset_of_array_pointer_buf, typestring_offset);
1860 else if (type_get_type(type) == TYPE_STRUCT)
1862 const var_t *v;
1863 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1865 if (offset_in_memory && offset_in_buffer)
1867 unsigned int padding;
1868 unsigned int align = 0;
1869 type_memsize_and_alignment(v->type, &align);
1870 padding = ROUNDING(*offset_in_memory, align);
1871 *offset_in_memory += padding;
1872 *offset_in_buffer += padding;
1874 pointer_count += write_fixed_array_pointer_descriptions(
1875 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1876 typestring_offset);
1879 else
1881 if (offset_in_memory && offset_in_buffer)
1883 unsigned int memsize;
1884 memsize = type_memsize(type);
1885 *offset_in_memory += memsize;
1886 /* increment these separately as in the case of conformant (varying)
1887 * structures these start at different values */
1888 *offset_in_buffer += memsize;
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_conformant_array_pointer_descriptions(
1898 FILE *file, const attr_list_t *attrs, type_t *type,
1899 unsigned int offset_in_memory, unsigned int *typestring_offset)
1901 int pointer_count = 0;
1903 if (is_conformant_array(type) && !type_array_has_variance(type))
1905 unsigned int temp = 0;
1906 /* unfortunately, this needs to be done in two passes to avoid
1907 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1908 pointer_count = write_pointer_description_offsets(
1909 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1910 if (pointer_count > 0)
1912 unsigned int increment_size;
1913 unsigned int offset_of_array_pointer_mem = offset_in_memory;
1914 unsigned int offset_of_array_pointer_buf = offset_in_memory;
1916 increment_size = type_memsize(type_array_get_element(type));
1918 if (increment_size > USHRT_MAX)
1919 error("array size of %u bytes is too large\n", increment_size);
1921 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1922 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
1923 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1924 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1925 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1926 *typestring_offset += 8;
1928 pointer_count = write_pointer_description_offsets(
1929 file, attrs, type_array_get_element(type),
1930 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
1931 typestring_offset);
1935 return pointer_count;
1938 /* Note: if file is NULL return value is number of pointers to write, else
1939 * it is the number of type format characters written */
1940 static int write_varying_array_pointer_descriptions(
1941 FILE *file, const attr_list_t *attrs, type_t *type,
1942 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1943 unsigned int *typestring_offset)
1945 int pointer_count = 0;
1947 if (is_array(type) && type_array_has_variance(type))
1949 unsigned int temp = 0;
1950 /* unfortunately, this needs to be done in two passes to avoid
1951 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1952 pointer_count = write_pointer_description_offsets(
1953 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
1954 if (pointer_count > 0)
1956 unsigned int increment_size;
1958 increment_size = type_memsize(type_array_get_element(type));
1960 if (increment_size > USHRT_MAX)
1961 error("array size of %u bytes is too large\n", increment_size);
1963 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
1964 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
1965 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size, increment_size);
1966 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
1967 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count, pointer_count);
1968 *typestring_offset += 8;
1970 pointer_count = write_pointer_description_offsets(
1971 file, attrs, type_array_get_element(type), offset_in_memory,
1972 offset_in_buffer, typestring_offset);
1975 else if (type_get_type(type) == TYPE_STRUCT)
1977 const var_t *v;
1978 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1980 if (offset_in_memory && offset_in_buffer)
1982 unsigned int align = 0, padding;
1984 if (is_array(v->type) && type_array_has_variance(v->type))
1986 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
1987 /* skip over variance and offset in buffer */
1988 *offset_in_buffer += 8;
1991 type_memsize_and_alignment(v->type, &align);
1992 padding = ROUNDING(*offset_in_memory, align);
1993 *offset_in_memory += padding;
1994 *offset_in_buffer += padding;
1996 pointer_count += write_varying_array_pointer_descriptions(
1997 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1998 typestring_offset);
2001 else
2003 if (offset_in_memory && offset_in_buffer)
2005 unsigned int memsize = type_memsize(type);
2006 *offset_in_memory += memsize;
2007 /* increment these separately as in the case of conformant (varying)
2008 * structures these start at different values */
2009 *offset_in_buffer += memsize;
2013 return pointer_count;
2016 static void write_pointer_description(FILE *file, type_t *type,
2017 unsigned int *typestring_offset)
2019 unsigned int offset_in_buffer;
2020 unsigned int offset_in_memory;
2022 /* pass 1: search for single instance of a pointer (i.e. don't descend
2023 * into arrays) */
2024 if (!is_array(type))
2026 offset_in_memory = 0;
2027 offset_in_buffer = 0;
2028 write_no_repeat_pointer_descriptions(
2029 file, NULL, type,
2030 &offset_in_memory, &offset_in_buffer, typestring_offset);
2033 /* pass 2: search for pointers in fixed arrays */
2034 offset_in_memory = 0;
2035 offset_in_buffer = 0;
2036 write_fixed_array_pointer_descriptions(
2037 file, NULL, type,
2038 &offset_in_memory, &offset_in_buffer, typestring_offset);
2040 /* pass 3: search for pointers in conformant only arrays (but don't descend
2041 * into conformant varying or varying arrays) */
2042 if (is_conformant_array(type) &&
2043 (type_array_is_decl_as_ptr(type) || !current_structure))
2044 write_conformant_array_pointer_descriptions(
2045 file, NULL, type, 0, typestring_offset);
2046 else if (type_get_type(type) == TYPE_STRUCT &&
2047 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2049 type_t *carray = find_array_or_string_in_struct(type)->type;
2050 write_conformant_array_pointer_descriptions( file, NULL, carray,
2051 type_memsize(type), typestring_offset);
2054 /* pass 4: search for pointers in varying arrays */
2055 offset_in_memory = 0;
2056 offset_in_buffer = 0;
2057 write_varying_array_pointer_descriptions(
2058 file, NULL, type,
2059 &offset_in_memory, &offset_in_buffer, typestring_offset);
2062 int is_declptr(const type_t *t)
2064 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2067 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2068 type_t *type, int toplevel_param,
2069 const char *name, unsigned int *typestring_offset)
2071 unsigned int start_offset;
2072 unsigned char rtype;
2073 type_t *elem_type;
2075 start_offset = *typestring_offset;
2076 update_tfsoff(type, start_offset, file);
2078 if (is_declptr(type))
2080 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2081 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2082 if (!pointer_type)
2083 pointer_type = RPC_FC_RP;
2084 print_start_tfs_comment(file, type, *typestring_offset);
2085 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2086 pointer_type, flag, string_of_type(pointer_type),
2087 flag ? " [simple_pointer]" : "");
2088 *typestring_offset += 2;
2089 if (!flag)
2091 print_file(file, 2, "NdrFcShort(0x2),\n");
2092 *typestring_offset += 2;
2096 if (is_array(type))
2097 elem_type = type_array_get_element(type);
2098 else
2099 elem_type = type_pointer_get_ref(type);
2101 if (type_get_type(elem_type) != TYPE_BASIC)
2103 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2104 return start_offset;
2107 rtype = get_basic_fc(elem_type);
2108 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2110 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2111 return start_offset;
2114 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2116 unsigned int dim = type_array_get_dim(type);
2118 /* FIXME: multi-dimensional array */
2119 if (0xffffu < dim)
2120 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2121 name, 0xffffu, dim - 0xffffu);
2123 if (rtype == RPC_FC_WCHAR)
2124 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2125 else
2126 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2127 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2128 *typestring_offset += 2;
2130 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim, dim);
2131 *typestring_offset += 2;
2133 return start_offset;
2135 else if (is_conformant_array(type))
2137 if (rtype == RPC_FC_WCHAR)
2138 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2139 else
2140 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2141 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2142 *typestring_offset += 2;
2144 *typestring_offset += write_conf_or_var_desc(
2145 file, current_structure,
2146 (!type_array_is_decl_as_ptr(type) && current_structure
2147 ? type_memsize(current_structure)
2148 : 0),
2149 type, type_array_get_conformance(type));
2151 return start_offset;
2153 else
2155 if (rtype == RPC_FC_WCHAR)
2156 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2157 else
2158 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2159 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2160 *typestring_offset += 2;
2162 return start_offset;
2166 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2167 const char *name, unsigned int *typestring_offset)
2169 const expr_t *length_is = type_array_get_variance(type);
2170 const expr_t *size_is = type_array_get_conformance(type);
2171 unsigned int align = 0;
2172 unsigned int size;
2173 unsigned int start_offset;
2174 unsigned char fc;
2175 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2176 unsigned int baseoff
2177 = !type_array_is_decl_as_ptr(type) && current_structure
2178 ? type_memsize(current_structure)
2179 : 0;
2181 if (!pointer_type)
2182 pointer_type = RPC_FC_RP;
2184 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2186 align = 0;
2187 size = type_memsize_and_alignment((is_conformant_array(type) ? type_array_get_element(type) : type), &align);
2188 fc = get_array_fc(type);
2190 start_offset = *typestring_offset;
2191 update_tfsoff(type, start_offset, file);
2192 print_start_tfs_comment(file, type, start_offset);
2193 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2194 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2195 *typestring_offset += 2;
2197 align = 0;
2198 if (fc != RPC_FC_BOGUS_ARRAY)
2200 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2202 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2203 *typestring_offset += 4;
2205 else
2207 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size, size);
2208 *typestring_offset += 2;
2211 if (is_conformant_array(type))
2212 *typestring_offset
2213 += write_conf_or_var_desc(file, current_structure, baseoff,
2214 type, size_is);
2216 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2218 unsigned int elsize = type_memsize(type_array_get_element(type));
2219 unsigned int dim = type_array_get_dim(type);
2221 if (fc == RPC_FC_LGVARRAY)
2223 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2224 *typestring_offset += 4;
2226 else
2228 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2229 *typestring_offset += 2;
2232 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize, elsize);
2233 *typestring_offset += 2;
2236 if (length_is)
2237 *typestring_offset
2238 += write_conf_or_var_desc(file, current_structure, baseoff,
2239 type, length_is);
2241 if (type_has_pointers(type_array_get_element(type)) &&
2242 (type_array_is_decl_as_ptr(type) || !current_structure))
2244 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2245 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2246 *typestring_offset += 2;
2247 write_pointer_description(file, type, typestring_offset);
2248 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2249 *typestring_offset += 1;
2252 write_array_element_type(file, type, FALSE, typestring_offset);
2253 write_end(file, typestring_offset);
2255 else
2257 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2258 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim, dim);
2259 *typestring_offset += 2;
2260 *typestring_offset
2261 += write_conf_or_var_desc(file, current_structure, baseoff,
2262 type, size_is);
2263 *typestring_offset
2264 += write_conf_or_var_desc(file, current_structure, baseoff,
2265 type, length_is);
2267 write_array_element_type(file, type, TRUE, typestring_offset);
2268 write_end(file, typestring_offset);
2271 return start_offset;
2274 static const var_t *find_array_or_string_in_struct(const type_t *type)
2276 const var_list_t *fields = type_struct_get_fields(type);
2277 const var_t *last_field;
2278 const type_t *ft;
2280 if (!fields || list_empty(fields))
2281 return NULL;
2283 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2284 ft = last_field->type;
2286 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2287 return last_field;
2289 if (type_get_type(ft) == TYPE_STRUCT)
2290 return find_array_or_string_in_struct(ft);
2291 else
2292 return NULL;
2295 static void write_struct_members(FILE *file, const type_t *type,
2296 int is_complex, unsigned int *corroff,
2297 unsigned int *typestring_offset)
2299 const var_t *field;
2300 unsigned short offset = 0;
2301 unsigned int salign = 1;
2302 int padding;
2303 var_list_t *fields = type_struct_get_fields(type);
2305 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2307 type_t *ft = field->type;
2308 unsigned int align = 0;
2309 unsigned int size = type_memsize_and_alignment(ft, &align);
2310 align = clamp_align(align);
2311 if (salign < align) salign = align;
2313 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2315 if ((align - 1) & offset)
2317 unsigned char fc = 0;
2318 switch (align)
2320 case 2:
2321 fc = RPC_FC_ALIGNM2;
2322 break;
2323 case 4:
2324 fc = RPC_FC_ALIGNM4;
2325 break;
2326 case 8:
2327 fc = RPC_FC_ALIGNM8;
2328 break;
2329 default:
2330 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2332 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2333 offset = ROUND_SIZE(offset, align);
2334 *typestring_offset += 1;
2336 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2337 typestring_offset);
2338 offset += size;
2342 padding = ROUNDING(offset, salign);
2343 if (padding)
2345 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2346 RPC_FC_STRUCTPAD1 + padding - 1,
2347 padding);
2348 *typestring_offset += 1;
2351 write_end(file, typestring_offset);
2354 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2355 const char *name, unsigned int *tfsoff)
2357 const type_t *save_current_structure = current_structure;
2358 unsigned int total_size;
2359 const var_t *array;
2360 unsigned int start_offset;
2361 unsigned int array_offset;
2362 unsigned int align = 0;
2363 unsigned int corroff;
2364 var_t *f;
2365 unsigned char fc = get_struct_fc(type);
2366 var_list_t *fields = type_struct_get_fields(type);
2368 guard_rec(type);
2369 current_structure = type;
2371 total_size = type_memsize_and_alignment(type, &align);
2372 if (total_size > USHRT_MAX)
2373 error("structure size for %s exceeds %d bytes by %d bytes\n",
2374 name, USHRT_MAX, total_size - USHRT_MAX);
2376 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2377 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
2379 array = find_array_or_string_in_struct(type);
2380 if (array && !processed(array->type))
2381 array_offset
2382 = is_string_type(array->attrs, array->type)
2383 ? write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff)
2384 : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2386 corroff = *tfsoff;
2387 write_descriptors(file, type, tfsoff);
2389 start_offset = *tfsoff;
2390 update_tfsoff(type, start_offset, file);
2391 print_start_tfs_comment(file, type, start_offset);
2392 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2393 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2394 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size, total_size);
2395 *tfsoff += 4;
2397 if (array)
2399 unsigned int absoff = array->type->typestring_offset;
2400 short reloff = absoff - *tfsoff;
2401 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2402 reloff, reloff, absoff);
2403 *tfsoff += 2;
2405 else if (fc == RPC_FC_BOGUS_STRUCT)
2407 print_file(file, 2, "NdrFcShort(0x0),\n");
2408 *tfsoff += 2;
2411 if (fc == RPC_FC_BOGUS_STRUCT)
2413 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2414 nothing is written to file yet. On the actual writing pass,
2415 this will have been updated. */
2416 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2417 int reloff = absoff - *tfsoff;
2418 assert( reloff >= 0 );
2419 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2420 reloff, reloff, absoff);
2421 *tfsoff += 2;
2423 else if ((fc == RPC_FC_PSTRUCT) ||
2424 (fc == RPC_FC_CPSTRUCT) ||
2425 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
2427 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2428 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2429 *tfsoff += 2;
2430 write_pointer_description(file, type, tfsoff);
2431 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2432 *tfsoff += 1;
2435 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2436 tfsoff);
2438 if (fc == RPC_FC_BOGUS_STRUCT)
2440 const var_t *f;
2442 type->ptrdesc = *tfsoff;
2443 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2445 type_t *ft = f->type;
2446 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
2448 case TGT_POINTER:
2449 if (is_string_type(f->attrs, ft))
2450 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2451 else
2452 write_pointer_tfs(file, f->attrs, ft,
2453 type_pointer_get_ref(ft)->typestring_offset,
2454 FALSE, tfsoff);
2455 break;
2456 case TGT_ARRAY:
2457 if (type_array_is_decl_as_ptr(ft))
2459 unsigned int offset;
2461 print_file(file, 0, "/* %d */\n", *tfsoff);
2463 offset = ft->typestring_offset;
2464 /* skip over the pointer that is written for strings, since a
2465 * pointer has to be written in-place here */
2466 if (is_string_type(f->attrs, ft))
2467 offset += 4;
2468 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2470 break;
2471 default:
2472 break;
2475 if (type->ptrdesc == *tfsoff)
2476 type->ptrdesc = 0;
2479 current_structure = save_current_structure;
2480 return start_offset;
2483 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2485 if (t == NULL)
2487 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2489 else
2491 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2493 unsigned char fc;
2494 if (type_get_type(t) == TYPE_BASIC)
2495 fc = get_basic_fc(t);
2496 else
2497 fc = get_enum_fc(t);
2498 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2499 fc, string_of_type(fc));
2501 else if (t->typestring_offset)
2503 short reloff = t->typestring_offset - *tfsoff;
2504 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2505 reloff, reloff, t->typestring_offset);
2507 else
2508 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2511 *tfsoff += 2;
2514 static unsigned int write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2516 unsigned int start_offset;
2517 unsigned int size;
2518 var_list_t *fields;
2519 unsigned int nbranch = 0;
2520 type_t *deftype = NULL;
2521 short nodeftype = 0xffff;
2522 var_t *f;
2524 guard_rec(type);
2526 size = type_memsize(type);
2528 fields = type_union_get_cases(type);
2530 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2532 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2533 if (cases)
2534 nbranch += list_count(cases);
2535 if (f->type)
2536 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2539 start_offset = *tfsoff;
2540 update_tfsoff(type, start_offset, file);
2541 print_start_tfs_comment(file, type, start_offset);
2542 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2544 const var_t *sv = type_union_get_switch_value(type);
2545 const type_t *st = sv->type;
2546 unsigned char fc;
2548 if (type_get_type(st) == TYPE_BASIC)
2550 switch (get_basic_fc(st))
2552 case RPC_FC_CHAR:
2553 case RPC_FC_SMALL:
2554 case RPC_FC_BYTE:
2555 case RPC_FC_USMALL:
2556 case RPC_FC_WCHAR:
2557 case RPC_FC_SHORT:
2558 case RPC_FC_USHORT:
2559 case RPC_FC_LONG:
2560 case RPC_FC_ULONG:
2561 fc = get_basic_fc(st);
2562 break;
2563 default:
2564 fc = 0;
2565 error("union switch type must be an integer, char, or enum\n");
2568 else if (type_get_type(st) == TYPE_ENUM)
2569 fc = get_enum_fc(st);
2570 else
2571 error("union switch type must be an integer, char, or enum\n");
2573 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2574 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2575 0x40 | fc, string_of_type(fc));
2576 *tfsoff += 2;
2578 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2580 static const expr_t dummy_expr; /* FIXME */
2581 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2582 unsigned char fc;
2584 if (type_get_type(st) == TYPE_BASIC)
2586 switch (get_basic_fc(st))
2588 case RPC_FC_CHAR:
2589 case RPC_FC_SMALL:
2590 case RPC_FC_USMALL:
2591 case RPC_FC_SHORT:
2592 case RPC_FC_USHORT:
2593 case RPC_FC_LONG:
2594 case RPC_FC_ULONG:
2595 case RPC_FC_ENUM16:
2596 case RPC_FC_ENUM32:
2597 fc = get_basic_fc(st);
2598 break;
2599 default:
2600 fc = 0;
2601 error("union switch type must be an integer, char, or enum\n");
2604 else if (type_get_type(st) == TYPE_ENUM)
2605 fc = get_enum_fc(st);
2606 else
2607 error("union switch type must be an integer, char, or enum\n");
2609 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2610 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2611 fc, string_of_type(fc));
2612 *tfsoff += 2;
2614 *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
2615 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2616 *tfsoff += 2;
2617 print_file(file, 0, "/* %u */\n", *tfsoff);
2620 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size, size);
2621 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch, nbranch);
2622 *tfsoff += 4;
2624 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2626 type_t *ft = f->type;
2627 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2628 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2629 expr_t *c;
2631 if (cases == NULL && !deflt)
2632 error("union field %s with neither case nor default attribute\n", f->name);
2634 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2636 /* MIDL doesn't check for duplicate cases, even though that seems
2637 like a reasonable thing to do, it just dumps them to the TFS
2638 like we're going to do here. */
2639 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2640 *tfsoff += 4;
2641 write_branch_type(file, ft, tfsoff);
2644 /* MIDL allows multiple default branches, even though that seems
2645 illogical, it just chooses the last one, which is what we will
2646 do. */
2647 if (deflt)
2649 deftype = ft;
2650 nodeftype = 0;
2654 if (deftype)
2656 write_branch_type(file, deftype, tfsoff);
2658 else
2660 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2661 *tfsoff += 2;
2664 return start_offset;
2667 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2668 unsigned int *typeformat_offset)
2670 unsigned int i;
2671 unsigned int start_offset = *typeformat_offset;
2672 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2674 print_start_tfs_comment(file, type, start_offset);
2676 if (iid)
2678 print_file(file, 2, "0x2f, /* FC_IP */\n");
2679 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2680 *typeformat_offset
2681 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
2683 else
2685 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2686 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2688 if (! uuid)
2689 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2691 update_tfsoff(type, start_offset, file);
2692 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2693 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2694 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2695 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2696 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2697 for (i = 0; i < 8; ++i)
2698 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2700 if (file)
2701 fprintf(file, "\n");
2703 *typeformat_offset += 18;
2705 return start_offset;
2708 static unsigned int write_contexthandle_tfs(FILE *file,
2709 const attr_list_t *attrs,
2710 const type_t *type,
2711 unsigned int *typeformat_offset)
2713 unsigned int start_offset = *typeformat_offset;
2714 unsigned char flags = 0;
2716 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2717 flags |= NDR_STRICT_CONTEXT_HANDLE;
2719 if (is_ptr(type))
2720 flags |= 0x80;
2721 if (is_attr(attrs, ATTR_IN))
2723 flags |= 0x40;
2724 if (!is_attr(attrs, ATTR_OUT))
2725 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2727 if (is_attr(attrs, ATTR_OUT))
2728 flags |= 0x20;
2730 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2731 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2732 /* return and can't be null values overlap */
2733 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2734 print_file(file, 0, "can't be null, ");
2735 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2736 print_file(file, 0, "serialize, ");
2737 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2738 print_file(file, 0, "no serialize, ");
2739 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2740 print_file(file, 0, "strict, ");
2741 if ((flags & 0x21) == 0x20)
2742 print_file(file, 0, "out, ");
2743 if ((flags & 0x21) == 0x21)
2744 print_file(file, 0, "return, ");
2745 if (flags & 0x40)
2746 print_file(file, 0, "in, ");
2747 if (flags & 0x80)
2748 print_file(file, 0, "via ptr, ");
2749 print_file(file, 0, "*/\n");
2750 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2751 print_file(file, 2, "0, /* FIXME: param num */\n");
2752 *typeformat_offset += 4;
2754 return start_offset;
2757 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
2758 type_t *type, expr_list_t *range_list,
2759 unsigned int *typeformat_offset)
2761 unsigned char fc;
2762 unsigned int start_offset = *typeformat_offset;
2763 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
2764 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
2766 if (type_get_type(type) == TYPE_BASIC)
2767 fc = get_basic_fc(type);
2768 else
2769 fc = get_enum_fc(type);
2771 /* fc must fit in lower 4-bits of 8-bit field below */
2772 assert(fc <= 0xf);
2774 print_file(file, 0, "/* %u */\n", *typeformat_offset);
2775 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
2776 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2777 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
2778 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
2779 *typeformat_offset += 10;
2781 return start_offset;
2784 static unsigned int write_type_tfs(FILE *file, int indent,
2785 const attr_list_t *attrs, type_t *type,
2786 const char *name,
2787 enum type_context context,
2788 unsigned int *typeformat_offset)
2790 unsigned int offset;
2792 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
2794 case TGT_CTXT_HANDLE:
2795 case TGT_CTXT_HANDLE_POINTER:
2796 return write_contexthandle_tfs(file, attrs, type, typeformat_offset);
2797 case TGT_USER_TYPE:
2798 write_user_tfs(file, type, typeformat_offset);
2799 return type->typestring_offset;
2800 case TGT_STRING:
2801 return write_string_tfs(file, attrs, type,
2802 context == TYPE_CONTEXT_TOPLEVELPARAM,
2803 name, typeformat_offset);
2804 case TGT_ARRAY:
2806 unsigned int off;
2807 /* conformant and pointer arrays are handled specially */
2808 if ((context != TYPE_CONTEXT_CONTAINER &&
2809 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
2810 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
2811 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
2812 else
2813 off = 0;
2814 if (context != TYPE_CONTEXT_CONTAINER &&
2815 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
2817 int ptr_type;
2818 ptr_type = get_pointer_fc(type, attrs,
2819 context == TYPE_CONTEXT_TOPLEVELPARAM);
2820 if (ptr_type != RPC_FC_RP)
2822 unsigned int absoff = type->typestring_offset;
2823 short reloff = absoff - (*typeformat_offset + 2);
2824 off = *typeformat_offset;
2825 print_file(file, 0, "/* %d */\n", off);
2826 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
2827 string_of_type(ptr_type));
2828 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2829 reloff, reloff, absoff);
2830 *typeformat_offset += 4;
2833 return off;
2835 case TGT_STRUCT:
2836 if (processed(type)) return type->typestring_offset;
2837 return write_struct_tfs(file, type, name, typeformat_offset);
2838 case TGT_UNION:
2839 if (processed(type)) return type->typestring_offset;
2840 return write_union_tfs(file, type, typeformat_offset);
2841 case TGT_ENUM:
2842 case TGT_BASIC:
2843 /* nothing to do */
2844 return 0;
2845 case TGT_RANGE:
2847 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
2848 if (!range_list)
2849 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
2850 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
2852 case TGT_IFACE_POINTER:
2853 return write_ip_tfs(file, attrs, type, typeformat_offset);
2854 case TGT_POINTER:
2855 if (processed(type_pointer_get_ref(type)))
2856 offset = type_pointer_get_ref(type)->typestring_offset;
2857 else
2859 enum type_context ref_context;
2860 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
2861 ref_context = TYPE_CONTEXT_PARAM;
2862 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
2863 ref_context = TYPE_CONTEXT_CONTAINER;
2864 else
2865 ref_context = context;
2866 offset = write_type_tfs(
2867 file, indent, attrs, type_pointer_get_ref(type), name,
2868 ref_context, typeformat_offset);
2870 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
2871 return 0;
2872 else
2873 return write_pointer_tfs(file, attrs, type, offset,
2874 context == TYPE_CONTEXT_TOPLEVELPARAM,
2875 typeformat_offset);
2876 case TGT_INVALID:
2877 break;
2879 error("invalid type %s for var %s\n", type->name, name);
2880 return 0;
2883 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2884 const char *name, int write_ptr, unsigned int *tfsoff)
2886 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
2889 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
2890 type_pred_t pred, unsigned int *typeformat_offset)
2892 const var_t *var;
2893 const statement_t *stmt;
2895 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2897 const type_t *iface;
2898 const statement_t *stmt_func;
2900 if (stmt->type == STMT_LIBRARY)
2902 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
2903 continue;
2905 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
2906 continue;
2908 iface = stmt->u.type;
2909 if (!pred(iface))
2910 continue;
2912 current_iface = iface;
2913 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
2915 const var_t *func = stmt_func->u.var;
2916 if (is_local(func->attrs)) continue;
2918 if (!is_void(type_function_get_rettype(func->type)))
2920 update_tfsoff(type_function_get_rettype(func->type),
2921 write_type_tfs(
2922 file, 2, func->attrs,
2923 type_function_get_rettype(func->type),
2924 func->name, TYPE_CONTEXT_PARAM,
2925 typeformat_offset),
2926 file);
2929 if (type_get_function_args(func->type))
2930 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2931 update_tfsoff(
2932 var->type,
2933 write_type_tfs(
2934 file, 2, var->attrs, var->type, var->name,
2935 TYPE_CONTEXT_TOPLEVELPARAM,
2936 typeformat_offset),
2937 file);
2941 return *typeformat_offset + 1;
2944 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2946 unsigned int typeformat_offset = 2;
2948 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2952 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2954 int indent = 0;
2956 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2957 print_file(file, indent, "{\n");
2958 indent++;
2959 print_file(file, indent, "0,\n");
2960 print_file(file, indent, "{\n");
2961 indent++;
2962 print_file(file, indent, "NdrFcShort(0x0),\n");
2964 set_all_tfswrite(TRUE);
2965 process_tfs(file, stmts, pred);
2967 print_file(file, indent, "0x0\n");
2968 indent--;
2969 print_file(file, indent, "}\n");
2970 indent--;
2971 print_file(file, indent, "};\n");
2972 print_file(file, indent, "\n");
2975 static unsigned int get_required_buffer_size_type(
2976 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
2978 *alignment = 0;
2979 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
2981 case TGT_USER_TYPE:
2983 const char *uname;
2984 const type_t *utype = get_user_type(type, &uname);
2985 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
2987 case TGT_BASIC:
2988 switch (get_basic_fc(type))
2990 case RPC_FC_BYTE:
2991 case RPC_FC_CHAR:
2992 case RPC_FC_USMALL:
2993 case RPC_FC_SMALL:
2994 *alignment = 4;
2995 return 1;
2997 case RPC_FC_WCHAR:
2998 case RPC_FC_USHORT:
2999 case RPC_FC_SHORT:
3000 *alignment = 4;
3001 return 2;
3003 case RPC_FC_ULONG:
3004 case RPC_FC_LONG:
3005 case RPC_FC_FLOAT:
3006 case RPC_FC_ERROR_STATUS_T:
3007 *alignment = 4;
3008 return 4;
3010 case RPC_FC_HYPER:
3011 case RPC_FC_DOUBLE:
3012 *alignment = 8;
3013 return 8;
3015 case RPC_FC_INT3264:
3016 case RPC_FC_UINT3264:
3017 assert( pointer_size );
3018 *alignment = pointer_size;
3019 return pointer_size;
3021 case RPC_FC_IGNORE:
3022 case RPC_FC_BIND_PRIMITIVE:
3023 return 0;
3025 default:
3026 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3027 get_basic_fc(type));
3028 return 0;
3030 break;
3032 case TGT_ENUM:
3033 switch (get_enum_fc(type))
3035 case RPC_FC_ENUM32:
3036 *alignment = 4;
3037 return 4;
3038 case RPC_FC_ENUM16:
3039 *alignment = 4;
3040 return 2;
3042 break;
3044 case TGT_STRUCT:
3045 if (get_struct_fc(type) == RPC_FC_STRUCT)
3047 if (!type_struct_get_fields(type)) return 0;
3048 return fields_memsize(type_struct_get_fields(type), alignment);
3050 break;
3052 case TGT_POINTER:
3053 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3055 const type_t *ref = type_pointer_get_ref(type);
3056 switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3058 case TGT_BASIC:
3059 case TGT_ENUM:
3060 case TGT_RANGE:
3061 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3062 case TGT_STRUCT:
3063 if (get_struct_fc(ref) == RPC_FC_STRUCT)
3064 return get_required_buffer_size_type( ref, name, NULL, FALSE, alignment );
3065 break;
3066 case TGT_USER_TYPE:
3067 case TGT_CTXT_HANDLE:
3068 case TGT_CTXT_HANDLE_POINTER:
3069 case TGT_STRING:
3070 case TGT_POINTER:
3071 case TGT_ARRAY:
3072 case TGT_IFACE_POINTER:
3073 case TGT_UNION:
3074 case TGT_INVALID:
3075 break;
3078 break;
3080 case TGT_ARRAY:
3081 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3082 return type_array_get_dim(type) *
3083 get_required_buffer_size_type(type_array_get_element(type), name,
3084 NULL, FALSE, alignment);
3086 default:
3087 break;
3089 return 0;
3092 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3094 int in_attr = is_attr(var->attrs, ATTR_IN);
3095 int out_attr = is_attr(var->attrs, ATTR_OUT);
3097 if (!in_attr && !out_attr)
3098 in_attr = 1;
3100 *alignment = 0;
3102 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3103 pass == PASS_RETURN)
3105 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3107 *alignment = 4;
3108 return 20;
3111 if (!is_string_type(var->attrs, var->type))
3112 return get_required_buffer_size_type(var->type, var->name,
3113 var->attrs, TRUE, alignment);
3115 return 0;
3118 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3120 const var_t *var;
3121 unsigned int total_size = 0, alignment;
3123 if (type_get_function_args(func->type))
3125 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3127 total_size += get_required_buffer_size(var, &alignment, pass);
3128 total_size += alignment;
3132 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3134 var_t v = *func;
3135 v.type = type_function_get_rettype(func->type);
3136 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3137 total_size += alignment;
3139 return total_size;
3142 static void print_phase_function(FILE *file, int indent, const char *type,
3143 const char *local_var_prefix, enum remoting_phase phase,
3144 const var_t *var, unsigned int type_offset)
3146 const char *function;
3147 switch (phase)
3149 case PHASE_BUFFERSIZE:
3150 function = "BufferSize";
3151 break;
3152 case PHASE_MARSHAL:
3153 function = "Marshall";
3154 break;
3155 case PHASE_UNMARSHAL:
3156 function = "Unmarshall";
3157 break;
3158 case PHASE_FREE:
3159 function = "Free";
3160 break;
3161 default:
3162 assert(0);
3163 return;
3166 print_file(file, indent, "Ndr%s%s(\n", type, function);
3167 indent++;
3168 print_file(file, indent, "&__frame->_StubMsg,\n");
3169 print_file(file, indent, "%s%s%s%s%s,\n",
3170 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3171 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3172 local_var_prefix,
3173 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3174 var->name);
3175 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3176 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3177 if (phase == PHASE_UNMARSHAL)
3178 print_file(file, indent, "0);\n");
3179 indent--;
3182 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3183 enum remoting_phase phase, enum pass pass, const var_t *var,
3184 const char *varname)
3186 type_t *type = var->type;
3187 unsigned int size;
3188 unsigned int alignment = 0;
3190 /* no work to do for other phases, buffer sizing is done elsewhere */
3191 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3192 return;
3194 if (type_get_type(type) == TYPE_ENUM ||
3195 (type_get_type(type) == TYPE_BASIC &&
3196 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3197 pointer_size != 4))
3199 unsigned char fc;
3201 if (type_get_type(type) == TYPE_ENUM)
3202 fc = get_enum_fc(type);
3203 else
3204 fc = get_basic_fc(type);
3206 if (phase == PHASE_MARSHAL)
3207 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3208 else
3209 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3210 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3211 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3212 local_var_prefix,
3213 var->name);
3214 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3216 else
3218 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3219 switch (get_basic_fc(ref))
3221 case RPC_FC_BYTE:
3222 case RPC_FC_CHAR:
3223 case RPC_FC_SMALL:
3224 case RPC_FC_USMALL:
3225 size = 1;
3226 alignment = 1;
3227 break;
3229 case RPC_FC_WCHAR:
3230 case RPC_FC_USHORT:
3231 case RPC_FC_SHORT:
3232 size = 2;
3233 alignment = 2;
3234 break;
3236 case RPC_FC_ULONG:
3237 case RPC_FC_LONG:
3238 case RPC_FC_FLOAT:
3239 case RPC_FC_ERROR_STATUS_T:
3240 /* pointer_size must be 4 if we got here in these two cases */
3241 case RPC_FC_INT3264:
3242 case RPC_FC_UINT3264:
3243 size = 4;
3244 alignment = 4;
3245 break;
3247 case RPC_FC_HYPER:
3248 case RPC_FC_DOUBLE:
3249 size = 8;
3250 alignment = 8;
3251 break;
3253 case RPC_FC_IGNORE:
3254 case RPC_FC_BIND_PRIMITIVE:
3255 /* no marshalling needed */
3256 return;
3258 default:
3259 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3260 var->name, get_basic_fc(ref));
3261 size = 0;
3264 if (phase == PHASE_MARSHAL && alignment > 1)
3265 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3266 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3267 alignment - 1, alignment - 1);
3269 if (phase == PHASE_MARSHAL)
3271 print_file(file, indent, "*(");
3272 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3273 if (is_ptr(type))
3274 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3275 else
3276 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3277 fprintf(file, "%s%s", local_var_prefix, varname);
3278 fprintf(file, ";\n");
3280 else if (phase == PHASE_UNMARSHAL)
3282 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3283 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3284 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3285 print_file(file, indent, "{\n");
3286 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3287 print_file(file, indent, "}\n");
3288 print_file(file, indent, "%s%s%s",
3289 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3290 local_var_prefix, varname);
3291 if (pass == PASS_IN && is_ptr(type))
3292 fprintf(file, " = (");
3293 else
3294 fprintf(file, " = *(");
3295 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3296 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3299 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3300 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3301 fprintf(file, ");\n");
3305 /* returns whether the MaxCount, Offset or ActualCount members need to be
3306 * filled in for the specified phase */
3307 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3309 return (phase != PHASE_UNMARSHAL);
3312 expr_t *get_size_is_expr(const type_t *t, const char *name)
3314 expr_t *x = NULL;
3316 for ( ; is_array(t); t = type_array_get_element(t))
3317 if (type_array_has_conformance(t) &&
3318 type_array_get_conformance(t)->type != EXPR_VOID)
3320 if (!x)
3321 x = type_array_get_conformance(t);
3322 else
3323 error("%s: multidimensional conformant"
3324 " arrays not supported at the top level\n",
3325 name);
3328 return x;
3331 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3332 enum remoting_phase phase, const var_t *var)
3334 const type_t *type = var->type;
3335 /* get fundamental type for the argument */
3336 for (;;)
3338 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3340 case TGT_ARRAY:
3341 if (is_conformance_needed_for_phase(phase))
3343 if (type_array_has_conformance(type) &&
3344 type_array_get_conformance(type)->type != EXPR_VOID)
3346 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3347 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3348 fprintf(file, ";\n\n");
3350 if (type_array_has_variance(type))
3352 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3353 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3354 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3355 fprintf(file, ";\n\n");
3358 break;
3359 case TGT_UNION:
3360 if (type_get_type(type) == TYPE_UNION &&
3361 is_conformance_needed_for_phase(phase))
3363 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3364 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3365 fprintf(file, ";\n\n");
3367 break;
3368 case TGT_IFACE_POINTER:
3370 expr_t *iid;
3372 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3374 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3375 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3376 fprintf( file, ";\n\n" );
3378 break;
3380 case TGT_POINTER:
3381 type = type_pointer_get_ref(type);
3382 continue;
3383 case TGT_INVALID:
3384 case TGT_USER_TYPE:
3385 case TGT_CTXT_HANDLE:
3386 case TGT_CTXT_HANDLE_POINTER:
3387 case TGT_STRING:
3388 case TGT_BASIC:
3389 case TGT_ENUM:
3390 case TGT_STRUCT:
3391 case TGT_RANGE:
3392 break;
3394 break;
3398 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3399 enum pass pass, enum remoting_phase phase, const var_t *var)
3401 int in_attr, out_attr, pointer_type;
3402 const type_t *type = var->type;
3403 unsigned int start_offset = type->typestring_offset;
3405 if (is_ptr(type) || is_array(type))
3406 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3407 else
3408 pointer_type = 0;
3410 in_attr = is_attr(var->attrs, ATTR_IN);
3411 out_attr = is_attr(var->attrs, ATTR_OUT);
3412 if (!in_attr && !out_attr)
3413 in_attr = 1;
3415 if (phase != PHASE_FREE)
3416 switch (pass)
3418 case PASS_IN:
3419 if (!in_attr) return;
3420 break;
3421 case PASS_OUT:
3422 if (!out_attr) return;
3423 break;
3424 case PASS_RETURN:
3425 break;
3428 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3430 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3432 case TGT_CTXT_HANDLE:
3433 case TGT_CTXT_HANDLE_POINTER:
3434 if (phase == PHASE_MARSHAL)
3436 if (pass == PASS_IN)
3438 /* if the context_handle attribute appears in the chain of types
3439 * without pointers being followed, then the context handle must
3440 * be direct, otherwise it is a pointer */
3441 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3442 print_file(file, indent, "NdrClientContextMarshall(\n");
3443 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3444 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3445 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3447 else
3449 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3450 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3451 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3452 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3453 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3456 else if (phase == PHASE_UNMARSHAL)
3458 if (pass == PASS_OUT)
3460 if (!in_attr)
3461 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3462 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3463 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3464 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3465 print_file(file, indent + 1, "__frame->_Handle);\n");
3467 else
3469 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3470 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3471 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3474 break;
3475 case TGT_USER_TYPE:
3476 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3477 break;
3478 case TGT_STRING:
3479 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3480 pointer_type != RPC_FC_RP)
3482 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3483 !in_attr && is_conformant_array(type))
3485 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3486 indent++;
3487 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3489 /* strings returned are assumed to be global and hence don't
3490 * need freeing */
3491 else if (is_declptr(type) &&
3492 !(phase == PHASE_FREE && pass == PASS_RETURN))
3493 print_phase_function(file, indent, "Pointer", local_var_prefix,
3494 phase, var, start_offset);
3496 else
3498 unsigned int real_start_offset = start_offset;
3499 /* skip over pointer description straight to string description */
3500 if (is_declptr(type))
3502 if (is_conformant_array(type))
3503 real_start_offset += 4;
3504 else
3505 real_start_offset += 2;
3507 if (is_array(type) && !is_conformant_array(type))
3508 print_phase_function(file, indent, "NonConformantString",
3509 local_var_prefix, phase, var,
3510 real_start_offset);
3511 else
3512 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3513 phase, var, real_start_offset);
3515 break;
3516 case TGT_ARRAY:
3518 unsigned char tc = get_array_fc(type);
3519 const char *array_type = NULL;
3521 /* We already have the size_is expression since it's at the
3522 top level, but do checks for multidimensional conformant
3523 arrays. When we handle them, we'll need to extend this
3524 function to return a list, and then we'll actually use
3525 the return value. */
3526 get_size_is_expr(type, var->name);
3528 switch (tc)
3530 case RPC_FC_SMFARRAY:
3531 case RPC_FC_LGFARRAY:
3532 array_type = "FixedArray";
3533 break;
3534 case RPC_FC_SMVARRAY:
3535 case RPC_FC_LGVARRAY:
3536 array_type = "VaryingArray";
3537 break;
3538 case RPC_FC_CARRAY:
3539 array_type = "ConformantArray";
3540 break;
3541 case RPC_FC_CVARRAY:
3542 array_type = "ConformantVaryingArray";
3543 break;
3544 case RPC_FC_BOGUS_ARRAY:
3545 array_type = "ComplexArray";
3546 break;
3549 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3550 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3551 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3553 /* these are all unmarshalled by allocating memory */
3554 if (tc == RPC_FC_BOGUS_ARRAY ||
3555 tc == RPC_FC_CVARRAY ||
3556 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3557 (tc == RPC_FC_CARRAY && !in_attr))
3559 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3560 indent++;
3561 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3564 break;
3566 case TGT_BASIC:
3567 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3568 break;
3569 case TGT_ENUM:
3570 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3571 break;
3572 case TGT_RANGE:
3573 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3574 /* Note: this goes beyond what MIDL does - it only supports arguments
3575 * with the [range] attribute in Oicf mode */
3576 if (phase == PHASE_UNMARSHAL)
3578 const expr_t *range_min;
3579 const expr_t *range_max;
3580 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
3581 if (!range_list)
3582 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3583 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3584 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3586 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
3587 write_type_decl(file, var->type, NULL);
3588 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
3589 write_type_decl(file, var->type, NULL);
3590 fprintf(file, ")0x%x))\n", range_max->cval);
3591 print_file(file, indent, "{\n");
3592 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
3593 print_file(file, indent, "}\n");
3595 break;
3596 case TGT_STRUCT:
3597 switch (get_struct_fc(type))
3599 case RPC_FC_STRUCT:
3600 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3601 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3602 break;
3603 case RPC_FC_PSTRUCT:
3604 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3605 break;
3606 case RPC_FC_CSTRUCT:
3607 case RPC_FC_CPSTRUCT:
3608 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3609 break;
3610 case RPC_FC_CVSTRUCT:
3611 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3612 break;
3613 case RPC_FC_BOGUS_STRUCT:
3614 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3615 break;
3616 default:
3617 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3619 break;
3620 case TGT_UNION:
3622 const char *union_type = NULL;
3624 if (type_get_type(type) == TYPE_UNION)
3625 union_type = "NonEncapsulatedUnion";
3626 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3627 union_type = "EncapsulatedUnion";
3629 print_phase_function(file, indent, union_type, local_var_prefix,
3630 phase, var, start_offset);
3631 break;
3633 case TGT_POINTER:
3635 const type_t *ref = type_pointer_get_ref(type);
3636 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3638 case TGT_BASIC:
3639 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3640 break;
3641 case TGT_ENUM:
3642 /* base types have known sizes, so don't need a sizing pass
3643 * and don't have any memory to free and so don't need a
3644 * freeing pass */
3645 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3646 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3647 break;
3648 case TGT_STRUCT:
3650 const char *struct_type = NULL;
3651 switch (get_struct_fc(ref))
3653 case RPC_FC_STRUCT:
3654 /* simple structs have known sizes, so don't need a sizing
3655 * pass and don't have any memory to free and so don't
3656 * need a freeing pass */
3657 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3658 struct_type = "SimpleStruct";
3659 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3661 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3662 indent++;
3663 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3664 indent--;
3666 break;
3667 case RPC_FC_PSTRUCT:
3668 struct_type = "SimpleStruct";
3669 break;
3670 case RPC_FC_CSTRUCT:
3671 case RPC_FC_CPSTRUCT:
3672 struct_type = "ConformantStruct";
3673 break;
3674 case RPC_FC_CVSTRUCT:
3675 struct_type = "ConformantVaryingStruct";
3676 break;
3677 case RPC_FC_BOGUS_STRUCT:
3678 struct_type = "ComplexStruct";
3679 break;
3680 default:
3681 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3684 if (struct_type)
3686 if (phase == PHASE_FREE)
3687 struct_type = "Pointer";
3688 else
3689 start_offset = ref->typestring_offset;
3690 print_phase_function(file, indent, struct_type, local_var_prefix, phase, var, start_offset);
3692 break;
3694 case TGT_UNION:
3696 const char *union_type = NULL;
3697 if (phase == PHASE_FREE)
3698 union_type = "Pointer";
3699 else
3701 if (type_get_type(ref) == TYPE_UNION)
3702 union_type = "NonEncapsulatedUnion";
3703 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3704 union_type = "EncapsulatedUnion";
3706 start_offset = ref->typestring_offset;
3709 print_phase_function(file, indent, union_type, local_var_prefix,
3710 phase, var, start_offset);
3711 break;
3713 case TGT_STRING:
3714 case TGT_POINTER:
3715 case TGT_ARRAY:
3716 case TGT_RANGE:
3717 case TGT_IFACE_POINTER:
3718 case TGT_USER_TYPE:
3719 case TGT_CTXT_HANDLE:
3720 case TGT_CTXT_HANDLE_POINTER:
3721 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3722 break;
3723 case TGT_INVALID:
3724 assert(0);
3725 break;
3727 else
3728 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3729 break;
3731 case TGT_IFACE_POINTER:
3732 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3733 break;
3734 case TGT_INVALID:
3735 assert(0);
3736 break;
3738 fprintf(file, "\n");
3741 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3742 enum pass pass, enum remoting_phase phase)
3744 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3746 unsigned int size = get_function_buffer_size( func, pass );
3747 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3750 if (pass == PASS_RETURN)
3752 var_t var;
3753 var = *func;
3754 var.type = type_function_get_rettype(func->type);
3755 var.name = xstrdup( "_RetVal" );
3756 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3757 free( var.name );
3759 else
3761 const var_t *var;
3762 if (!type_get_function_args(func->type))
3763 return;
3764 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3765 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3770 unsigned int get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3772 return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3776 unsigned int get_size_procformatstring_func(const var_t *func)
3778 const var_t *var;
3779 unsigned int size = 0;
3781 /* argument list size */
3782 if (type_get_function_args(func->type))
3783 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3784 size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3786 /* return value size */
3787 if (is_void(type_function_get_rettype(func->type)))
3788 size += 2; /* FC_END and FC_PAD */
3789 else
3790 size += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
3792 return size;
3795 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3797 const statement_t *stmt;
3798 unsigned int size = 1;
3800 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3802 const type_t *iface;
3803 const statement_t *stmt_func;
3805 if (stmt->type == STMT_LIBRARY)
3807 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
3808 continue;
3810 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3811 continue;
3813 iface = stmt->u.type;
3814 if (!pred(iface))
3815 continue;
3817 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3819 const var_t *func = stmt_func->u.var;
3820 if (!is_local(func->attrs))
3821 size += get_size_procformatstring_func( func );
3824 return size;
3827 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3829 set_all_tfswrite(FALSE);
3830 return process_tfs(NULL, stmts, pred);
3833 void declare_stub_args( FILE *file, int indent, const var_t *func )
3835 int in_attr, out_attr;
3836 int i = 0;
3837 const var_t *var;
3839 /* declare return value '_RetVal' */
3840 if (!is_void(type_function_get_rettype(func->type)))
3842 print_file(file, indent, "%s", "");
3843 write_type_decl_left(file, type_function_get_rettype(func->type));
3844 fprintf(file, " _RetVal;\n");
3847 if (!type_get_function_args(func->type))
3848 return;
3850 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3852 in_attr = is_attr(var->attrs, ATTR_IN);
3853 out_attr = is_attr(var->attrs, ATTR_OUT);
3854 if (!out_attr && !in_attr)
3855 in_attr = 1;
3857 if (is_context_handle(var->type))
3858 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
3859 else
3861 if (!in_attr && !is_conformant_array(var->type))
3863 type_t *type_to_print;
3864 char name[16];
3865 print_file(file, indent, "%s", "");
3866 if (type_get_type(var->type) == TYPE_ARRAY &&
3867 !type_array_is_decl_as_ptr(var->type))
3868 type_to_print = var->type;
3869 else
3870 type_to_print = type_pointer_get_ref(var->type);
3871 sprintf(name, "_W%u", i++);
3872 write_type_decl(file, type_to_print, name);
3873 fprintf(file, ";\n");
3876 print_file(file, indent, "%s", "");
3877 write_type_decl_left(file, var->type);
3878 fprintf(file, " ");
3879 if (type_get_type(var->type) == TYPE_ARRAY &&
3880 !type_array_is_decl_as_ptr(var->type)) {
3881 fprintf(file, "(*%s)", var->name);
3882 } else
3883 fprintf(file, "%s", var->name);
3884 write_type_right(file, var->type, FALSE);
3885 fprintf(file, ";\n");
3887 if (decl_indirect(var->type))
3888 print_file(file, indent, "void *_p_%s;\n", var->name);
3894 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
3896 int in_attr, out_attr;
3897 int i = 0, sep = 0;
3898 const var_t *var;
3900 if (!type_get_function_args(func->type))
3901 return;
3903 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3905 in_attr = is_attr(var->attrs, ATTR_IN);
3906 out_attr = is_attr(var->attrs, ATTR_OUT);
3907 if (!out_attr && !in_attr)
3908 in_attr = 1;
3910 if (!in_attr)
3912 print_file(file, indent, "%s%s", local_var_prefix, var->name);
3914 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
3916 case TGT_CTXT_HANDLE_POINTER:
3917 fprintf(file, " = NdrContextHandleInitialize(\n");
3918 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3919 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3920 var->type->typestring_offset);
3921 break;
3922 case TGT_ARRAY:
3923 if (type_array_has_conformance(var->type))
3925 unsigned int size;
3926 type_t *type = var->type;
3928 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3929 for ( ;
3930 is_array(type) && type_array_has_conformance(type);
3931 type = type_array_get_element(type))
3933 write_expr(file, type_array_get_conformance(type), TRUE,
3934 TRUE, NULL, NULL, local_var_prefix);
3935 fprintf(file, " * ");
3937 size = type_memsize(type);
3938 fprintf(file, "%u);\n", size);
3940 else
3941 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
3942 break;
3943 case TGT_POINTER:
3944 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3945 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
3947 case TGT_BASIC:
3948 case TGT_ENUM:
3949 case TGT_POINTER:
3950 case TGT_RANGE:
3951 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3952 break;
3953 case TGT_STRUCT:
3954 case TGT_UNION:
3955 case TGT_USER_TYPE:
3956 case TGT_IFACE_POINTER:
3957 case TGT_ARRAY:
3958 case TGT_CTXT_HANDLE:
3959 case TGT_CTXT_HANDLE_POINTER:
3960 case TGT_INVALID:
3961 case TGT_STRING:
3962 /* not initialised */
3963 break;
3965 i++;
3966 break;
3967 default:
3968 break;
3971 sep = 1;
3974 if (sep)
3975 fprintf(file, "\n");
3979 int write_expr_eval_routines(FILE *file, const char *iface)
3981 static const char *var_name = "pS";
3982 static const char *var_name_expr = "pS->";
3983 int result = 0;
3984 struct expr_eval_routine *eval;
3985 unsigned short callback_offset = 0;
3987 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
3989 const char *name = eval->structure->name;
3990 result = 1;
3992 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3993 iface, name, callback_offset);
3994 print_file(file, 0, "{\n");
3995 print_file(file, 1, "%s", "");
3996 write_type_left(file, (type_t *)eval->structure, TRUE);
3997 fprintf(file, " *%s = (", var_name);
3998 write_type_left(file, (type_t *)eval->structure, TRUE);
3999 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4000 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4001 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4002 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
4003 fprintf(file, ";\n");
4004 print_file(file, 0, "}\n\n");
4005 callback_offset++;
4007 return result;
4010 void write_expr_eval_routine_list(FILE *file, const char *iface)
4012 struct expr_eval_routine *eval;
4013 struct expr_eval_routine *cursor;
4014 unsigned short callback_offset = 0;
4016 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4017 fprintf(file, "{\n");
4019 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4021 const char *name = eval->structure->name;
4022 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
4023 callback_offset++;
4024 list_remove(&eval->entry);
4025 free(eval);
4028 fprintf(file, "};\n\n");
4031 void write_user_quad_list(FILE *file)
4033 user_type_t *ut;
4035 if (list_empty(&user_type_list))
4036 return;
4038 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4039 fprintf(file, "{\n");
4040 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4042 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4043 print_file(file, 1, "{\n");
4044 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4045 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4046 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4047 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4048 print_file(file, 1, "}%s\n", sep);
4050 fprintf(file, "};\n\n");
4053 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4055 const struct str_list_entry_t *endpoint;
4056 const char *p;
4058 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4059 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4060 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4062 print_file( f, 1, "{ (const unsigned char *)\"" );
4063 for (p = endpoint->str; *p && *p != ':'; p++)
4065 if (*p == '"' || *p == '\\') fputc( '\\', f );
4066 fputc( *p, f );
4068 if (!*p) goto error;
4069 if (p[1] != '[') goto error;
4071 fprintf( f, "\", (const unsigned char *)\"" );
4072 for (p += 2; *p && *p != ']'; p++)
4074 if (*p == '"' || *p == '\\') fputc( '\\', f );
4075 fputc( *p, f );
4077 if (*p != ']') goto error;
4078 fprintf( f, "\" },\n" );
4080 print_file( f, 0, "};\n\n" );
4081 return;
4083 error:
4084 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4087 void write_exceptions( FILE *file )
4089 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4090 fprintf( file, "\n");
4091 fprintf( file, "#include \"wine/exception.h\"\n");
4092 fprintf( file, "#undef RpcTryExcept\n");
4093 fprintf( file, "#undef RpcExcept\n");
4094 fprintf( file, "#undef RpcEndExcept\n");
4095 fprintf( file, "#undef RpcTryFinally\n");
4096 fprintf( file, "#undef RpcFinally\n");
4097 fprintf( file, "#undef RpcEndFinally\n");
4098 fprintf( file, "#undef RpcExceptionCode\n");
4099 fprintf( file, "#undef RpcAbnormalTermination\n");
4100 fprintf( file, "\n");
4101 fprintf( file, "struct __exception_frame;\n");
4102 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4103 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4104 fprintf( file, "\n");
4105 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4106 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4107 fprintf( file, " __filter_func filter; \\\n");
4108 fprintf( file, " __finally_func finally; \\\n");
4109 fprintf( file, " sigjmp_buf jmp; \\\n");
4110 fprintf( file, " DWORD code; \\\n");
4111 fprintf( file, " unsigned char abnormal_termination; \\\n");
4112 fprintf( file, " unsigned char filter_level; \\\n");
4113 fprintf( file, " unsigned char finally_level;\n");
4114 fprintf( file, "\n");
4115 fprintf( file, "struct __exception_frame\n{\n");
4116 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4117 fprintf( file, "};\n");
4118 fprintf( file, "\n");
4119 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4120 fprintf( file, "{\n");
4121 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4122 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4123 fprintf( file, " {\n");
4124 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4125 fprintf( file, " exc_frame->finally( exc_frame );\n");
4126 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4127 fprintf( file, " }\n");
4128 fprintf( file, " exc_frame->filter_level = 0;\n");
4129 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4130 fprintf( file, "}\n");
4131 fprintf( file, "\n");
4132 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4133 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4134 fprintf( file, " CONTEXT *context,\n");
4135 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4136 fprintf( file, "{\n");
4137 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4138 fprintf( file, "\n");
4139 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4140 fprintf( file, " {\n" );
4141 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4142 fprintf( file, " {\n" );
4143 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4144 fprintf( file, " exc_frame->finally( exc_frame );\n");
4145 fprintf( file, " }\n" );
4146 fprintf( file, " return ExceptionContinueSearch;\n");
4147 fprintf( file, " }\n" );
4148 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4149 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4150 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4151 fprintf( file, " return ExceptionContinueSearch;\n");
4152 fprintf( file, "}\n");
4153 fprintf( file, "\n");
4154 fprintf( file, "#define RpcTryExcept \\\n");
4155 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4156 fprintf( file, " { \\\n");
4157 fprintf( file, " if (!__frame->finally_level) \\\n" );
4158 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4159 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4160 fprintf( file, "\n");
4161 fprintf( file, "#define RpcExcept(expr) \\\n");
4162 fprintf( file, " if (!__frame->finally_level) \\\n" );
4163 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4164 fprintf( file, " __frame->filter_level = 0; \\\n" );
4165 fprintf( file, " } \\\n");
4166 fprintf( file, " else \\\n");
4167 fprintf( file, "\n");
4168 fprintf( file, "#define RpcEndExcept\n");
4169 fprintf( file, "\n");
4170 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4171 fprintf( file, "\n");
4172 fprintf( file, "#define RpcTryFinally \\\n");
4173 fprintf( file, " if (!__frame->filter_level) \\\n");
4174 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4175 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4176 fprintf( file, "\n");
4177 fprintf( file, "#define RpcFinally \\\n");
4178 fprintf( file, " if (!__frame->filter_level) \\\n");
4179 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4180 fprintf( file, " __frame->finally_level = 0;\n");
4181 fprintf( file, "\n");
4182 fprintf( file, "#define RpcEndFinally\n");
4183 fprintf( file, "\n");
4184 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4185 fprintf( file, "\n");
4186 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4187 fprintf( file, " do { \\\n");
4188 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4189 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4190 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4191 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4192 fprintf( file, " __frame->filter_level = 0; \\\n");
4193 fprintf( file, " __frame->finally_level = 0; \\\n");
4194 fprintf( file, " } while (0)\n");
4195 fprintf( file, "\n");
4196 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4197 fprintf( file, "\n");
4198 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4199 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4200 fprintf( file, "\n");
4201 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4202 fprintf( file, " DWORD code;\n");
4203 fprintf( file, "\n");
4204 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");