msado15: Implement Dispatch functions in Fields.
[wine.git] / tools / widl / typegen.c
blob2c1017b9f2b9fc67382086ae3a0245c7b27de00d
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_RUNTIMECLASS ||
357 type_get_type(type_pointer_get_ref_type(type)) == TYPE_DELEGATE ||
358 (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
359 return TGT_IFACE_POINTER;
360 else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
361 return TGT_CTXT_HANDLE_POINTER;
362 else
363 return TGT_POINTER;
364 case TYPE_STRUCT:
365 return TGT_STRUCT;
366 case TYPE_ENCAPSULATED_UNION:
367 case TYPE_UNION:
368 return TGT_UNION;
369 case TYPE_ARRAY:
370 return TGT_ARRAY;
371 case TYPE_FUNCTION:
372 case TYPE_COCLASS:
373 case TYPE_INTERFACE:
374 case TYPE_MODULE:
375 case TYPE_VOID:
376 case TYPE_ALIAS:
377 case TYPE_BITFIELD:
378 case TYPE_RUNTIMECLASS:
379 case TYPE_DELEGATE:
380 break;
381 case TYPE_APICONTRACT:
382 case TYPE_PARAMETERIZED_TYPE:
383 case TYPE_PARAMETER:
384 /* not supposed to be here */
385 assert(0);
386 break;
388 return TGT_INVALID;
391 static int cant_be_null(const var_t *v)
393 switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
395 case TGT_ARRAY:
396 if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
397 /* fall through */
398 case TGT_POINTER:
399 return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
400 case TGT_CTXT_HANDLE_POINTER:
401 return TRUE;
402 default:
403 return 0;
408 static int get_padding(const var_list_t *fields)
410 unsigned short offset = 0;
411 unsigned int salign = 1;
412 const var_t *f;
414 if (!fields)
415 return 0;
417 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
419 type_t *ft = f->declspec.type;
420 unsigned int align = 0;
421 unsigned int size = type_memsize_and_alignment(ft, &align);
422 align = clamp_align(align);
423 if (align > salign) salign = align;
424 offset = ROUND_SIZE(offset, align);
425 offset += size;
428 return ROUNDING(offset, salign);
431 static unsigned int get_stack_size( const var_t *var, int *by_value )
433 unsigned int stack_size;
434 int by_val;
436 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
438 case TGT_BASIC:
439 case TGT_ENUM:
440 case TGT_RANGE:
441 case TGT_STRUCT:
442 case TGT_UNION:
443 case TGT_USER_TYPE:
444 stack_size = type_memsize( var->declspec.type );
445 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
446 break;
447 default:
448 by_val = 0;
449 break;
451 if (!by_val) stack_size = pointer_size;
452 if (by_value) *by_value = by_val;
453 return ROUND_SIZE( stack_size, pointer_size );
456 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
457 const type_t *type, int is_return )
459 unsigned char flags = 0;
460 int is_out;
462 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
464 if (is_ptr(type) &&
465 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
466 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
467 flags |= HANDLE_PARAM_IS_VIA_PTR;
469 if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
471 is_out = is_attr(attrs, ATTR_OUT);
472 if (is_attr(attrs, ATTR_IN) || !is_out)
474 flags |= HANDLE_PARAM_IS_IN;
475 if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
477 if (is_out) flags |= HANDLE_PARAM_IS_OUT;
479 return flags;
482 static unsigned int get_rpc_flags( const attr_list_t *attrs )
484 unsigned int flags = 0;
486 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
487 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
488 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
489 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
490 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
491 return flags;
494 unsigned char get_struct_fc(const type_t *type)
496 int has_pointer = 0;
497 int has_conformance = 0;
498 int has_variance = 0;
499 var_t *field;
500 var_list_t *fields;
502 fields = type_struct_get_fields(type);
504 if (get_padding(fields))
505 return FC_BOGUS_STRUCT;
507 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
509 type_t *t = field->declspec.type;
510 enum typegen_type typegen_type;
512 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
514 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
516 if (is_string_type(field->attrs, field->declspec.type))
518 if (is_conformant_array(t))
519 has_conformance = 1;
520 has_variance = 1;
521 continue;
524 if (is_array(type_array_get_element_type(field->declspec.type)))
525 return FC_BOGUS_STRUCT;
527 if (type_array_has_conformance(field->declspec.type))
529 has_conformance = 1;
530 if (list_next(fields, &field->entry))
531 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
532 field->name);
534 if (type_array_has_variance(t))
535 has_variance = 1;
537 t = type_array_get_element_type(t);
538 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
541 switch (typegen_type)
543 case TGT_USER_TYPE:
544 case TGT_IFACE_POINTER:
545 return FC_BOGUS_STRUCT;
546 case TGT_BASIC:
547 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
548 return FC_BOGUS_STRUCT;
549 break;
550 case TGT_ENUM:
551 if (get_enum_fc(t) == FC_ENUM16)
552 return FC_BOGUS_STRUCT;
553 break;
554 case TGT_POINTER:
555 case TGT_ARRAY:
556 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
557 return FC_BOGUS_STRUCT;
558 has_pointer = 1;
559 break;
560 case TGT_UNION:
561 return FC_BOGUS_STRUCT;
562 case TGT_STRUCT:
564 unsigned char fc = get_struct_fc(t);
565 switch (fc)
567 case FC_STRUCT:
568 break;
569 case FC_CVSTRUCT:
570 has_conformance = 1;
571 has_variance = 1;
572 has_pointer = 1;
573 break;
575 case FC_CPSTRUCT:
576 has_conformance = 1;
577 if (list_next( fields, &field->entry ))
578 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
579 field->name);
580 has_pointer = 1;
581 break;
583 case FC_CSTRUCT:
584 has_conformance = 1;
585 if (list_next( fields, &field->entry ))
586 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
587 field->name);
588 break;
590 case FC_PSTRUCT:
591 has_pointer = 1;
592 break;
594 default:
595 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
596 /* fallthru - treat it as complex */
598 /* as soon as we see one of these these members, it's bogus... */
599 case FC_BOGUS_STRUCT:
600 return FC_BOGUS_STRUCT;
602 break;
604 case TGT_RANGE:
605 return FC_BOGUS_STRUCT;
606 case TGT_STRING:
607 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
608 case TGT_INVALID:
609 case TGT_CTXT_HANDLE:
610 case TGT_CTXT_HANDLE_POINTER:
611 /* checking after parsing should mean that we don't get here. if we do,
612 * it's a checker bug */
613 assert(0);
617 if( has_variance )
619 if ( has_conformance )
620 return FC_CVSTRUCT;
621 else
622 return FC_BOGUS_STRUCT;
624 if( has_conformance && has_pointer )
625 return FC_CPSTRUCT;
626 if( has_conformance )
627 return FC_CSTRUCT;
628 if( has_pointer )
629 return FC_PSTRUCT;
630 return FC_STRUCT;
633 static unsigned char get_array_fc(const type_t *type)
635 unsigned char fc;
636 const expr_t *size_is;
637 const type_t *elem_type;
639 elem_type = type_array_get_element_type(type);
640 size_is = type_array_get_conformance(type);
642 if (!size_is)
644 unsigned int size = type_memsize(elem_type);
645 if (size * type_array_get_dim(type) > 0xffffuL)
646 fc = FC_LGFARRAY;
647 else
648 fc = FC_SMFARRAY;
650 else
651 fc = FC_CARRAY;
653 if (type_array_has_variance(type))
655 if (fc == FC_SMFARRAY)
656 fc = FC_SMVARRAY;
657 else if (fc == FC_LGFARRAY)
658 fc = FC_LGVARRAY;
659 else if (fc == FC_CARRAY)
660 fc = FC_CVARRAY;
663 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
665 case TGT_USER_TYPE:
666 fc = FC_BOGUS_ARRAY;
667 break;
668 case TGT_BASIC:
669 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
670 pointer_size != 4)
671 fc = FC_BOGUS_ARRAY;
672 break;
673 case TGT_STRUCT:
674 switch (get_struct_fc(elem_type))
676 case FC_BOGUS_STRUCT:
677 fc = FC_BOGUS_ARRAY;
678 break;
680 break;
681 case TGT_ENUM:
682 /* is 16-bit enum - if so, wire size differs from mem size and so
683 * the array cannot be block copied, which means the array is complex */
684 if (get_enum_fc(elem_type) == FC_ENUM16)
685 fc = FC_BOGUS_ARRAY;
686 break;
687 case TGT_UNION:
688 case TGT_IFACE_POINTER:
689 fc = FC_BOGUS_ARRAY;
690 break;
691 case TGT_POINTER:
692 /* ref pointers cannot just be block copied. unique pointers to
693 * interfaces need special treatment. either case means the array is
694 * complex */
695 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
696 fc = FC_BOGUS_ARRAY;
697 break;
698 case TGT_RANGE:
699 fc = FC_BOGUS_ARRAY;
700 break;
701 case TGT_CTXT_HANDLE:
702 case TGT_CTXT_HANDLE_POINTER:
703 case TGT_STRING:
704 case TGT_INVALID:
705 case TGT_ARRAY:
706 /* nothing to do for everything else */
707 break;
710 return fc;
713 static int is_non_complex_struct(const type_t *type)
715 return (type_get_type(type) == TYPE_STRUCT &&
716 get_struct_fc(type) != FC_BOGUS_STRUCT);
719 static int type_has_pointers(const type_t *type)
721 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
723 case TGT_USER_TYPE:
724 return FALSE;
725 case TGT_POINTER:
726 return TRUE;
727 case TGT_ARRAY:
728 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
729 case TGT_STRUCT:
731 var_list_t *fields = type_struct_get_fields(type);
732 const var_t *field;
733 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
735 if (type_has_pointers(field->declspec.type))
736 return TRUE;
738 break;
740 case TGT_UNION:
742 var_list_t *fields;
743 const var_t *field;
744 fields = type_union_get_cases(type);
745 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
747 if (field->declspec.type && type_has_pointers(field->declspec.type))
748 return TRUE;
750 break;
752 case TGT_CTXT_HANDLE:
753 case TGT_CTXT_HANDLE_POINTER:
754 case TGT_STRING:
755 case TGT_IFACE_POINTER:
756 case TGT_BASIC:
757 case TGT_ENUM:
758 case TGT_RANGE:
759 case TGT_INVALID:
760 break;
763 return FALSE;
766 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
767 int toplevel_param)
769 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
771 case TGT_USER_TYPE:
772 return FALSE;
773 case TGT_POINTER:
774 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
775 return TRUE;
776 else
777 return FALSE;
778 case TGT_ARRAY:
779 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
780 return TRUE;
781 else
782 return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
783 case TGT_STRUCT:
785 var_list_t *fields = type_struct_get_fields(type);
786 const var_t *field;
787 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
789 if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
790 return TRUE;
792 break;
794 case TGT_UNION:
796 var_list_t *fields;
797 const var_t *field;
798 fields = type_union_get_cases(type);
799 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
801 if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
802 return TRUE;
804 break;
806 case TGT_CTXT_HANDLE:
807 case TGT_CTXT_HANDLE_POINTER:
808 case TGT_STRING:
809 case TGT_IFACE_POINTER:
810 case TGT_BASIC:
811 case TGT_ENUM:
812 case TGT_RANGE:
813 case TGT_INVALID:
814 break;
817 return FALSE;
820 static unsigned short user_type_offset(const char *name)
822 user_type_t *ut;
823 unsigned short off = 0;
824 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
826 if (strcmp(name, ut->name) == 0)
827 return off;
828 ++off;
830 error("user_type_offset: couldn't find type (%s)\n", name);
831 return 0;
834 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
836 type->typestring_offset = offset;
837 if (file) type->tfswrite = FALSE;
840 static void guard_rec(type_t *type)
842 /* types that contain references to themselves (like a linked list),
843 need to be shielded from infinite recursion when writing embedded
844 types */
845 if (type->typestring_offset)
846 type->tfswrite = FALSE;
847 else
848 type->typestring_offset = 1;
851 static int is_embedded_complex(const type_t *type)
853 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
855 case TGT_USER_TYPE:
856 case TGT_STRUCT:
857 case TGT_UNION:
858 case TGT_ARRAY:
859 case TGT_IFACE_POINTER:
860 return TRUE;
861 default:
862 return FALSE;
866 static const char *get_context_handle_type_name(const type_t *type)
868 const type_t *t;
869 for (t = type;
870 is_ptr(t) || type_is_alias(t);
871 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
872 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
873 return t->name;
874 assert(0);
875 return NULL;
878 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
879 do { \
880 if (file) \
881 fprintf(file, "/* %2u */\n", typestring_offset); \
882 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
884 while (0)
886 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
887 static void print_file(FILE *file, int indent, const char *format, ...)
889 va_list va;
890 va_start(va, format);
891 print(file, indent, format, va);
892 va_end(va);
895 void print(FILE *file, int indent, const char *format, va_list va)
897 if (file)
899 if (format[0] != '\n')
900 while (0 < indent--)
901 fprintf(file, " ");
902 vfprintf(file, format, va);
907 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
909 if (decl_indirect(t))
911 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
912 local_var_prefix, n, local_var_prefix, n);
913 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
915 else if (is_ptr(t) || is_array(t))
916 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
919 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
921 const var_t *var = type_function_get_retval(func->declspec.type);
923 if (!is_void(var->declspec.type))
924 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
926 if (!type_function_get_args(func->declspec.type))
927 return;
929 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
930 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
932 fprintf(file, "\n");
935 static void write_formatdesc(FILE *f, int indent, const char *str)
937 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
938 print_file(f, indent, "{\n");
939 print_file(f, indent + 1, "short Pad;\n");
940 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
941 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
942 print_file(f, indent, "\n");
945 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
947 clear_all_offsets();
949 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
950 get_size_typeformatstring(stmts, pred));
952 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
953 get_size_procformatstring(stmts, pred));
955 fprintf(f, "\n");
956 write_formatdesc(f, indent, "TYPE");
957 write_formatdesc(f, indent, "PROC");
958 fprintf(f, "\n");
959 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
960 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
961 print_file(f, indent, "\n");
964 int decl_indirect(const type_t *t)
966 if (is_user_type(t))
967 return TRUE;
968 return (type_get_type(t) != TYPE_BASIC &&
969 type_get_type(t) != TYPE_ENUM &&
970 type_get_type(t) != TYPE_POINTER &&
971 type_get_type(t) != TYPE_ARRAY);
974 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
975 unsigned int *stack_size, unsigned int *typestring_offset )
977 unsigned int alignment, server_size = 0, buffer_size = 0;
978 unsigned char fc = 0;
979 int is_byval;
980 int is_in = is_attr(var->attrs, ATTR_IN);
981 int is_out = is_attr(var->attrs, ATTR_OUT);
983 if (is_return) is_out = TRUE;
984 else if (!is_in && !is_out) is_in = TRUE;
986 *flags = 0;
987 *stack_size = get_stack_size( var, &is_byval );
988 *typestring_offset = var->typestring_offset;
990 if (is_in) *flags |= IsIn;
991 if (is_out) *flags |= IsOut;
992 if (is_return) *flags |= IsReturn;
994 if (!is_string_type( var->attrs, var->declspec.type ))
995 buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
997 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
999 case TGT_BASIC:
1000 *flags |= IsBasetype;
1001 fc = get_basic_fc_signed( var->declspec.type );
1002 if (fc == FC_BIND_PRIMITIVE)
1004 buffer_size = 4; /* actually 0 but avoids setting MustSize */
1005 fc = FC_LONG;
1007 break;
1008 case TGT_ENUM:
1009 *flags |= IsBasetype;
1010 fc = get_enum_fc( var->declspec.type );
1011 break;
1012 case TGT_RANGE:
1013 *flags |= IsByValue;
1014 break;
1015 case TGT_STRUCT:
1016 case TGT_UNION:
1017 case TGT_USER_TYPE:
1018 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1019 break;
1020 case TGT_IFACE_POINTER:
1021 *flags |= MustFree;
1022 break;
1023 case TGT_ARRAY:
1024 *flags |= MustFree;
1025 if (type_array_is_decl_as_ptr(var->declspec.type)
1026 && type_array_get_ptr_tfsoff(var->declspec.type)
1027 && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
1029 *typestring_offset = var->declspec.type->typestring_offset;
1030 *flags |= IsSimpleRef;
1032 break;
1033 case TGT_STRING:
1034 *flags |= MustFree;
1035 if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1037 /* skip over pointer description straight to string description */
1038 if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
1039 else *typestring_offset += 2;
1040 *flags |= IsSimpleRef;
1042 break;
1043 case TGT_CTXT_HANDLE_POINTER:
1044 *flags |= IsSimpleRef;
1045 *typestring_offset += 4;
1046 /* fall through */
1047 case TGT_CTXT_HANDLE:
1048 buffer_size = 20;
1049 break;
1050 case TGT_POINTER:
1051 if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1053 const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
1055 if (!is_string_type( var->attrs, ref ))
1056 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1058 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1060 case TGT_BASIC:
1061 *flags |= IsSimpleRef | IsBasetype;
1062 fc = get_basic_fc( ref );
1063 if (!is_in && is_out) server_size = pointer_size;
1064 break;
1065 case TGT_ENUM:
1066 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1068 *flags |= IsSimpleRef | IsBasetype;
1069 if (!is_in && is_out) server_size = pointer_size;
1071 else
1073 server_size = pointer_size;
1075 break;
1076 case TGT_UNION:
1077 case TGT_USER_TYPE:
1078 case TGT_RANGE:
1079 *flags |= MustFree | IsSimpleRef;
1080 *typestring_offset = ref->typestring_offset;
1081 if (!is_in && is_out) server_size = type_memsize( ref );
1082 break;
1083 case TGT_ARRAY:
1084 *flags |= MustFree;
1085 if (!type_array_is_decl_as_ptr(ref))
1087 *flags |= IsSimpleRef;
1088 *typestring_offset = ref->typestring_offset;
1090 if (!is_in && is_out) server_size = type_memsize( ref );
1091 break;
1092 case TGT_STRING:
1093 case TGT_POINTER:
1094 case TGT_CTXT_HANDLE:
1095 case TGT_CTXT_HANDLE_POINTER:
1096 *flags |= MustFree;
1097 server_size = pointer_size;
1098 break;
1099 case TGT_IFACE_POINTER:
1100 *flags |= MustFree;
1101 if (is_in && is_out) server_size = pointer_size;
1102 break;
1103 case TGT_STRUCT:
1104 *flags |= IsSimpleRef | MustFree;
1105 *typestring_offset = ref->typestring_offset;
1106 switch (get_struct_fc(ref))
1108 case FC_STRUCT:
1109 case FC_PSTRUCT:
1110 case FC_BOGUS_STRUCT:
1111 if (!is_in && is_out) server_size = type_memsize( ref );
1112 break;
1113 default:
1114 break;
1116 break;
1117 case TGT_INVALID:
1118 assert(0);
1121 else /* not ref pointer */
1123 *flags |= MustFree;
1125 break;
1126 case TGT_INVALID:
1127 assert(0);
1130 if (!buffer_size) *flags |= MustSize;
1132 if (server_size)
1134 server_size = (server_size + 7) / 8;
1135 if (server_size < 8) *flags |= server_size << 13;
1137 return fc;
1140 static unsigned char get_func_oi2_flags( const var_t *func )
1142 const var_t *var;
1143 var_list_t *args = type_function_get_args( func->declspec.type );
1144 var_t *retval = type_function_get_retval( func->declspec.type );
1145 unsigned char oi2_flags = 0x40; /* HasExtensions */
1146 unsigned short flags;
1147 unsigned int stack_size, typestring_offset;
1149 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1151 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1152 if (flags & MustSize)
1154 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1155 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1159 if (!is_void( retval->declspec.type ))
1161 oi2_flags |= 0x04; /* HasRet */
1162 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1163 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1165 return oi2_flags;
1168 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1169 int is_return, unsigned int *stack_offset)
1171 char buffer[128];
1172 unsigned int stack_size, typestring_offset;
1173 unsigned short flags;
1174 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1176 strcpy( buffer, "/* flags:" );
1177 if (flags & MustSize) strcat( buffer, " must size," );
1178 if (flags & MustFree) strcat( buffer, " must free," );
1179 if (flags & IsPipe) strcat( buffer, " pipe," );
1180 if (flags & IsIn) strcat( buffer, " in," );
1181 if (flags & IsOut) strcat( buffer, " out," );
1182 if (flags & IsReturn) strcat( buffer, " return," );
1183 if (flags & IsBasetype) strcat( buffer, " base type," );
1184 if (flags & IsByValue) strcat( buffer, " by value," );
1185 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1186 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1187 strcpy( buffer + strlen( buffer ) - 1, " */" );
1188 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1189 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1190 *stack_offset, *stack_offset );
1191 if (flags & IsBasetype)
1193 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1194 print_file( file, indent, "0x0,\n" );
1196 else
1197 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1198 typestring_offset, typestring_offset );
1199 *stack_offset += max( stack_size, pointer_size );
1200 return 6;
1203 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1204 int is_return, int is_interpreted)
1206 unsigned int size;
1208 int is_in = is_attr(var->attrs, ATTR_IN);
1209 int is_out = is_attr(var->attrs, ATTR_OUT);
1211 if (!is_in && !is_out) is_in = TRUE;
1213 if (type_get_type(var->declspec.type) == TYPE_BASIC ||
1214 type_get_type(var->declspec.type) == TYPE_ENUM)
1216 unsigned char fc;
1218 if (is_return)
1219 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1220 else
1221 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1223 if (type_get_type(var->declspec.type) == TYPE_ENUM)
1225 fc = get_enum_fc(var->declspec.type);
1227 else
1229 fc = get_basic_fc_signed(var->declspec.type);
1231 if (fc == FC_BIND_PRIMITIVE)
1232 fc = FC_IGNORE;
1235 print_file(file, indent, "0x%02x, /* %s */\n",
1236 fc, string_of_type(fc));
1237 size = 2; /* includes param type prefix */
1239 else
1241 unsigned short offset = var->typestring_offset;
1243 if (!is_interpreted && is_array(var->declspec.type)
1244 && type_array_is_decl_as_ptr(var->declspec.type)
1245 && type_array_get_ptr_tfsoff(var->declspec.type))
1246 offset = var->declspec.type->typestring_offset;
1248 if (is_return)
1249 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1250 else if (is_in && is_out)
1251 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1252 else if (is_out)
1253 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1254 else
1255 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1257 size = get_stack_size( var, NULL );
1258 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1259 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1260 size = 4; /* includes param type prefix */
1262 return size;
1265 int is_interpreted_func( const type_t *iface, const var_t *func )
1267 const char *str;
1268 const var_t *var;
1269 const var_list_t *args = type_function_get_args( func->declspec.type );
1270 const type_t *ret_type = type_function_get_rettype( func->declspec.type );
1272 if (type_get_type( ret_type ) == TYPE_BASIC)
1274 switch (type_basic_get_type( ret_type ))
1276 case TYPE_BASIC_INT64:
1277 case TYPE_BASIC_HYPER:
1278 /* return value must fit in a long_ptr */
1279 if (pointer_size < 8) return 0;
1280 break;
1281 case TYPE_BASIC_FLOAT:
1282 case TYPE_BASIC_DOUBLE:
1283 /* floating point values can't be returned */
1284 return 0;
1285 default:
1286 break;
1289 if (get_stub_mode() != MODE_Oif && args)
1291 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1292 switch (type_get_type( var->declspec.type ))
1294 case TYPE_BASIC:
1295 switch (type_basic_get_type( var->declspec.type ))
1297 /* floating point arguments are not supported in Oi mode */
1298 case TYPE_BASIC_FLOAT: return 0;
1299 case TYPE_BASIC_DOUBLE: return 0;
1300 default: break;
1302 break;
1303 /* unions passed by value are not supported in Oi mode */
1304 case TYPE_UNION: return 0;
1305 case TYPE_ENCAPSULATED_UNION: return 0;
1306 default: break;
1310 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1311 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1312 return (get_stub_mode() != MODE_Os);
1315 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1316 const var_t *func, unsigned int *offset,
1317 unsigned short num_proc )
1319 var_t *var;
1320 var_list_t *args = type_function_get_args( func->declspec.type );
1321 unsigned char explicit_fc, implicit_fc;
1322 unsigned char handle_flags;
1323 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1324 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1325 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1326 unsigned int nb_args = 0;
1327 unsigned int stack_size = 0;
1328 unsigned short param_num = 0;
1329 unsigned short handle_stack_offset = 0;
1330 unsigned short handle_param_num = 0;
1332 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1333 if (is_object( iface ))
1335 oi_flags |= Oi_OBJECT_PROC;
1336 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1337 stack_size += pointer_size;
1340 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1342 if (var == handle_var)
1344 handle_stack_offset = stack_size;
1345 handle_param_num = param_num;
1347 stack_size += get_stack_size( var, NULL );
1348 param_num++;
1349 nb_args++;
1351 if (!is_void( type_function_get_rettype( func->declspec.type )))
1353 stack_size += pointer_size;
1354 nb_args++;
1357 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1358 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1359 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1360 print_file( file, indent, "0x%02x,\n", oi_flags );
1361 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1362 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1363 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1364 *offset += 10;
1366 if (!implicit_fc)
1368 switch (explicit_fc)
1370 case FC_BIND_PRIMITIVE:
1371 handle_flags = 0;
1372 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1373 print_file( file, indent, "0x%02x,\n", handle_flags );
1374 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1375 handle_stack_offset, handle_stack_offset );
1376 *offset += 4;
1377 break;
1378 case FC_BIND_GENERIC:
1379 handle_flags = type_memsize( handle_var->declspec.type );
1380 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1381 print_file( file, indent, "0x%02x,\n", handle_flags );
1382 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1383 handle_stack_offset, handle_stack_offset );
1384 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
1385 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1386 *offset += 6;
1387 break;
1388 case FC_BIND_CONTEXT:
1389 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
1390 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1391 print_file( file, indent, "0x%02x,\n", handle_flags );
1392 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1393 handle_stack_offset, handle_stack_offset );
1394 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
1395 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1396 *offset += 6;
1397 break;
1401 if (get_stub_mode() == MODE_Oif)
1403 unsigned char oi2_flags = get_func_oi2_flags( func );
1404 unsigned char ext_flags = 0;
1405 unsigned int size;
1407 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1408 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1409 if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
1411 size = get_function_buffer_size( func, PASS_IN );
1412 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1413 size = get_function_buffer_size( func, PASS_OUT );
1414 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1415 print_file( file, indent, "0x%02x,\n", oi2_flags );
1416 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1417 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1418 print_file( file, indent, "0x%02x,\n", ext_flags );
1419 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1420 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1421 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1422 *offset += 14;
1423 if (pointer_size == 8)
1425 unsigned short pos = 0, fpu_mask = 0;
1427 if (is_object( iface )) pos += 2;
1428 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1430 if (type_get_type( var->declspec.type ) == TYPE_BASIC)
1432 switch (type_basic_get_type( var->declspec.type ))
1434 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1435 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1436 default: break;
1439 pos += 2;
1440 if (pos >= 16) break;
1442 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1443 *offset += 2;
1448 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1449 const var_t *func, unsigned int *offset,
1450 unsigned short num_proc )
1452 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1453 int is_interpreted = is_interpreted_func( iface, func );
1454 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1455 var_t *retval = type_function_get_retval( func->declspec.type );
1457 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1459 /* emit argument data */
1460 if (type_function_get_args(func->declspec.type))
1462 const var_t *var;
1463 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1465 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1466 if (is_new_style)
1467 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1468 else
1469 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1473 /* emit return value data */
1474 if (is_void(retval->declspec.type))
1476 if (!is_new_style)
1478 print_file(file, 0, "/* %u (void) */\n", *offset);
1479 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1480 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1481 *offset += 2;
1484 else
1486 print_file( file, 0, "/* %u (return value) */\n", *offset );
1487 if (is_new_style)
1488 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1489 else
1490 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1494 static void for_each_iface(const statement_list_t *stmts,
1495 void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
1496 type_pred_t pred, FILE *file, int indent, unsigned int *offset)
1498 const statement_t *stmt;
1499 type_t *iface;
1501 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1503 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
1504 continue;
1505 iface = stmt->u.type;
1506 if (!pred(iface)) continue;
1507 proc(iface, file, indent, offset);
1508 if (type_iface_get_async_iface(iface))
1509 proc(type_iface_get_async_iface(iface), file, indent, offset);
1513 static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
1515 const statement_t *stmt;
1516 const type_t *parent = type_iface_get_inherit( iface );
1517 int count = parent ? count_methods( parent ) : 0;
1519 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1521 var_t *func = stmt->u.var;
1522 if (is_local(func->attrs)) continue;
1523 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1527 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1529 int indent = 0;
1530 unsigned int offset = 0;
1532 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1533 print_file(file, indent, "{\n");
1534 indent++;
1535 print_file(file, indent, "0,\n");
1536 print_file(file, indent, "{\n");
1537 indent++;
1539 for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
1541 print_file(file, indent, "0x0\n");
1542 indent--;
1543 print_file(file, indent, "}\n");
1544 indent--;
1545 print_file(file, indent, "};\n");
1546 print_file(file, indent, "\n");
1549 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1551 const statement_t *stmt;
1552 int indent = 0;
1554 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1555 iface->name );
1556 print_file( file, indent, "{\n" );
1557 indent++;
1558 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1560 var_t *func = stmt->u.var;
1561 if (is_local( func->attrs )) continue;
1562 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1564 indent--;
1565 print_file( file, indent, "};\n\n" );
1568 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1570 unsigned char fc;
1572 if (type_get_type(type) == TYPE_BASIC)
1573 fc = get_basic_fc_signed(type);
1574 else if (type_get_type(type) == TYPE_ENUM)
1575 fc = get_enum_fc(type);
1576 else
1577 return 0;
1579 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1580 *typestring_offset += 1;
1581 return 1;
1584 /* write conformance / variance descriptor */
1585 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1586 unsigned int baseoff, const type_t *type,
1587 const expr_t *expr)
1589 unsigned char operator_type = 0;
1590 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1591 const char *conftype_string = "field";
1592 const expr_t *subexpr;
1593 const type_t *iface = NULL;
1594 const char *name;
1596 if (!expr)
1598 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1599 return 4;
1602 if (expr->is_const)
1604 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1605 error("write_conf_or_var_desc: constant value %d is greater than "
1606 "the maximum constant size of %d\n", expr->cval,
1607 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1609 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1610 FC_CONSTANT_CONFORMANCE, expr->cval);
1611 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1612 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1614 return 4;
1617 if (!cont_type) /* top-level conformance */
1619 conftype = FC_TOP_LEVEL_CONFORMANCE;
1620 conftype_string = "parameter";
1621 cont_type = current_func->declspec.type;
1622 name = current_func->name;
1623 iface = current_iface;
1625 else
1627 name = cont_type->name;
1628 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1630 conftype = FC_POINTER_CONFORMANCE;
1631 conftype_string = "field pointer";
1635 subexpr = expr;
1636 switch (subexpr->type)
1638 case EXPR_PPTR:
1639 subexpr = subexpr->ref;
1640 operator_type = FC_DEREFERENCE;
1641 break;
1642 case EXPR_DIV:
1643 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1645 subexpr = subexpr->ref;
1646 operator_type = FC_DIV_2;
1648 break;
1649 case EXPR_MUL:
1650 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1652 subexpr = subexpr->ref;
1653 operator_type = FC_MULT_2;
1655 break;
1656 case EXPR_SUB:
1657 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1659 subexpr = subexpr->ref;
1660 operator_type = FC_SUB_1;
1662 break;
1663 case EXPR_ADD:
1664 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1666 subexpr = subexpr->ref;
1667 operator_type = FC_ADD_1;
1669 break;
1670 default:
1671 break;
1674 if (subexpr->type == EXPR_IDENTIFIER)
1676 const type_t *correlation_variable = NULL;
1677 unsigned char param_type = 0;
1678 unsigned int offset = 0;
1679 const var_t *var;
1680 struct expr_loc expr_loc;
1682 if (type_get_type(cont_type) == TYPE_FUNCTION)
1684 var_list_t *args = type_function_get_args( cont_type );
1686 if (is_object( iface )) offset += pointer_size;
1687 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1689 if (var->name && !strcmp(var->name, subexpr->u.sval))
1691 expr_loc.v = var;
1692 correlation_variable = var->declspec.type;
1693 break;
1695 offset += get_stack_size( var, NULL );
1698 else
1700 var_list_t *fields = type_struct_get_fields( cont_type );
1702 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1704 unsigned int size = field_memsize( var->declspec.type, &offset );
1705 if (var->name && !strcmp(var->name, subexpr->u.sval))
1707 expr_loc.v = var;
1708 correlation_variable = var->declspec.type;
1709 break;
1711 offset += size;
1715 if (!correlation_variable)
1716 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1717 expr_loc.attr = NULL;
1718 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1720 offset -= baseoff;
1722 if (type_get_type(correlation_variable) == TYPE_BASIC)
1724 switch (get_basic_fc(correlation_variable))
1726 case FC_CHAR:
1727 case FC_SMALL:
1728 param_type = FC_SMALL;
1729 break;
1730 case FC_BYTE:
1731 case FC_USMALL:
1732 param_type = FC_USMALL;
1733 break;
1734 case FC_WCHAR:
1735 case FC_SHORT:
1736 param_type = FC_SHORT;
1737 break;
1738 case FC_USHORT:
1739 param_type = FC_USHORT;
1740 break;
1741 case FC_LONG:
1742 param_type = FC_LONG;
1743 break;
1744 case FC_ULONG:
1745 param_type = FC_ULONG;
1746 break;
1747 default:
1748 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1749 get_basic_fc(correlation_variable));
1752 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1754 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1755 param_type = FC_LONG;
1756 else
1757 param_type = FC_SHORT;
1759 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1761 if (pointer_size == 8)
1762 param_type = FC_HYPER;
1763 else
1764 param_type = FC_LONG;
1766 else
1768 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1769 subexpr->u.sval);
1770 return 0;
1773 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1774 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1775 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1776 operator_type ? string_of_type(operator_type) : "no operators");
1777 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1778 (unsigned short)offset, offset);
1780 else if (!iface || is_interpreted_func( iface, current_func ))
1782 unsigned int callback_offset = 0;
1783 struct expr_eval_routine *eval;
1784 int found = 0;
1786 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1788 if (eval->cont_type == cont_type ||
1789 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1790 eval->iface == iface &&
1791 eval->name && name && !strcmp(eval->name, name) &&
1792 !compare_expr(eval->expr, expr)))
1794 found = 1;
1795 break;
1797 callback_offset++;
1800 if (!found)
1802 eval = xmalloc (sizeof(*eval));
1803 eval->iface = iface;
1804 eval->cont_type = cont_type;
1805 eval->name = xstrdup( name );
1806 eval->baseoff = baseoff;
1807 eval->expr = expr;
1808 list_add_tail (&expr_eval_routines, &eval->entry);
1811 if (callback_offset > USHRT_MAX)
1812 error("Maximum number of callback routines reached\n");
1814 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1815 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1816 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1818 else /* output a dummy corr desc that isn't used */
1820 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1821 print_file(file, 2, "0x0,\n" );
1822 print_file(file, 2, "NdrFcShort(0x0),\n" );
1824 return 4;
1827 /* return size and start offset of a data field based on current offset */
1828 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1830 unsigned int align = 0;
1831 unsigned int size = type_memsize_and_alignment( type, &align );
1833 *offset = ROUND_SIZE( *offset, align );
1834 return size;
1837 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1839 unsigned int size = 0;
1840 unsigned int max_align;
1841 const var_t *v;
1843 if (!fields) return 0;
1844 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1846 unsigned int falign = 0;
1847 unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
1848 if (*align < falign) *align = falign;
1849 falign = clamp_align(falign);
1850 size = ROUND_SIZE(size, falign);
1851 size += fsize;
1854 max_align = clamp_align(*align);
1855 size = ROUND_SIZE(size, max_align);
1857 return size;
1860 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1862 unsigned int size, maxs = 0;
1863 unsigned int align = *pmaxa;
1864 const var_t *v;
1866 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1868 /* we could have an empty default field with NULL type */
1869 if (v->declspec.type)
1871 size = type_memsize_and_alignment(v->declspec.type, &align);
1872 if (maxs < size) maxs = size;
1873 if (*pmaxa < align) *pmaxa = align;
1877 return maxs;
1880 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1882 unsigned int size = 0;
1884 switch (type_get_type(t))
1886 case TYPE_BASIC:
1887 switch (get_basic_fc(t))
1889 case FC_BYTE:
1890 case FC_CHAR:
1891 case FC_USMALL:
1892 case FC_SMALL:
1893 size = 1;
1894 if (size > *align) *align = size;
1895 break;
1896 case FC_WCHAR:
1897 case FC_USHORT:
1898 case FC_SHORT:
1899 size = 2;
1900 if (size > *align) *align = size;
1901 break;
1902 case FC_ULONG:
1903 case FC_LONG:
1904 case FC_ERROR_STATUS_T:
1905 case FC_FLOAT:
1906 size = 4;
1907 if (size > *align) *align = size;
1908 break;
1909 case FC_HYPER:
1910 case FC_DOUBLE:
1911 size = 8;
1912 if (size > *align) *align = size;
1913 break;
1914 case FC_INT3264:
1915 case FC_UINT3264:
1916 case FC_BIND_PRIMITIVE:
1917 assert( pointer_size );
1918 size = pointer_size;
1919 if (size > *align) *align = size;
1920 break;
1921 default:
1922 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1923 size = 0;
1925 break;
1926 case TYPE_ENUM:
1927 switch (get_enum_fc(t))
1929 case FC_ENUM16:
1930 case FC_ENUM32:
1931 size = 4;
1932 if (size > *align) *align = size;
1933 break;
1934 default:
1935 error("type_memsize: Unknown enum type\n");
1936 size = 0;
1938 break;
1939 case TYPE_STRUCT:
1940 size = fields_memsize(type_struct_get_fields(t), align);
1941 break;
1942 case TYPE_ENCAPSULATED_UNION:
1943 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1944 break;
1945 case TYPE_UNION:
1946 size = union_memsize(type_union_get_cases(t), align);
1947 break;
1948 case TYPE_POINTER:
1949 case TYPE_INTERFACE:
1950 assert( pointer_size );
1951 size = pointer_size;
1952 if (size > *align) *align = size;
1953 break;
1954 case TYPE_ARRAY:
1955 if (!type_array_is_decl_as_ptr(t))
1957 if (is_conformant_array(t))
1959 type_memsize_and_alignment(type_array_get_element_type(t), align);
1960 size = 0;
1962 else
1963 size = type_array_get_dim(t) *
1964 type_memsize_and_alignment(type_array_get_element_type(t), align);
1966 else /* declared as a pointer */
1968 assert( pointer_size );
1969 size = pointer_size;
1970 if (size > *align) *align = size;
1972 break;
1973 case TYPE_ALIAS:
1974 case TYPE_VOID:
1975 case TYPE_COCLASS:
1976 case TYPE_MODULE:
1977 case TYPE_FUNCTION:
1978 case TYPE_BITFIELD:
1979 case TYPE_APICONTRACT:
1980 case TYPE_RUNTIMECLASS:
1981 case TYPE_PARAMETERIZED_TYPE:
1982 case TYPE_PARAMETER:
1983 case TYPE_DELEGATE:
1984 /* these types should not be encountered here due to language
1985 * restrictions (interface, void, coclass, module), logical
1986 * restrictions (alias - due to type_get_type call above) or
1987 * checking restrictions (function, bitfield). */
1988 assert(0);
1991 return size;
1994 unsigned int type_memsize(const type_t *t)
1996 unsigned int align = 0;
1997 return type_memsize_and_alignment( t, &align );
2000 static unsigned int type_buffer_alignment(const type_t *t)
2002 const var_list_t *fields;
2003 const var_t *var;
2004 unsigned int max = 0, align;
2006 switch (type_get_type(t))
2008 case TYPE_BASIC:
2009 switch (get_basic_fc(t))
2011 case FC_BYTE:
2012 case FC_CHAR:
2013 case FC_USMALL:
2014 case FC_SMALL:
2015 return 1;
2016 case FC_WCHAR:
2017 case FC_USHORT:
2018 case FC_SHORT:
2019 return 2;
2020 case FC_ULONG:
2021 case FC_LONG:
2022 case FC_ERROR_STATUS_T:
2023 case FC_FLOAT:
2024 case FC_INT3264:
2025 case FC_UINT3264:
2026 return 4;
2027 case FC_HYPER:
2028 case FC_DOUBLE:
2029 return 8;
2030 default:
2031 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
2033 break;
2034 case TYPE_ENUM:
2035 switch (get_enum_fc(t))
2037 case FC_ENUM16:
2038 return 2;
2039 case FC_ENUM32:
2040 return 4;
2041 default:
2042 error("type_buffer_alignment: Unknown enum type\n");
2044 break;
2045 case TYPE_STRUCT:
2046 if (!(fields = type_struct_get_fields(t))) break;
2047 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2049 if (!var->declspec.type) continue;
2050 align = type_buffer_alignment( var->declspec.type );
2051 if (max < align) max = align;
2053 break;
2054 case TYPE_ENCAPSULATED_UNION:
2055 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2056 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2058 if (!var->declspec.type) continue;
2059 align = type_buffer_alignment( var->declspec.type );
2060 if (max < align) max = align;
2062 break;
2063 case TYPE_UNION:
2064 if (!(fields = type_union_get_cases(t))) break;
2065 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2067 if (!var->declspec.type) continue;
2068 align = type_buffer_alignment( var->declspec.type );
2069 if (max < align) max = align;
2071 break;
2072 case TYPE_ARRAY:
2073 if (!type_array_is_decl_as_ptr(t))
2074 return type_buffer_alignment( type_array_get_element_type(t) );
2075 /* else fall through */
2076 case TYPE_POINTER:
2077 return 4;
2078 case TYPE_INTERFACE:
2079 case TYPE_ALIAS:
2080 case TYPE_VOID:
2081 case TYPE_COCLASS:
2082 case TYPE_MODULE:
2083 case TYPE_FUNCTION:
2084 case TYPE_BITFIELD:
2085 case TYPE_APICONTRACT:
2086 case TYPE_RUNTIMECLASS:
2087 case TYPE_PARAMETERIZED_TYPE:
2088 case TYPE_PARAMETER:
2089 case TYPE_DELEGATE:
2090 /* these types should not be encountered here due to language
2091 * restrictions (interface, void, coclass, module), logical
2092 * restrictions (alias - due to type_get_type call above) or
2093 * checking restrictions (function, bitfield). */
2094 assert(0);
2096 return max;
2099 int is_full_pointer_function(const var_t *func)
2101 const var_t *var;
2102 if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
2103 return TRUE;
2104 if (!type_function_get_args(func->declspec.type))
2105 return FALSE;
2106 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
2107 if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
2108 return TRUE;
2109 return FALSE;
2112 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2114 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2115 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2116 fprintf(file, "\n");
2119 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2121 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2122 fprintf(file, "\n");
2125 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2126 const type_t *type,
2127 enum type_context context,
2128 unsigned int offset,
2129 unsigned int *typeformat_offset)
2131 unsigned int start_offset = *typeformat_offset;
2132 short reloff = offset - (*typeformat_offset + 2);
2133 int in_attr, out_attr;
2134 int pointer_type;
2135 unsigned char flags = 0;
2137 pointer_type = get_pointer_fc_context(type, attrs, context);
2139 in_attr = is_attr(attrs, ATTR_IN);
2140 out_attr = is_attr(attrs, ATTR_OUT);
2141 if (!in_attr && !out_attr) in_attr = 1;
2143 if (!is_interpreted_func(current_iface, current_func))
2145 if (out_attr && !in_attr && pointer_type == FC_RP)
2146 flags |= FC_ALLOCED_ON_STACK;
2148 else if (get_stub_mode() == MODE_Oif)
2150 if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
2152 switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
2154 case TGT_STRING:
2155 case TGT_POINTER:
2156 case TGT_CTXT_HANDLE:
2157 case TGT_CTXT_HANDLE_POINTER:
2158 case TGT_ARRAY:
2159 flags |= FC_ALLOCED_ON_STACK;
2160 break;
2161 case TGT_IFACE_POINTER:
2162 if (in_attr && out_attr)
2163 flags |= FC_ALLOCED_ON_STACK;
2164 break;
2165 default:
2166 break;
2171 if (is_ptr(type))
2173 type_t *ref = type_pointer_get_ref_type(type);
2174 if(is_declptr(ref) && !is_user_type(ref))
2175 flags |= FC_POINTER_DEREF;
2176 if (pointer_type != FC_RP) {
2177 flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
2181 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2182 pointer_type,
2183 flags,
2184 string_of_type(pointer_type));
2185 if (file)
2187 if (flags & FC_ALLOCED_ON_STACK)
2188 fprintf(file, " [allocated_on_stack]");
2189 if (flags & FC_POINTER_DEREF)
2190 fprintf(file, " [pointer_deref]");
2191 if (flags & FC_DONT_FREE)
2192 fprintf(file, " [dont_free]");
2193 if (flags & FC_ALLOCATE_ALL_NODES)
2194 fprintf(file, " [all_nodes]");
2195 fprintf(file, " */\n");
2198 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2199 *typeformat_offset += 4;
2201 return start_offset;
2204 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2205 const type_t *type, enum type_context context)
2207 unsigned char fc;
2208 unsigned char pointer_fc;
2209 const type_t *ref;
2210 int in_attr = is_attr(attrs, ATTR_IN);
2211 int out_attr = is_attr(attrs, ATTR_OUT);
2212 unsigned char flags = FC_SIMPLE_POINTER;
2214 /* for historical reasons, write_simple_pointer also handled string types,
2215 * but no longer does. catch bad uses of the function with this check */
2216 if (is_string_type(attrs, type))
2217 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2219 pointer_fc = get_pointer_fc_context(type, attrs, context);
2221 ref = type_pointer_get_ref_type(type);
2222 if (type_get_type(ref) == TYPE_ENUM)
2223 fc = get_enum_fc(ref);
2224 else
2225 fc = get_basic_fc(ref);
2227 if (!is_interpreted_func(current_iface, current_func))
2229 if (out_attr && !in_attr && pointer_fc == FC_RP)
2230 flags |= FC_ALLOCED_ON_STACK;
2232 else if (get_stub_mode() == MODE_Oif)
2234 if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
2235 flags |= FC_ALLOCED_ON_STACK;
2238 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2239 pointer_fc, flags, string_of_type(pointer_fc),
2240 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2241 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2242 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2243 return 4;
2246 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2248 const decl_spec_t ds = {.type = t};
2249 print_file(file, 0, "/* %u (", tfsoff);
2250 write_type_decl(file, &ds, NULL);
2251 print_file(file, 0, ") */\n");
2254 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2255 type_t *type, unsigned int ref_offset,
2256 enum type_context context,
2257 unsigned int *typestring_offset)
2259 unsigned int offset = *typestring_offset;
2260 type_t *ref = type_pointer_get_ref_type(type);
2262 print_start_tfs_comment(file, type, offset);
2263 update_tfsoff(type, offset, file);
2265 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2267 case TGT_BASIC:
2268 case TGT_ENUM:
2269 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2270 break;
2271 default:
2272 if (ref_offset)
2273 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2274 break;
2277 return offset;
2280 static int processed(const type_t *type)
2282 return type->typestring_offset && !type->tfswrite;
2285 static int user_type_has_variable_size(const type_t *t)
2287 if (is_ptr(t))
2288 return TRUE;
2289 else if (type_get_type(t) == TYPE_STRUCT)
2291 switch (get_struct_fc(t))
2293 case FC_PSTRUCT:
2294 case FC_CSTRUCT:
2295 case FC_CPSTRUCT:
2296 case FC_CVSTRUCT:
2297 return TRUE;
2300 /* Note: Since this only applies to user types, we can't have a conformant
2301 array here, and strings should get filed under pointer in this case. */
2302 return FALSE;
2305 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2307 unsigned int start, absoff, flags;
2308 const char *name = NULL;
2309 type_t *utype = get_user_type(type, &name);
2310 unsigned int usize = type_memsize(utype);
2311 unsigned int ualign = type_buffer_alignment(utype);
2312 unsigned int size = type_memsize(type);
2313 unsigned short funoff = user_type_offset(name);
2314 short reloff;
2316 if (processed(type)) return type->typestring_offset;
2318 guard_rec(type);
2320 if(user_type_has_variable_size(utype)) usize = 0;
2322 if (type_get_type(utype) == TYPE_BASIC ||
2323 type_get_type(utype) == TYPE_ENUM)
2325 unsigned char fc;
2327 if (type_get_type(utype) == TYPE_ENUM)
2328 fc = get_enum_fc(utype);
2329 else
2330 fc = get_basic_fc(utype);
2332 absoff = *tfsoff;
2333 print_start_tfs_comment(file, utype, absoff);
2334 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2335 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2336 *tfsoff += 2;
2338 else
2340 if (!processed(utype))
2341 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2342 absoff = utype->typestring_offset;
2345 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2346 flags = 0x40;
2347 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2348 flags = 0x80;
2349 else
2350 flags = 0;
2352 start = *tfsoff;
2353 update_tfsoff(type, start, file);
2354 print_start_tfs_comment(file, type, start);
2355 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2356 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2357 flags | (ualign - 1), ualign - 1, flags);
2358 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2359 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2360 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2361 *tfsoff += 8;
2362 reloff = absoff - *tfsoff;
2363 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2364 *tfsoff += 2;
2365 return start;
2368 static void write_member_type(FILE *file, const type_t *cont,
2369 int cont_is_complex, const attr_list_t *attrs,
2370 const type_t *type, unsigned int *corroff,
2371 unsigned int *tfsoff)
2373 if (is_embedded_complex(type) && !is_conformant_array(type))
2375 unsigned int absoff;
2376 short reloff;
2378 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2380 absoff = *corroff;
2381 *corroff += 8;
2383 else
2385 absoff = type->typestring_offset;
2387 reloff = absoff - (*tfsoff + 2);
2389 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2390 /* padding is represented using FC_STRUCTPAD* types, so presumably
2391 * this is left over in the format for historical purposes in MIDL
2392 * or rpcrt4. */
2393 print_file(file, 2, "0x0,\n");
2394 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2395 reloff, reloff, absoff);
2396 *tfsoff += 4;
2398 else if (is_ptr(type) || is_conformant_array(type))
2400 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2401 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2402 *tfsoff += 1;
2404 else if (!write_base_type(file, type, tfsoff))
2405 error("Unsupported member type %d\n", type_get_type(type));
2408 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2409 int cont_is_complex, unsigned int *tfsoff)
2411 type_t *elem = type_array_get_element_type(type);
2413 if (!is_embedded_complex(elem) && is_ptr(elem))
2415 type_t *ref = type_pointer_get_ref_type(elem);
2417 if (processed(ref))
2419 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2420 ref->typestring_offset, tfsoff);
2421 return;
2423 if (cont_is_complex && is_string_type(attrs, elem))
2425 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2426 return;
2428 if (!is_string_type(NULL, elem) &&
2429 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2431 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2432 return;
2435 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2438 static void write_end(FILE *file, unsigned int *tfsoff)
2440 if (*tfsoff % 2 == 0)
2442 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2443 *tfsoff += 1;
2445 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2446 *tfsoff += 1;
2449 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2451 unsigned int offset = 0;
2452 var_list_t *fs = type_struct_get_fields(type);
2453 var_t *f;
2455 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2457 type_t *ft = f->declspec.type;
2458 unsigned int size = field_memsize( ft, &offset );
2459 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2461 short reloff;
2462 unsigned int absoff = ft->typestring_offset;
2463 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2464 absoff += 8; /* we already have a corr descr, skip it */
2465 reloff = absoff - (*tfsoff + 6);
2466 print_file(file, 0, "/* %d */\n", *tfsoff);
2467 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2468 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2469 write_conf_or_var_desc(file, current_structure, offset, ft,
2470 get_attrp(f->attrs, ATTR_SWITCHIS));
2471 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2472 (unsigned short)reloff, reloff, absoff);
2473 *tfsoff += 8;
2475 offset += size;
2479 static int write_pointer_description_offsets(
2480 FILE *file, const attr_list_t *attrs, type_t *type,
2481 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2482 unsigned int *typestring_offset)
2484 int written = 0;
2486 if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
2487 (is_array(type) && type_array_is_decl_as_ptr(type)))
2489 if (offset_in_memory && offset_in_buffer)
2491 unsigned int memsize;
2493 /* pointer instance
2495 * note that MSDN states that for pointer layouts in structures,
2496 * this is a negative offset from the end of the structure, but
2497 * this statement is incorrect. all offsets are positive */
2498 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2499 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2501 memsize = type_memsize(type);
2502 *offset_in_memory += memsize;
2503 /* increment these separately as in the case of conformant (varying)
2504 * structures these start at different values */
2505 *offset_in_buffer += memsize;
2507 *typestring_offset += 4;
2509 if (is_ptr(type))
2511 type_t *ref = type_pointer_get_ref_type(type);
2513 if (is_string_type(attrs, type))
2514 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2515 else if (processed(ref))
2516 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2517 ref->typestring_offset, typestring_offset);
2518 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2519 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2520 else
2521 error("write_pointer_description_offsets: type format string unknown\n");
2523 else
2525 unsigned int offset = type->typestring_offset;
2526 /* skip over the pointer that is written for strings, since a
2527 * pointer has to be written in-place here */
2528 if (is_string_type(attrs, type))
2529 offset += 4;
2530 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2533 return 1;
2536 if (is_array(type))
2538 return write_pointer_description_offsets(
2539 file, attrs, type_array_get_element_type(type), offset_in_memory,
2540 offset_in_buffer, typestring_offset);
2542 else if (is_non_complex_struct(type))
2544 /* otherwise search for interesting fields to parse */
2545 const var_t *v;
2546 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2548 if (offset_in_memory && offset_in_buffer)
2550 unsigned int padding;
2551 unsigned int align = 0;
2552 type_memsize_and_alignment(v->declspec.type, &align);
2553 padding = ROUNDING(*offset_in_memory, align);
2554 *offset_in_memory += padding;
2555 *offset_in_buffer += padding;
2557 written += write_pointer_description_offsets(
2558 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2559 typestring_offset);
2562 else
2564 if (offset_in_memory && offset_in_buffer)
2566 unsigned int memsize = type_memsize(type);
2567 *offset_in_memory += memsize;
2568 /* increment these separately as in the case of conformant (varying)
2569 * structures these start at different values */
2570 *offset_in_buffer += memsize;
2574 return written;
2577 static int write_no_repeat_pointer_descriptions(
2578 FILE *file, const attr_list_t *attrs, type_t *type,
2579 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2580 unsigned int *typestring_offset)
2582 int written = 0;
2584 if (is_ptr(type) ||
2585 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2587 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2588 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2589 *typestring_offset += 2;
2591 return write_pointer_description_offsets(file, attrs, type,
2592 offset_in_memory, offset_in_buffer, typestring_offset);
2595 if (is_non_complex_struct(type))
2597 const var_t *v;
2598 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2600 if (offset_in_memory && offset_in_buffer)
2602 unsigned int padding;
2603 unsigned int align = 0;
2604 type_memsize_and_alignment(v->declspec.type, &align);
2605 padding = ROUNDING(*offset_in_memory, align);
2606 *offset_in_memory += padding;
2607 *offset_in_buffer += padding;
2609 written += write_no_repeat_pointer_descriptions(
2610 file, v->attrs, v->declspec.type,
2611 offset_in_memory, offset_in_buffer, typestring_offset);
2614 else
2616 unsigned int memsize = type_memsize(type);
2617 *offset_in_memory += memsize;
2618 /* increment these separately as in the case of conformant (varying)
2619 * structures these start at different values */
2620 *offset_in_buffer += memsize;
2623 return written;
2626 /* Note: if file is NULL return value is number of pointers to write, else
2627 * it is the number of type format characters written */
2628 static int write_fixed_array_pointer_descriptions(
2629 FILE *file, const attr_list_t *attrs, type_t *type,
2630 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2631 unsigned int *typestring_offset)
2633 int pointer_count = 0;
2635 if (type_get_type(type) == TYPE_ARRAY &&
2636 !type_array_has_conformance(type) && !type_array_has_variance(type))
2638 unsigned int temp = 0;
2639 /* unfortunately, this needs to be done in two passes to avoid
2640 * writing out redundant FC_FIXED_REPEAT descriptions */
2641 pointer_count = write_pointer_description_offsets(
2642 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2643 if (pointer_count > 0)
2645 unsigned int increment_size;
2646 unsigned int offset_of_array_pointer_mem = 0;
2647 unsigned int offset_of_array_pointer_buf = 0;
2649 increment_size = type_memsize(type_array_get_element_type(type));
2651 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2652 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2653 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2654 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2655 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2656 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2657 *typestring_offset += 10;
2659 pointer_count = write_pointer_description_offsets(
2660 file, attrs, type, &offset_of_array_pointer_mem,
2661 &offset_of_array_pointer_buf, typestring_offset);
2664 else if (type_get_type(type) == TYPE_STRUCT)
2666 const var_t *v;
2667 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2669 if (offset_in_memory && offset_in_buffer)
2671 unsigned int padding;
2672 unsigned int align = 0;
2673 type_memsize_and_alignment(v->declspec.type, &align);
2674 padding = ROUNDING(*offset_in_memory, align);
2675 *offset_in_memory += padding;
2676 *offset_in_buffer += padding;
2678 pointer_count += write_fixed_array_pointer_descriptions(
2679 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2680 typestring_offset);
2683 else
2685 if (offset_in_memory && offset_in_buffer)
2687 unsigned int memsize;
2688 memsize = type_memsize(type);
2689 *offset_in_memory += memsize;
2690 /* increment these separately as in the case of conformant (varying)
2691 * structures these start at different values */
2692 *offset_in_buffer += memsize;
2696 return pointer_count;
2699 /* Note: if file is NULL return value is number of pointers to write, else
2700 * it is the number of type format characters written */
2701 static int write_conformant_array_pointer_descriptions(
2702 FILE *file, const attr_list_t *attrs, type_t *type,
2703 unsigned int offset_in_memory, unsigned int *typestring_offset)
2705 int pointer_count = 0;
2707 if (is_conformant_array(type) && !type_array_has_variance(type))
2709 unsigned int temp = 0;
2710 /* unfortunately, this needs to be done in two passes to avoid
2711 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2712 pointer_count = write_pointer_description_offsets(
2713 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2714 if (pointer_count > 0)
2716 unsigned int increment_size;
2717 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2718 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2720 increment_size = type_memsize(type_array_get_element_type(type));
2722 if (increment_size > USHRT_MAX)
2723 error("array size of %u bytes is too large\n", increment_size);
2725 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2726 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2727 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2728 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2729 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2730 *typestring_offset += 8;
2732 pointer_count = write_pointer_description_offsets(
2733 file, attrs, type_array_get_element_type(type),
2734 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2735 typestring_offset);
2739 return pointer_count;
2742 /* Note: if file is NULL return value is number of pointers to write, else
2743 * it is the number of type format characters written */
2744 static int write_varying_array_pointer_descriptions(
2745 FILE *file, const attr_list_t *attrs, type_t *type,
2746 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2747 unsigned int *typestring_offset)
2749 int pointer_count = 0;
2751 if (is_array(type) && type_array_has_variance(type))
2753 unsigned int temp = 0;
2754 /* unfortunately, this needs to be done in two passes to avoid
2755 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2756 pointer_count = write_pointer_description_offsets(
2757 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2758 if (pointer_count > 0)
2760 unsigned int increment_size;
2762 increment_size = type_memsize(type_array_get_element_type(type));
2764 if (increment_size > USHRT_MAX)
2765 error("array size of %u bytes is too large\n", increment_size);
2767 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2768 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2769 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2770 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2771 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2772 *typestring_offset += 8;
2774 pointer_count = write_pointer_description_offsets(
2775 file, attrs, type_array_get_element_type(type), offset_in_memory,
2776 offset_in_buffer, typestring_offset);
2779 else if (type_get_type(type) == TYPE_STRUCT)
2781 const var_t *v;
2782 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2784 if (offset_in_memory && offset_in_buffer)
2786 unsigned int align = 0, padding;
2788 if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
2790 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2791 /* skip over variance and offset in buffer */
2792 *offset_in_buffer += 8;
2795 type_memsize_and_alignment(v->declspec.type, &align);
2796 padding = ROUNDING(*offset_in_memory, align);
2797 *offset_in_memory += padding;
2798 *offset_in_buffer += padding;
2800 pointer_count += write_varying_array_pointer_descriptions(
2801 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2802 typestring_offset);
2805 else
2807 if (offset_in_memory && offset_in_buffer)
2809 unsigned int memsize = type_memsize(type);
2810 *offset_in_memory += memsize;
2811 /* increment these separately as in the case of conformant (varying)
2812 * structures these start at different values */
2813 *offset_in_buffer += memsize;
2817 return pointer_count;
2820 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2821 unsigned int *typestring_offset)
2823 unsigned int offset_in_buffer;
2824 unsigned int offset_in_memory;
2826 /* pass 1: search for single instance of a pointer (i.e. don't descend
2827 * into arrays) */
2828 if (!is_array(type))
2830 offset_in_memory = 0;
2831 offset_in_buffer = 0;
2832 write_no_repeat_pointer_descriptions(
2833 file, NULL, type,
2834 &offset_in_memory, &offset_in_buffer, typestring_offset);
2837 /* pass 2: search for pointers in fixed arrays */
2838 offset_in_memory = 0;
2839 offset_in_buffer = 0;
2840 write_fixed_array_pointer_descriptions(
2841 file, NULL, type,
2842 &offset_in_memory, &offset_in_buffer, typestring_offset);
2844 /* pass 3: search for pointers in conformant only arrays (but don't descend
2845 * into conformant varying or varying arrays) */
2846 if (is_conformant_array(type) &&
2847 (type_array_is_decl_as_ptr(type) || !current_structure))
2848 write_conformant_array_pointer_descriptions(
2849 file, attrs, type, 0, typestring_offset);
2850 else if (type_get_type(type) == TYPE_STRUCT &&
2851 get_struct_fc(type) == FC_CPSTRUCT)
2853 type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
2854 write_conformant_array_pointer_descriptions( file, NULL, carray,
2855 type_memsize(type), typestring_offset);
2858 /* pass 4: search for pointers in varying arrays */
2859 offset_in_memory = 0;
2860 offset_in_buffer = 0;
2861 write_varying_array_pointer_descriptions(
2862 file, NULL, type,
2863 &offset_in_memory, &offset_in_buffer, typestring_offset);
2866 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2867 type_t *type, enum type_context context,
2868 const char *name, unsigned int *typestring_offset)
2870 unsigned int start_offset;
2871 unsigned char rtype;
2872 type_t *elem_type;
2873 int is_processed = processed(type);
2875 start_offset = *typestring_offset;
2877 if (is_declptr(type))
2879 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2880 int pointer_type = get_pointer_fc_context(type, attrs, context);
2881 if (!pointer_type)
2882 pointer_type = FC_RP;
2883 print_start_tfs_comment(file, type, *typestring_offset);
2884 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2885 pointer_type, flag, string_of_type(pointer_type),
2886 flag ? " [simple_pointer]" : "");
2887 *typestring_offset += 2;
2888 if (!flag)
2890 print_file(file, 2, "NdrFcShort(0x2),\n");
2891 *typestring_offset += 2;
2893 is_processed = FALSE;
2896 if (is_array(type))
2897 elem_type = type_array_get_element_type(type);
2898 else
2899 elem_type = type_pointer_get_ref_type(type);
2901 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2902 return write_array_tfs(file, attrs, type, name, typestring_offset);
2904 if (type_get_type(elem_type) != TYPE_BASIC)
2906 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2907 return start_offset;
2910 rtype = get_basic_fc(elem_type);
2911 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2913 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2914 return start_offset;
2917 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2919 unsigned int dim = type_array_get_dim(type);
2921 if (is_processed) return start_offset;
2923 /* FIXME: multi-dimensional array */
2924 if (0xffffu < dim)
2925 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2926 name, 0xffffu, dim - 0xffffu);
2928 if (rtype == FC_WCHAR)
2929 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2930 else
2931 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2932 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2933 *typestring_offset += 2;
2935 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2936 *typestring_offset += 2;
2938 update_tfsoff(type, start_offset, file);
2939 return start_offset;
2941 else if (is_conformant_array(type))
2943 if (rtype == FC_WCHAR)
2944 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2945 else
2946 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2947 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2948 *typestring_offset += 2;
2950 *typestring_offset += write_conf_or_var_desc(
2951 file, current_structure,
2952 (!type_array_is_decl_as_ptr(type) && current_structure
2953 ? type_memsize(current_structure)
2954 : 0),
2955 type, type_array_get_conformance(type));
2957 update_tfsoff(type, start_offset, file);
2958 return start_offset;
2960 else
2962 if (is_processed) return start_offset;
2964 if (rtype == FC_WCHAR)
2965 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2966 else
2967 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2968 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2969 *typestring_offset += 2;
2971 update_tfsoff(type, start_offset, file);
2972 return start_offset;
2976 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2977 const char *name, unsigned int *typestring_offset)
2979 const expr_t *length_is = type_array_get_variance(type);
2980 const expr_t *size_is = type_array_get_conformance(type);
2981 unsigned int align;
2982 unsigned int size;
2983 unsigned int start_offset;
2984 unsigned char fc;
2985 unsigned int baseoff
2986 = !type_array_is_decl_as_ptr(type) && current_structure
2987 ? type_memsize(current_structure)
2988 : 0;
2990 if (!is_string_type(attrs, type_array_get_element_type(type)))
2991 write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
2993 size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2994 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2995 fc = get_array_fc(type);
2997 start_offset = *typestring_offset;
2998 update_tfsoff(type, start_offset, file);
2999 print_start_tfs_comment(file, type, start_offset);
3000 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
3001 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3002 *typestring_offset += 2;
3004 align = 0;
3005 if (fc != FC_BOGUS_ARRAY)
3007 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
3009 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
3010 *typestring_offset += 4;
3012 else
3014 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
3015 *typestring_offset += 2;
3018 if (is_conformant_array(type))
3019 *typestring_offset
3020 += write_conf_or_var_desc(file, current_structure, baseoff,
3021 type, size_is);
3023 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
3025 unsigned int elsize = type_memsize(type_array_get_element_type(type));
3026 unsigned int dim = type_array_get_dim(type);
3028 if (fc == FC_LGVARRAY)
3030 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
3031 *typestring_offset += 4;
3033 else
3035 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3036 *typestring_offset += 2;
3039 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
3040 *typestring_offset += 2;
3043 if (length_is)
3044 *typestring_offset
3045 += write_conf_or_var_desc(file, current_structure, baseoff,
3046 type, length_is);
3048 if (type_has_pointers(type_array_get_element_type(type)) &&
3049 (type_array_is_decl_as_ptr(type) || !current_structure))
3051 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3052 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3053 *typestring_offset += 2;
3054 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
3055 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3056 *typestring_offset += 1;
3059 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
3060 write_end(file, typestring_offset);
3062 else
3064 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
3065 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3066 *typestring_offset += 2;
3067 *typestring_offset
3068 += write_conf_or_var_desc(file, current_structure, baseoff,
3069 type, size_is);
3070 *typestring_offset
3071 += write_conf_or_var_desc(file, current_structure, baseoff,
3072 type, length_is);
3074 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
3075 write_end(file, typestring_offset);
3078 return start_offset;
3081 static const var_t *find_array_or_string_in_struct(const type_t *type)
3083 const var_list_t *fields = type_struct_get_fields(type);
3084 const var_t *last_field;
3085 const type_t *ft;
3087 if (!fields || list_empty(fields))
3088 return NULL;
3090 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3091 ft = last_field->declspec.type;
3093 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3094 return last_field;
3096 if (type_get_type(ft) == TYPE_STRUCT)
3097 return find_array_or_string_in_struct(ft);
3098 else
3099 return NULL;
3102 static void write_struct_members(FILE *file, const type_t *type,
3103 int is_complex, unsigned int *corroff,
3104 unsigned int *typestring_offset)
3106 const var_t *field;
3107 unsigned short offset = 0;
3108 unsigned int salign = 1;
3109 int padding;
3110 var_list_t *fields = type_struct_get_fields(type);
3112 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3114 type_t *ft = field->declspec.type;
3115 unsigned int align = 0;
3116 unsigned int size = type_memsize_and_alignment(ft, &align);
3117 align = clamp_align(align);
3118 if (salign < align) salign = align;
3120 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3122 if ((align - 1) & offset)
3124 unsigned char fc = 0;
3125 switch (align)
3127 case 2:
3128 fc = FC_ALIGNM2;
3129 break;
3130 case 4:
3131 fc = FC_ALIGNM4;
3132 break;
3133 case 8:
3134 fc = FC_ALIGNM8;
3135 break;
3136 default:
3137 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3139 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3140 offset = ROUND_SIZE(offset, align);
3141 *typestring_offset += 1;
3143 write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
3144 typestring_offset);
3145 offset += size;
3149 padding = ROUNDING(offset, salign);
3150 if (padding)
3152 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3153 FC_STRUCTPAD1 + padding - 1,
3154 padding);
3155 *typestring_offset += 1;
3158 write_end(file, typestring_offset);
3161 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3162 const char *name, unsigned int *tfsoff)
3164 const type_t *save_current_structure = current_structure;
3165 unsigned int total_size;
3166 const var_t *array;
3167 unsigned int start_offset;
3168 unsigned int align;
3169 unsigned int corroff;
3170 var_t *f;
3171 unsigned char fc = get_struct_fc(type);
3172 var_list_t *fields = type_struct_get_fields(type);
3174 if (processed(type)) return type->typestring_offset;
3176 guard_rec(type);
3177 current_structure = type;
3179 total_size = type_memsize(type);
3180 align = type_buffer_alignment(type);
3181 if (total_size > USHRT_MAX)
3182 error("structure size for %s exceeds %d bytes by %d bytes\n",
3183 name, USHRT_MAX, total_size - USHRT_MAX);
3185 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3186 write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
3188 array = find_array_or_string_in_struct(type);
3189 if (array && !processed(array->declspec.type))
3191 if(is_string_type(array->attrs, array->declspec.type))
3192 write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3193 else
3194 write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
3197 corroff = *tfsoff;
3198 write_descriptors(file, type, tfsoff);
3200 start_offset = *tfsoff;
3201 update_tfsoff(type, start_offset, file);
3202 print_start_tfs_comment(file, type, start_offset);
3203 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3204 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3205 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3206 *tfsoff += 4;
3208 if (array)
3210 unsigned int absoff = array->declspec.type->typestring_offset;
3211 short reloff = absoff - *tfsoff;
3212 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3213 reloff, reloff, absoff);
3214 *tfsoff += 2;
3216 else if (fc == FC_BOGUS_STRUCT)
3218 print_file(file, 2, "NdrFcShort(0x0),\n");
3219 *tfsoff += 2;
3222 if (fc == FC_BOGUS_STRUCT)
3224 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3225 nothing is written to file yet. On the actual writing pass,
3226 this will have been updated. */
3227 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3228 int reloff = absoff - *tfsoff;
3229 assert( reloff >= 0 );
3230 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3231 (unsigned short)reloff, reloff, absoff);
3232 *tfsoff += 2;
3234 else if ((fc == FC_PSTRUCT) ||
3235 (fc == FC_CPSTRUCT) ||
3236 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3238 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3239 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3240 *tfsoff += 2;
3241 write_pointer_description(file, NULL, type, tfsoff);
3242 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3243 *tfsoff += 1;
3246 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3247 tfsoff);
3249 if (fc == FC_BOGUS_STRUCT)
3251 const var_t *f;
3253 type->ptrdesc = *tfsoff;
3254 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3256 type_t *ft = f->declspec.type;
3257 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3259 case TGT_POINTER:
3260 if (is_string_type(f->attrs, ft))
3261 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3262 else
3263 write_pointer_tfs(file, f->attrs, ft,
3264 type_pointer_get_ref_type(ft)->typestring_offset,
3265 TYPE_CONTEXT_CONTAINER, tfsoff);
3266 break;
3267 case TGT_ARRAY:
3268 if (type_array_is_decl_as_ptr(ft))
3270 unsigned int offset;
3272 print_file(file, 0, "/* %d */\n", *tfsoff);
3274 offset = ft->typestring_offset;
3275 /* skip over the pointer that is written for strings, since a
3276 * pointer has to be written in-place here */
3277 if (is_string_type(f->attrs, ft))
3278 offset += 4;
3279 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3281 break;
3282 default:
3283 break;
3286 if (type->ptrdesc == *tfsoff)
3287 type->ptrdesc = 0;
3290 current_structure = save_current_structure;
3291 return start_offset;
3294 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3296 if (t == NULL)
3298 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3300 else
3302 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3304 unsigned char fc;
3305 if (type_get_type(t) == TYPE_BASIC)
3306 fc = get_basic_fc(t);
3307 else
3308 fc = get_enum_fc(t);
3309 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3310 fc, string_of_type(fc));
3312 else if (t->typestring_offset)
3314 short reloff = t->typestring_offset - *tfsoff;
3315 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3316 reloff, reloff, t->typestring_offset);
3318 else
3319 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3322 *tfsoff += 2;
3325 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3326 type_t *type, unsigned int *tfsoff)
3328 unsigned int start_offset;
3329 unsigned int size;
3330 var_list_t *fields;
3331 unsigned int nbranch = 0;
3332 type_t *deftype = NULL;
3333 short nodeftype = 0xffff;
3334 unsigned int dummy;
3335 var_t *f;
3337 if (processed(type) &&
3338 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3339 return type->typestring_offset;
3341 guard_rec(type);
3343 fields = type_union_get_cases(type);
3345 size = union_memsize(fields, &dummy);
3347 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3349 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3350 if (cases)
3351 nbranch += list_count(cases);
3352 if (f->declspec.type)
3353 write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
3356 start_offset = *tfsoff;
3357 update_tfsoff(type, start_offset, file);
3358 print_start_tfs_comment(file, type, start_offset);
3359 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3361 const var_t *sv = type_union_get_switch_value(type);
3362 const type_t *st = sv->declspec.type;
3363 unsigned int align = 0;
3364 unsigned char fc;
3366 if (type_get_type(st) == TYPE_BASIC)
3368 fc = get_basic_fc(st);
3369 switch (fc)
3371 case FC_CHAR:
3372 case FC_SMALL:
3373 case FC_BYTE:
3374 case FC_USMALL:
3375 case FC_WCHAR:
3376 case FC_SHORT:
3377 case FC_USHORT:
3378 case FC_LONG:
3379 case FC_ULONG:
3380 break;
3381 default:
3382 fc = 0;
3383 error("union switch type must be an integer, char, or enum\n");
3386 else if (type_get_type(st) == TYPE_ENUM)
3387 fc = get_enum_fc(st);
3388 else
3389 error("union switch type must be an integer, char, or enum\n");
3391 type_memsize_and_alignment(st, &align);
3392 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3394 if (f->declspec.type)
3395 type_memsize_and_alignment(f->declspec.type, &align);
3398 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3399 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3400 (align << 4) | fc, string_of_type(fc));
3401 *tfsoff += 2;
3403 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3405 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3406 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3407 unsigned char fc;
3409 if (type_get_type(st) == TYPE_BASIC)
3411 fc = get_basic_fc(st);
3412 switch (fc)
3414 case FC_CHAR:
3415 case FC_SMALL:
3416 case FC_USMALL:
3417 case FC_SHORT:
3418 case FC_USHORT:
3419 case FC_LONG:
3420 case FC_ULONG:
3421 case FC_ENUM16:
3422 case FC_ENUM32:
3423 break;
3424 default:
3425 fc = 0;
3426 error("union switch type must be an integer, char, or enum\n");
3429 else if (type_get_type(st) == TYPE_ENUM)
3430 fc = get_enum_fc(st);
3431 else
3432 error("union switch type must be an integer, char, or enum\n");
3434 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3435 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3436 fc, string_of_type(fc));
3437 *tfsoff += 2;
3438 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3439 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3440 *tfsoff += 2;
3441 print_file(file, 0, "/* %u */\n", *tfsoff);
3444 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3445 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3446 *tfsoff += 4;
3448 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3450 type_t *ft = f->declspec.type;
3451 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3452 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3453 expr_t *c;
3455 if (cases == NULL && !deflt)
3456 error("union field %s with neither case nor default attribute\n", f->name);
3458 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3460 /* MIDL doesn't check for duplicate cases, even though that seems
3461 like a reasonable thing to do, it just dumps them to the TFS
3462 like we're going to do here. */
3463 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3464 *tfsoff += 4;
3465 write_branch_type(file, ft, tfsoff);
3468 /* MIDL allows multiple default branches, even though that seems
3469 illogical, it just chooses the last one, which is what we will
3470 do. */
3471 if (deflt)
3473 deftype = ft;
3474 nodeftype = 0;
3478 if (deftype)
3480 write_branch_type(file, deftype, tfsoff);
3482 else
3484 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3485 *tfsoff += 2;
3488 return start_offset;
3491 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3492 unsigned int *typeformat_offset)
3494 unsigned int i;
3495 unsigned int start_offset = *typeformat_offset;
3496 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3498 if (!iid && processed(type)) return type->typestring_offset;
3500 print_start_tfs_comment(file, type, start_offset);
3501 update_tfsoff(type, start_offset, file);
3503 if (iid)
3505 print_file(file, 2, "0x2f, /* FC_IP */\n");
3506 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3507 *typeformat_offset
3508 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3510 else
3512 const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
3513 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3515 if (! uuid)
3516 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3518 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3519 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3520 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3521 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3522 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3523 for (i = 0; i < 8; ++i)
3524 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3526 if (file)
3527 fprintf(file, "\n");
3529 *typeformat_offset += 18;
3531 return start_offset;
3534 static unsigned int write_contexthandle_tfs(FILE *file,
3535 const attr_list_t *attrs,
3536 type_t *type,
3537 enum type_context context,
3538 unsigned int *typeformat_offset)
3540 unsigned int start_offset = *typeformat_offset;
3541 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
3543 print_start_tfs_comment(file, type, start_offset);
3545 if (flags & 0x80) /* via ptr */
3547 int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
3548 if (!pointer_type) pointer_type = FC_RP;
3549 *typeformat_offset += 4;
3550 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3551 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3552 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3555 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3556 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3557 if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
3558 print_file(file, 0, "can't be null, ");
3559 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3560 print_file(file, 0, "serialize, ");
3561 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3562 print_file(file, 0, "no serialize, ");
3563 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3564 print_file(file, 0, "strict, ");
3565 if (flags & HANDLE_PARAM_IS_RETURN)
3566 print_file(file, 0, "return, ");
3567 if (flags & HANDLE_PARAM_IS_OUT)
3568 print_file(file, 0, "out, ");
3569 if (flags & HANDLE_PARAM_IS_IN)
3570 print_file(file, 0, "in, ");
3571 if (flags & HANDLE_PARAM_IS_VIA_PTR)
3572 print_file(file, 0, "via ptr, ");
3573 print_file(file, 0, "*/\n");
3574 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3575 print_file(file, 2, "0, /* FIXME: param num */\n");
3576 *typeformat_offset += 4;
3578 update_tfsoff( type, start_offset, file );
3579 return start_offset;
3582 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3583 type_t *type, expr_list_t *range_list,
3584 unsigned int *typeformat_offset)
3586 unsigned char fc;
3587 unsigned int start_offset = *typeformat_offset;
3588 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3589 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3591 if (type_get_type(type) == TYPE_BASIC)
3592 fc = get_basic_fc(type);
3593 else
3594 fc = get_enum_fc(type);
3596 /* fc must fit in lower 4-bits of 8-bit field below */
3597 assert(fc <= 0xf);
3599 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3600 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3601 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3602 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3603 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3604 update_tfsoff( type, start_offset, file );
3605 *typeformat_offset += 10;
3607 return start_offset;
3610 static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
3611 type_t *type, const char *name,
3612 enum type_context context,
3613 unsigned int *typeformat_offset)
3615 unsigned int offset;
3617 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3619 case TGT_CTXT_HANDLE:
3620 case TGT_CTXT_HANDLE_POINTER:
3621 return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
3622 case TGT_USER_TYPE:
3623 return write_user_tfs(file, type, typeformat_offset);
3624 case TGT_STRING:
3625 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3626 case TGT_ARRAY:
3628 unsigned int off;
3629 /* conformant and pointer arrays are handled specially */
3630 if ((context != TYPE_CONTEXT_CONTAINER &&
3631 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3632 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3633 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3634 else
3635 off = 0;
3636 if (context != TYPE_CONTEXT_CONTAINER &&
3637 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3639 int ptr_type;
3640 ptr_type = get_pointer_fc_context(type, attrs, context);
3641 if (type_array_is_decl_as_ptr(type)
3642 || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
3644 unsigned int absoff = type->typestring_offset;
3645 short reloff = absoff - (*typeformat_offset + 2);
3646 off = *typeformat_offset;
3647 print_file(file, 0, "/* %d */\n", off);
3648 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3649 string_of_type(ptr_type));
3650 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3651 reloff, reloff, absoff);
3652 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3653 *typeformat_offset += 4;
3655 type_array_set_ptr_tfsoff(type, off);
3657 return off;
3659 case TGT_STRUCT:
3660 return write_struct_tfs(file, type, name, typeformat_offset);
3661 case TGT_UNION:
3662 return write_union_tfs(file, attrs, type, typeformat_offset);
3663 case TGT_ENUM:
3664 case TGT_BASIC:
3665 /* nothing to do */
3666 return 0;
3667 case TGT_RANGE:
3669 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3670 if (!range_list)
3671 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3672 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3674 case TGT_IFACE_POINTER:
3675 return write_ip_tfs(file, attrs, type, typeformat_offset);
3676 case TGT_POINTER:
3678 enum type_context ref_context;
3679 type_t *ref = type_pointer_get_ref_type(type);
3681 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3682 ref_context = TYPE_CONTEXT_PARAM;
3683 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3684 ref_context = TYPE_CONTEXT_CONTAINER;
3685 else
3686 ref_context = context;
3688 if (is_string_type(attrs, ref))
3690 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3691 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3693 offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
3694 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3695 return 0;
3696 return offset;
3699 offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
3700 ref_context, typeformat_offset);
3701 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3702 return 0;
3703 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3705 case TGT_INVALID:
3706 break;
3708 error("invalid type %s for var %s\n", type->name, name);
3709 return 0;
3712 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3713 const char *name, int write_ptr, unsigned int *tfsoff)
3715 return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3718 static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
3720 const statement_list_t *stmts = type_iface_get_stmts(iface);
3721 const statement_t *stmt;
3722 var_t *var;
3724 current_iface = iface;
3725 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
3727 switch(stmt->type)
3729 case STMT_DECLARATION:
3731 const var_t *func = stmt->u.var;
3733 if(stmt->u.var->declspec.stgclass != STG_NONE
3734 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
3735 continue;
3737 current_func = func;
3738 if (is_local(func->attrs)) continue;
3740 var = type_function_get_retval(func->declspec.type);
3741 if (!is_void(var->declspec.type))
3742 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
3743 TYPE_CONTEXT_RETVAL, offset);
3745 if (type_function_get_args(func->declspec.type))
3746 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
3747 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
3748 TYPE_CONTEXT_TOPLEVELPARAM, offset );
3749 break;
3752 case STMT_TYPEDEF:
3754 typeref_t *ref;
3755 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
3757 if (is_attr(ref->type->attrs, ATTR_ENCODE)
3758 || is_attr(ref->type->attrs, ATTR_DECODE))
3759 ref->type->typestring_offset = write_type_tfs( file,
3760 ref->type->attrs, ref->type, ref->type->name,
3761 TYPE_CONTEXT_CONTAINER, offset);
3763 break;
3765 default:
3766 break;
3771 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3773 unsigned int typeformat_offset = 2;
3774 for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
3775 return typeformat_offset + 1;
3779 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3781 int indent = 0;
3783 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3784 print_file(file, indent, "{\n");
3785 indent++;
3786 print_file(file, indent, "0,\n");
3787 print_file(file, indent, "{\n");
3788 indent++;
3789 print_file(file, indent, "NdrFcShort(0x0),\n");
3791 set_all_tfswrite(TRUE);
3792 process_tfs(file, stmts, pred);
3794 print_file(file, indent, "0x0\n");
3795 indent--;
3796 print_file(file, indent, "}\n");
3797 indent--;
3798 print_file(file, indent, "};\n");
3799 print_file(file, indent, "\n");
3802 static unsigned int get_required_buffer_size_type(
3803 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3805 *alignment = 0;
3806 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3808 case TGT_USER_TYPE:
3810 const char *uname = NULL;
3811 const type_t *utype = get_user_type(type, &uname);
3812 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3814 case TGT_BASIC:
3815 switch (get_basic_fc(type))
3817 case FC_BYTE:
3818 case FC_CHAR:
3819 case FC_USMALL:
3820 case FC_SMALL:
3821 *alignment = 4;
3822 return 1;
3824 case FC_WCHAR:
3825 case FC_USHORT:
3826 case FC_SHORT:
3827 *alignment = 4;
3828 return 2;
3830 case FC_ULONG:
3831 case FC_LONG:
3832 case FC_FLOAT:
3833 case FC_ERROR_STATUS_T:
3834 *alignment = 4;
3835 return 4;
3837 case FC_HYPER:
3838 case FC_DOUBLE:
3839 *alignment = 8;
3840 return 8;
3842 case FC_INT3264:
3843 case FC_UINT3264:
3844 assert( pointer_size );
3845 *alignment = pointer_size;
3846 return pointer_size;
3848 case FC_IGNORE:
3849 case FC_BIND_PRIMITIVE:
3850 return 0;
3852 default:
3853 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3854 get_basic_fc(type));
3855 return 0;
3857 break;
3859 case TGT_ENUM:
3860 switch (get_enum_fc(type))
3862 case FC_ENUM32:
3863 *alignment = 4;
3864 return 4;
3865 case FC_ENUM16:
3866 *alignment = 4;
3867 return 2;
3869 break;
3871 case TGT_STRUCT:
3872 if (get_struct_fc(type) == FC_STRUCT)
3874 if (!type_struct_get_fields(type)) return 0;
3875 return fields_memsize(type_struct_get_fields(type), alignment);
3877 break;
3879 case TGT_POINTER:
3881 unsigned int size, align;
3882 const type_t *ref = type_pointer_get_ref_type(type);
3883 if (is_string_type( attrs, ref )) break;
3884 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3885 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3887 size += 4 + align;
3888 align = 4;
3890 *alignment = align;
3891 return size;
3894 case TGT_ARRAY:
3895 switch (get_array_fc(type))
3897 case FC_SMFARRAY:
3898 case FC_LGFARRAY:
3899 return type_array_get_dim(type) *
3900 get_required_buffer_size_type(type_array_get_element_type(type), name,
3901 NULL, FALSE, alignment);
3903 break;
3905 default:
3906 break;
3908 return 0;
3911 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3913 int in_attr = is_attr(var->attrs, ATTR_IN);
3914 int out_attr = is_attr(var->attrs, ATTR_OUT);
3916 if (!in_attr && !out_attr)
3917 in_attr = 1;
3919 *alignment = 0;
3921 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3922 pass == PASS_RETURN)
3924 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3926 *alignment = 4;
3927 return 20;
3930 if (!is_string_type(var->attrs, var->declspec.type))
3931 return get_required_buffer_size_type(var->declspec.type, var->name,
3932 var->attrs, TRUE, alignment);
3934 return 0;
3937 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3939 const var_t *var;
3940 unsigned int total_size = 0, alignment;
3942 if (type_function_get_args(func->declspec.type))
3944 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
3946 total_size += get_required_buffer_size(var, &alignment, pass);
3947 total_size += alignment;
3951 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
3953 var_t v = *func;
3954 v.declspec.type = type_function_get_rettype(func->declspec.type);
3955 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3956 total_size += alignment;
3958 return total_size;
3961 static void print_phase_function(FILE *file, int indent, const char *type,
3962 const char *local_var_prefix, enum remoting_phase phase,
3963 const var_t *var, unsigned int type_offset)
3965 const char *function;
3966 switch (phase)
3968 case PHASE_BUFFERSIZE:
3969 function = "BufferSize";
3970 break;
3971 case PHASE_MARSHAL:
3972 function = "Marshall";
3973 break;
3974 case PHASE_UNMARSHAL:
3975 function = "Unmarshall";
3976 break;
3977 case PHASE_FREE:
3978 function = "Free";
3979 break;
3980 default:
3981 assert(0);
3982 return;
3985 print_file(file, indent, "Ndr%s%s(\n", type, function);
3986 indent++;
3987 print_file(file, indent, "&__frame->_StubMsg,\n");
3988 print_file(file, indent, "%s%s%s%s%s,\n",
3989 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3990 (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
3991 local_var_prefix,
3992 (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
3993 var->name);
3994 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3995 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3996 if (phase == PHASE_UNMARSHAL)
3997 print_file(file, indent, "0);\n");
3998 indent--;
4001 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
4002 enum remoting_phase phase, enum pass pass, const var_t *var,
4003 const char *varname)
4005 type_t *type = var->declspec.type;
4006 unsigned int alignment = 0;
4008 /* no work to do for other phases, buffer sizing is done elsewhere */
4009 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
4010 return;
4012 if (type_get_type(type) == TYPE_ENUM ||
4013 (type_get_type(type) == TYPE_BASIC &&
4014 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
4015 pointer_size != 4))
4017 unsigned char fc;
4019 if (type_get_type(type) == TYPE_ENUM)
4020 fc = get_enum_fc(type);
4021 else
4022 fc = get_basic_fc(type);
4024 if (phase == PHASE_MARSHAL)
4025 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
4026 else
4027 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
4028 print_file(file, indent+1, "&__frame->_StubMsg,\n");
4029 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
4030 local_var_prefix,
4031 var->name);
4032 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
4034 else
4036 const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
4038 switch (get_basic_fc(ref->type))
4040 case FC_BYTE:
4041 case FC_CHAR:
4042 case FC_SMALL:
4043 case FC_USMALL:
4044 alignment = 1;
4045 break;
4047 case FC_WCHAR:
4048 case FC_USHORT:
4049 case FC_SHORT:
4050 alignment = 2;
4051 break;
4053 case FC_ULONG:
4054 case FC_LONG:
4055 case FC_FLOAT:
4056 case FC_ERROR_STATUS_T:
4057 /* pointer_size must be 4 if we got here in these two cases */
4058 case FC_INT3264:
4059 case FC_UINT3264:
4060 alignment = 4;
4061 break;
4063 case FC_HYPER:
4064 case FC_DOUBLE:
4065 alignment = 8;
4066 break;
4068 case FC_IGNORE:
4069 case FC_BIND_PRIMITIVE:
4070 /* no marshalling needed */
4071 return;
4073 default:
4074 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
4075 var->name, get_basic_fc(ref->type));
4078 if (phase == PHASE_MARSHAL && alignment > 1)
4079 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
4080 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
4081 alignment - 1, alignment - 1);
4083 if (phase == PHASE_MARSHAL)
4085 print_file(file, indent, "*(");
4086 write_type_decl(file, ref, NULL);
4087 if (is_ptr(type))
4088 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
4089 else
4090 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4091 fprintf(file, "%s%s", local_var_prefix, varname);
4092 fprintf(file, ";\n");
4094 else if (phase == PHASE_UNMARSHAL)
4096 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4097 write_type_decl(file, ref, NULL);
4098 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4099 print_file(file, indent, "{\n");
4100 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4101 print_file(file, indent, "}\n");
4102 print_file(file, indent, "%s%s%s",
4103 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4104 local_var_prefix, varname);
4105 if (pass == PASS_IN && is_ptr(type))
4106 fprintf(file, " = (");
4107 else
4108 fprintf(file, " = *(");
4109 write_type_decl(file, ref, NULL);
4110 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4113 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4114 write_type_decl(file, ref, NULL);
4115 fprintf(file, ");\n");
4119 /* returns whether the MaxCount, Offset or ActualCount members need to be
4120 * filled in for the specified phase */
4121 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4123 return (phase != PHASE_UNMARSHAL);
4126 expr_t *get_size_is_expr(const type_t *t, const char *name)
4128 expr_t *x = NULL;
4130 for ( ; is_array(t); t = type_array_get_element_type(t))
4131 if (type_array_has_conformance(t) &&
4132 type_array_get_conformance(t)->type != EXPR_VOID)
4134 if (!x)
4135 x = type_array_get_conformance(t);
4136 else
4137 error("%s: multidimensional conformant"
4138 " arrays not supported at the top level\n",
4139 name);
4142 return x;
4145 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4146 enum remoting_phase phase, const var_t *var, int valid_variance)
4148 const type_t *type = var->declspec.type;
4149 /* get fundamental type for the argument */
4150 for (;;)
4152 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4154 case TGT_ARRAY:
4155 if (is_conformance_needed_for_phase(phase))
4157 if (type_array_has_conformance(type) &&
4158 type_array_get_conformance(type)->type != EXPR_VOID)
4160 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4161 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4162 fprintf(file, ";\n\n");
4164 if (type_array_has_variance(type))
4166 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4167 if (valid_variance)
4169 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4170 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4171 fprintf(file, ";\n\n");
4173 else
4174 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4177 break;
4178 case TGT_UNION:
4179 if (type_get_type(type) == TYPE_UNION &&
4180 is_conformance_needed_for_phase(phase))
4182 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4183 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4184 fprintf(file, ";\n\n");
4186 break;
4187 case TGT_IFACE_POINTER:
4189 expr_t *iid;
4191 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4193 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4194 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4195 fprintf( file, ";\n\n" );
4197 break;
4199 case TGT_POINTER:
4200 type = type_pointer_get_ref_type(type);
4201 continue;
4202 case TGT_INVALID:
4203 case TGT_USER_TYPE:
4204 case TGT_CTXT_HANDLE:
4205 case TGT_CTXT_HANDLE_POINTER:
4206 case TGT_STRING:
4207 case TGT_BASIC:
4208 case TGT_ENUM:
4209 case TGT_STRUCT:
4210 case TGT_RANGE:
4211 break;
4213 break;
4217 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4218 enum pass pass, enum remoting_phase phase, const var_t *var)
4220 int in_attr, out_attr, pointer_type;
4221 const char *type_str = NULL;
4222 const type_t *type = var->declspec.type;
4223 unsigned int alignment, start_offset = type->typestring_offset;
4225 if (is_ptr(type) || is_array(type))
4226 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4227 else
4228 pointer_type = 0;
4230 in_attr = is_attr(var->attrs, ATTR_IN);
4231 out_attr = is_attr(var->attrs, ATTR_OUT);
4232 if (!in_attr && !out_attr)
4233 in_attr = 1;
4235 if (phase != PHASE_FREE)
4236 switch (pass)
4238 case PASS_IN:
4239 if (!in_attr) return;
4240 break;
4241 case PASS_OUT:
4242 if (!out_attr) return;
4243 break;
4244 case PASS_RETURN:
4245 break;
4248 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4250 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4252 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4254 case TGT_CTXT_HANDLE:
4255 case TGT_CTXT_HANDLE_POINTER:
4256 if (phase == PHASE_MARSHAL)
4258 if (pass == PASS_IN)
4260 /* if the context_handle attribute appears in the chain of types
4261 * without pointers being followed, then the context handle must
4262 * be direct, otherwise it is a pointer */
4263 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4264 print_file(file, indent, "NdrClientContextMarshall(\n");
4265 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4266 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4267 var->name);
4268 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4270 else
4272 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4273 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4274 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4275 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
4276 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4279 else if (phase == PHASE_UNMARSHAL)
4281 if (pass == PASS_OUT || pass == PASS_RETURN)
4283 if (!in_attr)
4284 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4285 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4286 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4287 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
4288 pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
4289 print_file(file, indent + 1, "__frame->_Handle);\n");
4291 else
4293 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4294 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4295 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4298 break;
4299 case TGT_USER_TYPE:
4300 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4301 break;
4302 case TGT_STRING:
4303 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4304 pointer_type != FC_RP)
4306 /* strings returned are assumed to be global and hence don't
4307 * need freeing */
4308 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4309 print_phase_function(file, indent, "Pointer", local_var_prefix,
4310 phase, var, start_offset);
4311 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4312 !in_attr && is_conformant_array(type))
4314 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4315 indent++;
4316 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4319 else
4321 unsigned int real_start_offset = start_offset;
4322 /* skip over pointer description straight to string description */
4323 if (is_declptr(type))
4325 if (is_conformant_array(type))
4326 real_start_offset += 4;
4327 else
4328 real_start_offset += 2;
4330 if (is_array(type) && !is_conformant_array(type))
4331 print_phase_function(file, indent, "NonConformantString",
4332 local_var_prefix, phase, var,
4333 real_start_offset);
4334 else
4335 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4336 phase, var, real_start_offset);
4338 break;
4339 case TGT_ARRAY:
4341 unsigned char tc = get_array_fc(type);
4342 const char *array_type = NULL;
4344 /* We already have the size_is expression since it's at the
4345 top level, but do checks for multidimensional conformant
4346 arrays. When we handle them, we'll need to extend this
4347 function to return a list, and then we'll actually use
4348 the return value. */
4349 get_size_is_expr(type, var->name);
4351 switch (tc)
4353 case FC_SMFARRAY:
4354 case FC_LGFARRAY:
4355 array_type = "FixedArray";
4356 break;
4357 case FC_SMVARRAY:
4358 case FC_LGVARRAY:
4359 array_type = "VaryingArray";
4360 break;
4361 case FC_CARRAY:
4362 array_type = "ConformantArray";
4363 break;
4364 case FC_CVARRAY:
4365 array_type = "ConformantVaryingArray";
4366 break;
4367 case FC_BOGUS_ARRAY:
4368 array_type = "ComplexArray";
4369 break;
4372 if (pointer_type != FC_RP) array_type = "Pointer";
4374 if (phase == PHASE_FREE && pointer_type == FC_RP)
4376 /* these are all unmarshalled by allocating memory */
4377 if (tc == FC_BOGUS_ARRAY ||
4378 tc == FC_CVARRAY ||
4379 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4380 (tc == FC_CARRAY && !in_attr))
4382 if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
4384 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4385 type_array_get_ptr_tfsoff(type));
4386 break;
4388 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4389 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4390 indent++;
4391 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4392 break;
4395 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4396 break;
4398 case TGT_BASIC:
4399 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4400 break;
4401 case TGT_ENUM:
4402 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4403 break;
4404 case TGT_RANGE:
4405 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4406 /* Note: this goes beyond what MIDL does - it only supports arguments
4407 * with the [range] attribute in Oicf mode */
4408 if (phase == PHASE_UNMARSHAL)
4410 const expr_t *range_min;
4411 const expr_t *range_max;
4412 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4413 if (!range_list)
4414 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4415 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4416 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4418 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4419 write_type_decl(file, &var->declspec, NULL);
4420 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4421 write_type_decl(file, &var->declspec, NULL);
4422 fprintf(file, ")0x%x))\n", range_max->cval);
4423 print_file(file, indent, "{\n");
4424 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4425 print_file(file, indent, "}\n");
4427 break;
4428 case TGT_STRUCT:
4429 switch (get_struct_fc(type))
4431 case FC_STRUCT:
4432 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4433 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4434 break;
4435 case FC_PSTRUCT:
4436 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4437 break;
4438 case FC_CSTRUCT:
4439 case FC_CPSTRUCT:
4440 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4441 break;
4442 case FC_CVSTRUCT:
4443 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4444 break;
4445 case FC_BOGUS_STRUCT:
4446 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4447 break;
4448 default:
4449 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4451 break;
4452 case TGT_UNION:
4454 const char *union_type = NULL;
4456 if (type_get_type(type) == TYPE_UNION)
4457 union_type = "NonEncapsulatedUnion";
4458 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4459 union_type = "EncapsulatedUnion";
4461 print_phase_function(file, indent, union_type, local_var_prefix,
4462 phase, var, start_offset);
4463 break;
4465 case TGT_POINTER:
4467 const type_t *ref = type_pointer_get_ref_type(type);
4468 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4470 case TGT_BASIC:
4471 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4472 break;
4473 case TGT_ENUM:
4474 /* base types have known sizes, so don't need a sizing pass
4475 * and don't have any memory to free and so don't need a
4476 * freeing pass */
4477 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4478 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4479 break;
4480 case TGT_STRUCT:
4481 switch (get_struct_fc(ref))
4483 case FC_STRUCT:
4484 /* simple structs have known sizes, so don't need a sizing
4485 * pass and don't have any memory to free and so don't
4486 * need a freeing pass */
4487 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4488 type_str = "SimpleStruct";
4489 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4491 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4492 indent++;
4493 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4494 indent--;
4496 break;
4497 case FC_PSTRUCT:
4498 type_str = "SimpleStruct";
4499 break;
4500 case FC_CSTRUCT:
4501 case FC_CPSTRUCT:
4502 type_str = "ConformantStruct";
4503 break;
4504 case FC_CVSTRUCT:
4505 type_str = "ConformantVaryingStruct";
4506 break;
4507 case FC_BOGUS_STRUCT:
4508 type_str = "ComplexStruct";
4509 break;
4510 default:
4511 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4514 if (type_str)
4516 if (phase == PHASE_FREE)
4517 type_str = "Pointer";
4518 else
4519 start_offset = ref->typestring_offset;
4520 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4522 break;
4523 case TGT_UNION:
4524 if (phase == PHASE_FREE)
4525 type_str = "Pointer";
4526 else
4528 if (type_get_type(ref) == TYPE_UNION)
4529 type_str = "NonEncapsulatedUnion";
4530 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4531 type_str = "EncapsulatedUnion";
4533 start_offset = ref->typestring_offset;
4536 print_phase_function(file, indent, type_str, local_var_prefix,
4537 phase, var, start_offset);
4538 break;
4539 case TGT_USER_TYPE:
4540 if (phase != PHASE_FREE)
4542 type_str = "UserMarshal";
4543 start_offset = ref->typestring_offset;
4545 else type_str = "Pointer";
4547 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4548 break;
4549 case TGT_STRING:
4550 case TGT_POINTER:
4551 case TGT_ARRAY:
4552 case TGT_RANGE:
4553 case TGT_IFACE_POINTER:
4554 case TGT_CTXT_HANDLE:
4555 case TGT_CTXT_HANDLE_POINTER:
4556 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4557 break;
4558 case TGT_INVALID:
4559 assert(0);
4560 break;
4562 else
4563 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4564 break;
4566 case TGT_IFACE_POINTER:
4567 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4568 break;
4569 case TGT_INVALID:
4570 assert(0);
4571 break;
4573 fprintf(file, "\n");
4576 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4577 enum pass pass, enum remoting_phase phase)
4579 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4581 unsigned int size = get_function_buffer_size( func, pass );
4582 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4585 if (pass == PASS_RETURN)
4587 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4588 type_function_get_retval(func->declspec.type) );
4590 else
4592 const var_t *var;
4593 if (!type_function_get_args(func->declspec.type))
4594 return;
4595 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4596 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4601 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4603 unsigned int offset = 0;
4604 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4605 return offset;
4608 static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
4610 const statement_t *stmt;
4611 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
4613 const var_t *func = stmt->u.var;
4614 if (!is_local(func->attrs))
4615 *size += get_size_procformatstring_func( iface, func );
4619 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4621 unsigned int size = 1;
4622 for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
4623 return size;
4626 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4628 set_all_tfswrite(FALSE);
4629 return process_tfs(NULL, stmts, pred);
4632 void declare_stub_args( FILE *file, int indent, const var_t *func )
4634 int in_attr, out_attr;
4635 int i = 0;
4636 var_t *var = type_function_get_retval(func->declspec.type);
4638 /* declare return value */
4639 if (!is_void(var->declspec.type))
4641 if (is_context_handle(var->declspec.type))
4642 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4643 else
4645 print_file(file, indent, "%s", "");
4646 write_type_decl(file, &var->declspec, var->name);
4647 fprintf(file, ";\n");
4651 if (!type_function_get_args(func->declspec.type))
4652 return;
4654 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
4656 in_attr = is_attr(var->attrs, ATTR_IN);
4657 out_attr = is_attr(var->attrs, ATTR_OUT);
4658 if (!out_attr && !in_attr)
4659 in_attr = 1;
4661 if (is_context_handle(var->declspec.type))
4662 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4663 else
4665 if (!in_attr && !is_conformant_array(var->declspec.type))
4667 const decl_spec_t *type_to_print;
4668 char name[16];
4669 print_file(file, indent, "%s", "");
4670 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4671 !type_array_is_decl_as_ptr(var->declspec.type))
4672 type_to_print = &var->declspec;
4673 else
4674 type_to_print = type_pointer_get_ref(var->declspec.type);
4675 sprintf(name, "_W%u", i++);
4676 write_type_decl(file, type_to_print, name);
4677 fprintf(file, ";\n");
4680 print_file(file, indent, "%s", "");
4681 write_type_decl_left(file, &var->declspec);
4682 fprintf(file, " ");
4683 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4684 !type_array_is_decl_as_ptr(var->declspec.type)) {
4685 fprintf(file, "(*%s)", var->name);
4686 } else
4687 fprintf(file, "%s", var->name);
4688 write_type_right(file, var->declspec.type, FALSE);
4689 fprintf(file, ";\n");
4691 if (decl_indirect(var->declspec.type))
4692 print_file(file, indent, "void *_p_%s;\n", var->name);
4698 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4700 int in_attr, out_attr;
4701 int i = 0, sep = 0;
4702 const var_t *var;
4703 type_t *ref;
4705 if (!type_function_get_args(func->declspec.type))
4706 return;
4708 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4710 in_attr = is_attr(var->attrs, ATTR_IN);
4711 out_attr = is_attr(var->attrs, ATTR_OUT);
4712 if (!out_attr && !in_attr)
4713 in_attr = 1;
4715 if (!in_attr)
4717 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4719 switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
4721 case TGT_CTXT_HANDLE_POINTER:
4722 fprintf(file, " = NdrContextHandleInitialize(\n");
4723 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4724 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4725 var->typestring_offset);
4726 break;
4727 case TGT_ARRAY:
4728 if (type_array_has_conformance(var->declspec.type))
4730 unsigned int size;
4731 type_t *type;
4733 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4734 for (type = var->declspec.type;
4735 is_array(type) && type_array_has_conformance(type);
4736 type = type_array_get_element_type(type))
4738 write_expr(file, type_array_get_conformance(type), TRUE,
4739 TRUE, NULL, NULL, local_var_prefix);
4740 fprintf(file, " * ");
4742 size = type_memsize(type);
4743 fprintf(file, "%u);\n", size);
4745 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4746 for (type = var->declspec.type;
4747 is_array(type) && type_array_has_conformance(type);
4748 type = type_array_get_element_type(type))
4750 write_expr(file, type_array_get_conformance(type), TRUE,
4751 TRUE, NULL, NULL, local_var_prefix);
4752 fprintf(file, " * ");
4754 size = type_memsize(type);
4755 fprintf(file, "%u);\n", size);
4757 else
4758 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4759 break;
4760 case TGT_POINTER:
4761 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4762 ref = type_pointer_get_ref_type(var->declspec.type);
4763 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4765 case TGT_BASIC:
4766 case TGT_ENUM:
4767 case TGT_POINTER:
4768 case TGT_RANGE:
4769 case TGT_IFACE_POINTER:
4770 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4771 break;
4772 case TGT_USER_TYPE:
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_ARRAY:
4777 if (type_array_is_decl_as_ptr(ref))
4779 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4780 break;
4782 ref = type_array_get_element_type(ref);
4783 /* fall through */
4784 case TGT_STRUCT:
4785 case TGT_UNION:
4786 if (type_has_pointers(ref))
4787 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4788 local_var_prefix, i, local_var_prefix, i);
4789 break;
4790 case TGT_CTXT_HANDLE:
4791 case TGT_CTXT_HANDLE_POINTER:
4792 case TGT_INVALID:
4793 case TGT_STRING:
4794 /* not initialised */
4795 break;
4797 i++;
4798 break;
4799 default:
4800 break;
4803 sep = 1;
4806 if (sep)
4807 fprintf(file, "\n");
4811 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4812 const char *var_decl, int add_retval )
4814 var_t *retval = type_function_get_retval( func );
4815 const var_list_t *args = type_function_get_args( func );
4816 const var_t *arg;
4817 int needs_packing;
4818 unsigned int align = 0;
4820 if (args)
4821 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4822 if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
4824 needs_packing = (align > pointer_size);
4826 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4827 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4828 print_file(file, 1, "{\n" );
4829 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4831 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4833 print_file(file, 2, "%s", "");
4834 write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
4835 if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
4836 if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
4838 /* FIXME: should check for large args being passed by pointer */
4839 align = 0;
4840 if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
4841 else type_memsize_and_alignment( arg->declspec.type, &align );
4843 if (align < pointer_size)
4844 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4845 fprintf( file, "%s;\n", arg->name );
4847 if (add_retval && !is_void( retval->declspec.type ))
4849 print_file(file, 2, "%s", "");
4850 write_type_left( file, &retval->declspec, NAME_DEFAULT, TRUE, TRUE );
4851 if (needs_space_after( retval->declspec.type )) fputc( ' ', file );
4852 if (!is_array( retval->declspec.type ) && !is_ptr( retval->declspec.type ) &&
4853 type_memsize( retval->declspec.type ) != pointer_size)
4855 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4857 fprintf( file, "%s;\n", retval->name );
4859 print_file(file, 1, "} %s;\n", var_decl );
4860 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4861 print_file( file, 0, "\n" );
4864 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4866 const var_list_t *args = type_function_get_args( func->declspec.type );
4867 const var_t *var;
4869 if (!args) return;
4871 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4872 if (cant_be_null( var ))
4873 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4876 int write_expr_eval_routines(FILE *file, const char *iface)
4878 static const char *var_name = "pS";
4879 static const char *var_name_expr = "pS->";
4880 int result = 0;
4881 struct expr_eval_routine *eval;
4882 unsigned short callback_offset = 0;
4884 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4886 const char *name = eval->name;
4887 result = 1;
4889 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4890 eval->iface ? eval->iface->name : iface, name, callback_offset);
4891 print_file(file, 0, "{\n");
4892 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4894 write_func_param_struct( file, eval->iface, eval->cont_type,
4895 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4897 else
4899 decl_spec_t ds = {.type = (type_t *)eval->cont_type};
4900 print_file(file, 1, "%s", "");
4901 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4902 fprintf(file, " *%s = (", var_name);
4903 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4904 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4906 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4907 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4908 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4909 fprintf(file, ";\n");
4910 print_file(file, 0, "}\n\n");
4911 callback_offset++;
4913 return result;
4916 void write_expr_eval_routine_list(FILE *file, const char *iface)
4918 struct expr_eval_routine *eval;
4919 struct expr_eval_routine *cursor;
4920 unsigned short callback_offset = 0;
4922 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4923 fprintf(file, "{\n");
4925 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4927 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4928 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4929 callback_offset++;
4930 list_remove(&eval->entry);
4931 free(eval->name);
4932 free(eval);
4935 fprintf(file, "};\n\n");
4938 void write_user_quad_list(FILE *file)
4940 user_type_t *ut;
4942 if (list_empty(&user_type_list))
4943 return;
4945 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4946 fprintf(file, "{\n");
4947 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4949 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4950 print_file(file, 1, "{\n");
4951 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4952 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4953 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4954 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4955 print_file(file, 1, "}%s\n", sep);
4957 fprintf(file, "};\n\n");
4960 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4962 const struct str_list_entry_t *endpoint;
4963 const char *p;
4965 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4966 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4967 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4969 print_file( f, 1, "{ (const unsigned char *)\"" );
4970 for (p = endpoint->str; *p && *p != ':'; p++)
4972 if (*p == '"' || *p == '\\') fputc( '\\', f );
4973 fputc( *p, f );
4975 if (!*p) goto error;
4976 if (p[1] != '[') goto error;
4978 fprintf( f, "\", (const unsigned char *)\"" );
4979 for (p += 2; *p && *p != ']'; p++)
4981 if (*p == '"' || *p == '\\') fputc( '\\', f );
4982 fputc( *p, f );
4984 if (*p != ']') goto error;
4985 fprintf( f, "\" },\n" );
4987 print_file( f, 0, "};\n\n" );
4988 return;
4990 error:
4991 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4994 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4995 const char *prefix, unsigned int proc_offset )
4997 const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
4998 int has_ret = !is_void( rettype->type );
4999 const var_list_t *args = type_function_get_args( func->declspec.type );
5000 const var_t *arg;
5001 int len, needs_params = 0;
5003 /* we need a param structure if we have more than one arg */
5004 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
5006 print_file( file, 0, "{\n");
5007 if (needs_params)
5009 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
5010 write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
5011 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
5012 if (args)
5013 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5014 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
5016 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
5018 len = fprintf( file, " %s%s( ",
5019 has_ret ? "_RetVal = " : "",
5020 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
5021 fprintf( file, "&%s_StubDesc,", prefix );
5022 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
5023 if (needs_params)
5025 fprintf( file, ",\n%*s&__params", len, "" );
5027 else if (pointer_size == 8)
5029 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
5030 if (args)
5031 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5032 fprintf( file, ",\n%*s%s", len, "", arg->name );
5034 else
5036 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
5037 else if (args)
5039 arg = LIST_ENTRY( list_head(args), const var_t, entry );
5040 fprintf( file, ",\n%*s&%s", len, "", arg->name );
5043 fprintf( file, " );\n" );
5044 if (has_ret)
5046 print_file( file, 1, "return (" );
5047 write_type_decl_left(file, rettype);
5048 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
5050 print_file( file, 0, "}\n\n");
5053 void write_exceptions( FILE *file )
5055 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
5056 fprintf( file, "\n");
5057 fprintf( file, "#include \"wine/exception.h\"\n");
5058 fprintf( file, "#undef RpcTryExcept\n");
5059 fprintf( file, "#undef RpcExcept\n");
5060 fprintf( file, "#undef RpcEndExcept\n");
5061 fprintf( file, "#undef RpcTryFinally\n");
5062 fprintf( file, "#undef RpcFinally\n");
5063 fprintf( file, "#undef RpcEndFinally\n");
5064 fprintf( file, "#undef RpcExceptionCode\n");
5065 fprintf( file, "#undef RpcAbnormalTermination\n");
5066 fprintf( file, "\n");
5067 fprintf( file, "struct __exception_frame;\n");
5068 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
5069 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
5070 fprintf( file, "\n");
5071 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5072 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
5073 fprintf( file, " __filter_func filter; \\\n");
5074 fprintf( file, " __finally_func finally; \\\n");
5075 fprintf( file, " __wine_jmp_buf jmp; \\\n");
5076 fprintf( file, " DWORD code; \\\n");
5077 fprintf( file, " unsigned char abnormal_termination; \\\n");
5078 fprintf( file, " unsigned char filter_level; \\\n");
5079 fprintf( file, " unsigned char finally_level;\n");
5080 fprintf( file, "\n");
5081 fprintf( file, "struct __exception_frame\n{\n");
5082 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
5083 fprintf( file, "};\n");
5084 fprintf( file, "\n");
5085 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
5086 fprintf( file, "{\n");
5087 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
5088 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
5089 fprintf( file, " {\n");
5090 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5091 fprintf( file, " exc_frame->finally( exc_frame );\n");
5092 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5093 fprintf( file, " }\n");
5094 fprintf( file, " exc_frame->filter_level = 0;\n");
5095 fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
5096 fprintf( file, "}\n");
5097 fprintf( file, "\n");
5098 fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5099 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5100 fprintf( file, " CONTEXT *context,\n");
5101 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5102 fprintf( file, "{\n");
5103 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5104 fprintf( file, "\n");
5105 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5106 fprintf( file, " {\n" );
5107 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5108 fprintf( file, " {\n" );
5109 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5110 fprintf( file, " exc_frame->finally( exc_frame );\n");
5111 fprintf( file, " }\n" );
5112 fprintf( file, " return ExceptionContinueSearch;\n");
5113 fprintf( file, " }\n" );
5114 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5115 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5116 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5117 fprintf( file, " return ExceptionContinueSearch;\n");
5118 fprintf( file, "}\n");
5119 fprintf( file, "\n");
5120 fprintf( file, "#define RpcTryExcept \\\n");
5121 fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
5122 fprintf( file, " { \\\n");
5123 fprintf( file, " if (!__frame->finally_level) \\\n" );
5124 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5125 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5126 fprintf( file, "\n");
5127 fprintf( file, "#define RpcExcept(expr) \\\n");
5128 fprintf( file, " if (!__frame->finally_level) \\\n" );
5129 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5130 fprintf( file, " __frame->filter_level = 0; \\\n" );
5131 fprintf( file, " } \\\n");
5132 fprintf( file, " else \\\n");
5133 fprintf( file, "\n");
5134 fprintf( file, "#define RpcEndExcept\n");
5135 fprintf( file, "\n");
5136 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5137 fprintf( file, "\n");
5138 fprintf( file, "#define RpcTryFinally \\\n");
5139 fprintf( file, " if (!__frame->filter_level) \\\n");
5140 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5141 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5142 fprintf( file, "\n");
5143 fprintf( file, "#define RpcFinally \\\n");
5144 fprintf( file, " if (!__frame->filter_level) \\\n");
5145 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5146 fprintf( file, " __frame->finally_level = 0;\n");
5147 fprintf( file, "\n");
5148 fprintf( file, "#define RpcEndFinally\n");
5149 fprintf( file, "\n");
5150 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5151 fprintf( file, "\n");
5152 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5153 fprintf( file, " do { \\\n");
5154 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5155 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5156 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5157 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5158 fprintf( file, " __frame->filter_level = 0; \\\n");
5159 fprintf( file, " __frame->finally_level = 0; \\\n");
5160 fprintf( file, " } while (0)\n");
5161 fprintf( file, "\n");
5162 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5163 fprintf( file, "\n");
5164 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5165 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5166 fprintf( file, "\n");
5167 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5168 fprintf( file, " DWORD code;\n");
5169 fprintf( file, "\n");
5170 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");