comdlg32: Mark one vtable as static.
[wine/multimedia.git] / tools / widl / typegen.c
blob8d93b331a083f0fc47e7965cba9ba9aa1923ae51
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 var_t *current_func;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *iface;
58 const type_t *cont_type;
59 char *name;
60 unsigned int baseoff;
61 const expr_t *expr;
64 enum type_context
66 TYPE_CONTEXT_TOPLEVELPARAM,
67 TYPE_CONTEXT_PARAM,
68 TYPE_CONTEXT_CONTAINER,
69 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
72 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
73 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
74 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align);
75 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
76 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
77 const char *name, int write_ptr, unsigned int *tfsoff);
78 static const var_t *find_array_or_string_in_struct(const type_t *type);
79 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
80 type_t *type, int toplevel_param,
81 const char *name, unsigned int *typestring_offset);
83 static const char *string_of_type(unsigned char type)
85 switch (type)
87 case RPC_FC_BYTE: return "FC_BYTE";
88 case RPC_FC_CHAR: return "FC_CHAR";
89 case RPC_FC_SMALL: return "FC_SMALL";
90 case RPC_FC_USMALL: return "FC_USMALL";
91 case RPC_FC_WCHAR: return "FC_WCHAR";
92 case RPC_FC_SHORT: return "FC_SHORT";
93 case RPC_FC_USHORT: return "FC_USHORT";
94 case RPC_FC_LONG: return "FC_LONG";
95 case RPC_FC_ULONG: return "FC_ULONG";
96 case RPC_FC_FLOAT: return "FC_FLOAT";
97 case RPC_FC_HYPER: return "FC_HYPER";
98 case RPC_FC_DOUBLE: return "FC_DOUBLE";
99 case RPC_FC_ENUM16: return "FC_ENUM16";
100 case RPC_FC_ENUM32: return "FC_ENUM32";
101 case RPC_FC_IGNORE: return "FC_IGNORE";
102 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
103 case RPC_FC_RP: return "FC_RP";
104 case RPC_FC_UP: return "FC_UP";
105 case RPC_FC_OP: return "FC_OP";
106 case RPC_FC_FP: return "FC_FP";
107 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
108 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
109 case RPC_FC_STRUCT: return "FC_STRUCT";
110 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
111 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
112 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
113 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
114 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
115 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
116 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
117 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
118 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
119 case RPC_FC_CARRAY: return "FC_CARRAY";
120 case RPC_FC_CVARRAY: return "FC_CVARRAY";
121 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
122 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
123 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
124 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
125 case RPC_FC_POINTER: return "FC_POINTER";
126 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
127 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
128 case RPC_FC_CSTRING: return "FC_CSTRING";
129 case RPC_FC_WSTRING: return "FC_WSTRING";
130 case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
131 case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
132 case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
133 case RPC_FC_IP: return "FC_IP";
134 case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
135 case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
136 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
137 case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
138 case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
139 case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
140 case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
141 case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
142 case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
143 case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
144 case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
145 case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
146 case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
147 case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
148 case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
149 case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
150 case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
151 case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
152 case RPC_FC_PP: return "FC_PP";
153 case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
154 case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
155 case RPC_FC_DIV_2: return "FC_DIV_2";
156 case RPC_FC_MULT_2: return "FC_MULT_2";
157 case RPC_FC_ADD_1: return "FC_ADD_1";
158 case RPC_FC_SUB_1: return "FC_SUB_1";
159 case RPC_FC_CALLBACK: return "FC_CALLBACK";
160 case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
161 case RPC_FC_END: return "FC_END";
162 case RPC_FC_PAD: return "FC_PAD";
163 case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
164 case RPC_FC_RANGE: return "FC_RANGE";
165 case RPC_FC_INT3264: return "FC_INT3264";
166 case RPC_FC_UINT3264: return "FC_UINT3264";
167 default:
168 error("string_of_type: unknown type 0x%02x\n", type);
169 return NULL;
173 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
175 const type_t *t = type;
176 for (;;)
178 if (is_attr(t->attrs, attr))
179 return get_attrp(t->attrs, attr);
180 else if (type_is_alias(t))
181 t = type_alias_get_aliasee(t);
182 else return NULL;
186 unsigned char get_basic_fc(const type_t *type)
188 int sign = type_basic_get_sign(type);
189 switch (type_basic_get_type(type))
191 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
192 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
193 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
194 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
195 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
196 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
197 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
198 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
199 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
200 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
201 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
202 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
203 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
204 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
206 return 0;
209 static inline unsigned int clamp_align(unsigned int align)
211 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
212 if(align > packing) align = packing;
213 return align;
216 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
218 const type_t *t;
219 int pointer_type;
221 assert(is_ptr(type) || is_array(type));
223 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
224 if (pointer_type)
225 return pointer_type;
227 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
229 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
230 if (pointer_type)
231 return pointer_type;
234 if (toplevel_param)
235 return RPC_FC_RP;
236 else if (is_ptr(type))
237 return type_pointer_get_default_fc(type);
238 else
239 return type_array_get_ptr_default_fc(type);
242 static unsigned char get_enum_fc(const type_t *type)
244 assert(type_get_type(type) == TYPE_ENUM);
245 if (is_aliaschain_attr(type, ATTR_V1ENUM))
246 return RPC_FC_ENUM32;
247 else
248 return RPC_FC_ENUM16;
251 static type_t *get_user_type(const type_t *t, const char **pname)
253 for (;;)
255 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
256 if (ut)
258 if (pname)
259 *pname = t->name;
260 return ut;
263 if (type_is_alias(t))
264 t = type_alias_get_aliasee(t);
265 else
266 return NULL;
270 static int is_user_type(const type_t *t)
272 return get_user_type(t, NULL) != NULL;
275 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
277 if (is_user_type(type))
278 return TGT_USER_TYPE;
280 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
281 return TGT_CTXT_HANDLE;
283 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
284 return TGT_STRING;
286 switch (type_get_type(type))
288 case TYPE_BASIC:
289 if (!(flags & TDT_IGNORE_RANGES) &&
290 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
291 return TGT_RANGE;
292 return TGT_BASIC;
293 case TYPE_ENUM:
294 if (!(flags & TDT_IGNORE_RANGES) &&
295 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
296 return TGT_RANGE;
297 return TGT_ENUM;
298 case TYPE_POINTER:
299 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
300 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
301 return TGT_IFACE_POINTER;
302 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
303 return TGT_CTXT_HANDLE_POINTER;
304 else
305 return TGT_POINTER;
306 case TYPE_STRUCT:
307 return TGT_STRUCT;
308 case TYPE_ENCAPSULATED_UNION:
309 case TYPE_UNION:
310 return TGT_UNION;
311 case TYPE_ARRAY:
312 return TGT_ARRAY;
313 case TYPE_FUNCTION:
314 case TYPE_COCLASS:
315 case TYPE_INTERFACE:
316 case TYPE_MODULE:
317 case TYPE_VOID:
318 case TYPE_ALIAS:
319 case TYPE_BITFIELD:
320 break;
322 return TGT_INVALID;
325 static int get_padding(const var_list_t *fields)
327 unsigned short offset = 0;
328 unsigned int salign = 1;
329 const var_t *f;
331 if (!fields)
332 return 0;
334 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
336 type_t *ft = f->type;
337 unsigned int align = 0;
338 unsigned int size = type_memsize_and_alignment(ft, &align);
339 align = clamp_align(align);
340 if (align > salign) salign = align;
341 offset = ROUND_SIZE(offset, align);
342 offset += size;
345 return ROUNDING(offset, salign);
348 static unsigned int get_stack_size( const type_t *type, const attr_list_t *attrs, int *by_value )
350 unsigned int stack_size;
351 int by_val;
353 switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
355 case TGT_BASIC:
356 case TGT_ENUM:
357 case TGT_RANGE:
358 case TGT_STRUCT:
359 case TGT_UNION:
360 case TGT_USER_TYPE:
361 stack_size = type_memsize( type );
362 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
363 break;
364 default:
365 by_val = 0;
366 break;
368 if (!by_val) stack_size = pointer_size;
369 if (by_value) *by_value = by_val;
370 return ROUND_SIZE( stack_size, pointer_size );
373 unsigned char get_struct_fc(const type_t *type)
375 int has_pointer = 0;
376 int has_conformance = 0;
377 int has_variance = 0;
378 var_t *field;
379 var_list_t *fields;
381 fields = type_struct_get_fields(type);
383 if (get_padding(fields))
384 return RPC_FC_BOGUS_STRUCT;
386 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
388 type_t *t = field->type;
389 enum typegen_type typegen_type;
391 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
393 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
395 if (is_string_type(field->attrs, field->type))
397 if (is_conformant_array(t))
398 has_conformance = 1;
399 has_variance = 1;
400 continue;
403 if (is_array(type_array_get_element(field->type)))
404 return RPC_FC_BOGUS_STRUCT;
406 if (type_array_has_conformance(field->type))
408 has_conformance = 1;
409 if (list_next(fields, &field->entry))
410 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
411 field->name);
413 if (type_array_has_variance(t))
414 has_variance = 1;
416 t = type_array_get_element(t);
417 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
420 switch (typegen_type)
422 case TGT_USER_TYPE:
423 case TGT_IFACE_POINTER:
424 return RPC_FC_BOGUS_STRUCT;
425 case TGT_BASIC:
426 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
427 return RPC_FC_BOGUS_STRUCT;
428 break;
429 case TGT_ENUM:
430 if (get_enum_fc(t) == RPC_FC_ENUM16)
431 return RPC_FC_BOGUS_STRUCT;
432 break;
433 case TGT_POINTER:
434 case TGT_ARRAY:
435 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
436 return RPC_FC_BOGUS_STRUCT;
437 has_pointer = 1;
438 break;
439 case TGT_UNION:
440 return RPC_FC_BOGUS_STRUCT;
441 case TGT_STRUCT:
443 unsigned char fc = get_struct_fc(t);
444 switch (fc)
446 case RPC_FC_STRUCT:
447 break;
448 case RPC_FC_CVSTRUCT:
449 has_conformance = 1;
450 has_variance = 1;
451 has_pointer = 1;
452 break;
454 case RPC_FC_CPSTRUCT:
455 has_conformance = 1;
456 if (list_next( fields, &field->entry ))
457 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
458 field->name);
459 has_pointer = 1;
460 break;
462 case RPC_FC_CSTRUCT:
463 has_conformance = 1;
464 if (list_next( fields, &field->entry ))
465 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
466 field->name);
467 break;
469 case RPC_FC_PSTRUCT:
470 has_pointer = 1;
471 break;
473 default:
474 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
475 /* fallthru - treat it as complex */
477 /* as soon as we see one of these these members, it's bogus... */
478 case RPC_FC_BOGUS_STRUCT:
479 return RPC_FC_BOGUS_STRUCT;
481 break;
483 case TGT_RANGE:
484 return RPC_FC_BOGUS_STRUCT;
485 case TGT_STRING:
486 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
487 case TGT_INVALID:
488 case TGT_CTXT_HANDLE:
489 case TGT_CTXT_HANDLE_POINTER:
490 /* checking after parsing should mean that we don't get here. if we do,
491 * it's a checker bug */
492 assert(0);
496 if( has_variance )
498 if ( has_conformance )
499 return RPC_FC_CVSTRUCT;
500 else
501 return RPC_FC_BOGUS_STRUCT;
503 if( has_conformance && has_pointer )
504 return RPC_FC_CPSTRUCT;
505 if( has_conformance )
506 return RPC_FC_CSTRUCT;
507 if( has_pointer )
508 return RPC_FC_PSTRUCT;
509 return RPC_FC_STRUCT;
512 static unsigned char get_array_fc(const type_t *type)
514 unsigned char fc;
515 const expr_t *size_is;
516 const type_t *elem_type;
518 elem_type = type_array_get_element(type);
519 size_is = type_array_get_conformance(type);
521 if (!size_is)
523 unsigned int size = type_memsize(elem_type);
524 if (size * type_array_get_dim(type) > 0xffffuL)
525 fc = RPC_FC_LGFARRAY;
526 else
527 fc = RPC_FC_SMFARRAY;
529 else
530 fc = RPC_FC_CARRAY;
532 if (type_array_has_variance(type))
534 if (fc == RPC_FC_SMFARRAY)
535 fc = RPC_FC_SMVARRAY;
536 else if (fc == RPC_FC_LGFARRAY)
537 fc = RPC_FC_LGVARRAY;
538 else if (fc == RPC_FC_CARRAY)
539 fc = RPC_FC_CVARRAY;
542 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
544 case TGT_USER_TYPE:
545 fc = RPC_FC_BOGUS_ARRAY;
546 break;
547 case TGT_BASIC:
548 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
549 pointer_size != 4)
550 fc = RPC_FC_BOGUS_ARRAY;
551 break;
552 case TGT_STRUCT:
553 switch (get_struct_fc(elem_type))
555 case RPC_FC_BOGUS_STRUCT:
556 fc = RPC_FC_BOGUS_ARRAY;
557 break;
559 break;
560 case TGT_ENUM:
561 /* is 16-bit enum - if so, wire size differs from mem size and so
562 * the array cannot be block copied, which means the array is complex */
563 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
564 fc = RPC_FC_BOGUS_ARRAY;
565 break;
566 case TGT_UNION:
567 case TGT_IFACE_POINTER:
568 fc = RPC_FC_BOGUS_ARRAY;
569 break;
570 case TGT_POINTER:
571 /* ref pointers cannot just be block copied. unique pointers to
572 * interfaces need special treatment. either case means the array is
573 * complex */
574 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
575 fc = RPC_FC_BOGUS_ARRAY;
576 break;
577 case TGT_RANGE:
578 fc = RPC_FC_BOGUS_ARRAY;
579 break;
580 case TGT_CTXT_HANDLE:
581 case TGT_CTXT_HANDLE_POINTER:
582 case TGT_STRING:
583 case TGT_INVALID:
584 case TGT_ARRAY:
585 /* nothing to do for everything else */
586 break;
589 return fc;
592 static int is_non_complex_struct(const type_t *type)
594 return (type_get_type(type) == TYPE_STRUCT &&
595 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
598 static int type_has_pointers(const type_t *type)
600 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
602 case TGT_USER_TYPE:
603 return FALSE;
604 case TGT_POINTER:
605 return TRUE;
606 case TGT_ARRAY:
607 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
608 case TGT_STRUCT:
610 var_list_t *fields = type_struct_get_fields(type);
611 const var_t *field;
612 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
614 if (type_has_pointers(field->type))
615 return TRUE;
617 break;
619 case TGT_UNION:
621 var_list_t *fields;
622 const var_t *field;
623 fields = type_union_get_cases(type);
624 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
626 if (field->type && type_has_pointers(field->type))
627 return TRUE;
629 break;
631 case TGT_CTXT_HANDLE:
632 case TGT_CTXT_HANDLE_POINTER:
633 case TGT_STRING:
634 case TGT_IFACE_POINTER:
635 case TGT_BASIC:
636 case TGT_ENUM:
637 case TGT_RANGE:
638 case TGT_INVALID:
639 break;
642 return FALSE;
645 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
646 int toplevel_param)
648 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
650 case TGT_USER_TYPE:
651 return FALSE;
652 case TGT_POINTER:
653 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
654 return TRUE;
655 else
656 return FALSE;
657 case TGT_ARRAY:
658 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
659 return TRUE;
660 else
661 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
662 case TGT_STRUCT:
664 var_list_t *fields = type_struct_get_fields(type);
665 const var_t *field;
666 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
668 if (type_has_full_pointer(field->type, field->attrs, FALSE))
669 return TRUE;
671 break;
673 case TGT_UNION:
675 var_list_t *fields;
676 const var_t *field;
677 fields = type_union_get_cases(type);
678 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
680 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
681 return TRUE;
683 break;
685 case TGT_CTXT_HANDLE:
686 case TGT_CTXT_HANDLE_POINTER:
687 case TGT_STRING:
688 case TGT_IFACE_POINTER:
689 case TGT_BASIC:
690 case TGT_ENUM:
691 case TGT_RANGE:
692 case TGT_INVALID:
693 break;
696 return FALSE;
699 static unsigned short user_type_offset(const char *name)
701 user_type_t *ut;
702 unsigned short off = 0;
703 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
705 if (strcmp(name, ut->name) == 0)
706 return off;
707 ++off;
709 error("user_type_offset: couldn't find type (%s)\n", name);
710 return 0;
713 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
715 type->typestring_offset = offset;
716 if (file) type->tfswrite = FALSE;
719 static void guard_rec(type_t *type)
721 /* types that contain references to themselves (like a linked list),
722 need to be shielded from infinite recursion when writing embedded
723 types */
724 if (type->typestring_offset)
725 type->tfswrite = FALSE;
726 else
727 type->typestring_offset = 1;
730 static int is_embedded_complex(const type_t *type)
732 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
734 case TGT_USER_TYPE:
735 case TGT_STRUCT:
736 case TGT_UNION:
737 case TGT_ARRAY:
738 case TGT_IFACE_POINTER:
739 return TRUE;
740 default:
741 return FALSE;
745 static const char *get_context_handle_type_name(const type_t *type)
747 const type_t *t;
748 for (t = type;
749 is_ptr(t) || type_is_alias(t);
750 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
751 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
752 return t->name;
753 assert(0);
754 return NULL;
757 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
758 do { \
759 if (file) \
760 fprintf(file, "/* %2u */\n", typestring_offset); \
761 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
763 while (0)
765 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
766 static void print_file(FILE *file, int indent, const char *format, ...)
768 va_list va;
769 va_start(va, format);
770 print(file, indent, format, va);
771 va_end(va);
774 void print(FILE *file, int indent, const char *format, va_list va)
776 if (file)
778 if (format[0] != '\n')
779 while (0 < indent--)
780 fprintf(file, " ");
781 vfprintf(file, format, va);
786 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
788 if (decl_indirect(t))
790 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
791 local_var_prefix, n, local_var_prefix, n);
792 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
794 else if (is_ptr(t) || is_array(t))
795 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
798 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
800 const var_t *var;
802 if (!is_void(type_function_get_rettype(func->type)))
803 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
805 if (!type_get_function_args(func->type))
806 return;
808 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
809 write_var_init(file, indent, var->type, var->name, local_var_prefix);
811 fprintf(file, "\n");
814 static void write_formatdesc(FILE *f, int indent, const char *str)
816 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
817 print_file(f, indent, "{\n");
818 print_file(f, indent + 1, "short Pad;\n");
819 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
820 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
821 print_file(f, indent, "\n");
824 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
826 clear_all_offsets();
828 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
829 get_size_typeformatstring(stmts, pred));
831 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
832 get_size_procformatstring(stmts, pred));
834 fprintf(f, "\n");
835 write_formatdesc(f, indent, "TYPE");
836 write_formatdesc(f, indent, "PROC");
837 fprintf(f, "\n");
838 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
839 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
840 print_file(f, indent, "\n");
843 int decl_indirect(const type_t *t)
845 if (is_user_type(t))
846 return TRUE;
847 return (type_get_type(t) != TYPE_BASIC &&
848 type_get_type(t) != TYPE_ENUM &&
849 type_get_type(t) != TYPE_POINTER &&
850 type_get_type(t) != TYPE_ARRAY);
853 static unsigned int write_procformatstring_type(FILE *file, int indent,
854 const type_t *type,
855 const attr_list_t *attrs,
856 int is_return)
858 unsigned int size;
860 int is_in = is_attr(attrs, ATTR_IN);
861 int is_out = is_attr(attrs, ATTR_OUT);
863 if (!is_in && !is_out) is_in = TRUE;
865 if (type_get_type(type) == TYPE_BASIC ||
866 type_get_type(type) == TYPE_ENUM)
868 unsigned char fc;
870 if (is_return)
871 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
872 else
873 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
875 if (type_get_type(type) == TYPE_ENUM)
877 fc = get_enum_fc(type);
879 else
881 fc = get_basic_fc(type);
883 if (fc == RPC_FC_BIND_PRIMITIVE)
884 fc = RPC_FC_IGNORE;
887 print_file(file, indent, "0x%02x, /* %s */\n",
888 fc, string_of_type(fc));
889 size = 2; /* includes param type prefix */
891 else
893 if (is_return)
894 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
895 else if (is_in && is_out)
896 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
897 else if (is_out)
898 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
899 else
900 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
902 size = get_stack_size( type, attrs, NULL );
903 print_file(file, indent, "0x%02x,\n", size / pointer_size );
904 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
905 type->typestring_offset, type->typestring_offset);
906 size = 4; /* includes param type prefix */
908 return size;
911 int is_interpreted_func( const type_t *iface, const var_t *func )
913 const char *str;
914 const type_t *ret_type = type_function_get_rettype( func->type );
916 /* return value must fit in a long_ptr for interpreted functions */
917 if (type_get_type( ret_type ) == TYPE_BASIC && type_memsize( ret_type ) > pointer_size)
918 return 0;
919 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
920 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
921 return 0;
924 static void write_procformatstring_func( FILE *file, int indent,
925 const var_t *func, unsigned int *offset )
927 /* emit argument data */
928 if (type_get_function_args(func->type))
930 const var_t *var;
931 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
933 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
934 *offset += write_procformatstring_type(file, indent, var->type, var->attrs, FALSE);
938 /* emit return value data */
939 if (is_void(type_function_get_rettype(func->type)))
941 print_file(file, 0, "/* %u (void) */\n", *offset);
942 print_file(file, indent, "0x5b, /* FC_END */\n");
943 print_file(file, indent, "0x5c, /* FC_PAD */\n");
944 *offset += 2;
946 else
948 print_file( file, 0, "/* %u (return value) */\n", *offset );
949 *offset += write_procformatstring_type(file, indent, type_function_get_rettype(func->type), NULL, TRUE);
953 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
954 type_pred_t pred, unsigned int *offset)
956 const statement_t *stmt;
957 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
959 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
961 const statement_t *stmt_func;
962 if (!pred(stmt->u.type))
963 continue;
964 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type))
966 const var_t *func = stmt_func->u.var;
967 if (is_local(func->attrs)) continue;
968 write_procformatstring_func( file, indent, func, offset );
971 else if (stmt->type == STMT_LIBRARY)
972 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred, offset);
976 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
978 int indent = 0;
979 unsigned int offset = 0;
981 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
982 print_file(file, indent, "{\n");
983 indent++;
984 print_file(file, indent, "0,\n");
985 print_file(file, indent, "{\n");
986 indent++;
988 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
990 print_file(file, indent, "0x0\n");
991 indent--;
992 print_file(file, indent, "}\n");
993 indent--;
994 print_file(file, indent, "};\n");
995 print_file(file, indent, "\n");
998 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1000 const statement_t *stmt;
1001 int indent = 0;
1003 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1004 iface->name );
1005 print_file( file, indent, "{\n" );
1006 indent++;
1007 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1009 var_t *func = stmt->u.var;
1010 if (is_local( func->attrs )) continue;
1011 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1013 indent--;
1014 print_file( file, indent, "};\n\n" );
1017 static int write_base_type(FILE *file, const type_t *type, int convert_to_signed_type, unsigned int *typestring_offset)
1019 unsigned char fc;
1021 if (type_get_type(type) == TYPE_BASIC)
1022 fc = get_basic_fc(type);
1023 else if (type_get_type(type) == TYPE_ENUM)
1024 fc = get_enum_fc(type);
1025 else
1026 return 0;
1028 if (convert_to_signed_type)
1030 switch(fc)
1032 case RPC_FC_USMALL:
1033 fc = RPC_FC_SMALL;
1034 break;
1035 case RPC_FC_USHORT:
1036 fc = RPC_FC_SHORT;
1037 break;
1038 case RPC_FC_ULONG:
1039 fc = RPC_FC_LONG;
1040 break;
1044 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1045 *typestring_offset += 1;
1046 return 1;
1049 /* write conformance / variance descriptor */
1050 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1051 unsigned int baseoff, const type_t *type,
1052 const expr_t *expr)
1054 unsigned char operator_type = 0;
1055 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1056 const char *conftype_string = "field";
1057 const expr_t *subexpr;
1058 const type_t *iface = NULL;
1059 const char *name;
1061 if (!expr)
1063 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1064 return 4;
1067 if (expr->is_const)
1069 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1070 error("write_conf_or_var_desc: constant value %d is greater than "
1071 "the maximum constant size of %d\n", expr->cval,
1072 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1074 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1075 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1076 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1077 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1079 return 4;
1082 if (!cont_type) /* top-level conformance */
1084 conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1085 conftype_string = "parameter";
1086 cont_type = current_func->type;
1087 name = current_func->name;
1088 iface = current_iface;
1090 else
1092 name = cont_type->name;
1093 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1095 conftype = RPC_FC_POINTER_CONFORMANCE;
1096 conftype_string = "field pointer";
1100 subexpr = expr;
1101 switch (subexpr->type)
1103 case EXPR_PPTR:
1104 subexpr = subexpr->ref;
1105 operator_type = RPC_FC_DEREFERENCE;
1106 break;
1107 case EXPR_DIV:
1108 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1110 subexpr = subexpr->ref;
1111 operator_type = RPC_FC_DIV_2;
1113 break;
1114 case EXPR_MUL:
1115 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1117 subexpr = subexpr->ref;
1118 operator_type = RPC_FC_MULT_2;
1120 break;
1121 case EXPR_SUB:
1122 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1124 subexpr = subexpr->ref;
1125 operator_type = RPC_FC_SUB_1;
1127 break;
1128 case EXPR_ADD:
1129 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1131 subexpr = subexpr->ref;
1132 operator_type = RPC_FC_ADD_1;
1134 break;
1135 default:
1136 break;
1139 if (subexpr->type == EXPR_IDENTIFIER)
1141 const type_t *correlation_variable = NULL;
1142 unsigned char param_type = 0;
1143 unsigned int offset = 0;
1144 const var_t *var;
1145 struct expr_loc expr_loc;
1147 if (type_get_type(cont_type) == TYPE_FUNCTION)
1149 var_list_t *args = type_get_function_args( cont_type );
1151 if (is_object( iface )) offset += pointer_size;
1152 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1154 if (var->name && !strcmp(var->name, subexpr->u.sval))
1156 expr_loc.v = var;
1157 correlation_variable = var->type;
1158 break;
1160 offset += get_stack_size( var->type, var->attrs, NULL );
1163 else
1165 var_list_t *fields = type_struct_get_fields( cont_type );
1167 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1169 unsigned int size = field_memsize( var->type, &offset );
1170 if (var->name && !strcmp(var->name, subexpr->u.sval))
1172 expr_loc.v = var;
1173 correlation_variable = var->type;
1174 break;
1176 offset += size;
1180 if (!correlation_variable)
1181 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1182 expr_loc.attr = NULL;
1183 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1185 offset -= baseoff;
1187 if (type_get_type(correlation_variable) == TYPE_BASIC)
1189 switch (get_basic_fc(correlation_variable))
1191 case RPC_FC_CHAR:
1192 case RPC_FC_SMALL:
1193 param_type = RPC_FC_SMALL;
1194 break;
1195 case RPC_FC_BYTE:
1196 case RPC_FC_USMALL:
1197 param_type = RPC_FC_USMALL;
1198 break;
1199 case RPC_FC_WCHAR:
1200 case RPC_FC_SHORT:
1201 param_type = RPC_FC_SHORT;
1202 break;
1203 case RPC_FC_USHORT:
1204 param_type = RPC_FC_USHORT;
1205 break;
1206 case RPC_FC_LONG:
1207 param_type = RPC_FC_LONG;
1208 break;
1209 case RPC_FC_ULONG:
1210 param_type = RPC_FC_ULONG;
1211 break;
1212 default:
1213 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1214 get_basic_fc(correlation_variable));
1217 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1219 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1220 param_type = RPC_FC_LONG;
1221 else
1222 param_type = RPC_FC_SHORT;
1224 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1226 if (pointer_size == 8)
1227 param_type = RPC_FC_HYPER;
1228 else
1229 param_type = RPC_FC_LONG;
1231 else
1233 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1234 subexpr->u.sval);
1235 return 0;
1238 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1239 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1240 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1241 operator_type ? string_of_type(operator_type) : "no operators");
1242 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1243 (unsigned short)offset, offset);
1245 else if (!iface || is_interpreted_func( iface, current_func ))
1247 unsigned int callback_offset = 0;
1248 struct expr_eval_routine *eval;
1249 int found = 0;
1251 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1253 if (eval->cont_type == cont_type ||
1254 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1255 eval->iface == iface &&
1256 eval->name && name && !strcmp(eval->name, name) &&
1257 !compare_expr(eval->expr, expr)))
1259 found = 1;
1260 break;
1262 callback_offset++;
1265 if (!found)
1267 eval = xmalloc (sizeof(*eval));
1268 eval->iface = iface;
1269 eval->cont_type = cont_type;
1270 eval->name = xstrdup( name );
1271 eval->baseoff = baseoff;
1272 eval->expr = expr;
1273 list_add_tail (&expr_eval_routines, &eval->entry);
1276 if (callback_offset > USHRT_MAX)
1277 error("Maximum number of callback routines reached\n");
1279 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1280 print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1281 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1283 else /* output a dummy corr desc that isn't used */
1285 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1286 print_file(file, 2, "0x0,\n" );
1287 print_file(file, 2, "NdrFcShort(0x0),\n" );
1289 return 4;
1292 /* return size and start offset of a data field based on current offset */
1293 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1295 unsigned int align = 0;
1296 unsigned int size = type_memsize_and_alignment( type, &align );
1298 *offset = ROUND_SIZE( *offset, align );
1299 return size;
1302 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1304 unsigned int size = 0;
1305 unsigned int max_align;
1306 const var_t *v;
1308 if (!fields) return 0;
1309 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1311 unsigned int falign = 0;
1312 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1313 if (*align < falign) *align = falign;
1314 falign = clamp_align(falign);
1315 size = ROUND_SIZE(size, falign);
1316 size += fsize;
1319 max_align = clamp_align(*align);
1320 size = ROUND_SIZE(size, max_align);
1322 return size;
1325 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1327 unsigned int size, maxs = 0;
1328 unsigned int align = *pmaxa;
1329 const var_t *v;
1331 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1333 /* we could have an empty default field with NULL type */
1334 if (v->type)
1336 size = type_memsize_and_alignment(v->type, &align);
1337 if (maxs < size) maxs = size;
1338 if (*pmaxa < align) *pmaxa = align;
1342 return maxs;
1345 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1347 unsigned int size = 0;
1349 switch (type_get_type(t))
1351 case TYPE_BASIC:
1352 switch (get_basic_fc(t))
1354 case RPC_FC_BYTE:
1355 case RPC_FC_CHAR:
1356 case RPC_FC_USMALL:
1357 case RPC_FC_SMALL:
1358 size = 1;
1359 if (size > *align) *align = size;
1360 break;
1361 case RPC_FC_WCHAR:
1362 case RPC_FC_USHORT:
1363 case RPC_FC_SHORT:
1364 size = 2;
1365 if (size > *align) *align = size;
1366 break;
1367 case RPC_FC_ULONG:
1368 case RPC_FC_LONG:
1369 case RPC_FC_ERROR_STATUS_T:
1370 case RPC_FC_FLOAT:
1371 size = 4;
1372 if (size > *align) *align = size;
1373 break;
1374 case RPC_FC_HYPER:
1375 case RPC_FC_DOUBLE:
1376 size = 8;
1377 if (size > *align) *align = size;
1378 break;
1379 case RPC_FC_INT3264:
1380 case RPC_FC_UINT3264:
1381 case RPC_FC_BIND_PRIMITIVE:
1382 assert( pointer_size );
1383 size = pointer_size;
1384 if (size > *align) *align = size;
1385 break;
1386 default:
1387 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1388 size = 0;
1390 break;
1391 case TYPE_ENUM:
1392 switch (get_enum_fc(t))
1394 case RPC_FC_ENUM16:
1395 case RPC_FC_ENUM32:
1396 size = 4;
1397 if (size > *align) *align = size;
1398 break;
1399 default:
1400 error("type_memsize: Unknown enum type\n");
1401 size = 0;
1403 break;
1404 case TYPE_STRUCT:
1405 size = fields_memsize(type_struct_get_fields(t), align);
1406 break;
1407 case TYPE_ENCAPSULATED_UNION:
1408 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1409 break;
1410 case TYPE_UNION:
1411 size = union_memsize(type_union_get_cases(t), align);
1412 break;
1413 case TYPE_POINTER:
1414 assert( pointer_size );
1415 size = pointer_size;
1416 if (size > *align) *align = size;
1417 break;
1418 case TYPE_ARRAY:
1419 if (!type_array_is_decl_as_ptr(t))
1421 if (is_conformant_array(t))
1423 type_memsize_and_alignment(type_array_get_element(t), align);
1424 size = 0;
1426 else
1427 size = type_array_get_dim(t) *
1428 type_memsize_and_alignment(type_array_get_element(t), align);
1430 else /* declared as a pointer */
1432 assert( pointer_size );
1433 size = pointer_size;
1434 if (size > *align) *align = size;
1436 break;
1437 case TYPE_INTERFACE:
1438 case TYPE_ALIAS:
1439 case TYPE_VOID:
1440 case TYPE_COCLASS:
1441 case TYPE_MODULE:
1442 case TYPE_FUNCTION:
1443 case TYPE_BITFIELD:
1444 /* these types should not be encountered here due to language
1445 * restrictions (interface, void, coclass, module), logical
1446 * restrictions (alias - due to type_get_type call above) or
1447 * checking restrictions (function, bitfield). */
1448 assert(0);
1451 return size;
1454 unsigned int type_memsize(const type_t *t)
1456 unsigned int align = 0;
1457 return type_memsize_and_alignment( t, &align );
1460 static unsigned int type_buffer_alignment(const type_t *t)
1462 const var_list_t *fields;
1463 const var_t *var;
1464 unsigned int max = 0, align;
1466 switch (type_get_type(t))
1468 case TYPE_BASIC:
1469 switch (get_basic_fc(t))
1471 case RPC_FC_BYTE:
1472 case RPC_FC_CHAR:
1473 case RPC_FC_USMALL:
1474 case RPC_FC_SMALL:
1475 return 1;
1476 case RPC_FC_WCHAR:
1477 case RPC_FC_USHORT:
1478 case RPC_FC_SHORT:
1479 return 2;
1480 case RPC_FC_ULONG:
1481 case RPC_FC_LONG:
1482 case RPC_FC_ERROR_STATUS_T:
1483 case RPC_FC_FLOAT:
1484 case RPC_FC_INT3264:
1485 case RPC_FC_UINT3264:
1486 return 4;
1487 case RPC_FC_HYPER:
1488 case RPC_FC_DOUBLE:
1489 return 8;
1490 default:
1491 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1493 break;
1494 case TYPE_ENUM:
1495 switch (get_enum_fc(t))
1497 case RPC_FC_ENUM16:
1498 return 2;
1499 case RPC_FC_ENUM32:
1500 return 4;
1501 default:
1502 error("type_buffer_alignment: Unknown enum type\n");
1504 break;
1505 case TYPE_STRUCT:
1506 if (!(fields = type_struct_get_fields(t))) break;
1507 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1509 if (!var->type) continue;
1510 align = type_buffer_alignment( var->type );
1511 if (max < align) max = align;
1513 break;
1514 case TYPE_ENCAPSULATED_UNION:
1515 if (!(fields = type_encapsulated_union_get_fields(t))) break;
1516 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1518 if (!var->type) continue;
1519 align = type_buffer_alignment( var->type );
1520 if (max < align) max = align;
1522 break;
1523 case TYPE_UNION:
1524 if (!(fields = type_union_get_cases(t))) break;
1525 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1527 if (!var->type) continue;
1528 align = type_buffer_alignment( var->type );
1529 if (max < align) max = align;
1531 break;
1532 case TYPE_ARRAY:
1533 if (!type_array_is_decl_as_ptr(t))
1534 return type_buffer_alignment( type_array_get_element(t) );
1535 /* else fall through */
1536 case TYPE_POINTER:
1537 return 4;
1538 case TYPE_INTERFACE:
1539 case TYPE_ALIAS:
1540 case TYPE_VOID:
1541 case TYPE_COCLASS:
1542 case TYPE_MODULE:
1543 case TYPE_FUNCTION:
1544 case TYPE_BITFIELD:
1545 /* these types should not be encountered here due to language
1546 * restrictions (interface, void, coclass, module), logical
1547 * restrictions (alias - due to type_get_type call above) or
1548 * checking restrictions (function, bitfield). */
1549 assert(0);
1551 return max;
1554 int is_full_pointer_function(const var_t *func)
1556 const var_t *var;
1557 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
1558 return TRUE;
1559 if (!type_get_function_args(func->type))
1560 return FALSE;
1561 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1562 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
1563 return TRUE;
1564 return FALSE;
1567 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
1569 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1570 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
1571 fprintf(file, "\n");
1574 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
1576 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1577 fprintf(file, "\n");
1580 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
1581 const type_t *type,
1582 int toplevel_param,
1583 unsigned int offset,
1584 unsigned int *typeformat_offset)
1586 unsigned int start_offset = *typeformat_offset;
1587 short reloff = offset - (*typeformat_offset + 2);
1588 int in_attr, out_attr;
1589 int pointer_type;
1590 unsigned char flags = 0;
1592 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
1594 in_attr = is_attr(attrs, ATTR_IN);
1595 out_attr = is_attr(attrs, ATTR_OUT);
1596 if (!in_attr && !out_attr) in_attr = 1;
1598 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
1599 flags |= RPC_FC_P_ONSTACK;
1601 if (is_ptr(type))
1603 type_t *ref = type_pointer_get_ref(type);
1604 if(is_declptr(ref) && !is_user_type(ref))
1605 flags |= RPC_FC_P_DEREF;
1608 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1609 pointer_type,
1610 flags,
1611 string_of_type(pointer_type));
1612 if (file)
1614 if (flags & RPC_FC_P_ONSTACK)
1615 fprintf(file, " [allocated_on_stack]");
1616 if (flags & RPC_FC_P_DEREF)
1617 fprintf(file, " [pointer_deref]");
1618 fprintf(file, " */\n");
1621 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
1622 *typeformat_offset += 4;
1624 return start_offset;
1627 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
1629 unsigned char fc;
1630 unsigned char pointer_fc;
1631 const type_t *ref;
1632 int in_attr = is_attr(attrs, ATTR_IN);
1633 int out_attr = is_attr(attrs, ATTR_OUT);
1634 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
1636 /* for historical reasons, write_simple_pointer also handled string types,
1637 * but no longer does. catch bad uses of the function with this check */
1638 if (is_string_type(attrs, type))
1639 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
1641 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
1643 ref = type_pointer_get_ref(type);
1644 if (type_get_type(ref) == TYPE_ENUM)
1645 fc = get_enum_fc(ref);
1646 else
1647 fc = get_basic_fc(ref);
1649 if (out_attr && !in_attr)
1650 flags |= RPC_FC_P_ONSTACK;
1652 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
1653 pointer_fc, flags, string_of_type(pointer_fc),
1654 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
1655 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1656 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1657 return 4;
1660 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
1662 print_file(file, 0, "/* %u (", tfsoff);
1663 write_type_decl(file, t, NULL);
1664 print_file(file, 0, ") */\n");
1667 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
1668 type_t *type, unsigned int ref_offset,
1669 int toplevel_param,
1670 unsigned int *typestring_offset)
1672 unsigned int offset = *typestring_offset;
1673 type_t *ref = type_pointer_get_ref(type);
1675 print_start_tfs_comment(file, type, offset);
1676 update_tfsoff(type, offset, file);
1678 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
1680 case TGT_BASIC:
1681 case TGT_ENUM:
1682 *typestring_offset += write_simple_pointer(file, attrs, type,
1683 toplevel_param);
1684 break;
1685 default:
1686 if (ref_offset)
1687 write_nonsimple_pointer(file, attrs, type,
1688 toplevel_param,
1689 ref_offset,
1690 typestring_offset);
1691 break;
1694 return offset;
1697 static int processed(const type_t *type)
1699 return type->typestring_offset && !type->tfswrite;
1702 static int user_type_has_variable_size(const type_t *t)
1704 if (is_ptr(t))
1705 return TRUE;
1706 else if (type_get_type(t) == TYPE_STRUCT)
1708 switch (get_struct_fc(t))
1710 case RPC_FC_PSTRUCT:
1711 case RPC_FC_CSTRUCT:
1712 case RPC_FC_CPSTRUCT:
1713 case RPC_FC_CVSTRUCT:
1714 return TRUE;
1717 /* Note: Since this only applies to user types, we can't have a conformant
1718 array here, and strings should get filed under pointer in this case. */
1719 return FALSE;
1722 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1724 unsigned int start, absoff, flags;
1725 const char *name = NULL;
1726 type_t *utype = get_user_type(type, &name);
1727 unsigned int usize = type_memsize(utype);
1728 unsigned int ualign = type_buffer_alignment(utype);
1729 unsigned int size = type_memsize(type);
1730 unsigned short funoff = user_type_offset(name);
1731 short reloff;
1733 if (processed(type)) return type->typestring_offset;
1735 guard_rec(type);
1737 if(user_type_has_variable_size(utype)) usize = 0;
1739 if (type_get_type(utype) == TYPE_BASIC ||
1740 type_get_type(utype) == TYPE_ENUM)
1742 unsigned char fc;
1744 if (type_get_type(utype) == TYPE_ENUM)
1745 fc = get_enum_fc(utype);
1746 else
1747 fc = get_basic_fc(utype);
1749 absoff = *tfsoff;
1750 print_start_tfs_comment(file, utype, absoff);
1751 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1752 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
1753 *tfsoff += 2;
1755 else
1757 if (!processed(utype))
1758 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
1759 absoff = utype->typestring_offset;
1762 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
1763 flags = 0x40;
1764 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
1765 flags = 0x80;
1766 else
1767 flags = 0;
1769 start = *tfsoff;
1770 update_tfsoff(type, start, file);
1771 print_start_tfs_comment(file, type, start);
1772 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1773 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1774 flags | (ualign - 1), ualign - 1, flags);
1775 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
1776 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
1777 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
1778 *tfsoff += 8;
1779 reloff = absoff - *tfsoff;
1780 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
1781 *tfsoff += 2;
1782 return start;
1785 static void write_member_type(FILE *file, const type_t *cont,
1786 int cont_is_complex, const attr_list_t *attrs,
1787 const type_t *type, unsigned int *corroff,
1788 unsigned int *tfsoff)
1790 if (is_embedded_complex(type) && !is_conformant_array(type))
1792 unsigned int absoff;
1793 short reloff;
1795 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
1797 absoff = *corroff;
1798 *corroff += 8;
1800 else
1802 absoff = type->typestring_offset;
1804 reloff = absoff - (*tfsoff + 2);
1806 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1807 /* padding is represented using FC_STRUCTPAD* types, so presumably
1808 * this is left over in the format for historical purposes in MIDL
1809 * or rpcrt4. */
1810 print_file(file, 2, "0x0,\n");
1811 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1812 reloff, reloff, absoff);
1813 *tfsoff += 4;
1815 else if (is_ptr(type) || is_conformant_array(type))
1817 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
1818 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1819 *tfsoff += 1;
1821 else if (!write_base_type(file, type, TRUE, tfsoff))
1822 error("Unsupported member type %d\n", type_get_type(type));
1825 static void write_array_element_type(FILE *file, const type_t *type,
1826 int cont_is_complex, unsigned int *tfsoff)
1828 type_t *elem = type_array_get_element(type);
1830 if (!is_embedded_complex(elem) && is_ptr(elem))
1832 type_t *ref = type_pointer_get_ref(elem);
1834 if (processed(ref))
1836 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
1837 return;
1839 if (!is_string_type(NULL, elem) &&
1840 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
1842 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
1843 return;
1846 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
1849 static void write_end(FILE *file, unsigned int *tfsoff)
1851 if (*tfsoff % 2 == 0)
1853 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
1854 *tfsoff += 1;
1856 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
1857 *tfsoff += 1;
1860 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
1862 unsigned int offset = 0;
1863 var_list_t *fs = type_struct_get_fields(type);
1864 var_t *f;
1866 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
1868 type_t *ft = f->type;
1869 unsigned int size = field_memsize( ft, &offset );
1870 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
1872 short reloff;
1873 unsigned int absoff = ft->typestring_offset;
1874 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
1875 absoff += 8; /* we already have a corr descr, skip it */
1876 reloff = absoff - (*tfsoff + 6);
1877 print_file(file, 0, "/* %d */\n", *tfsoff);
1878 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
1879 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1880 write_conf_or_var_desc(file, current_structure, offset, ft,
1881 get_attrp(f->attrs, ATTR_SWITCHIS));
1882 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1883 reloff, reloff, absoff);
1884 *tfsoff += 8;
1886 offset += size;
1890 static int write_pointer_description_offsets(
1891 FILE *file, const attr_list_t *attrs, type_t *type,
1892 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1893 unsigned int *typestring_offset)
1895 int written = 0;
1897 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
1898 (is_array(type) && type_array_is_decl_as_ptr(type)))
1900 if (offset_in_memory && offset_in_buffer)
1902 unsigned int memsize;
1904 /* pointer instance
1906 * note that MSDN states that for pointer layouts in structures,
1907 * this is a negative offset from the end of the structure, but
1908 * this statement is incorrect. all offsets are positive */
1909 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
1910 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
1912 memsize = type_memsize(type);
1913 *offset_in_memory += memsize;
1914 /* increment these separately as in the case of conformant (varying)
1915 * structures these start at different values */
1916 *offset_in_buffer += memsize;
1918 *typestring_offset += 4;
1920 if (is_ptr(type))
1922 type_t *ref = type_pointer_get_ref(type);
1924 if (is_string_type(attrs, type))
1925 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
1926 else if (processed(ref))
1927 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
1928 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
1929 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
1930 else
1931 error("write_pointer_description_offsets: type format string unknown\n");
1933 else
1935 unsigned int offset = type->typestring_offset;
1936 /* skip over the pointer that is written for strings, since a
1937 * pointer has to be written in-place here */
1938 if (is_string_type(attrs, type))
1939 offset += 4;
1940 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
1943 return 1;
1946 if (is_array(type))
1948 return write_pointer_description_offsets(
1949 file, attrs, type_array_get_element(type), offset_in_memory,
1950 offset_in_buffer, typestring_offset);
1952 else if (is_non_complex_struct(type))
1954 /* otherwise search for interesting fields to parse */
1955 const var_t *v;
1956 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
1958 if (offset_in_memory && offset_in_buffer)
1960 unsigned int padding;
1961 unsigned int align = 0;
1962 type_memsize_and_alignment(v->type, &align);
1963 padding = ROUNDING(*offset_in_memory, align);
1964 *offset_in_memory += padding;
1965 *offset_in_buffer += padding;
1967 written += write_pointer_description_offsets(
1968 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
1969 typestring_offset);
1972 else
1974 if (offset_in_memory && offset_in_buffer)
1976 unsigned int memsize = type_memsize(type);
1977 *offset_in_memory += memsize;
1978 /* increment these separately as in the case of conformant (varying)
1979 * structures these start at different values */
1980 *offset_in_buffer += memsize;
1984 return written;
1987 static int write_no_repeat_pointer_descriptions(
1988 FILE *file, const attr_list_t *attrs, type_t *type,
1989 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
1990 unsigned int *typestring_offset)
1992 int written = 0;
1994 if (is_ptr(type) ||
1995 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
1997 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
1998 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
1999 *typestring_offset += 2;
2001 return write_pointer_description_offsets(file, attrs, type,
2002 offset_in_memory, offset_in_buffer, typestring_offset);
2005 if (is_non_complex_struct(type))
2007 const var_t *v;
2008 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2010 if (offset_in_memory && offset_in_buffer)
2012 unsigned int padding;
2013 unsigned int align = 0;
2014 type_memsize_and_alignment(v->type, &align);
2015 padding = ROUNDING(*offset_in_memory, align);
2016 *offset_in_memory += padding;
2017 *offset_in_buffer += padding;
2019 written += write_no_repeat_pointer_descriptions(
2020 file, v->attrs, v->type,
2021 offset_in_memory, offset_in_buffer, typestring_offset);
2024 else
2026 unsigned int memsize = type_memsize(type);
2027 *offset_in_memory += memsize;
2028 /* increment these separately as in the case of conformant (varying)
2029 * structures these start at different values */
2030 *offset_in_buffer += memsize;
2033 return written;
2036 /* Note: if file is NULL return value is number of pointers to write, else
2037 * it is the number of type format characters written */
2038 static int write_fixed_array_pointer_descriptions(
2039 FILE *file, const attr_list_t *attrs, type_t *type,
2040 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2041 unsigned int *typestring_offset)
2043 int pointer_count = 0;
2045 if (type_get_type(type) == TYPE_ARRAY &&
2046 !type_array_has_conformance(type) && !type_array_has_variance(type))
2048 unsigned int temp = 0;
2049 /* unfortunately, this needs to be done in two passes to avoid
2050 * writing out redundant FC_FIXED_REPEAT descriptions */
2051 pointer_count = write_pointer_description_offsets(
2052 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2053 if (pointer_count > 0)
2055 unsigned int increment_size;
2056 unsigned int offset_of_array_pointer_mem = 0;
2057 unsigned int offset_of_array_pointer_buf = 0;
2059 increment_size = type_memsize(type_array_get_element(type));
2061 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2062 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2063 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2064 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2065 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2066 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2067 *typestring_offset += 10;
2069 pointer_count = write_pointer_description_offsets(
2070 file, attrs, type, &offset_of_array_pointer_mem,
2071 &offset_of_array_pointer_buf, typestring_offset);
2074 else if (type_get_type(type) == TYPE_STRUCT)
2076 const var_t *v;
2077 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2079 if (offset_in_memory && offset_in_buffer)
2081 unsigned int padding;
2082 unsigned int align = 0;
2083 type_memsize_and_alignment(v->type, &align);
2084 padding = ROUNDING(*offset_in_memory, align);
2085 *offset_in_memory += padding;
2086 *offset_in_buffer += padding;
2088 pointer_count += write_fixed_array_pointer_descriptions(
2089 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2090 typestring_offset);
2093 else
2095 if (offset_in_memory && offset_in_buffer)
2097 unsigned int memsize;
2098 memsize = type_memsize(type);
2099 *offset_in_memory += memsize;
2100 /* increment these separately as in the case of conformant (varying)
2101 * structures these start at different values */
2102 *offset_in_buffer += memsize;
2106 return pointer_count;
2109 /* Note: if file is NULL return value is number of pointers to write, else
2110 * it is the number of type format characters written */
2111 static int write_conformant_array_pointer_descriptions(
2112 FILE *file, const attr_list_t *attrs, type_t *type,
2113 unsigned int offset_in_memory, unsigned int *typestring_offset)
2115 int pointer_count = 0;
2117 if (is_conformant_array(type) && !type_array_has_variance(type))
2119 unsigned int temp = 0;
2120 /* unfortunately, this needs to be done in two passes to avoid
2121 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2122 pointer_count = write_pointer_description_offsets(
2123 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2124 if (pointer_count > 0)
2126 unsigned int increment_size;
2127 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2128 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2130 increment_size = type_memsize(type_array_get_element(type));
2132 if (increment_size > USHRT_MAX)
2133 error("array size of %u bytes is too large\n", increment_size);
2135 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2136 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2137 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2138 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2139 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2140 *typestring_offset += 8;
2142 pointer_count = write_pointer_description_offsets(
2143 file, attrs, type_array_get_element(type),
2144 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2145 typestring_offset);
2149 return pointer_count;
2152 /* Note: if file is NULL return value is number of pointers to write, else
2153 * it is the number of type format characters written */
2154 static int write_varying_array_pointer_descriptions(
2155 FILE *file, const attr_list_t *attrs, type_t *type,
2156 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2157 unsigned int *typestring_offset)
2159 int pointer_count = 0;
2161 if (is_array(type) && type_array_has_variance(type))
2163 unsigned int temp = 0;
2164 /* unfortunately, this needs to be done in two passes to avoid
2165 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2166 pointer_count = write_pointer_description_offsets(
2167 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2168 if (pointer_count > 0)
2170 unsigned int increment_size;
2172 increment_size = type_memsize(type_array_get_element(type));
2174 if (increment_size > USHRT_MAX)
2175 error("array size of %u bytes is too large\n", increment_size);
2177 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2178 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2179 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2180 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2181 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2182 *typestring_offset += 8;
2184 pointer_count = write_pointer_description_offsets(
2185 file, attrs, type_array_get_element(type), offset_in_memory,
2186 offset_in_buffer, typestring_offset);
2189 else if (type_get_type(type) == TYPE_STRUCT)
2191 const var_t *v;
2192 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2194 if (offset_in_memory && offset_in_buffer)
2196 unsigned int align = 0, padding;
2198 if (is_array(v->type) && type_array_has_variance(v->type))
2200 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2201 /* skip over variance and offset in buffer */
2202 *offset_in_buffer += 8;
2205 type_memsize_and_alignment(v->type, &align);
2206 padding = ROUNDING(*offset_in_memory, align);
2207 *offset_in_memory += padding;
2208 *offset_in_buffer += padding;
2210 pointer_count += write_varying_array_pointer_descriptions(
2211 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2212 typestring_offset);
2215 else
2217 if (offset_in_memory && offset_in_buffer)
2219 unsigned int memsize = type_memsize(type);
2220 *offset_in_memory += memsize;
2221 /* increment these separately as in the case of conformant (varying)
2222 * structures these start at different values */
2223 *offset_in_buffer += memsize;
2227 return pointer_count;
2230 static void write_pointer_description(FILE *file, type_t *type,
2231 unsigned int *typestring_offset)
2233 unsigned int offset_in_buffer;
2234 unsigned int offset_in_memory;
2236 /* pass 1: search for single instance of a pointer (i.e. don't descend
2237 * into arrays) */
2238 if (!is_array(type))
2240 offset_in_memory = 0;
2241 offset_in_buffer = 0;
2242 write_no_repeat_pointer_descriptions(
2243 file, NULL, type,
2244 &offset_in_memory, &offset_in_buffer, typestring_offset);
2247 /* pass 2: search for pointers in fixed arrays */
2248 offset_in_memory = 0;
2249 offset_in_buffer = 0;
2250 write_fixed_array_pointer_descriptions(
2251 file, NULL, type,
2252 &offset_in_memory, &offset_in_buffer, typestring_offset);
2254 /* pass 3: search for pointers in conformant only arrays (but don't descend
2255 * into conformant varying or varying arrays) */
2256 if (is_conformant_array(type) &&
2257 (type_array_is_decl_as_ptr(type) || !current_structure))
2258 write_conformant_array_pointer_descriptions(
2259 file, NULL, type, 0, typestring_offset);
2260 else if (type_get_type(type) == TYPE_STRUCT &&
2261 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2263 type_t *carray = find_array_or_string_in_struct(type)->type;
2264 write_conformant_array_pointer_descriptions( file, NULL, carray,
2265 type_memsize(type), typestring_offset);
2268 /* pass 4: search for pointers in varying arrays */
2269 offset_in_memory = 0;
2270 offset_in_buffer = 0;
2271 write_varying_array_pointer_descriptions(
2272 file, NULL, type,
2273 &offset_in_memory, &offset_in_buffer, typestring_offset);
2276 int is_declptr(const type_t *t)
2278 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2281 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2282 type_t *type, int toplevel_param,
2283 const char *name, unsigned int *typestring_offset)
2285 unsigned int start_offset;
2286 unsigned char rtype;
2287 type_t *elem_type;
2288 int is_processed = processed(type);
2290 start_offset = *typestring_offset;
2292 if (is_declptr(type))
2294 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2295 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2296 if (!pointer_type)
2297 pointer_type = RPC_FC_RP;
2298 print_start_tfs_comment(file, type, *typestring_offset);
2299 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2300 pointer_type, flag, string_of_type(pointer_type),
2301 flag ? " [simple_pointer]" : "");
2302 *typestring_offset += 2;
2303 if (!flag)
2305 print_file(file, 2, "NdrFcShort(0x2),\n");
2306 *typestring_offset += 2;
2308 is_processed = FALSE;
2311 if (is_array(type))
2312 elem_type = type_array_get_element(type);
2313 else
2314 elem_type = type_pointer_get_ref(type);
2316 if (type_get_type(elem_type) != TYPE_BASIC)
2318 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2319 return start_offset;
2322 rtype = get_basic_fc(elem_type);
2323 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2325 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2326 return start_offset;
2329 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2331 unsigned int dim = type_array_get_dim(type);
2333 if (is_processed) return start_offset;
2335 /* FIXME: multi-dimensional array */
2336 if (0xffffu < dim)
2337 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2338 name, 0xffffu, dim - 0xffffu);
2340 if (rtype == RPC_FC_WCHAR)
2341 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2342 else
2343 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2344 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2345 *typestring_offset += 2;
2347 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2348 *typestring_offset += 2;
2350 update_tfsoff(type, start_offset, file);
2351 return start_offset;
2353 else if (is_conformant_array(type))
2355 if (rtype == RPC_FC_WCHAR)
2356 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2357 else
2358 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2359 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2360 *typestring_offset += 2;
2362 *typestring_offset += write_conf_or_var_desc(
2363 file, current_structure,
2364 (!type_array_is_decl_as_ptr(type) && current_structure
2365 ? type_memsize(current_structure)
2366 : 0),
2367 type, type_array_get_conformance(type));
2369 update_tfsoff(type, start_offset, file);
2370 return start_offset;
2372 else
2374 if (is_processed) return start_offset;
2376 if (rtype == RPC_FC_WCHAR)
2377 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2378 else
2379 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2380 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2381 *typestring_offset += 2;
2383 update_tfsoff(type, start_offset, file);
2384 return start_offset;
2388 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2389 const char *name, unsigned int *typestring_offset)
2391 const expr_t *length_is = type_array_get_variance(type);
2392 const expr_t *size_is = type_array_get_conformance(type);
2393 unsigned int align;
2394 unsigned int size;
2395 unsigned int start_offset;
2396 unsigned char fc;
2397 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2398 unsigned int baseoff
2399 = !type_array_is_decl_as_ptr(type) && current_structure
2400 ? type_memsize(current_structure)
2401 : 0;
2403 if (!pointer_type)
2404 pointer_type = RPC_FC_RP;
2406 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2408 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2409 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2410 fc = get_array_fc(type);
2412 start_offset = *typestring_offset;
2413 update_tfsoff(type, start_offset, file);
2414 print_start_tfs_comment(file, type, start_offset);
2415 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2416 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2417 *typestring_offset += 2;
2419 align = 0;
2420 if (fc != RPC_FC_BOGUS_ARRAY)
2422 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2424 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2425 *typestring_offset += 4;
2427 else
2429 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2430 *typestring_offset += 2;
2433 if (is_conformant_array(type))
2434 *typestring_offset
2435 += write_conf_or_var_desc(file, current_structure, baseoff,
2436 type, size_is);
2438 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2440 unsigned int elsize = type_memsize(type_array_get_element(type));
2441 unsigned int dim = type_array_get_dim(type);
2443 if (fc == RPC_FC_LGVARRAY)
2445 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2446 *typestring_offset += 4;
2448 else
2450 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2451 *typestring_offset += 2;
2454 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2455 *typestring_offset += 2;
2458 if (length_is)
2459 *typestring_offset
2460 += write_conf_or_var_desc(file, current_structure, baseoff,
2461 type, length_is);
2463 if (type_has_pointers(type_array_get_element(type)) &&
2464 (type_array_is_decl_as_ptr(type) || !current_structure))
2466 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2467 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2468 *typestring_offset += 2;
2469 write_pointer_description(file, type, typestring_offset);
2470 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2471 *typestring_offset += 1;
2474 write_array_element_type(file, type, FALSE, typestring_offset);
2475 write_end(file, typestring_offset);
2477 else
2479 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2480 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2481 *typestring_offset += 2;
2482 *typestring_offset
2483 += write_conf_or_var_desc(file, current_structure, baseoff,
2484 type, size_is);
2485 *typestring_offset
2486 += write_conf_or_var_desc(file, current_structure, baseoff,
2487 type, length_is);
2489 write_array_element_type(file, type, TRUE, typestring_offset);
2490 write_end(file, typestring_offset);
2493 return start_offset;
2496 static const var_t *find_array_or_string_in_struct(const type_t *type)
2498 const var_list_t *fields = type_struct_get_fields(type);
2499 const var_t *last_field;
2500 const type_t *ft;
2502 if (!fields || list_empty(fields))
2503 return NULL;
2505 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2506 ft = last_field->type;
2508 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2509 return last_field;
2511 if (type_get_type(ft) == TYPE_STRUCT)
2512 return find_array_or_string_in_struct(ft);
2513 else
2514 return NULL;
2517 static void write_struct_members(FILE *file, const type_t *type,
2518 int is_complex, unsigned int *corroff,
2519 unsigned int *typestring_offset)
2521 const var_t *field;
2522 unsigned short offset = 0;
2523 unsigned int salign = 1;
2524 int padding;
2525 var_list_t *fields = type_struct_get_fields(type);
2527 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2529 type_t *ft = field->type;
2530 unsigned int align = 0;
2531 unsigned int size = type_memsize_and_alignment(ft, &align);
2532 align = clamp_align(align);
2533 if (salign < align) salign = align;
2535 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2537 if ((align - 1) & offset)
2539 unsigned char fc = 0;
2540 switch (align)
2542 case 2:
2543 fc = RPC_FC_ALIGNM2;
2544 break;
2545 case 4:
2546 fc = RPC_FC_ALIGNM4;
2547 break;
2548 case 8:
2549 fc = RPC_FC_ALIGNM8;
2550 break;
2551 default:
2552 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2554 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2555 offset = ROUND_SIZE(offset, align);
2556 *typestring_offset += 1;
2558 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
2559 typestring_offset);
2560 offset += size;
2564 padding = ROUNDING(offset, salign);
2565 if (padding)
2567 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2568 RPC_FC_STRUCTPAD1 + padding - 1,
2569 padding);
2570 *typestring_offset += 1;
2573 write_end(file, typestring_offset);
2576 static unsigned int write_struct_tfs(FILE *file, type_t *type,
2577 const char *name, unsigned int *tfsoff)
2579 const type_t *save_current_structure = current_structure;
2580 unsigned int total_size;
2581 const var_t *array;
2582 unsigned int start_offset;
2583 unsigned int align;
2584 unsigned int corroff;
2585 var_t *f;
2586 unsigned char fc = get_struct_fc(type);
2587 var_list_t *fields = type_struct_get_fields(type);
2589 if (processed(type)) return type->typestring_offset;
2591 guard_rec(type);
2592 current_structure = type;
2594 total_size = type_memsize(type);
2595 align = type_buffer_alignment(type);
2596 if (total_size > USHRT_MAX)
2597 error("structure size for %s exceeds %d bytes by %d bytes\n",
2598 name, USHRT_MAX, total_size - USHRT_MAX);
2600 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2601 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
2603 array = find_array_or_string_in_struct(type);
2604 if (array && !processed(array->type))
2606 if(is_string_type(array->attrs, array->type))
2607 write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff);
2608 else
2609 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
2612 corroff = *tfsoff;
2613 write_descriptors(file, type, tfsoff);
2615 start_offset = *tfsoff;
2616 update_tfsoff(type, start_offset, file);
2617 print_start_tfs_comment(file, type, start_offset);
2618 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2619 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2620 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
2621 *tfsoff += 4;
2623 if (array)
2625 unsigned int absoff = array->type->typestring_offset;
2626 short reloff = absoff - *tfsoff;
2627 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2628 reloff, reloff, absoff);
2629 *tfsoff += 2;
2631 else if (fc == RPC_FC_BOGUS_STRUCT)
2633 print_file(file, 2, "NdrFcShort(0x0),\n");
2634 *tfsoff += 2;
2637 if (fc == RPC_FC_BOGUS_STRUCT)
2639 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2640 nothing is written to file yet. On the actual writing pass,
2641 this will have been updated. */
2642 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
2643 int reloff = absoff - *tfsoff;
2644 assert( reloff >= 0 );
2645 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2646 (unsigned short)reloff, reloff, absoff);
2647 *tfsoff += 2;
2649 else if ((fc == RPC_FC_PSTRUCT) ||
2650 (fc == RPC_FC_CPSTRUCT) ||
2651 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
2653 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2654 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2655 *tfsoff += 2;
2656 write_pointer_description(file, type, tfsoff);
2657 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2658 *tfsoff += 1;
2661 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
2662 tfsoff);
2664 if (fc == RPC_FC_BOGUS_STRUCT)
2666 const var_t *f;
2668 type->ptrdesc = *tfsoff;
2669 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
2671 type_t *ft = f->type;
2672 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
2674 case TGT_POINTER:
2675 if (is_string_type(f->attrs, ft))
2676 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
2677 else
2678 write_pointer_tfs(file, f->attrs, ft,
2679 type_pointer_get_ref(ft)->typestring_offset,
2680 FALSE, tfsoff);
2681 break;
2682 case TGT_ARRAY:
2683 if (type_array_is_decl_as_ptr(ft))
2685 unsigned int offset;
2687 print_file(file, 0, "/* %d */\n", *tfsoff);
2689 offset = ft->typestring_offset;
2690 /* skip over the pointer that is written for strings, since a
2691 * pointer has to be written in-place here */
2692 if (is_string_type(f->attrs, ft))
2693 offset += 4;
2694 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
2696 break;
2697 default:
2698 break;
2701 if (type->ptrdesc == *tfsoff)
2702 type->ptrdesc = 0;
2705 current_structure = save_current_structure;
2706 return start_offset;
2709 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
2711 if (t == NULL)
2713 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
2715 else
2717 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
2719 unsigned char fc;
2720 if (type_get_type(t) == TYPE_BASIC)
2721 fc = get_basic_fc(t);
2722 else
2723 fc = get_enum_fc(t);
2724 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2725 fc, string_of_type(fc));
2727 else if (t->typestring_offset)
2729 short reloff = t->typestring_offset - *tfsoff;
2730 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2731 reloff, reloff, t->typestring_offset);
2733 else
2734 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
2737 *tfsoff += 2;
2740 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
2741 type_t *type, unsigned int *tfsoff)
2743 unsigned int start_offset;
2744 unsigned int size;
2745 var_list_t *fields;
2746 unsigned int nbranch = 0;
2747 type_t *deftype = NULL;
2748 short nodeftype = 0xffff;
2749 var_t *f;
2751 if (processed(type) &&
2752 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
2753 return type->typestring_offset;
2755 guard_rec(type);
2757 size = type_memsize(type);
2759 fields = type_union_get_cases(type);
2761 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2763 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2764 if (cases)
2765 nbranch += list_count(cases);
2766 if (f->type)
2767 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2770 start_offset = *tfsoff;
2771 update_tfsoff(type, start_offset, file);
2772 print_start_tfs_comment(file, type, start_offset);
2773 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2775 const var_t *sv = type_union_get_switch_value(type);
2776 const type_t *st = sv->type;
2777 unsigned char fc;
2779 if (type_get_type(st) == TYPE_BASIC)
2781 switch (get_basic_fc(st))
2783 case RPC_FC_CHAR:
2784 case RPC_FC_SMALL:
2785 case RPC_FC_BYTE:
2786 case RPC_FC_USMALL:
2787 case RPC_FC_WCHAR:
2788 case RPC_FC_SHORT:
2789 case RPC_FC_USHORT:
2790 case RPC_FC_LONG:
2791 case RPC_FC_ULONG:
2792 fc = get_basic_fc(st);
2793 break;
2794 default:
2795 fc = 0;
2796 error("union switch type must be an integer, char, or enum\n");
2799 else if (type_get_type(st) == TYPE_ENUM)
2800 fc = get_enum_fc(st);
2801 else
2802 error("union switch type must be an integer, char, or enum\n");
2804 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
2805 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2806 0x40 | fc, string_of_type(fc));
2807 *tfsoff += 2;
2809 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
2811 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
2812 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
2813 unsigned char fc;
2815 if (type_get_type(st) == TYPE_BASIC)
2817 switch (get_basic_fc(st))
2819 case RPC_FC_CHAR:
2820 case RPC_FC_SMALL:
2821 case RPC_FC_USMALL:
2822 case RPC_FC_SHORT:
2823 case RPC_FC_USHORT:
2824 case RPC_FC_LONG:
2825 case RPC_FC_ULONG:
2826 case RPC_FC_ENUM16:
2827 case RPC_FC_ENUM32:
2828 fc = get_basic_fc(st);
2829 break;
2830 default:
2831 fc = 0;
2832 error("union switch type must be an integer, char, or enum\n");
2835 else if (type_get_type(st) == TYPE_ENUM)
2836 fc = get_enum_fc(st);
2837 else
2838 error("union switch type must be an integer, char, or enum\n");
2840 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2841 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
2842 fc, string_of_type(fc));
2843 *tfsoff += 2;
2844 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
2845 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
2846 *tfsoff += 2;
2847 print_file(file, 0, "/* %u */\n", *tfsoff);
2850 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
2851 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
2852 *tfsoff += 4;
2854 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
2856 type_t *ft = f->type;
2857 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
2858 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
2859 expr_t *c;
2861 if (cases == NULL && !deflt)
2862 error("union field %s with neither case nor default attribute\n", f->name);
2864 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
2866 /* MIDL doesn't check for duplicate cases, even though that seems
2867 like a reasonable thing to do, it just dumps them to the TFS
2868 like we're going to do here. */
2869 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
2870 *tfsoff += 4;
2871 write_branch_type(file, ft, tfsoff);
2874 /* MIDL allows multiple default branches, even though that seems
2875 illogical, it just chooses the last one, which is what we will
2876 do. */
2877 if (deflt)
2879 deftype = ft;
2880 nodeftype = 0;
2884 if (deftype)
2886 write_branch_type(file, deftype, tfsoff);
2888 else
2890 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
2891 *tfsoff += 2;
2894 return start_offset;
2897 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2898 unsigned int *typeformat_offset)
2900 unsigned int i;
2901 unsigned int start_offset = *typeformat_offset;
2902 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2904 if (!iid && processed(type)) return type->typestring_offset;
2906 print_start_tfs_comment(file, type, start_offset);
2908 if (iid)
2910 print_file(file, 2, "0x2f, /* FC_IP */\n");
2911 print_file(file, 2, "0x5c, /* FC_PAD */\n");
2912 *typeformat_offset
2913 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
2915 else
2917 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
2918 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2920 if (! uuid)
2921 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2923 update_tfsoff(type, start_offset, file);
2924 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
2925 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2926 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
2927 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
2928 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
2929 for (i = 0; i < 8; ++i)
2930 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
2932 if (file)
2933 fprintf(file, "\n");
2935 *typeformat_offset += 18;
2937 return start_offset;
2940 static unsigned int write_contexthandle_tfs(FILE *file,
2941 const attr_list_t *attrs,
2942 const type_t *type,
2943 unsigned int *typeformat_offset)
2945 unsigned int start_offset = *typeformat_offset;
2946 unsigned char flags = 0;
2948 if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2949 flags |= NDR_STRICT_CONTEXT_HANDLE;
2951 if (is_ptr(type))
2952 flags |= 0x80;
2953 if (is_attr(attrs, ATTR_IN))
2955 flags |= 0x40;
2956 if (!is_attr(attrs, ATTR_OUT))
2957 flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2959 if (is_attr(attrs, ATTR_OUT))
2960 flags |= 0x20;
2962 WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
2963 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2964 /* return and can't be null values overlap */
2965 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2966 print_file(file, 0, "can't be null, ");
2967 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2968 print_file(file, 0, "serialize, ");
2969 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2970 print_file(file, 0, "no serialize, ");
2971 if (flags & NDR_STRICT_CONTEXT_HANDLE)
2972 print_file(file, 0, "strict, ");
2973 if ((flags & 0x21) == 0x20)
2974 print_file(file, 0, "out, ");
2975 if ((flags & 0x21) == 0x21)
2976 print_file(file, 0, "return, ");
2977 if (flags & 0x40)
2978 print_file(file, 0, "in, ");
2979 if (flags & 0x80)
2980 print_file(file, 0, "via ptr, ");
2981 print_file(file, 0, "*/\n");
2982 print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
2983 print_file(file, 2, "0, /* FIXME: param num */\n");
2984 *typeformat_offset += 4;
2986 return start_offset;
2989 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
2990 type_t *type, expr_list_t *range_list,
2991 unsigned int *typeformat_offset)
2993 unsigned char fc;
2994 unsigned int start_offset = *typeformat_offset;
2995 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
2996 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
2998 if (type_get_type(type) == TYPE_BASIC)
2999 fc = get_basic_fc(type);
3000 else
3001 fc = get_enum_fc(type);
3003 /* fc must fit in lower 4-bits of 8-bit field below */
3004 assert(fc <= 0xf);
3006 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3007 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3008 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3009 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3010 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3011 *typeformat_offset += 10;
3013 return start_offset;
3016 static unsigned int write_type_tfs(FILE *file, int indent,
3017 const attr_list_t *attrs, type_t *type,
3018 const char *name,
3019 enum type_context context,
3020 unsigned int *typeformat_offset)
3022 unsigned int offset;
3024 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3026 case TGT_CTXT_HANDLE:
3027 case TGT_CTXT_HANDLE_POINTER:
3028 return write_contexthandle_tfs(file, attrs, type, typeformat_offset);
3029 case TGT_USER_TYPE:
3030 return write_user_tfs(file, type, typeformat_offset);
3031 case TGT_STRING:
3032 return write_string_tfs(file, attrs, type,
3033 context == TYPE_CONTEXT_TOPLEVELPARAM,
3034 name, typeformat_offset);
3035 case TGT_ARRAY:
3037 unsigned int off;
3038 /* conformant and pointer arrays are handled specially */
3039 if ((context != TYPE_CONTEXT_CONTAINER &&
3040 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3041 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3042 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3043 else
3044 off = 0;
3045 if (context != TYPE_CONTEXT_CONTAINER &&
3046 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3048 int ptr_type;
3049 ptr_type = get_pointer_fc(type, attrs,
3050 context == TYPE_CONTEXT_TOPLEVELPARAM);
3051 if (ptr_type != RPC_FC_RP)
3053 unsigned int absoff = type->typestring_offset;
3054 short reloff = absoff - (*typeformat_offset + 2);
3055 off = *typeformat_offset;
3056 print_file(file, 0, "/* %d */\n", off);
3057 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3058 string_of_type(ptr_type));
3059 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3060 reloff, reloff, absoff);
3061 *typeformat_offset += 4;
3064 return off;
3066 case TGT_STRUCT:
3067 return write_struct_tfs(file, type, name, typeformat_offset);
3068 case TGT_UNION:
3069 return write_union_tfs(file, attrs, type, typeformat_offset);
3070 case TGT_ENUM:
3071 case TGT_BASIC:
3072 /* nothing to do */
3073 return 0;
3074 case TGT_RANGE:
3076 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3077 if (!range_list)
3078 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3079 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3081 case TGT_IFACE_POINTER:
3082 return write_ip_tfs(file, attrs, type, typeformat_offset);
3083 case TGT_POINTER:
3085 enum type_context ref_context;
3086 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3087 ref_context = TYPE_CONTEXT_PARAM;
3088 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3089 ref_context = TYPE_CONTEXT_CONTAINER;
3090 else
3091 ref_context = context;
3092 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3093 ref_context, typeformat_offset);
3094 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3095 return 0;
3096 return write_pointer_tfs(file, attrs, type, offset,
3097 context == TYPE_CONTEXT_TOPLEVELPARAM,
3098 typeformat_offset);
3100 case TGT_INVALID:
3101 break;
3103 error("invalid type %s for var %s\n", type->name, name);
3104 return 0;
3107 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3108 const char *name, int write_ptr, unsigned int *tfsoff)
3110 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3113 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3114 type_pred_t pred, unsigned int *typeformat_offset)
3116 const var_t *var;
3117 const statement_t *stmt;
3119 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3121 const type_t *iface;
3122 const statement_t *stmt_func;
3124 if (stmt->type == STMT_LIBRARY)
3126 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
3127 continue;
3129 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3130 continue;
3132 iface = stmt->u.type;
3133 if (!pred(iface))
3134 continue;
3136 current_iface = iface;
3137 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3139 const var_t *func = stmt_func->u.var;
3140 current_func = func;
3141 if (is_local(func->attrs)) continue;
3143 if (!is_void(type_function_get_rettype(func->type)))
3145 update_tfsoff(type_function_get_rettype(func->type),
3146 write_type_tfs(
3147 file, 2, func->attrs,
3148 type_function_get_rettype(func->type),
3149 func->name, TYPE_CONTEXT_PARAM,
3150 typeformat_offset),
3151 file);
3154 if (type_get_function_args(func->type))
3155 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3156 update_tfsoff(
3157 var->type,
3158 write_type_tfs(
3159 file, 2, var->attrs, var->type, var->name,
3160 TYPE_CONTEXT_TOPLEVELPARAM,
3161 typeformat_offset),
3162 file);
3166 return *typeformat_offset + 1;
3169 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3171 unsigned int typeformat_offset = 2;
3173 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3177 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3179 int indent = 0;
3181 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3182 print_file(file, indent, "{\n");
3183 indent++;
3184 print_file(file, indent, "0,\n");
3185 print_file(file, indent, "{\n");
3186 indent++;
3187 print_file(file, indent, "NdrFcShort(0x0),\n");
3189 set_all_tfswrite(TRUE);
3190 process_tfs(file, stmts, pred);
3192 print_file(file, indent, "0x0\n");
3193 indent--;
3194 print_file(file, indent, "}\n");
3195 indent--;
3196 print_file(file, indent, "};\n");
3197 print_file(file, indent, "\n");
3200 static unsigned int get_required_buffer_size_type(
3201 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3203 *alignment = 0;
3204 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3206 case TGT_USER_TYPE:
3208 const char *uname;
3209 const type_t *utype = get_user_type(type, &uname);
3210 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3212 case TGT_BASIC:
3213 switch (get_basic_fc(type))
3215 case RPC_FC_BYTE:
3216 case RPC_FC_CHAR:
3217 case RPC_FC_USMALL:
3218 case RPC_FC_SMALL:
3219 *alignment = 4;
3220 return 1;
3222 case RPC_FC_WCHAR:
3223 case RPC_FC_USHORT:
3224 case RPC_FC_SHORT:
3225 *alignment = 4;
3226 return 2;
3228 case RPC_FC_ULONG:
3229 case RPC_FC_LONG:
3230 case RPC_FC_FLOAT:
3231 case RPC_FC_ERROR_STATUS_T:
3232 *alignment = 4;
3233 return 4;
3235 case RPC_FC_HYPER:
3236 case RPC_FC_DOUBLE:
3237 *alignment = 8;
3238 return 8;
3240 case RPC_FC_INT3264:
3241 case RPC_FC_UINT3264:
3242 assert( pointer_size );
3243 *alignment = pointer_size;
3244 return pointer_size;
3246 case RPC_FC_IGNORE:
3247 case RPC_FC_BIND_PRIMITIVE:
3248 return 0;
3250 default:
3251 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3252 get_basic_fc(type));
3253 return 0;
3255 break;
3257 case TGT_ENUM:
3258 switch (get_enum_fc(type))
3260 case RPC_FC_ENUM32:
3261 *alignment = 4;
3262 return 4;
3263 case RPC_FC_ENUM16:
3264 *alignment = 4;
3265 return 2;
3267 break;
3269 case TGT_STRUCT:
3270 if (get_struct_fc(type) == RPC_FC_STRUCT)
3272 if (!type_struct_get_fields(type)) return 0;
3273 return fields_memsize(type_struct_get_fields(type), alignment);
3275 break;
3277 case TGT_POINTER:
3279 unsigned int size, align;
3280 const type_t *ref = type_pointer_get_ref(type);
3281 if (is_string_type( attrs, ref )) break;
3282 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3283 if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3285 size += 4 + align;
3286 align = 4;
3288 *alignment = align;
3289 return size;
3292 case TGT_ARRAY:
3293 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3295 switch (get_array_fc(type))
3297 case RPC_FC_SMFARRAY:
3298 case RPC_FC_LGFARRAY:
3299 return type_array_get_dim(type) *
3300 get_required_buffer_size_type(type_array_get_element(type), name,
3301 NULL, FALSE, alignment);
3304 break;
3306 default:
3307 break;
3309 return 0;
3312 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3314 int in_attr = is_attr(var->attrs, ATTR_IN);
3315 int out_attr = is_attr(var->attrs, ATTR_OUT);
3317 if (!in_attr && !out_attr)
3318 in_attr = 1;
3320 *alignment = 0;
3322 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3323 pass == PASS_RETURN)
3325 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3327 *alignment = 4;
3328 return 20;
3331 if (!is_string_type(var->attrs, var->type))
3332 return get_required_buffer_size_type(var->type, var->name,
3333 var->attrs, TRUE, alignment);
3335 return 0;
3338 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3340 const var_t *var;
3341 unsigned int total_size = 0, alignment;
3343 if (type_get_function_args(func->type))
3345 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3347 total_size += get_required_buffer_size(var, &alignment, pass);
3348 total_size += alignment;
3352 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3354 var_t v = *func;
3355 v.type = type_function_get_rettype(func->type);
3356 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3357 total_size += alignment;
3359 return total_size;
3362 static void print_phase_function(FILE *file, int indent, const char *type,
3363 const char *local_var_prefix, enum remoting_phase phase,
3364 const var_t *var, unsigned int type_offset)
3366 const char *function;
3367 switch (phase)
3369 case PHASE_BUFFERSIZE:
3370 function = "BufferSize";
3371 break;
3372 case PHASE_MARSHAL:
3373 function = "Marshall";
3374 break;
3375 case PHASE_UNMARSHAL:
3376 function = "Unmarshall";
3377 break;
3378 case PHASE_FREE:
3379 function = "Free";
3380 break;
3381 default:
3382 assert(0);
3383 return;
3386 print_file(file, indent, "Ndr%s%s(\n", type, function);
3387 indent++;
3388 print_file(file, indent, "&__frame->_StubMsg,\n");
3389 print_file(file, indent, "%s%s%s%s%s,\n",
3390 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3391 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3392 local_var_prefix,
3393 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3394 var->name);
3395 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3396 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3397 if (phase == PHASE_UNMARSHAL)
3398 print_file(file, indent, "0);\n");
3399 indent--;
3402 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3403 enum remoting_phase phase, enum pass pass, const var_t *var,
3404 const char *varname)
3406 type_t *type = var->type;
3407 unsigned int alignment = 0;
3409 /* no work to do for other phases, buffer sizing is done elsewhere */
3410 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3411 return;
3413 if (type_get_type(type) == TYPE_ENUM ||
3414 (type_get_type(type) == TYPE_BASIC &&
3415 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3416 pointer_size != 4))
3418 unsigned char fc;
3420 if (type_get_type(type) == TYPE_ENUM)
3421 fc = get_enum_fc(type);
3422 else
3423 fc = get_basic_fc(type);
3425 if (phase == PHASE_MARSHAL)
3426 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3427 else
3428 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3429 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3430 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3431 local_var_prefix,
3432 var->name);
3433 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3435 else
3437 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3438 switch (get_basic_fc(ref))
3440 case RPC_FC_BYTE:
3441 case RPC_FC_CHAR:
3442 case RPC_FC_SMALL:
3443 case RPC_FC_USMALL:
3444 alignment = 1;
3445 break;
3447 case RPC_FC_WCHAR:
3448 case RPC_FC_USHORT:
3449 case RPC_FC_SHORT:
3450 alignment = 2;
3451 break;
3453 case RPC_FC_ULONG:
3454 case RPC_FC_LONG:
3455 case RPC_FC_FLOAT:
3456 case RPC_FC_ERROR_STATUS_T:
3457 /* pointer_size must be 4 if we got here in these two cases */
3458 case RPC_FC_INT3264:
3459 case RPC_FC_UINT3264:
3460 alignment = 4;
3461 break;
3463 case RPC_FC_HYPER:
3464 case RPC_FC_DOUBLE:
3465 alignment = 8;
3466 break;
3468 case RPC_FC_IGNORE:
3469 case RPC_FC_BIND_PRIMITIVE:
3470 /* no marshalling needed */
3471 return;
3473 default:
3474 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3475 var->name, get_basic_fc(ref));
3478 if (phase == PHASE_MARSHAL && alignment > 1)
3479 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3480 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3481 alignment - 1, alignment - 1);
3483 if (phase == PHASE_MARSHAL)
3485 print_file(file, indent, "*(");
3486 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3487 if (is_ptr(type))
3488 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3489 else
3490 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3491 fprintf(file, "%s%s", local_var_prefix, varname);
3492 fprintf(file, ";\n");
3494 else if (phase == PHASE_UNMARSHAL)
3496 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3497 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3498 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3499 print_file(file, indent, "{\n");
3500 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3501 print_file(file, indent, "}\n");
3502 print_file(file, indent, "%s%s%s",
3503 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3504 local_var_prefix, varname);
3505 if (pass == PASS_IN && is_ptr(type))
3506 fprintf(file, " = (");
3507 else
3508 fprintf(file, " = *(");
3509 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3510 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3513 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3514 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3515 fprintf(file, ");\n");
3519 /* returns whether the MaxCount, Offset or ActualCount members need to be
3520 * filled in for the specified phase */
3521 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3523 return (phase != PHASE_UNMARSHAL);
3526 expr_t *get_size_is_expr(const type_t *t, const char *name)
3528 expr_t *x = NULL;
3530 for ( ; is_array(t); t = type_array_get_element(t))
3531 if (type_array_has_conformance(t) &&
3532 type_array_get_conformance(t)->type != EXPR_VOID)
3534 if (!x)
3535 x = type_array_get_conformance(t);
3536 else
3537 error("%s: multidimensional conformant"
3538 " arrays not supported at the top level\n",
3539 name);
3542 return x;
3545 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3546 enum remoting_phase phase, const var_t *var)
3548 const type_t *type = var->type;
3549 /* get fundamental type for the argument */
3550 for (;;)
3552 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3554 case TGT_ARRAY:
3555 if (is_conformance_needed_for_phase(phase))
3557 if (type_array_has_conformance(type) &&
3558 type_array_get_conformance(type)->type != EXPR_VOID)
3560 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3561 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
3562 fprintf(file, ";\n\n");
3564 if (type_array_has_variance(type))
3566 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3567 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3568 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
3569 fprintf(file, ";\n\n");
3572 break;
3573 case TGT_UNION:
3574 if (type_get_type(type) == TYPE_UNION &&
3575 is_conformance_needed_for_phase(phase))
3577 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3578 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
3579 fprintf(file, ";\n\n");
3581 break;
3582 case TGT_IFACE_POINTER:
3584 expr_t *iid;
3586 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
3588 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3589 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3590 fprintf( file, ";\n\n" );
3592 break;
3594 case TGT_POINTER:
3595 type = type_pointer_get_ref(type);
3596 continue;
3597 case TGT_INVALID:
3598 case TGT_USER_TYPE:
3599 case TGT_CTXT_HANDLE:
3600 case TGT_CTXT_HANDLE_POINTER:
3601 case TGT_STRING:
3602 case TGT_BASIC:
3603 case TGT_ENUM:
3604 case TGT_STRUCT:
3605 case TGT_RANGE:
3606 break;
3608 break;
3612 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3613 enum pass pass, enum remoting_phase phase, const var_t *var)
3615 int in_attr, out_attr, pointer_type;
3616 const char *type_str = NULL;
3617 const type_t *type = var->type;
3618 unsigned int alignment, start_offset = type->typestring_offset;
3620 if (is_ptr(type) || is_array(type))
3621 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
3622 else
3623 pointer_type = 0;
3625 in_attr = is_attr(var->attrs, ATTR_IN);
3626 out_attr = is_attr(var->attrs, ATTR_OUT);
3627 if (!in_attr && !out_attr)
3628 in_attr = 1;
3630 if (phase != PHASE_FREE)
3631 switch (pass)
3633 case PASS_IN:
3634 if (!in_attr) return;
3635 break;
3636 case PASS_OUT:
3637 if (!out_attr) return;
3638 break;
3639 case PASS_RETURN:
3640 break;
3643 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
3645 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
3647 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
3649 case TGT_CTXT_HANDLE:
3650 case TGT_CTXT_HANDLE_POINTER:
3651 if (phase == PHASE_MARSHAL)
3653 if (pass == PASS_IN)
3655 /* if the context_handle attribute appears in the chain of types
3656 * without pointers being followed, then the context handle must
3657 * be direct, otherwise it is a pointer */
3658 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
3659 print_file(file, indent, "NdrClientContextMarshall(\n");
3660 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3661 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
3662 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
3664 else
3666 print_file(file, indent, "NdrServerContextNewMarshall(\n");
3667 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3668 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
3669 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
3670 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3673 else if (phase == PHASE_UNMARSHAL)
3675 if (pass == PASS_OUT)
3677 if (!in_attr)
3678 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
3679 print_file(file, indent, "NdrClientContextUnmarshall(\n");
3680 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3681 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
3682 print_file(file, indent + 1, "__frame->_Handle);\n");
3684 else
3686 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
3687 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3688 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
3691 break;
3692 case TGT_USER_TYPE:
3693 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
3694 break;
3695 case TGT_STRING:
3696 if (phase == PHASE_FREE || pass == PASS_RETURN ||
3697 pointer_type != RPC_FC_RP)
3699 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
3700 !in_attr && is_conformant_array(type))
3702 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3703 indent++;
3704 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3706 /* strings returned are assumed to be global and hence don't
3707 * need freeing */
3708 else if (is_declptr(type) &&
3709 !(phase == PHASE_FREE && pass == PASS_RETURN))
3710 print_phase_function(file, indent, "Pointer", local_var_prefix,
3711 phase, var, start_offset);
3713 else
3715 unsigned int real_start_offset = start_offset;
3716 /* skip over pointer description straight to string description */
3717 if (is_declptr(type))
3719 if (is_conformant_array(type))
3720 real_start_offset += 4;
3721 else
3722 real_start_offset += 2;
3724 if (is_array(type) && !is_conformant_array(type))
3725 print_phase_function(file, indent, "NonConformantString",
3726 local_var_prefix, phase, var,
3727 real_start_offset);
3728 else
3729 print_phase_function(file, indent, "ConformantString", local_var_prefix,
3730 phase, var, real_start_offset);
3732 break;
3733 case TGT_ARRAY:
3735 unsigned char tc = get_array_fc(type);
3736 const char *array_type = NULL;
3738 /* We already have the size_is expression since it's at the
3739 top level, but do checks for multidimensional conformant
3740 arrays. When we handle them, we'll need to extend this
3741 function to return a list, and then we'll actually use
3742 the return value. */
3743 get_size_is_expr(type, var->name);
3745 switch (tc)
3747 case RPC_FC_SMFARRAY:
3748 case RPC_FC_LGFARRAY:
3749 array_type = "FixedArray";
3750 break;
3751 case RPC_FC_SMVARRAY:
3752 case RPC_FC_LGVARRAY:
3753 array_type = "VaryingArray";
3754 break;
3755 case RPC_FC_CARRAY:
3756 array_type = "ConformantArray";
3757 break;
3758 case RPC_FC_CVARRAY:
3759 array_type = "ConformantVaryingArray";
3760 break;
3761 case RPC_FC_BOGUS_ARRAY:
3762 array_type = "ComplexArray";
3763 break;
3766 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
3767 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
3768 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
3770 /* these are all unmarshalled by allocating memory */
3771 if (tc == RPC_FC_BOGUS_ARRAY ||
3772 tc == RPC_FC_CVARRAY ||
3773 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
3774 (tc == RPC_FC_CARRAY && !in_attr))
3776 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3777 indent++;
3778 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3781 break;
3783 case TGT_BASIC:
3784 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3785 break;
3786 case TGT_ENUM:
3787 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3788 break;
3789 case TGT_RANGE:
3790 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3791 /* Note: this goes beyond what MIDL does - it only supports arguments
3792 * with the [range] attribute in Oicf mode */
3793 if (phase == PHASE_UNMARSHAL)
3795 const expr_t *range_min;
3796 const expr_t *range_max;
3797 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
3798 if (!range_list)
3799 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3800 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3801 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3803 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
3804 write_type_decl(file, var->type, NULL);
3805 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
3806 write_type_decl(file, var->type, NULL);
3807 fprintf(file, ")0x%x))\n", range_max->cval);
3808 print_file(file, indent, "{\n");
3809 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
3810 print_file(file, indent, "}\n");
3812 break;
3813 case TGT_STRUCT:
3814 switch (get_struct_fc(type))
3816 case RPC_FC_STRUCT:
3817 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3818 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3819 break;
3820 case RPC_FC_PSTRUCT:
3821 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3822 break;
3823 case RPC_FC_CSTRUCT:
3824 case RPC_FC_CPSTRUCT:
3825 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3826 break;
3827 case RPC_FC_CVSTRUCT:
3828 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3829 break;
3830 case RPC_FC_BOGUS_STRUCT:
3831 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3832 break;
3833 default:
3834 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
3836 break;
3837 case TGT_UNION:
3839 const char *union_type = NULL;
3841 if (type_get_type(type) == TYPE_UNION)
3842 union_type = "NonEncapsulatedUnion";
3843 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3844 union_type = "EncapsulatedUnion";
3846 print_phase_function(file, indent, union_type, local_var_prefix,
3847 phase, var, start_offset);
3848 break;
3850 case TGT_POINTER:
3852 const type_t *ref = type_pointer_get_ref(type);
3853 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
3855 case TGT_BASIC:
3856 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3857 break;
3858 case TGT_ENUM:
3859 /* base types have known sizes, so don't need a sizing pass
3860 * and don't have any memory to free and so don't need a
3861 * freeing pass */
3862 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3863 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3864 break;
3865 case TGT_STRUCT:
3866 switch (get_struct_fc(ref))
3868 case RPC_FC_STRUCT:
3869 /* simple structs have known sizes, so don't need a sizing
3870 * pass and don't have any memory to free and so don't
3871 * need a freeing pass */
3872 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
3873 type_str = "SimpleStruct";
3874 else if (phase == PHASE_FREE && pass == PASS_RETURN)
3876 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
3877 indent++;
3878 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
3879 indent--;
3881 break;
3882 case RPC_FC_PSTRUCT:
3883 type_str = "SimpleStruct";
3884 break;
3885 case RPC_FC_CSTRUCT:
3886 case RPC_FC_CPSTRUCT:
3887 type_str = "ConformantStruct";
3888 break;
3889 case RPC_FC_CVSTRUCT:
3890 type_str = "ConformantVaryingStruct";
3891 break;
3892 case RPC_FC_BOGUS_STRUCT:
3893 type_str = "ComplexStruct";
3894 break;
3895 default:
3896 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
3899 if (type_str)
3901 if (phase == PHASE_FREE)
3902 type_str = "Pointer";
3903 else
3904 start_offset = ref->typestring_offset;
3905 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
3907 break;
3908 case TGT_UNION:
3909 if (phase == PHASE_FREE)
3910 type_str = "Pointer";
3911 else
3913 if (type_get_type(ref) == TYPE_UNION)
3914 type_str = "NonEncapsulatedUnion";
3915 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
3916 type_str = "EncapsulatedUnion";
3918 start_offset = ref->typestring_offset;
3921 print_phase_function(file, indent, type_str, local_var_prefix,
3922 phase, var, start_offset);
3923 break;
3924 case TGT_USER_TYPE:
3925 if (phase != PHASE_FREE)
3927 type_str = "UserMarshal";
3928 start_offset = ref->typestring_offset;
3930 else type_str = "Pointer";
3932 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
3933 break;
3934 case TGT_STRING:
3935 case TGT_POINTER:
3936 case TGT_ARRAY:
3937 case TGT_RANGE:
3938 case TGT_IFACE_POINTER:
3939 case TGT_CTXT_HANDLE:
3940 case TGT_CTXT_HANDLE_POINTER:
3941 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3942 break;
3943 case TGT_INVALID:
3944 assert(0);
3945 break;
3947 else
3948 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3949 break;
3951 case TGT_IFACE_POINTER:
3952 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3953 break;
3954 case TGT_INVALID:
3955 assert(0);
3956 break;
3958 fprintf(file, "\n");
3961 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
3962 enum pass pass, enum remoting_phase phase)
3964 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
3966 unsigned int size = get_function_buffer_size( func, pass );
3967 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3970 if (pass == PASS_RETURN)
3972 var_t var;
3973 var = *func;
3974 var.type = type_function_get_rettype(func->type);
3975 var.name = xstrdup( "_RetVal" );
3976 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3977 free( var.name );
3979 else
3981 const var_t *var;
3982 if (!type_get_function_args(func->type))
3983 return;
3984 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3985 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3990 unsigned int get_size_procformatstring_func(const var_t *func)
3992 unsigned int offset = 0;
3993 write_procformatstring_func( NULL, 0, func, &offset );
3994 return offset;
3997 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3999 const statement_t *stmt;
4000 unsigned int size = 1;
4002 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4004 const type_t *iface;
4005 const statement_t *stmt_func;
4007 if (stmt->type == STMT_LIBRARY)
4009 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
4010 continue;
4012 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4013 continue;
4015 iface = stmt->u.type;
4016 if (!pred(iface))
4017 continue;
4019 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4021 const var_t *func = stmt_func->u.var;
4022 if (!is_local(func->attrs))
4023 size += get_size_procformatstring_func( func );
4026 return size;
4029 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4031 set_all_tfswrite(FALSE);
4032 return process_tfs(NULL, stmts, pred);
4035 void declare_stub_args( FILE *file, int indent, const var_t *func )
4037 int in_attr, out_attr;
4038 int i = 0;
4039 const var_t *var;
4041 /* declare return value '_RetVal' */
4042 if (!is_void(type_function_get_rettype(func->type)))
4044 print_file(file, indent, "%s", "");
4045 write_type_decl_left(file, type_function_get_rettype(func->type));
4046 fprintf(file, " _RetVal;\n");
4049 if (!type_get_function_args(func->type))
4050 return;
4052 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4054 in_attr = is_attr(var->attrs, ATTR_IN);
4055 out_attr = is_attr(var->attrs, ATTR_OUT);
4056 if (!out_attr && !in_attr)
4057 in_attr = 1;
4059 if (is_context_handle(var->type))
4060 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4061 else
4063 if (!in_attr && !is_conformant_array(var->type))
4065 type_t *type_to_print;
4066 char name[16];
4067 print_file(file, indent, "%s", "");
4068 if (type_get_type(var->type) == TYPE_ARRAY &&
4069 !type_array_is_decl_as_ptr(var->type))
4070 type_to_print = var->type;
4071 else
4072 type_to_print = type_pointer_get_ref(var->type);
4073 sprintf(name, "_W%u", i++);
4074 write_type_decl(file, type_to_print, name);
4075 fprintf(file, ";\n");
4078 print_file(file, indent, "%s", "");
4079 write_type_decl_left(file, var->type);
4080 fprintf(file, " ");
4081 if (type_get_type(var->type) == TYPE_ARRAY &&
4082 !type_array_is_decl_as_ptr(var->type)) {
4083 fprintf(file, "(*%s)", var->name);
4084 } else
4085 fprintf(file, "%s", var->name);
4086 write_type_right(file, var->type, FALSE);
4087 fprintf(file, ";\n");
4089 if (decl_indirect(var->type))
4090 print_file(file, indent, "void *_p_%s;\n", var->name);
4096 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4098 int in_attr, out_attr;
4099 int i = 0, sep = 0;
4100 const var_t *var;
4102 if (!type_get_function_args(func->type))
4103 return;
4105 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4107 in_attr = is_attr(var->attrs, ATTR_IN);
4108 out_attr = is_attr(var->attrs, ATTR_OUT);
4109 if (!out_attr && !in_attr)
4110 in_attr = 1;
4112 if (!in_attr)
4114 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4116 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4118 case TGT_CTXT_HANDLE_POINTER:
4119 fprintf(file, " = NdrContextHandleInitialize(\n");
4120 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4121 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4122 var->type->typestring_offset);
4123 break;
4124 case TGT_ARRAY:
4125 if (type_array_has_conformance(var->type))
4127 unsigned int size;
4128 type_t *type;
4130 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4131 for (type = var->type;
4132 is_array(type) && type_array_has_conformance(type);
4133 type = type_array_get_element(type))
4135 write_expr(file, type_array_get_conformance(type), TRUE,
4136 TRUE, NULL, NULL, local_var_prefix);
4137 fprintf(file, " * ");
4139 size = type_memsize(type);
4140 fprintf(file, "%u);\n", size);
4142 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4143 for (type = var->type;
4144 is_array(type) && type_array_has_conformance(type);
4145 type = type_array_get_element(type))
4147 write_expr(file, type_array_get_conformance(type), TRUE,
4148 TRUE, NULL, NULL, local_var_prefix);
4149 fprintf(file, " * ");
4151 size = type_memsize(type);
4152 fprintf(file, "%u);\n", size);
4154 else
4155 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4156 break;
4157 case TGT_POINTER:
4158 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4159 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
4161 case TGT_BASIC:
4162 case TGT_ENUM:
4163 case TGT_POINTER:
4164 case TGT_RANGE:
4165 case TGT_IFACE_POINTER:
4166 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4167 break;
4168 case TGT_USER_TYPE:
4169 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4170 local_var_prefix, i, local_var_prefix, i);
4171 break;
4172 case TGT_STRUCT:
4173 case TGT_UNION:
4174 case TGT_ARRAY:
4175 case TGT_CTXT_HANDLE:
4176 case TGT_CTXT_HANDLE_POINTER:
4177 case TGT_INVALID:
4178 case TGT_STRING:
4179 /* not initialised */
4180 break;
4182 i++;
4183 break;
4184 default:
4185 break;
4188 sep = 1;
4191 if (sep)
4192 fprintf(file, "\n");
4196 int write_expr_eval_routines(FILE *file, const char *iface)
4198 static const char *var_name = "pS";
4199 static const char *var_name_expr = "pS->";
4200 int result = 0;
4201 struct expr_eval_routine *eval;
4202 unsigned short callback_offset = 0;
4204 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4206 const char *name = eval->name;
4207 result = 1;
4209 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4210 iface, name, callback_offset);
4211 print_file(file, 0, "{\n");
4212 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4214 const var_list_t *args = type_get_function_args( eval->cont_type );
4215 const var_t *arg;
4217 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4218 print_file(file, 1, "{\n" );
4219 if (is_object( eval->iface )) print_file(file, 2, "%s *This;\n", eval->iface->name );
4221 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4223 print_file(file, 2, "%s", "");
4224 write_type_left( file, (type_t *)arg->type, TRUE );
4225 if (needs_space_after( arg->type )) fputc( ' ', file );
4226 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4227 /* FIXME: should check for large args being passed by pointer */
4228 if (is_array( arg->type ) || is_ptr( arg->type ) || type_memsize( arg->type ) == pointer_size)
4229 fprintf( file, "%s;\n", arg->name );
4230 else
4231 fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4233 print_file(file, 1, "} *pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop;\n" );
4235 else
4237 print_file(file, 1, "%s", "");
4238 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4239 fprintf(file, " *%s = (", var_name);
4240 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4241 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4243 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4244 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4245 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4246 fprintf(file, ";\n");
4247 print_file(file, 0, "}\n\n");
4248 callback_offset++;
4250 return result;
4253 void write_expr_eval_routine_list(FILE *file, const char *iface)
4255 struct expr_eval_routine *eval;
4256 struct expr_eval_routine *cursor;
4257 unsigned short callback_offset = 0;
4259 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4260 fprintf(file, "{\n");
4262 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4264 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, eval->name, callback_offset);
4265 callback_offset++;
4266 list_remove(&eval->entry);
4267 free(eval->name);
4268 free(eval);
4271 fprintf(file, "};\n\n");
4274 void write_user_quad_list(FILE *file)
4276 user_type_t *ut;
4278 if (list_empty(&user_type_list))
4279 return;
4281 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4282 fprintf(file, "{\n");
4283 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4285 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4286 print_file(file, 1, "{\n");
4287 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4288 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4289 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4290 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4291 print_file(file, 1, "}%s\n", sep);
4293 fprintf(file, "};\n\n");
4296 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4298 const struct str_list_entry_t *endpoint;
4299 const char *p;
4301 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4302 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4303 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4305 print_file( f, 1, "{ (const unsigned char *)\"" );
4306 for (p = endpoint->str; *p && *p != ':'; p++)
4308 if (*p == '"' || *p == '\\') fputc( '\\', f );
4309 fputc( *p, f );
4311 if (!*p) goto error;
4312 if (p[1] != '[') goto error;
4314 fprintf( f, "\", (const unsigned char *)\"" );
4315 for (p += 2; *p && *p != ']'; p++)
4317 if (*p == '"' || *p == '\\') fputc( '\\', f );
4318 fputc( *p, f );
4320 if (*p != ']') goto error;
4321 fprintf( f, "\" },\n" );
4323 print_file( f, 0, "};\n\n" );
4324 return;
4326 error:
4327 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4330 void write_exceptions( FILE *file )
4332 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4333 fprintf( file, "\n");
4334 fprintf( file, "#include \"wine/exception.h\"\n");
4335 fprintf( file, "#undef RpcTryExcept\n");
4336 fprintf( file, "#undef RpcExcept\n");
4337 fprintf( file, "#undef RpcEndExcept\n");
4338 fprintf( file, "#undef RpcTryFinally\n");
4339 fprintf( file, "#undef RpcFinally\n");
4340 fprintf( file, "#undef RpcEndFinally\n");
4341 fprintf( file, "#undef RpcExceptionCode\n");
4342 fprintf( file, "#undef RpcAbnormalTermination\n");
4343 fprintf( file, "\n");
4344 fprintf( file, "struct __exception_frame;\n");
4345 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4346 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4347 fprintf( file, "\n");
4348 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4349 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4350 fprintf( file, " __filter_func filter; \\\n");
4351 fprintf( file, " __finally_func finally; \\\n");
4352 fprintf( file, " sigjmp_buf jmp; \\\n");
4353 fprintf( file, " DWORD code; \\\n");
4354 fprintf( file, " unsigned char abnormal_termination; \\\n");
4355 fprintf( file, " unsigned char filter_level; \\\n");
4356 fprintf( file, " unsigned char finally_level;\n");
4357 fprintf( file, "\n");
4358 fprintf( file, "struct __exception_frame\n{\n");
4359 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4360 fprintf( file, "};\n");
4361 fprintf( file, "\n");
4362 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4363 fprintf( file, "{\n");
4364 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4365 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4366 fprintf( file, " {\n");
4367 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4368 fprintf( file, " exc_frame->finally( exc_frame );\n");
4369 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4370 fprintf( file, " }\n");
4371 fprintf( file, " exc_frame->filter_level = 0;\n");
4372 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4373 fprintf( file, "}\n");
4374 fprintf( file, "\n");
4375 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4376 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4377 fprintf( file, " CONTEXT *context,\n");
4378 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4379 fprintf( file, "{\n");
4380 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4381 fprintf( file, "\n");
4382 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4383 fprintf( file, " {\n" );
4384 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4385 fprintf( file, " {\n" );
4386 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4387 fprintf( file, " exc_frame->finally( exc_frame );\n");
4388 fprintf( file, " }\n" );
4389 fprintf( file, " return ExceptionContinueSearch;\n");
4390 fprintf( file, " }\n" );
4391 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4392 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4393 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4394 fprintf( file, " return ExceptionContinueSearch;\n");
4395 fprintf( file, "}\n");
4396 fprintf( file, "\n");
4397 fprintf( file, "#define RpcTryExcept \\\n");
4398 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4399 fprintf( file, " { \\\n");
4400 fprintf( file, " if (!__frame->finally_level) \\\n" );
4401 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4402 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4403 fprintf( file, "\n");
4404 fprintf( file, "#define RpcExcept(expr) \\\n");
4405 fprintf( file, " if (!__frame->finally_level) \\\n" );
4406 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4407 fprintf( file, " __frame->filter_level = 0; \\\n" );
4408 fprintf( file, " } \\\n");
4409 fprintf( file, " else \\\n");
4410 fprintf( file, "\n");
4411 fprintf( file, "#define RpcEndExcept\n");
4412 fprintf( file, "\n");
4413 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4414 fprintf( file, "\n");
4415 fprintf( file, "#define RpcTryFinally \\\n");
4416 fprintf( file, " if (!__frame->filter_level) \\\n");
4417 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4418 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4419 fprintf( file, "\n");
4420 fprintf( file, "#define RpcFinally \\\n");
4421 fprintf( file, " if (!__frame->filter_level) \\\n");
4422 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4423 fprintf( file, " __frame->finally_level = 0;\n");
4424 fprintf( file, "\n");
4425 fprintf( file, "#define RpcEndFinally\n");
4426 fprintf( file, "\n");
4427 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4428 fprintf( file, "\n");
4429 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4430 fprintf( file, " do { \\\n");
4431 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4432 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4433 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4434 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4435 fprintf( file, " __frame->filter_level = 0; \\\n");
4436 fprintf( file, " __frame->finally_level = 0; \\\n");
4437 fprintf( file, " } while (0)\n");
4438 fprintf( file, "\n");
4439 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4440 fprintf( file, "\n");
4441 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4442 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4443 fprintf( file, "\n");
4444 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4445 fprintf( file, " DWORD code;\n");
4446 fprintf( file, "\n");
4447 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");