kernelbase: Don't inherit all the handles in conhost.exe.
[wine.git] / tools / widl / typegen.c
blobb3373ded11dfd41d05caaa5b0d23cd6684014416
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"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <limits.h>
31 #include "widl.h"
32 #include "utils.h"
33 #include "parser.h"
34 #include "header.h"
35 #include "typetree.h"
37 #include "typegen.h"
38 #include "expr.h"
40 /* round size up to multiple of alignment */
41 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
42 /* value to add on to round size up to a multiple of alignment */
43 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
45 static const type_t *current_structure;
46 static const var_t *current_func;
47 static const type_t *current_iface;
49 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
50 struct expr_eval_routine
52 struct list entry;
53 const type_t *iface;
54 const type_t *cont_type;
55 char *name;
56 unsigned int baseoff;
57 const expr_t *expr;
60 enum type_context
62 TYPE_CONTEXT_TOPLEVELPARAM,
63 TYPE_CONTEXT_PARAM,
64 TYPE_CONTEXT_CONTAINER,
65 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
66 TYPE_CONTEXT_RETVAL,
69 /* parameter flags in Oif mode */
70 static const unsigned short MustSize = 0x0001;
71 static const unsigned short MustFree = 0x0002;
72 static const unsigned short IsPipe = 0x0004;
73 static const unsigned short IsIn = 0x0008;
74 static const unsigned short IsOut = 0x0010;
75 static const unsigned short IsReturn = 0x0020;
76 static const unsigned short IsBasetype = 0x0040;
77 static const unsigned short IsByValue = 0x0080;
78 static const unsigned short IsSimpleRef = 0x0100;
79 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
80 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
82 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
83 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
84 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
85 const char *name, unsigned int *typestring_offset);
86 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
87 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
88 const char *name, int write_ptr, unsigned int *tfsoff);
89 static const var_t *find_array_or_string_in_struct(const type_t *type);
90 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
91 type_t *type, enum type_context context,
92 const char *name, unsigned int *typestring_offset);
93 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
94 const attr_list_t *attrs, int toplevel_param,
95 unsigned int *alignment );
96 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
98 static const char *string_of_type(unsigned char type)
100 switch (type)
102 case FC_BYTE: return "FC_BYTE";
103 case FC_CHAR: return "FC_CHAR";
104 case FC_SMALL: return "FC_SMALL";
105 case FC_USMALL: return "FC_USMALL";
106 case FC_WCHAR: return "FC_WCHAR";
107 case FC_SHORT: return "FC_SHORT";
108 case FC_USHORT: return "FC_USHORT";
109 case FC_LONG: return "FC_LONG";
110 case FC_ULONG: return "FC_ULONG";
111 case FC_FLOAT: return "FC_FLOAT";
112 case FC_HYPER: return "FC_HYPER";
113 case FC_DOUBLE: return "FC_DOUBLE";
114 case FC_ENUM16: return "FC_ENUM16";
115 case FC_ENUM32: return "FC_ENUM32";
116 case FC_IGNORE: return "FC_IGNORE";
117 case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
118 case FC_RP: return "FC_RP";
119 case FC_UP: return "FC_UP";
120 case FC_OP: return "FC_OP";
121 case FC_FP: return "FC_FP";
122 case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
123 case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
124 case FC_STRUCT: return "FC_STRUCT";
125 case FC_PSTRUCT: return "FC_PSTRUCT";
126 case FC_CSTRUCT: return "FC_CSTRUCT";
127 case FC_CPSTRUCT: return "FC_CPSTRUCT";
128 case FC_CVSTRUCT: return "FC_CVSTRUCT";
129 case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
130 case FC_SMFARRAY: return "FC_SMFARRAY";
131 case FC_LGFARRAY: return "FC_LGFARRAY";
132 case FC_SMVARRAY: return "FC_SMVARRAY";
133 case FC_LGVARRAY: return "FC_LGVARRAY";
134 case FC_CARRAY: return "FC_CARRAY";
135 case FC_CVARRAY: return "FC_CVARRAY";
136 case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
137 case FC_ALIGNM2: return "FC_ALIGNM2";
138 case FC_ALIGNM4: return "FC_ALIGNM4";
139 case FC_ALIGNM8: return "FC_ALIGNM8";
140 case FC_POINTER: return "FC_POINTER";
141 case FC_C_CSTRING: return "FC_C_CSTRING";
142 case FC_C_WSTRING: return "FC_C_WSTRING";
143 case FC_CSTRING: return "FC_CSTRING";
144 case FC_WSTRING: return "FC_WSTRING";
145 case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
146 case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
147 case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
148 case FC_IP: return "FC_IP";
149 case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
150 case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
151 case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
152 case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
153 case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
154 case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
155 case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
156 case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
157 case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
158 case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
159 case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
160 case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
161 case FC_STRING_SIZED: return "FC_STRING_SIZED";
162 case FC_NO_REPEAT: return "FC_NO_REPEAT";
163 case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
164 case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
165 case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
166 case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
167 case FC_PP: return "FC_PP";
168 case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
169 case FC_DEREFERENCE: return "FC_DEREFERENCE";
170 case FC_DIV_2: return "FC_DIV_2";
171 case FC_MULT_2: return "FC_MULT_2";
172 case FC_ADD_1: return "FC_ADD_1";
173 case FC_SUB_1: return "FC_SUB_1";
174 case FC_CALLBACK: return "FC_CALLBACK";
175 case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
176 case FC_END: return "FC_END";
177 case FC_PAD: return "FC_PAD";
178 case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
179 case FC_RANGE: return "FC_RANGE";
180 case FC_INT3264: return "FC_INT3264";
181 case FC_UINT3264: return "FC_UINT3264";
182 default:
183 error("string_of_type: unknown type 0x%02x\n", type);
184 return NULL;
188 unsigned char get_basic_fc(const type_t *type)
190 int sign = type_basic_get_sign(type);
191 switch (type_basic_get_type(type))
193 case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
194 case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
195 case TYPE_BASIC_INT32:
196 case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
197 case TYPE_BASIC_INT64: return FC_HYPER;
198 case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
199 case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
200 case TYPE_BASIC_BYTE: return FC_BYTE;
201 case TYPE_BASIC_CHAR: return FC_CHAR;
202 case TYPE_BASIC_WCHAR: return FC_WCHAR;
203 case TYPE_BASIC_HYPER: return FC_HYPER;
204 case TYPE_BASIC_FLOAT: return FC_FLOAT;
205 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
206 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
207 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
209 return 0;
212 static unsigned char get_basic_fc_signed(const type_t *type)
214 switch (type_basic_get_type(type))
216 case TYPE_BASIC_INT8: return FC_SMALL;
217 case TYPE_BASIC_INT16: return FC_SHORT;
218 case TYPE_BASIC_INT32: return FC_LONG;
219 case TYPE_BASIC_INT64: return FC_HYPER;
220 case TYPE_BASIC_INT: return FC_LONG;
221 case TYPE_BASIC_INT3264: return FC_INT3264;
222 case TYPE_BASIC_LONG: return FC_LONG;
223 case TYPE_BASIC_BYTE: return FC_BYTE;
224 case TYPE_BASIC_CHAR: return FC_CHAR;
225 case TYPE_BASIC_WCHAR: return FC_WCHAR;
226 case TYPE_BASIC_HYPER: return FC_HYPER;
227 case TYPE_BASIC_FLOAT: return FC_FLOAT;
228 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
229 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
230 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
232 return 0;
235 static inline unsigned int clamp_align(unsigned int align)
237 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
238 if(align > packing) align = packing;
239 return align;
242 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
244 const type_t *t;
245 int pointer_type;
247 assert(is_ptr(type) || is_array(type));
249 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
250 if (pointer_type)
251 return pointer_type;
253 for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
255 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
256 if (pointer_type)
257 return pointer_type;
260 if (toplevel_param)
261 return FC_RP;
263 if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
264 return pointer_type;
266 return FC_UP;
269 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
270 enum type_context context )
272 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
274 if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
275 (context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
276 pointer_fc = FC_OP;
278 return pointer_fc;
281 static unsigned char get_enum_fc(const type_t *type)
283 assert(type_get_type(type) == TYPE_ENUM);
284 if (is_aliaschain_attr(type, ATTR_V1ENUM))
285 return FC_ENUM32;
286 else
287 return FC_ENUM16;
290 static type_t *get_user_type(const type_t *t, const char **pname)
292 for (;;)
294 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
295 if (ut)
297 if (pname)
298 *pname = t->name;
299 return ut;
302 if (type_is_alias(t))
303 t = type_alias_get_aliasee_type(t);
304 else
305 return NULL;
309 static int is_user_type(const type_t *t)
311 return get_user_type(t, NULL) != NULL;
314 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
316 if (is_user_type(type))
317 return TGT_USER_TYPE;
319 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
320 return TGT_CTXT_HANDLE;
322 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
323 return TGT_STRING;
325 switch (type_get_type(type))
327 case TYPE_BASIC:
328 if (!(flags & TDT_IGNORE_RANGES) &&
329 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
330 return TGT_RANGE;
331 return TGT_BASIC;
332 case TYPE_ENUM:
333 if (!(flags & TDT_IGNORE_RANGES) &&
334 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
335 return TGT_RANGE;
336 return TGT_ENUM;
337 case TYPE_POINTER:
338 if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
339 type_get_type(type_pointer_get_ref_type(type)) == TYPE_RUNTIMECLASS ||
340 type_get_type(type_pointer_get_ref_type(type)) == TYPE_DELEGATE ||
341 (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
342 return TGT_IFACE_POINTER;
343 else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
344 return TGT_CTXT_HANDLE_POINTER;
345 else
346 return TGT_POINTER;
347 case TYPE_STRUCT:
348 return TGT_STRUCT;
349 case TYPE_ENCAPSULATED_UNION:
350 case TYPE_UNION:
351 return TGT_UNION;
352 case TYPE_ARRAY:
353 return TGT_ARRAY;
354 case TYPE_FUNCTION:
355 case TYPE_COCLASS:
356 case TYPE_INTERFACE:
357 case TYPE_MODULE:
358 case TYPE_VOID:
359 case TYPE_ALIAS:
360 case TYPE_BITFIELD:
361 case TYPE_RUNTIMECLASS:
362 case TYPE_DELEGATE:
363 break;
364 case TYPE_APICONTRACT:
365 case TYPE_PARAMETERIZED_TYPE:
366 case TYPE_PARAMETER:
367 /* not supposed to be here */
368 assert(0);
369 break;
371 return TGT_INVALID;
374 static int cant_be_null(const var_t *v)
376 switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
378 case TGT_ARRAY:
379 if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
380 /* fall through */
381 case TGT_POINTER:
382 return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
383 case TGT_CTXT_HANDLE_POINTER:
384 return TRUE;
385 default:
386 return 0;
391 static int get_padding(const var_list_t *fields)
393 unsigned short offset = 0;
394 unsigned int salign = 1;
395 const var_t *f;
397 if (!fields)
398 return 0;
400 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
402 type_t *ft = f->declspec.type;
403 unsigned int align = 0;
404 unsigned int size = type_memsize_and_alignment(ft, &align);
405 align = clamp_align(align);
406 if (align > salign) salign = align;
407 offset = ROUND_SIZE(offset, align);
408 offset += size;
411 return ROUNDING(offset, salign);
414 static unsigned int get_stack_size( const var_t *var, int *by_value )
416 unsigned int stack_size;
417 int by_val;
419 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
421 case TGT_BASIC:
422 case TGT_ENUM:
423 case TGT_RANGE:
424 case TGT_STRUCT:
425 case TGT_UNION:
426 case TGT_USER_TYPE:
427 stack_size = type_memsize( var->declspec.type );
428 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
429 break;
430 default:
431 by_val = 0;
432 break;
434 if (!by_val) stack_size = pointer_size;
435 if (by_value) *by_value = by_val;
436 return ROUND_SIZE( stack_size, pointer_size );
439 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
440 const type_t *type, int is_return )
442 unsigned char flags = 0;
443 int is_out;
445 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
447 if (is_ptr(type) &&
448 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
449 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
450 flags |= HANDLE_PARAM_IS_VIA_PTR;
452 if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
454 is_out = is_attr(attrs, ATTR_OUT);
455 if (is_attr(attrs, ATTR_IN) || !is_out)
457 flags |= HANDLE_PARAM_IS_IN;
458 if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
460 if (is_out) flags |= HANDLE_PARAM_IS_OUT;
462 return flags;
465 static unsigned int get_rpc_flags( const attr_list_t *attrs )
467 unsigned int flags = 0;
469 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
470 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
471 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
472 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
473 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
474 return flags;
477 unsigned char get_struct_fc(const type_t *type)
479 int has_pointer = 0;
480 int has_conformance = 0;
481 int has_variance = 0;
482 var_t *field;
483 var_list_t *fields;
485 fields = type_struct_get_fields(type);
487 if (get_padding(fields))
488 return FC_BOGUS_STRUCT;
490 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
492 type_t *t = field->declspec.type;
493 enum typegen_type typegen_type;
495 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
497 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
499 if (is_string_type(field->attrs, field->declspec.type))
501 if (is_conformant_array(t))
502 has_conformance = 1;
503 has_variance = 1;
504 continue;
507 if (is_array(type_array_get_element_type(field->declspec.type)))
508 return FC_BOGUS_STRUCT;
510 if (type_array_has_conformance(field->declspec.type))
512 has_conformance = 1;
513 if (list_next(fields, &field->entry))
514 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
515 field->name);
517 if (type_array_has_variance(t))
518 has_variance = 1;
520 t = type_array_get_element_type(t);
521 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
524 switch (typegen_type)
526 case TGT_USER_TYPE:
527 case TGT_IFACE_POINTER:
528 return FC_BOGUS_STRUCT;
529 case TGT_BASIC:
530 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
531 return FC_BOGUS_STRUCT;
532 break;
533 case TGT_ENUM:
534 if (get_enum_fc(t) == FC_ENUM16)
535 return FC_BOGUS_STRUCT;
536 break;
537 case TGT_POINTER:
538 case TGT_ARRAY:
539 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
540 return FC_BOGUS_STRUCT;
541 has_pointer = 1;
542 break;
543 case TGT_UNION:
544 return FC_BOGUS_STRUCT;
545 case TGT_STRUCT:
547 unsigned char fc = get_struct_fc(t);
548 switch (fc)
550 case FC_STRUCT:
551 break;
552 case FC_CVSTRUCT:
553 has_conformance = 1;
554 has_variance = 1;
555 has_pointer = 1;
556 break;
558 case FC_CPSTRUCT:
559 has_conformance = 1;
560 if (list_next( fields, &field->entry ))
561 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
562 field->name);
563 has_pointer = 1;
564 break;
566 case FC_CSTRUCT:
567 has_conformance = 1;
568 if (list_next( fields, &field->entry ))
569 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
570 field->name);
571 break;
573 case FC_PSTRUCT:
574 has_pointer = 1;
575 break;
577 default:
578 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
579 /* fallthru - treat it as complex */
581 /* as soon as we see one of these these members, it's bogus... */
582 case FC_BOGUS_STRUCT:
583 return FC_BOGUS_STRUCT;
585 break;
587 case TGT_RANGE:
588 return FC_BOGUS_STRUCT;
589 case TGT_STRING:
590 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
591 case TGT_INVALID:
592 case TGT_CTXT_HANDLE:
593 case TGT_CTXT_HANDLE_POINTER:
594 /* checking after parsing should mean that we don't get here. if we do,
595 * it's a checker bug */
596 assert(0);
600 if( has_variance )
602 if ( has_conformance )
603 return FC_CVSTRUCT;
604 else
605 return FC_BOGUS_STRUCT;
607 if( has_conformance && has_pointer )
608 return FC_CPSTRUCT;
609 if( has_conformance )
610 return FC_CSTRUCT;
611 if( has_pointer )
612 return FC_PSTRUCT;
613 return FC_STRUCT;
616 static unsigned char get_array_fc(const type_t *type)
618 unsigned char fc;
619 const expr_t *size_is;
620 const type_t *elem_type;
622 elem_type = type_array_get_element_type(type);
623 size_is = type_array_get_conformance(type);
625 if (!size_is)
627 unsigned int size = type_memsize(elem_type);
628 if (size * type_array_get_dim(type) > 0xffffuL)
629 fc = FC_LGFARRAY;
630 else
631 fc = FC_SMFARRAY;
633 else
634 fc = FC_CARRAY;
636 if (type_array_has_variance(type))
638 if (fc == FC_SMFARRAY)
639 fc = FC_SMVARRAY;
640 else if (fc == FC_LGFARRAY)
641 fc = FC_LGVARRAY;
642 else if (fc == FC_CARRAY)
643 fc = FC_CVARRAY;
646 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
648 case TGT_USER_TYPE:
649 fc = FC_BOGUS_ARRAY;
650 break;
651 case TGT_BASIC:
652 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
653 pointer_size != 4)
654 fc = FC_BOGUS_ARRAY;
655 break;
656 case TGT_STRUCT:
657 switch (get_struct_fc(elem_type))
659 case FC_BOGUS_STRUCT:
660 fc = FC_BOGUS_ARRAY;
661 break;
663 break;
664 case TGT_ENUM:
665 /* is 16-bit enum - if so, wire size differs from mem size and so
666 * the array cannot be block copied, which means the array is complex */
667 if (get_enum_fc(elem_type) == FC_ENUM16)
668 fc = FC_BOGUS_ARRAY;
669 break;
670 case TGT_UNION:
671 case TGT_IFACE_POINTER:
672 fc = FC_BOGUS_ARRAY;
673 break;
674 case TGT_POINTER:
675 /* ref pointers cannot just be block copied. unique pointers to
676 * interfaces need special treatment. either case means the array is
677 * complex */
678 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
679 fc = FC_BOGUS_ARRAY;
680 break;
681 case TGT_RANGE:
682 fc = FC_BOGUS_ARRAY;
683 break;
684 case TGT_CTXT_HANDLE:
685 case TGT_CTXT_HANDLE_POINTER:
686 case TGT_STRING:
687 case TGT_INVALID:
688 case TGT_ARRAY:
689 /* nothing to do for everything else */
690 break;
693 return fc;
696 static int is_non_complex_struct(const type_t *type)
698 return (type_get_type(type) == TYPE_STRUCT &&
699 get_struct_fc(type) != FC_BOGUS_STRUCT);
702 static int type_has_pointers(const type_t *type)
704 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
706 case TGT_USER_TYPE:
707 return FALSE;
708 case TGT_POINTER:
709 return TRUE;
710 case TGT_ARRAY:
711 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
712 case TGT_STRUCT:
714 var_list_t *fields = type_struct_get_fields(type);
715 const var_t *field;
716 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
718 if (type_has_pointers(field->declspec.type))
719 return TRUE;
721 break;
723 case TGT_UNION:
725 var_list_t *fields;
726 const var_t *field;
727 fields = type_union_get_cases(type);
728 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
730 if (field->declspec.type && type_has_pointers(field->declspec.type))
731 return TRUE;
733 break;
735 case TGT_CTXT_HANDLE:
736 case TGT_CTXT_HANDLE_POINTER:
737 case TGT_STRING:
738 case TGT_IFACE_POINTER:
739 case TGT_BASIC:
740 case TGT_ENUM:
741 case TGT_RANGE:
742 case TGT_INVALID:
743 break;
746 return FALSE;
749 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
750 int toplevel_param)
752 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
754 case TGT_USER_TYPE:
755 return FALSE;
756 case TGT_POINTER:
757 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
758 return TRUE;
759 else
760 return FALSE;
761 case TGT_ARRAY:
762 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
763 return TRUE;
764 else
765 return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
766 case TGT_STRUCT:
768 var_list_t *fields = type_struct_get_fields(type);
769 const var_t *field;
770 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
772 if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
773 return TRUE;
775 break;
777 case TGT_UNION:
779 var_list_t *fields;
780 const var_t *field;
781 fields = type_union_get_cases(type);
782 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
784 if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
785 return TRUE;
787 break;
789 case TGT_CTXT_HANDLE:
790 case TGT_CTXT_HANDLE_POINTER:
791 case TGT_STRING:
792 case TGT_IFACE_POINTER:
793 case TGT_BASIC:
794 case TGT_ENUM:
795 case TGT_RANGE:
796 case TGT_INVALID:
797 break;
800 return FALSE;
803 static unsigned short user_type_offset(const char *name)
805 user_type_t *ut;
806 unsigned short off = 0;
807 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
809 if (strcmp(name, ut->name) == 0)
810 return off;
811 ++off;
813 error("user_type_offset: couldn't find type (%s)\n", name);
814 return 0;
817 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
819 type->typestring_offset = offset;
820 if (file) type->tfswrite = FALSE;
823 static void guard_rec(type_t *type)
825 /* types that contain references to themselves (like a linked list),
826 need to be shielded from infinite recursion when writing embedded
827 types */
828 if (type->typestring_offset)
829 type->tfswrite = FALSE;
830 else
831 type->typestring_offset = 1;
834 static int is_embedded_complex(const type_t *type)
836 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
838 case TGT_USER_TYPE:
839 case TGT_STRUCT:
840 case TGT_UNION:
841 case TGT_ARRAY:
842 case TGT_IFACE_POINTER:
843 return TRUE;
844 default:
845 return FALSE;
849 static const char *get_context_handle_type_name(const type_t *type)
851 const type_t *t;
852 for (t = type;
853 is_ptr(t) || type_is_alias(t);
854 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
855 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
856 return t->name;
857 assert(0);
858 return NULL;
861 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
862 do { \
863 if (file) \
864 fprintf(file, "/* %2u */\n", typestring_offset); \
865 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
867 while (0)
869 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
870 static void print_file(FILE *file, int indent, const char *format, ...)
872 va_list va;
873 va_start(va, format);
874 print(file, indent, format, va);
875 va_end(va);
878 void print(FILE *file, int indent, const char *format, va_list va)
880 if (file)
882 if (format[0] != '\n')
883 while (0 < indent--)
884 fprintf(file, " ");
885 vfprintf(file, format, va);
890 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
892 if (decl_indirect(t))
894 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
895 local_var_prefix, n, local_var_prefix, n);
896 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
898 else if (is_ptr(t) || is_array(t))
899 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
902 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
904 const var_t *var = type_function_get_retval(func->declspec.type);
906 if (!is_void(var->declspec.type))
907 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
909 if (!type_function_get_args(func->declspec.type))
910 return;
912 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
913 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
915 fprintf(file, "\n");
918 static void write_formatdesc(FILE *f, int indent, const char *str)
920 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
921 print_file(f, indent, "{\n");
922 print_file(f, indent + 1, "short Pad;\n");
923 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
924 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
925 print_file(f, indent, "\n");
928 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
930 clear_all_offsets();
932 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
933 get_size_typeformatstring(stmts, pred));
935 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
936 get_size_procformatstring(stmts, pred));
938 fprintf(f, "\n");
939 write_formatdesc(f, indent, "TYPE");
940 write_formatdesc(f, indent, "PROC");
941 fprintf(f, "\n");
942 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
943 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
944 print_file(f, indent, "\n");
947 int decl_indirect(const type_t *t)
949 if (is_user_type(t))
950 return TRUE;
951 return (type_get_type(t) != TYPE_BASIC &&
952 type_get_type(t) != TYPE_ENUM &&
953 type_get_type(t) != TYPE_POINTER &&
954 type_get_type(t) != TYPE_ARRAY);
957 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
958 unsigned int *stack_size, unsigned int *typestring_offset )
960 unsigned int alignment, server_size = 0, buffer_size = 0;
961 unsigned char fc = 0;
962 int is_byval;
963 int is_in = is_attr(var->attrs, ATTR_IN);
964 int is_out = is_attr(var->attrs, ATTR_OUT);
966 if (is_return) is_out = TRUE;
967 else if (!is_in && !is_out) is_in = TRUE;
969 *flags = 0;
970 *stack_size = get_stack_size( var, &is_byval );
971 *typestring_offset = var->typestring_offset;
973 if (is_in) *flags |= IsIn;
974 if (is_out) *flags |= IsOut;
975 if (is_return) *flags |= IsReturn;
977 if (!is_string_type( var->attrs, var->declspec.type ))
978 buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
980 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
982 case TGT_BASIC:
983 *flags |= IsBasetype;
984 fc = get_basic_fc_signed( var->declspec.type );
985 if (fc == FC_BIND_PRIMITIVE)
987 buffer_size = 4; /* actually 0 but avoids setting MustSize */
988 fc = FC_LONG;
990 break;
991 case TGT_ENUM:
992 *flags |= IsBasetype;
993 fc = get_enum_fc( var->declspec.type );
994 break;
995 case TGT_RANGE:
996 *flags |= IsByValue;
997 break;
998 case TGT_STRUCT:
999 case TGT_UNION:
1000 case TGT_USER_TYPE:
1001 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1002 break;
1003 case TGT_IFACE_POINTER:
1004 *flags |= MustFree;
1005 break;
1006 case TGT_ARRAY:
1007 *flags |= MustFree;
1008 if (type_array_is_decl_as_ptr(var->declspec.type)
1009 && type_array_get_ptr_tfsoff(var->declspec.type)
1010 && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
1012 *typestring_offset = var->declspec.type->typestring_offset;
1013 *flags |= IsSimpleRef;
1015 break;
1016 case TGT_STRING:
1017 *flags |= MustFree;
1018 if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1020 /* skip over pointer description straight to string description */
1021 if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
1022 else *typestring_offset += 2;
1023 *flags |= IsSimpleRef;
1025 break;
1026 case TGT_CTXT_HANDLE_POINTER:
1027 *flags |= IsSimpleRef;
1028 *typestring_offset += 4;
1029 /* fall through */
1030 case TGT_CTXT_HANDLE:
1031 buffer_size = 20;
1032 break;
1033 case TGT_POINTER:
1034 if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1036 const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
1038 if (!is_string_type( var->attrs, ref ))
1039 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1041 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1043 case TGT_BASIC:
1044 *flags |= IsSimpleRef | IsBasetype;
1045 fc = get_basic_fc( ref );
1046 if (!is_in && is_out) server_size = pointer_size;
1047 break;
1048 case TGT_ENUM:
1049 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1051 *flags |= IsSimpleRef | IsBasetype;
1052 if (!is_in && is_out) server_size = pointer_size;
1054 else
1056 server_size = pointer_size;
1058 break;
1059 case TGT_UNION:
1060 case TGT_USER_TYPE:
1061 case TGT_RANGE:
1062 *flags |= MustFree | IsSimpleRef;
1063 *typestring_offset = ref->typestring_offset;
1064 if (!is_in && is_out) server_size = type_memsize( ref );
1065 break;
1066 case TGT_ARRAY:
1067 *flags |= MustFree;
1068 if (!type_array_is_decl_as_ptr(ref))
1070 *flags |= IsSimpleRef;
1071 *typestring_offset = ref->typestring_offset;
1073 if (!is_in && is_out) server_size = type_memsize( ref );
1074 break;
1075 case TGT_STRING:
1076 case TGT_POINTER:
1077 case TGT_CTXT_HANDLE:
1078 case TGT_CTXT_HANDLE_POINTER:
1079 *flags |= MustFree;
1080 server_size = pointer_size;
1081 break;
1082 case TGT_IFACE_POINTER:
1083 *flags |= MustFree;
1084 if (is_in && is_out) server_size = pointer_size;
1085 break;
1086 case TGT_STRUCT:
1087 *flags |= IsSimpleRef | MustFree;
1088 *typestring_offset = ref->typestring_offset;
1089 switch (get_struct_fc(ref))
1091 case FC_STRUCT:
1092 case FC_PSTRUCT:
1093 case FC_BOGUS_STRUCT:
1094 if (!is_in && is_out) server_size = type_memsize( ref );
1095 break;
1096 default:
1097 break;
1099 break;
1100 case TGT_INVALID:
1101 assert(0);
1104 else /* not ref pointer */
1106 *flags |= MustFree;
1108 break;
1109 case TGT_INVALID:
1110 assert(0);
1113 if (!buffer_size) *flags |= MustSize;
1115 if (server_size)
1117 server_size = (server_size + 7) / 8;
1118 if (server_size < 8) *flags |= server_size << 13;
1120 return fc;
1123 static unsigned char get_func_oi2_flags( const var_t *func )
1125 const var_t *var;
1126 var_list_t *args = type_function_get_args( func->declspec.type );
1127 var_t *retval = type_function_get_retval( func->declspec.type );
1128 unsigned char oi2_flags = 0x40; /* HasExtensions */
1129 unsigned short flags;
1130 unsigned int stack_size, typestring_offset;
1132 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1134 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1135 if (flags & MustSize)
1137 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1138 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1142 if (!is_void( retval->declspec.type ))
1144 oi2_flags |= 0x04; /* HasRet */
1145 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1146 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1148 return oi2_flags;
1151 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1152 int is_return, unsigned int *stack_offset)
1154 char buffer[128];
1155 unsigned int stack_size, typestring_offset;
1156 unsigned short flags;
1157 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1159 strcpy( buffer, "/* flags:" );
1160 if (flags & MustSize) strcat( buffer, " must size," );
1161 if (flags & MustFree) strcat( buffer, " must free," );
1162 if (flags & IsPipe) strcat( buffer, " pipe," );
1163 if (flags & IsIn) strcat( buffer, " in," );
1164 if (flags & IsOut) strcat( buffer, " out," );
1165 if (flags & IsReturn) strcat( buffer, " return," );
1166 if (flags & IsBasetype) strcat( buffer, " base type," );
1167 if (flags & IsByValue) strcat( buffer, " by value," );
1168 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1169 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1170 strcpy( buffer + strlen( buffer ) - 1, " */" );
1171 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1172 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1173 *stack_offset, *stack_offset );
1174 if (flags & IsBasetype)
1176 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1177 print_file( file, indent, "0x0,\n" );
1179 else
1180 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1181 typestring_offset, typestring_offset );
1182 *stack_offset += max( stack_size, pointer_size );
1183 return 6;
1186 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1187 int is_return, int is_interpreted)
1189 unsigned int size;
1191 int is_in = is_attr(var->attrs, ATTR_IN);
1192 int is_out = is_attr(var->attrs, ATTR_OUT);
1194 if (!is_in && !is_out) is_in = TRUE;
1196 if (type_get_type(var->declspec.type) == TYPE_BASIC ||
1197 type_get_type(var->declspec.type) == TYPE_ENUM)
1199 unsigned char fc;
1201 if (is_return)
1202 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1203 else
1204 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1206 if (type_get_type(var->declspec.type) == TYPE_ENUM)
1208 fc = get_enum_fc(var->declspec.type);
1210 else
1212 fc = get_basic_fc_signed(var->declspec.type);
1214 if (fc == FC_BIND_PRIMITIVE)
1215 fc = FC_IGNORE;
1218 print_file(file, indent, "0x%02x, /* %s */\n",
1219 fc, string_of_type(fc));
1220 size = 2; /* includes param type prefix */
1222 else
1224 unsigned short offset = var->typestring_offset;
1226 if (!is_interpreted && is_array(var->declspec.type)
1227 && type_array_is_decl_as_ptr(var->declspec.type)
1228 && type_array_get_ptr_tfsoff(var->declspec.type))
1229 offset = var->declspec.type->typestring_offset;
1231 if (is_return)
1232 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1233 else if (is_in && is_out)
1234 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1235 else if (is_out)
1236 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1237 else
1238 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1240 size = get_stack_size( var, NULL );
1241 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1242 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1243 size = 4; /* includes param type prefix */
1245 return size;
1248 int is_interpreted_func( const type_t *iface, const var_t *func )
1250 const char *str;
1251 const var_t *var;
1252 const var_list_t *args = type_function_get_args( func->declspec.type );
1253 const type_t *ret_type = type_function_get_rettype( func->declspec.type );
1255 if (type_get_type( ret_type ) == TYPE_BASIC)
1257 switch (type_basic_get_type( ret_type ))
1259 case TYPE_BASIC_INT64:
1260 case TYPE_BASIC_HYPER:
1261 /* return value must fit in a long_ptr */
1262 if (pointer_size < 8) return 0;
1263 break;
1264 case TYPE_BASIC_FLOAT:
1265 case TYPE_BASIC_DOUBLE:
1266 /* floating point values can't be returned */
1267 return 0;
1268 default:
1269 break;
1272 if (get_stub_mode() != MODE_Oif && args)
1274 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1275 switch (type_get_type( var->declspec.type ))
1277 case TYPE_BASIC:
1278 switch (type_basic_get_type( var->declspec.type ))
1280 /* floating point arguments are not supported in Oi mode */
1281 case TYPE_BASIC_FLOAT: return 0;
1282 case TYPE_BASIC_DOUBLE: return 0;
1283 default: break;
1285 break;
1286 /* unions passed by value are not supported in Oi mode */
1287 case TYPE_UNION: return 0;
1288 case TYPE_ENCAPSULATED_UNION: return 0;
1289 default: break;
1293 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1294 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1295 return (get_stub_mode() != MODE_Os);
1298 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1299 const var_t *func, unsigned int *offset,
1300 unsigned short num_proc )
1302 var_t *var;
1303 var_list_t *args = type_function_get_args( func->declspec.type );
1304 unsigned char explicit_fc, implicit_fc;
1305 unsigned char handle_flags;
1306 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1307 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1308 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1309 unsigned int nb_args = 0;
1310 unsigned int stack_size = 0;
1311 unsigned short param_num = 0;
1312 unsigned short handle_stack_offset = 0;
1313 unsigned short handle_param_num = 0;
1315 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1316 if (is_object( iface ))
1318 oi_flags |= Oi_OBJECT_PROC;
1319 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1320 stack_size += pointer_size;
1323 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1325 if (var == handle_var)
1327 handle_stack_offset = stack_size;
1328 handle_param_num = param_num;
1330 stack_size += get_stack_size( var, NULL );
1331 param_num++;
1332 nb_args++;
1334 if (!is_void( type_function_get_rettype( func->declspec.type )))
1336 stack_size += pointer_size;
1337 nb_args++;
1340 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1341 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1342 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1343 print_file( file, indent, "0x%02x,\n", oi_flags );
1344 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1345 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1346 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1347 *offset += 10;
1349 if (!implicit_fc)
1351 switch (explicit_fc)
1353 case FC_BIND_PRIMITIVE:
1354 handle_flags = 0;
1355 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1356 print_file( file, indent, "0x%02x,\n", handle_flags );
1357 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1358 handle_stack_offset, handle_stack_offset );
1359 *offset += 4;
1360 break;
1361 case FC_BIND_GENERIC:
1362 handle_flags = type_memsize( handle_var->declspec.type );
1363 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1364 print_file( file, indent, "0x%02x,\n", handle_flags );
1365 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1366 handle_stack_offset, handle_stack_offset );
1367 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
1368 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1369 *offset += 6;
1370 break;
1371 case FC_BIND_CONTEXT:
1372 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
1373 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1374 print_file( file, indent, "0x%02x,\n", handle_flags );
1375 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1376 handle_stack_offset, handle_stack_offset );
1377 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
1378 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1379 *offset += 6;
1380 break;
1384 if (get_stub_mode() == MODE_Oif)
1386 unsigned char oi2_flags = get_func_oi2_flags( func );
1387 unsigned char ext_flags = 0;
1388 unsigned int size;
1390 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1391 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1392 if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
1394 size = get_function_buffer_size( func, PASS_IN );
1395 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1396 size = get_function_buffer_size( func, PASS_OUT );
1397 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1398 print_file( file, indent, "0x%02x,\n", oi2_flags );
1399 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1400 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1401 print_file( file, indent, "0x%02x,\n", ext_flags );
1402 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1403 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1404 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1405 *offset += 14;
1406 if (pointer_size == 8)
1408 unsigned short pos = 0, fpu_mask = 0;
1410 if (is_object( iface )) pos += 2;
1411 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1413 if (type_get_type( var->declspec.type ) == TYPE_BASIC)
1415 switch (type_basic_get_type( var->declspec.type ))
1417 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1418 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1419 default: break;
1422 pos += 2;
1423 if (pos >= 16) break;
1425 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1426 *offset += 2;
1431 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1432 const var_t *func, unsigned int *offset,
1433 unsigned short num_proc )
1435 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1436 int is_interpreted = is_interpreted_func( iface, func );
1437 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1438 var_t *retval = type_function_get_retval( func->declspec.type );
1440 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1442 /* emit argument data */
1443 if (type_function_get_args(func->declspec.type))
1445 const var_t *var;
1446 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1448 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1449 if (is_new_style)
1450 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1451 else
1452 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1456 /* emit return value data */
1457 if (is_void(retval->declspec.type))
1459 if (!is_new_style)
1461 print_file(file, 0, "/* %u (void) */\n", *offset);
1462 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1463 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1464 *offset += 2;
1467 else
1469 print_file( file, 0, "/* %u (return value) */\n", *offset );
1470 if (is_new_style)
1471 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1472 else
1473 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1477 static void for_each_iface(const statement_list_t *stmts,
1478 void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
1479 type_pred_t pred, FILE *file, int indent, unsigned int *offset)
1481 const statement_t *stmt;
1482 type_t *iface;
1484 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1486 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
1487 continue;
1488 iface = stmt->u.type;
1489 if (!pred(iface)) continue;
1490 proc(iface, file, indent, offset);
1491 if (type_iface_get_async_iface(iface))
1492 proc(type_iface_get_async_iface(iface), file, indent, offset);
1496 static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
1498 const statement_t *stmt;
1499 const type_t *parent = type_iface_get_inherit( iface );
1500 int count = parent ? count_methods( parent ) : 0;
1502 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1504 var_t *func = stmt->u.var;
1505 if (is_local(func->attrs)) continue;
1506 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1510 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1512 int indent = 0;
1513 unsigned int offset = 0;
1515 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1516 print_file(file, indent, "{\n");
1517 indent++;
1518 print_file(file, indent, "0,\n");
1519 print_file(file, indent, "{\n");
1520 indent++;
1522 for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
1524 print_file(file, indent, "0x0\n");
1525 indent--;
1526 print_file(file, indent, "}\n");
1527 indent--;
1528 print_file(file, indent, "};\n");
1529 print_file(file, indent, "\n");
1532 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1534 const statement_t *stmt;
1535 int indent = 0;
1537 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1538 iface->name );
1539 print_file( file, indent, "{\n" );
1540 indent++;
1541 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1543 var_t *func = stmt->u.var;
1544 if (is_local( func->attrs )) continue;
1545 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1547 indent--;
1548 print_file( file, indent, "};\n\n" );
1551 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1553 unsigned char fc;
1555 if (type_get_type(type) == TYPE_BASIC)
1556 fc = get_basic_fc_signed(type);
1557 else if (type_get_type(type) == TYPE_ENUM)
1558 fc = get_enum_fc(type);
1559 else
1560 return 0;
1562 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1563 *typestring_offset += 1;
1564 return 1;
1567 /* write conformance / variance descriptor */
1568 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1569 unsigned int baseoff, const type_t *type,
1570 const expr_t *expr)
1572 unsigned char operator_type = 0;
1573 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1574 const char *conftype_string = "field";
1575 const expr_t *subexpr;
1576 const type_t *iface = NULL;
1577 const char *name;
1579 if (!expr)
1581 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1582 return 4;
1585 if (expr->is_const)
1587 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1588 error("write_conf_or_var_desc: constant value %d is greater than "
1589 "the maximum constant size of %d\n", expr->cval,
1590 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1592 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1593 FC_CONSTANT_CONFORMANCE, expr->cval);
1594 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1595 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1597 return 4;
1600 if (!cont_type) /* top-level conformance */
1602 conftype = FC_TOP_LEVEL_CONFORMANCE;
1603 conftype_string = "parameter";
1604 cont_type = current_func->declspec.type;
1605 name = current_func->name;
1606 iface = current_iface;
1608 else
1610 name = cont_type->name;
1611 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1613 conftype = FC_POINTER_CONFORMANCE;
1614 conftype_string = "field pointer";
1618 subexpr = expr;
1619 switch (subexpr->type)
1621 case EXPR_PPTR:
1622 subexpr = subexpr->ref;
1623 operator_type = FC_DEREFERENCE;
1624 break;
1625 case EXPR_DIV:
1626 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1628 subexpr = subexpr->ref;
1629 operator_type = FC_DIV_2;
1631 break;
1632 case EXPR_MUL:
1633 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1635 subexpr = subexpr->ref;
1636 operator_type = FC_MULT_2;
1638 break;
1639 case EXPR_SUB:
1640 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1642 subexpr = subexpr->ref;
1643 operator_type = FC_SUB_1;
1645 break;
1646 case EXPR_ADD:
1647 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1649 subexpr = subexpr->ref;
1650 operator_type = FC_ADD_1;
1652 break;
1653 default:
1654 break;
1657 if (subexpr->type == EXPR_IDENTIFIER)
1659 const type_t *correlation_variable = NULL;
1660 unsigned char param_type = 0;
1661 unsigned int offset = 0;
1662 const var_t *var;
1663 struct expr_loc expr_loc;
1665 if (type_get_type(cont_type) == TYPE_FUNCTION)
1667 var_list_t *args = type_function_get_args( cont_type );
1669 if (is_object( iface )) offset += pointer_size;
1670 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1672 if (var->name && !strcmp(var->name, subexpr->u.sval))
1674 expr_loc.v = var;
1675 correlation_variable = var->declspec.type;
1676 break;
1678 offset += get_stack_size( var, NULL );
1681 else
1683 var_list_t *fields = type_struct_get_fields( cont_type );
1685 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1687 unsigned int size = field_memsize( var->declspec.type, &offset );
1688 if (var->name && !strcmp(var->name, subexpr->u.sval))
1690 expr_loc.v = var;
1691 correlation_variable = var->declspec.type;
1692 break;
1694 offset += size;
1698 if (!correlation_variable)
1699 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1700 expr_loc.attr = NULL;
1701 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1703 offset -= baseoff;
1705 if (type_get_type(correlation_variable) == TYPE_BASIC)
1707 switch (get_basic_fc(correlation_variable))
1709 case FC_CHAR:
1710 case FC_SMALL:
1711 param_type = FC_SMALL;
1712 break;
1713 case FC_BYTE:
1714 case FC_USMALL:
1715 param_type = FC_USMALL;
1716 break;
1717 case FC_WCHAR:
1718 case FC_SHORT:
1719 param_type = FC_SHORT;
1720 break;
1721 case FC_USHORT:
1722 param_type = FC_USHORT;
1723 break;
1724 case FC_LONG:
1725 param_type = FC_LONG;
1726 break;
1727 case FC_ULONG:
1728 param_type = FC_ULONG;
1729 break;
1730 default:
1731 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1732 get_basic_fc(correlation_variable));
1735 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1737 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1738 param_type = FC_LONG;
1739 else
1740 param_type = FC_SHORT;
1742 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1744 if (pointer_size == 8)
1745 param_type = FC_HYPER;
1746 else
1747 param_type = FC_LONG;
1749 else
1751 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1752 subexpr->u.sval);
1753 return 0;
1756 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1757 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1758 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1759 operator_type ? string_of_type(operator_type) : "no operators");
1760 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1761 (unsigned short)offset, offset);
1763 else if (!iface || is_interpreted_func( iface, current_func ))
1765 unsigned int callback_offset = 0;
1766 struct expr_eval_routine *eval;
1767 int found = 0;
1769 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1771 if (eval->cont_type == cont_type ||
1772 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1773 eval->iface == iface &&
1774 eval->name && name && !strcmp(eval->name, name) &&
1775 !compare_expr(eval->expr, expr)))
1777 found = 1;
1778 break;
1780 callback_offset++;
1783 if (!found)
1785 eval = xmalloc (sizeof(*eval));
1786 eval->iface = iface;
1787 eval->cont_type = cont_type;
1788 eval->name = xstrdup( name );
1789 eval->baseoff = baseoff;
1790 eval->expr = expr;
1791 list_add_tail (&expr_eval_routines, &eval->entry);
1794 if (callback_offset > USHRT_MAX)
1795 error("Maximum number of callback routines reached\n");
1797 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1798 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1799 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1801 else /* output a dummy corr desc that isn't used */
1803 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1804 print_file(file, 2, "0x0,\n" );
1805 print_file(file, 2, "NdrFcShort(0x0),\n" );
1807 return 4;
1810 /* return size and start offset of a data field based on current offset */
1811 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1813 unsigned int align = 0;
1814 unsigned int size = type_memsize_and_alignment( type, &align );
1816 *offset = ROUND_SIZE( *offset, align );
1817 return size;
1820 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1822 unsigned int size = 0;
1823 unsigned int max_align;
1824 const var_t *v;
1826 if (!fields) return 0;
1827 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1829 unsigned int falign = 0;
1830 unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
1831 if (*align < falign) *align = falign;
1832 falign = clamp_align(falign);
1833 size = ROUND_SIZE(size, falign);
1834 size += fsize;
1837 max_align = clamp_align(*align);
1838 size = ROUND_SIZE(size, max_align);
1840 return size;
1843 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1845 unsigned int size, maxs = 0;
1846 unsigned int align = *pmaxa;
1847 const var_t *v;
1849 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1851 /* we could have an empty default field with NULL type */
1852 if (v->declspec.type)
1854 size = type_memsize_and_alignment(v->declspec.type, &align);
1855 if (maxs < size) maxs = size;
1856 if (*pmaxa < align) *pmaxa = align;
1860 return maxs;
1863 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1865 unsigned int size = 0;
1867 switch (type_get_type(t))
1869 case TYPE_BASIC:
1870 switch (get_basic_fc(t))
1872 case FC_BYTE:
1873 case FC_CHAR:
1874 case FC_USMALL:
1875 case FC_SMALL:
1876 size = 1;
1877 if (size > *align) *align = size;
1878 break;
1879 case FC_WCHAR:
1880 case FC_USHORT:
1881 case FC_SHORT:
1882 size = 2;
1883 if (size > *align) *align = size;
1884 break;
1885 case FC_ULONG:
1886 case FC_LONG:
1887 case FC_ERROR_STATUS_T:
1888 case FC_FLOAT:
1889 size = 4;
1890 if (size > *align) *align = size;
1891 break;
1892 case FC_HYPER:
1893 case FC_DOUBLE:
1894 size = 8;
1895 if (size > *align) *align = size;
1896 break;
1897 case FC_INT3264:
1898 case FC_UINT3264:
1899 case FC_BIND_PRIMITIVE:
1900 assert( pointer_size );
1901 size = pointer_size;
1902 if (size > *align) *align = size;
1903 break;
1904 default:
1905 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1906 size = 0;
1908 break;
1909 case TYPE_ENUM:
1910 switch (get_enum_fc(t))
1912 case FC_ENUM16:
1913 case FC_ENUM32:
1914 size = 4;
1915 if (size > *align) *align = size;
1916 break;
1917 default:
1918 error("type_memsize: Unknown enum type\n");
1919 size = 0;
1921 break;
1922 case TYPE_STRUCT:
1923 size = fields_memsize(type_struct_get_fields(t), align);
1924 break;
1925 case TYPE_ENCAPSULATED_UNION:
1926 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1927 break;
1928 case TYPE_UNION:
1929 size = union_memsize(type_union_get_cases(t), align);
1930 break;
1931 case TYPE_POINTER:
1932 case TYPE_INTERFACE:
1933 assert( pointer_size );
1934 size = pointer_size;
1935 if (size > *align) *align = size;
1936 break;
1937 case TYPE_ARRAY:
1938 if (!type_array_is_decl_as_ptr(t))
1940 if (is_conformant_array(t))
1942 type_memsize_and_alignment(type_array_get_element_type(t), align);
1943 size = 0;
1945 else
1946 size = type_array_get_dim(t) *
1947 type_memsize_and_alignment(type_array_get_element_type(t), align);
1949 else /* declared as a pointer */
1951 assert( pointer_size );
1952 size = pointer_size;
1953 if (size > *align) *align = size;
1955 break;
1956 case TYPE_ALIAS:
1957 case TYPE_VOID:
1958 case TYPE_COCLASS:
1959 case TYPE_MODULE:
1960 case TYPE_FUNCTION:
1961 case TYPE_BITFIELD:
1962 case TYPE_APICONTRACT:
1963 case TYPE_RUNTIMECLASS:
1964 case TYPE_PARAMETERIZED_TYPE:
1965 case TYPE_PARAMETER:
1966 case TYPE_DELEGATE:
1967 /* these types should not be encountered here due to language
1968 * restrictions (interface, void, coclass, module), logical
1969 * restrictions (alias - due to type_get_type call above) or
1970 * checking restrictions (function, bitfield). */
1971 assert(0);
1974 return size;
1977 unsigned int type_memsize(const type_t *t)
1979 unsigned int align = 0;
1980 return type_memsize_and_alignment( t, &align );
1983 static unsigned int type_buffer_alignment(const type_t *t)
1985 const var_list_t *fields;
1986 const var_t *var;
1987 unsigned int max = 0, align;
1989 switch (type_get_type(t))
1991 case TYPE_BASIC:
1992 switch (get_basic_fc(t))
1994 case FC_BYTE:
1995 case FC_CHAR:
1996 case FC_USMALL:
1997 case FC_SMALL:
1998 return 1;
1999 case FC_WCHAR:
2000 case FC_USHORT:
2001 case FC_SHORT:
2002 return 2;
2003 case FC_ULONG:
2004 case FC_LONG:
2005 case FC_ERROR_STATUS_T:
2006 case FC_FLOAT:
2007 case FC_INT3264:
2008 case FC_UINT3264:
2009 return 4;
2010 case FC_HYPER:
2011 case FC_DOUBLE:
2012 return 8;
2013 default:
2014 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
2016 break;
2017 case TYPE_ENUM:
2018 switch (get_enum_fc(t))
2020 case FC_ENUM16:
2021 return 2;
2022 case FC_ENUM32:
2023 return 4;
2024 default:
2025 error("type_buffer_alignment: Unknown enum type\n");
2027 break;
2028 case TYPE_STRUCT:
2029 if (!(fields = type_struct_get_fields(t))) break;
2030 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2032 if (!var->declspec.type) continue;
2033 align = type_buffer_alignment( var->declspec.type );
2034 if (max < align) max = align;
2036 break;
2037 case TYPE_ENCAPSULATED_UNION:
2038 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2039 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2041 if (!var->declspec.type) continue;
2042 align = type_buffer_alignment( var->declspec.type );
2043 if (max < align) max = align;
2045 break;
2046 case TYPE_UNION:
2047 if (!(fields = type_union_get_cases(t))) break;
2048 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2050 if (!var->declspec.type) continue;
2051 align = type_buffer_alignment( var->declspec.type );
2052 if (max < align) max = align;
2054 break;
2055 case TYPE_ARRAY:
2056 if (!type_array_is_decl_as_ptr(t))
2057 return type_buffer_alignment( type_array_get_element_type(t) );
2058 /* else fall through */
2059 case TYPE_POINTER:
2060 return 4;
2061 case TYPE_INTERFACE:
2062 case TYPE_ALIAS:
2063 case TYPE_VOID:
2064 case TYPE_COCLASS:
2065 case TYPE_MODULE:
2066 case TYPE_FUNCTION:
2067 case TYPE_BITFIELD:
2068 case TYPE_APICONTRACT:
2069 case TYPE_RUNTIMECLASS:
2070 case TYPE_PARAMETERIZED_TYPE:
2071 case TYPE_PARAMETER:
2072 case TYPE_DELEGATE:
2073 /* these types should not be encountered here due to language
2074 * restrictions (interface, void, coclass, module), logical
2075 * restrictions (alias - due to type_get_type call above) or
2076 * checking restrictions (function, bitfield). */
2077 assert(0);
2079 return max;
2082 int is_full_pointer_function(const var_t *func)
2084 const var_t *var;
2085 if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
2086 return TRUE;
2087 if (!type_function_get_args(func->declspec.type))
2088 return FALSE;
2089 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
2090 if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
2091 return TRUE;
2092 return FALSE;
2095 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2097 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2098 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2099 fprintf(file, "\n");
2102 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2104 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2105 fprintf(file, "\n");
2108 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2109 const type_t *type,
2110 enum type_context context,
2111 unsigned int offset,
2112 unsigned int *typeformat_offset)
2114 unsigned int start_offset = *typeformat_offset;
2115 short reloff = offset - (*typeformat_offset + 2);
2116 int in_attr, out_attr;
2117 int pointer_type;
2118 unsigned char flags = 0;
2120 pointer_type = get_pointer_fc_context(type, attrs, context);
2122 in_attr = is_attr(attrs, ATTR_IN);
2123 out_attr = is_attr(attrs, ATTR_OUT);
2124 if (!in_attr && !out_attr) in_attr = 1;
2126 if (!is_interpreted_func(current_iface, current_func))
2128 if (out_attr && !in_attr && pointer_type == FC_RP)
2129 flags |= FC_ALLOCED_ON_STACK;
2131 else if (get_stub_mode() == MODE_Oif)
2133 if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
2135 switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
2137 case TGT_STRING:
2138 case TGT_POINTER:
2139 case TGT_CTXT_HANDLE:
2140 case TGT_CTXT_HANDLE_POINTER:
2141 case TGT_ARRAY:
2142 flags |= FC_ALLOCED_ON_STACK;
2143 break;
2144 case TGT_IFACE_POINTER:
2145 if (in_attr && out_attr)
2146 flags |= FC_ALLOCED_ON_STACK;
2147 break;
2148 default:
2149 break;
2154 if (is_ptr(type))
2156 type_t *ref = type_pointer_get_ref_type(type);
2157 if(is_declptr(ref) && !is_user_type(ref))
2158 flags |= FC_POINTER_DEREF;
2159 if (pointer_type != FC_RP) {
2160 flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
2164 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2165 pointer_type,
2166 flags,
2167 string_of_type(pointer_type));
2168 if (file)
2170 if (flags & FC_ALLOCED_ON_STACK)
2171 fprintf(file, " [allocated_on_stack]");
2172 if (flags & FC_POINTER_DEREF)
2173 fprintf(file, " [pointer_deref]");
2174 if (flags & FC_DONT_FREE)
2175 fprintf(file, " [dont_free]");
2176 if (flags & FC_ALLOCATE_ALL_NODES)
2177 fprintf(file, " [all_nodes]");
2178 fprintf(file, " */\n");
2181 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2182 *typeformat_offset += 4;
2184 return start_offset;
2187 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2188 const type_t *type, enum type_context context)
2190 unsigned char fc;
2191 unsigned char pointer_fc;
2192 const type_t *ref;
2193 int in_attr = is_attr(attrs, ATTR_IN);
2194 int out_attr = is_attr(attrs, ATTR_OUT);
2195 unsigned char flags = FC_SIMPLE_POINTER;
2197 /* for historical reasons, write_simple_pointer also handled string types,
2198 * but no longer does. catch bad uses of the function with this check */
2199 if (is_string_type(attrs, type))
2200 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2202 pointer_fc = get_pointer_fc_context(type, attrs, context);
2204 ref = type_pointer_get_ref_type(type);
2205 if (type_get_type(ref) == TYPE_ENUM)
2206 fc = get_enum_fc(ref);
2207 else
2208 fc = get_basic_fc(ref);
2210 if (!is_interpreted_func(current_iface, current_func))
2212 if (out_attr && !in_attr && pointer_fc == FC_RP)
2213 flags |= FC_ALLOCED_ON_STACK;
2215 else if (get_stub_mode() == MODE_Oif)
2217 if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
2218 flags |= FC_ALLOCED_ON_STACK;
2221 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2222 pointer_fc, flags, string_of_type(pointer_fc),
2223 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2224 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2225 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2226 return 4;
2229 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2231 const decl_spec_t ds = {.type = t};
2232 print_file(file, 0, "/* %u (", tfsoff);
2233 write_type_decl(file, &ds, NULL);
2234 print_file(file, 0, ") */\n");
2237 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2238 type_t *type, unsigned int ref_offset,
2239 enum type_context context,
2240 unsigned int *typestring_offset)
2242 unsigned int offset = *typestring_offset;
2243 type_t *ref = type_pointer_get_ref_type(type);
2245 print_start_tfs_comment(file, type, offset);
2246 update_tfsoff(type, offset, file);
2248 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2250 case TGT_BASIC:
2251 case TGT_ENUM:
2252 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2253 break;
2254 default:
2255 if (ref_offset)
2256 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2257 break;
2260 return offset;
2263 static int processed(const type_t *type)
2265 return type->typestring_offset && !type->tfswrite;
2268 static int user_type_has_variable_size(const type_t *t)
2270 if (is_ptr(t))
2271 return TRUE;
2272 else if (type_get_type(t) == TYPE_STRUCT)
2274 switch (get_struct_fc(t))
2276 case FC_PSTRUCT:
2277 case FC_CSTRUCT:
2278 case FC_CPSTRUCT:
2279 case FC_CVSTRUCT:
2280 return TRUE;
2283 /* Note: Since this only applies to user types, we can't have a conformant
2284 array here, and strings should get filed under pointer in this case. */
2285 return FALSE;
2288 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2290 unsigned int start, absoff, flags;
2291 const char *name = NULL;
2292 type_t *utype = get_user_type(type, &name);
2293 unsigned int usize = type_memsize(utype);
2294 unsigned int ualign = type_buffer_alignment(utype);
2295 unsigned int size = type_memsize(type);
2296 unsigned short funoff = user_type_offset(name);
2297 short reloff;
2299 if (processed(type)) return type->typestring_offset;
2301 guard_rec(type);
2303 if(user_type_has_variable_size(utype)) usize = 0;
2305 if (type_get_type(utype) == TYPE_BASIC ||
2306 type_get_type(utype) == TYPE_ENUM)
2308 unsigned char fc;
2310 if (type_get_type(utype) == TYPE_ENUM)
2311 fc = get_enum_fc(utype);
2312 else
2313 fc = get_basic_fc(utype);
2315 absoff = *tfsoff;
2316 print_start_tfs_comment(file, utype, absoff);
2317 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2318 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2319 *tfsoff += 2;
2321 else
2323 if (!processed(utype))
2324 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2325 absoff = utype->typestring_offset;
2328 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2329 flags = 0x40;
2330 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2331 flags = 0x80;
2332 else
2333 flags = 0;
2335 start = *tfsoff;
2336 update_tfsoff(type, start, file);
2337 print_start_tfs_comment(file, type, start);
2338 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2339 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2340 flags | (ualign - 1), ualign - 1, flags);
2341 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2342 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2343 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2344 *tfsoff += 8;
2345 reloff = absoff - *tfsoff;
2346 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2347 *tfsoff += 2;
2348 return start;
2351 static void write_member_type(FILE *file, const type_t *cont,
2352 int cont_is_complex, const attr_list_t *attrs,
2353 const type_t *type, unsigned int *corroff,
2354 unsigned int *tfsoff)
2356 if (is_embedded_complex(type) && !is_conformant_array(type))
2358 unsigned int absoff;
2359 short reloff;
2361 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2363 absoff = *corroff;
2364 *corroff += 8;
2366 else
2368 absoff = type->typestring_offset;
2370 reloff = absoff - (*tfsoff + 2);
2372 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2373 /* padding is represented using FC_STRUCTPAD* types, so presumably
2374 * this is left over in the format for historical purposes in MIDL
2375 * or rpcrt4. */
2376 print_file(file, 2, "0x0,\n");
2377 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2378 reloff, reloff, absoff);
2379 *tfsoff += 4;
2381 else if (is_ptr(type) || is_conformant_array(type))
2383 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2384 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2385 *tfsoff += 1;
2387 else if (!write_base_type(file, type, tfsoff))
2388 error("Unsupported member type %d\n", type_get_type(type));
2391 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2392 int cont_is_complex, unsigned int *tfsoff)
2394 type_t *elem = type_array_get_element_type(type);
2396 if (!is_embedded_complex(elem) && is_ptr(elem))
2398 type_t *ref = type_pointer_get_ref_type(elem);
2400 if (processed(ref))
2402 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2403 ref->typestring_offset, tfsoff);
2404 return;
2406 if (cont_is_complex && is_string_type(attrs, elem))
2408 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2409 return;
2411 if (!is_string_type(NULL, elem) &&
2412 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2414 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2415 return;
2418 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2421 static void write_end(FILE *file, unsigned int *tfsoff)
2423 if (*tfsoff % 2 == 0)
2425 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2426 *tfsoff += 1;
2428 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2429 *tfsoff += 1;
2432 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2434 unsigned int offset = 0;
2435 var_list_t *fs = type_struct_get_fields(type);
2436 var_t *f;
2438 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2440 type_t *ft = f->declspec.type;
2441 unsigned int size = field_memsize( ft, &offset );
2442 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2444 short reloff;
2445 unsigned int absoff = ft->typestring_offset;
2446 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2447 absoff += 8; /* we already have a corr descr, skip it */
2448 reloff = absoff - (*tfsoff + 6);
2449 print_file(file, 0, "/* %d */\n", *tfsoff);
2450 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2451 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2452 write_conf_or_var_desc(file, current_structure, offset, ft,
2453 get_attrp(f->attrs, ATTR_SWITCHIS));
2454 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2455 (unsigned short)reloff, reloff, absoff);
2456 *tfsoff += 8;
2458 offset += size;
2462 static int write_pointer_description_offsets(
2463 FILE *file, const attr_list_t *attrs, type_t *type,
2464 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2465 unsigned int *typestring_offset)
2467 int written = 0;
2469 if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
2470 (is_array(type) && type_array_is_decl_as_ptr(type)))
2472 if (offset_in_memory && offset_in_buffer)
2474 unsigned int memsize;
2476 /* pointer instance
2478 * note that MSDN states that for pointer layouts in structures,
2479 * this is a negative offset from the end of the structure, but
2480 * this statement is incorrect. all offsets are positive */
2481 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2482 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2484 memsize = type_memsize(type);
2485 *offset_in_memory += memsize;
2486 /* increment these separately as in the case of conformant (varying)
2487 * structures these start at different values */
2488 *offset_in_buffer += memsize;
2490 *typestring_offset += 4;
2492 if (is_ptr(type))
2494 type_t *ref = type_pointer_get_ref_type(type);
2496 if (is_string_type(attrs, type))
2497 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2498 else if (processed(ref))
2499 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2500 ref->typestring_offset, typestring_offset);
2501 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2502 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2503 else
2504 error("write_pointer_description_offsets: type format string unknown\n");
2506 else
2508 unsigned int offset = type->typestring_offset;
2509 /* skip over the pointer that is written for strings, since a
2510 * pointer has to be written in-place here */
2511 if (is_string_type(attrs, type))
2512 offset += 4;
2513 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2516 return 1;
2519 if (is_array(type))
2521 return write_pointer_description_offsets(
2522 file, attrs, type_array_get_element_type(type), offset_in_memory,
2523 offset_in_buffer, typestring_offset);
2525 else if (is_non_complex_struct(type))
2527 /* otherwise search for interesting fields to parse */
2528 const var_t *v;
2529 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2531 if (offset_in_memory && offset_in_buffer)
2533 unsigned int padding;
2534 unsigned int align = 0;
2535 type_memsize_and_alignment(v->declspec.type, &align);
2536 padding = ROUNDING(*offset_in_memory, align);
2537 *offset_in_memory += padding;
2538 *offset_in_buffer += padding;
2540 written += write_pointer_description_offsets(
2541 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2542 typestring_offset);
2545 else
2547 if (offset_in_memory && offset_in_buffer)
2549 unsigned int memsize = type_memsize(type);
2550 *offset_in_memory += memsize;
2551 /* increment these separately as in the case of conformant (varying)
2552 * structures these start at different values */
2553 *offset_in_buffer += memsize;
2557 return written;
2560 static int write_no_repeat_pointer_descriptions(
2561 FILE *file, const attr_list_t *attrs, type_t *type,
2562 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2563 unsigned int *typestring_offset)
2565 int written = 0;
2567 if (is_ptr(type) ||
2568 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2570 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2571 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2572 *typestring_offset += 2;
2574 return write_pointer_description_offsets(file, attrs, type,
2575 offset_in_memory, offset_in_buffer, typestring_offset);
2578 if (is_non_complex_struct(type))
2580 const var_t *v;
2581 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2583 if (offset_in_memory && offset_in_buffer)
2585 unsigned int padding;
2586 unsigned int align = 0;
2587 type_memsize_and_alignment(v->declspec.type, &align);
2588 padding = ROUNDING(*offset_in_memory, align);
2589 *offset_in_memory += padding;
2590 *offset_in_buffer += padding;
2592 written += write_no_repeat_pointer_descriptions(
2593 file, v->attrs, v->declspec.type,
2594 offset_in_memory, offset_in_buffer, typestring_offset);
2597 else
2599 unsigned int memsize = type_memsize(type);
2600 *offset_in_memory += memsize;
2601 /* increment these separately as in the case of conformant (varying)
2602 * structures these start at different values */
2603 *offset_in_buffer += memsize;
2606 return written;
2609 /* Note: if file is NULL return value is number of pointers to write, else
2610 * it is the number of type format characters written */
2611 static int write_fixed_array_pointer_descriptions(
2612 FILE *file, const attr_list_t *attrs, type_t *type,
2613 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2614 unsigned int *typestring_offset)
2616 int pointer_count = 0;
2618 if (type_get_type(type) == TYPE_ARRAY &&
2619 !type_array_has_conformance(type) && !type_array_has_variance(type))
2621 unsigned int temp = 0;
2622 /* unfortunately, this needs to be done in two passes to avoid
2623 * writing out redundant FC_FIXED_REPEAT descriptions */
2624 pointer_count = write_pointer_description_offsets(
2625 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2626 if (pointer_count > 0)
2628 unsigned int increment_size;
2629 unsigned int offset_of_array_pointer_mem = 0;
2630 unsigned int offset_of_array_pointer_buf = 0;
2632 increment_size = type_memsize(type_array_get_element_type(type));
2634 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2635 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2636 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2637 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2638 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2639 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2640 *typestring_offset += 10;
2642 pointer_count = write_pointer_description_offsets(
2643 file, attrs, type, &offset_of_array_pointer_mem,
2644 &offset_of_array_pointer_buf, typestring_offset);
2647 else if (type_get_type(type) == TYPE_STRUCT)
2649 const var_t *v;
2650 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2652 if (offset_in_memory && offset_in_buffer)
2654 unsigned int padding;
2655 unsigned int align = 0;
2656 type_memsize_and_alignment(v->declspec.type, &align);
2657 padding = ROUNDING(*offset_in_memory, align);
2658 *offset_in_memory += padding;
2659 *offset_in_buffer += padding;
2661 pointer_count += write_fixed_array_pointer_descriptions(
2662 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2663 typestring_offset);
2666 else
2668 if (offset_in_memory && offset_in_buffer)
2670 unsigned int memsize;
2671 memsize = type_memsize(type);
2672 *offset_in_memory += memsize;
2673 /* increment these separately as in the case of conformant (varying)
2674 * structures these start at different values */
2675 *offset_in_buffer += memsize;
2679 return pointer_count;
2682 /* Note: if file is NULL return value is number of pointers to write, else
2683 * it is the number of type format characters written */
2684 static int write_conformant_array_pointer_descriptions(
2685 FILE *file, const attr_list_t *attrs, type_t *type,
2686 unsigned int offset_in_memory, unsigned int *typestring_offset)
2688 int pointer_count = 0;
2690 if (is_conformant_array(type) && !type_array_has_variance(type))
2692 unsigned int temp = 0;
2693 /* unfortunately, this needs to be done in two passes to avoid
2694 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2695 pointer_count = write_pointer_description_offsets(
2696 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2697 if (pointer_count > 0)
2699 unsigned int increment_size;
2700 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2701 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2703 increment_size = type_memsize(type_array_get_element_type(type));
2705 if (increment_size > USHRT_MAX)
2706 error("array size of %u bytes is too large\n", increment_size);
2708 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2709 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2710 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2711 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2712 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2713 *typestring_offset += 8;
2715 pointer_count = write_pointer_description_offsets(
2716 file, attrs, type_array_get_element_type(type),
2717 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2718 typestring_offset);
2722 return pointer_count;
2725 /* Note: if file is NULL return value is number of pointers to write, else
2726 * it is the number of type format characters written */
2727 static int write_varying_array_pointer_descriptions(
2728 FILE *file, const attr_list_t *attrs, type_t *type,
2729 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2730 unsigned int *typestring_offset)
2732 int pointer_count = 0;
2734 if (is_array(type) && type_array_has_variance(type))
2736 unsigned int temp = 0;
2737 /* unfortunately, this needs to be done in two passes to avoid
2738 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2739 pointer_count = write_pointer_description_offsets(
2740 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2741 if (pointer_count > 0)
2743 unsigned int increment_size;
2745 increment_size = type_memsize(type_array_get_element_type(type));
2747 if (increment_size > USHRT_MAX)
2748 error("array size of %u bytes is too large\n", increment_size);
2750 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2751 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2752 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2753 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2754 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2755 *typestring_offset += 8;
2757 pointer_count = write_pointer_description_offsets(
2758 file, attrs, type_array_get_element_type(type), offset_in_memory,
2759 offset_in_buffer, typestring_offset);
2762 else if (type_get_type(type) == TYPE_STRUCT)
2764 const var_t *v;
2765 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2767 if (offset_in_memory && offset_in_buffer)
2769 unsigned int align = 0, padding;
2771 if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
2773 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2774 /* skip over variance and offset in buffer */
2775 *offset_in_buffer += 8;
2778 type_memsize_and_alignment(v->declspec.type, &align);
2779 padding = ROUNDING(*offset_in_memory, align);
2780 *offset_in_memory += padding;
2781 *offset_in_buffer += padding;
2783 pointer_count += write_varying_array_pointer_descriptions(
2784 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2785 typestring_offset);
2788 else
2790 if (offset_in_memory && offset_in_buffer)
2792 unsigned int memsize = type_memsize(type);
2793 *offset_in_memory += memsize;
2794 /* increment these separately as in the case of conformant (varying)
2795 * structures these start at different values */
2796 *offset_in_buffer += memsize;
2800 return pointer_count;
2803 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2804 unsigned int *typestring_offset)
2806 unsigned int offset_in_buffer;
2807 unsigned int offset_in_memory;
2809 /* pass 1: search for single instance of a pointer (i.e. don't descend
2810 * into arrays) */
2811 if (!is_array(type))
2813 offset_in_memory = 0;
2814 offset_in_buffer = 0;
2815 write_no_repeat_pointer_descriptions(
2816 file, NULL, type,
2817 &offset_in_memory, &offset_in_buffer, typestring_offset);
2820 /* pass 2: search for pointers in fixed arrays */
2821 offset_in_memory = 0;
2822 offset_in_buffer = 0;
2823 write_fixed_array_pointer_descriptions(
2824 file, NULL, type,
2825 &offset_in_memory, &offset_in_buffer, typestring_offset);
2827 /* pass 3: search for pointers in conformant only arrays (but don't descend
2828 * into conformant varying or varying arrays) */
2829 if (is_conformant_array(type) &&
2830 (type_array_is_decl_as_ptr(type) || !current_structure))
2831 write_conformant_array_pointer_descriptions(
2832 file, attrs, type, 0, typestring_offset);
2833 else if (type_get_type(type) == TYPE_STRUCT &&
2834 get_struct_fc(type) == FC_CPSTRUCT)
2836 type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
2837 write_conformant_array_pointer_descriptions( file, NULL, carray,
2838 type_memsize(type), typestring_offset);
2841 /* pass 4: search for pointers in varying arrays */
2842 offset_in_memory = 0;
2843 offset_in_buffer = 0;
2844 write_varying_array_pointer_descriptions(
2845 file, NULL, type,
2846 &offset_in_memory, &offset_in_buffer, typestring_offset);
2849 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2850 type_t *type, enum type_context context,
2851 const char *name, unsigned int *typestring_offset)
2853 unsigned int start_offset;
2854 unsigned char rtype;
2855 type_t *elem_type;
2856 int is_processed = processed(type);
2858 start_offset = *typestring_offset;
2860 if (is_declptr(type))
2862 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2863 int pointer_type = get_pointer_fc_context(type, attrs, context);
2864 if (!pointer_type)
2865 pointer_type = FC_RP;
2866 print_start_tfs_comment(file, type, *typestring_offset);
2867 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2868 pointer_type, flag, string_of_type(pointer_type),
2869 flag ? " [simple_pointer]" : "");
2870 *typestring_offset += 2;
2871 if (!flag)
2873 print_file(file, 2, "NdrFcShort(0x2),\n");
2874 *typestring_offset += 2;
2876 is_processed = FALSE;
2879 if (is_array(type))
2880 elem_type = type_array_get_element_type(type);
2881 else
2882 elem_type = type_pointer_get_ref_type(type);
2884 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2885 return write_array_tfs(file, attrs, type, name, typestring_offset);
2887 if (type_get_type(elem_type) != TYPE_BASIC)
2889 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2890 return start_offset;
2893 rtype = get_basic_fc(elem_type);
2894 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2896 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2897 return start_offset;
2900 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2902 unsigned int dim = type_array_get_dim(type);
2904 if (is_processed) return start_offset;
2906 /* FIXME: multi-dimensional array */
2907 if (0xffffu < dim)
2908 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2909 name, 0xffffu, dim - 0xffffu);
2911 if (rtype == FC_WCHAR)
2912 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2913 else
2914 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2915 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2916 *typestring_offset += 2;
2918 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2919 *typestring_offset += 2;
2921 update_tfsoff(type, start_offset, file);
2922 return start_offset;
2924 else if (is_conformant_array(type))
2926 if (rtype == FC_WCHAR)
2927 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2928 else
2929 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2930 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2931 *typestring_offset += 2;
2933 *typestring_offset += write_conf_or_var_desc(
2934 file, current_structure,
2935 (!type_array_is_decl_as_ptr(type) && current_structure
2936 ? type_memsize(current_structure)
2937 : 0),
2938 type, type_array_get_conformance(type));
2940 update_tfsoff(type, start_offset, file);
2941 return start_offset;
2943 else
2945 if (is_processed) return start_offset;
2947 if (rtype == FC_WCHAR)
2948 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2949 else
2950 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2951 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2952 *typestring_offset += 2;
2954 update_tfsoff(type, start_offset, file);
2955 return start_offset;
2959 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2960 const char *name, unsigned int *typestring_offset)
2962 const expr_t *length_is = type_array_get_variance(type);
2963 const expr_t *size_is = type_array_get_conformance(type);
2964 unsigned int align;
2965 unsigned int size;
2966 unsigned int start_offset;
2967 unsigned char fc;
2968 unsigned int baseoff
2969 = !type_array_is_decl_as_ptr(type) && current_structure
2970 ? type_memsize(current_structure)
2971 : 0;
2973 if (!is_string_type(attrs, type_array_get_element_type(type)))
2974 write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
2976 size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2977 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2978 fc = get_array_fc(type);
2980 start_offset = *typestring_offset;
2981 update_tfsoff(type, start_offset, file);
2982 print_start_tfs_comment(file, type, start_offset);
2983 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2984 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2985 *typestring_offset += 2;
2987 align = 0;
2988 if (fc != FC_BOGUS_ARRAY)
2990 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
2992 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2993 *typestring_offset += 4;
2995 else
2997 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2998 *typestring_offset += 2;
3001 if (is_conformant_array(type))
3002 *typestring_offset
3003 += write_conf_or_var_desc(file, current_structure, baseoff,
3004 type, size_is);
3006 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
3008 unsigned int elsize = type_memsize(type_array_get_element_type(type));
3009 unsigned int dim = type_array_get_dim(type);
3011 if (fc == FC_LGVARRAY)
3013 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
3014 *typestring_offset += 4;
3016 else
3018 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3019 *typestring_offset += 2;
3022 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
3023 *typestring_offset += 2;
3026 if (length_is)
3027 *typestring_offset
3028 += write_conf_or_var_desc(file, current_structure, baseoff,
3029 type, length_is);
3031 if (type_has_pointers(type_array_get_element_type(type)) &&
3032 (type_array_is_decl_as_ptr(type) || !current_structure))
3034 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3035 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3036 *typestring_offset += 2;
3037 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
3038 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3039 *typestring_offset += 1;
3042 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
3043 write_end(file, typestring_offset);
3045 else
3047 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
3048 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3049 *typestring_offset += 2;
3050 *typestring_offset
3051 += write_conf_or_var_desc(file, current_structure, baseoff,
3052 type, size_is);
3053 *typestring_offset
3054 += write_conf_or_var_desc(file, current_structure, baseoff,
3055 type, length_is);
3057 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
3058 write_end(file, typestring_offset);
3061 return start_offset;
3064 static const var_t *find_array_or_string_in_struct(const type_t *type)
3066 const var_list_t *fields = type_struct_get_fields(type);
3067 const var_t *last_field;
3068 const type_t *ft;
3070 if (!fields || list_empty(fields))
3071 return NULL;
3073 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3074 ft = last_field->declspec.type;
3076 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3077 return last_field;
3079 if (type_get_type(ft) == TYPE_STRUCT)
3080 return find_array_or_string_in_struct(ft);
3081 else
3082 return NULL;
3085 static void write_struct_members(FILE *file, const type_t *type,
3086 int is_complex, unsigned int *corroff,
3087 unsigned int *typestring_offset)
3089 const var_t *field;
3090 unsigned short offset = 0;
3091 unsigned int salign = 1;
3092 int padding;
3093 var_list_t *fields = type_struct_get_fields(type);
3095 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3097 type_t *ft = field->declspec.type;
3098 unsigned int align = 0;
3099 unsigned int size = type_memsize_and_alignment(ft, &align);
3100 align = clamp_align(align);
3101 if (salign < align) salign = align;
3103 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3105 if ((align - 1) & offset)
3107 unsigned char fc = 0;
3108 switch (align)
3110 case 2:
3111 fc = FC_ALIGNM2;
3112 break;
3113 case 4:
3114 fc = FC_ALIGNM4;
3115 break;
3116 case 8:
3117 fc = FC_ALIGNM8;
3118 break;
3119 default:
3120 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3122 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3123 offset = ROUND_SIZE(offset, align);
3124 *typestring_offset += 1;
3126 write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
3127 typestring_offset);
3128 offset += size;
3132 padding = ROUNDING(offset, salign);
3133 if (padding)
3135 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3136 FC_STRUCTPAD1 + padding - 1,
3137 padding);
3138 *typestring_offset += 1;
3141 write_end(file, typestring_offset);
3144 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3145 const char *name, unsigned int *tfsoff)
3147 const type_t *save_current_structure = current_structure;
3148 unsigned int total_size;
3149 const var_t *array;
3150 unsigned int start_offset;
3151 unsigned int align;
3152 unsigned int corroff;
3153 var_t *f;
3154 unsigned char fc = get_struct_fc(type);
3155 var_list_t *fields = type_struct_get_fields(type);
3157 if (processed(type)) return type->typestring_offset;
3159 guard_rec(type);
3160 current_structure = type;
3162 total_size = type_memsize(type);
3163 align = type_buffer_alignment(type);
3164 if (total_size > USHRT_MAX)
3165 error("structure size for %s exceeds %d bytes by %d bytes\n",
3166 name, USHRT_MAX, total_size - USHRT_MAX);
3168 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3169 write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
3171 array = find_array_or_string_in_struct(type);
3172 if (array && !processed(array->declspec.type))
3174 if(is_string_type(array->attrs, array->declspec.type))
3175 write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3176 else
3177 write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
3180 corroff = *tfsoff;
3181 write_descriptors(file, type, tfsoff);
3183 start_offset = *tfsoff;
3184 update_tfsoff(type, start_offset, file);
3185 print_start_tfs_comment(file, type, start_offset);
3186 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3187 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3188 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3189 *tfsoff += 4;
3191 if (array)
3193 unsigned int absoff = array->declspec.type->typestring_offset;
3194 short reloff = absoff - *tfsoff;
3195 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3196 reloff, reloff, absoff);
3197 *tfsoff += 2;
3199 else if (fc == FC_BOGUS_STRUCT)
3201 print_file(file, 2, "NdrFcShort(0x0),\n");
3202 *tfsoff += 2;
3205 if (fc == FC_BOGUS_STRUCT)
3207 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3208 nothing is written to file yet. On the actual writing pass,
3209 this will have been updated. */
3210 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3211 int reloff = absoff - *tfsoff;
3212 assert( reloff >= 0 );
3213 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3214 (unsigned short)reloff, reloff, absoff);
3215 *tfsoff += 2;
3217 else if ((fc == FC_PSTRUCT) ||
3218 (fc == FC_CPSTRUCT) ||
3219 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3221 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3222 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3223 *tfsoff += 2;
3224 write_pointer_description(file, NULL, type, tfsoff);
3225 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3226 *tfsoff += 1;
3229 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3230 tfsoff);
3232 if (fc == FC_BOGUS_STRUCT)
3234 const var_t *f;
3236 type->ptrdesc = *tfsoff;
3237 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3239 type_t *ft = f->declspec.type;
3240 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3242 case TGT_POINTER:
3243 if (is_string_type(f->attrs, ft))
3244 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3245 else
3246 write_pointer_tfs(file, f->attrs, ft,
3247 type_pointer_get_ref_type(ft)->typestring_offset,
3248 TYPE_CONTEXT_CONTAINER, tfsoff);
3249 break;
3250 case TGT_ARRAY:
3251 if (type_array_is_decl_as_ptr(ft))
3253 unsigned int offset;
3255 print_file(file, 0, "/* %d */\n", *tfsoff);
3257 offset = ft->typestring_offset;
3258 /* skip over the pointer that is written for strings, since a
3259 * pointer has to be written in-place here */
3260 if (is_string_type(f->attrs, ft))
3261 offset += 4;
3262 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3264 break;
3265 default:
3266 break;
3269 if (type->ptrdesc == *tfsoff)
3270 type->ptrdesc = 0;
3273 current_structure = save_current_structure;
3274 return start_offset;
3277 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3279 if (t == NULL)
3281 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3283 else
3285 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3287 unsigned char fc;
3288 if (type_get_type(t) == TYPE_BASIC)
3289 fc = get_basic_fc(t);
3290 else
3291 fc = get_enum_fc(t);
3292 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3293 fc, string_of_type(fc));
3295 else if (t->typestring_offset)
3297 short reloff = t->typestring_offset - *tfsoff;
3298 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3299 reloff, reloff, t->typestring_offset);
3301 else
3302 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3305 *tfsoff += 2;
3308 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3309 type_t *type, unsigned int *tfsoff)
3311 unsigned int start_offset;
3312 unsigned int size;
3313 var_list_t *fields;
3314 unsigned int nbranch = 0;
3315 type_t *deftype = NULL;
3316 short nodeftype = 0xffff;
3317 unsigned int dummy;
3318 var_t *f;
3320 if (processed(type) &&
3321 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3322 return type->typestring_offset;
3324 guard_rec(type);
3326 fields = type_union_get_cases(type);
3328 size = union_memsize(fields, &dummy);
3330 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3332 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3333 if (cases)
3334 nbranch += list_count(cases);
3335 if (f->declspec.type)
3336 write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
3339 start_offset = *tfsoff;
3340 update_tfsoff(type, start_offset, file);
3341 print_start_tfs_comment(file, type, start_offset);
3342 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3344 const var_t *sv = type_union_get_switch_value(type);
3345 const type_t *st = sv->declspec.type;
3346 unsigned int align = 0;
3347 unsigned char fc;
3349 if (type_get_type(st) == TYPE_BASIC)
3351 fc = get_basic_fc(st);
3352 switch (fc)
3354 case FC_CHAR:
3355 case FC_SMALL:
3356 case FC_BYTE:
3357 case FC_USMALL:
3358 case FC_WCHAR:
3359 case FC_SHORT:
3360 case FC_USHORT:
3361 case FC_LONG:
3362 case FC_ULONG:
3363 break;
3364 default:
3365 fc = 0;
3366 error("union switch type must be an integer, char, or enum\n");
3369 else if (type_get_type(st) == TYPE_ENUM)
3370 fc = get_enum_fc(st);
3371 else
3372 error("union switch type must be an integer, char, or enum\n");
3374 type_memsize_and_alignment(st, &align);
3375 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3377 if (f->declspec.type)
3378 type_memsize_and_alignment(f->declspec.type, &align);
3381 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3382 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3383 (align << 4) | fc, string_of_type(fc));
3384 *tfsoff += 2;
3386 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3388 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3389 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3390 unsigned char fc;
3392 if (type_get_type(st) == TYPE_BASIC)
3394 fc = get_basic_fc(st);
3395 switch (fc)
3397 case FC_CHAR:
3398 case FC_SMALL:
3399 case FC_USMALL:
3400 case FC_SHORT:
3401 case FC_USHORT:
3402 case FC_LONG:
3403 case FC_ULONG:
3404 case FC_ENUM16:
3405 case FC_ENUM32:
3406 break;
3407 default:
3408 fc = 0;
3409 error("union switch type must be an integer, char, or enum\n");
3412 else if (type_get_type(st) == TYPE_ENUM)
3413 fc = get_enum_fc(st);
3414 else
3415 error("union switch type must be an integer, char, or enum\n");
3417 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3418 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3419 fc, string_of_type(fc));
3420 *tfsoff += 2;
3421 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3422 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3423 *tfsoff += 2;
3424 print_file(file, 0, "/* %u */\n", *tfsoff);
3427 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3428 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3429 *tfsoff += 4;
3431 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3433 type_t *ft = f->declspec.type;
3434 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3435 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3436 expr_t *c;
3438 if (cases == NULL && !deflt)
3439 error("union field %s with neither case nor default attribute\n", f->name);
3441 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3443 /* MIDL doesn't check for duplicate cases, even though that seems
3444 like a reasonable thing to do, it just dumps them to the TFS
3445 like we're going to do here. */
3446 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3447 *tfsoff += 4;
3448 write_branch_type(file, ft, tfsoff);
3451 /* MIDL allows multiple default branches, even though that seems
3452 illogical, it just chooses the last one, which is what we will
3453 do. */
3454 if (deflt)
3456 deftype = ft;
3457 nodeftype = 0;
3461 if (deftype)
3463 write_branch_type(file, deftype, tfsoff);
3465 else
3467 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3468 *tfsoff += 2;
3471 return start_offset;
3474 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3475 unsigned int *typeformat_offset)
3477 unsigned int i;
3478 unsigned int start_offset = *typeformat_offset;
3479 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3481 if (!iid && processed(type)) return type->typestring_offset;
3483 print_start_tfs_comment(file, type, start_offset);
3484 update_tfsoff(type, start_offset, file);
3486 if (iid)
3488 print_file(file, 2, "0x2f, /* FC_IP */\n");
3489 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3490 *typeformat_offset
3491 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3493 else
3495 const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
3496 const struct uuid *uuid = get_attrp(base->attrs, ATTR_UUID);
3498 if (! uuid)
3499 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3501 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3502 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3503 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3504 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3505 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3506 for (i = 0; i < 8; ++i)
3507 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3509 if (file)
3510 fprintf(file, "\n");
3512 *typeformat_offset += 18;
3514 return start_offset;
3517 static unsigned int write_contexthandle_tfs(FILE *file,
3518 const attr_list_t *attrs,
3519 type_t *type,
3520 enum type_context context,
3521 unsigned int *typeformat_offset)
3523 unsigned int start_offset = *typeformat_offset;
3524 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
3526 print_start_tfs_comment(file, type, start_offset);
3528 if (flags & 0x80) /* via ptr */
3530 int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
3531 if (!pointer_type) pointer_type = FC_RP;
3532 *typeformat_offset += 4;
3533 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3534 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3535 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3538 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3539 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3540 if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
3541 print_file(file, 0, "can't be null, ");
3542 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3543 print_file(file, 0, "serialize, ");
3544 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3545 print_file(file, 0, "no serialize, ");
3546 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3547 print_file(file, 0, "strict, ");
3548 if (flags & HANDLE_PARAM_IS_RETURN)
3549 print_file(file, 0, "return, ");
3550 if (flags & HANDLE_PARAM_IS_OUT)
3551 print_file(file, 0, "out, ");
3552 if (flags & HANDLE_PARAM_IS_IN)
3553 print_file(file, 0, "in, ");
3554 if (flags & HANDLE_PARAM_IS_VIA_PTR)
3555 print_file(file, 0, "via ptr, ");
3556 print_file(file, 0, "*/\n");
3557 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3558 print_file(file, 2, "0, /* FIXME: param num */\n");
3559 *typeformat_offset += 4;
3561 update_tfsoff( type, start_offset, file );
3562 return start_offset;
3565 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3566 type_t *type, expr_list_t *range_list,
3567 unsigned int *typeformat_offset)
3569 unsigned char fc;
3570 unsigned int start_offset = *typeformat_offset;
3571 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3572 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3574 if (type_get_type(type) == TYPE_BASIC)
3575 fc = get_basic_fc(type);
3576 else
3577 fc = get_enum_fc(type);
3579 /* fc must fit in lower 4-bits of 8-bit field below */
3580 assert(fc <= 0xf);
3582 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3583 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3584 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3585 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3586 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3587 update_tfsoff( type, start_offset, file );
3588 *typeformat_offset += 10;
3590 return start_offset;
3593 static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
3594 type_t *type, const char *name,
3595 enum type_context context,
3596 unsigned int *typeformat_offset)
3598 unsigned int offset;
3600 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3602 case TGT_CTXT_HANDLE:
3603 case TGT_CTXT_HANDLE_POINTER:
3604 return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
3605 case TGT_USER_TYPE:
3606 return write_user_tfs(file, type, typeformat_offset);
3607 case TGT_STRING:
3608 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3609 case TGT_ARRAY:
3611 unsigned int off;
3612 /* conformant and pointer arrays are handled specially */
3613 if ((context != TYPE_CONTEXT_CONTAINER &&
3614 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3615 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3616 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3617 else
3618 off = 0;
3619 if (context != TYPE_CONTEXT_CONTAINER &&
3620 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3622 int ptr_type;
3623 ptr_type = get_pointer_fc_context(type, attrs, context);
3624 if (type_array_is_decl_as_ptr(type)
3625 || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
3627 unsigned int absoff = type->typestring_offset;
3628 short reloff = absoff - (*typeformat_offset + 2);
3629 off = *typeformat_offset;
3630 print_file(file, 0, "/* %d */\n", off);
3631 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3632 string_of_type(ptr_type));
3633 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3634 reloff, reloff, absoff);
3635 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3636 *typeformat_offset += 4;
3638 type_array_set_ptr_tfsoff(type, off);
3640 return off;
3642 case TGT_STRUCT:
3643 return write_struct_tfs(file, type, name, typeformat_offset);
3644 case TGT_UNION:
3645 return write_union_tfs(file, attrs, type, typeformat_offset);
3646 case TGT_ENUM:
3647 case TGT_BASIC:
3648 /* nothing to do */
3649 return 0;
3650 case TGT_RANGE:
3652 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3653 if (!range_list)
3654 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3655 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3657 case TGT_IFACE_POINTER:
3658 return write_ip_tfs(file, attrs, type, typeformat_offset);
3659 case TGT_POINTER:
3661 enum type_context ref_context;
3662 type_t *ref = type_pointer_get_ref_type(type);
3664 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3665 ref_context = TYPE_CONTEXT_PARAM;
3666 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3667 ref_context = TYPE_CONTEXT_CONTAINER;
3668 else
3669 ref_context = context;
3671 if (is_string_type(attrs, ref))
3673 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3674 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3676 offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
3677 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3678 return 0;
3679 return offset;
3682 offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
3683 ref_context, typeformat_offset);
3684 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3685 return 0;
3686 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3688 case TGT_INVALID:
3689 break;
3691 error("invalid type %s for var %s\n", type->name, name);
3692 return 0;
3695 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3696 const char *name, int write_ptr, unsigned int *tfsoff)
3698 return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3701 static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
3703 const statement_list_t *stmts = type_iface_get_stmts(iface);
3704 const statement_t *stmt;
3705 var_t *var;
3707 current_iface = iface;
3708 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
3710 switch(stmt->type)
3712 case STMT_DECLARATION:
3714 const var_t *func = stmt->u.var;
3716 if(stmt->u.var->declspec.stgclass != STG_NONE
3717 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
3718 continue;
3720 current_func = func;
3721 if (is_local(func->attrs)) continue;
3723 var = type_function_get_retval(func->declspec.type);
3724 if (!is_void(var->declspec.type))
3725 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
3726 TYPE_CONTEXT_RETVAL, offset);
3728 if (type_function_get_args(func->declspec.type))
3729 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
3730 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
3731 TYPE_CONTEXT_TOPLEVELPARAM, offset );
3732 break;
3735 case STMT_TYPEDEF:
3737 typeref_t *ref;
3738 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
3740 if (is_attr(ref->type->attrs, ATTR_ENCODE)
3741 || is_attr(ref->type->attrs, ATTR_DECODE))
3742 ref->type->typestring_offset = write_type_tfs( file,
3743 ref->type->attrs, ref->type, ref->type->name,
3744 TYPE_CONTEXT_CONTAINER, offset);
3746 break;
3748 default:
3749 break;
3754 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3756 unsigned int typeformat_offset = 2;
3757 for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
3758 return typeformat_offset + 1;
3762 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3764 int indent = 0;
3766 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3767 print_file(file, indent, "{\n");
3768 indent++;
3769 print_file(file, indent, "0,\n");
3770 print_file(file, indent, "{\n");
3771 indent++;
3772 print_file(file, indent, "NdrFcShort(0x0),\n");
3774 set_all_tfswrite(TRUE);
3775 process_tfs(file, stmts, pred);
3777 print_file(file, indent, "0x0\n");
3778 indent--;
3779 print_file(file, indent, "}\n");
3780 indent--;
3781 print_file(file, indent, "};\n");
3782 print_file(file, indent, "\n");
3785 static unsigned int get_required_buffer_size_type(
3786 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3788 *alignment = 0;
3789 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3791 case TGT_USER_TYPE:
3793 const char *uname = NULL;
3794 const type_t *utype = get_user_type(type, &uname);
3795 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3797 case TGT_BASIC:
3798 switch (get_basic_fc(type))
3800 case FC_BYTE:
3801 case FC_CHAR:
3802 case FC_USMALL:
3803 case FC_SMALL:
3804 *alignment = 4;
3805 return 1;
3807 case FC_WCHAR:
3808 case FC_USHORT:
3809 case FC_SHORT:
3810 *alignment = 4;
3811 return 2;
3813 case FC_ULONG:
3814 case FC_LONG:
3815 case FC_FLOAT:
3816 case FC_ERROR_STATUS_T:
3817 *alignment = 4;
3818 return 4;
3820 case FC_HYPER:
3821 case FC_DOUBLE:
3822 *alignment = 8;
3823 return 8;
3825 case FC_INT3264:
3826 case FC_UINT3264:
3827 assert( pointer_size );
3828 *alignment = pointer_size;
3829 return pointer_size;
3831 case FC_IGNORE:
3832 case FC_BIND_PRIMITIVE:
3833 return 0;
3835 default:
3836 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3837 get_basic_fc(type));
3838 return 0;
3840 break;
3842 case TGT_ENUM:
3843 switch (get_enum_fc(type))
3845 case FC_ENUM32:
3846 *alignment = 4;
3847 return 4;
3848 case FC_ENUM16:
3849 *alignment = 4;
3850 return 2;
3852 break;
3854 case TGT_STRUCT:
3855 if (get_struct_fc(type) == FC_STRUCT)
3857 if (!type_struct_get_fields(type)) return 0;
3858 return fields_memsize(type_struct_get_fields(type), alignment);
3860 break;
3862 case TGT_POINTER:
3864 unsigned int size, align;
3865 const type_t *ref = type_pointer_get_ref_type(type);
3866 if (is_string_type( attrs, ref )) break;
3867 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3868 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3870 size += 4 + align;
3871 align = 4;
3873 *alignment = align;
3874 return size;
3877 case TGT_ARRAY:
3878 switch (get_array_fc(type))
3880 case FC_SMFARRAY:
3881 case FC_LGFARRAY:
3882 return type_array_get_dim(type) *
3883 get_required_buffer_size_type(type_array_get_element_type(type), name,
3884 NULL, FALSE, alignment);
3886 break;
3888 default:
3889 break;
3891 return 0;
3894 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3896 int in_attr = is_attr(var->attrs, ATTR_IN);
3897 int out_attr = is_attr(var->attrs, ATTR_OUT);
3899 if (!in_attr && !out_attr)
3900 in_attr = 1;
3902 *alignment = 0;
3904 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3905 pass == PASS_RETURN)
3907 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3909 *alignment = 4;
3910 return 20;
3913 if (!is_string_type(var->attrs, var->declspec.type))
3914 return get_required_buffer_size_type(var->declspec.type, var->name,
3915 var->attrs, TRUE, alignment);
3917 return 0;
3920 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3922 const var_t *var;
3923 unsigned int total_size = 0, alignment;
3925 if (type_function_get_args(func->declspec.type))
3927 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
3929 total_size += get_required_buffer_size(var, &alignment, pass);
3930 total_size += alignment;
3934 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
3936 var_t v = *func;
3937 v.declspec.type = type_function_get_rettype(func->declspec.type);
3938 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3939 total_size += alignment;
3941 return total_size;
3944 static void print_phase_function(FILE *file, int indent, const char *type,
3945 const char *local_var_prefix, enum remoting_phase phase,
3946 const var_t *var, unsigned int type_offset)
3948 const char *function;
3949 switch (phase)
3951 case PHASE_BUFFERSIZE:
3952 function = "BufferSize";
3953 break;
3954 case PHASE_MARSHAL:
3955 function = "Marshall";
3956 break;
3957 case PHASE_UNMARSHAL:
3958 function = "Unmarshall";
3959 break;
3960 case PHASE_FREE:
3961 function = "Free";
3962 break;
3963 default:
3964 assert(0);
3965 return;
3968 print_file(file, indent, "Ndr%s%s(\n", type, function);
3969 indent++;
3970 print_file(file, indent, "&__frame->_StubMsg,\n");
3971 print_file(file, indent, "%s%s%s%s%s,\n",
3972 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3973 (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
3974 local_var_prefix,
3975 (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
3976 var->name);
3977 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3978 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3979 if (phase == PHASE_UNMARSHAL)
3980 print_file(file, indent, "0);\n");
3981 indent--;
3984 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3985 enum remoting_phase phase, enum pass pass, const var_t *var,
3986 const char *varname)
3988 type_t *type = var->declspec.type;
3989 unsigned int alignment = 0;
3991 /* no work to do for other phases, buffer sizing is done elsewhere */
3992 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3993 return;
3995 if (type_get_type(type) == TYPE_ENUM ||
3996 (type_get_type(type) == TYPE_BASIC &&
3997 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3998 pointer_size != 4))
4000 unsigned char fc;
4002 if (type_get_type(type) == TYPE_ENUM)
4003 fc = get_enum_fc(type);
4004 else
4005 fc = get_basic_fc(type);
4007 if (phase == PHASE_MARSHAL)
4008 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
4009 else
4010 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
4011 print_file(file, indent+1, "&__frame->_StubMsg,\n");
4012 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
4013 local_var_prefix,
4014 var->name);
4015 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
4017 else
4019 const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
4021 switch (get_basic_fc(ref->type))
4023 case FC_BYTE:
4024 case FC_CHAR:
4025 case FC_SMALL:
4026 case FC_USMALL:
4027 alignment = 1;
4028 break;
4030 case FC_WCHAR:
4031 case FC_USHORT:
4032 case FC_SHORT:
4033 alignment = 2;
4034 break;
4036 case FC_ULONG:
4037 case FC_LONG:
4038 case FC_FLOAT:
4039 case FC_ERROR_STATUS_T:
4040 /* pointer_size must be 4 if we got here in these two cases */
4041 case FC_INT3264:
4042 case FC_UINT3264:
4043 alignment = 4;
4044 break;
4046 case FC_HYPER:
4047 case FC_DOUBLE:
4048 alignment = 8;
4049 break;
4051 case FC_IGNORE:
4052 case FC_BIND_PRIMITIVE:
4053 /* no marshalling needed */
4054 return;
4056 default:
4057 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
4058 var->name, get_basic_fc(ref->type));
4061 if (phase == PHASE_MARSHAL && alignment > 1)
4062 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
4063 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
4064 alignment - 1, alignment - 1);
4066 if (phase == PHASE_MARSHAL)
4068 print_file(file, indent, "*(");
4069 write_type_decl(file, ref, NULL);
4070 if (is_ptr(type))
4071 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
4072 else
4073 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4074 fprintf(file, "%s%s", local_var_prefix, varname);
4075 fprintf(file, ";\n");
4077 else if (phase == PHASE_UNMARSHAL)
4079 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4080 write_type_decl(file, ref, NULL);
4081 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4082 print_file(file, indent, "{\n");
4083 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4084 print_file(file, indent, "}\n");
4085 print_file(file, indent, "%s%s%s",
4086 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4087 local_var_prefix, varname);
4088 if (pass == PASS_IN && is_ptr(type))
4089 fprintf(file, " = (");
4090 else
4091 fprintf(file, " = *(");
4092 write_type_decl(file, ref, NULL);
4093 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4096 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4097 write_type_decl(file, ref, NULL);
4098 fprintf(file, ");\n");
4102 /* returns whether the MaxCount, Offset or ActualCount members need to be
4103 * filled in for the specified phase */
4104 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4106 return (phase != PHASE_UNMARSHAL);
4109 expr_t *get_size_is_expr(const type_t *t, const char *name)
4111 expr_t *x = NULL;
4113 for ( ; is_array(t); t = type_array_get_element_type(t))
4114 if (type_array_has_conformance(t) &&
4115 type_array_get_conformance(t)->type != EXPR_VOID)
4117 if (!x)
4118 x = type_array_get_conformance(t);
4119 else
4120 error("%s: multidimensional conformant"
4121 " arrays not supported at the top level\n",
4122 name);
4125 return x;
4128 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4129 enum remoting_phase phase, const var_t *var, int valid_variance)
4131 const type_t *type = var->declspec.type;
4132 /* get fundamental type for the argument */
4133 for (;;)
4135 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4137 case TGT_ARRAY:
4138 if (is_conformance_needed_for_phase(phase))
4140 if (type_array_has_conformance(type) &&
4141 type_array_get_conformance(type)->type != EXPR_VOID)
4143 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4144 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4145 fprintf(file, ";\n\n");
4147 if (type_array_has_variance(type))
4149 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4150 if (valid_variance)
4152 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4153 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4154 fprintf(file, ";\n\n");
4156 else
4157 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4160 break;
4161 case TGT_UNION:
4162 if (type_get_type(type) == TYPE_UNION &&
4163 is_conformance_needed_for_phase(phase))
4165 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4166 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4167 fprintf(file, ";\n\n");
4169 break;
4170 case TGT_IFACE_POINTER:
4172 expr_t *iid;
4174 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4176 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4177 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4178 fprintf( file, ";\n\n" );
4180 break;
4182 case TGT_POINTER:
4183 type = type_pointer_get_ref_type(type);
4184 continue;
4185 case TGT_INVALID:
4186 case TGT_USER_TYPE:
4187 case TGT_CTXT_HANDLE:
4188 case TGT_CTXT_HANDLE_POINTER:
4189 case TGT_STRING:
4190 case TGT_BASIC:
4191 case TGT_ENUM:
4192 case TGT_STRUCT:
4193 case TGT_RANGE:
4194 break;
4196 break;
4200 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4201 enum pass pass, enum remoting_phase phase, const var_t *var)
4203 int in_attr, out_attr, pointer_type;
4204 const char *type_str = NULL;
4205 const type_t *type = var->declspec.type;
4206 unsigned int alignment, start_offset = type->typestring_offset;
4208 if (is_ptr(type) || is_array(type))
4209 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4210 else
4211 pointer_type = 0;
4213 in_attr = is_attr(var->attrs, ATTR_IN);
4214 out_attr = is_attr(var->attrs, ATTR_OUT);
4215 if (!in_attr && !out_attr)
4216 in_attr = 1;
4218 if (phase != PHASE_FREE)
4219 switch (pass)
4221 case PASS_IN:
4222 if (!in_attr) return;
4223 break;
4224 case PASS_OUT:
4225 if (!out_attr) return;
4226 break;
4227 case PASS_RETURN:
4228 break;
4231 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4233 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4235 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4237 case TGT_CTXT_HANDLE:
4238 case TGT_CTXT_HANDLE_POINTER:
4239 if (phase == PHASE_MARSHAL)
4241 if (pass == PASS_IN)
4243 /* if the context_handle attribute appears in the chain of types
4244 * without pointers being followed, then the context handle must
4245 * be direct, otherwise it is a pointer */
4246 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4247 print_file(file, indent, "NdrClientContextMarshall(\n");
4248 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4249 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4250 var->name);
4251 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4253 else
4255 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4256 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4257 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4258 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
4259 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4262 else if (phase == PHASE_UNMARSHAL)
4264 if (pass == PASS_OUT || pass == PASS_RETURN)
4266 if (!in_attr)
4267 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4268 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4269 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4270 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
4271 pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
4272 print_file(file, indent + 1, "__frame->_Handle);\n");
4274 else
4276 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4277 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4278 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4281 break;
4282 case TGT_USER_TYPE:
4283 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4284 break;
4285 case TGT_STRING:
4286 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4287 pointer_type != FC_RP)
4289 /* strings returned are assumed to be global and hence don't
4290 * need freeing */
4291 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4292 print_phase_function(file, indent, "Pointer", local_var_prefix,
4293 phase, var, start_offset);
4294 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4295 !in_attr && is_conformant_array(type))
4297 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4298 indent++;
4299 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4302 else
4304 unsigned int real_start_offset = start_offset;
4305 /* skip over pointer description straight to string description */
4306 if (is_declptr(type))
4308 if (is_conformant_array(type))
4309 real_start_offset += 4;
4310 else
4311 real_start_offset += 2;
4313 if (is_array(type) && !is_conformant_array(type))
4314 print_phase_function(file, indent, "NonConformantString",
4315 local_var_prefix, phase, var,
4316 real_start_offset);
4317 else
4318 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4319 phase, var, real_start_offset);
4321 break;
4322 case TGT_ARRAY:
4324 unsigned char tc = get_array_fc(type);
4325 const char *array_type = NULL;
4327 /* We already have the size_is expression since it's at the
4328 top level, but do checks for multidimensional conformant
4329 arrays. When we handle them, we'll need to extend this
4330 function to return a list, and then we'll actually use
4331 the return value. */
4332 get_size_is_expr(type, var->name);
4334 switch (tc)
4336 case FC_SMFARRAY:
4337 case FC_LGFARRAY:
4338 array_type = "FixedArray";
4339 break;
4340 case FC_SMVARRAY:
4341 case FC_LGVARRAY:
4342 array_type = "VaryingArray";
4343 break;
4344 case FC_CARRAY:
4345 array_type = "ConformantArray";
4346 break;
4347 case FC_CVARRAY:
4348 array_type = "ConformantVaryingArray";
4349 break;
4350 case FC_BOGUS_ARRAY:
4351 array_type = "ComplexArray";
4352 break;
4355 if (pointer_type != FC_RP) array_type = "Pointer";
4357 if (phase == PHASE_FREE && pointer_type == FC_RP)
4359 /* these are all unmarshalled by allocating memory */
4360 if (tc == FC_BOGUS_ARRAY ||
4361 tc == FC_CVARRAY ||
4362 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4363 (tc == FC_CARRAY && !in_attr))
4365 if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
4367 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4368 type_array_get_ptr_tfsoff(type));
4369 break;
4371 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4372 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4373 indent++;
4374 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4375 break;
4378 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4379 break;
4381 case TGT_BASIC:
4382 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4383 break;
4384 case TGT_ENUM:
4385 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4386 break;
4387 case TGT_RANGE:
4388 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4389 /* Note: this goes beyond what MIDL does - it only supports arguments
4390 * with the [range] attribute in Oicf mode */
4391 if (phase == PHASE_UNMARSHAL)
4393 const expr_t *range_min;
4394 const expr_t *range_max;
4395 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4396 if (!range_list)
4397 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4398 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4399 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4401 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4402 write_type_decl(file, &var->declspec, NULL);
4403 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4404 write_type_decl(file, &var->declspec, NULL);
4405 fprintf(file, ")0x%x))\n", range_max->cval);
4406 print_file(file, indent, "{\n");
4407 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4408 print_file(file, indent, "}\n");
4410 break;
4411 case TGT_STRUCT:
4412 switch (get_struct_fc(type))
4414 case FC_STRUCT:
4415 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4416 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4417 break;
4418 case FC_PSTRUCT:
4419 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4420 break;
4421 case FC_CSTRUCT:
4422 case FC_CPSTRUCT:
4423 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4424 break;
4425 case FC_CVSTRUCT:
4426 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4427 break;
4428 case FC_BOGUS_STRUCT:
4429 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4430 break;
4431 default:
4432 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4434 break;
4435 case TGT_UNION:
4437 const char *union_type = NULL;
4439 if (type_get_type(type) == TYPE_UNION)
4440 union_type = "NonEncapsulatedUnion";
4441 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4442 union_type = "EncapsulatedUnion";
4444 print_phase_function(file, indent, union_type, local_var_prefix,
4445 phase, var, start_offset);
4446 break;
4448 case TGT_POINTER:
4450 const type_t *ref = type_pointer_get_ref_type(type);
4451 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4453 case TGT_BASIC:
4454 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4455 break;
4456 case TGT_ENUM:
4457 /* base types have known sizes, so don't need a sizing pass
4458 * and don't have any memory to free and so don't need a
4459 * freeing pass */
4460 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4461 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4462 break;
4463 case TGT_STRUCT:
4464 switch (get_struct_fc(ref))
4466 case FC_STRUCT:
4467 /* simple structs have known sizes, so don't need a sizing
4468 * pass and don't have any memory to free and so don't
4469 * need a freeing pass */
4470 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4471 type_str = "SimpleStruct";
4472 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4474 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4475 indent++;
4476 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4477 indent--;
4479 break;
4480 case FC_PSTRUCT:
4481 type_str = "SimpleStruct";
4482 break;
4483 case FC_CSTRUCT:
4484 case FC_CPSTRUCT:
4485 type_str = "ConformantStruct";
4486 break;
4487 case FC_CVSTRUCT:
4488 type_str = "ConformantVaryingStruct";
4489 break;
4490 case FC_BOGUS_STRUCT:
4491 type_str = "ComplexStruct";
4492 break;
4493 default:
4494 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4497 if (type_str)
4499 if (phase == PHASE_FREE)
4500 type_str = "Pointer";
4501 else
4502 start_offset = ref->typestring_offset;
4503 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4505 break;
4506 case TGT_UNION:
4507 if (phase == PHASE_FREE)
4508 type_str = "Pointer";
4509 else
4511 if (type_get_type(ref) == TYPE_UNION)
4512 type_str = "NonEncapsulatedUnion";
4513 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4514 type_str = "EncapsulatedUnion";
4516 start_offset = ref->typestring_offset;
4519 print_phase_function(file, indent, type_str, local_var_prefix,
4520 phase, var, start_offset);
4521 break;
4522 case TGT_USER_TYPE:
4523 if (phase != PHASE_FREE)
4525 type_str = "UserMarshal";
4526 start_offset = ref->typestring_offset;
4528 else type_str = "Pointer";
4530 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4531 break;
4532 case TGT_STRING:
4533 case TGT_POINTER:
4534 case TGT_ARRAY:
4535 case TGT_RANGE:
4536 case TGT_IFACE_POINTER:
4537 case TGT_CTXT_HANDLE:
4538 case TGT_CTXT_HANDLE_POINTER:
4539 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4540 break;
4541 case TGT_INVALID:
4542 assert(0);
4543 break;
4545 else
4546 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4547 break;
4549 case TGT_IFACE_POINTER:
4550 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4551 break;
4552 case TGT_INVALID:
4553 assert(0);
4554 break;
4556 fprintf(file, "\n");
4559 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4560 enum pass pass, enum remoting_phase phase)
4562 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4564 unsigned int size = get_function_buffer_size( func, pass );
4565 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4568 if (pass == PASS_RETURN)
4570 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4571 type_function_get_retval(func->declspec.type) );
4573 else
4575 const var_t *var;
4576 if (!type_function_get_args(func->declspec.type))
4577 return;
4578 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4579 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4584 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4586 unsigned int offset = 0;
4587 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4588 return offset;
4591 static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
4593 const statement_t *stmt;
4594 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
4596 const var_t *func = stmt->u.var;
4597 if (!is_local(func->attrs))
4598 *size += get_size_procformatstring_func( iface, func );
4602 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4604 unsigned int size = 1;
4605 for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
4606 return size;
4609 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4611 set_all_tfswrite(FALSE);
4612 return process_tfs(NULL, stmts, pred);
4615 void declare_stub_args( FILE *file, int indent, const var_t *func )
4617 int in_attr, out_attr;
4618 int i = 0;
4619 var_t *var = type_function_get_retval(func->declspec.type);
4621 /* declare return value */
4622 if (!is_void(var->declspec.type))
4624 if (is_context_handle(var->declspec.type))
4625 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4626 else
4628 print_file(file, indent, "%s", "");
4629 write_type_decl(file, &var->declspec, var->name);
4630 fprintf(file, ";\n");
4634 if (!type_function_get_args(func->declspec.type))
4635 return;
4637 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
4639 in_attr = is_attr(var->attrs, ATTR_IN);
4640 out_attr = is_attr(var->attrs, ATTR_OUT);
4641 if (!out_attr && !in_attr)
4642 in_attr = 1;
4644 if (is_context_handle(var->declspec.type))
4645 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4646 else
4648 if (!in_attr && !is_conformant_array(var->declspec.type))
4650 const decl_spec_t *type_to_print;
4651 char name[16];
4652 print_file(file, indent, "%s", "");
4653 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4654 !type_array_is_decl_as_ptr(var->declspec.type))
4655 type_to_print = &var->declspec;
4656 else
4657 type_to_print = type_pointer_get_ref(var->declspec.type);
4658 sprintf(name, "_W%u", i++);
4659 write_type_decl(file, type_to_print, name);
4660 fprintf(file, ";\n");
4663 print_file(file, indent, "%s", "");
4664 write_type_decl_left(file, &var->declspec);
4665 fprintf(file, " ");
4666 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4667 !type_array_is_decl_as_ptr(var->declspec.type)) {
4668 fprintf(file, "(*%s)", var->name);
4669 } else
4670 fprintf(file, "%s", var->name);
4671 write_type_right(file, var->declspec.type, FALSE);
4672 fprintf(file, ";\n");
4674 if (decl_indirect(var->declspec.type))
4675 print_file(file, indent, "void *_p_%s;\n", var->name);
4681 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4683 int in_attr, out_attr;
4684 int i = 0, sep = 0;
4685 const var_t *var;
4686 type_t *ref;
4688 if (!type_function_get_args(func->declspec.type))
4689 return;
4691 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4693 in_attr = is_attr(var->attrs, ATTR_IN);
4694 out_attr = is_attr(var->attrs, ATTR_OUT);
4695 if (!out_attr && !in_attr)
4696 in_attr = 1;
4698 if (!in_attr)
4700 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4702 switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
4704 case TGT_CTXT_HANDLE_POINTER:
4705 fprintf(file, " = NdrContextHandleInitialize(\n");
4706 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4707 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4708 var->typestring_offset);
4709 break;
4710 case TGT_ARRAY:
4711 if (type_array_has_conformance(var->declspec.type))
4713 unsigned int size;
4714 type_t *type;
4716 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4717 for (type = var->declspec.type;
4718 is_array(type) && type_array_has_conformance(type);
4719 type = type_array_get_element_type(type))
4721 write_expr(file, type_array_get_conformance(type), TRUE,
4722 TRUE, NULL, NULL, local_var_prefix);
4723 fprintf(file, " * ");
4725 size = type_memsize(type);
4726 fprintf(file, "%u);\n", size);
4728 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4729 for (type = var->declspec.type;
4730 is_array(type) && type_array_has_conformance(type);
4731 type = type_array_get_element_type(type))
4733 write_expr(file, type_array_get_conformance(type), TRUE,
4734 TRUE, NULL, NULL, local_var_prefix);
4735 fprintf(file, " * ");
4737 size = type_memsize(type);
4738 fprintf(file, "%u);\n", size);
4740 else
4741 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4742 break;
4743 case TGT_POINTER:
4744 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4745 ref = type_pointer_get_ref_type(var->declspec.type);
4746 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4748 case TGT_BASIC:
4749 case TGT_ENUM:
4750 case TGT_POINTER:
4751 case TGT_RANGE:
4752 case TGT_IFACE_POINTER:
4753 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4754 break;
4755 case TGT_USER_TYPE:
4756 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4757 local_var_prefix, i, local_var_prefix, i);
4758 break;
4759 case TGT_ARRAY:
4760 if (type_array_is_decl_as_ptr(ref))
4762 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4763 break;
4765 ref = type_array_get_element_type(ref);
4766 /* fall through */
4767 case TGT_STRUCT:
4768 case TGT_UNION:
4769 if (type_has_pointers(ref))
4770 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4771 local_var_prefix, i, local_var_prefix, i);
4772 break;
4773 case TGT_CTXT_HANDLE:
4774 case TGT_CTXT_HANDLE_POINTER:
4775 case TGT_INVALID:
4776 case TGT_STRING:
4777 /* not initialised */
4778 break;
4780 i++;
4781 break;
4782 default:
4783 break;
4786 sep = 1;
4789 if (sep)
4790 fprintf(file, "\n");
4794 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4795 const char *var_decl, int add_retval )
4797 var_t *retval = type_function_get_retval( func );
4798 const var_list_t *args = type_function_get_args( func );
4799 const var_t *arg;
4800 int needs_packing;
4801 unsigned int align = 0;
4803 if (args)
4804 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4805 if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
4807 needs_packing = (align > pointer_size);
4809 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4810 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4811 print_file(file, 1, "{\n" );
4812 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4814 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4816 print_file(file, 2, "%s", "");
4817 write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
4818 if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
4819 if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
4821 /* FIXME: should check for large args being passed by pointer */
4822 align = 0;
4823 if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
4824 else type_memsize_and_alignment( arg->declspec.type, &align );
4826 if (align < pointer_size)
4827 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4828 fprintf( file, "%s;\n", arg->name );
4830 if (add_retval && !is_void( retval->declspec.type ))
4832 print_file(file, 2, "%s", "");
4833 write_type_left( file, &retval->declspec, NAME_DEFAULT, TRUE, TRUE );
4834 if (needs_space_after( retval->declspec.type )) fputc( ' ', file );
4835 if (!is_array( retval->declspec.type ) && !is_ptr( retval->declspec.type ) &&
4836 type_memsize( retval->declspec.type ) != pointer_size)
4838 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4840 fprintf( file, "%s;\n", retval->name );
4842 print_file(file, 1, "} %s;\n", var_decl );
4843 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4844 print_file( file, 0, "\n" );
4847 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4849 const var_list_t *args = type_function_get_args( func->declspec.type );
4850 const var_t *var;
4852 if (!args) return;
4854 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4855 if (cant_be_null( var ))
4856 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4859 int write_expr_eval_routines(FILE *file, const char *iface)
4861 static const char *var_name = "pS";
4862 static const char *var_name_expr = "pS->";
4863 int result = 0;
4864 struct expr_eval_routine *eval;
4865 unsigned short callback_offset = 0;
4867 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4869 const char *name = eval->name;
4870 result = 1;
4872 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4873 eval->iface ? eval->iface->name : iface, name, callback_offset);
4874 print_file(file, 0, "{\n");
4875 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4877 write_func_param_struct( file, eval->iface, eval->cont_type,
4878 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4880 else
4882 decl_spec_t ds = {.type = (type_t *)eval->cont_type};
4883 print_file(file, 1, "%s", "");
4884 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4885 fprintf(file, " *%s = (", var_name);
4886 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4887 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4889 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4890 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4891 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4892 fprintf(file, ";\n");
4893 print_file(file, 0, "}\n\n");
4894 callback_offset++;
4896 return result;
4899 void write_expr_eval_routine_list(FILE *file, const char *iface)
4901 struct expr_eval_routine *eval;
4902 struct expr_eval_routine *cursor;
4903 unsigned short callback_offset = 0;
4905 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4906 fprintf(file, "{\n");
4908 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4910 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4911 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4912 callback_offset++;
4913 list_remove(&eval->entry);
4914 free(eval->name);
4915 free(eval);
4918 fprintf(file, "};\n\n");
4921 void write_user_quad_list(FILE *file)
4923 user_type_t *ut;
4925 if (list_empty(&user_type_list))
4926 return;
4928 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4929 fprintf(file, "{\n");
4930 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4932 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4933 print_file(file, 1, "{\n");
4934 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4935 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4936 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4937 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4938 print_file(file, 1, "}%s\n", sep);
4940 fprintf(file, "};\n\n");
4943 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4945 const struct str_list_entry_t *endpoint;
4946 const char *p;
4948 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4949 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4950 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4952 print_file( f, 1, "{ (const unsigned char *)\"" );
4953 for (p = endpoint->str; *p && *p != ':'; p++)
4955 if (*p == '"' || *p == '\\') fputc( '\\', f );
4956 fputc( *p, f );
4958 if (!*p) goto error;
4959 if (p[1] != '[') goto error;
4961 fprintf( f, "\", (const unsigned char *)\"" );
4962 for (p += 2; *p && *p != ']'; p++)
4964 if (*p == '"' || *p == '\\') fputc( '\\', f );
4965 fputc( *p, f );
4967 if (*p != ']') goto error;
4968 fprintf( f, "\" },\n" );
4970 print_file( f, 0, "};\n\n" );
4971 return;
4973 error:
4974 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4977 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4978 const char *prefix, unsigned int proc_offset )
4980 const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
4981 int has_ret = !is_void( rettype->type );
4982 const var_list_t *args = type_function_get_args( func->declspec.type );
4983 const var_t *arg;
4984 int len, needs_params = 0;
4986 /* we need a param structure if we have more than one arg */
4987 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4989 print_file( file, 0, "{\n");
4990 if (needs_params)
4992 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4993 write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
4994 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4995 if (args)
4996 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4997 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4999 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
5001 len = fprintf( file, " %s%s( ",
5002 has_ret ? "_RetVal = " : "",
5003 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
5004 fprintf( file, "&%s_StubDesc,", prefix );
5005 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
5006 if (needs_params)
5008 fprintf( file, ",\n%*s&__params", len, "" );
5010 else if (pointer_size == 8)
5012 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
5013 if (args)
5014 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5015 fprintf( file, ",\n%*s%s", len, "", arg->name );
5017 else
5019 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
5020 else if (args)
5022 arg = LIST_ENTRY( list_head(args), const var_t, entry );
5023 fprintf( file, ",\n%*s&%s", len, "", arg->name );
5026 fprintf( file, " );\n" );
5027 if (has_ret)
5029 print_file( file, 1, "return (" );
5030 write_type_decl_left(file, rettype);
5031 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
5033 print_file( file, 0, "}\n\n");
5036 void write_exceptions( FILE *file )
5038 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
5039 fprintf( file, "\n");
5040 fprintf( file, "#include \"wine/exception.h\"\n");
5041 fprintf( file, "#undef RpcTryExcept\n");
5042 fprintf( file, "#undef RpcExcept\n");
5043 fprintf( file, "#undef RpcEndExcept\n");
5044 fprintf( file, "#undef RpcTryFinally\n");
5045 fprintf( file, "#undef RpcFinally\n");
5046 fprintf( file, "#undef RpcEndFinally\n");
5047 fprintf( file, "#undef RpcExceptionCode\n");
5048 fprintf( file, "#undef RpcAbnormalTermination\n");
5049 fprintf( file, "\n");
5050 fprintf( file, "struct __exception_frame;\n");
5051 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
5052 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
5053 fprintf( file, "\n");
5054 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5055 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
5056 fprintf( file, " __filter_func filter; \\\n");
5057 fprintf( file, " __finally_func finally; \\\n");
5058 fprintf( file, " __wine_jmp_buf jmp; \\\n");
5059 fprintf( file, " DWORD code; \\\n");
5060 fprintf( file, " unsigned char abnormal_termination; \\\n");
5061 fprintf( file, " unsigned char filter_level; \\\n");
5062 fprintf( file, " unsigned char finally_level;\n");
5063 fprintf( file, "\n");
5064 fprintf( file, "struct __exception_frame\n{\n");
5065 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
5066 fprintf( file, "};\n");
5067 fprintf( file, "\n");
5068 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
5069 fprintf( file, "{\n");
5070 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
5071 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
5072 fprintf( file, " {\n");
5073 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5074 fprintf( file, " exc_frame->finally( exc_frame );\n");
5075 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5076 fprintf( file, " }\n");
5077 fprintf( file, " exc_frame->filter_level = 0;\n");
5078 fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
5079 fprintf( file, "}\n");
5080 fprintf( file, "\n");
5081 fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5082 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5083 fprintf( file, " CONTEXT *context,\n");
5084 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5085 fprintf( file, "{\n");
5086 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5087 fprintf( file, "\n");
5088 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5089 fprintf( file, " {\n" );
5090 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5091 fprintf( file, " {\n" );
5092 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5093 fprintf( file, " exc_frame->finally( exc_frame );\n");
5094 fprintf( file, " }\n" );
5095 fprintf( file, " return ExceptionContinueSearch;\n");
5096 fprintf( file, " }\n" );
5097 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5098 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5099 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5100 fprintf( file, " return ExceptionContinueSearch;\n");
5101 fprintf( file, "}\n");
5102 fprintf( file, "\n");
5103 fprintf( file, "#define RpcTryExcept \\\n");
5104 fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
5105 fprintf( file, " { \\\n");
5106 fprintf( file, " if (!__frame->finally_level) \\\n" );
5107 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5108 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5109 fprintf( file, "\n");
5110 fprintf( file, "#define RpcExcept(expr) \\\n");
5111 fprintf( file, " if (!__frame->finally_level) \\\n" );
5112 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5113 fprintf( file, " __frame->filter_level = 0; \\\n" );
5114 fprintf( file, " } \\\n");
5115 fprintf( file, " else \\\n");
5116 fprintf( file, "\n");
5117 fprintf( file, "#define RpcEndExcept\n");
5118 fprintf( file, "\n");
5119 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5120 fprintf( file, "\n");
5121 fprintf( file, "#define RpcTryFinally \\\n");
5122 fprintf( file, " if (!__frame->filter_level) \\\n");
5123 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5124 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5125 fprintf( file, "\n");
5126 fprintf( file, "#define RpcFinally \\\n");
5127 fprintf( file, " if (!__frame->filter_level) \\\n");
5128 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5129 fprintf( file, " __frame->finally_level = 0;\n");
5130 fprintf( file, "\n");
5131 fprintf( file, "#define RpcEndFinally\n");
5132 fprintf( file, "\n");
5133 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5134 fprintf( file, "\n");
5135 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5136 fprintf( file, " do { \\\n");
5137 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5138 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5139 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5140 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5141 fprintf( file, " __frame->filter_level = 0; \\\n");
5142 fprintf( file, " __frame->finally_level = 0; \\\n");
5143 fprintf( file, " } while (0)\n");
5144 fprintf( file, "\n");
5145 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5146 fprintf( file, "\n");
5147 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5148 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5149 fprintf( file, "\n");
5150 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5151 fprintf( file, " DWORD code;\n");
5152 fprintf( file, "\n");
5153 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");