msvcp: Fix some spec file discrepancies.
[wine.git] / tools / widl / typegen.c
blob5d0f24be06f2c51f416b5bcc5bc79e6a38ffe2a4
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const type_t *current_structure;
50 static const var_t *current_func;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *iface;
58 const type_t *cont_type;
59 char *name;
60 unsigned int baseoff;
61 const expr_t *expr;
64 enum type_context
66 TYPE_CONTEXT_TOPLEVELPARAM,
67 TYPE_CONTEXT_PARAM,
68 TYPE_CONTEXT_CONTAINER,
69 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
70 TYPE_CONTEXT_RETVAL,
73 /* parameter flags in Oif mode */
74 static const unsigned short MustSize = 0x0001;
75 static const unsigned short MustFree = 0x0002;
76 static const unsigned short IsPipe = 0x0004;
77 static const unsigned short IsIn = 0x0008;
78 static const unsigned short IsOut = 0x0010;
79 static const unsigned short IsReturn = 0x0020;
80 static const unsigned short IsBasetype = 0x0040;
81 static const unsigned short IsByValue = 0x0080;
82 static const unsigned short IsSimpleRef = 0x0100;
83 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
84 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
86 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
87 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
88 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
89 const char *name, unsigned int *typestring_offset);
90 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
91 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
92 const char *name, int write_ptr, unsigned int *tfsoff);
93 static const var_t *find_array_or_string_in_struct(const type_t *type);
94 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
95 type_t *type, enum type_context context,
96 const char *name, unsigned int *typestring_offset);
97 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
98 const attr_list_t *attrs, int toplevel_param,
99 unsigned int *alignment );
100 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
102 static const char *string_of_type(unsigned char type)
104 switch (type)
106 case FC_BYTE: return "FC_BYTE";
107 case FC_CHAR: return "FC_CHAR";
108 case FC_SMALL: return "FC_SMALL";
109 case FC_USMALL: return "FC_USMALL";
110 case FC_WCHAR: return "FC_WCHAR";
111 case FC_SHORT: return "FC_SHORT";
112 case FC_USHORT: return "FC_USHORT";
113 case FC_LONG: return "FC_LONG";
114 case FC_ULONG: return "FC_ULONG";
115 case FC_FLOAT: return "FC_FLOAT";
116 case FC_HYPER: return "FC_HYPER";
117 case FC_DOUBLE: return "FC_DOUBLE";
118 case FC_ENUM16: return "FC_ENUM16";
119 case FC_ENUM32: return "FC_ENUM32";
120 case FC_IGNORE: return "FC_IGNORE";
121 case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
122 case FC_RP: return "FC_RP";
123 case FC_UP: return "FC_UP";
124 case FC_OP: return "FC_OP";
125 case FC_FP: return "FC_FP";
126 case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
127 case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
128 case FC_STRUCT: return "FC_STRUCT";
129 case FC_PSTRUCT: return "FC_PSTRUCT";
130 case FC_CSTRUCT: return "FC_CSTRUCT";
131 case FC_CPSTRUCT: return "FC_CPSTRUCT";
132 case FC_CVSTRUCT: return "FC_CVSTRUCT";
133 case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
134 case FC_SMFARRAY: return "FC_SMFARRAY";
135 case FC_LGFARRAY: return "FC_LGFARRAY";
136 case FC_SMVARRAY: return "FC_SMVARRAY";
137 case FC_LGVARRAY: return "FC_LGVARRAY";
138 case FC_CARRAY: return "FC_CARRAY";
139 case FC_CVARRAY: return "FC_CVARRAY";
140 case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
141 case FC_ALIGNM2: return "FC_ALIGNM2";
142 case FC_ALIGNM4: return "FC_ALIGNM4";
143 case FC_ALIGNM8: return "FC_ALIGNM8";
144 case FC_POINTER: return "FC_POINTER";
145 case FC_C_CSTRING: return "FC_C_CSTRING";
146 case FC_C_WSTRING: return "FC_C_WSTRING";
147 case FC_CSTRING: return "FC_CSTRING";
148 case FC_WSTRING: return "FC_WSTRING";
149 case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
150 case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
151 case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
152 case FC_IP: return "FC_IP";
153 case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
154 case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
155 case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
156 case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
157 case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
158 case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
159 case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
160 case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
161 case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
162 case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
163 case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
164 case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
165 case FC_STRING_SIZED: return "FC_STRING_SIZED";
166 case FC_NO_REPEAT: return "FC_NO_REPEAT";
167 case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
168 case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
169 case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
170 case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
171 case FC_PP: return "FC_PP";
172 case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
173 case FC_DEREFERENCE: return "FC_DEREFERENCE";
174 case FC_DIV_2: return "FC_DIV_2";
175 case FC_MULT_2: return "FC_MULT_2";
176 case FC_ADD_1: return "FC_ADD_1";
177 case FC_SUB_1: return "FC_SUB_1";
178 case FC_CALLBACK: return "FC_CALLBACK";
179 case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
180 case FC_END: return "FC_END";
181 case FC_PAD: return "FC_PAD";
182 case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
183 case FC_RANGE: return "FC_RANGE";
184 case FC_INT3264: return "FC_INT3264";
185 case FC_UINT3264: return "FC_UINT3264";
186 default:
187 error("string_of_type: unknown type 0x%02x\n", type);
188 return NULL;
192 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
194 const type_t *t = type;
195 for (;;)
197 if (is_attr(t->attrs, attr))
198 return get_attrp(t->attrs, attr);
199 else if (type_is_alias(t))
200 t = type_alias_get_aliasee_type(t);
201 else return NULL;
205 unsigned char get_basic_fc(const type_t *type)
207 int sign = type_basic_get_sign(type);
208 switch (type_basic_get_type(type))
210 case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
211 case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
212 case TYPE_BASIC_INT32:
213 case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
214 case TYPE_BASIC_INT64: return FC_HYPER;
215 case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
216 case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
217 case TYPE_BASIC_BYTE: return FC_BYTE;
218 case TYPE_BASIC_CHAR: return FC_CHAR;
219 case TYPE_BASIC_WCHAR: return FC_WCHAR;
220 case TYPE_BASIC_HYPER: return FC_HYPER;
221 case TYPE_BASIC_FLOAT: return FC_FLOAT;
222 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
223 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
224 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
226 return 0;
229 static unsigned char get_basic_fc_signed(const type_t *type)
231 switch (type_basic_get_type(type))
233 case TYPE_BASIC_INT8: return FC_SMALL;
234 case TYPE_BASIC_INT16: return FC_SHORT;
235 case TYPE_BASIC_INT32: return FC_LONG;
236 case TYPE_BASIC_INT64: return FC_HYPER;
237 case TYPE_BASIC_INT: return FC_LONG;
238 case TYPE_BASIC_INT3264: return FC_INT3264;
239 case TYPE_BASIC_LONG: return FC_LONG;
240 case TYPE_BASIC_BYTE: return FC_BYTE;
241 case TYPE_BASIC_CHAR: return FC_CHAR;
242 case TYPE_BASIC_WCHAR: return FC_WCHAR;
243 case TYPE_BASIC_HYPER: return FC_HYPER;
244 case TYPE_BASIC_FLOAT: return FC_FLOAT;
245 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
246 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
247 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
249 return 0;
252 static inline unsigned int clamp_align(unsigned int align)
254 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
255 if(align > packing) align = packing;
256 return align;
259 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
261 const type_t *t;
262 int pointer_type;
264 assert(is_ptr(type) || is_array(type));
266 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
267 if (pointer_type)
268 return pointer_type;
270 for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
272 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
273 if (pointer_type)
274 return pointer_type;
277 if (toplevel_param)
278 return FC_RP;
280 if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
281 return pointer_type;
283 return FC_UP;
286 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
287 enum type_context context )
289 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
291 if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
292 (context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
293 pointer_fc = FC_OP;
295 return pointer_fc;
298 static unsigned char get_enum_fc(const type_t *type)
300 assert(type_get_type(type) == TYPE_ENUM);
301 if (is_aliaschain_attr(type, ATTR_V1ENUM))
302 return FC_ENUM32;
303 else
304 return FC_ENUM16;
307 static type_t *get_user_type(const type_t *t, const char **pname)
309 for (;;)
311 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
312 if (ut)
314 if (pname)
315 *pname = t->name;
316 return ut;
319 if (type_is_alias(t))
320 t = type_alias_get_aliasee_type(t);
321 else
322 return NULL;
326 static int is_user_type(const type_t *t)
328 return get_user_type(t, NULL) != NULL;
331 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
333 if (is_user_type(type))
334 return TGT_USER_TYPE;
336 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
337 return TGT_CTXT_HANDLE;
339 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
340 return TGT_STRING;
342 switch (type_get_type(type))
344 case TYPE_BASIC:
345 if (!(flags & TDT_IGNORE_RANGES) &&
346 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
347 return TGT_RANGE;
348 return TGT_BASIC;
349 case TYPE_ENUM:
350 if (!(flags & TDT_IGNORE_RANGES) &&
351 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
352 return TGT_RANGE;
353 return TGT_ENUM;
354 case TYPE_POINTER:
355 if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
356 (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
357 return TGT_IFACE_POINTER;
358 else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
359 return TGT_CTXT_HANDLE_POINTER;
360 else
361 return TGT_POINTER;
362 case TYPE_STRUCT:
363 return TGT_STRUCT;
364 case TYPE_ENCAPSULATED_UNION:
365 case TYPE_UNION:
366 return TGT_UNION;
367 case TYPE_ARRAY:
368 return TGT_ARRAY;
369 case TYPE_FUNCTION:
370 case TYPE_COCLASS:
371 case TYPE_INTERFACE:
372 case TYPE_MODULE:
373 case TYPE_VOID:
374 case TYPE_ALIAS:
375 case TYPE_BITFIELD:
376 break;
377 case TYPE_APICONTRACT:
378 /* not supposed to be here */
379 assert(0);
380 break;
382 return TGT_INVALID;
385 static int cant_be_null(const var_t *v)
387 switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
389 case TGT_ARRAY:
390 if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
391 /* fall through */
392 case TGT_POINTER:
393 return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
394 case TGT_CTXT_HANDLE_POINTER:
395 return TRUE;
396 default:
397 return 0;
402 static int get_padding(const var_list_t *fields)
404 unsigned short offset = 0;
405 unsigned int salign = 1;
406 const var_t *f;
408 if (!fields)
409 return 0;
411 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
413 type_t *ft = f->declspec.type;
414 unsigned int align = 0;
415 unsigned int size = type_memsize_and_alignment(ft, &align);
416 align = clamp_align(align);
417 if (align > salign) salign = align;
418 offset = ROUND_SIZE(offset, align);
419 offset += size;
422 return ROUNDING(offset, salign);
425 static unsigned int get_stack_size( const var_t *var, int *by_value )
427 unsigned int stack_size;
428 int by_val;
430 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
432 case TGT_BASIC:
433 case TGT_ENUM:
434 case TGT_RANGE:
435 case TGT_STRUCT:
436 case TGT_UNION:
437 case TGT_USER_TYPE:
438 stack_size = type_memsize( var->declspec.type );
439 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
440 break;
441 default:
442 by_val = 0;
443 break;
445 if (!by_val) stack_size = pointer_size;
446 if (by_value) *by_value = by_val;
447 return ROUND_SIZE( stack_size, pointer_size );
450 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
451 const type_t *type, int is_return )
453 unsigned char flags = 0;
454 int is_out;
456 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
458 if (is_ptr(type) &&
459 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
460 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
461 flags |= HANDLE_PARAM_IS_VIA_PTR;
463 if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
465 is_out = is_attr(attrs, ATTR_OUT);
466 if (is_attr(attrs, ATTR_IN) || !is_out)
468 flags |= HANDLE_PARAM_IS_IN;
469 if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
471 if (is_out) flags |= HANDLE_PARAM_IS_OUT;
473 return flags;
476 static unsigned int get_rpc_flags( const attr_list_t *attrs )
478 unsigned int flags = 0;
480 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
481 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
482 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
483 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
484 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
485 return flags;
488 unsigned char get_struct_fc(const type_t *type)
490 int has_pointer = 0;
491 int has_conformance = 0;
492 int has_variance = 0;
493 var_t *field;
494 var_list_t *fields;
496 fields = type_struct_get_fields(type);
498 if (get_padding(fields))
499 return FC_BOGUS_STRUCT;
501 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
503 type_t *t = field->declspec.type;
504 enum typegen_type typegen_type;
506 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
508 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
510 if (is_string_type(field->attrs, field->declspec.type))
512 if (is_conformant_array(t))
513 has_conformance = 1;
514 has_variance = 1;
515 continue;
518 if (is_array(type_array_get_element_type(field->declspec.type)))
519 return FC_BOGUS_STRUCT;
521 if (type_array_has_conformance(field->declspec.type))
523 has_conformance = 1;
524 if (list_next(fields, &field->entry))
525 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
526 field->name);
528 if (type_array_has_variance(t))
529 has_variance = 1;
531 t = type_array_get_element_type(t);
532 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
535 switch (typegen_type)
537 case TGT_USER_TYPE:
538 case TGT_IFACE_POINTER:
539 return FC_BOGUS_STRUCT;
540 case TGT_BASIC:
541 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
542 return FC_BOGUS_STRUCT;
543 break;
544 case TGT_ENUM:
545 if (get_enum_fc(t) == FC_ENUM16)
546 return FC_BOGUS_STRUCT;
547 break;
548 case TGT_POINTER:
549 case TGT_ARRAY:
550 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
551 return FC_BOGUS_STRUCT;
552 has_pointer = 1;
553 break;
554 case TGT_UNION:
555 return FC_BOGUS_STRUCT;
556 case TGT_STRUCT:
558 unsigned char fc = get_struct_fc(t);
559 switch (fc)
561 case FC_STRUCT:
562 break;
563 case FC_CVSTRUCT:
564 has_conformance = 1;
565 has_variance = 1;
566 has_pointer = 1;
567 break;
569 case FC_CPSTRUCT:
570 has_conformance = 1;
571 if (list_next( fields, &field->entry ))
572 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
573 field->name);
574 has_pointer = 1;
575 break;
577 case FC_CSTRUCT:
578 has_conformance = 1;
579 if (list_next( fields, &field->entry ))
580 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
581 field->name);
582 break;
584 case FC_PSTRUCT:
585 has_pointer = 1;
586 break;
588 default:
589 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
590 /* fallthru - treat it as complex */
592 /* as soon as we see one of these these members, it's bogus... */
593 case FC_BOGUS_STRUCT:
594 return FC_BOGUS_STRUCT;
596 break;
598 case TGT_RANGE:
599 return FC_BOGUS_STRUCT;
600 case TGT_STRING:
601 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
602 case TGT_INVALID:
603 case TGT_CTXT_HANDLE:
604 case TGT_CTXT_HANDLE_POINTER:
605 /* checking after parsing should mean that we don't get here. if we do,
606 * it's a checker bug */
607 assert(0);
611 if( has_variance )
613 if ( has_conformance )
614 return FC_CVSTRUCT;
615 else
616 return FC_BOGUS_STRUCT;
618 if( has_conformance && has_pointer )
619 return FC_CPSTRUCT;
620 if( has_conformance )
621 return FC_CSTRUCT;
622 if( has_pointer )
623 return FC_PSTRUCT;
624 return FC_STRUCT;
627 static unsigned char get_array_fc(const type_t *type)
629 unsigned char fc;
630 const expr_t *size_is;
631 const type_t *elem_type;
633 elem_type = type_array_get_element_type(type);
634 size_is = type_array_get_conformance(type);
636 if (!size_is)
638 unsigned int size = type_memsize(elem_type);
639 if (size * type_array_get_dim(type) > 0xffffuL)
640 fc = FC_LGFARRAY;
641 else
642 fc = FC_SMFARRAY;
644 else
645 fc = FC_CARRAY;
647 if (type_array_has_variance(type))
649 if (fc == FC_SMFARRAY)
650 fc = FC_SMVARRAY;
651 else if (fc == FC_LGFARRAY)
652 fc = FC_LGVARRAY;
653 else if (fc == FC_CARRAY)
654 fc = FC_CVARRAY;
657 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
659 case TGT_USER_TYPE:
660 fc = FC_BOGUS_ARRAY;
661 break;
662 case TGT_BASIC:
663 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
664 pointer_size != 4)
665 fc = FC_BOGUS_ARRAY;
666 break;
667 case TGT_STRUCT:
668 switch (get_struct_fc(elem_type))
670 case FC_BOGUS_STRUCT:
671 fc = FC_BOGUS_ARRAY;
672 break;
674 break;
675 case TGT_ENUM:
676 /* is 16-bit enum - if so, wire size differs from mem size and so
677 * the array cannot be block copied, which means the array is complex */
678 if (get_enum_fc(elem_type) == FC_ENUM16)
679 fc = FC_BOGUS_ARRAY;
680 break;
681 case TGT_UNION:
682 case TGT_IFACE_POINTER:
683 fc = FC_BOGUS_ARRAY;
684 break;
685 case TGT_POINTER:
686 /* ref pointers cannot just be block copied. unique pointers to
687 * interfaces need special treatment. either case means the array is
688 * complex */
689 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
690 fc = FC_BOGUS_ARRAY;
691 break;
692 case TGT_RANGE:
693 fc = FC_BOGUS_ARRAY;
694 break;
695 case TGT_CTXT_HANDLE:
696 case TGT_CTXT_HANDLE_POINTER:
697 case TGT_STRING:
698 case TGT_INVALID:
699 case TGT_ARRAY:
700 /* nothing to do for everything else */
701 break;
704 return fc;
707 static int is_non_complex_struct(const type_t *type)
709 return (type_get_type(type) == TYPE_STRUCT &&
710 get_struct_fc(type) != FC_BOGUS_STRUCT);
713 static int type_has_pointers(const type_t *type)
715 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
717 case TGT_USER_TYPE:
718 return FALSE;
719 case TGT_POINTER:
720 return TRUE;
721 case TGT_ARRAY:
722 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
723 case TGT_STRUCT:
725 var_list_t *fields = type_struct_get_fields(type);
726 const var_t *field;
727 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
729 if (type_has_pointers(field->declspec.type))
730 return TRUE;
732 break;
734 case TGT_UNION:
736 var_list_t *fields;
737 const var_t *field;
738 fields = type_union_get_cases(type);
739 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
741 if (field->declspec.type && type_has_pointers(field->declspec.type))
742 return TRUE;
744 break;
746 case TGT_CTXT_HANDLE:
747 case TGT_CTXT_HANDLE_POINTER:
748 case TGT_STRING:
749 case TGT_IFACE_POINTER:
750 case TGT_BASIC:
751 case TGT_ENUM:
752 case TGT_RANGE:
753 case TGT_INVALID:
754 break;
757 return FALSE;
760 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
761 int toplevel_param)
763 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
765 case TGT_USER_TYPE:
766 return FALSE;
767 case TGT_POINTER:
768 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
769 return TRUE;
770 else
771 return FALSE;
772 case TGT_ARRAY:
773 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
774 return TRUE;
775 else
776 return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
777 case TGT_STRUCT:
779 var_list_t *fields = type_struct_get_fields(type);
780 const var_t *field;
781 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
783 if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
784 return TRUE;
786 break;
788 case TGT_UNION:
790 var_list_t *fields;
791 const var_t *field;
792 fields = type_union_get_cases(type);
793 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
795 if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
796 return TRUE;
798 break;
800 case TGT_CTXT_HANDLE:
801 case TGT_CTXT_HANDLE_POINTER:
802 case TGT_STRING:
803 case TGT_IFACE_POINTER:
804 case TGT_BASIC:
805 case TGT_ENUM:
806 case TGT_RANGE:
807 case TGT_INVALID:
808 break;
811 return FALSE;
814 static unsigned short user_type_offset(const char *name)
816 user_type_t *ut;
817 unsigned short off = 0;
818 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
820 if (strcmp(name, ut->name) == 0)
821 return off;
822 ++off;
824 error("user_type_offset: couldn't find type (%s)\n", name);
825 return 0;
828 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
830 type->typestring_offset = offset;
831 if (file) type->tfswrite = FALSE;
834 static void guard_rec(type_t *type)
836 /* types that contain references to themselves (like a linked list),
837 need to be shielded from infinite recursion when writing embedded
838 types */
839 if (type->typestring_offset)
840 type->tfswrite = FALSE;
841 else
842 type->typestring_offset = 1;
845 static int is_embedded_complex(const type_t *type)
847 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
849 case TGT_USER_TYPE:
850 case TGT_STRUCT:
851 case TGT_UNION:
852 case TGT_ARRAY:
853 case TGT_IFACE_POINTER:
854 return TRUE;
855 default:
856 return FALSE;
860 static const char *get_context_handle_type_name(const type_t *type)
862 const type_t *t;
863 for (t = type;
864 is_ptr(t) || type_is_alias(t);
865 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
866 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
867 return t->name;
868 assert(0);
869 return NULL;
872 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
873 do { \
874 if (file) \
875 fprintf(file, "/* %2u */\n", typestring_offset); \
876 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
878 while (0)
880 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
881 static void print_file(FILE *file, int indent, const char *format, ...)
883 va_list va;
884 va_start(va, format);
885 print(file, indent, format, va);
886 va_end(va);
889 void print(FILE *file, int indent, const char *format, va_list va)
891 if (file)
893 if (format[0] != '\n')
894 while (0 < indent--)
895 fprintf(file, " ");
896 vfprintf(file, format, va);
901 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
903 if (decl_indirect(t))
905 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
906 local_var_prefix, n, local_var_prefix, n);
907 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
909 else if (is_ptr(t) || is_array(t))
910 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
913 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
915 const var_t *var = type_function_get_retval(func->declspec.type);
917 if (!is_void(var->declspec.type))
918 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
920 if (!type_function_get_args(func->declspec.type))
921 return;
923 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
924 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
926 fprintf(file, "\n");
929 static void write_formatdesc(FILE *f, int indent, const char *str)
931 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
932 print_file(f, indent, "{\n");
933 print_file(f, indent + 1, "short Pad;\n");
934 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
935 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
936 print_file(f, indent, "\n");
939 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
941 clear_all_offsets();
943 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
944 get_size_typeformatstring(stmts, pred));
946 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
947 get_size_procformatstring(stmts, pred));
949 fprintf(f, "\n");
950 write_formatdesc(f, indent, "TYPE");
951 write_formatdesc(f, indent, "PROC");
952 fprintf(f, "\n");
953 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
954 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
955 print_file(f, indent, "\n");
958 int decl_indirect(const type_t *t)
960 if (is_user_type(t))
961 return TRUE;
962 return (type_get_type(t) != TYPE_BASIC &&
963 type_get_type(t) != TYPE_ENUM &&
964 type_get_type(t) != TYPE_POINTER &&
965 type_get_type(t) != TYPE_ARRAY);
968 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
969 unsigned int *stack_size, unsigned int *typestring_offset )
971 unsigned int alignment, server_size = 0, buffer_size = 0;
972 unsigned char fc = 0;
973 int is_byval;
974 int is_in = is_attr(var->attrs, ATTR_IN);
975 int is_out = is_attr(var->attrs, ATTR_OUT);
977 if (is_return) is_out = TRUE;
978 else if (!is_in && !is_out) is_in = TRUE;
980 *flags = 0;
981 *stack_size = get_stack_size( var, &is_byval );
982 *typestring_offset = var->typestring_offset;
984 if (is_in) *flags |= IsIn;
985 if (is_out) *flags |= IsOut;
986 if (is_return) *flags |= IsReturn;
988 if (!is_string_type( var->attrs, var->declspec.type ))
989 buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
991 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
993 case TGT_BASIC:
994 *flags |= IsBasetype;
995 fc = get_basic_fc_signed( var->declspec.type );
996 if (fc == FC_BIND_PRIMITIVE)
998 buffer_size = 4; /* actually 0 but avoids setting MustSize */
999 fc = FC_LONG;
1001 break;
1002 case TGT_ENUM:
1003 *flags |= IsBasetype;
1004 fc = get_enum_fc( var->declspec.type );
1005 break;
1006 case TGT_RANGE:
1007 *flags |= IsByValue;
1008 break;
1009 case TGT_STRUCT:
1010 case TGT_UNION:
1011 case TGT_USER_TYPE:
1012 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1013 break;
1014 case TGT_IFACE_POINTER:
1015 *flags |= MustFree;
1016 break;
1017 case TGT_ARRAY:
1018 *flags |= MustFree;
1019 if (type_array_is_decl_as_ptr(var->declspec.type)
1020 && type_array_get_ptr_tfsoff(var->declspec.type)
1021 && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
1023 *typestring_offset = var->declspec.type->typestring_offset;
1024 *flags |= IsSimpleRef;
1026 break;
1027 case TGT_STRING:
1028 *flags |= MustFree;
1029 if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1031 /* skip over pointer description straight to string description */
1032 if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
1033 else *typestring_offset += 2;
1034 *flags |= IsSimpleRef;
1036 break;
1037 case TGT_CTXT_HANDLE_POINTER:
1038 *flags |= IsSimpleRef;
1039 *typestring_offset += 4;
1040 /* fall through */
1041 case TGT_CTXT_HANDLE:
1042 buffer_size = 20;
1043 break;
1044 case TGT_POINTER:
1045 if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1047 const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
1049 if (!is_string_type( var->attrs, ref ))
1050 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1052 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1054 case TGT_BASIC:
1055 *flags |= IsSimpleRef | IsBasetype;
1056 fc = get_basic_fc( ref );
1057 if (!is_in && is_out) server_size = pointer_size;
1058 break;
1059 case TGT_ENUM:
1060 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1062 *flags |= IsSimpleRef | IsBasetype;
1063 if (!is_in && is_out) server_size = pointer_size;
1065 else
1067 server_size = pointer_size;
1069 break;
1070 case TGT_UNION:
1071 case TGT_USER_TYPE:
1072 case TGT_RANGE:
1073 *flags |= MustFree | IsSimpleRef;
1074 *typestring_offset = ref->typestring_offset;
1075 if (!is_in && is_out) server_size = type_memsize( ref );
1076 break;
1077 case TGT_ARRAY:
1078 *flags |= MustFree;
1079 if (!type_array_is_decl_as_ptr(ref))
1081 *flags |= IsSimpleRef;
1082 *typestring_offset = ref->typestring_offset;
1084 if (!is_in && is_out) server_size = type_memsize( ref );
1085 break;
1086 case TGT_STRING:
1087 case TGT_POINTER:
1088 case TGT_CTXT_HANDLE:
1089 case TGT_CTXT_HANDLE_POINTER:
1090 *flags |= MustFree;
1091 server_size = pointer_size;
1092 break;
1093 case TGT_IFACE_POINTER:
1094 *flags |= MustFree;
1095 if (is_in && is_out) server_size = pointer_size;
1096 break;
1097 case TGT_STRUCT:
1098 *flags |= IsSimpleRef | MustFree;
1099 *typestring_offset = ref->typestring_offset;
1100 switch (get_struct_fc(ref))
1102 case FC_STRUCT:
1103 case FC_PSTRUCT:
1104 case FC_BOGUS_STRUCT:
1105 if (!is_in && is_out) server_size = type_memsize( ref );
1106 break;
1107 default:
1108 break;
1110 break;
1111 case TGT_INVALID:
1112 assert(0);
1115 else /* not ref pointer */
1117 *flags |= MustFree;
1119 break;
1120 case TGT_INVALID:
1121 assert(0);
1124 if (!buffer_size) *flags |= MustSize;
1126 if (server_size)
1128 server_size = (server_size + 7) / 8;
1129 if (server_size < 8) *flags |= server_size << 13;
1131 return fc;
1134 static unsigned char get_func_oi2_flags( const var_t *func )
1136 const var_t *var;
1137 var_list_t *args = type_function_get_args( func->declspec.type );
1138 var_t *retval = type_function_get_retval( func->declspec.type );
1139 unsigned char oi2_flags = 0x40; /* HasExtensions */
1140 unsigned short flags;
1141 unsigned int stack_size, typestring_offset;
1143 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1145 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1146 if (flags & MustSize)
1148 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1149 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1153 if (!is_void( retval->declspec.type ))
1155 oi2_flags |= 0x04; /* HasRet */
1156 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1157 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1159 return oi2_flags;
1162 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1163 int is_return, unsigned int *stack_offset)
1165 char buffer[128];
1166 unsigned int stack_size, typestring_offset;
1167 unsigned short flags;
1168 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1170 strcpy( buffer, "/* flags:" );
1171 if (flags & MustSize) strcat( buffer, " must size," );
1172 if (flags & MustFree) strcat( buffer, " must free," );
1173 if (flags & IsPipe) strcat( buffer, " pipe," );
1174 if (flags & IsIn) strcat( buffer, " in," );
1175 if (flags & IsOut) strcat( buffer, " out," );
1176 if (flags & IsReturn) strcat( buffer, " return," );
1177 if (flags & IsBasetype) strcat( buffer, " base type," );
1178 if (flags & IsByValue) strcat( buffer, " by value," );
1179 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1180 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1181 strcpy( buffer + strlen( buffer ) - 1, " */" );
1182 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1183 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1184 *stack_offset, *stack_offset );
1185 if (flags & IsBasetype)
1187 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1188 print_file( file, indent, "0x0,\n" );
1190 else
1191 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1192 typestring_offset, typestring_offset );
1193 *stack_offset += max( stack_size, pointer_size );
1194 return 6;
1197 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1198 int is_return, int is_interpreted)
1200 unsigned int size;
1202 int is_in = is_attr(var->attrs, ATTR_IN);
1203 int is_out = is_attr(var->attrs, ATTR_OUT);
1205 if (!is_in && !is_out) is_in = TRUE;
1207 if (type_get_type(var->declspec.type) == TYPE_BASIC ||
1208 type_get_type(var->declspec.type) == TYPE_ENUM)
1210 unsigned char fc;
1212 if (is_return)
1213 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1214 else
1215 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1217 if (type_get_type(var->declspec.type) == TYPE_ENUM)
1219 fc = get_enum_fc(var->declspec.type);
1221 else
1223 fc = get_basic_fc_signed(var->declspec.type);
1225 if (fc == FC_BIND_PRIMITIVE)
1226 fc = FC_IGNORE;
1229 print_file(file, indent, "0x%02x, /* %s */\n",
1230 fc, string_of_type(fc));
1231 size = 2; /* includes param type prefix */
1233 else
1235 unsigned short offset = var->typestring_offset;
1237 if (!is_interpreted && is_array(var->declspec.type)
1238 && type_array_is_decl_as_ptr(var->declspec.type)
1239 && type_array_get_ptr_tfsoff(var->declspec.type))
1240 offset = var->declspec.type->typestring_offset;
1242 if (is_return)
1243 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1244 else if (is_in && is_out)
1245 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1246 else if (is_out)
1247 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1248 else
1249 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1251 size = get_stack_size( var, NULL );
1252 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1253 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1254 size = 4; /* includes param type prefix */
1256 return size;
1259 int is_interpreted_func( const type_t *iface, const var_t *func )
1261 const char *str;
1262 const var_t *var;
1263 const var_list_t *args = type_function_get_args( func->declspec.type );
1264 const type_t *ret_type = type_function_get_rettype( func->declspec.type );
1266 if (type_get_type( ret_type ) == TYPE_BASIC)
1268 switch (type_basic_get_type( ret_type ))
1270 case TYPE_BASIC_INT64:
1271 case TYPE_BASIC_HYPER:
1272 /* return value must fit in a long_ptr */
1273 if (pointer_size < 8) return 0;
1274 break;
1275 case TYPE_BASIC_FLOAT:
1276 case TYPE_BASIC_DOUBLE:
1277 /* floating point values can't be returned */
1278 return 0;
1279 default:
1280 break;
1283 if (get_stub_mode() != MODE_Oif && args)
1285 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1286 switch (type_get_type( var->declspec.type ))
1288 case TYPE_BASIC:
1289 switch (type_basic_get_type( var->declspec.type ))
1291 /* floating point arguments are not supported in Oi mode */
1292 case TYPE_BASIC_FLOAT: return 0;
1293 case TYPE_BASIC_DOUBLE: return 0;
1294 default: break;
1296 break;
1297 /* unions passed by value are not supported in Oi mode */
1298 case TYPE_UNION: return 0;
1299 case TYPE_ENCAPSULATED_UNION: return 0;
1300 default: break;
1304 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1305 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1306 return (get_stub_mode() != MODE_Os);
1309 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1310 const var_t *func, unsigned int *offset,
1311 unsigned short num_proc )
1313 var_t *var;
1314 var_list_t *args = type_function_get_args( func->declspec.type );
1315 unsigned char explicit_fc, implicit_fc;
1316 unsigned char handle_flags;
1317 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1318 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1319 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1320 unsigned int nb_args = 0;
1321 unsigned int stack_size = 0;
1322 unsigned short param_num = 0;
1323 unsigned short handle_stack_offset = 0;
1324 unsigned short handle_param_num = 0;
1326 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1327 if (is_object( iface ))
1329 oi_flags |= Oi_OBJECT_PROC;
1330 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1331 stack_size += pointer_size;
1334 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1336 if (var == handle_var)
1338 handle_stack_offset = stack_size;
1339 handle_param_num = param_num;
1341 stack_size += get_stack_size( var, NULL );
1342 param_num++;
1343 nb_args++;
1345 if (!is_void( type_function_get_rettype( func->declspec.type )))
1347 stack_size += pointer_size;
1348 nb_args++;
1351 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1352 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1353 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1354 print_file( file, indent, "0x%02x,\n", oi_flags );
1355 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1356 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1357 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1358 *offset += 10;
1360 if (!implicit_fc)
1362 switch (explicit_fc)
1364 case FC_BIND_PRIMITIVE:
1365 handle_flags = 0;
1366 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1367 print_file( file, indent, "0x%02x,\n", handle_flags );
1368 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1369 handle_stack_offset, handle_stack_offset );
1370 *offset += 4;
1371 break;
1372 case FC_BIND_GENERIC:
1373 handle_flags = type_memsize( handle_var->declspec.type );
1374 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1375 print_file( file, indent, "0x%02x,\n", handle_flags );
1376 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1377 handle_stack_offset, handle_stack_offset );
1378 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
1379 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1380 *offset += 6;
1381 break;
1382 case FC_BIND_CONTEXT:
1383 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
1384 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1385 print_file( file, indent, "0x%02x,\n", handle_flags );
1386 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1387 handle_stack_offset, handle_stack_offset );
1388 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
1389 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1390 *offset += 6;
1391 break;
1395 if (get_stub_mode() == MODE_Oif)
1397 unsigned char oi2_flags = get_func_oi2_flags( func );
1398 unsigned char ext_flags = 0;
1399 unsigned int size;
1401 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1402 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1403 if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
1405 size = get_function_buffer_size( func, PASS_IN );
1406 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1407 size = get_function_buffer_size( func, PASS_OUT );
1408 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1409 print_file( file, indent, "0x%02x,\n", oi2_flags );
1410 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1411 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1412 print_file( file, indent, "0x%02x,\n", ext_flags );
1413 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1414 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1415 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1416 *offset += 14;
1417 if (pointer_size == 8)
1419 unsigned short pos = 0, fpu_mask = 0;
1421 if (is_object( iface )) pos += 2;
1422 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1424 if (type_get_type( var->declspec.type ) == TYPE_BASIC)
1426 switch (type_basic_get_type( var->declspec.type ))
1428 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1429 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1430 default: break;
1433 pos += 2;
1434 if (pos >= 16) break;
1436 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1437 *offset += 2;
1442 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1443 const var_t *func, unsigned int *offset,
1444 unsigned short num_proc )
1446 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1447 int is_interpreted = is_interpreted_func( iface, func );
1448 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1449 var_t *retval = type_function_get_retval( func->declspec.type );
1451 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1453 /* emit argument data */
1454 if (type_function_get_args(func->declspec.type))
1456 const var_t *var;
1457 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1459 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1460 if (is_new_style)
1461 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1462 else
1463 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1467 /* emit return value data */
1468 if (is_void(retval->declspec.type))
1470 if (!is_new_style)
1472 print_file(file, 0, "/* %u (void) */\n", *offset);
1473 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1474 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1475 *offset += 2;
1478 else
1480 print_file( file, 0, "/* %u (return value) */\n", *offset );
1481 if (is_new_style)
1482 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1483 else
1484 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1488 static void for_each_iface(const statement_list_t *stmts,
1489 void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
1490 type_pred_t pred, FILE *file, int indent, unsigned int *offset)
1492 const statement_t *stmt;
1493 type_t *iface;
1495 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1497 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
1498 continue;
1499 iface = stmt->u.type;
1500 if (!pred(iface)) continue;
1501 proc(iface, file, indent, offset);
1502 if (type_iface_get_async_iface(iface))
1503 proc(type_iface_get_async_iface(iface), file, indent, offset);
1507 static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
1509 const statement_t *stmt;
1510 const type_t *parent = type_iface_get_inherit( iface );
1511 int count = parent ? count_methods( parent ) : 0;
1513 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1515 var_t *func = stmt->u.var;
1516 if (is_local(func->attrs)) continue;
1517 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1521 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1523 int indent = 0;
1524 unsigned int offset = 0;
1526 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1527 print_file(file, indent, "{\n");
1528 indent++;
1529 print_file(file, indent, "0,\n");
1530 print_file(file, indent, "{\n");
1531 indent++;
1533 for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
1535 print_file(file, indent, "0x0\n");
1536 indent--;
1537 print_file(file, indent, "}\n");
1538 indent--;
1539 print_file(file, indent, "};\n");
1540 print_file(file, indent, "\n");
1543 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1545 const statement_t *stmt;
1546 int indent = 0;
1548 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1549 iface->name );
1550 print_file( file, indent, "{\n" );
1551 indent++;
1552 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1554 var_t *func = stmt->u.var;
1555 if (is_local( func->attrs )) continue;
1556 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1558 indent--;
1559 print_file( file, indent, "};\n\n" );
1562 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1564 unsigned char fc;
1566 if (type_get_type(type) == TYPE_BASIC)
1567 fc = get_basic_fc_signed(type);
1568 else if (type_get_type(type) == TYPE_ENUM)
1569 fc = get_enum_fc(type);
1570 else
1571 return 0;
1573 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1574 *typestring_offset += 1;
1575 return 1;
1578 /* write conformance / variance descriptor */
1579 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1580 unsigned int baseoff, const type_t *type,
1581 const expr_t *expr)
1583 unsigned char operator_type = 0;
1584 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1585 const char *conftype_string = "field";
1586 const expr_t *subexpr;
1587 const type_t *iface = NULL;
1588 const char *name;
1590 if (!expr)
1592 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1593 return 4;
1596 if (expr->is_const)
1598 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1599 error("write_conf_or_var_desc: constant value %d is greater than "
1600 "the maximum constant size of %d\n", expr->cval,
1601 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1603 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1604 FC_CONSTANT_CONFORMANCE, expr->cval);
1605 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1606 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1608 return 4;
1611 if (!cont_type) /* top-level conformance */
1613 conftype = FC_TOP_LEVEL_CONFORMANCE;
1614 conftype_string = "parameter";
1615 cont_type = current_func->declspec.type;
1616 name = current_func->name;
1617 iface = current_iface;
1619 else
1621 name = cont_type->name;
1622 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1624 conftype = FC_POINTER_CONFORMANCE;
1625 conftype_string = "field pointer";
1629 subexpr = expr;
1630 switch (subexpr->type)
1632 case EXPR_PPTR:
1633 subexpr = subexpr->ref;
1634 operator_type = FC_DEREFERENCE;
1635 break;
1636 case EXPR_DIV:
1637 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1639 subexpr = subexpr->ref;
1640 operator_type = FC_DIV_2;
1642 break;
1643 case EXPR_MUL:
1644 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1646 subexpr = subexpr->ref;
1647 operator_type = FC_MULT_2;
1649 break;
1650 case EXPR_SUB:
1651 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1653 subexpr = subexpr->ref;
1654 operator_type = FC_SUB_1;
1656 break;
1657 case EXPR_ADD:
1658 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1660 subexpr = subexpr->ref;
1661 operator_type = FC_ADD_1;
1663 break;
1664 default:
1665 break;
1668 if (subexpr->type == EXPR_IDENTIFIER)
1670 const type_t *correlation_variable = NULL;
1671 unsigned char param_type = 0;
1672 unsigned int offset = 0;
1673 const var_t *var;
1674 struct expr_loc expr_loc;
1676 if (type_get_type(cont_type) == TYPE_FUNCTION)
1678 var_list_t *args = type_function_get_args( cont_type );
1680 if (is_object( iface )) offset += pointer_size;
1681 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1683 if (var->name && !strcmp(var->name, subexpr->u.sval))
1685 expr_loc.v = var;
1686 correlation_variable = var->declspec.type;
1687 break;
1689 offset += get_stack_size( var, NULL );
1692 else
1694 var_list_t *fields = type_struct_get_fields( cont_type );
1696 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1698 unsigned int size = field_memsize( var->declspec.type, &offset );
1699 if (var->name && !strcmp(var->name, subexpr->u.sval))
1701 expr_loc.v = var;
1702 correlation_variable = var->declspec.type;
1703 break;
1705 offset += size;
1709 if (!correlation_variable)
1710 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1711 expr_loc.attr = NULL;
1712 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1714 offset -= baseoff;
1716 if (type_get_type(correlation_variable) == TYPE_BASIC)
1718 switch (get_basic_fc(correlation_variable))
1720 case FC_CHAR:
1721 case FC_SMALL:
1722 param_type = FC_SMALL;
1723 break;
1724 case FC_BYTE:
1725 case FC_USMALL:
1726 param_type = FC_USMALL;
1727 break;
1728 case FC_WCHAR:
1729 case FC_SHORT:
1730 param_type = FC_SHORT;
1731 break;
1732 case FC_USHORT:
1733 param_type = FC_USHORT;
1734 break;
1735 case FC_LONG:
1736 param_type = FC_LONG;
1737 break;
1738 case FC_ULONG:
1739 param_type = FC_ULONG;
1740 break;
1741 default:
1742 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1743 get_basic_fc(correlation_variable));
1746 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1748 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1749 param_type = FC_LONG;
1750 else
1751 param_type = FC_SHORT;
1753 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1755 if (pointer_size == 8)
1756 param_type = FC_HYPER;
1757 else
1758 param_type = FC_LONG;
1760 else
1762 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1763 subexpr->u.sval);
1764 return 0;
1767 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1768 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1769 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1770 operator_type ? string_of_type(operator_type) : "no operators");
1771 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1772 (unsigned short)offset, offset);
1774 else if (!iface || is_interpreted_func( iface, current_func ))
1776 unsigned int callback_offset = 0;
1777 struct expr_eval_routine *eval;
1778 int found = 0;
1780 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1782 if (eval->cont_type == cont_type ||
1783 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1784 eval->iface == iface &&
1785 eval->name && name && !strcmp(eval->name, name) &&
1786 !compare_expr(eval->expr, expr)))
1788 found = 1;
1789 break;
1791 callback_offset++;
1794 if (!found)
1796 eval = xmalloc (sizeof(*eval));
1797 eval->iface = iface;
1798 eval->cont_type = cont_type;
1799 eval->name = xstrdup( name );
1800 eval->baseoff = baseoff;
1801 eval->expr = expr;
1802 list_add_tail (&expr_eval_routines, &eval->entry);
1805 if (callback_offset > USHRT_MAX)
1806 error("Maximum number of callback routines reached\n");
1808 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1809 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1810 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1812 else /* output a dummy corr desc that isn't used */
1814 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1815 print_file(file, 2, "0x0,\n" );
1816 print_file(file, 2, "NdrFcShort(0x0),\n" );
1818 return 4;
1821 /* return size and start offset of a data field based on current offset */
1822 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1824 unsigned int align = 0;
1825 unsigned int size = type_memsize_and_alignment( type, &align );
1827 *offset = ROUND_SIZE( *offset, align );
1828 return size;
1831 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1833 unsigned int size = 0;
1834 unsigned int max_align;
1835 const var_t *v;
1837 if (!fields) return 0;
1838 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1840 unsigned int falign = 0;
1841 unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
1842 if (*align < falign) *align = falign;
1843 falign = clamp_align(falign);
1844 size = ROUND_SIZE(size, falign);
1845 size += fsize;
1848 max_align = clamp_align(*align);
1849 size = ROUND_SIZE(size, max_align);
1851 return size;
1854 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1856 unsigned int size, maxs = 0;
1857 unsigned int align = *pmaxa;
1858 const var_t *v;
1860 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1862 /* we could have an empty default field with NULL type */
1863 if (v->declspec.type)
1865 size = type_memsize_and_alignment(v->declspec.type, &align);
1866 if (maxs < size) maxs = size;
1867 if (*pmaxa < align) *pmaxa = align;
1871 return maxs;
1874 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1876 unsigned int size = 0;
1878 switch (type_get_type(t))
1880 case TYPE_BASIC:
1881 switch (get_basic_fc(t))
1883 case FC_BYTE:
1884 case FC_CHAR:
1885 case FC_USMALL:
1886 case FC_SMALL:
1887 size = 1;
1888 if (size > *align) *align = size;
1889 break;
1890 case FC_WCHAR:
1891 case FC_USHORT:
1892 case FC_SHORT:
1893 size = 2;
1894 if (size > *align) *align = size;
1895 break;
1896 case FC_ULONG:
1897 case FC_LONG:
1898 case FC_ERROR_STATUS_T:
1899 case FC_FLOAT:
1900 size = 4;
1901 if (size > *align) *align = size;
1902 break;
1903 case FC_HYPER:
1904 case FC_DOUBLE:
1905 size = 8;
1906 if (size > *align) *align = size;
1907 break;
1908 case FC_INT3264:
1909 case FC_UINT3264:
1910 case FC_BIND_PRIMITIVE:
1911 assert( pointer_size );
1912 size = pointer_size;
1913 if (size > *align) *align = size;
1914 break;
1915 default:
1916 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1917 size = 0;
1919 break;
1920 case TYPE_ENUM:
1921 switch (get_enum_fc(t))
1923 case FC_ENUM16:
1924 case FC_ENUM32:
1925 size = 4;
1926 if (size > *align) *align = size;
1927 break;
1928 default:
1929 error("type_memsize: Unknown enum type\n");
1930 size = 0;
1932 break;
1933 case TYPE_STRUCT:
1934 size = fields_memsize(type_struct_get_fields(t), align);
1935 break;
1936 case TYPE_ENCAPSULATED_UNION:
1937 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1938 break;
1939 case TYPE_UNION:
1940 size = union_memsize(type_union_get_cases(t), align);
1941 break;
1942 case TYPE_POINTER:
1943 case TYPE_INTERFACE:
1944 assert( pointer_size );
1945 size = pointer_size;
1946 if (size > *align) *align = size;
1947 break;
1948 case TYPE_ARRAY:
1949 if (!type_array_is_decl_as_ptr(t))
1951 if (is_conformant_array(t))
1953 type_memsize_and_alignment(type_array_get_element_type(t), align);
1954 size = 0;
1956 else
1957 size = type_array_get_dim(t) *
1958 type_memsize_and_alignment(type_array_get_element_type(t), align);
1960 else /* declared as a pointer */
1962 assert( pointer_size );
1963 size = pointer_size;
1964 if (size > *align) *align = size;
1966 break;
1967 case TYPE_ALIAS:
1968 case TYPE_VOID:
1969 case TYPE_COCLASS:
1970 case TYPE_MODULE:
1971 case TYPE_FUNCTION:
1972 case TYPE_BITFIELD:
1973 case TYPE_APICONTRACT:
1974 /* these types should not be encountered here due to language
1975 * restrictions (interface, void, coclass, module), logical
1976 * restrictions (alias - due to type_get_type call above) or
1977 * checking restrictions (function, bitfield). */
1978 assert(0);
1981 return size;
1984 unsigned int type_memsize(const type_t *t)
1986 unsigned int align = 0;
1987 return type_memsize_and_alignment( t, &align );
1990 static unsigned int type_buffer_alignment(const type_t *t)
1992 const var_list_t *fields;
1993 const var_t *var;
1994 unsigned int max = 0, align;
1996 switch (type_get_type(t))
1998 case TYPE_BASIC:
1999 switch (get_basic_fc(t))
2001 case FC_BYTE:
2002 case FC_CHAR:
2003 case FC_USMALL:
2004 case FC_SMALL:
2005 return 1;
2006 case FC_WCHAR:
2007 case FC_USHORT:
2008 case FC_SHORT:
2009 return 2;
2010 case FC_ULONG:
2011 case FC_LONG:
2012 case FC_ERROR_STATUS_T:
2013 case FC_FLOAT:
2014 case FC_INT3264:
2015 case FC_UINT3264:
2016 return 4;
2017 case FC_HYPER:
2018 case FC_DOUBLE:
2019 return 8;
2020 default:
2021 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
2023 break;
2024 case TYPE_ENUM:
2025 switch (get_enum_fc(t))
2027 case FC_ENUM16:
2028 return 2;
2029 case FC_ENUM32:
2030 return 4;
2031 default:
2032 error("type_buffer_alignment: Unknown enum type\n");
2034 break;
2035 case TYPE_STRUCT:
2036 if (!(fields = type_struct_get_fields(t))) break;
2037 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2039 if (!var->declspec.type) continue;
2040 align = type_buffer_alignment( var->declspec.type );
2041 if (max < align) max = align;
2043 break;
2044 case TYPE_ENCAPSULATED_UNION:
2045 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2046 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2048 if (!var->declspec.type) continue;
2049 align = type_buffer_alignment( var->declspec.type );
2050 if (max < align) max = align;
2052 break;
2053 case TYPE_UNION:
2054 if (!(fields = type_union_get_cases(t))) break;
2055 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2057 if (!var->declspec.type) continue;
2058 align = type_buffer_alignment( var->declspec.type );
2059 if (max < align) max = align;
2061 break;
2062 case TYPE_ARRAY:
2063 if (!type_array_is_decl_as_ptr(t))
2064 return type_buffer_alignment( type_array_get_element_type(t) );
2065 /* else fall through */
2066 case TYPE_POINTER:
2067 return 4;
2068 case TYPE_INTERFACE:
2069 case TYPE_ALIAS:
2070 case TYPE_VOID:
2071 case TYPE_COCLASS:
2072 case TYPE_MODULE:
2073 case TYPE_FUNCTION:
2074 case TYPE_BITFIELD:
2075 case TYPE_APICONTRACT:
2076 /* these types should not be encountered here due to language
2077 * restrictions (interface, void, coclass, module), logical
2078 * restrictions (alias - due to type_get_type call above) or
2079 * checking restrictions (function, bitfield). */
2080 assert(0);
2082 return max;
2085 int is_full_pointer_function(const var_t *func)
2087 const var_t *var;
2088 if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
2089 return TRUE;
2090 if (!type_function_get_args(func->declspec.type))
2091 return FALSE;
2092 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
2093 if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
2094 return TRUE;
2095 return FALSE;
2098 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2100 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2101 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2102 fprintf(file, "\n");
2105 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2107 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2108 fprintf(file, "\n");
2111 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2112 const type_t *type,
2113 enum type_context context,
2114 unsigned int offset,
2115 unsigned int *typeformat_offset)
2117 unsigned int start_offset = *typeformat_offset;
2118 short reloff = offset - (*typeformat_offset + 2);
2119 int in_attr, out_attr;
2120 int pointer_type;
2121 unsigned char flags = 0;
2123 pointer_type = get_pointer_fc_context(type, attrs, context);
2125 in_attr = is_attr(attrs, ATTR_IN);
2126 out_attr = is_attr(attrs, ATTR_OUT);
2127 if (!in_attr && !out_attr) in_attr = 1;
2129 if (!is_interpreted_func(current_iface, current_func))
2131 if (out_attr && !in_attr && pointer_type == FC_RP)
2132 flags |= FC_ALLOCED_ON_STACK;
2134 else if (get_stub_mode() == MODE_Oif)
2136 if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
2138 switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
2140 case TGT_STRING:
2141 case TGT_POINTER:
2142 case TGT_CTXT_HANDLE:
2143 case TGT_CTXT_HANDLE_POINTER:
2144 case TGT_ARRAY:
2145 flags |= FC_ALLOCED_ON_STACK;
2146 break;
2147 case TGT_IFACE_POINTER:
2148 if (in_attr && out_attr)
2149 flags |= FC_ALLOCED_ON_STACK;
2150 break;
2151 default:
2152 break;
2157 if (is_ptr(type))
2159 type_t *ref = type_pointer_get_ref_type(type);
2160 if(is_declptr(ref) && !is_user_type(ref))
2161 flags |= FC_POINTER_DEREF;
2162 if (pointer_type != FC_RP) {
2163 flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
2167 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2168 pointer_type,
2169 flags,
2170 string_of_type(pointer_type));
2171 if (file)
2173 if (flags & FC_ALLOCED_ON_STACK)
2174 fprintf(file, " [allocated_on_stack]");
2175 if (flags & FC_POINTER_DEREF)
2176 fprintf(file, " [pointer_deref]");
2177 if (flags & FC_DONT_FREE)
2178 fprintf(file, " [dont_free]");
2179 if (flags & FC_ALLOCATE_ALL_NODES)
2180 fprintf(file, " [all_nodes]");
2181 fprintf(file, " */\n");
2184 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2185 *typeformat_offset += 4;
2187 return start_offset;
2190 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2191 const type_t *type, enum type_context context)
2193 unsigned char fc;
2194 unsigned char pointer_fc;
2195 const type_t *ref;
2196 int in_attr = is_attr(attrs, ATTR_IN);
2197 int out_attr = is_attr(attrs, ATTR_OUT);
2198 unsigned char flags = FC_SIMPLE_POINTER;
2200 /* for historical reasons, write_simple_pointer also handled string types,
2201 * but no longer does. catch bad uses of the function with this check */
2202 if (is_string_type(attrs, type))
2203 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2205 pointer_fc = get_pointer_fc_context(type, attrs, context);
2207 ref = type_pointer_get_ref_type(type);
2208 if (type_get_type(ref) == TYPE_ENUM)
2209 fc = get_enum_fc(ref);
2210 else
2211 fc = get_basic_fc(ref);
2213 if (!is_interpreted_func(current_iface, current_func))
2215 if (out_attr && !in_attr && pointer_fc == FC_RP)
2216 flags |= FC_ALLOCED_ON_STACK;
2218 else if (get_stub_mode() == MODE_Oif)
2220 if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
2221 flags |= FC_ALLOCED_ON_STACK;
2224 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2225 pointer_fc, flags, string_of_type(pointer_fc),
2226 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2227 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2228 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2229 return 4;
2232 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2234 const decl_spec_t ds = {.type = t};
2235 print_file(file, 0, "/* %u (", tfsoff);
2236 write_type_decl(file, &ds, NULL);
2237 print_file(file, 0, ") */\n");
2240 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2241 type_t *type, unsigned int ref_offset,
2242 enum type_context context,
2243 unsigned int *typestring_offset)
2245 unsigned int offset = *typestring_offset;
2246 type_t *ref = type_pointer_get_ref_type(type);
2248 print_start_tfs_comment(file, type, offset);
2249 update_tfsoff(type, offset, file);
2251 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2253 case TGT_BASIC:
2254 case TGT_ENUM:
2255 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2256 break;
2257 default:
2258 if (ref_offset)
2259 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2260 break;
2263 return offset;
2266 static int processed(const type_t *type)
2268 return type->typestring_offset && !type->tfswrite;
2271 static int user_type_has_variable_size(const type_t *t)
2273 if (is_ptr(t))
2274 return TRUE;
2275 else if (type_get_type(t) == TYPE_STRUCT)
2277 switch (get_struct_fc(t))
2279 case FC_PSTRUCT:
2280 case FC_CSTRUCT:
2281 case FC_CPSTRUCT:
2282 case FC_CVSTRUCT:
2283 return TRUE;
2286 /* Note: Since this only applies to user types, we can't have a conformant
2287 array here, and strings should get filed under pointer in this case. */
2288 return FALSE;
2291 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2293 unsigned int start, absoff, flags;
2294 const char *name = NULL;
2295 type_t *utype = get_user_type(type, &name);
2296 unsigned int usize = type_memsize(utype);
2297 unsigned int ualign = type_buffer_alignment(utype);
2298 unsigned int size = type_memsize(type);
2299 unsigned short funoff = user_type_offset(name);
2300 short reloff;
2302 if (processed(type)) return type->typestring_offset;
2304 guard_rec(type);
2306 if(user_type_has_variable_size(utype)) usize = 0;
2308 if (type_get_type(utype) == TYPE_BASIC ||
2309 type_get_type(utype) == TYPE_ENUM)
2311 unsigned char fc;
2313 if (type_get_type(utype) == TYPE_ENUM)
2314 fc = get_enum_fc(utype);
2315 else
2316 fc = get_basic_fc(utype);
2318 absoff = *tfsoff;
2319 print_start_tfs_comment(file, utype, absoff);
2320 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2321 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2322 *tfsoff += 2;
2324 else
2326 if (!processed(utype))
2327 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2328 absoff = utype->typestring_offset;
2331 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2332 flags = 0x40;
2333 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2334 flags = 0x80;
2335 else
2336 flags = 0;
2338 start = *tfsoff;
2339 update_tfsoff(type, start, file);
2340 print_start_tfs_comment(file, type, start);
2341 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2342 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2343 flags | (ualign - 1), ualign - 1, flags);
2344 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2345 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2346 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2347 *tfsoff += 8;
2348 reloff = absoff - *tfsoff;
2349 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2350 *tfsoff += 2;
2351 return start;
2354 static void write_member_type(FILE *file, const type_t *cont,
2355 int cont_is_complex, const attr_list_t *attrs,
2356 const type_t *type, unsigned int *corroff,
2357 unsigned int *tfsoff)
2359 if (is_embedded_complex(type) && !is_conformant_array(type))
2361 unsigned int absoff;
2362 short reloff;
2364 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2366 absoff = *corroff;
2367 *corroff += 8;
2369 else
2371 absoff = type->typestring_offset;
2373 reloff = absoff - (*tfsoff + 2);
2375 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2376 /* padding is represented using FC_STRUCTPAD* types, so presumably
2377 * this is left over in the format for historical purposes in MIDL
2378 * or rpcrt4. */
2379 print_file(file, 2, "0x0,\n");
2380 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2381 reloff, reloff, absoff);
2382 *tfsoff += 4;
2384 else if (is_ptr(type) || is_conformant_array(type))
2386 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2387 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2388 *tfsoff += 1;
2390 else if (!write_base_type(file, type, tfsoff))
2391 error("Unsupported member type %d\n", type_get_type(type));
2394 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2395 int cont_is_complex, unsigned int *tfsoff)
2397 type_t *elem = type_array_get_element_type(type);
2399 if (!is_embedded_complex(elem) && is_ptr(elem))
2401 type_t *ref = type_pointer_get_ref_type(elem);
2403 if (processed(ref))
2405 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2406 ref->typestring_offset, tfsoff);
2407 return;
2409 if (cont_is_complex && is_string_type(attrs, elem))
2411 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2412 return;
2414 if (!is_string_type(NULL, elem) &&
2415 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2417 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2418 return;
2421 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2424 static void write_end(FILE *file, unsigned int *tfsoff)
2426 if (*tfsoff % 2 == 0)
2428 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2429 *tfsoff += 1;
2431 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2432 *tfsoff += 1;
2435 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2437 unsigned int offset = 0;
2438 var_list_t *fs = type_struct_get_fields(type);
2439 var_t *f;
2441 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2443 type_t *ft = f->declspec.type;
2444 unsigned int size = field_memsize( ft, &offset );
2445 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2447 short reloff;
2448 unsigned int absoff = ft->typestring_offset;
2449 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2450 absoff += 8; /* we already have a corr descr, skip it */
2451 reloff = absoff - (*tfsoff + 6);
2452 print_file(file, 0, "/* %d */\n", *tfsoff);
2453 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2454 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2455 write_conf_or_var_desc(file, current_structure, offset, ft,
2456 get_attrp(f->attrs, ATTR_SWITCHIS));
2457 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2458 (unsigned short)reloff, reloff, absoff);
2459 *tfsoff += 8;
2461 offset += size;
2465 static int write_pointer_description_offsets(
2466 FILE *file, const attr_list_t *attrs, type_t *type,
2467 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2468 unsigned int *typestring_offset)
2470 int written = 0;
2472 if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
2473 (is_array(type) && type_array_is_decl_as_ptr(type)))
2475 if (offset_in_memory && offset_in_buffer)
2477 unsigned int memsize;
2479 /* pointer instance
2481 * note that MSDN states that for pointer layouts in structures,
2482 * this is a negative offset from the end of the structure, but
2483 * this statement is incorrect. all offsets are positive */
2484 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2485 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2487 memsize = type_memsize(type);
2488 *offset_in_memory += memsize;
2489 /* increment these separately as in the case of conformant (varying)
2490 * structures these start at different values */
2491 *offset_in_buffer += memsize;
2493 *typestring_offset += 4;
2495 if (is_ptr(type))
2497 type_t *ref = type_pointer_get_ref_type(type);
2499 if (is_string_type(attrs, type))
2500 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2501 else if (processed(ref))
2502 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2503 ref->typestring_offset, typestring_offset);
2504 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2505 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2506 else
2507 error("write_pointer_description_offsets: type format string unknown\n");
2509 else
2511 unsigned int offset = type->typestring_offset;
2512 /* skip over the pointer that is written for strings, since a
2513 * pointer has to be written in-place here */
2514 if (is_string_type(attrs, type))
2515 offset += 4;
2516 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2519 return 1;
2522 if (is_array(type))
2524 return write_pointer_description_offsets(
2525 file, attrs, type_array_get_element_type(type), offset_in_memory,
2526 offset_in_buffer, typestring_offset);
2528 else if (is_non_complex_struct(type))
2530 /* otherwise search for interesting fields to parse */
2531 const var_t *v;
2532 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2534 if (offset_in_memory && offset_in_buffer)
2536 unsigned int padding;
2537 unsigned int align = 0;
2538 type_memsize_and_alignment(v->declspec.type, &align);
2539 padding = ROUNDING(*offset_in_memory, align);
2540 *offset_in_memory += padding;
2541 *offset_in_buffer += padding;
2543 written += write_pointer_description_offsets(
2544 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2545 typestring_offset);
2548 else
2550 if (offset_in_memory && offset_in_buffer)
2552 unsigned int memsize = type_memsize(type);
2553 *offset_in_memory += memsize;
2554 /* increment these separately as in the case of conformant (varying)
2555 * structures these start at different values */
2556 *offset_in_buffer += memsize;
2560 return written;
2563 static int write_no_repeat_pointer_descriptions(
2564 FILE *file, const attr_list_t *attrs, type_t *type,
2565 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2566 unsigned int *typestring_offset)
2568 int written = 0;
2570 if (is_ptr(type) ||
2571 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2573 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2574 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2575 *typestring_offset += 2;
2577 return write_pointer_description_offsets(file, attrs, type,
2578 offset_in_memory, offset_in_buffer, typestring_offset);
2581 if (is_non_complex_struct(type))
2583 const var_t *v;
2584 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2586 if (offset_in_memory && offset_in_buffer)
2588 unsigned int padding;
2589 unsigned int align = 0;
2590 type_memsize_and_alignment(v->declspec.type, &align);
2591 padding = ROUNDING(*offset_in_memory, align);
2592 *offset_in_memory += padding;
2593 *offset_in_buffer += padding;
2595 written += write_no_repeat_pointer_descriptions(
2596 file, v->attrs, v->declspec.type,
2597 offset_in_memory, offset_in_buffer, typestring_offset);
2600 else
2602 unsigned int memsize = type_memsize(type);
2603 *offset_in_memory += memsize;
2604 /* increment these separately as in the case of conformant (varying)
2605 * structures these start at different values */
2606 *offset_in_buffer += memsize;
2609 return written;
2612 /* Note: if file is NULL return value is number of pointers to write, else
2613 * it is the number of type format characters written */
2614 static int write_fixed_array_pointer_descriptions(
2615 FILE *file, const attr_list_t *attrs, type_t *type,
2616 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2617 unsigned int *typestring_offset)
2619 int pointer_count = 0;
2621 if (type_get_type(type) == TYPE_ARRAY &&
2622 !type_array_has_conformance(type) && !type_array_has_variance(type))
2624 unsigned int temp = 0;
2625 /* unfortunately, this needs to be done in two passes to avoid
2626 * writing out redundant FC_FIXED_REPEAT descriptions */
2627 pointer_count = write_pointer_description_offsets(
2628 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2629 if (pointer_count > 0)
2631 unsigned int increment_size;
2632 unsigned int offset_of_array_pointer_mem = 0;
2633 unsigned int offset_of_array_pointer_buf = 0;
2635 increment_size = type_memsize(type_array_get_element_type(type));
2637 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2638 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2639 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2640 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2641 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2642 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2643 *typestring_offset += 10;
2645 pointer_count = write_pointer_description_offsets(
2646 file, attrs, type, &offset_of_array_pointer_mem,
2647 &offset_of_array_pointer_buf, typestring_offset);
2650 else if (type_get_type(type) == TYPE_STRUCT)
2652 const var_t *v;
2653 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2655 if (offset_in_memory && offset_in_buffer)
2657 unsigned int padding;
2658 unsigned int align = 0;
2659 type_memsize_and_alignment(v->declspec.type, &align);
2660 padding = ROUNDING(*offset_in_memory, align);
2661 *offset_in_memory += padding;
2662 *offset_in_buffer += padding;
2664 pointer_count += write_fixed_array_pointer_descriptions(
2665 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2666 typestring_offset);
2669 else
2671 if (offset_in_memory && offset_in_buffer)
2673 unsigned int memsize;
2674 memsize = type_memsize(type);
2675 *offset_in_memory += memsize;
2676 /* increment these separately as in the case of conformant (varying)
2677 * structures these start at different values */
2678 *offset_in_buffer += memsize;
2682 return pointer_count;
2685 /* Note: if file is NULL return value is number of pointers to write, else
2686 * it is the number of type format characters written */
2687 static int write_conformant_array_pointer_descriptions(
2688 FILE *file, const attr_list_t *attrs, type_t *type,
2689 unsigned int offset_in_memory, unsigned int *typestring_offset)
2691 int pointer_count = 0;
2693 if (is_conformant_array(type) && !type_array_has_variance(type))
2695 unsigned int temp = 0;
2696 /* unfortunately, this needs to be done in two passes to avoid
2697 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2698 pointer_count = write_pointer_description_offsets(
2699 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2700 if (pointer_count > 0)
2702 unsigned int increment_size;
2703 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2704 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2706 increment_size = type_memsize(type_array_get_element_type(type));
2708 if (increment_size > USHRT_MAX)
2709 error("array size of %u bytes is too large\n", increment_size);
2711 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2712 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2713 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2714 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2715 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2716 *typestring_offset += 8;
2718 pointer_count = write_pointer_description_offsets(
2719 file, attrs, type_array_get_element_type(type),
2720 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2721 typestring_offset);
2725 return pointer_count;
2728 /* Note: if file is NULL return value is number of pointers to write, else
2729 * it is the number of type format characters written */
2730 static int write_varying_array_pointer_descriptions(
2731 FILE *file, const attr_list_t *attrs, type_t *type,
2732 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2733 unsigned int *typestring_offset)
2735 int pointer_count = 0;
2737 if (is_array(type) && type_array_has_variance(type))
2739 unsigned int temp = 0;
2740 /* unfortunately, this needs to be done in two passes to avoid
2741 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2742 pointer_count = write_pointer_description_offsets(
2743 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2744 if (pointer_count > 0)
2746 unsigned int increment_size;
2748 increment_size = type_memsize(type_array_get_element_type(type));
2750 if (increment_size > USHRT_MAX)
2751 error("array size of %u bytes is too large\n", increment_size);
2753 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2754 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2755 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2756 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2757 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2758 *typestring_offset += 8;
2760 pointer_count = write_pointer_description_offsets(
2761 file, attrs, type_array_get_element_type(type), offset_in_memory,
2762 offset_in_buffer, typestring_offset);
2765 else if (type_get_type(type) == TYPE_STRUCT)
2767 const var_t *v;
2768 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2770 if (offset_in_memory && offset_in_buffer)
2772 unsigned int align = 0, padding;
2774 if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
2776 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2777 /* skip over variance and offset in buffer */
2778 *offset_in_buffer += 8;
2781 type_memsize_and_alignment(v->declspec.type, &align);
2782 padding = ROUNDING(*offset_in_memory, align);
2783 *offset_in_memory += padding;
2784 *offset_in_buffer += padding;
2786 pointer_count += write_varying_array_pointer_descriptions(
2787 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2788 typestring_offset);
2791 else
2793 if (offset_in_memory && offset_in_buffer)
2795 unsigned int memsize = type_memsize(type);
2796 *offset_in_memory += memsize;
2797 /* increment these separately as in the case of conformant (varying)
2798 * structures these start at different values */
2799 *offset_in_buffer += memsize;
2803 return pointer_count;
2806 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2807 unsigned int *typestring_offset)
2809 unsigned int offset_in_buffer;
2810 unsigned int offset_in_memory;
2812 /* pass 1: search for single instance of a pointer (i.e. don't descend
2813 * into arrays) */
2814 if (!is_array(type))
2816 offset_in_memory = 0;
2817 offset_in_buffer = 0;
2818 write_no_repeat_pointer_descriptions(
2819 file, NULL, type,
2820 &offset_in_memory, &offset_in_buffer, typestring_offset);
2823 /* pass 2: search for pointers in fixed arrays */
2824 offset_in_memory = 0;
2825 offset_in_buffer = 0;
2826 write_fixed_array_pointer_descriptions(
2827 file, NULL, type,
2828 &offset_in_memory, &offset_in_buffer, typestring_offset);
2830 /* pass 3: search for pointers in conformant only arrays (but don't descend
2831 * into conformant varying or varying arrays) */
2832 if (is_conformant_array(type) &&
2833 (type_array_is_decl_as_ptr(type) || !current_structure))
2834 write_conformant_array_pointer_descriptions(
2835 file, attrs, type, 0, typestring_offset);
2836 else if (type_get_type(type) == TYPE_STRUCT &&
2837 get_struct_fc(type) == FC_CPSTRUCT)
2839 type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
2840 write_conformant_array_pointer_descriptions( file, NULL, carray,
2841 type_memsize(type), typestring_offset);
2844 /* pass 4: search for pointers in varying arrays */
2845 offset_in_memory = 0;
2846 offset_in_buffer = 0;
2847 write_varying_array_pointer_descriptions(
2848 file, NULL, type,
2849 &offset_in_memory, &offset_in_buffer, typestring_offset);
2852 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2853 type_t *type, enum type_context context,
2854 const char *name, unsigned int *typestring_offset)
2856 unsigned int start_offset;
2857 unsigned char rtype;
2858 type_t *elem_type;
2859 int is_processed = processed(type);
2861 start_offset = *typestring_offset;
2863 if (is_declptr(type))
2865 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2866 int pointer_type = get_pointer_fc_context(type, attrs, context);
2867 if (!pointer_type)
2868 pointer_type = FC_RP;
2869 print_start_tfs_comment(file, type, *typestring_offset);
2870 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2871 pointer_type, flag, string_of_type(pointer_type),
2872 flag ? " [simple_pointer]" : "");
2873 *typestring_offset += 2;
2874 if (!flag)
2876 print_file(file, 2, "NdrFcShort(0x2),\n");
2877 *typestring_offset += 2;
2879 is_processed = FALSE;
2882 if (is_array(type))
2883 elem_type = type_array_get_element_type(type);
2884 else
2885 elem_type = type_pointer_get_ref_type(type);
2887 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2888 return write_array_tfs(file, attrs, type, name, typestring_offset);
2890 if (type_get_type(elem_type) != TYPE_BASIC)
2892 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2893 return start_offset;
2896 rtype = get_basic_fc(elem_type);
2897 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2899 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2900 return start_offset;
2903 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2905 unsigned int dim = type_array_get_dim(type);
2907 if (is_processed) return start_offset;
2909 /* FIXME: multi-dimensional array */
2910 if (0xffffu < dim)
2911 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2912 name, 0xffffu, dim - 0xffffu);
2914 if (rtype == FC_WCHAR)
2915 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2916 else
2917 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2918 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2919 *typestring_offset += 2;
2921 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2922 *typestring_offset += 2;
2924 update_tfsoff(type, start_offset, file);
2925 return start_offset;
2927 else if (is_conformant_array(type))
2929 if (rtype == FC_WCHAR)
2930 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2931 else
2932 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2933 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2934 *typestring_offset += 2;
2936 *typestring_offset += write_conf_or_var_desc(
2937 file, current_structure,
2938 (!type_array_is_decl_as_ptr(type) && current_structure
2939 ? type_memsize(current_structure)
2940 : 0),
2941 type, type_array_get_conformance(type));
2943 update_tfsoff(type, start_offset, file);
2944 return start_offset;
2946 else
2948 if (is_processed) return start_offset;
2950 if (rtype == FC_WCHAR)
2951 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2952 else
2953 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2954 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2955 *typestring_offset += 2;
2957 update_tfsoff(type, start_offset, file);
2958 return start_offset;
2962 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2963 const char *name, unsigned int *typestring_offset)
2965 const expr_t *length_is = type_array_get_variance(type);
2966 const expr_t *size_is = type_array_get_conformance(type);
2967 unsigned int align;
2968 unsigned int size;
2969 unsigned int start_offset;
2970 unsigned char fc;
2971 unsigned int baseoff
2972 = !type_array_is_decl_as_ptr(type) && current_structure
2973 ? type_memsize(current_structure)
2974 : 0;
2976 if (!is_string_type(attrs, type_array_get_element_type(type)))
2977 write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
2979 size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2980 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2981 fc = get_array_fc(type);
2983 start_offset = *typestring_offset;
2984 update_tfsoff(type, start_offset, file);
2985 print_start_tfs_comment(file, type, start_offset);
2986 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2987 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2988 *typestring_offset += 2;
2990 align = 0;
2991 if (fc != FC_BOGUS_ARRAY)
2993 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
2995 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2996 *typestring_offset += 4;
2998 else
3000 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
3001 *typestring_offset += 2;
3004 if (is_conformant_array(type))
3005 *typestring_offset
3006 += write_conf_or_var_desc(file, current_structure, baseoff,
3007 type, size_is);
3009 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
3011 unsigned int elsize = type_memsize(type_array_get_element_type(type));
3012 unsigned int dim = type_array_get_dim(type);
3014 if (fc == FC_LGVARRAY)
3016 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
3017 *typestring_offset += 4;
3019 else
3021 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3022 *typestring_offset += 2;
3025 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
3026 *typestring_offset += 2;
3029 if (length_is)
3030 *typestring_offset
3031 += write_conf_or_var_desc(file, current_structure, baseoff,
3032 type, length_is);
3034 if (type_has_pointers(type_array_get_element_type(type)) &&
3035 (type_array_is_decl_as_ptr(type) || !current_structure))
3037 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3038 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3039 *typestring_offset += 2;
3040 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
3041 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3042 *typestring_offset += 1;
3045 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
3046 write_end(file, typestring_offset);
3048 else
3050 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
3051 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3052 *typestring_offset += 2;
3053 *typestring_offset
3054 += write_conf_or_var_desc(file, current_structure, baseoff,
3055 type, size_is);
3056 *typestring_offset
3057 += write_conf_or_var_desc(file, current_structure, baseoff,
3058 type, length_is);
3060 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
3061 write_end(file, typestring_offset);
3064 return start_offset;
3067 static const var_t *find_array_or_string_in_struct(const type_t *type)
3069 const var_list_t *fields = type_struct_get_fields(type);
3070 const var_t *last_field;
3071 const type_t *ft;
3073 if (!fields || list_empty(fields))
3074 return NULL;
3076 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3077 ft = last_field->declspec.type;
3079 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3080 return last_field;
3082 if (type_get_type(ft) == TYPE_STRUCT)
3083 return find_array_or_string_in_struct(ft);
3084 else
3085 return NULL;
3088 static void write_struct_members(FILE *file, const type_t *type,
3089 int is_complex, unsigned int *corroff,
3090 unsigned int *typestring_offset)
3092 const var_t *field;
3093 unsigned short offset = 0;
3094 unsigned int salign = 1;
3095 int padding;
3096 var_list_t *fields = type_struct_get_fields(type);
3098 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3100 type_t *ft = field->declspec.type;
3101 unsigned int align = 0;
3102 unsigned int size = type_memsize_and_alignment(ft, &align);
3103 align = clamp_align(align);
3104 if (salign < align) salign = align;
3106 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3108 if ((align - 1) & offset)
3110 unsigned char fc = 0;
3111 switch (align)
3113 case 2:
3114 fc = FC_ALIGNM2;
3115 break;
3116 case 4:
3117 fc = FC_ALIGNM4;
3118 break;
3119 case 8:
3120 fc = FC_ALIGNM8;
3121 break;
3122 default:
3123 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3125 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3126 offset = ROUND_SIZE(offset, align);
3127 *typestring_offset += 1;
3129 write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
3130 typestring_offset);
3131 offset += size;
3135 padding = ROUNDING(offset, salign);
3136 if (padding)
3138 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3139 FC_STRUCTPAD1 + padding - 1,
3140 padding);
3141 *typestring_offset += 1;
3144 write_end(file, typestring_offset);
3147 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3148 const char *name, unsigned int *tfsoff)
3150 const type_t *save_current_structure = current_structure;
3151 unsigned int total_size;
3152 const var_t *array;
3153 unsigned int start_offset;
3154 unsigned int align;
3155 unsigned int corroff;
3156 var_t *f;
3157 unsigned char fc = get_struct_fc(type);
3158 var_list_t *fields = type_struct_get_fields(type);
3160 if (processed(type)) return type->typestring_offset;
3162 guard_rec(type);
3163 current_structure = type;
3165 total_size = type_memsize(type);
3166 align = type_buffer_alignment(type);
3167 if (total_size > USHRT_MAX)
3168 error("structure size for %s exceeds %d bytes by %d bytes\n",
3169 name, USHRT_MAX, total_size - USHRT_MAX);
3171 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3172 write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
3174 array = find_array_or_string_in_struct(type);
3175 if (array && !processed(array->declspec.type))
3177 if(is_string_type(array->attrs, array->declspec.type))
3178 write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3179 else
3180 write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
3183 corroff = *tfsoff;
3184 write_descriptors(file, type, tfsoff);
3186 start_offset = *tfsoff;
3187 update_tfsoff(type, start_offset, file);
3188 print_start_tfs_comment(file, type, start_offset);
3189 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3190 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3191 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3192 *tfsoff += 4;
3194 if (array)
3196 unsigned int absoff = array->declspec.type->typestring_offset;
3197 short reloff = absoff - *tfsoff;
3198 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3199 reloff, reloff, absoff);
3200 *tfsoff += 2;
3202 else if (fc == FC_BOGUS_STRUCT)
3204 print_file(file, 2, "NdrFcShort(0x0),\n");
3205 *tfsoff += 2;
3208 if (fc == FC_BOGUS_STRUCT)
3210 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3211 nothing is written to file yet. On the actual writing pass,
3212 this will have been updated. */
3213 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3214 int reloff = absoff - *tfsoff;
3215 assert( reloff >= 0 );
3216 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3217 (unsigned short)reloff, reloff, absoff);
3218 *tfsoff += 2;
3220 else if ((fc == FC_PSTRUCT) ||
3221 (fc == FC_CPSTRUCT) ||
3222 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3224 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3225 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3226 *tfsoff += 2;
3227 write_pointer_description(file, NULL, type, tfsoff);
3228 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3229 *tfsoff += 1;
3232 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3233 tfsoff);
3235 if (fc == FC_BOGUS_STRUCT)
3237 const var_t *f;
3239 type->ptrdesc = *tfsoff;
3240 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3242 type_t *ft = f->declspec.type;
3243 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3245 case TGT_POINTER:
3246 if (is_string_type(f->attrs, ft))
3247 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3248 else
3249 write_pointer_tfs(file, f->attrs, ft,
3250 type_pointer_get_ref_type(ft)->typestring_offset,
3251 TYPE_CONTEXT_CONTAINER, tfsoff);
3252 break;
3253 case TGT_ARRAY:
3254 if (type_array_is_decl_as_ptr(ft))
3256 unsigned int offset;
3258 print_file(file, 0, "/* %d */\n", *tfsoff);
3260 offset = ft->typestring_offset;
3261 /* skip over the pointer that is written for strings, since a
3262 * pointer has to be written in-place here */
3263 if (is_string_type(f->attrs, ft))
3264 offset += 4;
3265 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3267 break;
3268 default:
3269 break;
3272 if (type->ptrdesc == *tfsoff)
3273 type->ptrdesc = 0;
3276 current_structure = save_current_structure;
3277 return start_offset;
3280 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3282 if (t == NULL)
3284 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3286 else
3288 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3290 unsigned char fc;
3291 if (type_get_type(t) == TYPE_BASIC)
3292 fc = get_basic_fc(t);
3293 else
3294 fc = get_enum_fc(t);
3295 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3296 fc, string_of_type(fc));
3298 else if (t->typestring_offset)
3300 short reloff = t->typestring_offset - *tfsoff;
3301 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3302 reloff, reloff, t->typestring_offset);
3304 else
3305 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3308 *tfsoff += 2;
3311 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3312 type_t *type, unsigned int *tfsoff)
3314 unsigned int start_offset;
3315 unsigned int size;
3316 var_list_t *fields;
3317 unsigned int nbranch = 0;
3318 type_t *deftype = NULL;
3319 short nodeftype = 0xffff;
3320 unsigned int dummy;
3321 var_t *f;
3323 if (processed(type) &&
3324 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3325 return type->typestring_offset;
3327 guard_rec(type);
3329 fields = type_union_get_cases(type);
3331 size = union_memsize(fields, &dummy);
3333 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3335 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3336 if (cases)
3337 nbranch += list_count(cases);
3338 if (f->declspec.type)
3339 write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
3342 start_offset = *tfsoff;
3343 update_tfsoff(type, start_offset, file);
3344 print_start_tfs_comment(file, type, start_offset);
3345 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3347 const var_t *sv = type_union_get_switch_value(type);
3348 const type_t *st = sv->declspec.type;
3349 unsigned int align = 0;
3350 unsigned char fc;
3352 if (type_get_type(st) == TYPE_BASIC)
3354 fc = get_basic_fc(st);
3355 switch (fc)
3357 case FC_CHAR:
3358 case FC_SMALL:
3359 case FC_BYTE:
3360 case FC_USMALL:
3361 case FC_WCHAR:
3362 case FC_SHORT:
3363 case FC_USHORT:
3364 case FC_LONG:
3365 case FC_ULONG:
3366 break;
3367 default:
3368 fc = 0;
3369 error("union switch type must be an integer, char, or enum\n");
3372 else if (type_get_type(st) == TYPE_ENUM)
3373 fc = get_enum_fc(st);
3374 else
3375 error("union switch type must be an integer, char, or enum\n");
3377 type_memsize_and_alignment(st, &align);
3378 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3380 if (f->declspec.type)
3381 type_memsize_and_alignment(f->declspec.type, &align);
3384 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3385 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3386 (align << 4) | fc, string_of_type(fc));
3387 *tfsoff += 2;
3389 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3391 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3392 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3393 unsigned char fc;
3395 if (type_get_type(st) == TYPE_BASIC)
3397 fc = get_basic_fc(st);
3398 switch (fc)
3400 case FC_CHAR:
3401 case FC_SMALL:
3402 case FC_USMALL:
3403 case FC_SHORT:
3404 case FC_USHORT:
3405 case FC_LONG:
3406 case FC_ULONG:
3407 case FC_ENUM16:
3408 case FC_ENUM32:
3409 break;
3410 default:
3411 fc = 0;
3412 error("union switch type must be an integer, char, or enum\n");
3415 else if (type_get_type(st) == TYPE_ENUM)
3416 fc = get_enum_fc(st);
3417 else
3418 error("union switch type must be an integer, char, or enum\n");
3420 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3421 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3422 fc, string_of_type(fc));
3423 *tfsoff += 2;
3424 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3425 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3426 *tfsoff += 2;
3427 print_file(file, 0, "/* %u */\n", *tfsoff);
3430 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3431 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3432 *tfsoff += 4;
3434 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3436 type_t *ft = f->declspec.type;
3437 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3438 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3439 expr_t *c;
3441 if (cases == NULL && !deflt)
3442 error("union field %s with neither case nor default attribute\n", f->name);
3444 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3446 /* MIDL doesn't check for duplicate cases, even though that seems
3447 like a reasonable thing to do, it just dumps them to the TFS
3448 like we're going to do here. */
3449 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3450 *tfsoff += 4;
3451 write_branch_type(file, ft, tfsoff);
3454 /* MIDL allows multiple default branches, even though that seems
3455 illogical, it just chooses the last one, which is what we will
3456 do. */
3457 if (deflt)
3459 deftype = ft;
3460 nodeftype = 0;
3464 if (deftype)
3466 write_branch_type(file, deftype, tfsoff);
3468 else
3470 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3471 *tfsoff += 2;
3474 return start_offset;
3477 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3478 unsigned int *typeformat_offset)
3480 unsigned int i;
3481 unsigned int start_offset = *typeformat_offset;
3482 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3484 if (!iid && processed(type)) return type->typestring_offset;
3486 print_start_tfs_comment(file, type, start_offset);
3487 update_tfsoff(type, start_offset, file);
3489 if (iid)
3491 print_file(file, 2, "0x2f, /* FC_IP */\n");
3492 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3493 *typeformat_offset
3494 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3496 else
3498 const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
3499 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3501 if (! uuid)
3502 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3504 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3505 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3506 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3507 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3508 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3509 for (i = 0; i < 8; ++i)
3510 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3512 if (file)
3513 fprintf(file, "\n");
3515 *typeformat_offset += 18;
3517 return start_offset;
3520 static unsigned int write_contexthandle_tfs(FILE *file,
3521 const attr_list_t *attrs,
3522 type_t *type,
3523 enum type_context context,
3524 unsigned int *typeformat_offset)
3526 unsigned int start_offset = *typeformat_offset;
3527 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
3529 print_start_tfs_comment(file, type, start_offset);
3531 if (flags & 0x80) /* via ptr */
3533 int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
3534 if (!pointer_type) pointer_type = FC_RP;
3535 *typeformat_offset += 4;
3536 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3537 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3538 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3541 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3542 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3543 if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
3544 print_file(file, 0, "can't be null, ");
3545 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3546 print_file(file, 0, "serialize, ");
3547 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3548 print_file(file, 0, "no serialize, ");
3549 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3550 print_file(file, 0, "strict, ");
3551 if (flags & HANDLE_PARAM_IS_RETURN)
3552 print_file(file, 0, "return, ");
3553 if (flags & HANDLE_PARAM_IS_OUT)
3554 print_file(file, 0, "out, ");
3555 if (flags & HANDLE_PARAM_IS_IN)
3556 print_file(file, 0, "in, ");
3557 if (flags & HANDLE_PARAM_IS_VIA_PTR)
3558 print_file(file, 0, "via ptr, ");
3559 print_file(file, 0, "*/\n");
3560 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3561 print_file(file, 2, "0, /* FIXME: param num */\n");
3562 *typeformat_offset += 4;
3564 update_tfsoff( type, start_offset, file );
3565 return start_offset;
3568 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3569 type_t *type, expr_list_t *range_list,
3570 unsigned int *typeformat_offset)
3572 unsigned char fc;
3573 unsigned int start_offset = *typeformat_offset;
3574 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3575 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3577 if (type_get_type(type) == TYPE_BASIC)
3578 fc = get_basic_fc(type);
3579 else
3580 fc = get_enum_fc(type);
3582 /* fc must fit in lower 4-bits of 8-bit field below */
3583 assert(fc <= 0xf);
3585 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3586 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3587 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3588 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3589 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3590 update_tfsoff( type, start_offset, file );
3591 *typeformat_offset += 10;
3593 return start_offset;
3596 static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
3597 type_t *type, const char *name,
3598 enum type_context context,
3599 unsigned int *typeformat_offset)
3601 unsigned int offset;
3603 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3605 case TGT_CTXT_HANDLE:
3606 case TGT_CTXT_HANDLE_POINTER:
3607 return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
3608 case TGT_USER_TYPE:
3609 return write_user_tfs(file, type, typeformat_offset);
3610 case TGT_STRING:
3611 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3612 case TGT_ARRAY:
3614 unsigned int off;
3615 /* conformant and pointer arrays are handled specially */
3616 if ((context != TYPE_CONTEXT_CONTAINER &&
3617 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3618 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3619 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3620 else
3621 off = 0;
3622 if (context != TYPE_CONTEXT_CONTAINER &&
3623 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3625 int ptr_type;
3626 ptr_type = get_pointer_fc_context(type, attrs, context);
3627 if (type_array_is_decl_as_ptr(type)
3628 || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
3630 unsigned int absoff = type->typestring_offset;
3631 short reloff = absoff - (*typeformat_offset + 2);
3632 off = *typeformat_offset;
3633 print_file(file, 0, "/* %d */\n", off);
3634 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3635 string_of_type(ptr_type));
3636 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3637 reloff, reloff, absoff);
3638 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3639 *typeformat_offset += 4;
3641 type_array_set_ptr_tfsoff(type, off);
3643 return off;
3645 case TGT_STRUCT:
3646 return write_struct_tfs(file, type, name, typeformat_offset);
3647 case TGT_UNION:
3648 return write_union_tfs(file, attrs, type, typeformat_offset);
3649 case TGT_ENUM:
3650 case TGT_BASIC:
3651 /* nothing to do */
3652 return 0;
3653 case TGT_RANGE:
3655 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3656 if (!range_list)
3657 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3658 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3660 case TGT_IFACE_POINTER:
3661 return write_ip_tfs(file, attrs, type, typeformat_offset);
3662 case TGT_POINTER:
3664 enum type_context ref_context;
3665 type_t *ref = type_pointer_get_ref_type(type);
3667 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3668 ref_context = TYPE_CONTEXT_PARAM;
3669 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3670 ref_context = TYPE_CONTEXT_CONTAINER;
3671 else
3672 ref_context = context;
3674 if (is_string_type(attrs, ref))
3676 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3677 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3679 offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
3680 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3681 return 0;
3682 return offset;
3685 offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
3686 ref_context, typeformat_offset);
3687 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3688 return 0;
3689 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3691 case TGT_INVALID:
3692 break;
3694 error("invalid type %s for var %s\n", type->name, name);
3695 return 0;
3698 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3699 const char *name, int write_ptr, unsigned int *tfsoff)
3701 return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3704 static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
3706 const statement_list_t *stmts = type_iface_get_stmts(iface);
3707 const statement_t *stmt;
3708 var_t *var;
3710 current_iface = iface;
3711 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
3713 switch(stmt->type)
3715 case STMT_DECLARATION:
3717 const var_t *func = stmt->u.var;
3719 if(stmt->u.var->declspec.stgclass != STG_NONE
3720 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
3721 continue;
3723 current_func = func;
3724 if (is_local(func->attrs)) continue;
3726 var = type_function_get_retval(func->declspec.type);
3727 if (!is_void(var->declspec.type))
3728 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
3729 TYPE_CONTEXT_RETVAL, offset);
3731 if (type_function_get_args(func->declspec.type))
3732 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
3733 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
3734 TYPE_CONTEXT_TOPLEVELPARAM, offset );
3735 break;
3738 case STMT_TYPEDEF:
3740 const type_list_t *type_entry;
3741 for (type_entry = stmt->u.type_list; type_entry; type_entry = type_entry->next)
3743 if (is_attr(type_entry->type->attrs, ATTR_ENCODE)
3744 || is_attr(type_entry->type->attrs, ATTR_DECODE))
3745 type_entry->type->typestring_offset = write_type_tfs( file,
3746 type_entry->type->attrs, type_entry->type, type_entry->type->name,
3747 TYPE_CONTEXT_CONTAINER, offset);
3749 break;
3751 default:
3752 break;
3757 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3759 unsigned int typeformat_offset = 2;
3760 for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
3761 return typeformat_offset + 1;
3765 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3767 int indent = 0;
3769 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3770 print_file(file, indent, "{\n");
3771 indent++;
3772 print_file(file, indent, "0,\n");
3773 print_file(file, indent, "{\n");
3774 indent++;
3775 print_file(file, indent, "NdrFcShort(0x0),\n");
3777 set_all_tfswrite(TRUE);
3778 process_tfs(file, stmts, pred);
3780 print_file(file, indent, "0x0\n");
3781 indent--;
3782 print_file(file, indent, "}\n");
3783 indent--;
3784 print_file(file, indent, "};\n");
3785 print_file(file, indent, "\n");
3788 static unsigned int get_required_buffer_size_type(
3789 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3791 *alignment = 0;
3792 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3794 case TGT_USER_TYPE:
3796 const char *uname = NULL;
3797 const type_t *utype = get_user_type(type, &uname);
3798 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3800 case TGT_BASIC:
3801 switch (get_basic_fc(type))
3803 case FC_BYTE:
3804 case FC_CHAR:
3805 case FC_USMALL:
3806 case FC_SMALL:
3807 *alignment = 4;
3808 return 1;
3810 case FC_WCHAR:
3811 case FC_USHORT:
3812 case FC_SHORT:
3813 *alignment = 4;
3814 return 2;
3816 case FC_ULONG:
3817 case FC_LONG:
3818 case FC_FLOAT:
3819 case FC_ERROR_STATUS_T:
3820 *alignment = 4;
3821 return 4;
3823 case FC_HYPER:
3824 case FC_DOUBLE:
3825 *alignment = 8;
3826 return 8;
3828 case FC_INT3264:
3829 case FC_UINT3264:
3830 assert( pointer_size );
3831 *alignment = pointer_size;
3832 return pointer_size;
3834 case FC_IGNORE:
3835 case FC_BIND_PRIMITIVE:
3836 return 0;
3838 default:
3839 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3840 get_basic_fc(type));
3841 return 0;
3843 break;
3845 case TGT_ENUM:
3846 switch (get_enum_fc(type))
3848 case FC_ENUM32:
3849 *alignment = 4;
3850 return 4;
3851 case FC_ENUM16:
3852 *alignment = 4;
3853 return 2;
3855 break;
3857 case TGT_STRUCT:
3858 if (get_struct_fc(type) == FC_STRUCT)
3860 if (!type_struct_get_fields(type)) return 0;
3861 return fields_memsize(type_struct_get_fields(type), alignment);
3863 break;
3865 case TGT_POINTER:
3867 unsigned int size, align;
3868 const type_t *ref = type_pointer_get_ref_type(type);
3869 if (is_string_type( attrs, ref )) break;
3870 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3871 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3873 size += 4 + align;
3874 align = 4;
3876 *alignment = align;
3877 return size;
3880 case TGT_ARRAY:
3881 switch (get_array_fc(type))
3883 case FC_SMFARRAY:
3884 case FC_LGFARRAY:
3885 return type_array_get_dim(type) *
3886 get_required_buffer_size_type(type_array_get_element_type(type), name,
3887 NULL, FALSE, alignment);
3889 break;
3891 default:
3892 break;
3894 return 0;
3897 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3899 int in_attr = is_attr(var->attrs, ATTR_IN);
3900 int out_attr = is_attr(var->attrs, ATTR_OUT);
3902 if (!in_attr && !out_attr)
3903 in_attr = 1;
3905 *alignment = 0;
3907 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3908 pass == PASS_RETURN)
3910 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3912 *alignment = 4;
3913 return 20;
3916 if (!is_string_type(var->attrs, var->declspec.type))
3917 return get_required_buffer_size_type(var->declspec.type, var->name,
3918 var->attrs, TRUE, alignment);
3920 return 0;
3923 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3925 const var_t *var;
3926 unsigned int total_size = 0, alignment;
3928 if (type_function_get_args(func->declspec.type))
3930 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
3932 total_size += get_required_buffer_size(var, &alignment, pass);
3933 total_size += alignment;
3937 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
3939 var_t v = *func;
3940 v.declspec.type = type_function_get_rettype(func->declspec.type);
3941 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3942 total_size += alignment;
3944 return total_size;
3947 static void print_phase_function(FILE *file, int indent, const char *type,
3948 const char *local_var_prefix, enum remoting_phase phase,
3949 const var_t *var, unsigned int type_offset)
3951 const char *function;
3952 switch (phase)
3954 case PHASE_BUFFERSIZE:
3955 function = "BufferSize";
3956 break;
3957 case PHASE_MARSHAL:
3958 function = "Marshall";
3959 break;
3960 case PHASE_UNMARSHAL:
3961 function = "Unmarshall";
3962 break;
3963 case PHASE_FREE:
3964 function = "Free";
3965 break;
3966 default:
3967 assert(0);
3968 return;
3971 print_file(file, indent, "Ndr%s%s(\n", type, function);
3972 indent++;
3973 print_file(file, indent, "&__frame->_StubMsg,\n");
3974 print_file(file, indent, "%s%s%s%s%s,\n",
3975 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3976 (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
3977 local_var_prefix,
3978 (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
3979 var->name);
3980 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3981 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3982 if (phase == PHASE_UNMARSHAL)
3983 print_file(file, indent, "0);\n");
3984 indent--;
3987 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3988 enum remoting_phase phase, enum pass pass, const var_t *var,
3989 const char *varname)
3991 type_t *type = var->declspec.type;
3992 unsigned int alignment = 0;
3994 /* no work to do for other phases, buffer sizing is done elsewhere */
3995 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3996 return;
3998 if (type_get_type(type) == TYPE_ENUM ||
3999 (type_get_type(type) == TYPE_BASIC &&
4000 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
4001 pointer_size != 4))
4003 unsigned char fc;
4005 if (type_get_type(type) == TYPE_ENUM)
4006 fc = get_enum_fc(type);
4007 else
4008 fc = get_basic_fc(type);
4010 if (phase == PHASE_MARSHAL)
4011 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
4012 else
4013 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
4014 print_file(file, indent+1, "&__frame->_StubMsg,\n");
4015 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
4016 local_var_prefix,
4017 var->name);
4018 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
4020 else
4022 const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
4024 switch (get_basic_fc(ref->type))
4026 case FC_BYTE:
4027 case FC_CHAR:
4028 case FC_SMALL:
4029 case FC_USMALL:
4030 alignment = 1;
4031 break;
4033 case FC_WCHAR:
4034 case FC_USHORT:
4035 case FC_SHORT:
4036 alignment = 2;
4037 break;
4039 case FC_ULONG:
4040 case FC_LONG:
4041 case FC_FLOAT:
4042 case FC_ERROR_STATUS_T:
4043 /* pointer_size must be 4 if we got here in these two cases */
4044 case FC_INT3264:
4045 case FC_UINT3264:
4046 alignment = 4;
4047 break;
4049 case FC_HYPER:
4050 case FC_DOUBLE:
4051 alignment = 8;
4052 break;
4054 case FC_IGNORE:
4055 case FC_BIND_PRIMITIVE:
4056 /* no marshalling needed */
4057 return;
4059 default:
4060 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
4061 var->name, get_basic_fc(ref->type));
4064 if (phase == PHASE_MARSHAL && alignment > 1)
4065 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
4066 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
4067 alignment - 1, alignment - 1);
4069 if (phase == PHASE_MARSHAL)
4071 print_file(file, indent, "*(");
4072 write_type_decl(file, ref, NULL);
4073 if (is_ptr(type))
4074 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
4075 else
4076 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4077 fprintf(file, "%s%s", local_var_prefix, varname);
4078 fprintf(file, ";\n");
4080 else if (phase == PHASE_UNMARSHAL)
4082 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4083 write_type_decl(file, ref, NULL);
4084 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4085 print_file(file, indent, "{\n");
4086 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4087 print_file(file, indent, "}\n");
4088 print_file(file, indent, "%s%s%s",
4089 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4090 local_var_prefix, varname);
4091 if (pass == PASS_IN && is_ptr(type))
4092 fprintf(file, " = (");
4093 else
4094 fprintf(file, " = *(");
4095 write_type_decl(file, ref, NULL);
4096 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4099 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4100 write_type_decl(file, ref, NULL);
4101 fprintf(file, ");\n");
4105 /* returns whether the MaxCount, Offset or ActualCount members need to be
4106 * filled in for the specified phase */
4107 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4109 return (phase != PHASE_UNMARSHAL);
4112 expr_t *get_size_is_expr(const type_t *t, const char *name)
4114 expr_t *x = NULL;
4116 for ( ; is_array(t); t = type_array_get_element_type(t))
4117 if (type_array_has_conformance(t) &&
4118 type_array_get_conformance(t)->type != EXPR_VOID)
4120 if (!x)
4121 x = type_array_get_conformance(t);
4122 else
4123 error("%s: multidimensional conformant"
4124 " arrays not supported at the top level\n",
4125 name);
4128 return x;
4131 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4132 enum remoting_phase phase, const var_t *var, int valid_variance)
4134 const type_t *type = var->declspec.type;
4135 /* get fundamental type for the argument */
4136 for (;;)
4138 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4140 case TGT_ARRAY:
4141 if (is_conformance_needed_for_phase(phase))
4143 if (type_array_has_conformance(type) &&
4144 type_array_get_conformance(type)->type != EXPR_VOID)
4146 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4147 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4148 fprintf(file, ";\n\n");
4150 if (type_array_has_variance(type))
4152 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4153 if (valid_variance)
4155 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4156 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4157 fprintf(file, ";\n\n");
4159 else
4160 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4163 break;
4164 case TGT_UNION:
4165 if (type_get_type(type) == TYPE_UNION &&
4166 is_conformance_needed_for_phase(phase))
4168 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4169 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4170 fprintf(file, ";\n\n");
4172 break;
4173 case TGT_IFACE_POINTER:
4175 expr_t *iid;
4177 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4179 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4180 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4181 fprintf( file, ";\n\n" );
4183 break;
4185 case TGT_POINTER:
4186 type = type_pointer_get_ref_type(type);
4187 continue;
4188 case TGT_INVALID:
4189 case TGT_USER_TYPE:
4190 case TGT_CTXT_HANDLE:
4191 case TGT_CTXT_HANDLE_POINTER:
4192 case TGT_STRING:
4193 case TGT_BASIC:
4194 case TGT_ENUM:
4195 case TGT_STRUCT:
4196 case TGT_RANGE:
4197 break;
4199 break;
4203 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4204 enum pass pass, enum remoting_phase phase, const var_t *var)
4206 int in_attr, out_attr, pointer_type;
4207 const char *type_str = NULL;
4208 const type_t *type = var->declspec.type;
4209 unsigned int alignment, start_offset = type->typestring_offset;
4211 if (is_ptr(type) || is_array(type))
4212 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4213 else
4214 pointer_type = 0;
4216 in_attr = is_attr(var->attrs, ATTR_IN);
4217 out_attr = is_attr(var->attrs, ATTR_OUT);
4218 if (!in_attr && !out_attr)
4219 in_attr = 1;
4221 if (phase != PHASE_FREE)
4222 switch (pass)
4224 case PASS_IN:
4225 if (!in_attr) return;
4226 break;
4227 case PASS_OUT:
4228 if (!out_attr) return;
4229 break;
4230 case PASS_RETURN:
4231 break;
4234 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4236 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4238 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4240 case TGT_CTXT_HANDLE:
4241 case TGT_CTXT_HANDLE_POINTER:
4242 if (phase == PHASE_MARSHAL)
4244 if (pass == PASS_IN)
4246 /* if the context_handle attribute appears in the chain of types
4247 * without pointers being followed, then the context handle must
4248 * be direct, otherwise it is a pointer */
4249 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4250 print_file(file, indent, "NdrClientContextMarshall(\n");
4251 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4252 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4253 var->name);
4254 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4256 else
4258 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4259 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4260 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4261 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
4262 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4265 else if (phase == PHASE_UNMARSHAL)
4267 if (pass == PASS_OUT || pass == PASS_RETURN)
4269 if (!in_attr)
4270 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4271 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4272 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4273 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
4274 pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
4275 print_file(file, indent + 1, "__frame->_Handle);\n");
4277 else
4279 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4280 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4281 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4284 break;
4285 case TGT_USER_TYPE:
4286 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4287 break;
4288 case TGT_STRING:
4289 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4290 pointer_type != FC_RP)
4292 /* strings returned are assumed to be global and hence don't
4293 * need freeing */
4294 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4295 print_phase_function(file, indent, "Pointer", local_var_prefix,
4296 phase, var, start_offset);
4297 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4298 !in_attr && is_conformant_array(type))
4300 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4301 indent++;
4302 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4305 else
4307 unsigned int real_start_offset = start_offset;
4308 /* skip over pointer description straight to string description */
4309 if (is_declptr(type))
4311 if (is_conformant_array(type))
4312 real_start_offset += 4;
4313 else
4314 real_start_offset += 2;
4316 if (is_array(type) && !is_conformant_array(type))
4317 print_phase_function(file, indent, "NonConformantString",
4318 local_var_prefix, phase, var,
4319 real_start_offset);
4320 else
4321 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4322 phase, var, real_start_offset);
4324 break;
4325 case TGT_ARRAY:
4327 unsigned char tc = get_array_fc(type);
4328 const char *array_type = NULL;
4330 /* We already have the size_is expression since it's at the
4331 top level, but do checks for multidimensional conformant
4332 arrays. When we handle them, we'll need to extend this
4333 function to return a list, and then we'll actually use
4334 the return value. */
4335 get_size_is_expr(type, var->name);
4337 switch (tc)
4339 case FC_SMFARRAY:
4340 case FC_LGFARRAY:
4341 array_type = "FixedArray";
4342 break;
4343 case FC_SMVARRAY:
4344 case FC_LGVARRAY:
4345 array_type = "VaryingArray";
4346 break;
4347 case FC_CARRAY:
4348 array_type = "ConformantArray";
4349 break;
4350 case FC_CVARRAY:
4351 array_type = "ConformantVaryingArray";
4352 break;
4353 case FC_BOGUS_ARRAY:
4354 array_type = "ComplexArray";
4355 break;
4358 if (pointer_type != FC_RP) array_type = "Pointer";
4360 if (phase == PHASE_FREE && pointer_type == FC_RP)
4362 /* these are all unmarshalled by allocating memory */
4363 if (tc == FC_BOGUS_ARRAY ||
4364 tc == FC_CVARRAY ||
4365 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4366 (tc == FC_CARRAY && !in_attr))
4368 if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
4370 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4371 type_array_get_ptr_tfsoff(type));
4372 break;
4374 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4375 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4376 indent++;
4377 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4378 break;
4381 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4382 break;
4384 case TGT_BASIC:
4385 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4386 break;
4387 case TGT_ENUM:
4388 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4389 break;
4390 case TGT_RANGE:
4391 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4392 /* Note: this goes beyond what MIDL does - it only supports arguments
4393 * with the [range] attribute in Oicf mode */
4394 if (phase == PHASE_UNMARSHAL)
4396 const expr_t *range_min;
4397 const expr_t *range_max;
4398 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4399 if (!range_list)
4400 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4401 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4402 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4404 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4405 write_type_decl(file, &var->declspec, NULL);
4406 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4407 write_type_decl(file, &var->declspec, NULL);
4408 fprintf(file, ")0x%x))\n", range_max->cval);
4409 print_file(file, indent, "{\n");
4410 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4411 print_file(file, indent, "}\n");
4413 break;
4414 case TGT_STRUCT:
4415 switch (get_struct_fc(type))
4417 case FC_STRUCT:
4418 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4419 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4420 break;
4421 case FC_PSTRUCT:
4422 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4423 break;
4424 case FC_CSTRUCT:
4425 case FC_CPSTRUCT:
4426 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4427 break;
4428 case FC_CVSTRUCT:
4429 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4430 break;
4431 case FC_BOGUS_STRUCT:
4432 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4433 break;
4434 default:
4435 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4437 break;
4438 case TGT_UNION:
4440 const char *union_type = NULL;
4442 if (type_get_type(type) == TYPE_UNION)
4443 union_type = "NonEncapsulatedUnion";
4444 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4445 union_type = "EncapsulatedUnion";
4447 print_phase_function(file, indent, union_type, local_var_prefix,
4448 phase, var, start_offset);
4449 break;
4451 case TGT_POINTER:
4453 const type_t *ref = type_pointer_get_ref_type(type);
4454 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4456 case TGT_BASIC:
4457 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4458 break;
4459 case TGT_ENUM:
4460 /* base types have known sizes, so don't need a sizing pass
4461 * and don't have any memory to free and so don't need a
4462 * freeing pass */
4463 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4464 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4465 break;
4466 case TGT_STRUCT:
4467 switch (get_struct_fc(ref))
4469 case FC_STRUCT:
4470 /* simple structs have known sizes, so don't need a sizing
4471 * pass and don't have any memory to free and so don't
4472 * need a freeing pass */
4473 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4474 type_str = "SimpleStruct";
4475 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4477 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4478 indent++;
4479 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4480 indent--;
4482 break;
4483 case FC_PSTRUCT:
4484 type_str = "SimpleStruct";
4485 break;
4486 case FC_CSTRUCT:
4487 case FC_CPSTRUCT:
4488 type_str = "ConformantStruct";
4489 break;
4490 case FC_CVSTRUCT:
4491 type_str = "ConformantVaryingStruct";
4492 break;
4493 case FC_BOGUS_STRUCT:
4494 type_str = "ComplexStruct";
4495 break;
4496 default:
4497 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4500 if (type_str)
4502 if (phase == PHASE_FREE)
4503 type_str = "Pointer";
4504 else
4505 start_offset = ref->typestring_offset;
4506 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4508 break;
4509 case TGT_UNION:
4510 if (phase == PHASE_FREE)
4511 type_str = "Pointer";
4512 else
4514 if (type_get_type(ref) == TYPE_UNION)
4515 type_str = "NonEncapsulatedUnion";
4516 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4517 type_str = "EncapsulatedUnion";
4519 start_offset = ref->typestring_offset;
4522 print_phase_function(file, indent, type_str, local_var_prefix,
4523 phase, var, start_offset);
4524 break;
4525 case TGT_USER_TYPE:
4526 if (phase != PHASE_FREE)
4528 type_str = "UserMarshal";
4529 start_offset = ref->typestring_offset;
4531 else type_str = "Pointer";
4533 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4534 break;
4535 case TGT_STRING:
4536 case TGT_POINTER:
4537 case TGT_ARRAY:
4538 case TGT_RANGE:
4539 case TGT_IFACE_POINTER:
4540 case TGT_CTXT_HANDLE:
4541 case TGT_CTXT_HANDLE_POINTER:
4542 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4543 break;
4544 case TGT_INVALID:
4545 assert(0);
4546 break;
4548 else
4549 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4550 break;
4552 case TGT_IFACE_POINTER:
4553 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4554 break;
4555 case TGT_INVALID:
4556 assert(0);
4557 break;
4559 fprintf(file, "\n");
4562 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4563 enum pass pass, enum remoting_phase phase)
4565 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4567 unsigned int size = get_function_buffer_size( func, pass );
4568 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4571 if (pass == PASS_RETURN)
4573 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4574 type_function_get_retval(func->declspec.type) );
4576 else
4578 const var_t *var;
4579 if (!type_function_get_args(func->declspec.type))
4580 return;
4581 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4582 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4587 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4589 unsigned int offset = 0;
4590 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4591 return offset;
4594 static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
4596 const statement_t *stmt;
4597 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
4599 const var_t *func = stmt->u.var;
4600 if (!is_local(func->attrs))
4601 *size += get_size_procformatstring_func( iface, func );
4605 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4607 unsigned int size = 1;
4608 for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
4609 return size;
4612 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4614 set_all_tfswrite(FALSE);
4615 return process_tfs(NULL, stmts, pred);
4618 void declare_stub_args( FILE *file, int indent, const var_t *func )
4620 int in_attr, out_attr;
4621 int i = 0;
4622 var_t *var = type_function_get_retval(func->declspec.type);
4624 /* declare return value */
4625 if (!is_void(var->declspec.type))
4627 if (is_context_handle(var->declspec.type))
4628 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4629 else
4631 print_file(file, indent, "%s", "");
4632 write_type_decl(file, &var->declspec, var->name);
4633 fprintf(file, ";\n");
4637 if (!type_function_get_args(func->declspec.type))
4638 return;
4640 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
4642 in_attr = is_attr(var->attrs, ATTR_IN);
4643 out_attr = is_attr(var->attrs, ATTR_OUT);
4644 if (!out_attr && !in_attr)
4645 in_attr = 1;
4647 if (is_context_handle(var->declspec.type))
4648 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4649 else
4651 if (!in_attr && !is_conformant_array(var->declspec.type))
4653 const decl_spec_t *type_to_print;
4654 char name[16];
4655 print_file(file, indent, "%s", "");
4656 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4657 !type_array_is_decl_as_ptr(var->declspec.type))
4658 type_to_print = &var->declspec;
4659 else
4660 type_to_print = type_pointer_get_ref(var->declspec.type);
4661 sprintf(name, "_W%u", i++);
4662 write_type_decl(file, type_to_print, name);
4663 fprintf(file, ";\n");
4666 print_file(file, indent, "%s", "");
4667 write_type_decl_left(file, &var->declspec);
4668 fprintf(file, " ");
4669 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4670 !type_array_is_decl_as_ptr(var->declspec.type)) {
4671 fprintf(file, "(*%s)", var->name);
4672 } else
4673 fprintf(file, "%s", var->name);
4674 write_type_right(file, var->declspec.type, FALSE);
4675 fprintf(file, ";\n");
4677 if (decl_indirect(var->declspec.type))
4678 print_file(file, indent, "void *_p_%s;\n", var->name);
4684 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4686 int in_attr, out_attr;
4687 int i = 0, sep = 0;
4688 const var_t *var;
4689 type_t *ref;
4691 if (!type_function_get_args(func->declspec.type))
4692 return;
4694 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4696 in_attr = is_attr(var->attrs, ATTR_IN);
4697 out_attr = is_attr(var->attrs, ATTR_OUT);
4698 if (!out_attr && !in_attr)
4699 in_attr = 1;
4701 if (!in_attr)
4703 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4705 switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
4707 case TGT_CTXT_HANDLE_POINTER:
4708 fprintf(file, " = NdrContextHandleInitialize(\n");
4709 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4710 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4711 var->typestring_offset);
4712 break;
4713 case TGT_ARRAY:
4714 if (type_array_has_conformance(var->declspec.type))
4716 unsigned int size;
4717 type_t *type;
4719 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4720 for (type = var->declspec.type;
4721 is_array(type) && type_array_has_conformance(type);
4722 type = type_array_get_element_type(type))
4724 write_expr(file, type_array_get_conformance(type), TRUE,
4725 TRUE, NULL, NULL, local_var_prefix);
4726 fprintf(file, " * ");
4728 size = type_memsize(type);
4729 fprintf(file, "%u);\n", size);
4731 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4732 for (type = var->declspec.type;
4733 is_array(type) && type_array_has_conformance(type);
4734 type = type_array_get_element_type(type))
4736 write_expr(file, type_array_get_conformance(type), TRUE,
4737 TRUE, NULL, NULL, local_var_prefix);
4738 fprintf(file, " * ");
4740 size = type_memsize(type);
4741 fprintf(file, "%u);\n", size);
4743 else
4744 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4745 break;
4746 case TGT_POINTER:
4747 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4748 ref = type_pointer_get_ref_type(var->declspec.type);
4749 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4751 case TGT_BASIC:
4752 case TGT_ENUM:
4753 case TGT_POINTER:
4754 case TGT_RANGE:
4755 case TGT_IFACE_POINTER:
4756 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4757 break;
4758 case TGT_USER_TYPE:
4759 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4760 local_var_prefix, i, local_var_prefix, i);
4761 break;
4762 case TGT_ARRAY:
4763 if (type_array_is_decl_as_ptr(ref))
4765 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4766 break;
4768 ref = type_array_get_element_type(ref);
4769 /* fall through */
4770 case TGT_STRUCT:
4771 case TGT_UNION:
4772 if (type_has_pointers(ref))
4773 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4774 local_var_prefix, i, local_var_prefix, i);
4775 break;
4776 case TGT_CTXT_HANDLE:
4777 case TGT_CTXT_HANDLE_POINTER:
4778 case TGT_INVALID:
4779 case TGT_STRING:
4780 /* not initialised */
4781 break;
4783 i++;
4784 break;
4785 default:
4786 break;
4789 sep = 1;
4792 if (sep)
4793 fprintf(file, "\n");
4797 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4798 const char *var_decl, int add_retval )
4800 var_t *retval = type_function_get_retval( func );
4801 const var_list_t *args = type_function_get_args( func );
4802 const var_t *arg;
4803 int needs_packing;
4804 unsigned int align = 0;
4806 if (args)
4807 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4808 if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
4810 needs_packing = (align > pointer_size);
4812 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4813 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4814 print_file(file, 1, "{\n" );
4815 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4817 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4819 print_file(file, 2, "%s", "");
4820 write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
4821 if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
4822 if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
4824 /* FIXME: should check for large args being passed by pointer */
4825 align = 0;
4826 if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
4827 else type_memsize_and_alignment( arg->declspec.type, &align );
4829 if (align < pointer_size)
4830 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4831 fprintf( file, "%s;\n", arg->name );
4833 if (add_retval && !is_void( retval->declspec.type ))
4835 print_file(file, 2, "%s", "");
4836 write_type_left( file, &retval->declspec, NAME_DEFAULT, TRUE, TRUE );
4837 if (needs_space_after( retval->declspec.type )) fputc( ' ', file );
4838 if (!is_array( retval->declspec.type ) && !is_ptr( retval->declspec.type ) &&
4839 type_memsize( retval->declspec.type ) != pointer_size)
4841 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4843 fprintf( file, "%s;\n", retval->name );
4845 print_file(file, 1, "} %s;\n", var_decl );
4846 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4847 print_file( file, 0, "\n" );
4850 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4852 const var_list_t *args = type_function_get_args( func->declspec.type );
4853 const var_t *var;
4855 if (!args) return;
4857 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4858 if (cant_be_null( var ))
4859 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4862 int write_expr_eval_routines(FILE *file, const char *iface)
4864 static const char *var_name = "pS";
4865 static const char *var_name_expr = "pS->";
4866 int result = 0;
4867 struct expr_eval_routine *eval;
4868 unsigned short callback_offset = 0;
4870 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4872 const char *name = eval->name;
4873 result = 1;
4875 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4876 eval->iface ? eval->iface->name : iface, name, callback_offset);
4877 print_file(file, 0, "{\n");
4878 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4880 write_func_param_struct( file, eval->iface, eval->cont_type,
4881 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4883 else
4885 decl_spec_t ds = {.type = (type_t *)eval->cont_type};
4886 print_file(file, 1, "%s", "");
4887 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4888 fprintf(file, " *%s = (", var_name);
4889 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4890 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4892 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4893 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4894 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4895 fprintf(file, ";\n");
4896 print_file(file, 0, "}\n\n");
4897 callback_offset++;
4899 return result;
4902 void write_expr_eval_routine_list(FILE *file, const char *iface)
4904 struct expr_eval_routine *eval;
4905 struct expr_eval_routine *cursor;
4906 unsigned short callback_offset = 0;
4908 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4909 fprintf(file, "{\n");
4911 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4913 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4914 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4915 callback_offset++;
4916 list_remove(&eval->entry);
4917 free(eval->name);
4918 free(eval);
4921 fprintf(file, "};\n\n");
4924 void write_user_quad_list(FILE *file)
4926 user_type_t *ut;
4928 if (list_empty(&user_type_list))
4929 return;
4931 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4932 fprintf(file, "{\n");
4933 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4935 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4936 print_file(file, 1, "{\n");
4937 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4938 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4939 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4940 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4941 print_file(file, 1, "}%s\n", sep);
4943 fprintf(file, "};\n\n");
4946 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4948 const struct str_list_entry_t *endpoint;
4949 const char *p;
4951 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4952 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4953 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4955 print_file( f, 1, "{ (const unsigned char *)\"" );
4956 for (p = endpoint->str; *p && *p != ':'; p++)
4958 if (*p == '"' || *p == '\\') fputc( '\\', f );
4959 fputc( *p, f );
4961 if (!*p) goto error;
4962 if (p[1] != '[') goto error;
4964 fprintf( f, "\", (const unsigned char *)\"" );
4965 for (p += 2; *p && *p != ']'; p++)
4967 if (*p == '"' || *p == '\\') fputc( '\\', f );
4968 fputc( *p, f );
4970 if (*p != ']') goto error;
4971 fprintf( f, "\" },\n" );
4973 print_file( f, 0, "};\n\n" );
4974 return;
4976 error:
4977 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4980 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4981 const char *prefix, unsigned int proc_offset )
4983 const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
4984 int has_ret = !is_void( rettype->type );
4985 const var_list_t *args = type_function_get_args( func->declspec.type );
4986 const var_t *arg;
4987 int len, needs_params = 0;
4989 /* we need a param structure if we have more than one arg */
4990 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4992 print_file( file, 0, "{\n");
4993 if (needs_params)
4995 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4996 write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
4997 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4998 if (args)
4999 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5000 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
5002 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
5004 len = fprintf( file, " %s%s( ",
5005 has_ret ? "_RetVal = " : "",
5006 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
5007 fprintf( file, "&%s_StubDesc,", prefix );
5008 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
5009 if (needs_params)
5011 fprintf( file, ",\n%*s&__params", len, "" );
5013 else if (pointer_size == 8)
5015 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
5016 if (args)
5017 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5018 fprintf( file, ",\n%*s%s", len, "", arg->name );
5020 else
5022 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
5023 else if (args)
5025 arg = LIST_ENTRY( list_head(args), const var_t, entry );
5026 fprintf( file, ",\n%*s&%s", len, "", arg->name );
5029 fprintf( file, " );\n" );
5030 if (has_ret)
5032 print_file( file, 1, "return (" );
5033 write_type_decl_left(file, rettype);
5034 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
5036 print_file( file, 0, "}\n\n");
5039 void write_exceptions( FILE *file )
5041 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
5042 fprintf( file, "\n");
5043 fprintf( file, "#include \"wine/exception.h\"\n");
5044 fprintf( file, "#undef RpcTryExcept\n");
5045 fprintf( file, "#undef RpcExcept\n");
5046 fprintf( file, "#undef RpcEndExcept\n");
5047 fprintf( file, "#undef RpcTryFinally\n");
5048 fprintf( file, "#undef RpcFinally\n");
5049 fprintf( file, "#undef RpcEndFinally\n");
5050 fprintf( file, "#undef RpcExceptionCode\n");
5051 fprintf( file, "#undef RpcAbnormalTermination\n");
5052 fprintf( file, "\n");
5053 fprintf( file, "struct __exception_frame;\n");
5054 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
5055 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
5056 fprintf( file, "\n");
5057 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5058 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
5059 fprintf( file, " __filter_func filter; \\\n");
5060 fprintf( file, " __finally_func finally; \\\n");
5061 fprintf( file, " __wine_jmp_buf jmp; \\\n");
5062 fprintf( file, " DWORD code; \\\n");
5063 fprintf( file, " unsigned char abnormal_termination; \\\n");
5064 fprintf( file, " unsigned char filter_level; \\\n");
5065 fprintf( file, " unsigned char finally_level;\n");
5066 fprintf( file, "\n");
5067 fprintf( file, "struct __exception_frame\n{\n");
5068 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
5069 fprintf( file, "};\n");
5070 fprintf( file, "\n");
5071 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
5072 fprintf( file, "{\n");
5073 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
5074 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
5075 fprintf( file, " {\n");
5076 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5077 fprintf( file, " exc_frame->finally( exc_frame );\n");
5078 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5079 fprintf( file, " }\n");
5080 fprintf( file, " exc_frame->filter_level = 0;\n");
5081 fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
5082 fprintf( file, "}\n");
5083 fprintf( file, "\n");
5084 fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5085 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5086 fprintf( file, " CONTEXT *context,\n");
5087 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5088 fprintf( file, "{\n");
5089 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5090 fprintf( file, "\n");
5091 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5092 fprintf( file, " {\n" );
5093 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5094 fprintf( file, " {\n" );
5095 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5096 fprintf( file, " exc_frame->finally( exc_frame );\n");
5097 fprintf( file, " }\n" );
5098 fprintf( file, " return ExceptionContinueSearch;\n");
5099 fprintf( file, " }\n" );
5100 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5101 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5102 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5103 fprintf( file, " return ExceptionContinueSearch;\n");
5104 fprintf( file, "}\n");
5105 fprintf( file, "\n");
5106 fprintf( file, "#define RpcTryExcept \\\n");
5107 fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
5108 fprintf( file, " { \\\n");
5109 fprintf( file, " if (!__frame->finally_level) \\\n" );
5110 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5111 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5112 fprintf( file, "\n");
5113 fprintf( file, "#define RpcExcept(expr) \\\n");
5114 fprintf( file, " if (!__frame->finally_level) \\\n" );
5115 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5116 fprintf( file, " __frame->filter_level = 0; \\\n" );
5117 fprintf( file, " } \\\n");
5118 fprintf( file, " else \\\n");
5119 fprintf( file, "\n");
5120 fprintf( file, "#define RpcEndExcept\n");
5121 fprintf( file, "\n");
5122 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5123 fprintf( file, "\n");
5124 fprintf( file, "#define RpcTryFinally \\\n");
5125 fprintf( file, " if (!__frame->filter_level) \\\n");
5126 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5127 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5128 fprintf( file, "\n");
5129 fprintf( file, "#define RpcFinally \\\n");
5130 fprintf( file, " if (!__frame->filter_level) \\\n");
5131 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5132 fprintf( file, " __frame->finally_level = 0;\n");
5133 fprintf( file, "\n");
5134 fprintf( file, "#define RpcEndFinally\n");
5135 fprintf( file, "\n");
5136 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5137 fprintf( file, "\n");
5138 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5139 fprintf( file, " do { \\\n");
5140 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5141 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5142 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5143 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5144 fprintf( file, " __frame->filter_level = 0; \\\n");
5145 fprintf( file, " __frame->finally_level = 0; \\\n");
5146 fprintf( file, " } while (0)\n");
5147 fprintf( file, "\n");
5148 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5149 fprintf( file, "\n");
5150 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5151 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5152 fprintf( file, "\n");
5153 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5154 fprintf( file, " DWORD code;\n");
5155 fprintf( file, "\n");
5156 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");