qcap/tests: Get rid of the VFW capture filter tests.
[wine.git] / tools / widl / typegen.c
bloba1db88a7c0978f73bcd02c34483e050a90979d82
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,
70 TYPE_CONTEXT_RETVAL,
73 /* parameter flags in Oif mode */
74 static const unsigned short MustSize = 0x0001;
75 static const unsigned short MustFree = 0x0002;
76 static const unsigned short IsPipe = 0x0004;
77 static const unsigned short IsIn = 0x0008;
78 static const unsigned short IsOut = 0x0010;
79 static const unsigned short IsReturn = 0x0020;
80 static const unsigned short IsBasetype = 0x0040;
81 static const unsigned short IsByValue = 0x0080;
82 static const unsigned short IsSimpleRef = 0x0100;
83 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
84 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
86 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
87 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
88 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
89 const char *name, unsigned int *typestring_offset);
90 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
91 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
92 const char *name, int write_ptr, unsigned int *tfsoff);
93 static const var_t *find_array_or_string_in_struct(const type_t *type);
94 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
95 type_t *type, enum type_context context,
96 const char *name, unsigned int *typestring_offset);
97 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
98 const attr_list_t *attrs, int toplevel_param,
99 unsigned int *alignment );
100 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
102 static const char *string_of_type(unsigned char type)
104 switch (type)
106 case FC_BYTE: return "FC_BYTE";
107 case FC_CHAR: return "FC_CHAR";
108 case FC_SMALL: return "FC_SMALL";
109 case FC_USMALL: return "FC_USMALL";
110 case FC_WCHAR: return "FC_WCHAR";
111 case FC_SHORT: return "FC_SHORT";
112 case FC_USHORT: return "FC_USHORT";
113 case FC_LONG: return "FC_LONG";
114 case FC_ULONG: return "FC_ULONG";
115 case FC_FLOAT: return "FC_FLOAT";
116 case FC_HYPER: return "FC_HYPER";
117 case FC_DOUBLE: return "FC_DOUBLE";
118 case FC_ENUM16: return "FC_ENUM16";
119 case FC_ENUM32: return "FC_ENUM32";
120 case FC_IGNORE: return "FC_IGNORE";
121 case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
122 case FC_RP: return "FC_RP";
123 case FC_UP: return "FC_UP";
124 case FC_OP: return "FC_OP";
125 case FC_FP: return "FC_FP";
126 case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
127 case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
128 case FC_STRUCT: return "FC_STRUCT";
129 case FC_PSTRUCT: return "FC_PSTRUCT";
130 case FC_CSTRUCT: return "FC_CSTRUCT";
131 case FC_CPSTRUCT: return "FC_CPSTRUCT";
132 case FC_CVSTRUCT: return "FC_CVSTRUCT";
133 case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
134 case FC_SMFARRAY: return "FC_SMFARRAY";
135 case FC_LGFARRAY: return "FC_LGFARRAY";
136 case FC_SMVARRAY: return "FC_SMVARRAY";
137 case FC_LGVARRAY: return "FC_LGVARRAY";
138 case FC_CARRAY: return "FC_CARRAY";
139 case FC_CVARRAY: return "FC_CVARRAY";
140 case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
141 case FC_ALIGNM2: return "FC_ALIGNM2";
142 case FC_ALIGNM4: return "FC_ALIGNM4";
143 case FC_ALIGNM8: return "FC_ALIGNM8";
144 case FC_POINTER: return "FC_POINTER";
145 case FC_C_CSTRING: return "FC_C_CSTRING";
146 case FC_C_WSTRING: return "FC_C_WSTRING";
147 case FC_CSTRING: return "FC_CSTRING";
148 case FC_WSTRING: return "FC_WSTRING";
149 case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
150 case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
151 case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
152 case FC_IP: return "FC_IP";
153 case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
154 case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
155 case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
156 case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
157 case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
158 case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
159 case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
160 case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
161 case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
162 case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
163 case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
164 case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
165 case FC_STRING_SIZED: return "FC_STRING_SIZED";
166 case FC_NO_REPEAT: return "FC_NO_REPEAT";
167 case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
168 case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
169 case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
170 case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
171 case FC_PP: return "FC_PP";
172 case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
173 case FC_DEREFERENCE: return "FC_DEREFERENCE";
174 case FC_DIV_2: return "FC_DIV_2";
175 case FC_MULT_2: return "FC_MULT_2";
176 case FC_ADD_1: return "FC_ADD_1";
177 case FC_SUB_1: return "FC_SUB_1";
178 case FC_CALLBACK: return "FC_CALLBACK";
179 case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
180 case FC_END: return "FC_END";
181 case FC_PAD: return "FC_PAD";
182 case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
183 case FC_RANGE: return "FC_RANGE";
184 case FC_INT3264: return "FC_INT3264";
185 case FC_UINT3264: return "FC_UINT3264";
186 default:
187 error("string_of_type: unknown type 0x%02x\n", type);
188 return NULL;
192 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
194 const type_t *t = type;
195 for (;;)
197 if (is_attr(t->attrs, attr))
198 return get_attrp(t->attrs, attr);
199 else if (type_is_alias(t))
200 t = type_alias_get_aliasee_type(t);
201 else return NULL;
205 unsigned char get_basic_fc(const type_t *type)
207 int sign = type_basic_get_sign(type);
208 switch (type_basic_get_type(type))
210 case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
211 case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
212 case TYPE_BASIC_INT32:
213 case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
214 case TYPE_BASIC_INT64: return FC_HYPER;
215 case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
216 case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
217 case TYPE_BASIC_BYTE: return FC_BYTE;
218 case TYPE_BASIC_CHAR: return FC_CHAR;
219 case TYPE_BASIC_WCHAR: return FC_WCHAR;
220 case TYPE_BASIC_HYPER: return FC_HYPER;
221 case TYPE_BASIC_FLOAT: return FC_FLOAT;
222 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
223 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
224 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
226 return 0;
229 static unsigned char get_basic_fc_signed(const type_t *type)
231 switch (type_basic_get_type(type))
233 case TYPE_BASIC_INT8: return FC_SMALL;
234 case TYPE_BASIC_INT16: return FC_SHORT;
235 case TYPE_BASIC_INT32: return FC_LONG;
236 case TYPE_BASIC_INT64: return FC_HYPER;
237 case TYPE_BASIC_INT: return FC_LONG;
238 case TYPE_BASIC_INT3264: return FC_INT3264;
239 case TYPE_BASIC_LONG: return FC_LONG;
240 case TYPE_BASIC_BYTE: return FC_BYTE;
241 case TYPE_BASIC_CHAR: return FC_CHAR;
242 case TYPE_BASIC_WCHAR: return FC_WCHAR;
243 case TYPE_BASIC_HYPER: return FC_HYPER;
244 case TYPE_BASIC_FLOAT: return FC_FLOAT;
245 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
246 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
247 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
249 return 0;
252 static inline unsigned int clamp_align(unsigned int align)
254 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
255 if(align > packing) align = packing;
256 return align;
259 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
261 const type_t *t;
262 int pointer_type;
264 assert(is_ptr(type) || is_array(type));
266 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
267 if (pointer_type)
268 return pointer_type;
270 for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
272 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
273 if (pointer_type)
274 return pointer_type;
277 if (toplevel_param)
278 return FC_RP;
280 if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
281 return pointer_type;
283 return FC_UP;
286 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
287 enum type_context context )
289 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
291 if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
292 (context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
293 pointer_fc = FC_OP;
295 return pointer_fc;
298 static unsigned char get_enum_fc(const type_t *type)
300 assert(type_get_type(type) == TYPE_ENUM);
301 if (is_aliaschain_attr(type, ATTR_V1ENUM))
302 return FC_ENUM32;
303 else
304 return FC_ENUM16;
307 static type_t *get_user_type(const type_t *t, const char **pname)
309 for (;;)
311 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
312 if (ut)
314 if (pname)
315 *pname = t->name;
316 return ut;
319 if (type_is_alias(t))
320 t = type_alias_get_aliasee_type(t);
321 else
322 return NULL;
326 static int is_user_type(const type_t *t)
328 return get_user_type(t, NULL) != NULL;
331 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
333 if (is_user_type(type))
334 return TGT_USER_TYPE;
336 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
337 return TGT_CTXT_HANDLE;
339 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
340 return TGT_STRING;
342 switch (type_get_type(type))
344 case TYPE_BASIC:
345 if (!(flags & TDT_IGNORE_RANGES) &&
346 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
347 return TGT_RANGE;
348 return TGT_BASIC;
349 case TYPE_ENUM:
350 if (!(flags & TDT_IGNORE_RANGES) &&
351 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
352 return TGT_RANGE;
353 return TGT_ENUM;
354 case TYPE_POINTER:
355 if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
356 (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
357 return TGT_IFACE_POINTER;
358 else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
359 return TGT_CTXT_HANDLE_POINTER;
360 else
361 return TGT_POINTER;
362 case TYPE_STRUCT:
363 return TGT_STRUCT;
364 case TYPE_ENCAPSULATED_UNION:
365 case TYPE_UNION:
366 return TGT_UNION;
367 case TYPE_ARRAY:
368 return TGT_ARRAY;
369 case TYPE_FUNCTION:
370 case TYPE_COCLASS:
371 case TYPE_INTERFACE:
372 case TYPE_MODULE:
373 case TYPE_VOID:
374 case TYPE_ALIAS:
375 case TYPE_BITFIELD:
376 break;
378 return TGT_INVALID;
381 static int cant_be_null(const var_t *v)
383 switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
385 case TGT_ARRAY:
386 if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
387 /* fall through */
388 case TGT_POINTER:
389 return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
390 case TGT_CTXT_HANDLE_POINTER:
391 return TRUE;
392 default:
393 return 0;
398 static int get_padding(const var_list_t *fields)
400 unsigned short offset = 0;
401 unsigned int salign = 1;
402 const var_t *f;
404 if (!fields)
405 return 0;
407 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
409 type_t *ft = f->declspec.type;
410 unsigned int align = 0;
411 unsigned int size = type_memsize_and_alignment(ft, &align);
412 align = clamp_align(align);
413 if (align > salign) salign = align;
414 offset = ROUND_SIZE(offset, align);
415 offset += size;
418 return ROUNDING(offset, salign);
421 static unsigned int get_stack_size( const var_t *var, int *by_value )
423 unsigned int stack_size;
424 int by_val;
426 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
428 case TGT_BASIC:
429 case TGT_ENUM:
430 case TGT_RANGE:
431 case TGT_STRUCT:
432 case TGT_UNION:
433 case TGT_USER_TYPE:
434 stack_size = type_memsize( var->declspec.type );
435 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
436 break;
437 default:
438 by_val = 0;
439 break;
441 if (!by_val) stack_size = pointer_size;
442 if (by_value) *by_value = by_val;
443 return ROUND_SIZE( stack_size, pointer_size );
446 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
447 const type_t *type, int is_return )
449 unsigned char flags = 0;
450 int is_out;
452 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
454 if (is_ptr(type) &&
455 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
456 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
457 flags |= HANDLE_PARAM_IS_VIA_PTR;
459 if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
461 is_out = is_attr(attrs, ATTR_OUT);
462 if (is_attr(attrs, ATTR_IN) || !is_out)
464 flags |= HANDLE_PARAM_IS_IN;
465 if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
467 if (is_out) flags |= HANDLE_PARAM_IS_OUT;
469 return flags;
472 static unsigned int get_rpc_flags( const attr_list_t *attrs )
474 unsigned int flags = 0;
476 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
477 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
478 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
479 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
480 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
481 return flags;
484 unsigned char get_struct_fc(const type_t *type)
486 int has_pointer = 0;
487 int has_conformance = 0;
488 int has_variance = 0;
489 var_t *field;
490 var_list_t *fields;
492 fields = type_struct_get_fields(type);
494 if (get_padding(fields))
495 return FC_BOGUS_STRUCT;
497 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
499 type_t *t = field->declspec.type;
500 enum typegen_type typegen_type;
502 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
504 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
506 if (is_string_type(field->attrs, field->declspec.type))
508 if (is_conformant_array(t))
509 has_conformance = 1;
510 has_variance = 1;
511 continue;
514 if (is_array(type_array_get_element_type(field->declspec.type)))
515 return FC_BOGUS_STRUCT;
517 if (type_array_has_conformance(field->declspec.type))
519 has_conformance = 1;
520 if (list_next(fields, &field->entry))
521 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
522 field->name);
524 if (type_array_has_variance(t))
525 has_variance = 1;
527 t = type_array_get_element_type(t);
528 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
531 switch (typegen_type)
533 case TGT_USER_TYPE:
534 case TGT_IFACE_POINTER:
535 return FC_BOGUS_STRUCT;
536 case TGT_BASIC:
537 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
538 return FC_BOGUS_STRUCT;
539 break;
540 case TGT_ENUM:
541 if (get_enum_fc(t) == FC_ENUM16)
542 return FC_BOGUS_STRUCT;
543 break;
544 case TGT_POINTER:
545 case TGT_ARRAY:
546 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
547 return FC_BOGUS_STRUCT;
548 has_pointer = 1;
549 break;
550 case TGT_UNION:
551 return FC_BOGUS_STRUCT;
552 case TGT_STRUCT:
554 unsigned char fc = get_struct_fc(t);
555 switch (fc)
557 case FC_STRUCT:
558 break;
559 case FC_CVSTRUCT:
560 has_conformance = 1;
561 has_variance = 1;
562 has_pointer = 1;
563 break;
565 case FC_CPSTRUCT:
566 has_conformance = 1;
567 if (list_next( fields, &field->entry ))
568 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
569 field->name);
570 has_pointer = 1;
571 break;
573 case FC_CSTRUCT:
574 has_conformance = 1;
575 if (list_next( fields, &field->entry ))
576 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
577 field->name);
578 break;
580 case FC_PSTRUCT:
581 has_pointer = 1;
582 break;
584 default:
585 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
586 /* fallthru - treat it as complex */
588 /* as soon as we see one of these these members, it's bogus... */
589 case FC_BOGUS_STRUCT:
590 return FC_BOGUS_STRUCT;
592 break;
594 case TGT_RANGE:
595 return FC_BOGUS_STRUCT;
596 case TGT_STRING:
597 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
598 case TGT_INVALID:
599 case TGT_CTXT_HANDLE:
600 case TGT_CTXT_HANDLE_POINTER:
601 /* checking after parsing should mean that we don't get here. if we do,
602 * it's a checker bug */
603 assert(0);
607 if( has_variance )
609 if ( has_conformance )
610 return FC_CVSTRUCT;
611 else
612 return FC_BOGUS_STRUCT;
614 if( has_conformance && has_pointer )
615 return FC_CPSTRUCT;
616 if( has_conformance )
617 return FC_CSTRUCT;
618 if( has_pointer )
619 return FC_PSTRUCT;
620 return FC_STRUCT;
623 static unsigned char get_array_fc(const type_t *type)
625 unsigned char fc;
626 const expr_t *size_is;
627 const type_t *elem_type;
629 elem_type = type_array_get_element_type(type);
630 size_is = type_array_get_conformance(type);
632 if (!size_is)
634 unsigned int size = type_memsize(elem_type);
635 if (size * type_array_get_dim(type) > 0xffffuL)
636 fc = FC_LGFARRAY;
637 else
638 fc = FC_SMFARRAY;
640 else
641 fc = FC_CARRAY;
643 if (type_array_has_variance(type))
645 if (fc == FC_SMFARRAY)
646 fc = FC_SMVARRAY;
647 else if (fc == FC_LGFARRAY)
648 fc = FC_LGVARRAY;
649 else if (fc == FC_CARRAY)
650 fc = FC_CVARRAY;
653 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
655 case TGT_USER_TYPE:
656 fc = FC_BOGUS_ARRAY;
657 break;
658 case TGT_BASIC:
659 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
660 pointer_size != 4)
661 fc = FC_BOGUS_ARRAY;
662 break;
663 case TGT_STRUCT:
664 switch (get_struct_fc(elem_type))
666 case FC_BOGUS_STRUCT:
667 fc = FC_BOGUS_ARRAY;
668 break;
670 break;
671 case TGT_ENUM:
672 /* is 16-bit enum - if so, wire size differs from mem size and so
673 * the array cannot be block copied, which means the array is complex */
674 if (get_enum_fc(elem_type) == FC_ENUM16)
675 fc = FC_BOGUS_ARRAY;
676 break;
677 case TGT_UNION:
678 case TGT_IFACE_POINTER:
679 fc = FC_BOGUS_ARRAY;
680 break;
681 case TGT_POINTER:
682 /* ref pointers cannot just be block copied. unique pointers to
683 * interfaces need special treatment. either case means the array is
684 * complex */
685 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
686 fc = FC_BOGUS_ARRAY;
687 break;
688 case TGT_RANGE:
689 fc = FC_BOGUS_ARRAY;
690 break;
691 case TGT_CTXT_HANDLE:
692 case TGT_CTXT_HANDLE_POINTER:
693 case TGT_STRING:
694 case TGT_INVALID:
695 case TGT_ARRAY:
696 /* nothing to do for everything else */
697 break;
700 return fc;
703 static int is_non_complex_struct(const type_t *type)
705 return (type_get_type(type) == TYPE_STRUCT &&
706 get_struct_fc(type) != FC_BOGUS_STRUCT);
709 static int type_has_pointers(const type_t *type)
711 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
713 case TGT_USER_TYPE:
714 return FALSE;
715 case TGT_POINTER:
716 return TRUE;
717 case TGT_ARRAY:
718 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
719 case TGT_STRUCT:
721 var_list_t *fields = type_struct_get_fields(type);
722 const var_t *field;
723 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
725 if (type_has_pointers(field->declspec.type))
726 return TRUE;
728 break;
730 case TGT_UNION:
732 var_list_t *fields;
733 const var_t *field;
734 fields = type_union_get_cases(type);
735 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
737 if (field->declspec.type && type_has_pointers(field->declspec.type))
738 return TRUE;
740 break;
742 case TGT_CTXT_HANDLE:
743 case TGT_CTXT_HANDLE_POINTER:
744 case TGT_STRING:
745 case TGT_IFACE_POINTER:
746 case TGT_BASIC:
747 case TGT_ENUM:
748 case TGT_RANGE:
749 case TGT_INVALID:
750 break;
753 return FALSE;
756 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
757 int toplevel_param)
759 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
761 case TGT_USER_TYPE:
762 return FALSE;
763 case TGT_POINTER:
764 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
765 return TRUE;
766 else
767 return FALSE;
768 case TGT_ARRAY:
769 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
770 return TRUE;
771 else
772 return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
773 case TGT_STRUCT:
775 var_list_t *fields = type_struct_get_fields(type);
776 const var_t *field;
777 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
779 if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
780 return TRUE;
782 break;
784 case TGT_UNION:
786 var_list_t *fields;
787 const var_t *field;
788 fields = type_union_get_cases(type);
789 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
791 if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
792 return TRUE;
794 break;
796 case TGT_CTXT_HANDLE:
797 case TGT_CTXT_HANDLE_POINTER:
798 case TGT_STRING:
799 case TGT_IFACE_POINTER:
800 case TGT_BASIC:
801 case TGT_ENUM:
802 case TGT_RANGE:
803 case TGT_INVALID:
804 break;
807 return FALSE;
810 static unsigned short user_type_offset(const char *name)
812 user_type_t *ut;
813 unsigned short off = 0;
814 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
816 if (strcmp(name, ut->name) == 0)
817 return off;
818 ++off;
820 error("user_type_offset: couldn't find type (%s)\n", name);
821 return 0;
824 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
826 type->typestring_offset = offset;
827 if (file) type->tfswrite = FALSE;
830 static void guard_rec(type_t *type)
832 /* types that contain references to themselves (like a linked list),
833 need to be shielded from infinite recursion when writing embedded
834 types */
835 if (type->typestring_offset)
836 type->tfswrite = FALSE;
837 else
838 type->typestring_offset = 1;
841 static int is_embedded_complex(const type_t *type)
843 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
845 case TGT_USER_TYPE:
846 case TGT_STRUCT:
847 case TGT_UNION:
848 case TGT_ARRAY:
849 case TGT_IFACE_POINTER:
850 return TRUE;
851 default:
852 return FALSE;
856 static const char *get_context_handle_type_name(const type_t *type)
858 const type_t *t;
859 for (t = type;
860 is_ptr(t) || type_is_alias(t);
861 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
862 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
863 return t->name;
864 assert(0);
865 return NULL;
868 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
869 do { \
870 if (file) \
871 fprintf(file, "/* %2u */\n", typestring_offset); \
872 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
874 while (0)
876 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
877 static void print_file(FILE *file, int indent, const char *format, ...)
879 va_list va;
880 va_start(va, format);
881 print(file, indent, format, va);
882 va_end(va);
885 void print(FILE *file, int indent, const char *format, va_list va)
887 if (file)
889 if (format[0] != '\n')
890 while (0 < indent--)
891 fprintf(file, " ");
892 vfprintf(file, format, va);
897 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
899 if (decl_indirect(t))
901 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
902 local_var_prefix, n, local_var_prefix, n);
903 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
905 else if (is_ptr(t) || is_array(t))
906 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
909 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
911 const var_t *var = type_function_get_retval(func->declspec.type);
913 if (!is_void(var->declspec.type))
914 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
916 if (!type_function_get_args(func->declspec.type))
917 return;
919 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
920 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
922 fprintf(file, "\n");
925 static void write_formatdesc(FILE *f, int indent, const char *str)
927 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
928 print_file(f, indent, "{\n");
929 print_file(f, indent + 1, "short Pad;\n");
930 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
931 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
932 print_file(f, indent, "\n");
935 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
937 clear_all_offsets();
939 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
940 get_size_typeformatstring(stmts, pred));
942 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
943 get_size_procformatstring(stmts, pred));
945 fprintf(f, "\n");
946 write_formatdesc(f, indent, "TYPE");
947 write_formatdesc(f, indent, "PROC");
948 fprintf(f, "\n");
949 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
950 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
951 print_file(f, indent, "\n");
954 int decl_indirect(const type_t *t)
956 if (is_user_type(t))
957 return TRUE;
958 return (type_get_type(t) != TYPE_BASIC &&
959 type_get_type(t) != TYPE_ENUM &&
960 type_get_type(t) != TYPE_POINTER &&
961 type_get_type(t) != TYPE_ARRAY);
964 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
965 unsigned int *stack_size, unsigned int *typestring_offset )
967 unsigned int alignment, server_size = 0, buffer_size = 0;
968 unsigned char fc = 0;
969 int is_byval;
970 int is_in = is_attr(var->attrs, ATTR_IN);
971 int is_out = is_attr(var->attrs, ATTR_OUT);
973 if (is_return) is_out = TRUE;
974 else if (!is_in && !is_out) is_in = TRUE;
976 *flags = 0;
977 *stack_size = get_stack_size( var, &is_byval );
978 *typestring_offset = var->typestring_offset;
980 if (is_in) *flags |= IsIn;
981 if (is_out) *flags |= IsOut;
982 if (is_return) *flags |= IsReturn;
984 if (!is_string_type( var->attrs, var->declspec.type ))
985 buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
987 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
989 case TGT_BASIC:
990 *flags |= IsBasetype;
991 fc = get_basic_fc_signed( var->declspec.type );
992 if (fc == FC_BIND_PRIMITIVE)
994 buffer_size = 4; /* actually 0 but avoids setting MustSize */
995 fc = FC_LONG;
997 break;
998 case TGT_ENUM:
999 *flags |= IsBasetype;
1000 fc = get_enum_fc( var->declspec.type );
1001 break;
1002 case TGT_RANGE:
1003 *flags |= IsByValue;
1004 break;
1005 case TGT_STRUCT:
1006 case TGT_UNION:
1007 case TGT_USER_TYPE:
1008 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1009 break;
1010 case TGT_IFACE_POINTER:
1011 *flags |= MustFree;
1012 break;
1013 case TGT_ARRAY:
1014 *flags |= MustFree;
1015 if (type_array_is_decl_as_ptr(var->declspec.type)
1016 && type_array_get_ptr_tfsoff(var->declspec.type)
1017 && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
1019 *typestring_offset = var->declspec.type->typestring_offset;
1020 *flags |= IsSimpleRef;
1022 break;
1023 case TGT_STRING:
1024 *flags |= MustFree;
1025 if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1027 /* skip over pointer description straight to string description */
1028 if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
1029 else *typestring_offset += 2;
1030 *flags |= IsSimpleRef;
1032 break;
1033 case TGT_CTXT_HANDLE_POINTER:
1034 *flags |= IsSimpleRef;
1035 *typestring_offset += 4;
1036 /* fall through */
1037 case TGT_CTXT_HANDLE:
1038 buffer_size = 20;
1039 break;
1040 case TGT_POINTER:
1041 if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1043 const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
1045 if (!is_string_type( var->attrs, ref ))
1046 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1048 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1050 case TGT_BASIC:
1051 *flags |= IsSimpleRef | IsBasetype;
1052 fc = get_basic_fc( ref );
1053 if (!is_in && is_out) server_size = pointer_size;
1054 break;
1055 case TGT_ENUM:
1056 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1058 *flags |= IsSimpleRef | IsBasetype;
1059 if (!is_in && is_out) server_size = pointer_size;
1061 else
1063 server_size = pointer_size;
1065 break;
1066 case TGT_UNION:
1067 case TGT_USER_TYPE:
1068 case TGT_RANGE:
1069 *flags |= MustFree | IsSimpleRef;
1070 *typestring_offset = ref->typestring_offset;
1071 if (!is_in && is_out) server_size = type_memsize( ref );
1072 break;
1073 case TGT_ARRAY:
1074 *flags |= MustFree;
1075 if (!type_array_is_decl_as_ptr(ref))
1077 *flags |= IsSimpleRef;
1078 *typestring_offset = ref->typestring_offset;
1080 if (!is_in && is_out) server_size = type_memsize( ref );
1081 break;
1082 case TGT_STRING:
1083 case TGT_POINTER:
1084 case TGT_CTXT_HANDLE:
1085 case TGT_CTXT_HANDLE_POINTER:
1086 *flags |= MustFree;
1087 server_size = pointer_size;
1088 break;
1089 case TGT_IFACE_POINTER:
1090 *flags |= MustFree;
1091 if (is_in && is_out) server_size = pointer_size;
1092 break;
1093 case TGT_STRUCT:
1094 *flags |= IsSimpleRef | MustFree;
1095 *typestring_offset = ref->typestring_offset;
1096 switch (get_struct_fc(ref))
1098 case FC_STRUCT:
1099 case FC_PSTRUCT:
1100 case FC_BOGUS_STRUCT:
1101 if (!is_in && is_out) server_size = type_memsize( ref );
1102 break;
1103 default:
1104 break;
1106 break;
1107 case TGT_INVALID:
1108 assert(0);
1111 else /* not ref pointer */
1113 *flags |= MustFree;
1115 break;
1116 case TGT_INVALID:
1117 assert(0);
1120 if (!buffer_size) *flags |= MustSize;
1122 if (server_size)
1124 server_size = (server_size + 7) / 8;
1125 if (server_size < 8) *flags |= server_size << 13;
1127 return fc;
1130 static unsigned char get_func_oi2_flags( const var_t *func )
1132 const var_t *var;
1133 var_list_t *args = type_function_get_args( func->declspec.type );
1134 var_t *retval = type_function_get_retval( func->declspec.type );
1135 unsigned char oi2_flags = 0x40; /* HasExtensions */
1136 unsigned short flags;
1137 unsigned int stack_size, typestring_offset;
1139 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1141 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1142 if (flags & MustSize)
1144 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1145 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1149 if (!is_void( retval->declspec.type ))
1151 oi2_flags |= 0x04; /* HasRet */
1152 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1153 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1155 return oi2_flags;
1158 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1159 int is_return, unsigned int *stack_offset)
1161 char buffer[128];
1162 unsigned int stack_size, typestring_offset;
1163 unsigned short flags;
1164 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1166 strcpy( buffer, "/* flags:" );
1167 if (flags & MustSize) strcat( buffer, " must size," );
1168 if (flags & MustFree) strcat( buffer, " must free," );
1169 if (flags & IsPipe) strcat( buffer, " pipe," );
1170 if (flags & IsIn) strcat( buffer, " in," );
1171 if (flags & IsOut) strcat( buffer, " out," );
1172 if (flags & IsReturn) strcat( buffer, " return," );
1173 if (flags & IsBasetype) strcat( buffer, " base type," );
1174 if (flags & IsByValue) strcat( buffer, " by value," );
1175 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1176 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1177 strcpy( buffer + strlen( buffer ) - 1, " */" );
1178 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1179 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1180 *stack_offset, *stack_offset );
1181 if (flags & IsBasetype)
1183 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1184 print_file( file, indent, "0x0,\n" );
1186 else
1187 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1188 typestring_offset, typestring_offset );
1189 *stack_offset += max( stack_size, pointer_size );
1190 return 6;
1193 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1194 int is_return, int is_interpreted)
1196 unsigned int size;
1198 int is_in = is_attr(var->attrs, ATTR_IN);
1199 int is_out = is_attr(var->attrs, ATTR_OUT);
1201 if (!is_in && !is_out) is_in = TRUE;
1203 if (type_get_type(var->declspec.type) == TYPE_BASIC ||
1204 type_get_type(var->declspec.type) == TYPE_ENUM)
1206 unsigned char fc;
1208 if (is_return)
1209 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1210 else
1211 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1213 if (type_get_type(var->declspec.type) == TYPE_ENUM)
1215 fc = get_enum_fc(var->declspec.type);
1217 else
1219 fc = get_basic_fc_signed(var->declspec.type);
1221 if (fc == FC_BIND_PRIMITIVE)
1222 fc = FC_IGNORE;
1225 print_file(file, indent, "0x%02x, /* %s */\n",
1226 fc, string_of_type(fc));
1227 size = 2; /* includes param type prefix */
1229 else
1231 unsigned short offset = var->typestring_offset;
1233 if (!is_interpreted && is_array(var->declspec.type)
1234 && type_array_is_decl_as_ptr(var->declspec.type)
1235 && type_array_get_ptr_tfsoff(var->declspec.type))
1236 offset = var->declspec.type->typestring_offset;
1238 if (is_return)
1239 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1240 else if (is_in && is_out)
1241 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1242 else if (is_out)
1243 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1244 else
1245 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1247 size = get_stack_size( var, NULL );
1248 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1249 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1250 size = 4; /* includes param type prefix */
1252 return size;
1255 int is_interpreted_func( const type_t *iface, const var_t *func )
1257 const char *str;
1258 const var_t *var;
1259 const var_list_t *args = type_function_get_args( func->declspec.type );
1260 const type_t *ret_type = type_function_get_rettype( func->declspec.type );
1262 if (type_get_type( ret_type ) == TYPE_BASIC)
1264 switch (type_basic_get_type( ret_type ))
1266 case TYPE_BASIC_INT64:
1267 case TYPE_BASIC_HYPER:
1268 /* return value must fit in a long_ptr */
1269 if (pointer_size < 8) return 0;
1270 break;
1271 case TYPE_BASIC_FLOAT:
1272 case TYPE_BASIC_DOUBLE:
1273 /* floating point values can't be returned */
1274 return 0;
1275 default:
1276 break;
1279 if (get_stub_mode() != MODE_Oif && args)
1281 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1282 switch (type_get_type( var->declspec.type ))
1284 case TYPE_BASIC:
1285 switch (type_basic_get_type( var->declspec.type ))
1287 /* floating point arguments are not supported in Oi mode */
1288 case TYPE_BASIC_FLOAT: return 0;
1289 case TYPE_BASIC_DOUBLE: return 0;
1290 default: break;
1292 break;
1293 /* unions passed by value are not supported in Oi mode */
1294 case TYPE_UNION: return 0;
1295 case TYPE_ENCAPSULATED_UNION: return 0;
1296 default: break;
1300 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1301 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1302 return (get_stub_mode() != MODE_Os);
1305 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1306 const var_t *func, unsigned int *offset,
1307 unsigned short num_proc )
1309 var_t *var;
1310 var_list_t *args = type_function_get_args( func->declspec.type );
1311 unsigned char explicit_fc, implicit_fc;
1312 unsigned char handle_flags;
1313 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1314 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1315 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1316 unsigned int nb_args = 0;
1317 unsigned int stack_size = 0;
1318 unsigned short param_num = 0;
1319 unsigned short handle_stack_offset = 0;
1320 unsigned short handle_param_num = 0;
1322 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1323 if (is_object( iface ))
1325 oi_flags |= Oi_OBJECT_PROC;
1326 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1327 stack_size += pointer_size;
1330 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1332 if (var == handle_var)
1334 handle_stack_offset = stack_size;
1335 handle_param_num = param_num;
1337 stack_size += get_stack_size( var, NULL );
1338 param_num++;
1339 nb_args++;
1341 if (!is_void( type_function_get_rettype( func->declspec.type )))
1343 stack_size += pointer_size;
1344 nb_args++;
1347 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1348 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1349 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1350 print_file( file, indent, "0x%02x,\n", oi_flags );
1351 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1352 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1353 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1354 *offset += 10;
1356 if (!implicit_fc)
1358 switch (explicit_fc)
1360 case FC_BIND_PRIMITIVE:
1361 handle_flags = 0;
1362 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1363 print_file( file, indent, "0x%02x,\n", handle_flags );
1364 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1365 handle_stack_offset, handle_stack_offset );
1366 *offset += 4;
1367 break;
1368 case FC_BIND_GENERIC:
1369 handle_flags = type_memsize( handle_var->declspec.type );
1370 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1371 print_file( file, indent, "0x%02x,\n", handle_flags );
1372 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1373 handle_stack_offset, handle_stack_offset );
1374 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
1375 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1376 *offset += 6;
1377 break;
1378 case FC_BIND_CONTEXT:
1379 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
1380 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1381 print_file( file, indent, "0x%02x,\n", handle_flags );
1382 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1383 handle_stack_offset, handle_stack_offset );
1384 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
1385 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1386 *offset += 6;
1387 break;
1391 if (get_stub_mode() == MODE_Oif)
1393 unsigned char oi2_flags = get_func_oi2_flags( func );
1394 unsigned char ext_flags = 0;
1395 unsigned int size;
1397 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1398 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1399 if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
1401 size = get_function_buffer_size( func, PASS_IN );
1402 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1403 size = get_function_buffer_size( func, PASS_OUT );
1404 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1405 print_file( file, indent, "0x%02x,\n", oi2_flags );
1406 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1407 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1408 print_file( file, indent, "0x%02x,\n", ext_flags );
1409 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1410 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1411 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1412 *offset += 14;
1413 if (pointer_size == 8)
1415 unsigned short pos = 0, fpu_mask = 0;
1417 if (is_object( iface )) pos += 2;
1418 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1420 if (type_get_type( var->declspec.type ) == TYPE_BASIC)
1422 switch (type_basic_get_type( var->declspec.type ))
1424 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1425 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1426 default: break;
1429 pos += 2;
1430 if (pos >= 16) break;
1432 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1433 *offset += 2;
1438 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1439 const var_t *func, unsigned int *offset,
1440 unsigned short num_proc )
1442 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1443 int is_interpreted = is_interpreted_func( iface, func );
1444 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1445 var_t *retval = type_function_get_retval( func->declspec.type );
1447 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1449 /* emit argument data */
1450 if (type_function_get_args(func->declspec.type))
1452 const var_t *var;
1453 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1455 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1456 if (is_new_style)
1457 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1458 else
1459 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1463 /* emit return value data */
1464 if (is_void(retval->declspec.type))
1466 if (!is_new_style)
1468 print_file(file, 0, "/* %u (void) */\n", *offset);
1469 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1470 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1471 *offset += 2;
1474 else
1476 print_file( file, 0, "/* %u (return value) */\n", *offset );
1477 if (is_new_style)
1478 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1479 else
1480 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1484 static void for_each_iface(const statement_list_t *stmts,
1485 void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
1486 type_pred_t pred, FILE *file, int indent, unsigned int *offset)
1488 const statement_t *stmt;
1489 type_t *iface;
1491 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1493 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
1494 continue;
1495 iface = stmt->u.type;
1496 if (!pred(iface)) continue;
1497 proc(iface, file, indent, offset);
1498 if (type_iface_get_async_iface(iface))
1499 proc(type_iface_get_async_iface(iface), file, indent, offset);
1503 static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
1505 const statement_t *stmt;
1506 const type_t *parent = type_iface_get_inherit( iface );
1507 int count = parent ? count_methods( parent ) : 0;
1509 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1511 var_t *func = stmt->u.var;
1512 if (is_local(func->attrs)) continue;
1513 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1517 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1519 int indent = 0;
1520 unsigned int offset = 0;
1522 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1523 print_file(file, indent, "{\n");
1524 indent++;
1525 print_file(file, indent, "0,\n");
1526 print_file(file, indent, "{\n");
1527 indent++;
1529 for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
1531 print_file(file, indent, "0x0\n");
1532 indent--;
1533 print_file(file, indent, "}\n");
1534 indent--;
1535 print_file(file, indent, "};\n");
1536 print_file(file, indent, "\n");
1539 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1541 const statement_t *stmt;
1542 int indent = 0;
1544 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1545 iface->name );
1546 print_file( file, indent, "{\n" );
1547 indent++;
1548 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1550 var_t *func = stmt->u.var;
1551 if (is_local( func->attrs )) continue;
1552 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1554 indent--;
1555 print_file( file, indent, "};\n\n" );
1558 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1560 unsigned char fc;
1562 if (type_get_type(type) == TYPE_BASIC)
1563 fc = get_basic_fc_signed(type);
1564 else if (type_get_type(type) == TYPE_ENUM)
1565 fc = get_enum_fc(type);
1566 else
1567 return 0;
1569 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1570 *typestring_offset += 1;
1571 return 1;
1574 /* write conformance / variance descriptor */
1575 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1576 unsigned int baseoff, const type_t *type,
1577 const expr_t *expr)
1579 unsigned char operator_type = 0;
1580 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1581 const char *conftype_string = "field";
1582 const expr_t *subexpr;
1583 const type_t *iface = NULL;
1584 const char *name;
1586 if (!expr)
1588 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1589 return 4;
1592 if (expr->is_const)
1594 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1595 error("write_conf_or_var_desc: constant value %d is greater than "
1596 "the maximum constant size of %d\n", expr->cval,
1597 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1599 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1600 FC_CONSTANT_CONFORMANCE, expr->cval);
1601 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1602 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1604 return 4;
1607 if (!cont_type) /* top-level conformance */
1609 conftype = FC_TOP_LEVEL_CONFORMANCE;
1610 conftype_string = "parameter";
1611 cont_type = current_func->declspec.type;
1612 name = current_func->name;
1613 iface = current_iface;
1615 else
1617 name = cont_type->name;
1618 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1620 conftype = FC_POINTER_CONFORMANCE;
1621 conftype_string = "field pointer";
1625 subexpr = expr;
1626 switch (subexpr->type)
1628 case EXPR_PPTR:
1629 subexpr = subexpr->ref;
1630 operator_type = FC_DEREFERENCE;
1631 break;
1632 case EXPR_DIV:
1633 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1635 subexpr = subexpr->ref;
1636 operator_type = FC_DIV_2;
1638 break;
1639 case EXPR_MUL:
1640 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1642 subexpr = subexpr->ref;
1643 operator_type = FC_MULT_2;
1645 break;
1646 case EXPR_SUB:
1647 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1649 subexpr = subexpr->ref;
1650 operator_type = FC_SUB_1;
1652 break;
1653 case EXPR_ADD:
1654 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1656 subexpr = subexpr->ref;
1657 operator_type = FC_ADD_1;
1659 break;
1660 default:
1661 break;
1664 if (subexpr->type == EXPR_IDENTIFIER)
1666 const type_t *correlation_variable = NULL;
1667 unsigned char param_type = 0;
1668 unsigned int offset = 0;
1669 const var_t *var;
1670 struct expr_loc expr_loc;
1672 if (type_get_type(cont_type) == TYPE_FUNCTION)
1674 var_list_t *args = type_function_get_args( cont_type );
1676 if (is_object( iface )) offset += pointer_size;
1677 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1679 if (var->name && !strcmp(var->name, subexpr->u.sval))
1681 expr_loc.v = var;
1682 correlation_variable = var->declspec.type;
1683 break;
1685 offset += get_stack_size( var, NULL );
1688 else
1690 var_list_t *fields = type_struct_get_fields( cont_type );
1692 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1694 unsigned int size = field_memsize( var->declspec.type, &offset );
1695 if (var->name && !strcmp(var->name, subexpr->u.sval))
1697 expr_loc.v = var;
1698 correlation_variable = var->declspec.type;
1699 break;
1701 offset += size;
1705 if (!correlation_variable)
1706 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1707 expr_loc.attr = NULL;
1708 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1710 offset -= baseoff;
1712 if (type_get_type(correlation_variable) == TYPE_BASIC)
1714 switch (get_basic_fc(correlation_variable))
1716 case FC_CHAR:
1717 case FC_SMALL:
1718 param_type = FC_SMALL;
1719 break;
1720 case FC_BYTE:
1721 case FC_USMALL:
1722 param_type = FC_USMALL;
1723 break;
1724 case FC_WCHAR:
1725 case FC_SHORT:
1726 param_type = FC_SHORT;
1727 break;
1728 case FC_USHORT:
1729 param_type = FC_USHORT;
1730 break;
1731 case FC_LONG:
1732 param_type = FC_LONG;
1733 break;
1734 case FC_ULONG:
1735 param_type = FC_ULONG;
1736 break;
1737 default:
1738 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1739 get_basic_fc(correlation_variable));
1742 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1744 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1745 param_type = FC_LONG;
1746 else
1747 param_type = FC_SHORT;
1749 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1751 if (pointer_size == 8)
1752 param_type = FC_HYPER;
1753 else
1754 param_type = FC_LONG;
1756 else
1758 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1759 subexpr->u.sval);
1760 return 0;
1763 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1764 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1765 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1766 operator_type ? string_of_type(operator_type) : "no operators");
1767 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1768 (unsigned short)offset, offset);
1770 else if (!iface || is_interpreted_func( iface, current_func ))
1772 unsigned int callback_offset = 0;
1773 struct expr_eval_routine *eval;
1774 int found = 0;
1776 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1778 if (eval->cont_type == cont_type ||
1779 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1780 eval->iface == iface &&
1781 eval->name && name && !strcmp(eval->name, name) &&
1782 !compare_expr(eval->expr, expr)))
1784 found = 1;
1785 break;
1787 callback_offset++;
1790 if (!found)
1792 eval = xmalloc (sizeof(*eval));
1793 eval->iface = iface;
1794 eval->cont_type = cont_type;
1795 eval->name = xstrdup( name );
1796 eval->baseoff = baseoff;
1797 eval->expr = expr;
1798 list_add_tail (&expr_eval_routines, &eval->entry);
1801 if (callback_offset > USHRT_MAX)
1802 error("Maximum number of callback routines reached\n");
1804 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1805 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1806 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1808 else /* output a dummy corr desc that isn't used */
1810 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1811 print_file(file, 2, "0x0,\n" );
1812 print_file(file, 2, "NdrFcShort(0x0),\n" );
1814 return 4;
1817 /* return size and start offset of a data field based on current offset */
1818 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1820 unsigned int align = 0;
1821 unsigned int size = type_memsize_and_alignment( type, &align );
1823 *offset = ROUND_SIZE( *offset, align );
1824 return size;
1827 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1829 unsigned int size = 0;
1830 unsigned int max_align;
1831 const var_t *v;
1833 if (!fields) return 0;
1834 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1836 unsigned int falign = 0;
1837 unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
1838 if (*align < falign) *align = falign;
1839 falign = clamp_align(falign);
1840 size = ROUND_SIZE(size, falign);
1841 size += fsize;
1844 max_align = clamp_align(*align);
1845 size = ROUND_SIZE(size, max_align);
1847 return size;
1850 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1852 unsigned int size, maxs = 0;
1853 unsigned int align = *pmaxa;
1854 const var_t *v;
1856 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1858 /* we could have an empty default field with NULL type */
1859 if (v->declspec.type)
1861 size = type_memsize_and_alignment(v->declspec.type, &align);
1862 if (maxs < size) maxs = size;
1863 if (*pmaxa < align) *pmaxa = align;
1867 return maxs;
1870 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1872 unsigned int size = 0;
1874 switch (type_get_type(t))
1876 case TYPE_BASIC:
1877 switch (get_basic_fc(t))
1879 case FC_BYTE:
1880 case FC_CHAR:
1881 case FC_USMALL:
1882 case FC_SMALL:
1883 size = 1;
1884 if (size > *align) *align = size;
1885 break;
1886 case FC_WCHAR:
1887 case FC_USHORT:
1888 case FC_SHORT:
1889 size = 2;
1890 if (size > *align) *align = size;
1891 break;
1892 case FC_ULONG:
1893 case FC_LONG:
1894 case FC_ERROR_STATUS_T:
1895 case FC_FLOAT:
1896 size = 4;
1897 if (size > *align) *align = size;
1898 break;
1899 case FC_HYPER:
1900 case FC_DOUBLE:
1901 size = 8;
1902 if (size > *align) *align = size;
1903 break;
1904 case FC_INT3264:
1905 case FC_UINT3264:
1906 case FC_BIND_PRIMITIVE:
1907 assert( pointer_size );
1908 size = pointer_size;
1909 if (size > *align) *align = size;
1910 break;
1911 default:
1912 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1913 size = 0;
1915 break;
1916 case TYPE_ENUM:
1917 switch (get_enum_fc(t))
1919 case FC_ENUM16:
1920 case FC_ENUM32:
1921 size = 4;
1922 if (size > *align) *align = size;
1923 break;
1924 default:
1925 error("type_memsize: Unknown enum type\n");
1926 size = 0;
1928 break;
1929 case TYPE_STRUCT:
1930 size = fields_memsize(type_struct_get_fields(t), align);
1931 break;
1932 case TYPE_ENCAPSULATED_UNION:
1933 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1934 break;
1935 case TYPE_UNION:
1936 size = union_memsize(type_union_get_cases(t), align);
1937 break;
1938 case TYPE_POINTER:
1939 case TYPE_INTERFACE:
1940 assert( pointer_size );
1941 size = pointer_size;
1942 if (size > *align) *align = size;
1943 break;
1944 case TYPE_ARRAY:
1945 if (!type_array_is_decl_as_ptr(t))
1947 if (is_conformant_array(t))
1949 type_memsize_and_alignment(type_array_get_element_type(t), align);
1950 size = 0;
1952 else
1953 size = type_array_get_dim(t) *
1954 type_memsize_and_alignment(type_array_get_element_type(t), align);
1956 else /* declared as a pointer */
1958 assert( pointer_size );
1959 size = pointer_size;
1960 if (size > *align) *align = size;
1962 break;
1963 case TYPE_ALIAS:
1964 case TYPE_VOID:
1965 case TYPE_COCLASS:
1966 case TYPE_MODULE:
1967 case TYPE_FUNCTION:
1968 case TYPE_BITFIELD:
1969 /* these types should not be encountered here due to language
1970 * restrictions (interface, void, coclass, module), logical
1971 * restrictions (alias - due to type_get_type call above) or
1972 * checking restrictions (function, bitfield). */
1973 assert(0);
1976 return size;
1979 unsigned int type_memsize(const type_t *t)
1981 unsigned int align = 0;
1982 return type_memsize_and_alignment( t, &align );
1985 static unsigned int type_buffer_alignment(const type_t *t)
1987 const var_list_t *fields;
1988 const var_t *var;
1989 unsigned int max = 0, align;
1991 switch (type_get_type(t))
1993 case TYPE_BASIC:
1994 switch (get_basic_fc(t))
1996 case FC_BYTE:
1997 case FC_CHAR:
1998 case FC_USMALL:
1999 case FC_SMALL:
2000 return 1;
2001 case FC_WCHAR:
2002 case FC_USHORT:
2003 case FC_SHORT:
2004 return 2;
2005 case FC_ULONG:
2006 case FC_LONG:
2007 case FC_ERROR_STATUS_T:
2008 case FC_FLOAT:
2009 case FC_INT3264:
2010 case FC_UINT3264:
2011 return 4;
2012 case FC_HYPER:
2013 case FC_DOUBLE:
2014 return 8;
2015 default:
2016 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
2018 break;
2019 case TYPE_ENUM:
2020 switch (get_enum_fc(t))
2022 case FC_ENUM16:
2023 return 2;
2024 case FC_ENUM32:
2025 return 4;
2026 default:
2027 error("type_buffer_alignment: Unknown enum type\n");
2029 break;
2030 case TYPE_STRUCT:
2031 if (!(fields = type_struct_get_fields(t))) break;
2032 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2034 if (!var->declspec.type) continue;
2035 align = type_buffer_alignment( var->declspec.type );
2036 if (max < align) max = align;
2038 break;
2039 case TYPE_ENCAPSULATED_UNION:
2040 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2041 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2043 if (!var->declspec.type) continue;
2044 align = type_buffer_alignment( var->declspec.type );
2045 if (max < align) max = align;
2047 break;
2048 case TYPE_UNION:
2049 if (!(fields = type_union_get_cases(t))) break;
2050 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2052 if (!var->declspec.type) continue;
2053 align = type_buffer_alignment( var->declspec.type );
2054 if (max < align) max = align;
2056 break;
2057 case TYPE_ARRAY:
2058 if (!type_array_is_decl_as_ptr(t))
2059 return type_buffer_alignment( type_array_get_element_type(t) );
2060 /* else fall through */
2061 case TYPE_POINTER:
2062 return 4;
2063 case TYPE_INTERFACE:
2064 case TYPE_ALIAS:
2065 case TYPE_VOID:
2066 case TYPE_COCLASS:
2067 case TYPE_MODULE:
2068 case TYPE_FUNCTION:
2069 case TYPE_BITFIELD:
2070 /* these types should not be encountered here due to language
2071 * restrictions (interface, void, coclass, module), logical
2072 * restrictions (alias - due to type_get_type call above) or
2073 * checking restrictions (function, bitfield). */
2074 assert(0);
2076 return max;
2079 int is_full_pointer_function(const var_t *func)
2081 const var_t *var;
2082 if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
2083 return TRUE;
2084 if (!type_function_get_args(func->declspec.type))
2085 return FALSE;
2086 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
2087 if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
2088 return TRUE;
2089 return FALSE;
2092 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2094 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2095 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2096 fprintf(file, "\n");
2099 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2101 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2102 fprintf(file, "\n");
2105 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2106 const type_t *type,
2107 enum type_context context,
2108 unsigned int offset,
2109 unsigned int *typeformat_offset)
2111 unsigned int start_offset = *typeformat_offset;
2112 short reloff = offset - (*typeformat_offset + 2);
2113 int in_attr, out_attr;
2114 int pointer_type;
2115 unsigned char flags = 0;
2117 pointer_type = get_pointer_fc_context(type, attrs, context);
2119 in_attr = is_attr(attrs, ATTR_IN);
2120 out_attr = is_attr(attrs, ATTR_OUT);
2121 if (!in_attr && !out_attr) in_attr = 1;
2123 if (!is_interpreted_func(current_iface, current_func))
2125 if (out_attr && !in_attr && pointer_type == FC_RP)
2126 flags |= FC_ALLOCED_ON_STACK;
2128 else if (get_stub_mode() == MODE_Oif)
2130 if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
2132 switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
2134 case TGT_STRING:
2135 case TGT_POINTER:
2136 case TGT_CTXT_HANDLE:
2137 case TGT_CTXT_HANDLE_POINTER:
2138 case TGT_ARRAY:
2139 flags |= FC_ALLOCED_ON_STACK;
2140 break;
2141 case TGT_IFACE_POINTER:
2142 if (in_attr && out_attr)
2143 flags |= FC_ALLOCED_ON_STACK;
2144 break;
2145 default:
2146 break;
2151 if (is_ptr(type))
2153 type_t *ref = type_pointer_get_ref_type(type);
2154 if(is_declptr(ref) && !is_user_type(ref))
2155 flags |= FC_POINTER_DEREF;
2158 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2159 pointer_type,
2160 flags,
2161 string_of_type(pointer_type));
2162 if (file)
2164 if (flags & FC_ALLOCED_ON_STACK)
2165 fprintf(file, " [allocated_on_stack]");
2166 if (flags & FC_POINTER_DEREF)
2167 fprintf(file, " [pointer_deref]");
2168 fprintf(file, " */\n");
2171 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2172 *typeformat_offset += 4;
2174 return start_offset;
2177 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2178 const type_t *type, enum type_context context)
2180 unsigned char fc;
2181 unsigned char pointer_fc;
2182 const type_t *ref;
2183 int in_attr = is_attr(attrs, ATTR_IN);
2184 int out_attr = is_attr(attrs, ATTR_OUT);
2185 unsigned char flags = FC_SIMPLE_POINTER;
2187 /* for historical reasons, write_simple_pointer also handled string types,
2188 * but no longer does. catch bad uses of the function with this check */
2189 if (is_string_type(attrs, type))
2190 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2192 pointer_fc = get_pointer_fc_context(type, attrs, context);
2194 ref = type_pointer_get_ref_type(type);
2195 if (type_get_type(ref) == TYPE_ENUM)
2196 fc = get_enum_fc(ref);
2197 else
2198 fc = get_basic_fc(ref);
2200 if (!is_interpreted_func(current_iface, current_func))
2202 if (out_attr && !in_attr && pointer_fc == FC_RP)
2203 flags |= FC_ALLOCED_ON_STACK;
2205 else if (get_stub_mode() == MODE_Oif)
2207 if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
2208 flags |= FC_ALLOCED_ON_STACK;
2211 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2212 pointer_fc, flags, string_of_type(pointer_fc),
2213 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2214 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2215 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2216 return 4;
2219 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2221 const decl_spec_t ds = {.type = t};
2222 print_file(file, 0, "/* %u (", tfsoff);
2223 write_type_decl(file, &ds, NULL);
2224 print_file(file, 0, ") */\n");
2227 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2228 type_t *type, unsigned int ref_offset,
2229 enum type_context context,
2230 unsigned int *typestring_offset)
2232 unsigned int offset = *typestring_offset;
2233 type_t *ref = type_pointer_get_ref_type(type);
2235 print_start_tfs_comment(file, type, offset);
2236 update_tfsoff(type, offset, file);
2238 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2240 case TGT_BASIC:
2241 case TGT_ENUM:
2242 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2243 break;
2244 default:
2245 if (ref_offset)
2246 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2247 break;
2250 return offset;
2253 static int processed(const type_t *type)
2255 return type->typestring_offset && !type->tfswrite;
2258 static int user_type_has_variable_size(const type_t *t)
2260 if (is_ptr(t))
2261 return TRUE;
2262 else if (type_get_type(t) == TYPE_STRUCT)
2264 switch (get_struct_fc(t))
2266 case FC_PSTRUCT:
2267 case FC_CSTRUCT:
2268 case FC_CPSTRUCT:
2269 case FC_CVSTRUCT:
2270 return TRUE;
2273 /* Note: Since this only applies to user types, we can't have a conformant
2274 array here, and strings should get filed under pointer in this case. */
2275 return FALSE;
2278 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2280 unsigned int start, absoff, flags;
2281 const char *name = NULL;
2282 type_t *utype = get_user_type(type, &name);
2283 unsigned int usize = type_memsize(utype);
2284 unsigned int ualign = type_buffer_alignment(utype);
2285 unsigned int size = type_memsize(type);
2286 unsigned short funoff = user_type_offset(name);
2287 short reloff;
2289 if (processed(type)) return type->typestring_offset;
2291 guard_rec(type);
2293 if(user_type_has_variable_size(utype)) usize = 0;
2295 if (type_get_type(utype) == TYPE_BASIC ||
2296 type_get_type(utype) == TYPE_ENUM)
2298 unsigned char fc;
2300 if (type_get_type(utype) == TYPE_ENUM)
2301 fc = get_enum_fc(utype);
2302 else
2303 fc = get_basic_fc(utype);
2305 absoff = *tfsoff;
2306 print_start_tfs_comment(file, utype, absoff);
2307 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2308 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2309 *tfsoff += 2;
2311 else
2313 if (!processed(utype))
2314 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2315 absoff = utype->typestring_offset;
2318 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2319 flags = 0x40;
2320 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2321 flags = 0x80;
2322 else
2323 flags = 0;
2325 start = *tfsoff;
2326 update_tfsoff(type, start, file);
2327 print_start_tfs_comment(file, type, start);
2328 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2329 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2330 flags | (ualign - 1), ualign - 1, flags);
2331 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2332 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2333 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2334 *tfsoff += 8;
2335 reloff = absoff - *tfsoff;
2336 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2337 *tfsoff += 2;
2338 return start;
2341 static void write_member_type(FILE *file, const type_t *cont,
2342 int cont_is_complex, const attr_list_t *attrs,
2343 const type_t *type, unsigned int *corroff,
2344 unsigned int *tfsoff)
2346 if (is_embedded_complex(type) && !is_conformant_array(type))
2348 unsigned int absoff;
2349 short reloff;
2351 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2353 absoff = *corroff;
2354 *corroff += 8;
2356 else
2358 absoff = type->typestring_offset;
2360 reloff = absoff - (*tfsoff + 2);
2362 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2363 /* padding is represented using FC_STRUCTPAD* types, so presumably
2364 * this is left over in the format for historical purposes in MIDL
2365 * or rpcrt4. */
2366 print_file(file, 2, "0x0,\n");
2367 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2368 reloff, reloff, absoff);
2369 *tfsoff += 4;
2371 else if (is_ptr(type) || is_conformant_array(type))
2373 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2374 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2375 *tfsoff += 1;
2377 else if (!write_base_type(file, type, tfsoff))
2378 error("Unsupported member type %d\n", type_get_type(type));
2381 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2382 int cont_is_complex, unsigned int *tfsoff)
2384 type_t *elem = type_array_get_element_type(type);
2386 if (!is_embedded_complex(elem) && is_ptr(elem))
2388 type_t *ref = type_pointer_get_ref_type(elem);
2390 if (processed(ref))
2392 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2393 ref->typestring_offset, tfsoff);
2394 return;
2396 if (cont_is_complex && is_string_type(attrs, elem))
2398 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2399 return;
2401 if (!is_string_type(NULL, elem) &&
2402 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2404 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2405 return;
2408 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2411 static void write_end(FILE *file, unsigned int *tfsoff)
2413 if (*tfsoff % 2 == 0)
2415 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2416 *tfsoff += 1;
2418 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2419 *tfsoff += 1;
2422 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2424 unsigned int offset = 0;
2425 var_list_t *fs = type_struct_get_fields(type);
2426 var_t *f;
2428 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2430 type_t *ft = f->declspec.type;
2431 unsigned int size = field_memsize( ft, &offset );
2432 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2434 short reloff;
2435 unsigned int absoff = ft->typestring_offset;
2436 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2437 absoff += 8; /* we already have a corr descr, skip it */
2438 reloff = absoff - (*tfsoff + 6);
2439 print_file(file, 0, "/* %d */\n", *tfsoff);
2440 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2441 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2442 write_conf_or_var_desc(file, current_structure, offset, ft,
2443 get_attrp(f->attrs, ATTR_SWITCHIS));
2444 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2445 (unsigned short)reloff, reloff, absoff);
2446 *tfsoff += 8;
2448 offset += size;
2452 static int write_pointer_description_offsets(
2453 FILE *file, const attr_list_t *attrs, type_t *type,
2454 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2455 unsigned int *typestring_offset)
2457 int written = 0;
2459 if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
2460 (is_array(type) && type_array_is_decl_as_ptr(type)))
2462 if (offset_in_memory && offset_in_buffer)
2464 unsigned int memsize;
2466 /* pointer instance
2468 * note that MSDN states that for pointer layouts in structures,
2469 * this is a negative offset from the end of the structure, but
2470 * this statement is incorrect. all offsets are positive */
2471 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2472 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2474 memsize = type_memsize(type);
2475 *offset_in_memory += memsize;
2476 /* increment these separately as in the case of conformant (varying)
2477 * structures these start at different values */
2478 *offset_in_buffer += memsize;
2480 *typestring_offset += 4;
2482 if (is_ptr(type))
2484 type_t *ref = type_pointer_get_ref_type(type);
2486 if (is_string_type(attrs, type))
2487 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2488 else if (processed(ref))
2489 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2490 ref->typestring_offset, typestring_offset);
2491 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2492 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2493 else
2494 error("write_pointer_description_offsets: type format string unknown\n");
2496 else
2498 unsigned int offset = type->typestring_offset;
2499 /* skip over the pointer that is written for strings, since a
2500 * pointer has to be written in-place here */
2501 if (is_string_type(attrs, type))
2502 offset += 4;
2503 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2506 return 1;
2509 if (is_array(type))
2511 return write_pointer_description_offsets(
2512 file, attrs, type_array_get_element_type(type), offset_in_memory,
2513 offset_in_buffer, typestring_offset);
2515 else if (is_non_complex_struct(type))
2517 /* otherwise search for interesting fields to parse */
2518 const var_t *v;
2519 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2521 if (offset_in_memory && offset_in_buffer)
2523 unsigned int padding;
2524 unsigned int align = 0;
2525 type_memsize_and_alignment(v->declspec.type, &align);
2526 padding = ROUNDING(*offset_in_memory, align);
2527 *offset_in_memory += padding;
2528 *offset_in_buffer += padding;
2530 written += write_pointer_description_offsets(
2531 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2532 typestring_offset);
2535 else
2537 if (offset_in_memory && offset_in_buffer)
2539 unsigned int memsize = type_memsize(type);
2540 *offset_in_memory += memsize;
2541 /* increment these separately as in the case of conformant (varying)
2542 * structures these start at different values */
2543 *offset_in_buffer += memsize;
2547 return written;
2550 static int write_no_repeat_pointer_descriptions(
2551 FILE *file, const attr_list_t *attrs, type_t *type,
2552 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2553 unsigned int *typestring_offset)
2555 int written = 0;
2557 if (is_ptr(type) ||
2558 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2560 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2561 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2562 *typestring_offset += 2;
2564 return write_pointer_description_offsets(file, attrs, type,
2565 offset_in_memory, offset_in_buffer, typestring_offset);
2568 if (is_non_complex_struct(type))
2570 const var_t *v;
2571 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2573 if (offset_in_memory && offset_in_buffer)
2575 unsigned int padding;
2576 unsigned int align = 0;
2577 type_memsize_and_alignment(v->declspec.type, &align);
2578 padding = ROUNDING(*offset_in_memory, align);
2579 *offset_in_memory += padding;
2580 *offset_in_buffer += padding;
2582 written += write_no_repeat_pointer_descriptions(
2583 file, v->attrs, v->declspec.type,
2584 offset_in_memory, offset_in_buffer, typestring_offset);
2587 else
2589 unsigned int memsize = type_memsize(type);
2590 *offset_in_memory += memsize;
2591 /* increment these separately as in the case of conformant (varying)
2592 * structures these start at different values */
2593 *offset_in_buffer += memsize;
2596 return written;
2599 /* Note: if file is NULL return value is number of pointers to write, else
2600 * it is the number of type format characters written */
2601 static int write_fixed_array_pointer_descriptions(
2602 FILE *file, const attr_list_t *attrs, type_t *type,
2603 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2604 unsigned int *typestring_offset)
2606 int pointer_count = 0;
2608 if (type_get_type(type) == TYPE_ARRAY &&
2609 !type_array_has_conformance(type) && !type_array_has_variance(type))
2611 unsigned int temp = 0;
2612 /* unfortunately, this needs to be done in two passes to avoid
2613 * writing out redundant FC_FIXED_REPEAT descriptions */
2614 pointer_count = write_pointer_description_offsets(
2615 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2616 if (pointer_count > 0)
2618 unsigned int increment_size;
2619 unsigned int offset_of_array_pointer_mem = 0;
2620 unsigned int offset_of_array_pointer_buf = 0;
2622 increment_size = type_memsize(type_array_get_element_type(type));
2624 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2625 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2626 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2627 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2628 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2629 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2630 *typestring_offset += 10;
2632 pointer_count = write_pointer_description_offsets(
2633 file, attrs, type, &offset_of_array_pointer_mem,
2634 &offset_of_array_pointer_buf, typestring_offset);
2637 else if (type_get_type(type) == TYPE_STRUCT)
2639 const var_t *v;
2640 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2642 if (offset_in_memory && offset_in_buffer)
2644 unsigned int padding;
2645 unsigned int align = 0;
2646 type_memsize_and_alignment(v->declspec.type, &align);
2647 padding = ROUNDING(*offset_in_memory, align);
2648 *offset_in_memory += padding;
2649 *offset_in_buffer += padding;
2651 pointer_count += write_fixed_array_pointer_descriptions(
2652 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2653 typestring_offset);
2656 else
2658 if (offset_in_memory && offset_in_buffer)
2660 unsigned int memsize;
2661 memsize = type_memsize(type);
2662 *offset_in_memory += memsize;
2663 /* increment these separately as in the case of conformant (varying)
2664 * structures these start at different values */
2665 *offset_in_buffer += memsize;
2669 return pointer_count;
2672 /* Note: if file is NULL return value is number of pointers to write, else
2673 * it is the number of type format characters written */
2674 static int write_conformant_array_pointer_descriptions(
2675 FILE *file, const attr_list_t *attrs, type_t *type,
2676 unsigned int offset_in_memory, unsigned int *typestring_offset)
2678 int pointer_count = 0;
2680 if (is_conformant_array(type) && !type_array_has_variance(type))
2682 unsigned int temp = 0;
2683 /* unfortunately, this needs to be done in two passes to avoid
2684 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2685 pointer_count = write_pointer_description_offsets(
2686 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2687 if (pointer_count > 0)
2689 unsigned int increment_size;
2690 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2691 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2693 increment_size = type_memsize(type_array_get_element_type(type));
2695 if (increment_size > USHRT_MAX)
2696 error("array size of %u bytes is too large\n", increment_size);
2698 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2699 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2700 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2701 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2702 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2703 *typestring_offset += 8;
2705 pointer_count = write_pointer_description_offsets(
2706 file, attrs, type_array_get_element_type(type),
2707 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2708 typestring_offset);
2712 return pointer_count;
2715 /* Note: if file is NULL return value is number of pointers to write, else
2716 * it is the number of type format characters written */
2717 static int write_varying_array_pointer_descriptions(
2718 FILE *file, const attr_list_t *attrs, type_t *type,
2719 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2720 unsigned int *typestring_offset)
2722 int pointer_count = 0;
2724 if (is_array(type) && type_array_has_variance(type))
2726 unsigned int temp = 0;
2727 /* unfortunately, this needs to be done in two passes to avoid
2728 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2729 pointer_count = write_pointer_description_offsets(
2730 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2731 if (pointer_count > 0)
2733 unsigned int increment_size;
2735 increment_size = type_memsize(type_array_get_element_type(type));
2737 if (increment_size > USHRT_MAX)
2738 error("array size of %u bytes is too large\n", increment_size);
2740 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2741 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2742 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2743 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2744 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2745 *typestring_offset += 8;
2747 pointer_count = write_pointer_description_offsets(
2748 file, attrs, type_array_get_element_type(type), offset_in_memory,
2749 offset_in_buffer, typestring_offset);
2752 else if (type_get_type(type) == TYPE_STRUCT)
2754 const var_t *v;
2755 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2757 if (offset_in_memory && offset_in_buffer)
2759 unsigned int align = 0, padding;
2761 if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
2763 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2764 /* skip over variance and offset in buffer */
2765 *offset_in_buffer += 8;
2768 type_memsize_and_alignment(v->declspec.type, &align);
2769 padding = ROUNDING(*offset_in_memory, align);
2770 *offset_in_memory += padding;
2771 *offset_in_buffer += padding;
2773 pointer_count += write_varying_array_pointer_descriptions(
2774 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2775 typestring_offset);
2778 else
2780 if (offset_in_memory && offset_in_buffer)
2782 unsigned int memsize = type_memsize(type);
2783 *offset_in_memory += memsize;
2784 /* increment these separately as in the case of conformant (varying)
2785 * structures these start at different values */
2786 *offset_in_buffer += memsize;
2790 return pointer_count;
2793 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2794 unsigned int *typestring_offset)
2796 unsigned int offset_in_buffer;
2797 unsigned int offset_in_memory;
2799 /* pass 1: search for single instance of a pointer (i.e. don't descend
2800 * into arrays) */
2801 if (!is_array(type))
2803 offset_in_memory = 0;
2804 offset_in_buffer = 0;
2805 write_no_repeat_pointer_descriptions(
2806 file, NULL, type,
2807 &offset_in_memory, &offset_in_buffer, typestring_offset);
2810 /* pass 2: search for pointers in fixed arrays */
2811 offset_in_memory = 0;
2812 offset_in_buffer = 0;
2813 write_fixed_array_pointer_descriptions(
2814 file, NULL, type,
2815 &offset_in_memory, &offset_in_buffer, typestring_offset);
2817 /* pass 3: search for pointers in conformant only arrays (but don't descend
2818 * into conformant varying or varying arrays) */
2819 if (is_conformant_array(type) &&
2820 (type_array_is_decl_as_ptr(type) || !current_structure))
2821 write_conformant_array_pointer_descriptions(
2822 file, attrs, type, 0, typestring_offset);
2823 else if (type_get_type(type) == TYPE_STRUCT &&
2824 get_struct_fc(type) == FC_CPSTRUCT)
2826 type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
2827 write_conformant_array_pointer_descriptions( file, NULL, carray,
2828 type_memsize(type), typestring_offset);
2831 /* pass 4: search for pointers in varying arrays */
2832 offset_in_memory = 0;
2833 offset_in_buffer = 0;
2834 write_varying_array_pointer_descriptions(
2835 file, NULL, type,
2836 &offset_in_memory, &offset_in_buffer, typestring_offset);
2839 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2840 type_t *type, enum type_context context,
2841 const char *name, unsigned int *typestring_offset)
2843 unsigned int start_offset;
2844 unsigned char rtype;
2845 type_t *elem_type;
2846 int is_processed = processed(type);
2848 start_offset = *typestring_offset;
2850 if (is_declptr(type))
2852 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2853 int pointer_type = get_pointer_fc_context(type, attrs, context);
2854 if (!pointer_type)
2855 pointer_type = FC_RP;
2856 print_start_tfs_comment(file, type, *typestring_offset);
2857 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2858 pointer_type, flag, string_of_type(pointer_type),
2859 flag ? " [simple_pointer]" : "");
2860 *typestring_offset += 2;
2861 if (!flag)
2863 print_file(file, 2, "NdrFcShort(0x2),\n");
2864 *typestring_offset += 2;
2866 is_processed = FALSE;
2869 if (is_array(type))
2870 elem_type = type_array_get_element_type(type);
2871 else
2872 elem_type = type_pointer_get_ref_type(type);
2874 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2875 return write_array_tfs(file, attrs, type, name, typestring_offset);
2877 if (type_get_type(elem_type) != TYPE_BASIC)
2879 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2880 return start_offset;
2883 rtype = get_basic_fc(elem_type);
2884 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2886 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2887 return start_offset;
2890 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2892 unsigned int dim = type_array_get_dim(type);
2894 if (is_processed) return start_offset;
2896 /* FIXME: multi-dimensional array */
2897 if (0xffffu < dim)
2898 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2899 name, 0xffffu, dim - 0xffffu);
2901 if (rtype == FC_WCHAR)
2902 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2903 else
2904 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2905 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2906 *typestring_offset += 2;
2908 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2909 *typestring_offset += 2;
2911 update_tfsoff(type, start_offset, file);
2912 return start_offset;
2914 else if (is_conformant_array(type))
2916 if (rtype == FC_WCHAR)
2917 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2918 else
2919 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2920 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2921 *typestring_offset += 2;
2923 *typestring_offset += write_conf_or_var_desc(
2924 file, current_structure,
2925 (!type_array_is_decl_as_ptr(type) && current_structure
2926 ? type_memsize(current_structure)
2927 : 0),
2928 type, type_array_get_conformance(type));
2930 update_tfsoff(type, start_offset, file);
2931 return start_offset;
2933 else
2935 if (is_processed) return start_offset;
2937 if (rtype == FC_WCHAR)
2938 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2939 else
2940 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2941 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2942 *typestring_offset += 2;
2944 update_tfsoff(type, start_offset, file);
2945 return start_offset;
2949 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2950 const char *name, unsigned int *typestring_offset)
2952 const expr_t *length_is = type_array_get_variance(type);
2953 const expr_t *size_is = type_array_get_conformance(type);
2954 unsigned int align;
2955 unsigned int size;
2956 unsigned int start_offset;
2957 unsigned char fc;
2958 unsigned int baseoff
2959 = !type_array_is_decl_as_ptr(type) && current_structure
2960 ? type_memsize(current_structure)
2961 : 0;
2963 if (!is_string_type(attrs, type_array_get_element_type(type)))
2964 write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
2966 size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2967 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2968 fc = get_array_fc(type);
2970 start_offset = *typestring_offset;
2971 update_tfsoff(type, start_offset, file);
2972 print_start_tfs_comment(file, type, start_offset);
2973 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2974 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2975 *typestring_offset += 2;
2977 align = 0;
2978 if (fc != FC_BOGUS_ARRAY)
2980 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
2982 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2983 *typestring_offset += 4;
2985 else
2987 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2988 *typestring_offset += 2;
2991 if (is_conformant_array(type))
2992 *typestring_offset
2993 += write_conf_or_var_desc(file, current_structure, baseoff,
2994 type, size_is);
2996 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
2998 unsigned int elsize = type_memsize(type_array_get_element_type(type));
2999 unsigned int dim = type_array_get_dim(type);
3001 if (fc == FC_LGVARRAY)
3003 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
3004 *typestring_offset += 4;
3006 else
3008 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3009 *typestring_offset += 2;
3012 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
3013 *typestring_offset += 2;
3016 if (length_is)
3017 *typestring_offset
3018 += write_conf_or_var_desc(file, current_structure, baseoff,
3019 type, length_is);
3021 if (type_has_pointers(type_array_get_element_type(type)) &&
3022 (type_array_is_decl_as_ptr(type) || !current_structure))
3024 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3025 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3026 *typestring_offset += 2;
3027 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
3028 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3029 *typestring_offset += 1;
3032 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
3033 write_end(file, typestring_offset);
3035 else
3037 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
3038 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3039 *typestring_offset += 2;
3040 *typestring_offset
3041 += write_conf_or_var_desc(file, current_structure, baseoff,
3042 type, size_is);
3043 *typestring_offset
3044 += write_conf_or_var_desc(file, current_structure, baseoff,
3045 type, length_is);
3047 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
3048 write_end(file, typestring_offset);
3051 return start_offset;
3054 static const var_t *find_array_or_string_in_struct(const type_t *type)
3056 const var_list_t *fields = type_struct_get_fields(type);
3057 const var_t *last_field;
3058 const type_t *ft;
3060 if (!fields || list_empty(fields))
3061 return NULL;
3063 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3064 ft = last_field->declspec.type;
3066 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3067 return last_field;
3069 if (type_get_type(ft) == TYPE_STRUCT)
3070 return find_array_or_string_in_struct(ft);
3071 else
3072 return NULL;
3075 static void write_struct_members(FILE *file, const type_t *type,
3076 int is_complex, unsigned int *corroff,
3077 unsigned int *typestring_offset)
3079 const var_t *field;
3080 unsigned short offset = 0;
3081 unsigned int salign = 1;
3082 int padding;
3083 var_list_t *fields = type_struct_get_fields(type);
3085 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3087 type_t *ft = field->declspec.type;
3088 unsigned int align = 0;
3089 unsigned int size = type_memsize_and_alignment(ft, &align);
3090 align = clamp_align(align);
3091 if (salign < align) salign = align;
3093 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3095 if ((align - 1) & offset)
3097 unsigned char fc = 0;
3098 switch (align)
3100 case 2:
3101 fc = FC_ALIGNM2;
3102 break;
3103 case 4:
3104 fc = FC_ALIGNM4;
3105 break;
3106 case 8:
3107 fc = FC_ALIGNM8;
3108 break;
3109 default:
3110 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3112 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3113 offset = ROUND_SIZE(offset, align);
3114 *typestring_offset += 1;
3116 write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
3117 typestring_offset);
3118 offset += size;
3122 padding = ROUNDING(offset, salign);
3123 if (padding)
3125 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3126 FC_STRUCTPAD1 + padding - 1,
3127 padding);
3128 *typestring_offset += 1;
3131 write_end(file, typestring_offset);
3134 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3135 const char *name, unsigned int *tfsoff)
3137 const type_t *save_current_structure = current_structure;
3138 unsigned int total_size;
3139 const var_t *array;
3140 unsigned int start_offset;
3141 unsigned int align;
3142 unsigned int corroff;
3143 var_t *f;
3144 unsigned char fc = get_struct_fc(type);
3145 var_list_t *fields = type_struct_get_fields(type);
3147 if (processed(type)) return type->typestring_offset;
3149 guard_rec(type);
3150 current_structure = type;
3152 total_size = type_memsize(type);
3153 align = type_buffer_alignment(type);
3154 if (total_size > USHRT_MAX)
3155 error("structure size for %s exceeds %d bytes by %d bytes\n",
3156 name, USHRT_MAX, total_size - USHRT_MAX);
3158 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3159 write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
3161 array = find_array_or_string_in_struct(type);
3162 if (array && !processed(array->declspec.type))
3164 if(is_string_type(array->attrs, array->declspec.type))
3165 write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3166 else
3167 write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
3170 corroff = *tfsoff;
3171 write_descriptors(file, type, tfsoff);
3173 start_offset = *tfsoff;
3174 update_tfsoff(type, start_offset, file);
3175 print_start_tfs_comment(file, type, start_offset);
3176 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3177 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3178 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3179 *tfsoff += 4;
3181 if (array)
3183 unsigned int absoff = array->declspec.type->typestring_offset;
3184 short reloff = absoff - *tfsoff;
3185 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3186 reloff, reloff, absoff);
3187 *tfsoff += 2;
3189 else if (fc == FC_BOGUS_STRUCT)
3191 print_file(file, 2, "NdrFcShort(0x0),\n");
3192 *tfsoff += 2;
3195 if (fc == FC_BOGUS_STRUCT)
3197 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3198 nothing is written to file yet. On the actual writing pass,
3199 this will have been updated. */
3200 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3201 int reloff = absoff - *tfsoff;
3202 assert( reloff >= 0 );
3203 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3204 (unsigned short)reloff, reloff, absoff);
3205 *tfsoff += 2;
3207 else if ((fc == FC_PSTRUCT) ||
3208 (fc == FC_CPSTRUCT) ||
3209 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3211 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3212 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3213 *tfsoff += 2;
3214 write_pointer_description(file, NULL, type, tfsoff);
3215 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3216 *tfsoff += 1;
3219 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3220 tfsoff);
3222 if (fc == FC_BOGUS_STRUCT)
3224 const var_t *f;
3226 type->ptrdesc = *tfsoff;
3227 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3229 type_t *ft = f->declspec.type;
3230 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3232 case TGT_POINTER:
3233 if (is_string_type(f->attrs, ft))
3234 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3235 else
3236 write_pointer_tfs(file, f->attrs, ft,
3237 type_pointer_get_ref_type(ft)->typestring_offset,
3238 TYPE_CONTEXT_CONTAINER, tfsoff);
3239 break;
3240 case TGT_ARRAY:
3241 if (type_array_is_decl_as_ptr(ft))
3243 unsigned int offset;
3245 print_file(file, 0, "/* %d */\n", *tfsoff);
3247 offset = ft->typestring_offset;
3248 /* skip over the pointer that is written for strings, since a
3249 * pointer has to be written in-place here */
3250 if (is_string_type(f->attrs, ft))
3251 offset += 4;
3252 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3254 break;
3255 default:
3256 break;
3259 if (type->ptrdesc == *tfsoff)
3260 type->ptrdesc = 0;
3263 current_structure = save_current_structure;
3264 return start_offset;
3267 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3269 if (t == NULL)
3271 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3273 else
3275 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3277 unsigned char fc;
3278 if (type_get_type(t) == TYPE_BASIC)
3279 fc = get_basic_fc(t);
3280 else
3281 fc = get_enum_fc(t);
3282 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3283 fc, string_of_type(fc));
3285 else if (t->typestring_offset)
3287 short reloff = t->typestring_offset - *tfsoff;
3288 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3289 reloff, reloff, t->typestring_offset);
3291 else
3292 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3295 *tfsoff += 2;
3298 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3299 type_t *type, unsigned int *tfsoff)
3301 unsigned int start_offset;
3302 unsigned int size;
3303 var_list_t *fields;
3304 unsigned int nbranch = 0;
3305 type_t *deftype = NULL;
3306 short nodeftype = 0xffff;
3307 unsigned int dummy;
3308 var_t *f;
3310 if (processed(type) &&
3311 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3312 return type->typestring_offset;
3314 guard_rec(type);
3316 fields = type_union_get_cases(type);
3318 size = union_memsize(fields, &dummy);
3320 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3322 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3323 if (cases)
3324 nbranch += list_count(cases);
3325 if (f->declspec.type)
3326 write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
3329 start_offset = *tfsoff;
3330 update_tfsoff(type, start_offset, file);
3331 print_start_tfs_comment(file, type, start_offset);
3332 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3334 const var_t *sv = type_union_get_switch_value(type);
3335 const type_t *st = sv->declspec.type;
3336 unsigned int align = 0;
3337 unsigned char fc;
3339 if (type_get_type(st) == TYPE_BASIC)
3341 fc = get_basic_fc(st);
3342 switch (fc)
3344 case FC_CHAR:
3345 case FC_SMALL:
3346 case FC_BYTE:
3347 case FC_USMALL:
3348 case FC_WCHAR:
3349 case FC_SHORT:
3350 case FC_USHORT:
3351 case FC_LONG:
3352 case FC_ULONG:
3353 break;
3354 default:
3355 fc = 0;
3356 error("union switch type must be an integer, char, or enum\n");
3359 else if (type_get_type(st) == TYPE_ENUM)
3360 fc = get_enum_fc(st);
3361 else
3362 error("union switch type must be an integer, char, or enum\n");
3364 type_memsize_and_alignment(st, &align);
3365 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3367 if (f->declspec.type)
3368 type_memsize_and_alignment(f->declspec.type, &align);
3371 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3372 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3373 (align << 4) | fc, string_of_type(fc));
3374 *tfsoff += 2;
3376 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3378 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3379 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3380 unsigned char fc;
3382 if (type_get_type(st) == TYPE_BASIC)
3384 fc = get_basic_fc(st);
3385 switch (fc)
3387 case FC_CHAR:
3388 case FC_SMALL:
3389 case FC_USMALL:
3390 case FC_SHORT:
3391 case FC_USHORT:
3392 case FC_LONG:
3393 case FC_ULONG:
3394 case FC_ENUM16:
3395 case FC_ENUM32:
3396 break;
3397 default:
3398 fc = 0;
3399 error("union switch type must be an integer, char, or enum\n");
3402 else if (type_get_type(st) == TYPE_ENUM)
3403 fc = get_enum_fc(st);
3404 else
3405 error("union switch type must be an integer, char, or enum\n");
3407 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3408 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3409 fc, string_of_type(fc));
3410 *tfsoff += 2;
3411 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3412 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3413 *tfsoff += 2;
3414 print_file(file, 0, "/* %u */\n", *tfsoff);
3417 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3418 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3419 *tfsoff += 4;
3421 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3423 type_t *ft = f->declspec.type;
3424 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3425 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3426 expr_t *c;
3428 if (cases == NULL && !deflt)
3429 error("union field %s with neither case nor default attribute\n", f->name);
3431 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3433 /* MIDL doesn't check for duplicate cases, even though that seems
3434 like a reasonable thing to do, it just dumps them to the TFS
3435 like we're going to do here. */
3436 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3437 *tfsoff += 4;
3438 write_branch_type(file, ft, tfsoff);
3441 /* MIDL allows multiple default branches, even though that seems
3442 illogical, it just chooses the last one, which is what we will
3443 do. */
3444 if (deflt)
3446 deftype = ft;
3447 nodeftype = 0;
3451 if (deftype)
3453 write_branch_type(file, deftype, tfsoff);
3455 else
3457 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3458 *tfsoff += 2;
3461 return start_offset;
3464 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3465 unsigned int *typeformat_offset)
3467 unsigned int i;
3468 unsigned int start_offset = *typeformat_offset;
3469 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3471 if (!iid && processed(type)) return type->typestring_offset;
3473 print_start_tfs_comment(file, type, start_offset);
3474 update_tfsoff(type, start_offset, file);
3476 if (iid)
3478 print_file(file, 2, "0x2f, /* FC_IP */\n");
3479 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3480 *typeformat_offset
3481 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3483 else
3485 const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
3486 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3488 if (! uuid)
3489 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3491 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3492 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3493 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3494 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3495 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3496 for (i = 0; i < 8; ++i)
3497 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3499 if (file)
3500 fprintf(file, "\n");
3502 *typeformat_offset += 18;
3504 return start_offset;
3507 static unsigned int write_contexthandle_tfs(FILE *file,
3508 const attr_list_t *attrs,
3509 type_t *type,
3510 enum type_context context,
3511 unsigned int *typeformat_offset)
3513 unsigned int start_offset = *typeformat_offset;
3514 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
3516 print_start_tfs_comment(file, type, start_offset);
3518 if (flags & 0x80) /* via ptr */
3520 int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
3521 if (!pointer_type) pointer_type = FC_RP;
3522 *typeformat_offset += 4;
3523 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3524 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3525 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3528 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3529 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3530 if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
3531 print_file(file, 0, "can't be null, ");
3532 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3533 print_file(file, 0, "serialize, ");
3534 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3535 print_file(file, 0, "no serialize, ");
3536 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3537 print_file(file, 0, "strict, ");
3538 if (flags & HANDLE_PARAM_IS_RETURN)
3539 print_file(file, 0, "return, ");
3540 if (flags & HANDLE_PARAM_IS_OUT)
3541 print_file(file, 0, "out, ");
3542 if (flags & HANDLE_PARAM_IS_IN)
3543 print_file(file, 0, "in, ");
3544 if (flags & HANDLE_PARAM_IS_VIA_PTR)
3545 print_file(file, 0, "via ptr, ");
3546 print_file(file, 0, "*/\n");
3547 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3548 print_file(file, 2, "0, /* FIXME: param num */\n");
3549 *typeformat_offset += 4;
3551 update_tfsoff( type, start_offset, file );
3552 return start_offset;
3555 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3556 type_t *type, expr_list_t *range_list,
3557 unsigned int *typeformat_offset)
3559 unsigned char fc;
3560 unsigned int start_offset = *typeformat_offset;
3561 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3562 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3564 if (type_get_type(type) == TYPE_BASIC)
3565 fc = get_basic_fc(type);
3566 else
3567 fc = get_enum_fc(type);
3569 /* fc must fit in lower 4-bits of 8-bit field below */
3570 assert(fc <= 0xf);
3572 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3573 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3574 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3575 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3576 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3577 update_tfsoff( type, start_offset, file );
3578 *typeformat_offset += 10;
3580 return start_offset;
3583 static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
3584 type_t *type, const char *name,
3585 enum type_context context,
3586 unsigned int *typeformat_offset)
3588 unsigned int offset;
3590 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3592 case TGT_CTXT_HANDLE:
3593 case TGT_CTXT_HANDLE_POINTER:
3594 return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
3595 case TGT_USER_TYPE:
3596 return write_user_tfs(file, type, typeformat_offset);
3597 case TGT_STRING:
3598 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3599 case TGT_ARRAY:
3601 unsigned int off;
3602 /* conformant and pointer arrays are handled specially */
3603 if ((context != TYPE_CONTEXT_CONTAINER &&
3604 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3605 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3606 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3607 else
3608 off = 0;
3609 if (context != TYPE_CONTEXT_CONTAINER &&
3610 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3612 int ptr_type;
3613 ptr_type = get_pointer_fc_context(type, attrs, context);
3614 if (type_array_is_decl_as_ptr(type)
3615 || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
3617 unsigned int absoff = type->typestring_offset;
3618 short reloff = absoff - (*typeformat_offset + 2);
3619 off = *typeformat_offset;
3620 print_file(file, 0, "/* %d */\n", off);
3621 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3622 string_of_type(ptr_type));
3623 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3624 reloff, reloff, absoff);
3625 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3626 *typeformat_offset += 4;
3628 type_array_set_ptr_tfsoff(type, off);
3630 return off;
3632 case TGT_STRUCT:
3633 return write_struct_tfs(file, type, name, typeformat_offset);
3634 case TGT_UNION:
3635 return write_union_tfs(file, attrs, type, typeformat_offset);
3636 case TGT_ENUM:
3637 case TGT_BASIC:
3638 /* nothing to do */
3639 return 0;
3640 case TGT_RANGE:
3642 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3643 if (!range_list)
3644 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3645 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3647 case TGT_IFACE_POINTER:
3648 return write_ip_tfs(file, attrs, type, typeformat_offset);
3649 case TGT_POINTER:
3651 enum type_context ref_context;
3652 type_t *ref = type_pointer_get_ref_type(type);
3654 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3655 ref_context = TYPE_CONTEXT_PARAM;
3656 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3657 ref_context = TYPE_CONTEXT_CONTAINER;
3658 else
3659 ref_context = context;
3661 if (is_string_type(attrs, ref))
3663 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3664 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3666 offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
3667 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3668 return 0;
3669 return offset;
3672 offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
3673 ref_context, typeformat_offset);
3674 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3675 return 0;
3676 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3678 case TGT_INVALID:
3679 break;
3681 error("invalid type %s for var %s\n", type->name, name);
3682 return 0;
3685 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3686 const char *name, int write_ptr, unsigned int *tfsoff)
3688 return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3691 static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
3693 const statement_list_t *stmts = type_iface_get_stmts(iface);
3694 const statement_t *stmt;
3695 var_t *var;
3697 current_iface = iface;
3698 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
3700 switch(stmt->type)
3702 case STMT_DECLARATION:
3704 const var_t *func = stmt->u.var;
3706 if(stmt->u.var->declspec.stgclass != STG_NONE
3707 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
3708 continue;
3710 current_func = func;
3711 if (is_local(func->attrs)) continue;
3713 var = type_function_get_retval(func->declspec.type);
3714 if (!is_void(var->declspec.type))
3715 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
3716 TYPE_CONTEXT_RETVAL, offset);
3718 if (type_function_get_args(func->declspec.type))
3719 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
3720 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
3721 TYPE_CONTEXT_TOPLEVELPARAM, offset );
3722 break;
3725 case STMT_TYPEDEF:
3727 const type_list_t *type_entry;
3728 for (type_entry = stmt->u.type_list; type_entry; type_entry = type_entry->next)
3730 if (is_attr(type_entry->type->attrs, ATTR_ENCODE)
3731 || is_attr(type_entry->type->attrs, ATTR_DECODE))
3732 type_entry->type->typestring_offset = write_type_tfs( file,
3733 type_entry->type->attrs, type_entry->type, type_entry->type->name,
3734 TYPE_CONTEXT_CONTAINER, offset);
3736 break;
3738 default:
3739 break;
3744 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3746 unsigned int typeformat_offset = 2;
3747 for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
3748 return typeformat_offset + 1;
3752 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3754 int indent = 0;
3756 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3757 print_file(file, indent, "{\n");
3758 indent++;
3759 print_file(file, indent, "0,\n");
3760 print_file(file, indent, "{\n");
3761 indent++;
3762 print_file(file, indent, "NdrFcShort(0x0),\n");
3764 set_all_tfswrite(TRUE);
3765 process_tfs(file, stmts, pred);
3767 print_file(file, indent, "0x0\n");
3768 indent--;
3769 print_file(file, indent, "}\n");
3770 indent--;
3771 print_file(file, indent, "};\n");
3772 print_file(file, indent, "\n");
3775 static unsigned int get_required_buffer_size_type(
3776 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3778 *alignment = 0;
3779 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3781 case TGT_USER_TYPE:
3783 const char *uname = NULL;
3784 const type_t *utype = get_user_type(type, &uname);
3785 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3787 case TGT_BASIC:
3788 switch (get_basic_fc(type))
3790 case FC_BYTE:
3791 case FC_CHAR:
3792 case FC_USMALL:
3793 case FC_SMALL:
3794 *alignment = 4;
3795 return 1;
3797 case FC_WCHAR:
3798 case FC_USHORT:
3799 case FC_SHORT:
3800 *alignment = 4;
3801 return 2;
3803 case FC_ULONG:
3804 case FC_LONG:
3805 case FC_FLOAT:
3806 case FC_ERROR_STATUS_T:
3807 *alignment = 4;
3808 return 4;
3810 case FC_HYPER:
3811 case FC_DOUBLE:
3812 *alignment = 8;
3813 return 8;
3815 case FC_INT3264:
3816 case FC_UINT3264:
3817 assert( pointer_size );
3818 *alignment = pointer_size;
3819 return pointer_size;
3821 case FC_IGNORE:
3822 case FC_BIND_PRIMITIVE:
3823 return 0;
3825 default:
3826 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3827 get_basic_fc(type));
3828 return 0;
3830 break;
3832 case TGT_ENUM:
3833 switch (get_enum_fc(type))
3835 case FC_ENUM32:
3836 *alignment = 4;
3837 return 4;
3838 case FC_ENUM16:
3839 *alignment = 4;
3840 return 2;
3842 break;
3844 case TGT_STRUCT:
3845 if (get_struct_fc(type) == FC_STRUCT)
3847 if (!type_struct_get_fields(type)) return 0;
3848 return fields_memsize(type_struct_get_fields(type), alignment);
3850 break;
3852 case TGT_POINTER:
3854 unsigned int size, align;
3855 const type_t *ref = type_pointer_get_ref_type(type);
3856 if (is_string_type( attrs, ref )) break;
3857 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3858 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3860 size += 4 + align;
3861 align = 4;
3863 *alignment = align;
3864 return size;
3867 case TGT_ARRAY:
3868 switch (get_array_fc(type))
3870 case FC_SMFARRAY:
3871 case FC_LGFARRAY:
3872 return type_array_get_dim(type) *
3873 get_required_buffer_size_type(type_array_get_element_type(type), name,
3874 NULL, FALSE, alignment);
3876 break;
3878 default:
3879 break;
3881 return 0;
3884 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3886 int in_attr = is_attr(var->attrs, ATTR_IN);
3887 int out_attr = is_attr(var->attrs, ATTR_OUT);
3889 if (!in_attr && !out_attr)
3890 in_attr = 1;
3892 *alignment = 0;
3894 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3895 pass == PASS_RETURN)
3897 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3899 *alignment = 4;
3900 return 20;
3903 if (!is_string_type(var->attrs, var->declspec.type))
3904 return get_required_buffer_size_type(var->declspec.type, var->name,
3905 var->attrs, TRUE, alignment);
3907 return 0;
3910 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3912 const var_t *var;
3913 unsigned int total_size = 0, alignment;
3915 if (type_function_get_args(func->declspec.type))
3917 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
3919 total_size += get_required_buffer_size(var, &alignment, pass);
3920 total_size += alignment;
3924 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
3926 var_t v = *func;
3927 v.declspec.type = type_function_get_rettype(func->declspec.type);
3928 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3929 total_size += alignment;
3931 return total_size;
3934 static void print_phase_function(FILE *file, int indent, const char *type,
3935 const char *local_var_prefix, enum remoting_phase phase,
3936 const var_t *var, unsigned int type_offset)
3938 const char *function;
3939 switch (phase)
3941 case PHASE_BUFFERSIZE:
3942 function = "BufferSize";
3943 break;
3944 case PHASE_MARSHAL:
3945 function = "Marshall";
3946 break;
3947 case PHASE_UNMARSHAL:
3948 function = "Unmarshall";
3949 break;
3950 case PHASE_FREE:
3951 function = "Free";
3952 break;
3953 default:
3954 assert(0);
3955 return;
3958 print_file(file, indent, "Ndr%s%s(\n", type, function);
3959 indent++;
3960 print_file(file, indent, "&__frame->_StubMsg,\n");
3961 print_file(file, indent, "%s%s%s%s%s,\n",
3962 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3963 (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
3964 local_var_prefix,
3965 (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
3966 var->name);
3967 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3968 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3969 if (phase == PHASE_UNMARSHAL)
3970 print_file(file, indent, "0);\n");
3971 indent--;
3974 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3975 enum remoting_phase phase, enum pass pass, const var_t *var,
3976 const char *varname)
3978 type_t *type = var->declspec.type;
3979 unsigned int alignment = 0;
3981 /* no work to do for other phases, buffer sizing is done elsewhere */
3982 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3983 return;
3985 if (type_get_type(type) == TYPE_ENUM ||
3986 (type_get_type(type) == TYPE_BASIC &&
3987 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3988 pointer_size != 4))
3990 unsigned char fc;
3992 if (type_get_type(type) == TYPE_ENUM)
3993 fc = get_enum_fc(type);
3994 else
3995 fc = get_basic_fc(type);
3997 if (phase == PHASE_MARSHAL)
3998 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3999 else
4000 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
4001 print_file(file, indent+1, "&__frame->_StubMsg,\n");
4002 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
4003 local_var_prefix,
4004 var->name);
4005 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
4007 else
4009 const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
4011 switch (get_basic_fc(ref->type))
4013 case FC_BYTE:
4014 case FC_CHAR:
4015 case FC_SMALL:
4016 case FC_USMALL:
4017 alignment = 1;
4018 break;
4020 case FC_WCHAR:
4021 case FC_USHORT:
4022 case FC_SHORT:
4023 alignment = 2;
4024 break;
4026 case FC_ULONG:
4027 case FC_LONG:
4028 case FC_FLOAT:
4029 case FC_ERROR_STATUS_T:
4030 /* pointer_size must be 4 if we got here in these two cases */
4031 case FC_INT3264:
4032 case FC_UINT3264:
4033 alignment = 4;
4034 break;
4036 case FC_HYPER:
4037 case FC_DOUBLE:
4038 alignment = 8;
4039 break;
4041 case FC_IGNORE:
4042 case FC_BIND_PRIMITIVE:
4043 /* no marshalling needed */
4044 return;
4046 default:
4047 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
4048 var->name, get_basic_fc(ref->type));
4051 if (phase == PHASE_MARSHAL && alignment > 1)
4052 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
4053 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
4054 alignment - 1, alignment - 1);
4056 if (phase == PHASE_MARSHAL)
4058 print_file(file, indent, "*(");
4059 write_type_decl(file, ref, NULL);
4060 if (is_ptr(type))
4061 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
4062 else
4063 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4064 fprintf(file, "%s%s", local_var_prefix, varname);
4065 fprintf(file, ";\n");
4067 else if (phase == PHASE_UNMARSHAL)
4069 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4070 write_type_decl(file, ref, NULL);
4071 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4072 print_file(file, indent, "{\n");
4073 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4074 print_file(file, indent, "}\n");
4075 print_file(file, indent, "%s%s%s",
4076 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4077 local_var_prefix, varname);
4078 if (pass == PASS_IN && is_ptr(type))
4079 fprintf(file, " = (");
4080 else
4081 fprintf(file, " = *(");
4082 write_type_decl(file, ref, NULL);
4083 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4086 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4087 write_type_decl(file, ref, NULL);
4088 fprintf(file, ");\n");
4092 /* returns whether the MaxCount, Offset or ActualCount members need to be
4093 * filled in for the specified phase */
4094 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4096 return (phase != PHASE_UNMARSHAL);
4099 expr_t *get_size_is_expr(const type_t *t, const char *name)
4101 expr_t *x = NULL;
4103 for ( ; is_array(t); t = type_array_get_element_type(t))
4104 if (type_array_has_conformance(t) &&
4105 type_array_get_conformance(t)->type != EXPR_VOID)
4107 if (!x)
4108 x = type_array_get_conformance(t);
4109 else
4110 error("%s: multidimensional conformant"
4111 " arrays not supported at the top level\n",
4112 name);
4115 return x;
4118 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4119 enum remoting_phase phase, const var_t *var, int valid_variance)
4121 const type_t *type = var->declspec.type;
4122 /* get fundamental type for the argument */
4123 for (;;)
4125 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4127 case TGT_ARRAY:
4128 if (is_conformance_needed_for_phase(phase))
4130 if (type_array_has_conformance(type) &&
4131 type_array_get_conformance(type)->type != EXPR_VOID)
4133 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4134 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4135 fprintf(file, ";\n\n");
4137 if (type_array_has_variance(type))
4139 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4140 if (valid_variance)
4142 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4143 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4144 fprintf(file, ";\n\n");
4146 else
4147 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4150 break;
4151 case TGT_UNION:
4152 if (type_get_type(type) == TYPE_UNION &&
4153 is_conformance_needed_for_phase(phase))
4155 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4156 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4157 fprintf(file, ";\n\n");
4159 break;
4160 case TGT_IFACE_POINTER:
4162 expr_t *iid;
4164 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4166 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4167 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4168 fprintf( file, ";\n\n" );
4170 break;
4172 case TGT_POINTER:
4173 type = type_pointer_get_ref_type(type);
4174 continue;
4175 case TGT_INVALID:
4176 case TGT_USER_TYPE:
4177 case TGT_CTXT_HANDLE:
4178 case TGT_CTXT_HANDLE_POINTER:
4179 case TGT_STRING:
4180 case TGT_BASIC:
4181 case TGT_ENUM:
4182 case TGT_STRUCT:
4183 case TGT_RANGE:
4184 break;
4186 break;
4190 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4191 enum pass pass, enum remoting_phase phase, const var_t *var)
4193 int in_attr, out_attr, pointer_type;
4194 const char *type_str = NULL;
4195 const type_t *type = var->declspec.type;
4196 unsigned int alignment, start_offset = type->typestring_offset;
4198 if (is_ptr(type) || is_array(type))
4199 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4200 else
4201 pointer_type = 0;
4203 in_attr = is_attr(var->attrs, ATTR_IN);
4204 out_attr = is_attr(var->attrs, ATTR_OUT);
4205 if (!in_attr && !out_attr)
4206 in_attr = 1;
4208 if (phase != PHASE_FREE)
4209 switch (pass)
4211 case PASS_IN:
4212 if (!in_attr) return;
4213 break;
4214 case PASS_OUT:
4215 if (!out_attr) return;
4216 break;
4217 case PASS_RETURN:
4218 break;
4221 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4223 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4225 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4227 case TGT_CTXT_HANDLE:
4228 case TGT_CTXT_HANDLE_POINTER:
4229 if (phase == PHASE_MARSHAL)
4231 if (pass == PASS_IN)
4233 /* if the context_handle attribute appears in the chain of types
4234 * without pointers being followed, then the context handle must
4235 * be direct, otherwise it is a pointer */
4236 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4237 print_file(file, indent, "NdrClientContextMarshall(\n");
4238 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4239 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4240 var->name);
4241 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4243 else
4245 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4246 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4247 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4248 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
4249 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4252 else if (phase == PHASE_UNMARSHAL)
4254 if (pass == PASS_OUT || pass == PASS_RETURN)
4256 if (!in_attr)
4257 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4258 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4259 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4260 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
4261 pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
4262 print_file(file, indent + 1, "__frame->_Handle);\n");
4264 else
4266 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4267 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4268 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4271 break;
4272 case TGT_USER_TYPE:
4273 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4274 break;
4275 case TGT_STRING:
4276 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4277 pointer_type != FC_RP)
4279 /* strings returned are assumed to be global and hence don't
4280 * need freeing */
4281 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4282 print_phase_function(file, indent, "Pointer", local_var_prefix,
4283 phase, var, start_offset);
4284 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4285 !in_attr && is_conformant_array(type))
4287 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4288 indent++;
4289 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4292 else
4294 unsigned int real_start_offset = start_offset;
4295 /* skip over pointer description straight to string description */
4296 if (is_declptr(type))
4298 if (is_conformant_array(type))
4299 real_start_offset += 4;
4300 else
4301 real_start_offset += 2;
4303 if (is_array(type) && !is_conformant_array(type))
4304 print_phase_function(file, indent, "NonConformantString",
4305 local_var_prefix, phase, var,
4306 real_start_offset);
4307 else
4308 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4309 phase, var, real_start_offset);
4311 break;
4312 case TGT_ARRAY:
4314 unsigned char tc = get_array_fc(type);
4315 const char *array_type = NULL;
4317 /* We already have the size_is expression since it's at the
4318 top level, but do checks for multidimensional conformant
4319 arrays. When we handle them, we'll need to extend this
4320 function to return a list, and then we'll actually use
4321 the return value. */
4322 get_size_is_expr(type, var->name);
4324 switch (tc)
4326 case FC_SMFARRAY:
4327 case FC_LGFARRAY:
4328 array_type = "FixedArray";
4329 break;
4330 case FC_SMVARRAY:
4331 case FC_LGVARRAY:
4332 array_type = "VaryingArray";
4333 break;
4334 case FC_CARRAY:
4335 array_type = "ConformantArray";
4336 break;
4337 case FC_CVARRAY:
4338 array_type = "ConformantVaryingArray";
4339 break;
4340 case FC_BOGUS_ARRAY:
4341 array_type = "ComplexArray";
4342 break;
4345 if (pointer_type != FC_RP) array_type = "Pointer";
4347 if (phase == PHASE_FREE && pointer_type == FC_RP)
4349 /* these are all unmarshalled by allocating memory */
4350 if (tc == FC_BOGUS_ARRAY ||
4351 tc == FC_CVARRAY ||
4352 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4353 (tc == FC_CARRAY && !in_attr))
4355 if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
4357 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4358 type_array_get_ptr_tfsoff(type));
4359 break;
4361 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4362 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4363 indent++;
4364 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4365 break;
4368 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4369 break;
4371 case TGT_BASIC:
4372 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4373 break;
4374 case TGT_ENUM:
4375 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4376 break;
4377 case TGT_RANGE:
4378 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4379 /* Note: this goes beyond what MIDL does - it only supports arguments
4380 * with the [range] attribute in Oicf mode */
4381 if (phase == PHASE_UNMARSHAL)
4383 const expr_t *range_min;
4384 const expr_t *range_max;
4385 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4386 if (!range_list)
4387 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4388 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4389 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4391 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4392 write_type_decl(file, &var->declspec, NULL);
4393 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4394 write_type_decl(file, &var->declspec, NULL);
4395 fprintf(file, ")0x%x))\n", range_max->cval);
4396 print_file(file, indent, "{\n");
4397 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4398 print_file(file, indent, "}\n");
4400 break;
4401 case TGT_STRUCT:
4402 switch (get_struct_fc(type))
4404 case FC_STRUCT:
4405 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4406 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4407 break;
4408 case FC_PSTRUCT:
4409 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4410 break;
4411 case FC_CSTRUCT:
4412 case FC_CPSTRUCT:
4413 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4414 break;
4415 case FC_CVSTRUCT:
4416 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4417 break;
4418 case FC_BOGUS_STRUCT:
4419 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4420 break;
4421 default:
4422 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4424 break;
4425 case TGT_UNION:
4427 const char *union_type = NULL;
4429 if (type_get_type(type) == TYPE_UNION)
4430 union_type = "NonEncapsulatedUnion";
4431 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4432 union_type = "EncapsulatedUnion";
4434 print_phase_function(file, indent, union_type, local_var_prefix,
4435 phase, var, start_offset);
4436 break;
4438 case TGT_POINTER:
4440 const type_t *ref = type_pointer_get_ref_type(type);
4441 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4443 case TGT_BASIC:
4444 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4445 break;
4446 case TGT_ENUM:
4447 /* base types have known sizes, so don't need a sizing pass
4448 * and don't have any memory to free and so don't need a
4449 * freeing pass */
4450 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4451 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4452 break;
4453 case TGT_STRUCT:
4454 switch (get_struct_fc(ref))
4456 case FC_STRUCT:
4457 /* simple structs have known sizes, so don't need a sizing
4458 * pass and don't have any memory to free and so don't
4459 * need a freeing pass */
4460 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4461 type_str = "SimpleStruct";
4462 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4464 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4465 indent++;
4466 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4467 indent--;
4469 break;
4470 case FC_PSTRUCT:
4471 type_str = "SimpleStruct";
4472 break;
4473 case FC_CSTRUCT:
4474 case FC_CPSTRUCT:
4475 type_str = "ConformantStruct";
4476 break;
4477 case FC_CVSTRUCT:
4478 type_str = "ConformantVaryingStruct";
4479 break;
4480 case FC_BOGUS_STRUCT:
4481 type_str = "ComplexStruct";
4482 break;
4483 default:
4484 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4487 if (type_str)
4489 if (phase == PHASE_FREE)
4490 type_str = "Pointer";
4491 else
4492 start_offset = ref->typestring_offset;
4493 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4495 break;
4496 case TGT_UNION:
4497 if (phase == PHASE_FREE)
4498 type_str = "Pointer";
4499 else
4501 if (type_get_type(ref) == TYPE_UNION)
4502 type_str = "NonEncapsulatedUnion";
4503 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4504 type_str = "EncapsulatedUnion";
4506 start_offset = ref->typestring_offset;
4509 print_phase_function(file, indent, type_str, local_var_prefix,
4510 phase, var, start_offset);
4511 break;
4512 case TGT_USER_TYPE:
4513 if (phase != PHASE_FREE)
4515 type_str = "UserMarshal";
4516 start_offset = ref->typestring_offset;
4518 else type_str = "Pointer";
4520 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4521 break;
4522 case TGT_STRING:
4523 case TGT_POINTER:
4524 case TGT_ARRAY:
4525 case TGT_RANGE:
4526 case TGT_IFACE_POINTER:
4527 case TGT_CTXT_HANDLE:
4528 case TGT_CTXT_HANDLE_POINTER:
4529 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4530 break;
4531 case TGT_INVALID:
4532 assert(0);
4533 break;
4535 else
4536 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4537 break;
4539 case TGT_IFACE_POINTER:
4540 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4541 break;
4542 case TGT_INVALID:
4543 assert(0);
4544 break;
4546 fprintf(file, "\n");
4549 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4550 enum pass pass, enum remoting_phase phase)
4552 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4554 unsigned int size = get_function_buffer_size( func, pass );
4555 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4558 if (pass == PASS_RETURN)
4560 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4561 type_function_get_retval(func->declspec.type) );
4563 else
4565 const var_t *var;
4566 if (!type_function_get_args(func->declspec.type))
4567 return;
4568 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4569 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4574 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4576 unsigned int offset = 0;
4577 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4578 return offset;
4581 static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
4583 const statement_t *stmt;
4584 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
4586 const var_t *func = stmt->u.var;
4587 if (!is_local(func->attrs))
4588 *size += get_size_procformatstring_func( iface, func );
4592 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4594 unsigned int size = 1;
4595 for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
4596 return size;
4599 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4601 set_all_tfswrite(FALSE);
4602 return process_tfs(NULL, stmts, pred);
4605 void declare_stub_args( FILE *file, int indent, const var_t *func )
4607 int in_attr, out_attr;
4608 int i = 0;
4609 var_t *var = type_function_get_retval(func->declspec.type);
4611 /* declare return value */
4612 if (!is_void(var->declspec.type))
4614 if (is_context_handle(var->declspec.type))
4615 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4616 else
4618 print_file(file, indent, "%s", "");
4619 write_type_decl(file, &var->declspec, var->name);
4620 fprintf(file, ";\n");
4624 if (!type_function_get_args(func->declspec.type))
4625 return;
4627 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
4629 in_attr = is_attr(var->attrs, ATTR_IN);
4630 out_attr = is_attr(var->attrs, ATTR_OUT);
4631 if (!out_attr && !in_attr)
4632 in_attr = 1;
4634 if (is_context_handle(var->declspec.type))
4635 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4636 else
4638 if (!in_attr && !is_conformant_array(var->declspec.type))
4640 const decl_spec_t *type_to_print;
4641 char name[16];
4642 print_file(file, indent, "%s", "");
4643 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4644 !type_array_is_decl_as_ptr(var->declspec.type))
4645 type_to_print = &var->declspec;
4646 else
4647 type_to_print = type_pointer_get_ref(var->declspec.type);
4648 sprintf(name, "_W%u", i++);
4649 write_type_decl(file, type_to_print, name);
4650 fprintf(file, ";\n");
4653 print_file(file, indent, "%s", "");
4654 write_type_decl_left(file, &var->declspec);
4655 fprintf(file, " ");
4656 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4657 !type_array_is_decl_as_ptr(var->declspec.type)) {
4658 fprintf(file, "(*%s)", var->name);
4659 } else
4660 fprintf(file, "%s", var->name);
4661 write_type_right(file, var->declspec.type, FALSE);
4662 fprintf(file, ";\n");
4664 if (decl_indirect(var->declspec.type))
4665 print_file(file, indent, "void *_p_%s;\n", var->name);
4671 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4673 int in_attr, out_attr;
4674 int i = 0, sep = 0;
4675 const var_t *var;
4676 type_t *ref;
4678 if (!type_function_get_args(func->declspec.type))
4679 return;
4681 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4683 in_attr = is_attr(var->attrs, ATTR_IN);
4684 out_attr = is_attr(var->attrs, ATTR_OUT);
4685 if (!out_attr && !in_attr)
4686 in_attr = 1;
4688 if (!in_attr)
4690 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4692 switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
4694 case TGT_CTXT_HANDLE_POINTER:
4695 fprintf(file, " = NdrContextHandleInitialize(\n");
4696 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4697 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4698 var->typestring_offset);
4699 break;
4700 case TGT_ARRAY:
4701 if (type_array_has_conformance(var->declspec.type))
4703 unsigned int size;
4704 type_t *type;
4706 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4707 for (type = var->declspec.type;
4708 is_array(type) && type_array_has_conformance(type);
4709 type = type_array_get_element_type(type))
4711 write_expr(file, type_array_get_conformance(type), TRUE,
4712 TRUE, NULL, NULL, local_var_prefix);
4713 fprintf(file, " * ");
4715 size = type_memsize(type);
4716 fprintf(file, "%u);\n", size);
4718 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4719 for (type = var->declspec.type;
4720 is_array(type) && type_array_has_conformance(type);
4721 type = type_array_get_element_type(type))
4723 write_expr(file, type_array_get_conformance(type), TRUE,
4724 TRUE, NULL, NULL, local_var_prefix);
4725 fprintf(file, " * ");
4727 size = type_memsize(type);
4728 fprintf(file, "%u);\n", size);
4730 else
4731 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4732 break;
4733 case TGT_POINTER:
4734 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4735 ref = type_pointer_get_ref_type(var->declspec.type);
4736 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4738 case TGT_BASIC:
4739 case TGT_ENUM:
4740 case TGT_POINTER:
4741 case TGT_RANGE:
4742 case TGT_IFACE_POINTER:
4743 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4744 break;
4745 case TGT_USER_TYPE:
4746 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4747 local_var_prefix, i, local_var_prefix, i);
4748 break;
4749 case TGT_ARRAY:
4750 if (type_array_is_decl_as_ptr(ref))
4752 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4753 break;
4755 ref = type_array_get_element_type(ref);
4756 /* fall through */
4757 case TGT_STRUCT:
4758 case TGT_UNION:
4759 if (type_has_pointers(ref))
4760 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4761 local_var_prefix, i, local_var_prefix, i);
4762 break;
4763 case TGT_CTXT_HANDLE:
4764 case TGT_CTXT_HANDLE_POINTER:
4765 case TGT_INVALID:
4766 case TGT_STRING:
4767 /* not initialised */
4768 break;
4770 i++;
4771 break;
4772 default:
4773 break;
4776 sep = 1;
4779 if (sep)
4780 fprintf(file, "\n");
4784 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4785 const char *var_decl, int add_retval )
4787 var_t *retval = type_function_get_retval( func );
4788 const var_list_t *args = type_function_get_args( func );
4789 const var_t *arg;
4790 int needs_packing;
4791 unsigned int align = 0;
4793 if (args)
4794 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4795 if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
4797 needs_packing = (align > pointer_size);
4799 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4800 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4801 print_file(file, 1, "{\n" );
4802 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4804 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4806 print_file(file, 2, "%s", "");
4807 write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
4808 if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
4809 if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
4811 /* FIXME: should check for large args being passed by pointer */
4812 align = 0;
4813 if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
4814 else type_memsize_and_alignment( arg->declspec.type, &align );
4816 if (align >= pointer_size)
4817 fprintf( file, "%s;\n", arg->name );
4818 else
4819 fprintf( file, "DECLSPEC_ALIGN(%u) %s;\n", pointer_size, arg->name );
4821 if (add_retval && !is_void( retval->declspec.type ))
4823 print_file(file, 2, "%s", "");
4824 write_type_decl( file, &retval->declspec, retval->name );
4825 if (is_array( retval->declspec.type ) || is_ptr( retval->declspec.type ) ||
4826 type_memsize( retval->declspec.type ) == pointer_size)
4827 fprintf( file, ";\n" );
4828 else
4829 fprintf( file, " DECLSPEC_ALIGN(%u);\n", pointer_size );
4831 print_file(file, 1, "} %s;\n", var_decl );
4832 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4833 print_file( file, 0, "\n" );
4836 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4838 const var_list_t *args = type_function_get_args( func->declspec.type );
4839 const var_t *var;
4841 if (!args) return;
4843 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4844 if (cant_be_null( var ))
4845 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4848 int write_expr_eval_routines(FILE *file, const char *iface)
4850 static const char *var_name = "pS";
4851 static const char *var_name_expr = "pS->";
4852 int result = 0;
4853 struct expr_eval_routine *eval;
4854 unsigned short callback_offset = 0;
4856 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4858 const char *name = eval->name;
4859 result = 1;
4861 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4862 eval->iface ? eval->iface->name : iface, name, callback_offset);
4863 print_file(file, 0, "{\n");
4864 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4866 write_func_param_struct( file, eval->iface, eval->cont_type,
4867 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4869 else
4871 decl_spec_t ds = {.type = (type_t *)eval->cont_type};
4872 print_file(file, 1, "%s", "");
4873 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4874 fprintf(file, " *%s = (", var_name);
4875 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4876 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4878 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4879 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4880 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4881 fprintf(file, ";\n");
4882 print_file(file, 0, "}\n\n");
4883 callback_offset++;
4885 return result;
4888 void write_expr_eval_routine_list(FILE *file, const char *iface)
4890 struct expr_eval_routine *eval;
4891 struct expr_eval_routine *cursor;
4892 unsigned short callback_offset = 0;
4894 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4895 fprintf(file, "{\n");
4897 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4899 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4900 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4901 callback_offset++;
4902 list_remove(&eval->entry);
4903 free(eval->name);
4904 free(eval);
4907 fprintf(file, "};\n\n");
4910 void write_user_quad_list(FILE *file)
4912 user_type_t *ut;
4914 if (list_empty(&user_type_list))
4915 return;
4917 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4918 fprintf(file, "{\n");
4919 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4921 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4922 print_file(file, 1, "{\n");
4923 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4924 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4925 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4926 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4927 print_file(file, 1, "}%s\n", sep);
4929 fprintf(file, "};\n\n");
4932 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4934 const struct str_list_entry_t *endpoint;
4935 const char *p;
4937 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4938 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4939 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4941 print_file( f, 1, "{ (const unsigned char *)\"" );
4942 for (p = endpoint->str; *p && *p != ':'; p++)
4944 if (*p == '"' || *p == '\\') fputc( '\\', f );
4945 fputc( *p, f );
4947 if (!*p) goto error;
4948 if (p[1] != '[') goto error;
4950 fprintf( f, "\", (const unsigned char *)\"" );
4951 for (p += 2; *p && *p != ']'; p++)
4953 if (*p == '"' || *p == '\\') fputc( '\\', f );
4954 fputc( *p, f );
4956 if (*p != ']') goto error;
4957 fprintf( f, "\" },\n" );
4959 print_file( f, 0, "};\n\n" );
4960 return;
4962 error:
4963 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4966 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4967 const char *prefix, unsigned int proc_offset )
4969 const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
4970 int has_ret = !is_void( rettype->type );
4971 const var_list_t *args = type_function_get_args( func->declspec.type );
4972 const var_t *arg;
4973 int len, needs_params = 0;
4975 /* we need a param structure if we have more than one arg */
4976 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4978 print_file( file, 0, "{\n");
4979 if (needs_params)
4981 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4982 write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
4983 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4984 if (args)
4985 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4986 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4988 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4990 len = fprintf( file, " %s%s( ",
4991 has_ret ? "_RetVal = " : "",
4992 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
4993 fprintf( file, "&%s_StubDesc,", prefix );
4994 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
4995 if (needs_params)
4997 fprintf( file, ",\n%*s&__params", len, "" );
4999 else if (pointer_size == 8)
5001 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
5002 if (args)
5003 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5004 fprintf( file, ",\n%*s%s", len, "", arg->name );
5006 else
5008 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
5009 else if (args)
5011 arg = LIST_ENTRY( list_head(args), const var_t, entry );
5012 fprintf( file, ",\n%*s&%s", len, "", arg->name );
5015 fprintf( file, " );\n" );
5016 if (has_ret)
5018 print_file( file, 1, "return (" );
5019 write_type_decl_left(file, rettype);
5020 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
5022 print_file( file, 0, "}\n\n");
5025 void write_exceptions( FILE *file )
5027 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
5028 fprintf( file, "\n");
5029 fprintf( file, "#include \"wine/exception.h\"\n");
5030 fprintf( file, "#undef RpcTryExcept\n");
5031 fprintf( file, "#undef RpcExcept\n");
5032 fprintf( file, "#undef RpcEndExcept\n");
5033 fprintf( file, "#undef RpcTryFinally\n");
5034 fprintf( file, "#undef RpcFinally\n");
5035 fprintf( file, "#undef RpcEndFinally\n");
5036 fprintf( file, "#undef RpcExceptionCode\n");
5037 fprintf( file, "#undef RpcAbnormalTermination\n");
5038 fprintf( file, "\n");
5039 fprintf( file, "struct __exception_frame;\n");
5040 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
5041 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
5042 fprintf( file, "\n");
5043 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5044 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
5045 fprintf( file, " __filter_func filter; \\\n");
5046 fprintf( file, " __finally_func finally; \\\n");
5047 fprintf( file, " __wine_jmp_buf jmp; \\\n");
5048 fprintf( file, " DWORD code; \\\n");
5049 fprintf( file, " unsigned char abnormal_termination; \\\n");
5050 fprintf( file, " unsigned char filter_level; \\\n");
5051 fprintf( file, " unsigned char finally_level;\n");
5052 fprintf( file, "\n");
5053 fprintf( file, "struct __exception_frame\n{\n");
5054 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
5055 fprintf( file, "};\n");
5056 fprintf( file, "\n");
5057 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
5058 fprintf( file, "{\n");
5059 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
5060 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
5061 fprintf( file, " {\n");
5062 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5063 fprintf( file, " exc_frame->finally( exc_frame );\n");
5064 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5065 fprintf( file, " }\n");
5066 fprintf( file, " exc_frame->filter_level = 0;\n");
5067 fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
5068 fprintf( file, "}\n");
5069 fprintf( file, "\n");
5070 fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5071 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5072 fprintf( file, " CONTEXT *context,\n");
5073 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5074 fprintf( file, "{\n");
5075 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5076 fprintf( file, "\n");
5077 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5078 fprintf( file, " {\n" );
5079 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5080 fprintf( file, " {\n" );
5081 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5082 fprintf( file, " exc_frame->finally( exc_frame );\n");
5083 fprintf( file, " }\n" );
5084 fprintf( file, " return ExceptionContinueSearch;\n");
5085 fprintf( file, " }\n" );
5086 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5087 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5088 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5089 fprintf( file, " return ExceptionContinueSearch;\n");
5090 fprintf( file, "}\n");
5091 fprintf( file, "\n");
5092 fprintf( file, "#define RpcTryExcept \\\n");
5093 fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
5094 fprintf( file, " { \\\n");
5095 fprintf( file, " if (!__frame->finally_level) \\\n" );
5096 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5097 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5098 fprintf( file, "\n");
5099 fprintf( file, "#define RpcExcept(expr) \\\n");
5100 fprintf( file, " if (!__frame->finally_level) \\\n" );
5101 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5102 fprintf( file, " __frame->filter_level = 0; \\\n" );
5103 fprintf( file, " } \\\n");
5104 fprintf( file, " else \\\n");
5105 fprintf( file, "\n");
5106 fprintf( file, "#define RpcEndExcept\n");
5107 fprintf( file, "\n");
5108 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5109 fprintf( file, "\n");
5110 fprintf( file, "#define RpcTryFinally \\\n");
5111 fprintf( file, " if (!__frame->filter_level) \\\n");
5112 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5113 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5114 fprintf( file, "\n");
5115 fprintf( file, "#define RpcFinally \\\n");
5116 fprintf( file, " if (!__frame->filter_level) \\\n");
5117 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5118 fprintf( file, " __frame->finally_level = 0;\n");
5119 fprintf( file, "\n");
5120 fprintf( file, "#define RpcEndFinally\n");
5121 fprintf( file, "\n");
5122 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5123 fprintf( file, "\n");
5124 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5125 fprintf( file, " do { \\\n");
5126 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5127 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5128 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5129 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5130 fprintf( file, " __frame->filter_level = 0; \\\n");
5131 fprintf( file, " __frame->finally_level = 0; \\\n");
5132 fprintf( file, " } while (0)\n");
5133 fprintf( file, "\n");
5134 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5135 fprintf( file, "\n");
5136 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5137 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5138 fprintf( file, "\n");
5139 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5140 fprintf( file, " DWORD code;\n");
5141 fprintf( file, "\n");
5142 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");