windows.media.speech: Add IIterator<Inspectable*>.
[wine.git] / tools / widl / typegen.c
blobb2d9ab5d100aa95f6b00fbb1d206c02ab01fbffa
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <limits.h>
31 #include "widl.h"
32 #include "utils.h"
33 #include "parser.h"
34 #include "header.h"
35 #include "typetree.h"
37 #include "typegen.h"
38 #include "expr.h"
40 /* round size up to multiple of alignment */
41 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
42 /* value to add on to round size up to a multiple of alignment */
43 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
45 static const type_t *current_structure;
46 static const var_t *current_func;
47 static const type_t *current_iface;
49 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
50 struct expr_eval_routine
52 struct list entry;
53 const type_t *iface;
54 const type_t *cont_type;
55 char *name;
56 unsigned int baseoff;
57 const expr_t *expr;
60 enum type_context
62 TYPE_CONTEXT_TOPLEVELPARAM,
63 TYPE_CONTEXT_PARAM,
64 TYPE_CONTEXT_CONTAINER,
65 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
66 TYPE_CONTEXT_RETVAL,
69 /* parameter flags in Oif mode */
70 static const unsigned short MustSize = 0x0001;
71 static const unsigned short MustFree = 0x0002;
72 static const unsigned short IsPipe = 0x0004;
73 static const unsigned short IsIn = 0x0008;
74 static const unsigned short IsOut = 0x0010;
75 static const unsigned short IsReturn = 0x0020;
76 static const unsigned short IsBasetype = 0x0040;
77 static const unsigned short IsByValue = 0x0080;
78 static const unsigned short IsSimpleRef = 0x0100;
79 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
80 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
82 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
83 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
84 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
85 const char *name, unsigned int *typestring_offset);
86 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
87 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
88 const char *name, int write_ptr, unsigned int *tfsoff);
89 static const var_t *find_array_or_string_in_struct(const type_t *type);
90 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
91 type_t *type, enum type_context context,
92 const char *name, unsigned int *typestring_offset);
93 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
94 const attr_list_t *attrs, int toplevel_param,
95 unsigned int *alignment );
96 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
98 static const char *string_of_type(unsigned char type)
100 switch (type)
102 case FC_BYTE: return "FC_BYTE";
103 case FC_CHAR: return "FC_CHAR";
104 case FC_SMALL: return "FC_SMALL";
105 case FC_USMALL: return "FC_USMALL";
106 case FC_WCHAR: return "FC_WCHAR";
107 case FC_SHORT: return "FC_SHORT";
108 case FC_USHORT: return "FC_USHORT";
109 case FC_LONG: return "FC_LONG";
110 case FC_ULONG: return "FC_ULONG";
111 case FC_FLOAT: return "FC_FLOAT";
112 case FC_HYPER: return "FC_HYPER";
113 case FC_DOUBLE: return "FC_DOUBLE";
114 case FC_ENUM16: return "FC_ENUM16";
115 case FC_ENUM32: return "FC_ENUM32";
116 case FC_IGNORE: return "FC_IGNORE";
117 case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
118 case FC_RP: return "FC_RP";
119 case FC_UP: return "FC_UP";
120 case FC_OP: return "FC_OP";
121 case FC_FP: return "FC_FP";
122 case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
123 case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
124 case FC_STRUCT: return "FC_STRUCT";
125 case FC_PSTRUCT: return "FC_PSTRUCT";
126 case FC_CSTRUCT: return "FC_CSTRUCT";
127 case FC_CPSTRUCT: return "FC_CPSTRUCT";
128 case FC_CVSTRUCT: return "FC_CVSTRUCT";
129 case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
130 case FC_SMFARRAY: return "FC_SMFARRAY";
131 case FC_LGFARRAY: return "FC_LGFARRAY";
132 case FC_SMVARRAY: return "FC_SMVARRAY";
133 case FC_LGVARRAY: return "FC_LGVARRAY";
134 case FC_CARRAY: return "FC_CARRAY";
135 case FC_CVARRAY: return "FC_CVARRAY";
136 case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
137 case FC_ALIGNM2: return "FC_ALIGNM2";
138 case FC_ALIGNM4: return "FC_ALIGNM4";
139 case FC_ALIGNM8: return "FC_ALIGNM8";
140 case FC_POINTER: return "FC_POINTER";
141 case FC_C_CSTRING: return "FC_C_CSTRING";
142 case FC_C_WSTRING: return "FC_C_WSTRING";
143 case FC_CSTRING: return "FC_CSTRING";
144 case FC_WSTRING: return "FC_WSTRING";
145 case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
146 case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
147 case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
148 case FC_IP: return "FC_IP";
149 case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
150 case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
151 case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
152 case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
153 case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
154 case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
155 case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
156 case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
157 case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
158 case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
159 case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
160 case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
161 case FC_STRING_SIZED: return "FC_STRING_SIZED";
162 case FC_NO_REPEAT: return "FC_NO_REPEAT";
163 case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
164 case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
165 case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
166 case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
167 case FC_PP: return "FC_PP";
168 case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
169 case FC_DEREFERENCE: return "FC_DEREFERENCE";
170 case FC_DIV_2: return "FC_DIV_2";
171 case FC_MULT_2: return "FC_MULT_2";
172 case FC_ADD_1: return "FC_ADD_1";
173 case FC_SUB_1: return "FC_SUB_1";
174 case FC_CALLBACK: return "FC_CALLBACK";
175 case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
176 case FC_END: return "FC_END";
177 case FC_PAD: return "FC_PAD";
178 case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
179 case FC_RANGE: return "FC_RANGE";
180 case FC_INT3264: return "FC_INT3264";
181 case FC_UINT3264: return "FC_UINT3264";
182 default:
183 error("string_of_type: unknown type 0x%02x\n", type);
184 return NULL;
188 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
190 const type_t *t = type;
191 for (;;)
193 if (is_attr(t->attrs, attr))
194 return get_attrp(t->attrs, attr);
195 else if (type_is_alias(t))
196 t = type_alias_get_aliasee_type(t);
197 else return NULL;
201 unsigned char get_basic_fc(const type_t *type)
203 int sign = type_basic_get_sign(type);
204 switch (type_basic_get_type(type))
206 case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
207 case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
208 case TYPE_BASIC_INT32:
209 case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
210 case TYPE_BASIC_INT64: return FC_HYPER;
211 case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
212 case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
213 case TYPE_BASIC_BYTE: return FC_BYTE;
214 case TYPE_BASIC_CHAR: return FC_CHAR;
215 case TYPE_BASIC_WCHAR: return FC_WCHAR;
216 case TYPE_BASIC_HYPER: return FC_HYPER;
217 case TYPE_BASIC_FLOAT: return FC_FLOAT;
218 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
219 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
220 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
222 return 0;
225 static unsigned char get_basic_fc_signed(const type_t *type)
227 switch (type_basic_get_type(type))
229 case TYPE_BASIC_INT8: return FC_SMALL;
230 case TYPE_BASIC_INT16: return FC_SHORT;
231 case TYPE_BASIC_INT32: return FC_LONG;
232 case TYPE_BASIC_INT64: return FC_HYPER;
233 case TYPE_BASIC_INT: return FC_LONG;
234 case TYPE_BASIC_INT3264: return FC_INT3264;
235 case TYPE_BASIC_LONG: return FC_LONG;
236 case TYPE_BASIC_BYTE: return FC_BYTE;
237 case TYPE_BASIC_CHAR: return FC_CHAR;
238 case TYPE_BASIC_WCHAR: return FC_WCHAR;
239 case TYPE_BASIC_HYPER: return FC_HYPER;
240 case TYPE_BASIC_FLOAT: return FC_FLOAT;
241 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
242 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
243 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
245 return 0;
248 static inline unsigned int clamp_align(unsigned int align)
250 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
251 if(align > packing) align = packing;
252 return align;
255 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
257 const type_t *t;
258 int pointer_type;
260 assert(is_ptr(type) || is_array(type));
262 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
263 if (pointer_type)
264 return pointer_type;
266 for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
268 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
269 if (pointer_type)
270 return pointer_type;
273 if (toplevel_param)
274 return FC_RP;
276 if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
277 return pointer_type;
279 return FC_UP;
282 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
283 enum type_context context )
285 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
287 if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
288 (context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
289 pointer_fc = FC_OP;
291 return pointer_fc;
294 static unsigned char get_enum_fc(const type_t *type)
296 assert(type_get_type(type) == TYPE_ENUM);
297 if (is_aliaschain_attr(type, ATTR_V1ENUM))
298 return FC_ENUM32;
299 else
300 return FC_ENUM16;
303 static type_t *get_user_type(const type_t *t, const char **pname)
305 for (;;)
307 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
308 if (ut)
310 if (pname)
311 *pname = t->name;
312 return ut;
315 if (type_is_alias(t))
316 t = type_alias_get_aliasee_type(t);
317 else
318 return NULL;
322 static int is_user_type(const type_t *t)
324 return get_user_type(t, NULL) != NULL;
327 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
329 if (is_user_type(type))
330 return TGT_USER_TYPE;
332 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
333 return TGT_CTXT_HANDLE;
335 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
336 return TGT_STRING;
338 switch (type_get_type(type))
340 case TYPE_BASIC:
341 if (!(flags & TDT_IGNORE_RANGES) &&
342 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
343 return TGT_RANGE;
344 return TGT_BASIC;
345 case TYPE_ENUM:
346 if (!(flags & TDT_IGNORE_RANGES) &&
347 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
348 return TGT_RANGE;
349 return TGT_ENUM;
350 case TYPE_POINTER:
351 if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
352 type_get_type(type_pointer_get_ref_type(type)) == TYPE_RUNTIMECLASS ||
353 type_get_type(type_pointer_get_ref_type(type)) == TYPE_DELEGATE ||
354 (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
355 return TGT_IFACE_POINTER;
356 else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
357 return TGT_CTXT_HANDLE_POINTER;
358 else
359 return TGT_POINTER;
360 case TYPE_STRUCT:
361 return TGT_STRUCT;
362 case TYPE_ENCAPSULATED_UNION:
363 case TYPE_UNION:
364 return TGT_UNION;
365 case TYPE_ARRAY:
366 return TGT_ARRAY;
367 case TYPE_FUNCTION:
368 case TYPE_COCLASS:
369 case TYPE_INTERFACE:
370 case TYPE_MODULE:
371 case TYPE_VOID:
372 case TYPE_ALIAS:
373 case TYPE_BITFIELD:
374 case TYPE_RUNTIMECLASS:
375 case TYPE_DELEGATE:
376 break;
377 case TYPE_APICONTRACT:
378 case TYPE_PARAMETERIZED_TYPE:
379 case TYPE_PARAMETER:
380 /* not supposed to be here */
381 assert(0);
382 break;
384 return TGT_INVALID;
387 static int cant_be_null(const var_t *v)
389 switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
391 case TGT_ARRAY:
392 if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
393 /* fall through */
394 case TGT_POINTER:
395 return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
396 case TGT_CTXT_HANDLE_POINTER:
397 return TRUE;
398 default:
399 return 0;
404 static int get_padding(const var_list_t *fields)
406 unsigned short offset = 0;
407 unsigned int salign = 1;
408 const var_t *f;
410 if (!fields)
411 return 0;
413 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
415 type_t *ft = f->declspec.type;
416 unsigned int align = 0;
417 unsigned int size = type_memsize_and_alignment(ft, &align);
418 align = clamp_align(align);
419 if (align > salign) salign = align;
420 offset = ROUND_SIZE(offset, align);
421 offset += size;
424 return ROUNDING(offset, salign);
427 static unsigned int get_stack_size( const var_t *var, int *by_value )
429 unsigned int stack_size;
430 int by_val;
432 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
434 case TGT_BASIC:
435 case TGT_ENUM:
436 case TGT_RANGE:
437 case TGT_STRUCT:
438 case TGT_UNION:
439 case TGT_USER_TYPE:
440 stack_size = type_memsize( var->declspec.type );
441 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
442 break;
443 default:
444 by_val = 0;
445 break;
447 if (!by_val) stack_size = pointer_size;
448 if (by_value) *by_value = by_val;
449 return ROUND_SIZE( stack_size, pointer_size );
452 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
453 const type_t *type, int is_return )
455 unsigned char flags = 0;
456 int is_out;
458 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
460 if (is_ptr(type) &&
461 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
462 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
463 flags |= HANDLE_PARAM_IS_VIA_PTR;
465 if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
467 is_out = is_attr(attrs, ATTR_OUT);
468 if (is_attr(attrs, ATTR_IN) || !is_out)
470 flags |= HANDLE_PARAM_IS_IN;
471 if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
473 if (is_out) flags |= HANDLE_PARAM_IS_OUT;
475 return flags;
478 static unsigned int get_rpc_flags( const attr_list_t *attrs )
480 unsigned int flags = 0;
482 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
483 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
484 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
485 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
486 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
487 return flags;
490 unsigned char get_struct_fc(const type_t *type)
492 int has_pointer = 0;
493 int has_conformance = 0;
494 int has_variance = 0;
495 var_t *field;
496 var_list_t *fields;
498 fields = type_struct_get_fields(type);
500 if (get_padding(fields))
501 return FC_BOGUS_STRUCT;
503 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
505 type_t *t = field->declspec.type;
506 enum typegen_type typegen_type;
508 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
510 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
512 if (is_string_type(field->attrs, field->declspec.type))
514 if (is_conformant_array(t))
515 has_conformance = 1;
516 has_variance = 1;
517 continue;
520 if (is_array(type_array_get_element_type(field->declspec.type)))
521 return FC_BOGUS_STRUCT;
523 if (type_array_has_conformance(field->declspec.type))
525 has_conformance = 1;
526 if (list_next(fields, &field->entry))
527 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
528 field->name);
530 if (type_array_has_variance(t))
531 has_variance = 1;
533 t = type_array_get_element_type(t);
534 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
537 switch (typegen_type)
539 case TGT_USER_TYPE:
540 case TGT_IFACE_POINTER:
541 return FC_BOGUS_STRUCT;
542 case TGT_BASIC:
543 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
544 return FC_BOGUS_STRUCT;
545 break;
546 case TGT_ENUM:
547 if (get_enum_fc(t) == FC_ENUM16)
548 return FC_BOGUS_STRUCT;
549 break;
550 case TGT_POINTER:
551 case TGT_ARRAY:
552 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
553 return FC_BOGUS_STRUCT;
554 has_pointer = 1;
555 break;
556 case TGT_UNION:
557 return FC_BOGUS_STRUCT;
558 case TGT_STRUCT:
560 unsigned char fc = get_struct_fc(t);
561 switch (fc)
563 case FC_STRUCT:
564 break;
565 case FC_CVSTRUCT:
566 has_conformance = 1;
567 has_variance = 1;
568 has_pointer = 1;
569 break;
571 case FC_CPSTRUCT:
572 has_conformance = 1;
573 if (list_next( fields, &field->entry ))
574 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
575 field->name);
576 has_pointer = 1;
577 break;
579 case FC_CSTRUCT:
580 has_conformance = 1;
581 if (list_next( fields, &field->entry ))
582 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
583 field->name);
584 break;
586 case FC_PSTRUCT:
587 has_pointer = 1;
588 break;
590 default:
591 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
592 /* fallthru - treat it as complex */
594 /* as soon as we see one of these these members, it's bogus... */
595 case FC_BOGUS_STRUCT:
596 return FC_BOGUS_STRUCT;
598 break;
600 case TGT_RANGE:
601 return FC_BOGUS_STRUCT;
602 case TGT_STRING:
603 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
604 case TGT_INVALID:
605 case TGT_CTXT_HANDLE:
606 case TGT_CTXT_HANDLE_POINTER:
607 /* checking after parsing should mean that we don't get here. if we do,
608 * it's a checker bug */
609 assert(0);
613 if( has_variance )
615 if ( has_conformance )
616 return FC_CVSTRUCT;
617 else
618 return FC_BOGUS_STRUCT;
620 if( has_conformance && has_pointer )
621 return FC_CPSTRUCT;
622 if( has_conformance )
623 return FC_CSTRUCT;
624 if( has_pointer )
625 return FC_PSTRUCT;
626 return FC_STRUCT;
629 static unsigned char get_array_fc(const type_t *type)
631 unsigned char fc;
632 const expr_t *size_is;
633 const type_t *elem_type;
635 elem_type = type_array_get_element_type(type);
636 size_is = type_array_get_conformance(type);
638 if (!size_is)
640 unsigned int size = type_memsize(elem_type);
641 if (size * type_array_get_dim(type) > 0xffffuL)
642 fc = FC_LGFARRAY;
643 else
644 fc = FC_SMFARRAY;
646 else
647 fc = FC_CARRAY;
649 if (type_array_has_variance(type))
651 if (fc == FC_SMFARRAY)
652 fc = FC_SMVARRAY;
653 else if (fc == FC_LGFARRAY)
654 fc = FC_LGVARRAY;
655 else if (fc == FC_CARRAY)
656 fc = FC_CVARRAY;
659 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
661 case TGT_USER_TYPE:
662 fc = FC_BOGUS_ARRAY;
663 break;
664 case TGT_BASIC:
665 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
666 pointer_size != 4)
667 fc = FC_BOGUS_ARRAY;
668 break;
669 case TGT_STRUCT:
670 switch (get_struct_fc(elem_type))
672 case FC_BOGUS_STRUCT:
673 fc = FC_BOGUS_ARRAY;
674 break;
676 break;
677 case TGT_ENUM:
678 /* is 16-bit enum - if so, wire size differs from mem size and so
679 * the array cannot be block copied, which means the array is complex */
680 if (get_enum_fc(elem_type) == FC_ENUM16)
681 fc = FC_BOGUS_ARRAY;
682 break;
683 case TGT_UNION:
684 case TGT_IFACE_POINTER:
685 fc = FC_BOGUS_ARRAY;
686 break;
687 case TGT_POINTER:
688 /* ref pointers cannot just be block copied. unique pointers to
689 * interfaces need special treatment. either case means the array is
690 * complex */
691 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
692 fc = FC_BOGUS_ARRAY;
693 break;
694 case TGT_RANGE:
695 fc = FC_BOGUS_ARRAY;
696 break;
697 case TGT_CTXT_HANDLE:
698 case TGT_CTXT_HANDLE_POINTER:
699 case TGT_STRING:
700 case TGT_INVALID:
701 case TGT_ARRAY:
702 /* nothing to do for everything else */
703 break;
706 return fc;
709 static int is_non_complex_struct(const type_t *type)
711 return (type_get_type(type) == TYPE_STRUCT &&
712 get_struct_fc(type) != FC_BOGUS_STRUCT);
715 static int type_has_pointers(const type_t *type)
717 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
719 case TGT_USER_TYPE:
720 return FALSE;
721 case TGT_POINTER:
722 return TRUE;
723 case TGT_ARRAY:
724 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
725 case TGT_STRUCT:
727 var_list_t *fields = type_struct_get_fields(type);
728 const var_t *field;
729 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
731 if (type_has_pointers(field->declspec.type))
732 return TRUE;
734 break;
736 case TGT_UNION:
738 var_list_t *fields;
739 const var_t *field;
740 fields = type_union_get_cases(type);
741 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
743 if (field->declspec.type && type_has_pointers(field->declspec.type))
744 return TRUE;
746 break;
748 case TGT_CTXT_HANDLE:
749 case TGT_CTXT_HANDLE_POINTER:
750 case TGT_STRING:
751 case TGT_IFACE_POINTER:
752 case TGT_BASIC:
753 case TGT_ENUM:
754 case TGT_RANGE:
755 case TGT_INVALID:
756 break;
759 return FALSE;
762 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
763 int toplevel_param)
765 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
767 case TGT_USER_TYPE:
768 return FALSE;
769 case TGT_POINTER:
770 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
771 return TRUE;
772 else
773 return FALSE;
774 case TGT_ARRAY:
775 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
776 return TRUE;
777 else
778 return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
779 case TGT_STRUCT:
781 var_list_t *fields = type_struct_get_fields(type);
782 const var_t *field;
783 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
785 if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
786 return TRUE;
788 break;
790 case TGT_UNION:
792 var_list_t *fields;
793 const var_t *field;
794 fields = type_union_get_cases(type);
795 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
797 if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
798 return TRUE;
800 break;
802 case TGT_CTXT_HANDLE:
803 case TGT_CTXT_HANDLE_POINTER:
804 case TGT_STRING:
805 case TGT_IFACE_POINTER:
806 case TGT_BASIC:
807 case TGT_ENUM:
808 case TGT_RANGE:
809 case TGT_INVALID:
810 break;
813 return FALSE;
816 static unsigned short user_type_offset(const char *name)
818 user_type_t *ut;
819 unsigned short off = 0;
820 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
822 if (strcmp(name, ut->name) == 0)
823 return off;
824 ++off;
826 error("user_type_offset: couldn't find type (%s)\n", name);
827 return 0;
830 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
832 type->typestring_offset = offset;
833 if (file) type->tfswrite = FALSE;
836 static void guard_rec(type_t *type)
838 /* types that contain references to themselves (like a linked list),
839 need to be shielded from infinite recursion when writing embedded
840 types */
841 if (type->typestring_offset)
842 type->tfswrite = FALSE;
843 else
844 type->typestring_offset = 1;
847 static int is_embedded_complex(const type_t *type)
849 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
851 case TGT_USER_TYPE:
852 case TGT_STRUCT:
853 case TGT_UNION:
854 case TGT_ARRAY:
855 case TGT_IFACE_POINTER:
856 return TRUE;
857 default:
858 return FALSE;
862 static const char *get_context_handle_type_name(const type_t *type)
864 const type_t *t;
865 for (t = type;
866 is_ptr(t) || type_is_alias(t);
867 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
868 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
869 return t->name;
870 assert(0);
871 return NULL;
874 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
875 do { \
876 if (file) \
877 fprintf(file, "/* %2u */\n", typestring_offset); \
878 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
880 while (0)
882 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
883 static void print_file(FILE *file, int indent, const char *format, ...)
885 va_list va;
886 va_start(va, format);
887 print(file, indent, format, va);
888 va_end(va);
891 void print(FILE *file, int indent, const char *format, va_list va)
893 if (file)
895 if (format[0] != '\n')
896 while (0 < indent--)
897 fprintf(file, " ");
898 vfprintf(file, format, va);
903 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
905 if (decl_indirect(t))
907 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
908 local_var_prefix, n, local_var_prefix, n);
909 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
911 else if (is_ptr(t) || is_array(t))
912 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
915 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
917 const var_t *var = type_function_get_retval(func->declspec.type);
919 if (!is_void(var->declspec.type))
920 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
922 if (!type_function_get_args(func->declspec.type))
923 return;
925 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
926 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
928 fprintf(file, "\n");
931 static void write_formatdesc(FILE *f, int indent, const char *str)
933 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
934 print_file(f, indent, "{\n");
935 print_file(f, indent + 1, "short Pad;\n");
936 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
937 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
938 print_file(f, indent, "\n");
941 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
943 clear_all_offsets();
945 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
946 get_size_typeformatstring(stmts, pred));
948 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
949 get_size_procformatstring(stmts, pred));
951 fprintf(f, "\n");
952 write_formatdesc(f, indent, "TYPE");
953 write_formatdesc(f, indent, "PROC");
954 fprintf(f, "\n");
955 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
956 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
957 print_file(f, indent, "\n");
960 int decl_indirect(const type_t *t)
962 if (is_user_type(t))
963 return TRUE;
964 return (type_get_type(t) != TYPE_BASIC &&
965 type_get_type(t) != TYPE_ENUM &&
966 type_get_type(t) != TYPE_POINTER &&
967 type_get_type(t) != TYPE_ARRAY);
970 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
971 unsigned int *stack_size, unsigned int *typestring_offset )
973 unsigned int alignment, server_size = 0, buffer_size = 0;
974 unsigned char fc = 0;
975 int is_byval;
976 int is_in = is_attr(var->attrs, ATTR_IN);
977 int is_out = is_attr(var->attrs, ATTR_OUT);
979 if (is_return) is_out = TRUE;
980 else if (!is_in && !is_out) is_in = TRUE;
982 *flags = 0;
983 *stack_size = get_stack_size( var, &is_byval );
984 *typestring_offset = var->typestring_offset;
986 if (is_in) *flags |= IsIn;
987 if (is_out) *flags |= IsOut;
988 if (is_return) *flags |= IsReturn;
990 if (!is_string_type( var->attrs, var->declspec.type ))
991 buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
993 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
995 case TGT_BASIC:
996 *flags |= IsBasetype;
997 fc = get_basic_fc_signed( var->declspec.type );
998 if (fc == FC_BIND_PRIMITIVE)
1000 buffer_size = 4; /* actually 0 but avoids setting MustSize */
1001 fc = FC_LONG;
1003 break;
1004 case TGT_ENUM:
1005 *flags |= IsBasetype;
1006 fc = get_enum_fc( var->declspec.type );
1007 break;
1008 case TGT_RANGE:
1009 *flags |= IsByValue;
1010 break;
1011 case TGT_STRUCT:
1012 case TGT_UNION:
1013 case TGT_USER_TYPE:
1014 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1015 break;
1016 case TGT_IFACE_POINTER:
1017 *flags |= MustFree;
1018 break;
1019 case TGT_ARRAY:
1020 *flags |= MustFree;
1021 if (type_array_is_decl_as_ptr(var->declspec.type)
1022 && type_array_get_ptr_tfsoff(var->declspec.type)
1023 && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
1025 *typestring_offset = var->declspec.type->typestring_offset;
1026 *flags |= IsSimpleRef;
1028 break;
1029 case TGT_STRING:
1030 *flags |= MustFree;
1031 if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1033 /* skip over pointer description straight to string description */
1034 if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
1035 else *typestring_offset += 2;
1036 *flags |= IsSimpleRef;
1038 break;
1039 case TGT_CTXT_HANDLE_POINTER:
1040 *flags |= IsSimpleRef;
1041 *typestring_offset += 4;
1042 /* fall through */
1043 case TGT_CTXT_HANDLE:
1044 buffer_size = 20;
1045 break;
1046 case TGT_POINTER:
1047 if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1049 const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
1051 if (!is_string_type( var->attrs, ref ))
1052 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1054 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1056 case TGT_BASIC:
1057 *flags |= IsSimpleRef | IsBasetype;
1058 fc = get_basic_fc( ref );
1059 if (!is_in && is_out) server_size = pointer_size;
1060 break;
1061 case TGT_ENUM:
1062 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1064 *flags |= IsSimpleRef | IsBasetype;
1065 if (!is_in && is_out) server_size = pointer_size;
1067 else
1069 server_size = pointer_size;
1071 break;
1072 case TGT_UNION:
1073 case TGT_USER_TYPE:
1074 case TGT_RANGE:
1075 *flags |= MustFree | IsSimpleRef;
1076 *typestring_offset = ref->typestring_offset;
1077 if (!is_in && is_out) server_size = type_memsize( ref );
1078 break;
1079 case TGT_ARRAY:
1080 *flags |= MustFree;
1081 if (!type_array_is_decl_as_ptr(ref))
1083 *flags |= IsSimpleRef;
1084 *typestring_offset = ref->typestring_offset;
1086 if (!is_in && is_out) server_size = type_memsize( ref );
1087 break;
1088 case TGT_STRING:
1089 case TGT_POINTER:
1090 case TGT_CTXT_HANDLE:
1091 case TGT_CTXT_HANDLE_POINTER:
1092 *flags |= MustFree;
1093 server_size = pointer_size;
1094 break;
1095 case TGT_IFACE_POINTER:
1096 *flags |= MustFree;
1097 if (is_in && is_out) server_size = pointer_size;
1098 break;
1099 case TGT_STRUCT:
1100 *flags |= IsSimpleRef | MustFree;
1101 *typestring_offset = ref->typestring_offset;
1102 switch (get_struct_fc(ref))
1104 case FC_STRUCT:
1105 case FC_PSTRUCT:
1106 case FC_BOGUS_STRUCT:
1107 if (!is_in && is_out) server_size = type_memsize( ref );
1108 break;
1109 default:
1110 break;
1112 break;
1113 case TGT_INVALID:
1114 assert(0);
1117 else /* not ref pointer */
1119 *flags |= MustFree;
1121 break;
1122 case TGT_INVALID:
1123 assert(0);
1126 if (!buffer_size) *flags |= MustSize;
1128 if (server_size)
1130 server_size = (server_size + 7) / 8;
1131 if (server_size < 8) *flags |= server_size << 13;
1133 return fc;
1136 static unsigned char get_func_oi2_flags( const var_t *func )
1138 const var_t *var;
1139 var_list_t *args = type_function_get_args( func->declspec.type );
1140 var_t *retval = type_function_get_retval( func->declspec.type );
1141 unsigned char oi2_flags = 0x40; /* HasExtensions */
1142 unsigned short flags;
1143 unsigned int stack_size, typestring_offset;
1145 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1147 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1148 if (flags & MustSize)
1150 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1151 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1155 if (!is_void( retval->declspec.type ))
1157 oi2_flags |= 0x04; /* HasRet */
1158 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1159 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1161 return oi2_flags;
1164 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1165 int is_return, unsigned int *stack_offset)
1167 char buffer[128];
1168 unsigned int stack_size, typestring_offset;
1169 unsigned short flags;
1170 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1172 strcpy( buffer, "/* flags:" );
1173 if (flags & MustSize) strcat( buffer, " must size," );
1174 if (flags & MustFree) strcat( buffer, " must free," );
1175 if (flags & IsPipe) strcat( buffer, " pipe," );
1176 if (flags & IsIn) strcat( buffer, " in," );
1177 if (flags & IsOut) strcat( buffer, " out," );
1178 if (flags & IsReturn) strcat( buffer, " return," );
1179 if (flags & IsBasetype) strcat( buffer, " base type," );
1180 if (flags & IsByValue) strcat( buffer, " by value," );
1181 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1182 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1183 strcpy( buffer + strlen( buffer ) - 1, " */" );
1184 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1185 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1186 *stack_offset, *stack_offset );
1187 if (flags & IsBasetype)
1189 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1190 print_file( file, indent, "0x0,\n" );
1192 else
1193 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1194 typestring_offset, typestring_offset );
1195 *stack_offset += max( stack_size, pointer_size );
1196 return 6;
1199 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1200 int is_return, int is_interpreted)
1202 unsigned int size;
1204 int is_in = is_attr(var->attrs, ATTR_IN);
1205 int is_out = is_attr(var->attrs, ATTR_OUT);
1207 if (!is_in && !is_out) is_in = TRUE;
1209 if (type_get_type(var->declspec.type) == TYPE_BASIC ||
1210 type_get_type(var->declspec.type) == TYPE_ENUM)
1212 unsigned char fc;
1214 if (is_return)
1215 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1216 else
1217 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1219 if (type_get_type(var->declspec.type) == TYPE_ENUM)
1221 fc = get_enum_fc(var->declspec.type);
1223 else
1225 fc = get_basic_fc_signed(var->declspec.type);
1227 if (fc == FC_BIND_PRIMITIVE)
1228 fc = FC_IGNORE;
1231 print_file(file, indent, "0x%02x, /* %s */\n",
1232 fc, string_of_type(fc));
1233 size = 2; /* includes param type prefix */
1235 else
1237 unsigned short offset = var->typestring_offset;
1239 if (!is_interpreted && is_array(var->declspec.type)
1240 && type_array_is_decl_as_ptr(var->declspec.type)
1241 && type_array_get_ptr_tfsoff(var->declspec.type))
1242 offset = var->declspec.type->typestring_offset;
1244 if (is_return)
1245 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1246 else if (is_in && is_out)
1247 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1248 else if (is_out)
1249 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1250 else
1251 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1253 size = get_stack_size( var, NULL );
1254 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1255 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1256 size = 4; /* includes param type prefix */
1258 return size;
1261 int is_interpreted_func( const type_t *iface, const var_t *func )
1263 const char *str;
1264 const var_t *var;
1265 const var_list_t *args = type_function_get_args( func->declspec.type );
1266 const type_t *ret_type = type_function_get_rettype( func->declspec.type );
1268 if (type_get_type( ret_type ) == TYPE_BASIC)
1270 switch (type_basic_get_type( ret_type ))
1272 case TYPE_BASIC_INT64:
1273 case TYPE_BASIC_HYPER:
1274 /* return value must fit in a long_ptr */
1275 if (pointer_size < 8) return 0;
1276 break;
1277 case TYPE_BASIC_FLOAT:
1278 case TYPE_BASIC_DOUBLE:
1279 /* floating point values can't be returned */
1280 return 0;
1281 default:
1282 break;
1285 if (get_stub_mode() != MODE_Oif && args)
1287 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1288 switch (type_get_type( var->declspec.type ))
1290 case TYPE_BASIC:
1291 switch (type_basic_get_type( var->declspec.type ))
1293 /* floating point arguments are not supported in Oi mode */
1294 case TYPE_BASIC_FLOAT: return 0;
1295 case TYPE_BASIC_DOUBLE: return 0;
1296 default: break;
1298 break;
1299 /* unions passed by value are not supported in Oi mode */
1300 case TYPE_UNION: return 0;
1301 case TYPE_ENCAPSULATED_UNION: return 0;
1302 default: break;
1306 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1307 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1308 return (get_stub_mode() != MODE_Os);
1311 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1312 const var_t *func, unsigned int *offset,
1313 unsigned short num_proc )
1315 var_t *var;
1316 var_list_t *args = type_function_get_args( func->declspec.type );
1317 unsigned char explicit_fc, implicit_fc;
1318 unsigned char handle_flags;
1319 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1320 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1321 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1322 unsigned int nb_args = 0;
1323 unsigned int stack_size = 0;
1324 unsigned short param_num = 0;
1325 unsigned short handle_stack_offset = 0;
1326 unsigned short handle_param_num = 0;
1328 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1329 if (is_object( iface ))
1331 oi_flags |= Oi_OBJECT_PROC;
1332 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1333 stack_size += pointer_size;
1336 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1338 if (var == handle_var)
1340 handle_stack_offset = stack_size;
1341 handle_param_num = param_num;
1343 stack_size += get_stack_size( var, NULL );
1344 param_num++;
1345 nb_args++;
1347 if (!is_void( type_function_get_rettype( func->declspec.type )))
1349 stack_size += pointer_size;
1350 nb_args++;
1353 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1354 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1355 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1356 print_file( file, indent, "0x%02x,\n", oi_flags );
1357 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1358 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1359 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1360 *offset += 10;
1362 if (!implicit_fc)
1364 switch (explicit_fc)
1366 case FC_BIND_PRIMITIVE:
1367 handle_flags = 0;
1368 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1369 print_file( file, indent, "0x%02x,\n", handle_flags );
1370 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1371 handle_stack_offset, handle_stack_offset );
1372 *offset += 4;
1373 break;
1374 case FC_BIND_GENERIC:
1375 handle_flags = type_memsize( handle_var->declspec.type );
1376 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1377 print_file( file, indent, "0x%02x,\n", handle_flags );
1378 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1379 handle_stack_offset, handle_stack_offset );
1380 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
1381 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1382 *offset += 6;
1383 break;
1384 case FC_BIND_CONTEXT:
1385 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
1386 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1387 print_file( file, indent, "0x%02x,\n", handle_flags );
1388 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1389 handle_stack_offset, handle_stack_offset );
1390 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
1391 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1392 *offset += 6;
1393 break;
1397 if (get_stub_mode() == MODE_Oif)
1399 unsigned char oi2_flags = get_func_oi2_flags( func );
1400 unsigned char ext_flags = 0;
1401 unsigned int size;
1403 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1404 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1405 if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
1407 size = get_function_buffer_size( func, PASS_IN );
1408 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1409 size = get_function_buffer_size( func, PASS_OUT );
1410 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1411 print_file( file, indent, "0x%02x,\n", oi2_flags );
1412 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1413 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1414 print_file( file, indent, "0x%02x,\n", ext_flags );
1415 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1416 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1417 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1418 *offset += 14;
1419 if (pointer_size == 8)
1421 unsigned short pos = 0, fpu_mask = 0;
1423 if (is_object( iface )) pos += 2;
1424 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1426 if (type_get_type( var->declspec.type ) == TYPE_BASIC)
1428 switch (type_basic_get_type( var->declspec.type ))
1430 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1431 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1432 default: break;
1435 pos += 2;
1436 if (pos >= 16) break;
1438 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1439 *offset += 2;
1444 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1445 const var_t *func, unsigned int *offset,
1446 unsigned short num_proc )
1448 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1449 int is_interpreted = is_interpreted_func( iface, func );
1450 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1451 var_t *retval = type_function_get_retval( func->declspec.type );
1453 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1455 /* emit argument data */
1456 if (type_function_get_args(func->declspec.type))
1458 const var_t *var;
1459 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1461 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1462 if (is_new_style)
1463 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1464 else
1465 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1469 /* emit return value data */
1470 if (is_void(retval->declspec.type))
1472 if (!is_new_style)
1474 print_file(file, 0, "/* %u (void) */\n", *offset);
1475 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1476 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1477 *offset += 2;
1480 else
1482 print_file( file, 0, "/* %u (return value) */\n", *offset );
1483 if (is_new_style)
1484 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1485 else
1486 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1490 static void for_each_iface(const statement_list_t *stmts,
1491 void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
1492 type_pred_t pred, FILE *file, int indent, unsigned int *offset)
1494 const statement_t *stmt;
1495 type_t *iface;
1497 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1499 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
1500 continue;
1501 iface = stmt->u.type;
1502 if (!pred(iface)) continue;
1503 proc(iface, file, indent, offset);
1504 if (type_iface_get_async_iface(iface))
1505 proc(type_iface_get_async_iface(iface), file, indent, offset);
1509 static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
1511 const statement_t *stmt;
1512 const type_t *parent = type_iface_get_inherit( iface );
1513 int count = parent ? count_methods( parent ) : 0;
1515 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1517 var_t *func = stmt->u.var;
1518 if (is_local(func->attrs)) continue;
1519 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1523 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1525 int indent = 0;
1526 unsigned int offset = 0;
1528 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1529 print_file(file, indent, "{\n");
1530 indent++;
1531 print_file(file, indent, "0,\n");
1532 print_file(file, indent, "{\n");
1533 indent++;
1535 for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
1537 print_file(file, indent, "0x0\n");
1538 indent--;
1539 print_file(file, indent, "}\n");
1540 indent--;
1541 print_file(file, indent, "};\n");
1542 print_file(file, indent, "\n");
1545 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1547 const statement_t *stmt;
1548 int indent = 0;
1550 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1551 iface->name );
1552 print_file( file, indent, "{\n" );
1553 indent++;
1554 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1556 var_t *func = stmt->u.var;
1557 if (is_local( func->attrs )) continue;
1558 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1560 indent--;
1561 print_file( file, indent, "};\n\n" );
1564 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1566 unsigned char fc;
1568 if (type_get_type(type) == TYPE_BASIC)
1569 fc = get_basic_fc_signed(type);
1570 else if (type_get_type(type) == TYPE_ENUM)
1571 fc = get_enum_fc(type);
1572 else
1573 return 0;
1575 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1576 *typestring_offset += 1;
1577 return 1;
1580 /* write conformance / variance descriptor */
1581 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1582 unsigned int baseoff, const type_t *type,
1583 const expr_t *expr)
1585 unsigned char operator_type = 0;
1586 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1587 const char *conftype_string = "field";
1588 const expr_t *subexpr;
1589 const type_t *iface = NULL;
1590 const char *name;
1592 if (!expr)
1594 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1595 return 4;
1598 if (expr->is_const)
1600 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1601 error("write_conf_or_var_desc: constant value %d is greater than "
1602 "the maximum constant size of %d\n", expr->cval,
1603 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1605 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1606 FC_CONSTANT_CONFORMANCE, expr->cval);
1607 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1608 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1610 return 4;
1613 if (!cont_type) /* top-level conformance */
1615 conftype = FC_TOP_LEVEL_CONFORMANCE;
1616 conftype_string = "parameter";
1617 cont_type = current_func->declspec.type;
1618 name = current_func->name;
1619 iface = current_iface;
1621 else
1623 name = cont_type->name;
1624 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1626 conftype = FC_POINTER_CONFORMANCE;
1627 conftype_string = "field pointer";
1631 subexpr = expr;
1632 switch (subexpr->type)
1634 case EXPR_PPTR:
1635 subexpr = subexpr->ref;
1636 operator_type = FC_DEREFERENCE;
1637 break;
1638 case EXPR_DIV:
1639 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1641 subexpr = subexpr->ref;
1642 operator_type = FC_DIV_2;
1644 break;
1645 case EXPR_MUL:
1646 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1648 subexpr = subexpr->ref;
1649 operator_type = FC_MULT_2;
1651 break;
1652 case EXPR_SUB:
1653 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1655 subexpr = subexpr->ref;
1656 operator_type = FC_SUB_1;
1658 break;
1659 case EXPR_ADD:
1660 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1662 subexpr = subexpr->ref;
1663 operator_type = FC_ADD_1;
1665 break;
1666 default:
1667 break;
1670 if (subexpr->type == EXPR_IDENTIFIER)
1672 const type_t *correlation_variable = NULL;
1673 unsigned char param_type = 0;
1674 unsigned int offset = 0;
1675 const var_t *var;
1676 struct expr_loc expr_loc;
1678 if (type_get_type(cont_type) == TYPE_FUNCTION)
1680 var_list_t *args = type_function_get_args( cont_type );
1682 if (is_object( iface )) offset += pointer_size;
1683 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1685 if (var->name && !strcmp(var->name, subexpr->u.sval))
1687 expr_loc.v = var;
1688 correlation_variable = var->declspec.type;
1689 break;
1691 offset += get_stack_size( var, NULL );
1694 else
1696 var_list_t *fields = type_struct_get_fields( cont_type );
1698 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1700 unsigned int size = field_memsize( var->declspec.type, &offset );
1701 if (var->name && !strcmp(var->name, subexpr->u.sval))
1703 expr_loc.v = var;
1704 correlation_variable = var->declspec.type;
1705 break;
1707 offset += size;
1711 if (!correlation_variable)
1712 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1713 expr_loc.attr = NULL;
1714 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1716 offset -= baseoff;
1718 if (type_get_type(correlation_variable) == TYPE_BASIC)
1720 switch (get_basic_fc(correlation_variable))
1722 case FC_CHAR:
1723 case FC_SMALL:
1724 param_type = FC_SMALL;
1725 break;
1726 case FC_BYTE:
1727 case FC_USMALL:
1728 param_type = FC_USMALL;
1729 break;
1730 case FC_WCHAR:
1731 case FC_SHORT:
1732 param_type = FC_SHORT;
1733 break;
1734 case FC_USHORT:
1735 param_type = FC_USHORT;
1736 break;
1737 case FC_LONG:
1738 param_type = FC_LONG;
1739 break;
1740 case FC_ULONG:
1741 param_type = FC_ULONG;
1742 break;
1743 default:
1744 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1745 get_basic_fc(correlation_variable));
1748 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1750 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1751 param_type = FC_LONG;
1752 else
1753 param_type = FC_SHORT;
1755 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1757 if (pointer_size == 8)
1758 param_type = FC_HYPER;
1759 else
1760 param_type = FC_LONG;
1762 else
1764 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1765 subexpr->u.sval);
1766 return 0;
1769 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1770 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1771 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1772 operator_type ? string_of_type(operator_type) : "no operators");
1773 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1774 (unsigned short)offset, offset);
1776 else if (!iface || is_interpreted_func( iface, current_func ))
1778 unsigned int callback_offset = 0;
1779 struct expr_eval_routine *eval;
1780 int found = 0;
1782 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1784 if (eval->cont_type == cont_type ||
1785 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1786 eval->iface == iface &&
1787 eval->name && name && !strcmp(eval->name, name) &&
1788 !compare_expr(eval->expr, expr)))
1790 found = 1;
1791 break;
1793 callback_offset++;
1796 if (!found)
1798 eval = xmalloc (sizeof(*eval));
1799 eval->iface = iface;
1800 eval->cont_type = cont_type;
1801 eval->name = xstrdup( name );
1802 eval->baseoff = baseoff;
1803 eval->expr = expr;
1804 list_add_tail (&expr_eval_routines, &eval->entry);
1807 if (callback_offset > USHRT_MAX)
1808 error("Maximum number of callback routines reached\n");
1810 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1811 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1812 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1814 else /* output a dummy corr desc that isn't used */
1816 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1817 print_file(file, 2, "0x0,\n" );
1818 print_file(file, 2, "NdrFcShort(0x0),\n" );
1820 return 4;
1823 /* return size and start offset of a data field based on current offset */
1824 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1826 unsigned int align = 0;
1827 unsigned int size = type_memsize_and_alignment( type, &align );
1829 *offset = ROUND_SIZE( *offset, align );
1830 return size;
1833 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1835 unsigned int size = 0;
1836 unsigned int max_align;
1837 const var_t *v;
1839 if (!fields) return 0;
1840 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1842 unsigned int falign = 0;
1843 unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
1844 if (*align < falign) *align = falign;
1845 falign = clamp_align(falign);
1846 size = ROUND_SIZE(size, falign);
1847 size += fsize;
1850 max_align = clamp_align(*align);
1851 size = ROUND_SIZE(size, max_align);
1853 return size;
1856 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1858 unsigned int size, maxs = 0;
1859 unsigned int align = *pmaxa;
1860 const var_t *v;
1862 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1864 /* we could have an empty default field with NULL type */
1865 if (v->declspec.type)
1867 size = type_memsize_and_alignment(v->declspec.type, &align);
1868 if (maxs < size) maxs = size;
1869 if (*pmaxa < align) *pmaxa = align;
1873 return maxs;
1876 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1878 unsigned int size = 0;
1880 switch (type_get_type(t))
1882 case TYPE_BASIC:
1883 switch (get_basic_fc(t))
1885 case FC_BYTE:
1886 case FC_CHAR:
1887 case FC_USMALL:
1888 case FC_SMALL:
1889 size = 1;
1890 if (size > *align) *align = size;
1891 break;
1892 case FC_WCHAR:
1893 case FC_USHORT:
1894 case FC_SHORT:
1895 size = 2;
1896 if (size > *align) *align = size;
1897 break;
1898 case FC_ULONG:
1899 case FC_LONG:
1900 case FC_ERROR_STATUS_T:
1901 case FC_FLOAT:
1902 size = 4;
1903 if (size > *align) *align = size;
1904 break;
1905 case FC_HYPER:
1906 case FC_DOUBLE:
1907 size = 8;
1908 if (size > *align) *align = size;
1909 break;
1910 case FC_INT3264:
1911 case FC_UINT3264:
1912 case FC_BIND_PRIMITIVE:
1913 assert( pointer_size );
1914 size = pointer_size;
1915 if (size > *align) *align = size;
1916 break;
1917 default:
1918 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1919 size = 0;
1921 break;
1922 case TYPE_ENUM:
1923 switch (get_enum_fc(t))
1925 case FC_ENUM16:
1926 case FC_ENUM32:
1927 size = 4;
1928 if (size > *align) *align = size;
1929 break;
1930 default:
1931 error("type_memsize: Unknown enum type\n");
1932 size = 0;
1934 break;
1935 case TYPE_STRUCT:
1936 size = fields_memsize(type_struct_get_fields(t), align);
1937 break;
1938 case TYPE_ENCAPSULATED_UNION:
1939 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1940 break;
1941 case TYPE_UNION:
1942 size = union_memsize(type_union_get_cases(t), align);
1943 break;
1944 case TYPE_POINTER:
1945 case TYPE_INTERFACE:
1946 assert( pointer_size );
1947 size = pointer_size;
1948 if (size > *align) *align = size;
1949 break;
1950 case TYPE_ARRAY:
1951 if (!type_array_is_decl_as_ptr(t))
1953 if (is_conformant_array(t))
1955 type_memsize_and_alignment(type_array_get_element_type(t), align);
1956 size = 0;
1958 else
1959 size = type_array_get_dim(t) *
1960 type_memsize_and_alignment(type_array_get_element_type(t), align);
1962 else /* declared as a pointer */
1964 assert( pointer_size );
1965 size = pointer_size;
1966 if (size > *align) *align = size;
1968 break;
1969 case TYPE_ALIAS:
1970 case TYPE_VOID:
1971 case TYPE_COCLASS:
1972 case TYPE_MODULE:
1973 case TYPE_FUNCTION:
1974 case TYPE_BITFIELD:
1975 case TYPE_APICONTRACT:
1976 case TYPE_RUNTIMECLASS:
1977 case TYPE_PARAMETERIZED_TYPE:
1978 case TYPE_PARAMETER:
1979 case TYPE_DELEGATE:
1980 /* these types should not be encountered here due to language
1981 * restrictions (interface, void, coclass, module), logical
1982 * restrictions (alias - due to type_get_type call above) or
1983 * checking restrictions (function, bitfield). */
1984 assert(0);
1987 return size;
1990 unsigned int type_memsize(const type_t *t)
1992 unsigned int align = 0;
1993 return type_memsize_and_alignment( t, &align );
1996 static unsigned int type_buffer_alignment(const type_t *t)
1998 const var_list_t *fields;
1999 const var_t *var;
2000 unsigned int max = 0, align;
2002 switch (type_get_type(t))
2004 case TYPE_BASIC:
2005 switch (get_basic_fc(t))
2007 case FC_BYTE:
2008 case FC_CHAR:
2009 case FC_USMALL:
2010 case FC_SMALL:
2011 return 1;
2012 case FC_WCHAR:
2013 case FC_USHORT:
2014 case FC_SHORT:
2015 return 2;
2016 case FC_ULONG:
2017 case FC_LONG:
2018 case FC_ERROR_STATUS_T:
2019 case FC_FLOAT:
2020 case FC_INT3264:
2021 case FC_UINT3264:
2022 return 4;
2023 case FC_HYPER:
2024 case FC_DOUBLE:
2025 return 8;
2026 default:
2027 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
2029 break;
2030 case TYPE_ENUM:
2031 switch (get_enum_fc(t))
2033 case FC_ENUM16:
2034 return 2;
2035 case FC_ENUM32:
2036 return 4;
2037 default:
2038 error("type_buffer_alignment: Unknown enum type\n");
2040 break;
2041 case TYPE_STRUCT:
2042 if (!(fields = type_struct_get_fields(t))) break;
2043 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2045 if (!var->declspec.type) continue;
2046 align = type_buffer_alignment( var->declspec.type );
2047 if (max < align) max = align;
2049 break;
2050 case TYPE_ENCAPSULATED_UNION:
2051 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2052 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2054 if (!var->declspec.type) continue;
2055 align = type_buffer_alignment( var->declspec.type );
2056 if (max < align) max = align;
2058 break;
2059 case TYPE_UNION:
2060 if (!(fields = type_union_get_cases(t))) break;
2061 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2063 if (!var->declspec.type) continue;
2064 align = type_buffer_alignment( var->declspec.type );
2065 if (max < align) max = align;
2067 break;
2068 case TYPE_ARRAY:
2069 if (!type_array_is_decl_as_ptr(t))
2070 return type_buffer_alignment( type_array_get_element_type(t) );
2071 /* else fall through */
2072 case TYPE_POINTER:
2073 return 4;
2074 case TYPE_INTERFACE:
2075 case TYPE_ALIAS:
2076 case TYPE_VOID:
2077 case TYPE_COCLASS:
2078 case TYPE_MODULE:
2079 case TYPE_FUNCTION:
2080 case TYPE_BITFIELD:
2081 case TYPE_APICONTRACT:
2082 case TYPE_RUNTIMECLASS:
2083 case TYPE_PARAMETERIZED_TYPE:
2084 case TYPE_PARAMETER:
2085 case TYPE_DELEGATE:
2086 /* these types should not be encountered here due to language
2087 * restrictions (interface, void, coclass, module), logical
2088 * restrictions (alias - due to type_get_type call above) or
2089 * checking restrictions (function, bitfield). */
2090 assert(0);
2092 return max;
2095 int is_full_pointer_function(const var_t *func)
2097 const var_t *var;
2098 if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
2099 return TRUE;
2100 if (!type_function_get_args(func->declspec.type))
2101 return FALSE;
2102 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
2103 if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
2104 return TRUE;
2105 return FALSE;
2108 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2110 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2111 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2112 fprintf(file, "\n");
2115 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2117 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2118 fprintf(file, "\n");
2121 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2122 const type_t *type,
2123 enum type_context context,
2124 unsigned int offset,
2125 unsigned int *typeformat_offset)
2127 unsigned int start_offset = *typeformat_offset;
2128 short reloff = offset - (*typeformat_offset + 2);
2129 int in_attr, out_attr;
2130 int pointer_type;
2131 unsigned char flags = 0;
2133 pointer_type = get_pointer_fc_context(type, attrs, context);
2135 in_attr = is_attr(attrs, ATTR_IN);
2136 out_attr = is_attr(attrs, ATTR_OUT);
2137 if (!in_attr && !out_attr) in_attr = 1;
2139 if (!is_interpreted_func(current_iface, current_func))
2141 if (out_attr && !in_attr && pointer_type == FC_RP)
2142 flags |= FC_ALLOCED_ON_STACK;
2144 else if (get_stub_mode() == MODE_Oif)
2146 if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
2148 switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
2150 case TGT_STRING:
2151 case TGT_POINTER:
2152 case TGT_CTXT_HANDLE:
2153 case TGT_CTXT_HANDLE_POINTER:
2154 case TGT_ARRAY:
2155 flags |= FC_ALLOCED_ON_STACK;
2156 break;
2157 case TGT_IFACE_POINTER:
2158 if (in_attr && out_attr)
2159 flags |= FC_ALLOCED_ON_STACK;
2160 break;
2161 default:
2162 break;
2167 if (is_ptr(type))
2169 type_t *ref = type_pointer_get_ref_type(type);
2170 if(is_declptr(ref) && !is_user_type(ref))
2171 flags |= FC_POINTER_DEREF;
2172 if (pointer_type != FC_RP) {
2173 flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
2177 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2178 pointer_type,
2179 flags,
2180 string_of_type(pointer_type));
2181 if (file)
2183 if (flags & FC_ALLOCED_ON_STACK)
2184 fprintf(file, " [allocated_on_stack]");
2185 if (flags & FC_POINTER_DEREF)
2186 fprintf(file, " [pointer_deref]");
2187 if (flags & FC_DONT_FREE)
2188 fprintf(file, " [dont_free]");
2189 if (flags & FC_ALLOCATE_ALL_NODES)
2190 fprintf(file, " [all_nodes]");
2191 fprintf(file, " */\n");
2194 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2195 *typeformat_offset += 4;
2197 return start_offset;
2200 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2201 const type_t *type, enum type_context context)
2203 unsigned char fc;
2204 unsigned char pointer_fc;
2205 const type_t *ref;
2206 int in_attr = is_attr(attrs, ATTR_IN);
2207 int out_attr = is_attr(attrs, ATTR_OUT);
2208 unsigned char flags = FC_SIMPLE_POINTER;
2210 /* for historical reasons, write_simple_pointer also handled string types,
2211 * but no longer does. catch bad uses of the function with this check */
2212 if (is_string_type(attrs, type))
2213 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2215 pointer_fc = get_pointer_fc_context(type, attrs, context);
2217 ref = type_pointer_get_ref_type(type);
2218 if (type_get_type(ref) == TYPE_ENUM)
2219 fc = get_enum_fc(ref);
2220 else
2221 fc = get_basic_fc(ref);
2223 if (!is_interpreted_func(current_iface, current_func))
2225 if (out_attr && !in_attr && pointer_fc == FC_RP)
2226 flags |= FC_ALLOCED_ON_STACK;
2228 else if (get_stub_mode() == MODE_Oif)
2230 if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
2231 flags |= FC_ALLOCED_ON_STACK;
2234 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2235 pointer_fc, flags, string_of_type(pointer_fc),
2236 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2237 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2238 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2239 return 4;
2242 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2244 const decl_spec_t ds = {.type = t};
2245 print_file(file, 0, "/* %u (", tfsoff);
2246 write_type_decl(file, &ds, NULL);
2247 print_file(file, 0, ") */\n");
2250 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2251 type_t *type, unsigned int ref_offset,
2252 enum type_context context,
2253 unsigned int *typestring_offset)
2255 unsigned int offset = *typestring_offset;
2256 type_t *ref = type_pointer_get_ref_type(type);
2258 print_start_tfs_comment(file, type, offset);
2259 update_tfsoff(type, offset, file);
2261 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2263 case TGT_BASIC:
2264 case TGT_ENUM:
2265 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2266 break;
2267 default:
2268 if (ref_offset)
2269 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2270 break;
2273 return offset;
2276 static int processed(const type_t *type)
2278 return type->typestring_offset && !type->tfswrite;
2281 static int user_type_has_variable_size(const type_t *t)
2283 if (is_ptr(t))
2284 return TRUE;
2285 else if (type_get_type(t) == TYPE_STRUCT)
2287 switch (get_struct_fc(t))
2289 case FC_PSTRUCT:
2290 case FC_CSTRUCT:
2291 case FC_CPSTRUCT:
2292 case FC_CVSTRUCT:
2293 return TRUE;
2296 /* Note: Since this only applies to user types, we can't have a conformant
2297 array here, and strings should get filed under pointer in this case. */
2298 return FALSE;
2301 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2303 unsigned int start, absoff, flags;
2304 const char *name = NULL;
2305 type_t *utype = get_user_type(type, &name);
2306 unsigned int usize = type_memsize(utype);
2307 unsigned int ualign = type_buffer_alignment(utype);
2308 unsigned int size = type_memsize(type);
2309 unsigned short funoff = user_type_offset(name);
2310 short reloff;
2312 if (processed(type)) return type->typestring_offset;
2314 guard_rec(type);
2316 if(user_type_has_variable_size(utype)) usize = 0;
2318 if (type_get_type(utype) == TYPE_BASIC ||
2319 type_get_type(utype) == TYPE_ENUM)
2321 unsigned char fc;
2323 if (type_get_type(utype) == TYPE_ENUM)
2324 fc = get_enum_fc(utype);
2325 else
2326 fc = get_basic_fc(utype);
2328 absoff = *tfsoff;
2329 print_start_tfs_comment(file, utype, absoff);
2330 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2331 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2332 *tfsoff += 2;
2334 else
2336 if (!processed(utype))
2337 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2338 absoff = utype->typestring_offset;
2341 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2342 flags = 0x40;
2343 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2344 flags = 0x80;
2345 else
2346 flags = 0;
2348 start = *tfsoff;
2349 update_tfsoff(type, start, file);
2350 print_start_tfs_comment(file, type, start);
2351 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2352 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2353 flags | (ualign - 1), ualign - 1, flags);
2354 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2355 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2356 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2357 *tfsoff += 8;
2358 reloff = absoff - *tfsoff;
2359 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2360 *tfsoff += 2;
2361 return start;
2364 static void write_member_type(FILE *file, const type_t *cont,
2365 int cont_is_complex, const attr_list_t *attrs,
2366 const type_t *type, unsigned int *corroff,
2367 unsigned int *tfsoff)
2369 if (is_embedded_complex(type) && !is_conformant_array(type))
2371 unsigned int absoff;
2372 short reloff;
2374 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2376 absoff = *corroff;
2377 *corroff += 8;
2379 else
2381 absoff = type->typestring_offset;
2383 reloff = absoff - (*tfsoff + 2);
2385 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2386 /* padding is represented using FC_STRUCTPAD* types, so presumably
2387 * this is left over in the format for historical purposes in MIDL
2388 * or rpcrt4. */
2389 print_file(file, 2, "0x0,\n");
2390 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2391 reloff, reloff, absoff);
2392 *tfsoff += 4;
2394 else if (is_ptr(type) || is_conformant_array(type))
2396 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2397 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2398 *tfsoff += 1;
2400 else if (!write_base_type(file, type, tfsoff))
2401 error("Unsupported member type %d\n", type_get_type(type));
2404 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2405 int cont_is_complex, unsigned int *tfsoff)
2407 type_t *elem = type_array_get_element_type(type);
2409 if (!is_embedded_complex(elem) && is_ptr(elem))
2411 type_t *ref = type_pointer_get_ref_type(elem);
2413 if (processed(ref))
2415 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2416 ref->typestring_offset, tfsoff);
2417 return;
2419 if (cont_is_complex && is_string_type(attrs, elem))
2421 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2422 return;
2424 if (!is_string_type(NULL, elem) &&
2425 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2427 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2428 return;
2431 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2434 static void write_end(FILE *file, unsigned int *tfsoff)
2436 if (*tfsoff % 2 == 0)
2438 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2439 *tfsoff += 1;
2441 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2442 *tfsoff += 1;
2445 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2447 unsigned int offset = 0;
2448 var_list_t *fs = type_struct_get_fields(type);
2449 var_t *f;
2451 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2453 type_t *ft = f->declspec.type;
2454 unsigned int size = field_memsize( ft, &offset );
2455 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2457 short reloff;
2458 unsigned int absoff = ft->typestring_offset;
2459 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2460 absoff += 8; /* we already have a corr descr, skip it */
2461 reloff = absoff - (*tfsoff + 6);
2462 print_file(file, 0, "/* %d */\n", *tfsoff);
2463 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2464 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2465 write_conf_or_var_desc(file, current_structure, offset, ft,
2466 get_attrp(f->attrs, ATTR_SWITCHIS));
2467 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2468 (unsigned short)reloff, reloff, absoff);
2469 *tfsoff += 8;
2471 offset += size;
2475 static int write_pointer_description_offsets(
2476 FILE *file, const attr_list_t *attrs, type_t *type,
2477 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2478 unsigned int *typestring_offset)
2480 int written = 0;
2482 if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
2483 (is_array(type) && type_array_is_decl_as_ptr(type)))
2485 if (offset_in_memory && offset_in_buffer)
2487 unsigned int memsize;
2489 /* pointer instance
2491 * note that MSDN states that for pointer layouts in structures,
2492 * this is a negative offset from the end of the structure, but
2493 * this statement is incorrect. all offsets are positive */
2494 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2495 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2497 memsize = type_memsize(type);
2498 *offset_in_memory += memsize;
2499 /* increment these separately as in the case of conformant (varying)
2500 * structures these start at different values */
2501 *offset_in_buffer += memsize;
2503 *typestring_offset += 4;
2505 if (is_ptr(type))
2507 type_t *ref = type_pointer_get_ref_type(type);
2509 if (is_string_type(attrs, type))
2510 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2511 else if (processed(ref))
2512 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2513 ref->typestring_offset, typestring_offset);
2514 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2515 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2516 else
2517 error("write_pointer_description_offsets: type format string unknown\n");
2519 else
2521 unsigned int offset = type->typestring_offset;
2522 /* skip over the pointer that is written for strings, since a
2523 * pointer has to be written in-place here */
2524 if (is_string_type(attrs, type))
2525 offset += 4;
2526 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2529 return 1;
2532 if (is_array(type))
2534 return write_pointer_description_offsets(
2535 file, attrs, type_array_get_element_type(type), offset_in_memory,
2536 offset_in_buffer, typestring_offset);
2538 else if (is_non_complex_struct(type))
2540 /* otherwise search for interesting fields to parse */
2541 const var_t *v;
2542 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2544 if (offset_in_memory && offset_in_buffer)
2546 unsigned int padding;
2547 unsigned int align = 0;
2548 type_memsize_and_alignment(v->declspec.type, &align);
2549 padding = ROUNDING(*offset_in_memory, align);
2550 *offset_in_memory += padding;
2551 *offset_in_buffer += padding;
2553 written += write_pointer_description_offsets(
2554 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2555 typestring_offset);
2558 else
2560 if (offset_in_memory && offset_in_buffer)
2562 unsigned int memsize = type_memsize(type);
2563 *offset_in_memory += memsize;
2564 /* increment these separately as in the case of conformant (varying)
2565 * structures these start at different values */
2566 *offset_in_buffer += memsize;
2570 return written;
2573 static int write_no_repeat_pointer_descriptions(
2574 FILE *file, const attr_list_t *attrs, type_t *type,
2575 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2576 unsigned int *typestring_offset)
2578 int written = 0;
2580 if (is_ptr(type) ||
2581 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2583 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2584 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2585 *typestring_offset += 2;
2587 return write_pointer_description_offsets(file, attrs, type,
2588 offset_in_memory, offset_in_buffer, typestring_offset);
2591 if (is_non_complex_struct(type))
2593 const var_t *v;
2594 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2596 if (offset_in_memory && offset_in_buffer)
2598 unsigned int padding;
2599 unsigned int align = 0;
2600 type_memsize_and_alignment(v->declspec.type, &align);
2601 padding = ROUNDING(*offset_in_memory, align);
2602 *offset_in_memory += padding;
2603 *offset_in_buffer += padding;
2605 written += write_no_repeat_pointer_descriptions(
2606 file, v->attrs, v->declspec.type,
2607 offset_in_memory, offset_in_buffer, typestring_offset);
2610 else
2612 unsigned int memsize = type_memsize(type);
2613 *offset_in_memory += memsize;
2614 /* increment these separately as in the case of conformant (varying)
2615 * structures these start at different values */
2616 *offset_in_buffer += memsize;
2619 return written;
2622 /* Note: if file is NULL return value is number of pointers to write, else
2623 * it is the number of type format characters written */
2624 static int write_fixed_array_pointer_descriptions(
2625 FILE *file, const attr_list_t *attrs, type_t *type,
2626 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2627 unsigned int *typestring_offset)
2629 int pointer_count = 0;
2631 if (type_get_type(type) == TYPE_ARRAY &&
2632 !type_array_has_conformance(type) && !type_array_has_variance(type))
2634 unsigned int temp = 0;
2635 /* unfortunately, this needs to be done in two passes to avoid
2636 * writing out redundant FC_FIXED_REPEAT descriptions */
2637 pointer_count = write_pointer_description_offsets(
2638 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2639 if (pointer_count > 0)
2641 unsigned int increment_size;
2642 unsigned int offset_of_array_pointer_mem = 0;
2643 unsigned int offset_of_array_pointer_buf = 0;
2645 increment_size = type_memsize(type_array_get_element_type(type));
2647 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2648 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2649 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2650 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2651 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2652 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2653 *typestring_offset += 10;
2655 pointer_count = write_pointer_description_offsets(
2656 file, attrs, type, &offset_of_array_pointer_mem,
2657 &offset_of_array_pointer_buf, typestring_offset);
2660 else if (type_get_type(type) == TYPE_STRUCT)
2662 const var_t *v;
2663 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2665 if (offset_in_memory && offset_in_buffer)
2667 unsigned int padding;
2668 unsigned int align = 0;
2669 type_memsize_and_alignment(v->declspec.type, &align);
2670 padding = ROUNDING(*offset_in_memory, align);
2671 *offset_in_memory += padding;
2672 *offset_in_buffer += padding;
2674 pointer_count += write_fixed_array_pointer_descriptions(
2675 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2676 typestring_offset);
2679 else
2681 if (offset_in_memory && offset_in_buffer)
2683 unsigned int memsize;
2684 memsize = type_memsize(type);
2685 *offset_in_memory += memsize;
2686 /* increment these separately as in the case of conformant (varying)
2687 * structures these start at different values */
2688 *offset_in_buffer += memsize;
2692 return pointer_count;
2695 /* Note: if file is NULL return value is number of pointers to write, else
2696 * it is the number of type format characters written */
2697 static int write_conformant_array_pointer_descriptions(
2698 FILE *file, const attr_list_t *attrs, type_t *type,
2699 unsigned int offset_in_memory, unsigned int *typestring_offset)
2701 int pointer_count = 0;
2703 if (is_conformant_array(type) && !type_array_has_variance(type))
2705 unsigned int temp = 0;
2706 /* unfortunately, this needs to be done in two passes to avoid
2707 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2708 pointer_count = write_pointer_description_offsets(
2709 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2710 if (pointer_count > 0)
2712 unsigned int increment_size;
2713 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2714 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2716 increment_size = type_memsize(type_array_get_element_type(type));
2718 if (increment_size > USHRT_MAX)
2719 error("array size of %u bytes is too large\n", increment_size);
2721 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2722 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2723 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2724 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2725 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2726 *typestring_offset += 8;
2728 pointer_count = write_pointer_description_offsets(
2729 file, attrs, type_array_get_element_type(type),
2730 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2731 typestring_offset);
2735 return pointer_count;
2738 /* Note: if file is NULL return value is number of pointers to write, else
2739 * it is the number of type format characters written */
2740 static int write_varying_array_pointer_descriptions(
2741 FILE *file, const attr_list_t *attrs, type_t *type,
2742 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2743 unsigned int *typestring_offset)
2745 int pointer_count = 0;
2747 if (is_array(type) && type_array_has_variance(type))
2749 unsigned int temp = 0;
2750 /* unfortunately, this needs to be done in two passes to avoid
2751 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2752 pointer_count = write_pointer_description_offsets(
2753 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2754 if (pointer_count > 0)
2756 unsigned int increment_size;
2758 increment_size = type_memsize(type_array_get_element_type(type));
2760 if (increment_size > USHRT_MAX)
2761 error("array size of %u bytes is too large\n", increment_size);
2763 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2764 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2765 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2766 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2767 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2768 *typestring_offset += 8;
2770 pointer_count = write_pointer_description_offsets(
2771 file, attrs, type_array_get_element_type(type), offset_in_memory,
2772 offset_in_buffer, typestring_offset);
2775 else if (type_get_type(type) == TYPE_STRUCT)
2777 const var_t *v;
2778 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2780 if (offset_in_memory && offset_in_buffer)
2782 unsigned int align = 0, padding;
2784 if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
2786 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2787 /* skip over variance and offset in buffer */
2788 *offset_in_buffer += 8;
2791 type_memsize_and_alignment(v->declspec.type, &align);
2792 padding = ROUNDING(*offset_in_memory, align);
2793 *offset_in_memory += padding;
2794 *offset_in_buffer += padding;
2796 pointer_count += write_varying_array_pointer_descriptions(
2797 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2798 typestring_offset);
2801 else
2803 if (offset_in_memory && offset_in_buffer)
2805 unsigned int memsize = type_memsize(type);
2806 *offset_in_memory += memsize;
2807 /* increment these separately as in the case of conformant (varying)
2808 * structures these start at different values */
2809 *offset_in_buffer += memsize;
2813 return pointer_count;
2816 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2817 unsigned int *typestring_offset)
2819 unsigned int offset_in_buffer;
2820 unsigned int offset_in_memory;
2822 /* pass 1: search for single instance of a pointer (i.e. don't descend
2823 * into arrays) */
2824 if (!is_array(type))
2826 offset_in_memory = 0;
2827 offset_in_buffer = 0;
2828 write_no_repeat_pointer_descriptions(
2829 file, NULL, type,
2830 &offset_in_memory, &offset_in_buffer, typestring_offset);
2833 /* pass 2: search for pointers in fixed arrays */
2834 offset_in_memory = 0;
2835 offset_in_buffer = 0;
2836 write_fixed_array_pointer_descriptions(
2837 file, NULL, type,
2838 &offset_in_memory, &offset_in_buffer, typestring_offset);
2840 /* pass 3: search for pointers in conformant only arrays (but don't descend
2841 * into conformant varying or varying arrays) */
2842 if (is_conformant_array(type) &&
2843 (type_array_is_decl_as_ptr(type) || !current_structure))
2844 write_conformant_array_pointer_descriptions(
2845 file, attrs, type, 0, typestring_offset);
2846 else if (type_get_type(type) == TYPE_STRUCT &&
2847 get_struct_fc(type) == FC_CPSTRUCT)
2849 type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
2850 write_conformant_array_pointer_descriptions( file, NULL, carray,
2851 type_memsize(type), typestring_offset);
2854 /* pass 4: search for pointers in varying arrays */
2855 offset_in_memory = 0;
2856 offset_in_buffer = 0;
2857 write_varying_array_pointer_descriptions(
2858 file, NULL, type,
2859 &offset_in_memory, &offset_in_buffer, typestring_offset);
2862 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2863 type_t *type, enum type_context context,
2864 const char *name, unsigned int *typestring_offset)
2866 unsigned int start_offset;
2867 unsigned char rtype;
2868 type_t *elem_type;
2869 int is_processed = processed(type);
2871 start_offset = *typestring_offset;
2873 if (is_declptr(type))
2875 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2876 int pointer_type = get_pointer_fc_context(type, attrs, context);
2877 if (!pointer_type)
2878 pointer_type = FC_RP;
2879 print_start_tfs_comment(file, type, *typestring_offset);
2880 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2881 pointer_type, flag, string_of_type(pointer_type),
2882 flag ? " [simple_pointer]" : "");
2883 *typestring_offset += 2;
2884 if (!flag)
2886 print_file(file, 2, "NdrFcShort(0x2),\n");
2887 *typestring_offset += 2;
2889 is_processed = FALSE;
2892 if (is_array(type))
2893 elem_type = type_array_get_element_type(type);
2894 else
2895 elem_type = type_pointer_get_ref_type(type);
2897 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2898 return write_array_tfs(file, attrs, type, name, typestring_offset);
2900 if (type_get_type(elem_type) != TYPE_BASIC)
2902 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2903 return start_offset;
2906 rtype = get_basic_fc(elem_type);
2907 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2909 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2910 return start_offset;
2913 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2915 unsigned int dim = type_array_get_dim(type);
2917 if (is_processed) return start_offset;
2919 /* FIXME: multi-dimensional array */
2920 if (0xffffu < dim)
2921 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2922 name, 0xffffu, dim - 0xffffu);
2924 if (rtype == FC_WCHAR)
2925 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2926 else
2927 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2928 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2929 *typestring_offset += 2;
2931 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2932 *typestring_offset += 2;
2934 update_tfsoff(type, start_offset, file);
2935 return start_offset;
2937 else if (is_conformant_array(type))
2939 if (rtype == FC_WCHAR)
2940 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2941 else
2942 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2943 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2944 *typestring_offset += 2;
2946 *typestring_offset += write_conf_or_var_desc(
2947 file, current_structure,
2948 (!type_array_is_decl_as_ptr(type) && current_structure
2949 ? type_memsize(current_structure)
2950 : 0),
2951 type, type_array_get_conformance(type));
2953 update_tfsoff(type, start_offset, file);
2954 return start_offset;
2956 else
2958 if (is_processed) return start_offset;
2960 if (rtype == FC_WCHAR)
2961 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2962 else
2963 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2964 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2965 *typestring_offset += 2;
2967 update_tfsoff(type, start_offset, file);
2968 return start_offset;
2972 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2973 const char *name, unsigned int *typestring_offset)
2975 const expr_t *length_is = type_array_get_variance(type);
2976 const expr_t *size_is = type_array_get_conformance(type);
2977 unsigned int align;
2978 unsigned int size;
2979 unsigned int start_offset;
2980 unsigned char fc;
2981 unsigned int baseoff
2982 = !type_array_is_decl_as_ptr(type) && current_structure
2983 ? type_memsize(current_structure)
2984 : 0;
2986 if (!is_string_type(attrs, type_array_get_element_type(type)))
2987 write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
2989 size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2990 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2991 fc = get_array_fc(type);
2993 start_offset = *typestring_offset;
2994 update_tfsoff(type, start_offset, file);
2995 print_start_tfs_comment(file, type, start_offset);
2996 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2997 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2998 *typestring_offset += 2;
3000 align = 0;
3001 if (fc != FC_BOGUS_ARRAY)
3003 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
3005 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
3006 *typestring_offset += 4;
3008 else
3010 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
3011 *typestring_offset += 2;
3014 if (is_conformant_array(type))
3015 *typestring_offset
3016 += write_conf_or_var_desc(file, current_structure, baseoff,
3017 type, size_is);
3019 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
3021 unsigned int elsize = type_memsize(type_array_get_element_type(type));
3022 unsigned int dim = type_array_get_dim(type);
3024 if (fc == FC_LGVARRAY)
3026 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
3027 *typestring_offset += 4;
3029 else
3031 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3032 *typestring_offset += 2;
3035 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
3036 *typestring_offset += 2;
3039 if (length_is)
3040 *typestring_offset
3041 += write_conf_or_var_desc(file, current_structure, baseoff,
3042 type, length_is);
3044 if (type_has_pointers(type_array_get_element_type(type)) &&
3045 (type_array_is_decl_as_ptr(type) || !current_structure))
3047 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3048 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3049 *typestring_offset += 2;
3050 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
3051 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3052 *typestring_offset += 1;
3055 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
3056 write_end(file, typestring_offset);
3058 else
3060 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
3061 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3062 *typestring_offset += 2;
3063 *typestring_offset
3064 += write_conf_or_var_desc(file, current_structure, baseoff,
3065 type, size_is);
3066 *typestring_offset
3067 += write_conf_or_var_desc(file, current_structure, baseoff,
3068 type, length_is);
3070 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
3071 write_end(file, typestring_offset);
3074 return start_offset;
3077 static const var_t *find_array_or_string_in_struct(const type_t *type)
3079 const var_list_t *fields = type_struct_get_fields(type);
3080 const var_t *last_field;
3081 const type_t *ft;
3083 if (!fields || list_empty(fields))
3084 return NULL;
3086 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3087 ft = last_field->declspec.type;
3089 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3090 return last_field;
3092 if (type_get_type(ft) == TYPE_STRUCT)
3093 return find_array_or_string_in_struct(ft);
3094 else
3095 return NULL;
3098 static void write_struct_members(FILE *file, const type_t *type,
3099 int is_complex, unsigned int *corroff,
3100 unsigned int *typestring_offset)
3102 const var_t *field;
3103 unsigned short offset = 0;
3104 unsigned int salign = 1;
3105 int padding;
3106 var_list_t *fields = type_struct_get_fields(type);
3108 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3110 type_t *ft = field->declspec.type;
3111 unsigned int align = 0;
3112 unsigned int size = type_memsize_and_alignment(ft, &align);
3113 align = clamp_align(align);
3114 if (salign < align) salign = align;
3116 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3118 if ((align - 1) & offset)
3120 unsigned char fc = 0;
3121 switch (align)
3123 case 2:
3124 fc = FC_ALIGNM2;
3125 break;
3126 case 4:
3127 fc = FC_ALIGNM4;
3128 break;
3129 case 8:
3130 fc = FC_ALIGNM8;
3131 break;
3132 default:
3133 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3135 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3136 offset = ROUND_SIZE(offset, align);
3137 *typestring_offset += 1;
3139 write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
3140 typestring_offset);
3141 offset += size;
3145 padding = ROUNDING(offset, salign);
3146 if (padding)
3148 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3149 FC_STRUCTPAD1 + padding - 1,
3150 padding);
3151 *typestring_offset += 1;
3154 write_end(file, typestring_offset);
3157 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3158 const char *name, unsigned int *tfsoff)
3160 const type_t *save_current_structure = current_structure;
3161 unsigned int total_size;
3162 const var_t *array;
3163 unsigned int start_offset;
3164 unsigned int align;
3165 unsigned int corroff;
3166 var_t *f;
3167 unsigned char fc = get_struct_fc(type);
3168 var_list_t *fields = type_struct_get_fields(type);
3170 if (processed(type)) return type->typestring_offset;
3172 guard_rec(type);
3173 current_structure = type;
3175 total_size = type_memsize(type);
3176 align = type_buffer_alignment(type);
3177 if (total_size > USHRT_MAX)
3178 error("structure size for %s exceeds %d bytes by %d bytes\n",
3179 name, USHRT_MAX, total_size - USHRT_MAX);
3181 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3182 write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
3184 array = find_array_or_string_in_struct(type);
3185 if (array && !processed(array->declspec.type))
3187 if(is_string_type(array->attrs, array->declspec.type))
3188 write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3189 else
3190 write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
3193 corroff = *tfsoff;
3194 write_descriptors(file, type, tfsoff);
3196 start_offset = *tfsoff;
3197 update_tfsoff(type, start_offset, file);
3198 print_start_tfs_comment(file, type, start_offset);
3199 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3200 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3201 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3202 *tfsoff += 4;
3204 if (array)
3206 unsigned int absoff = array->declspec.type->typestring_offset;
3207 short reloff = absoff - *tfsoff;
3208 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3209 reloff, reloff, absoff);
3210 *tfsoff += 2;
3212 else if (fc == FC_BOGUS_STRUCT)
3214 print_file(file, 2, "NdrFcShort(0x0),\n");
3215 *tfsoff += 2;
3218 if (fc == FC_BOGUS_STRUCT)
3220 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3221 nothing is written to file yet. On the actual writing pass,
3222 this will have been updated. */
3223 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3224 int reloff = absoff - *tfsoff;
3225 assert( reloff >= 0 );
3226 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3227 (unsigned short)reloff, reloff, absoff);
3228 *tfsoff += 2;
3230 else if ((fc == FC_PSTRUCT) ||
3231 (fc == FC_CPSTRUCT) ||
3232 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3234 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3235 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3236 *tfsoff += 2;
3237 write_pointer_description(file, NULL, type, tfsoff);
3238 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3239 *tfsoff += 1;
3242 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3243 tfsoff);
3245 if (fc == FC_BOGUS_STRUCT)
3247 const var_t *f;
3249 type->ptrdesc = *tfsoff;
3250 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3252 type_t *ft = f->declspec.type;
3253 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3255 case TGT_POINTER:
3256 if (is_string_type(f->attrs, ft))
3257 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3258 else
3259 write_pointer_tfs(file, f->attrs, ft,
3260 type_pointer_get_ref_type(ft)->typestring_offset,
3261 TYPE_CONTEXT_CONTAINER, tfsoff);
3262 break;
3263 case TGT_ARRAY:
3264 if (type_array_is_decl_as_ptr(ft))
3266 unsigned int offset;
3268 print_file(file, 0, "/* %d */\n", *tfsoff);
3270 offset = ft->typestring_offset;
3271 /* skip over the pointer that is written for strings, since a
3272 * pointer has to be written in-place here */
3273 if (is_string_type(f->attrs, ft))
3274 offset += 4;
3275 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3277 break;
3278 default:
3279 break;
3282 if (type->ptrdesc == *tfsoff)
3283 type->ptrdesc = 0;
3286 current_structure = save_current_structure;
3287 return start_offset;
3290 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3292 if (t == NULL)
3294 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3296 else
3298 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3300 unsigned char fc;
3301 if (type_get_type(t) == TYPE_BASIC)
3302 fc = get_basic_fc(t);
3303 else
3304 fc = get_enum_fc(t);
3305 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3306 fc, string_of_type(fc));
3308 else if (t->typestring_offset)
3310 short reloff = t->typestring_offset - *tfsoff;
3311 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3312 reloff, reloff, t->typestring_offset);
3314 else
3315 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3318 *tfsoff += 2;
3321 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3322 type_t *type, unsigned int *tfsoff)
3324 unsigned int start_offset;
3325 unsigned int size;
3326 var_list_t *fields;
3327 unsigned int nbranch = 0;
3328 type_t *deftype = NULL;
3329 short nodeftype = 0xffff;
3330 unsigned int dummy;
3331 var_t *f;
3333 if (processed(type) &&
3334 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3335 return type->typestring_offset;
3337 guard_rec(type);
3339 fields = type_union_get_cases(type);
3341 size = union_memsize(fields, &dummy);
3343 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3345 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3346 if (cases)
3347 nbranch += list_count(cases);
3348 if (f->declspec.type)
3349 write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
3352 start_offset = *tfsoff;
3353 update_tfsoff(type, start_offset, file);
3354 print_start_tfs_comment(file, type, start_offset);
3355 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3357 const var_t *sv = type_union_get_switch_value(type);
3358 const type_t *st = sv->declspec.type;
3359 unsigned int align = 0;
3360 unsigned char fc;
3362 if (type_get_type(st) == TYPE_BASIC)
3364 fc = get_basic_fc(st);
3365 switch (fc)
3367 case FC_CHAR:
3368 case FC_SMALL:
3369 case FC_BYTE:
3370 case FC_USMALL:
3371 case FC_WCHAR:
3372 case FC_SHORT:
3373 case FC_USHORT:
3374 case FC_LONG:
3375 case FC_ULONG:
3376 break;
3377 default:
3378 fc = 0;
3379 error("union switch type must be an integer, char, or enum\n");
3382 else if (type_get_type(st) == TYPE_ENUM)
3383 fc = get_enum_fc(st);
3384 else
3385 error("union switch type must be an integer, char, or enum\n");
3387 type_memsize_and_alignment(st, &align);
3388 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3390 if (f->declspec.type)
3391 type_memsize_and_alignment(f->declspec.type, &align);
3394 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3395 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3396 (align << 4) | fc, string_of_type(fc));
3397 *tfsoff += 2;
3399 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3401 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3402 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3403 unsigned char fc;
3405 if (type_get_type(st) == TYPE_BASIC)
3407 fc = get_basic_fc(st);
3408 switch (fc)
3410 case FC_CHAR:
3411 case FC_SMALL:
3412 case FC_USMALL:
3413 case FC_SHORT:
3414 case FC_USHORT:
3415 case FC_LONG:
3416 case FC_ULONG:
3417 case FC_ENUM16:
3418 case FC_ENUM32:
3419 break;
3420 default:
3421 fc = 0;
3422 error("union switch type must be an integer, char, or enum\n");
3425 else if (type_get_type(st) == TYPE_ENUM)
3426 fc = get_enum_fc(st);
3427 else
3428 error("union switch type must be an integer, char, or enum\n");
3430 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3431 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3432 fc, string_of_type(fc));
3433 *tfsoff += 2;
3434 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3435 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3436 *tfsoff += 2;
3437 print_file(file, 0, "/* %u */\n", *tfsoff);
3440 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3441 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3442 *tfsoff += 4;
3444 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3446 type_t *ft = f->declspec.type;
3447 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3448 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3449 expr_t *c;
3451 if (cases == NULL && !deflt)
3452 error("union field %s with neither case nor default attribute\n", f->name);
3454 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3456 /* MIDL doesn't check for duplicate cases, even though that seems
3457 like a reasonable thing to do, it just dumps them to the TFS
3458 like we're going to do here. */
3459 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3460 *tfsoff += 4;
3461 write_branch_type(file, ft, tfsoff);
3464 /* MIDL allows multiple default branches, even though that seems
3465 illogical, it just chooses the last one, which is what we will
3466 do. */
3467 if (deflt)
3469 deftype = ft;
3470 nodeftype = 0;
3474 if (deftype)
3476 write_branch_type(file, deftype, tfsoff);
3478 else
3480 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3481 *tfsoff += 2;
3484 return start_offset;
3487 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3488 unsigned int *typeformat_offset)
3490 unsigned int i;
3491 unsigned int start_offset = *typeformat_offset;
3492 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3494 if (!iid && processed(type)) return type->typestring_offset;
3496 print_start_tfs_comment(file, type, start_offset);
3497 update_tfsoff(type, start_offset, file);
3499 if (iid)
3501 print_file(file, 2, "0x2f, /* FC_IP */\n");
3502 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3503 *typeformat_offset
3504 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3506 else
3508 const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
3509 const struct uuid *uuid = get_attrp(base->attrs, ATTR_UUID);
3511 if (! uuid)
3512 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3514 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3515 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3516 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3517 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3518 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3519 for (i = 0; i < 8; ++i)
3520 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3522 if (file)
3523 fprintf(file, "\n");
3525 *typeformat_offset += 18;
3527 return start_offset;
3530 static unsigned int write_contexthandle_tfs(FILE *file,
3531 const attr_list_t *attrs,
3532 type_t *type,
3533 enum type_context context,
3534 unsigned int *typeformat_offset)
3536 unsigned int start_offset = *typeformat_offset;
3537 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
3539 print_start_tfs_comment(file, type, start_offset);
3541 if (flags & 0x80) /* via ptr */
3543 int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
3544 if (!pointer_type) pointer_type = FC_RP;
3545 *typeformat_offset += 4;
3546 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3547 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3548 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3551 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3552 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3553 if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
3554 print_file(file, 0, "can't be null, ");
3555 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3556 print_file(file, 0, "serialize, ");
3557 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3558 print_file(file, 0, "no serialize, ");
3559 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3560 print_file(file, 0, "strict, ");
3561 if (flags & HANDLE_PARAM_IS_RETURN)
3562 print_file(file, 0, "return, ");
3563 if (flags & HANDLE_PARAM_IS_OUT)
3564 print_file(file, 0, "out, ");
3565 if (flags & HANDLE_PARAM_IS_IN)
3566 print_file(file, 0, "in, ");
3567 if (flags & HANDLE_PARAM_IS_VIA_PTR)
3568 print_file(file, 0, "via ptr, ");
3569 print_file(file, 0, "*/\n");
3570 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3571 print_file(file, 2, "0, /* FIXME: param num */\n");
3572 *typeformat_offset += 4;
3574 update_tfsoff( type, start_offset, file );
3575 return start_offset;
3578 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3579 type_t *type, expr_list_t *range_list,
3580 unsigned int *typeformat_offset)
3582 unsigned char fc;
3583 unsigned int start_offset = *typeformat_offset;
3584 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3585 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3587 if (type_get_type(type) == TYPE_BASIC)
3588 fc = get_basic_fc(type);
3589 else
3590 fc = get_enum_fc(type);
3592 /* fc must fit in lower 4-bits of 8-bit field below */
3593 assert(fc <= 0xf);
3595 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3596 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3597 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3598 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3599 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3600 update_tfsoff( type, start_offset, file );
3601 *typeformat_offset += 10;
3603 return start_offset;
3606 static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
3607 type_t *type, const char *name,
3608 enum type_context context,
3609 unsigned int *typeformat_offset)
3611 unsigned int offset;
3613 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3615 case TGT_CTXT_HANDLE:
3616 case TGT_CTXT_HANDLE_POINTER:
3617 return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
3618 case TGT_USER_TYPE:
3619 return write_user_tfs(file, type, typeformat_offset);
3620 case TGT_STRING:
3621 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3622 case TGT_ARRAY:
3624 unsigned int off;
3625 /* conformant and pointer arrays are handled specially */
3626 if ((context != TYPE_CONTEXT_CONTAINER &&
3627 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3628 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3629 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3630 else
3631 off = 0;
3632 if (context != TYPE_CONTEXT_CONTAINER &&
3633 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3635 int ptr_type;
3636 ptr_type = get_pointer_fc_context(type, attrs, context);
3637 if (type_array_is_decl_as_ptr(type)
3638 || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
3640 unsigned int absoff = type->typestring_offset;
3641 short reloff = absoff - (*typeformat_offset + 2);
3642 off = *typeformat_offset;
3643 print_file(file, 0, "/* %d */\n", off);
3644 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3645 string_of_type(ptr_type));
3646 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3647 reloff, reloff, absoff);
3648 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3649 *typeformat_offset += 4;
3651 type_array_set_ptr_tfsoff(type, off);
3653 return off;
3655 case TGT_STRUCT:
3656 return write_struct_tfs(file, type, name, typeformat_offset);
3657 case TGT_UNION:
3658 return write_union_tfs(file, attrs, type, typeformat_offset);
3659 case TGT_ENUM:
3660 case TGT_BASIC:
3661 /* nothing to do */
3662 return 0;
3663 case TGT_RANGE:
3665 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3666 if (!range_list)
3667 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3668 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3670 case TGT_IFACE_POINTER:
3671 return write_ip_tfs(file, attrs, type, typeformat_offset);
3672 case TGT_POINTER:
3674 enum type_context ref_context;
3675 type_t *ref = type_pointer_get_ref_type(type);
3677 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3678 ref_context = TYPE_CONTEXT_PARAM;
3679 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3680 ref_context = TYPE_CONTEXT_CONTAINER;
3681 else
3682 ref_context = context;
3684 if (is_string_type(attrs, ref))
3686 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3687 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3689 offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
3690 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3691 return 0;
3692 return offset;
3695 offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
3696 ref_context, typeformat_offset);
3697 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3698 return 0;
3699 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3701 case TGT_INVALID:
3702 break;
3704 error("invalid type %s for var %s\n", type->name, name);
3705 return 0;
3708 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3709 const char *name, int write_ptr, unsigned int *tfsoff)
3711 return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3714 static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
3716 const statement_list_t *stmts = type_iface_get_stmts(iface);
3717 const statement_t *stmt;
3718 var_t *var;
3720 current_iface = iface;
3721 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
3723 switch(stmt->type)
3725 case STMT_DECLARATION:
3727 const var_t *func = stmt->u.var;
3729 if(stmt->u.var->declspec.stgclass != STG_NONE
3730 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
3731 continue;
3733 current_func = func;
3734 if (is_local(func->attrs)) continue;
3736 var = type_function_get_retval(func->declspec.type);
3737 if (!is_void(var->declspec.type))
3738 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
3739 TYPE_CONTEXT_RETVAL, offset);
3741 if (type_function_get_args(func->declspec.type))
3742 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
3743 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
3744 TYPE_CONTEXT_TOPLEVELPARAM, offset );
3745 break;
3748 case STMT_TYPEDEF:
3750 typeref_t *ref;
3751 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
3753 if (is_attr(ref->type->attrs, ATTR_ENCODE)
3754 || is_attr(ref->type->attrs, ATTR_DECODE))
3755 ref->type->typestring_offset = write_type_tfs( file,
3756 ref->type->attrs, ref->type, ref->type->name,
3757 TYPE_CONTEXT_CONTAINER, offset);
3759 break;
3761 default:
3762 break;
3767 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3769 unsigned int typeformat_offset = 2;
3770 for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
3771 return typeformat_offset + 1;
3775 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3777 int indent = 0;
3779 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3780 print_file(file, indent, "{\n");
3781 indent++;
3782 print_file(file, indent, "0,\n");
3783 print_file(file, indent, "{\n");
3784 indent++;
3785 print_file(file, indent, "NdrFcShort(0x0),\n");
3787 set_all_tfswrite(TRUE);
3788 process_tfs(file, stmts, pred);
3790 print_file(file, indent, "0x0\n");
3791 indent--;
3792 print_file(file, indent, "}\n");
3793 indent--;
3794 print_file(file, indent, "};\n");
3795 print_file(file, indent, "\n");
3798 static unsigned int get_required_buffer_size_type(
3799 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3801 *alignment = 0;
3802 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3804 case TGT_USER_TYPE:
3806 const char *uname = NULL;
3807 const type_t *utype = get_user_type(type, &uname);
3808 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3810 case TGT_BASIC:
3811 switch (get_basic_fc(type))
3813 case FC_BYTE:
3814 case FC_CHAR:
3815 case FC_USMALL:
3816 case FC_SMALL:
3817 *alignment = 4;
3818 return 1;
3820 case FC_WCHAR:
3821 case FC_USHORT:
3822 case FC_SHORT:
3823 *alignment = 4;
3824 return 2;
3826 case FC_ULONG:
3827 case FC_LONG:
3828 case FC_FLOAT:
3829 case FC_ERROR_STATUS_T:
3830 *alignment = 4;
3831 return 4;
3833 case FC_HYPER:
3834 case FC_DOUBLE:
3835 *alignment = 8;
3836 return 8;
3838 case FC_INT3264:
3839 case FC_UINT3264:
3840 assert( pointer_size );
3841 *alignment = pointer_size;
3842 return pointer_size;
3844 case FC_IGNORE:
3845 case FC_BIND_PRIMITIVE:
3846 return 0;
3848 default:
3849 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3850 get_basic_fc(type));
3851 return 0;
3853 break;
3855 case TGT_ENUM:
3856 switch (get_enum_fc(type))
3858 case FC_ENUM32:
3859 *alignment = 4;
3860 return 4;
3861 case FC_ENUM16:
3862 *alignment = 4;
3863 return 2;
3865 break;
3867 case TGT_STRUCT:
3868 if (get_struct_fc(type) == FC_STRUCT)
3870 if (!type_struct_get_fields(type)) return 0;
3871 return fields_memsize(type_struct_get_fields(type), alignment);
3873 break;
3875 case TGT_POINTER:
3877 unsigned int size, align;
3878 const type_t *ref = type_pointer_get_ref_type(type);
3879 if (is_string_type( attrs, ref )) break;
3880 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3881 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3883 size += 4 + align;
3884 align = 4;
3886 *alignment = align;
3887 return size;
3890 case TGT_ARRAY:
3891 switch (get_array_fc(type))
3893 case FC_SMFARRAY:
3894 case FC_LGFARRAY:
3895 return type_array_get_dim(type) *
3896 get_required_buffer_size_type(type_array_get_element_type(type), name,
3897 NULL, FALSE, alignment);
3899 break;
3901 default:
3902 break;
3904 return 0;
3907 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3909 int in_attr = is_attr(var->attrs, ATTR_IN);
3910 int out_attr = is_attr(var->attrs, ATTR_OUT);
3912 if (!in_attr && !out_attr)
3913 in_attr = 1;
3915 *alignment = 0;
3917 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3918 pass == PASS_RETURN)
3920 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3922 *alignment = 4;
3923 return 20;
3926 if (!is_string_type(var->attrs, var->declspec.type))
3927 return get_required_buffer_size_type(var->declspec.type, var->name,
3928 var->attrs, TRUE, alignment);
3930 return 0;
3933 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3935 const var_t *var;
3936 unsigned int total_size = 0, alignment;
3938 if (type_function_get_args(func->declspec.type))
3940 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
3942 total_size += get_required_buffer_size(var, &alignment, pass);
3943 total_size += alignment;
3947 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
3949 var_t v = *func;
3950 v.declspec.type = type_function_get_rettype(func->declspec.type);
3951 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3952 total_size += alignment;
3954 return total_size;
3957 static void print_phase_function(FILE *file, int indent, const char *type,
3958 const char *local_var_prefix, enum remoting_phase phase,
3959 const var_t *var, unsigned int type_offset)
3961 const char *function;
3962 switch (phase)
3964 case PHASE_BUFFERSIZE:
3965 function = "BufferSize";
3966 break;
3967 case PHASE_MARSHAL:
3968 function = "Marshall";
3969 break;
3970 case PHASE_UNMARSHAL:
3971 function = "Unmarshall";
3972 break;
3973 case PHASE_FREE:
3974 function = "Free";
3975 break;
3976 default:
3977 assert(0);
3978 return;
3981 print_file(file, indent, "Ndr%s%s(\n", type, function);
3982 indent++;
3983 print_file(file, indent, "&__frame->_StubMsg,\n");
3984 print_file(file, indent, "%s%s%s%s%s,\n",
3985 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3986 (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
3987 local_var_prefix,
3988 (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
3989 var->name);
3990 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3991 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3992 if (phase == PHASE_UNMARSHAL)
3993 print_file(file, indent, "0);\n");
3994 indent--;
3997 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3998 enum remoting_phase phase, enum pass pass, const var_t *var,
3999 const char *varname)
4001 type_t *type = var->declspec.type;
4002 unsigned int alignment = 0;
4004 /* no work to do for other phases, buffer sizing is done elsewhere */
4005 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
4006 return;
4008 if (type_get_type(type) == TYPE_ENUM ||
4009 (type_get_type(type) == TYPE_BASIC &&
4010 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
4011 pointer_size != 4))
4013 unsigned char fc;
4015 if (type_get_type(type) == TYPE_ENUM)
4016 fc = get_enum_fc(type);
4017 else
4018 fc = get_basic_fc(type);
4020 if (phase == PHASE_MARSHAL)
4021 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
4022 else
4023 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
4024 print_file(file, indent+1, "&__frame->_StubMsg,\n");
4025 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
4026 local_var_prefix,
4027 var->name);
4028 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
4030 else
4032 const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
4034 switch (get_basic_fc(ref->type))
4036 case FC_BYTE:
4037 case FC_CHAR:
4038 case FC_SMALL:
4039 case FC_USMALL:
4040 alignment = 1;
4041 break;
4043 case FC_WCHAR:
4044 case FC_USHORT:
4045 case FC_SHORT:
4046 alignment = 2;
4047 break;
4049 case FC_ULONG:
4050 case FC_LONG:
4051 case FC_FLOAT:
4052 case FC_ERROR_STATUS_T:
4053 /* pointer_size must be 4 if we got here in these two cases */
4054 case FC_INT3264:
4055 case FC_UINT3264:
4056 alignment = 4;
4057 break;
4059 case FC_HYPER:
4060 case FC_DOUBLE:
4061 alignment = 8;
4062 break;
4064 case FC_IGNORE:
4065 case FC_BIND_PRIMITIVE:
4066 /* no marshalling needed */
4067 return;
4069 default:
4070 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
4071 var->name, get_basic_fc(ref->type));
4074 if (phase == PHASE_MARSHAL && alignment > 1)
4075 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
4076 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
4077 alignment - 1, alignment - 1);
4079 if (phase == PHASE_MARSHAL)
4081 print_file(file, indent, "*(");
4082 write_type_decl(file, ref, NULL);
4083 if (is_ptr(type))
4084 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
4085 else
4086 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4087 fprintf(file, "%s%s", local_var_prefix, varname);
4088 fprintf(file, ";\n");
4090 else if (phase == PHASE_UNMARSHAL)
4092 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4093 write_type_decl(file, ref, NULL);
4094 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4095 print_file(file, indent, "{\n");
4096 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4097 print_file(file, indent, "}\n");
4098 print_file(file, indent, "%s%s%s",
4099 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4100 local_var_prefix, varname);
4101 if (pass == PASS_IN && is_ptr(type))
4102 fprintf(file, " = (");
4103 else
4104 fprintf(file, " = *(");
4105 write_type_decl(file, ref, NULL);
4106 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4109 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4110 write_type_decl(file, ref, NULL);
4111 fprintf(file, ");\n");
4115 /* returns whether the MaxCount, Offset or ActualCount members need to be
4116 * filled in for the specified phase */
4117 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4119 return (phase != PHASE_UNMARSHAL);
4122 expr_t *get_size_is_expr(const type_t *t, const char *name)
4124 expr_t *x = NULL;
4126 for ( ; is_array(t); t = type_array_get_element_type(t))
4127 if (type_array_has_conformance(t) &&
4128 type_array_get_conformance(t)->type != EXPR_VOID)
4130 if (!x)
4131 x = type_array_get_conformance(t);
4132 else
4133 error("%s: multidimensional conformant"
4134 " arrays not supported at the top level\n",
4135 name);
4138 return x;
4141 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4142 enum remoting_phase phase, const var_t *var, int valid_variance)
4144 const type_t *type = var->declspec.type;
4145 /* get fundamental type for the argument */
4146 for (;;)
4148 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4150 case TGT_ARRAY:
4151 if (is_conformance_needed_for_phase(phase))
4153 if (type_array_has_conformance(type) &&
4154 type_array_get_conformance(type)->type != EXPR_VOID)
4156 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4157 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4158 fprintf(file, ";\n\n");
4160 if (type_array_has_variance(type))
4162 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4163 if (valid_variance)
4165 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4166 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4167 fprintf(file, ";\n\n");
4169 else
4170 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4173 break;
4174 case TGT_UNION:
4175 if (type_get_type(type) == TYPE_UNION &&
4176 is_conformance_needed_for_phase(phase))
4178 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4179 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4180 fprintf(file, ";\n\n");
4182 break;
4183 case TGT_IFACE_POINTER:
4185 expr_t *iid;
4187 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4189 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4190 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4191 fprintf( file, ";\n\n" );
4193 break;
4195 case TGT_POINTER:
4196 type = type_pointer_get_ref_type(type);
4197 continue;
4198 case TGT_INVALID:
4199 case TGT_USER_TYPE:
4200 case TGT_CTXT_HANDLE:
4201 case TGT_CTXT_HANDLE_POINTER:
4202 case TGT_STRING:
4203 case TGT_BASIC:
4204 case TGT_ENUM:
4205 case TGT_STRUCT:
4206 case TGT_RANGE:
4207 break;
4209 break;
4213 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4214 enum pass pass, enum remoting_phase phase, const var_t *var)
4216 int in_attr, out_attr, pointer_type;
4217 const char *type_str = NULL;
4218 const type_t *type = var->declspec.type;
4219 unsigned int alignment, start_offset = type->typestring_offset;
4221 if (is_ptr(type) || is_array(type))
4222 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4223 else
4224 pointer_type = 0;
4226 in_attr = is_attr(var->attrs, ATTR_IN);
4227 out_attr = is_attr(var->attrs, ATTR_OUT);
4228 if (!in_attr && !out_attr)
4229 in_attr = 1;
4231 if (phase != PHASE_FREE)
4232 switch (pass)
4234 case PASS_IN:
4235 if (!in_attr) return;
4236 break;
4237 case PASS_OUT:
4238 if (!out_attr) return;
4239 break;
4240 case PASS_RETURN:
4241 break;
4244 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4246 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4248 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4250 case TGT_CTXT_HANDLE:
4251 case TGT_CTXT_HANDLE_POINTER:
4252 if (phase == PHASE_MARSHAL)
4254 if (pass == PASS_IN)
4256 /* if the context_handle attribute appears in the chain of types
4257 * without pointers being followed, then the context handle must
4258 * be direct, otherwise it is a pointer */
4259 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4260 print_file(file, indent, "NdrClientContextMarshall(\n");
4261 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4262 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4263 var->name);
4264 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4266 else
4268 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4269 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4270 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4271 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
4272 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4275 else if (phase == PHASE_UNMARSHAL)
4277 if (pass == PASS_OUT || pass == PASS_RETURN)
4279 if (!in_attr)
4280 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4281 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4282 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4283 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
4284 pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
4285 print_file(file, indent + 1, "__frame->_Handle);\n");
4287 else
4289 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4290 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4291 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4294 break;
4295 case TGT_USER_TYPE:
4296 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4297 break;
4298 case TGT_STRING:
4299 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4300 pointer_type != FC_RP)
4302 /* strings returned are assumed to be global and hence don't
4303 * need freeing */
4304 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4305 print_phase_function(file, indent, "Pointer", local_var_prefix,
4306 phase, var, start_offset);
4307 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4308 !in_attr && is_conformant_array(type))
4310 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4311 indent++;
4312 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4315 else
4317 unsigned int real_start_offset = start_offset;
4318 /* skip over pointer description straight to string description */
4319 if (is_declptr(type))
4321 if (is_conformant_array(type))
4322 real_start_offset += 4;
4323 else
4324 real_start_offset += 2;
4326 if (is_array(type) && !is_conformant_array(type))
4327 print_phase_function(file, indent, "NonConformantString",
4328 local_var_prefix, phase, var,
4329 real_start_offset);
4330 else
4331 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4332 phase, var, real_start_offset);
4334 break;
4335 case TGT_ARRAY:
4337 unsigned char tc = get_array_fc(type);
4338 const char *array_type = NULL;
4340 /* We already have the size_is expression since it's at the
4341 top level, but do checks for multidimensional conformant
4342 arrays. When we handle them, we'll need to extend this
4343 function to return a list, and then we'll actually use
4344 the return value. */
4345 get_size_is_expr(type, var->name);
4347 switch (tc)
4349 case FC_SMFARRAY:
4350 case FC_LGFARRAY:
4351 array_type = "FixedArray";
4352 break;
4353 case FC_SMVARRAY:
4354 case FC_LGVARRAY:
4355 array_type = "VaryingArray";
4356 break;
4357 case FC_CARRAY:
4358 array_type = "ConformantArray";
4359 break;
4360 case FC_CVARRAY:
4361 array_type = "ConformantVaryingArray";
4362 break;
4363 case FC_BOGUS_ARRAY:
4364 array_type = "ComplexArray";
4365 break;
4368 if (pointer_type != FC_RP) array_type = "Pointer";
4370 if (phase == PHASE_FREE && pointer_type == FC_RP)
4372 /* these are all unmarshalled by allocating memory */
4373 if (tc == FC_BOGUS_ARRAY ||
4374 tc == FC_CVARRAY ||
4375 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4376 (tc == FC_CARRAY && !in_attr))
4378 if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
4380 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4381 type_array_get_ptr_tfsoff(type));
4382 break;
4384 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4385 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4386 indent++;
4387 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4388 break;
4391 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4392 break;
4394 case TGT_BASIC:
4395 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4396 break;
4397 case TGT_ENUM:
4398 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4399 break;
4400 case TGT_RANGE:
4401 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4402 /* Note: this goes beyond what MIDL does - it only supports arguments
4403 * with the [range] attribute in Oicf mode */
4404 if (phase == PHASE_UNMARSHAL)
4406 const expr_t *range_min;
4407 const expr_t *range_max;
4408 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4409 if (!range_list)
4410 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4411 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4412 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4414 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4415 write_type_decl(file, &var->declspec, NULL);
4416 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4417 write_type_decl(file, &var->declspec, NULL);
4418 fprintf(file, ")0x%x))\n", range_max->cval);
4419 print_file(file, indent, "{\n");
4420 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4421 print_file(file, indent, "}\n");
4423 break;
4424 case TGT_STRUCT:
4425 switch (get_struct_fc(type))
4427 case FC_STRUCT:
4428 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4429 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4430 break;
4431 case FC_PSTRUCT:
4432 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4433 break;
4434 case FC_CSTRUCT:
4435 case FC_CPSTRUCT:
4436 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4437 break;
4438 case FC_CVSTRUCT:
4439 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4440 break;
4441 case FC_BOGUS_STRUCT:
4442 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4443 break;
4444 default:
4445 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4447 break;
4448 case TGT_UNION:
4450 const char *union_type = NULL;
4452 if (type_get_type(type) == TYPE_UNION)
4453 union_type = "NonEncapsulatedUnion";
4454 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4455 union_type = "EncapsulatedUnion";
4457 print_phase_function(file, indent, union_type, local_var_prefix,
4458 phase, var, start_offset);
4459 break;
4461 case TGT_POINTER:
4463 const type_t *ref = type_pointer_get_ref_type(type);
4464 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4466 case TGT_BASIC:
4467 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4468 break;
4469 case TGT_ENUM:
4470 /* base types have known sizes, so don't need a sizing pass
4471 * and don't have any memory to free and so don't need a
4472 * freeing pass */
4473 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4474 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4475 break;
4476 case TGT_STRUCT:
4477 switch (get_struct_fc(ref))
4479 case FC_STRUCT:
4480 /* simple structs have known sizes, so don't need a sizing
4481 * pass and don't have any memory to free and so don't
4482 * need a freeing pass */
4483 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4484 type_str = "SimpleStruct";
4485 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4487 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4488 indent++;
4489 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4490 indent--;
4492 break;
4493 case FC_PSTRUCT:
4494 type_str = "SimpleStruct";
4495 break;
4496 case FC_CSTRUCT:
4497 case FC_CPSTRUCT:
4498 type_str = "ConformantStruct";
4499 break;
4500 case FC_CVSTRUCT:
4501 type_str = "ConformantVaryingStruct";
4502 break;
4503 case FC_BOGUS_STRUCT:
4504 type_str = "ComplexStruct";
4505 break;
4506 default:
4507 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4510 if (type_str)
4512 if (phase == PHASE_FREE)
4513 type_str = "Pointer";
4514 else
4515 start_offset = ref->typestring_offset;
4516 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4518 break;
4519 case TGT_UNION:
4520 if (phase == PHASE_FREE)
4521 type_str = "Pointer";
4522 else
4524 if (type_get_type(ref) == TYPE_UNION)
4525 type_str = "NonEncapsulatedUnion";
4526 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4527 type_str = "EncapsulatedUnion";
4529 start_offset = ref->typestring_offset;
4532 print_phase_function(file, indent, type_str, local_var_prefix,
4533 phase, var, start_offset);
4534 break;
4535 case TGT_USER_TYPE:
4536 if (phase != PHASE_FREE)
4538 type_str = "UserMarshal";
4539 start_offset = ref->typestring_offset;
4541 else type_str = "Pointer";
4543 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4544 break;
4545 case TGT_STRING:
4546 case TGT_POINTER:
4547 case TGT_ARRAY:
4548 case TGT_RANGE:
4549 case TGT_IFACE_POINTER:
4550 case TGT_CTXT_HANDLE:
4551 case TGT_CTXT_HANDLE_POINTER:
4552 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4553 break;
4554 case TGT_INVALID:
4555 assert(0);
4556 break;
4558 else
4559 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4560 break;
4562 case TGT_IFACE_POINTER:
4563 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4564 break;
4565 case TGT_INVALID:
4566 assert(0);
4567 break;
4569 fprintf(file, "\n");
4572 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4573 enum pass pass, enum remoting_phase phase)
4575 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4577 unsigned int size = get_function_buffer_size( func, pass );
4578 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4581 if (pass == PASS_RETURN)
4583 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4584 type_function_get_retval(func->declspec.type) );
4586 else
4588 const var_t *var;
4589 if (!type_function_get_args(func->declspec.type))
4590 return;
4591 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4592 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4597 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4599 unsigned int offset = 0;
4600 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4601 return offset;
4604 static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
4606 const statement_t *stmt;
4607 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
4609 const var_t *func = stmt->u.var;
4610 if (!is_local(func->attrs))
4611 *size += get_size_procformatstring_func( iface, func );
4615 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4617 unsigned int size = 1;
4618 for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
4619 return size;
4622 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4624 set_all_tfswrite(FALSE);
4625 return process_tfs(NULL, stmts, pred);
4628 void declare_stub_args( FILE *file, int indent, const var_t *func )
4630 int in_attr, out_attr;
4631 int i = 0;
4632 var_t *var = type_function_get_retval(func->declspec.type);
4634 /* declare return value */
4635 if (!is_void(var->declspec.type))
4637 if (is_context_handle(var->declspec.type))
4638 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4639 else
4641 print_file(file, indent, "%s", "");
4642 write_type_decl(file, &var->declspec, var->name);
4643 fprintf(file, ";\n");
4647 if (!type_function_get_args(func->declspec.type))
4648 return;
4650 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
4652 in_attr = is_attr(var->attrs, ATTR_IN);
4653 out_attr = is_attr(var->attrs, ATTR_OUT);
4654 if (!out_attr && !in_attr)
4655 in_attr = 1;
4657 if (is_context_handle(var->declspec.type))
4658 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4659 else
4661 if (!in_attr && !is_conformant_array(var->declspec.type))
4663 const decl_spec_t *type_to_print;
4664 char name[16];
4665 print_file(file, indent, "%s", "");
4666 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4667 !type_array_is_decl_as_ptr(var->declspec.type))
4668 type_to_print = &var->declspec;
4669 else
4670 type_to_print = type_pointer_get_ref(var->declspec.type);
4671 sprintf(name, "_W%u", i++);
4672 write_type_decl(file, type_to_print, name);
4673 fprintf(file, ";\n");
4676 print_file(file, indent, "%s", "");
4677 write_type_decl_left(file, &var->declspec);
4678 fprintf(file, " ");
4679 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4680 !type_array_is_decl_as_ptr(var->declspec.type)) {
4681 fprintf(file, "(*%s)", var->name);
4682 } else
4683 fprintf(file, "%s", var->name);
4684 write_type_right(file, var->declspec.type, FALSE);
4685 fprintf(file, ";\n");
4687 if (decl_indirect(var->declspec.type))
4688 print_file(file, indent, "void *_p_%s;\n", var->name);
4694 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4696 int in_attr, out_attr;
4697 int i = 0, sep = 0;
4698 const var_t *var;
4699 type_t *ref;
4701 if (!type_function_get_args(func->declspec.type))
4702 return;
4704 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4706 in_attr = is_attr(var->attrs, ATTR_IN);
4707 out_attr = is_attr(var->attrs, ATTR_OUT);
4708 if (!out_attr && !in_attr)
4709 in_attr = 1;
4711 if (!in_attr)
4713 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4715 switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
4717 case TGT_CTXT_HANDLE_POINTER:
4718 fprintf(file, " = NdrContextHandleInitialize(\n");
4719 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4720 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4721 var->typestring_offset);
4722 break;
4723 case TGT_ARRAY:
4724 if (type_array_has_conformance(var->declspec.type))
4726 unsigned int size;
4727 type_t *type;
4729 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4730 for (type = var->declspec.type;
4731 is_array(type) && type_array_has_conformance(type);
4732 type = type_array_get_element_type(type))
4734 write_expr(file, type_array_get_conformance(type), TRUE,
4735 TRUE, NULL, NULL, local_var_prefix);
4736 fprintf(file, " * ");
4738 size = type_memsize(type);
4739 fprintf(file, "%u);\n", size);
4741 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4742 for (type = var->declspec.type;
4743 is_array(type) && type_array_has_conformance(type);
4744 type = type_array_get_element_type(type))
4746 write_expr(file, type_array_get_conformance(type), TRUE,
4747 TRUE, NULL, NULL, local_var_prefix);
4748 fprintf(file, " * ");
4750 size = type_memsize(type);
4751 fprintf(file, "%u);\n", size);
4753 else
4754 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4755 break;
4756 case TGT_POINTER:
4757 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4758 ref = type_pointer_get_ref_type(var->declspec.type);
4759 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4761 case TGT_BASIC:
4762 case TGT_ENUM:
4763 case TGT_POINTER:
4764 case TGT_RANGE:
4765 case TGT_IFACE_POINTER:
4766 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4767 break;
4768 case TGT_USER_TYPE:
4769 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4770 local_var_prefix, i, local_var_prefix, i);
4771 break;
4772 case TGT_ARRAY:
4773 if (type_array_is_decl_as_ptr(ref))
4775 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4776 break;
4778 ref = type_array_get_element_type(ref);
4779 /* fall through */
4780 case TGT_STRUCT:
4781 case TGT_UNION:
4782 if (type_has_pointers(ref))
4783 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4784 local_var_prefix, i, local_var_prefix, i);
4785 break;
4786 case TGT_CTXT_HANDLE:
4787 case TGT_CTXT_HANDLE_POINTER:
4788 case TGT_INVALID:
4789 case TGT_STRING:
4790 /* not initialised */
4791 break;
4793 i++;
4794 break;
4795 default:
4796 break;
4799 sep = 1;
4802 if (sep)
4803 fprintf(file, "\n");
4807 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4808 const char *var_decl, int add_retval )
4810 var_t *retval = type_function_get_retval( func );
4811 const var_list_t *args = type_function_get_args( func );
4812 const var_t *arg;
4813 int needs_packing;
4814 unsigned int align = 0;
4816 if (args)
4817 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4818 if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
4820 needs_packing = (align > pointer_size);
4822 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4823 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4824 print_file(file, 1, "{\n" );
4825 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4827 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4829 print_file(file, 2, "%s", "");
4830 write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
4831 if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
4832 if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
4834 /* FIXME: should check for large args being passed by pointer */
4835 align = 0;
4836 if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
4837 else type_memsize_and_alignment( arg->declspec.type, &align );
4839 if (align < pointer_size)
4840 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4841 fprintf( file, "%s;\n", arg->name );
4843 if (add_retval && !is_void( retval->declspec.type ))
4845 print_file(file, 2, "%s", "");
4846 write_type_left( file, &retval->declspec, NAME_DEFAULT, TRUE, TRUE );
4847 if (needs_space_after( retval->declspec.type )) fputc( ' ', file );
4848 if (!is_array( retval->declspec.type ) && !is_ptr( retval->declspec.type ) &&
4849 type_memsize( retval->declspec.type ) != pointer_size)
4851 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4853 fprintf( file, "%s;\n", retval->name );
4855 print_file(file, 1, "} %s;\n", var_decl );
4856 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4857 print_file( file, 0, "\n" );
4860 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4862 const var_list_t *args = type_function_get_args( func->declspec.type );
4863 const var_t *var;
4865 if (!args) return;
4867 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4868 if (cant_be_null( var ))
4869 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4872 int write_expr_eval_routines(FILE *file, const char *iface)
4874 static const char *var_name = "pS";
4875 static const char *var_name_expr = "pS->";
4876 int result = 0;
4877 struct expr_eval_routine *eval;
4878 unsigned short callback_offset = 0;
4880 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4882 const char *name = eval->name;
4883 result = 1;
4885 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4886 eval->iface ? eval->iface->name : iface, name, callback_offset);
4887 print_file(file, 0, "{\n");
4888 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4890 write_func_param_struct( file, eval->iface, eval->cont_type,
4891 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4893 else
4895 decl_spec_t ds = {.type = (type_t *)eval->cont_type};
4896 print_file(file, 1, "%s", "");
4897 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4898 fprintf(file, " *%s = (", var_name);
4899 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4900 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4902 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4903 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4904 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4905 fprintf(file, ";\n");
4906 print_file(file, 0, "}\n\n");
4907 callback_offset++;
4909 return result;
4912 void write_expr_eval_routine_list(FILE *file, const char *iface)
4914 struct expr_eval_routine *eval;
4915 struct expr_eval_routine *cursor;
4916 unsigned short callback_offset = 0;
4918 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4919 fprintf(file, "{\n");
4921 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4923 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4924 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4925 callback_offset++;
4926 list_remove(&eval->entry);
4927 free(eval->name);
4928 free(eval);
4931 fprintf(file, "};\n\n");
4934 void write_user_quad_list(FILE *file)
4936 user_type_t *ut;
4938 if (list_empty(&user_type_list))
4939 return;
4941 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4942 fprintf(file, "{\n");
4943 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4945 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4946 print_file(file, 1, "{\n");
4947 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4948 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4949 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4950 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4951 print_file(file, 1, "}%s\n", sep);
4953 fprintf(file, "};\n\n");
4956 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4958 const struct str_list_entry_t *endpoint;
4959 const char *p;
4961 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4962 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4963 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4965 print_file( f, 1, "{ (const unsigned char *)\"" );
4966 for (p = endpoint->str; *p && *p != ':'; p++)
4968 if (*p == '"' || *p == '\\') fputc( '\\', f );
4969 fputc( *p, f );
4971 if (!*p) goto error;
4972 if (p[1] != '[') goto error;
4974 fprintf( f, "\", (const unsigned char *)\"" );
4975 for (p += 2; *p && *p != ']'; p++)
4977 if (*p == '"' || *p == '\\') fputc( '\\', f );
4978 fputc( *p, f );
4980 if (*p != ']') goto error;
4981 fprintf( f, "\" },\n" );
4983 print_file( f, 0, "};\n\n" );
4984 return;
4986 error:
4987 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4990 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4991 const char *prefix, unsigned int proc_offset )
4993 const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
4994 int has_ret = !is_void( rettype->type );
4995 const var_list_t *args = type_function_get_args( func->declspec.type );
4996 const var_t *arg;
4997 int len, needs_params = 0;
4999 /* we need a param structure if we have more than one arg */
5000 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
5002 print_file( file, 0, "{\n");
5003 if (needs_params)
5005 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
5006 write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
5007 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
5008 if (args)
5009 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5010 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
5012 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
5014 len = fprintf( file, " %s%s( ",
5015 has_ret ? "_RetVal = " : "",
5016 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
5017 fprintf( file, "&%s_StubDesc,", prefix );
5018 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
5019 if (needs_params)
5021 fprintf( file, ",\n%*s&__params", len, "" );
5023 else if (pointer_size == 8)
5025 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
5026 if (args)
5027 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5028 fprintf( file, ",\n%*s%s", len, "", arg->name );
5030 else
5032 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
5033 else if (args)
5035 arg = LIST_ENTRY( list_head(args), const var_t, entry );
5036 fprintf( file, ",\n%*s&%s", len, "", arg->name );
5039 fprintf( file, " );\n" );
5040 if (has_ret)
5042 print_file( file, 1, "return (" );
5043 write_type_decl_left(file, rettype);
5044 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
5046 print_file( file, 0, "}\n\n");
5049 void write_exceptions( FILE *file )
5051 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
5052 fprintf( file, "\n");
5053 fprintf( file, "#include \"wine/exception.h\"\n");
5054 fprintf( file, "#undef RpcTryExcept\n");
5055 fprintf( file, "#undef RpcExcept\n");
5056 fprintf( file, "#undef RpcEndExcept\n");
5057 fprintf( file, "#undef RpcTryFinally\n");
5058 fprintf( file, "#undef RpcFinally\n");
5059 fprintf( file, "#undef RpcEndFinally\n");
5060 fprintf( file, "#undef RpcExceptionCode\n");
5061 fprintf( file, "#undef RpcAbnormalTermination\n");
5062 fprintf( file, "\n");
5063 fprintf( file, "struct __exception_frame;\n");
5064 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
5065 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
5066 fprintf( file, "\n");
5067 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5068 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
5069 fprintf( file, " __filter_func filter; \\\n");
5070 fprintf( file, " __finally_func finally; \\\n");
5071 fprintf( file, " __wine_jmp_buf jmp; \\\n");
5072 fprintf( file, " DWORD code; \\\n");
5073 fprintf( file, " unsigned char abnormal_termination; \\\n");
5074 fprintf( file, " unsigned char filter_level; \\\n");
5075 fprintf( file, " unsigned char finally_level;\n");
5076 fprintf( file, "\n");
5077 fprintf( file, "struct __exception_frame\n{\n");
5078 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
5079 fprintf( file, "};\n");
5080 fprintf( file, "\n");
5081 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
5082 fprintf( file, "{\n");
5083 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
5084 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
5085 fprintf( file, " {\n");
5086 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5087 fprintf( file, " exc_frame->finally( exc_frame );\n");
5088 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5089 fprintf( file, " }\n");
5090 fprintf( file, " exc_frame->filter_level = 0;\n");
5091 fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
5092 fprintf( file, "}\n");
5093 fprintf( file, "\n");
5094 fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5095 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5096 fprintf( file, " CONTEXT *context,\n");
5097 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5098 fprintf( file, "{\n");
5099 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5100 fprintf( file, "\n");
5101 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5102 fprintf( file, " {\n" );
5103 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5104 fprintf( file, " {\n" );
5105 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5106 fprintf( file, " exc_frame->finally( exc_frame );\n");
5107 fprintf( file, " }\n" );
5108 fprintf( file, " return ExceptionContinueSearch;\n");
5109 fprintf( file, " }\n" );
5110 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5111 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5112 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5113 fprintf( file, " return ExceptionContinueSearch;\n");
5114 fprintf( file, "}\n");
5115 fprintf( file, "\n");
5116 fprintf( file, "#define RpcTryExcept \\\n");
5117 fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
5118 fprintf( file, " { \\\n");
5119 fprintf( file, " if (!__frame->finally_level) \\\n" );
5120 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5121 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5122 fprintf( file, "\n");
5123 fprintf( file, "#define RpcExcept(expr) \\\n");
5124 fprintf( file, " if (!__frame->finally_level) \\\n" );
5125 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5126 fprintf( file, " __frame->filter_level = 0; \\\n" );
5127 fprintf( file, " } \\\n");
5128 fprintf( file, " else \\\n");
5129 fprintf( file, "\n");
5130 fprintf( file, "#define RpcEndExcept\n");
5131 fprintf( file, "\n");
5132 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5133 fprintf( file, "\n");
5134 fprintf( file, "#define RpcTryFinally \\\n");
5135 fprintf( file, " if (!__frame->filter_level) \\\n");
5136 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5137 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5138 fprintf( file, "\n");
5139 fprintf( file, "#define RpcFinally \\\n");
5140 fprintf( file, " if (!__frame->filter_level) \\\n");
5141 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5142 fprintf( file, " __frame->finally_level = 0;\n");
5143 fprintf( file, "\n");
5144 fprintf( file, "#define RpcEndFinally\n");
5145 fprintf( file, "\n");
5146 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5147 fprintf( file, "\n");
5148 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5149 fprintf( file, " do { \\\n");
5150 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5151 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5152 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5153 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5154 fprintf( file, " __frame->filter_level = 0; \\\n");
5155 fprintf( file, " __frame->finally_level = 0; \\\n");
5156 fprintf( file, " } while (0)\n");
5157 fprintf( file, "\n");
5158 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5159 fprintf( file, "\n");
5160 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5161 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5162 fprintf( file, "\n");
5163 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5164 fprintf( file, " DWORD code;\n");
5165 fprintf( file, "\n");
5166 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");