msado15: Implement ADORecordsetConstruction get/put Rowset.
[wine.git] / tools / widl / typegen.c
blob4874dfd6c24427dde811e2a3ca08891813ab82c0
1 /*
2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <string.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <limits.h>
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typetree.h"
41 #include "typegen.h"
42 #include "expr.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const type_t *current_structure;
50 static const var_t *current_func;
51 static const type_t *current_iface;
53 static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
54 struct expr_eval_routine
56 struct list entry;
57 const type_t *iface;
58 const type_t *cont_type;
59 char *name;
60 unsigned int baseoff;
61 const expr_t *expr;
64 enum type_context
66 TYPE_CONTEXT_TOPLEVELPARAM,
67 TYPE_CONTEXT_PARAM,
68 TYPE_CONTEXT_CONTAINER,
69 TYPE_CONTEXT_CONTAINER_NO_POINTERS,
70 TYPE_CONTEXT_RETVAL,
73 /* parameter flags in Oif mode */
74 static const unsigned short MustSize = 0x0001;
75 static const unsigned short MustFree = 0x0002;
76 static const unsigned short IsPipe = 0x0004;
77 static const unsigned short IsIn = 0x0008;
78 static const unsigned short IsOut = 0x0010;
79 static const unsigned short IsReturn = 0x0020;
80 static const unsigned short IsBasetype = 0x0040;
81 static const unsigned short IsByValue = 0x0080;
82 static const unsigned short IsSimpleRef = 0x0100;
83 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
84 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
86 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
87 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
88 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
89 const char *name, unsigned int *typestring_offset);
90 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
91 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
92 const char *name, int write_ptr, unsigned int *tfsoff);
93 static const var_t *find_array_or_string_in_struct(const type_t *type);
94 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
95 type_t *type, enum type_context context,
96 const char *name, unsigned int *typestring_offset);
97 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
98 const attr_list_t *attrs, int toplevel_param,
99 unsigned int *alignment );
100 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
102 static const char *string_of_type(unsigned char type)
104 switch (type)
106 case FC_BYTE: return "FC_BYTE";
107 case FC_CHAR: return "FC_CHAR";
108 case FC_SMALL: return "FC_SMALL";
109 case FC_USMALL: return "FC_USMALL";
110 case FC_WCHAR: return "FC_WCHAR";
111 case FC_SHORT: return "FC_SHORT";
112 case FC_USHORT: return "FC_USHORT";
113 case FC_LONG: return "FC_LONG";
114 case FC_ULONG: return "FC_ULONG";
115 case FC_FLOAT: return "FC_FLOAT";
116 case FC_HYPER: return "FC_HYPER";
117 case FC_DOUBLE: return "FC_DOUBLE";
118 case FC_ENUM16: return "FC_ENUM16";
119 case FC_ENUM32: return "FC_ENUM32";
120 case FC_IGNORE: return "FC_IGNORE";
121 case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
122 case FC_RP: return "FC_RP";
123 case FC_UP: return "FC_UP";
124 case FC_OP: return "FC_OP";
125 case FC_FP: return "FC_FP";
126 case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
127 case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
128 case FC_STRUCT: return "FC_STRUCT";
129 case FC_PSTRUCT: return "FC_PSTRUCT";
130 case FC_CSTRUCT: return "FC_CSTRUCT";
131 case FC_CPSTRUCT: return "FC_CPSTRUCT";
132 case FC_CVSTRUCT: return "FC_CVSTRUCT";
133 case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
134 case FC_SMFARRAY: return "FC_SMFARRAY";
135 case FC_LGFARRAY: return "FC_LGFARRAY";
136 case FC_SMVARRAY: return "FC_SMVARRAY";
137 case FC_LGVARRAY: return "FC_LGVARRAY";
138 case FC_CARRAY: return "FC_CARRAY";
139 case FC_CVARRAY: return "FC_CVARRAY";
140 case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
141 case FC_ALIGNM2: return "FC_ALIGNM2";
142 case FC_ALIGNM4: return "FC_ALIGNM4";
143 case FC_ALIGNM8: return "FC_ALIGNM8";
144 case FC_POINTER: return "FC_POINTER";
145 case FC_C_CSTRING: return "FC_C_CSTRING";
146 case FC_C_WSTRING: return "FC_C_WSTRING";
147 case FC_CSTRING: return "FC_CSTRING";
148 case FC_WSTRING: return "FC_WSTRING";
149 case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
150 case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
151 case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
152 case FC_IP: return "FC_IP";
153 case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
154 case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
155 case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
156 case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
157 case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
158 case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
159 case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
160 case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
161 case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
162 case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
163 case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
164 case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
165 case FC_STRING_SIZED: return "FC_STRING_SIZED";
166 case FC_NO_REPEAT: return "FC_NO_REPEAT";
167 case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
168 case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
169 case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
170 case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
171 case FC_PP: return "FC_PP";
172 case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
173 case FC_DEREFERENCE: return "FC_DEREFERENCE";
174 case FC_DIV_2: return "FC_DIV_2";
175 case FC_MULT_2: return "FC_MULT_2";
176 case FC_ADD_1: return "FC_ADD_1";
177 case FC_SUB_1: return "FC_SUB_1";
178 case FC_CALLBACK: return "FC_CALLBACK";
179 case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
180 case FC_END: return "FC_END";
181 case FC_PAD: return "FC_PAD";
182 case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
183 case FC_RANGE: return "FC_RANGE";
184 case FC_INT3264: return "FC_INT3264";
185 case FC_UINT3264: return "FC_UINT3264";
186 default:
187 error("string_of_type: unknown type 0x%02x\n", type);
188 return NULL;
192 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
194 const type_t *t = type;
195 for (;;)
197 if (is_attr(t->attrs, attr))
198 return get_attrp(t->attrs, attr);
199 else if (type_is_alias(t))
200 t = type_alias_get_aliasee_type(t);
201 else return NULL;
205 unsigned char get_basic_fc(const type_t *type)
207 int sign = type_basic_get_sign(type);
208 switch (type_basic_get_type(type))
210 case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
211 case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
212 case TYPE_BASIC_INT32:
213 case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
214 case TYPE_BASIC_INT64: return FC_HYPER;
215 case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
216 case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
217 case TYPE_BASIC_BYTE: return FC_BYTE;
218 case TYPE_BASIC_CHAR: return FC_CHAR;
219 case TYPE_BASIC_WCHAR: return FC_WCHAR;
220 case TYPE_BASIC_HYPER: return FC_HYPER;
221 case TYPE_BASIC_FLOAT: return FC_FLOAT;
222 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
223 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
224 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
226 return 0;
229 static unsigned char get_basic_fc_signed(const type_t *type)
231 switch (type_basic_get_type(type))
233 case TYPE_BASIC_INT8: return FC_SMALL;
234 case TYPE_BASIC_INT16: return FC_SHORT;
235 case TYPE_BASIC_INT32: return FC_LONG;
236 case TYPE_BASIC_INT64: return FC_HYPER;
237 case TYPE_BASIC_INT: return FC_LONG;
238 case TYPE_BASIC_INT3264: return FC_INT3264;
239 case TYPE_BASIC_LONG: return FC_LONG;
240 case TYPE_BASIC_BYTE: return FC_BYTE;
241 case TYPE_BASIC_CHAR: return FC_CHAR;
242 case TYPE_BASIC_WCHAR: return FC_WCHAR;
243 case TYPE_BASIC_HYPER: return FC_HYPER;
244 case TYPE_BASIC_FLOAT: return FC_FLOAT;
245 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
246 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
247 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
249 return 0;
252 static inline unsigned int clamp_align(unsigned int align)
254 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
255 if(align > packing) align = packing;
256 return align;
259 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
261 const type_t *t;
262 int pointer_type;
264 assert(is_ptr(type) || is_array(type));
266 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
267 if (pointer_type)
268 return pointer_type;
270 for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
272 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
273 if (pointer_type)
274 return pointer_type;
277 if (toplevel_param)
278 return FC_RP;
280 if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
281 return pointer_type;
283 return FC_UP;
286 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
287 enum type_context context )
289 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
291 if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
292 (context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
293 pointer_fc = FC_OP;
295 return pointer_fc;
298 static unsigned char get_enum_fc(const type_t *type)
300 assert(type_get_type(type) == TYPE_ENUM);
301 if (is_aliaschain_attr(type, ATTR_V1ENUM))
302 return FC_ENUM32;
303 else
304 return FC_ENUM16;
307 static type_t *get_user_type(const type_t *t, const char **pname)
309 for (;;)
311 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
312 if (ut)
314 if (pname)
315 *pname = t->name;
316 return ut;
319 if (type_is_alias(t))
320 t = type_alias_get_aliasee_type(t);
321 else
322 return NULL;
326 static int is_user_type(const type_t *t)
328 return get_user_type(t, NULL) != NULL;
331 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
333 if (is_user_type(type))
334 return TGT_USER_TYPE;
336 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
337 return TGT_CTXT_HANDLE;
339 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
340 return TGT_STRING;
342 switch (type_get_type(type))
344 case TYPE_BASIC:
345 if (!(flags & TDT_IGNORE_RANGES) &&
346 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
347 return TGT_RANGE;
348 return TGT_BASIC;
349 case TYPE_ENUM:
350 if (!(flags & TDT_IGNORE_RANGES) &&
351 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
352 return TGT_RANGE;
353 return TGT_ENUM;
354 case TYPE_POINTER:
355 if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
356 (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
357 return TGT_IFACE_POINTER;
358 else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
359 return TGT_CTXT_HANDLE_POINTER;
360 else
361 return TGT_POINTER;
362 case TYPE_STRUCT:
363 return TGT_STRUCT;
364 case TYPE_ENCAPSULATED_UNION:
365 case TYPE_UNION:
366 return TGT_UNION;
367 case TYPE_ARRAY:
368 return TGT_ARRAY;
369 case TYPE_FUNCTION:
370 case TYPE_COCLASS:
371 case TYPE_INTERFACE:
372 case TYPE_MODULE:
373 case TYPE_VOID:
374 case TYPE_ALIAS:
375 case TYPE_BITFIELD:
376 break;
378 return TGT_INVALID;
381 static int cant_be_null(const var_t *v)
383 switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
385 case TGT_ARRAY:
386 if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
387 /* fall through */
388 case TGT_POINTER:
389 return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
390 case TGT_CTXT_HANDLE_POINTER:
391 return TRUE;
392 default:
393 return 0;
398 static int get_padding(const var_list_t *fields)
400 unsigned short offset = 0;
401 unsigned int salign = 1;
402 const var_t *f;
404 if (!fields)
405 return 0;
407 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
409 type_t *ft = f->declspec.type;
410 unsigned int align = 0;
411 unsigned int size = type_memsize_and_alignment(ft, &align);
412 align = clamp_align(align);
413 if (align > salign) salign = align;
414 offset = ROUND_SIZE(offset, align);
415 offset += size;
418 return ROUNDING(offset, salign);
421 static unsigned int get_stack_size( const var_t *var, int *by_value )
423 unsigned int stack_size;
424 int by_val;
426 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
428 case TGT_BASIC:
429 case TGT_ENUM:
430 case TGT_RANGE:
431 case TGT_STRUCT:
432 case TGT_UNION:
433 case TGT_USER_TYPE:
434 stack_size = type_memsize( var->declspec.type );
435 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
436 break;
437 default:
438 by_val = 0;
439 break;
441 if (!by_val) stack_size = pointer_size;
442 if (by_value) *by_value = by_val;
443 return ROUND_SIZE( stack_size, pointer_size );
446 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
447 const type_t *type, int is_return )
449 unsigned char flags = 0;
450 int is_out;
452 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
454 if (is_ptr(type) &&
455 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
456 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
457 flags |= HANDLE_PARAM_IS_VIA_PTR;
459 if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
461 is_out = is_attr(attrs, ATTR_OUT);
462 if (is_attr(attrs, ATTR_IN) || !is_out)
464 flags |= HANDLE_PARAM_IS_IN;
465 if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
467 if (is_out) flags |= HANDLE_PARAM_IS_OUT;
469 return flags;
472 static unsigned int get_rpc_flags( const attr_list_t *attrs )
474 unsigned int flags = 0;
476 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
477 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
478 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
479 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
480 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
481 return flags;
484 unsigned char get_struct_fc(const type_t *type)
486 int has_pointer = 0;
487 int has_conformance = 0;
488 int has_variance = 0;
489 var_t *field;
490 var_list_t *fields;
492 fields = type_struct_get_fields(type);
494 if (get_padding(fields))
495 return FC_BOGUS_STRUCT;
497 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
499 type_t *t = field->declspec.type;
500 enum typegen_type typegen_type;
502 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
504 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
506 if (is_string_type(field->attrs, field->declspec.type))
508 if (is_conformant_array(t))
509 has_conformance = 1;
510 has_variance = 1;
511 continue;
514 if (is_array(type_array_get_element_type(field->declspec.type)))
515 return FC_BOGUS_STRUCT;
517 if (type_array_has_conformance(field->declspec.type))
519 has_conformance = 1;
520 if (list_next(fields, &field->entry))
521 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
522 field->name);
524 if (type_array_has_variance(t))
525 has_variance = 1;
527 t = type_array_get_element_type(t);
528 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
531 switch (typegen_type)
533 case TGT_USER_TYPE:
534 case TGT_IFACE_POINTER:
535 return FC_BOGUS_STRUCT;
536 case TGT_BASIC:
537 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
538 return FC_BOGUS_STRUCT;
539 break;
540 case TGT_ENUM:
541 if (get_enum_fc(t) == FC_ENUM16)
542 return FC_BOGUS_STRUCT;
543 break;
544 case TGT_POINTER:
545 case TGT_ARRAY:
546 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
547 return FC_BOGUS_STRUCT;
548 has_pointer = 1;
549 break;
550 case TGT_UNION:
551 return FC_BOGUS_STRUCT;
552 case TGT_STRUCT:
554 unsigned char fc = get_struct_fc(t);
555 switch (fc)
557 case FC_STRUCT:
558 break;
559 case FC_CVSTRUCT:
560 has_conformance = 1;
561 has_variance = 1;
562 has_pointer = 1;
563 break;
565 case FC_CPSTRUCT:
566 has_conformance = 1;
567 if (list_next( fields, &field->entry ))
568 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
569 field->name);
570 has_pointer = 1;
571 break;
573 case FC_CSTRUCT:
574 has_conformance = 1;
575 if (list_next( fields, &field->entry ))
576 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
577 field->name);
578 break;
580 case FC_PSTRUCT:
581 has_pointer = 1;
582 break;
584 default:
585 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
586 /* fallthru - treat it as complex */
588 /* as soon as we see one of these these members, it's bogus... */
589 case FC_BOGUS_STRUCT:
590 return FC_BOGUS_STRUCT;
592 break;
594 case TGT_RANGE:
595 return FC_BOGUS_STRUCT;
596 case TGT_STRING:
597 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
598 case TGT_INVALID:
599 case TGT_CTXT_HANDLE:
600 case TGT_CTXT_HANDLE_POINTER:
601 /* checking after parsing should mean that we don't get here. if we do,
602 * it's a checker bug */
603 assert(0);
607 if( has_variance )
609 if ( has_conformance )
610 return FC_CVSTRUCT;
611 else
612 return FC_BOGUS_STRUCT;
614 if( has_conformance && has_pointer )
615 return FC_CPSTRUCT;
616 if( has_conformance )
617 return FC_CSTRUCT;
618 if( has_pointer )
619 return FC_PSTRUCT;
620 return FC_STRUCT;
623 static unsigned char get_array_fc(const type_t *type)
625 unsigned char fc;
626 const expr_t *size_is;
627 const type_t *elem_type;
629 elem_type = type_array_get_element_type(type);
630 size_is = type_array_get_conformance(type);
632 if (!size_is)
634 unsigned int size = type_memsize(elem_type);
635 if (size * type_array_get_dim(type) > 0xffffuL)
636 fc = FC_LGFARRAY;
637 else
638 fc = FC_SMFARRAY;
640 else
641 fc = FC_CARRAY;
643 if (type_array_has_variance(type))
645 if (fc == FC_SMFARRAY)
646 fc = FC_SMVARRAY;
647 else if (fc == FC_LGFARRAY)
648 fc = FC_LGVARRAY;
649 else if (fc == FC_CARRAY)
650 fc = FC_CVARRAY;
653 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
655 case TGT_USER_TYPE:
656 fc = FC_BOGUS_ARRAY;
657 break;
658 case TGT_BASIC:
659 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
660 pointer_size != 4)
661 fc = FC_BOGUS_ARRAY;
662 break;
663 case TGT_STRUCT:
664 switch (get_struct_fc(elem_type))
666 case FC_BOGUS_STRUCT:
667 fc = FC_BOGUS_ARRAY;
668 break;
670 break;
671 case TGT_ENUM:
672 /* is 16-bit enum - if so, wire size differs from mem size and so
673 * the array cannot be block copied, which means the array is complex */
674 if (get_enum_fc(elem_type) == FC_ENUM16)
675 fc = FC_BOGUS_ARRAY;
676 break;
677 case TGT_UNION:
678 case TGT_IFACE_POINTER:
679 fc = FC_BOGUS_ARRAY;
680 break;
681 case TGT_POINTER:
682 /* ref pointers cannot just be block copied. unique pointers to
683 * interfaces need special treatment. either case means the array is
684 * complex */
685 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
686 fc = FC_BOGUS_ARRAY;
687 break;
688 case TGT_RANGE:
689 fc = FC_BOGUS_ARRAY;
690 break;
691 case TGT_CTXT_HANDLE:
692 case TGT_CTXT_HANDLE_POINTER:
693 case TGT_STRING:
694 case TGT_INVALID:
695 case TGT_ARRAY:
696 /* nothing to do for everything else */
697 break;
700 return fc;
703 static int is_non_complex_struct(const type_t *type)
705 return (type_get_type(type) == TYPE_STRUCT &&
706 get_struct_fc(type) != FC_BOGUS_STRUCT);
709 static int type_has_pointers(const type_t *type)
711 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
713 case TGT_USER_TYPE:
714 return FALSE;
715 case TGT_POINTER:
716 return TRUE;
717 case TGT_ARRAY:
718 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
719 case TGT_STRUCT:
721 var_list_t *fields = type_struct_get_fields(type);
722 const var_t *field;
723 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
725 if (type_has_pointers(field->declspec.type))
726 return TRUE;
728 break;
730 case TGT_UNION:
732 var_list_t *fields;
733 const var_t *field;
734 fields = type_union_get_cases(type);
735 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
737 if (field->declspec.type && type_has_pointers(field->declspec.type))
738 return TRUE;
740 break;
742 case TGT_CTXT_HANDLE:
743 case TGT_CTXT_HANDLE_POINTER:
744 case TGT_STRING:
745 case TGT_IFACE_POINTER:
746 case TGT_BASIC:
747 case TGT_ENUM:
748 case TGT_RANGE:
749 case TGT_INVALID:
750 break;
753 return FALSE;
756 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
757 int toplevel_param)
759 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
761 case TGT_USER_TYPE:
762 return FALSE;
763 case TGT_POINTER:
764 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
765 return TRUE;
766 else
767 return FALSE;
768 case TGT_ARRAY:
769 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
770 return TRUE;
771 else
772 return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
773 case TGT_STRUCT:
775 var_list_t *fields = type_struct_get_fields(type);
776 const var_t *field;
777 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
779 if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
780 return TRUE;
782 break;
784 case TGT_UNION:
786 var_list_t *fields;
787 const var_t *field;
788 fields = type_union_get_cases(type);
789 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
791 if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
792 return TRUE;
794 break;
796 case TGT_CTXT_HANDLE:
797 case TGT_CTXT_HANDLE_POINTER:
798 case TGT_STRING:
799 case TGT_IFACE_POINTER:
800 case TGT_BASIC:
801 case TGT_ENUM:
802 case TGT_RANGE:
803 case TGT_INVALID:
804 break;
807 return FALSE;
810 static unsigned short user_type_offset(const char *name)
812 user_type_t *ut;
813 unsigned short off = 0;
814 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
816 if (strcmp(name, ut->name) == 0)
817 return off;
818 ++off;
820 error("user_type_offset: couldn't find type (%s)\n", name);
821 return 0;
824 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
826 type->typestring_offset = offset;
827 if (file) type->tfswrite = FALSE;
830 static void guard_rec(type_t *type)
832 /* types that contain references to themselves (like a linked list),
833 need to be shielded from infinite recursion when writing embedded
834 types */
835 if (type->typestring_offset)
836 type->tfswrite = FALSE;
837 else
838 type->typestring_offset = 1;
841 static int is_embedded_complex(const type_t *type)
843 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
845 case TGT_USER_TYPE:
846 case TGT_STRUCT:
847 case TGT_UNION:
848 case TGT_ARRAY:
849 case TGT_IFACE_POINTER:
850 return TRUE;
851 default:
852 return FALSE;
856 static const char *get_context_handle_type_name(const type_t *type)
858 const type_t *t;
859 for (t = type;
860 is_ptr(t) || type_is_alias(t);
861 t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
862 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
863 return t->name;
864 assert(0);
865 return NULL;
868 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
869 do { \
870 if (file) \
871 fprintf(file, "/* %2u */\n", typestring_offset); \
872 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
874 while (0)
876 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
877 static void print_file(FILE *file, int indent, const char *format, ...)
879 va_list va;
880 va_start(va, format);
881 print(file, indent, format, va);
882 va_end(va);
885 void print(FILE *file, int indent, const char *format, va_list va)
887 if (file)
889 if (format[0] != '\n')
890 while (0 < indent--)
891 fprintf(file, " ");
892 vfprintf(file, format, va);
897 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
899 if (decl_indirect(t))
901 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
902 local_var_prefix, n, local_var_prefix, n);
903 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
905 else if (is_ptr(t) || is_array(t))
906 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
909 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
911 const var_t *var = type_function_get_retval(func->declspec.type);
913 if (!is_void(var->declspec.type))
914 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
916 if (!type_function_get_args(func->declspec.type))
917 return;
919 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
920 write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
922 fprintf(file, "\n");
925 static void write_formatdesc(FILE *f, int indent, const char *str)
927 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
928 print_file(f, indent, "{\n");
929 print_file(f, indent + 1, "short Pad;\n");
930 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
931 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
932 print_file(f, indent, "\n");
935 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
937 clear_all_offsets();
939 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
940 get_size_typeformatstring(stmts, pred));
942 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
943 get_size_procformatstring(stmts, pred));
945 fprintf(f, "\n");
946 write_formatdesc(f, indent, "TYPE");
947 write_formatdesc(f, indent, "PROC");
948 fprintf(f, "\n");
949 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
950 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
951 print_file(f, indent, "\n");
954 int decl_indirect(const type_t *t)
956 if (is_user_type(t))
957 return TRUE;
958 return (type_get_type(t) != TYPE_BASIC &&
959 type_get_type(t) != TYPE_ENUM &&
960 type_get_type(t) != TYPE_POINTER &&
961 type_get_type(t) != TYPE_ARRAY);
964 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
965 unsigned int *stack_size, unsigned int *typestring_offset )
967 unsigned int alignment, server_size = 0, buffer_size = 0;
968 unsigned char fc = 0;
969 int is_byval;
970 int is_in = is_attr(var->attrs, ATTR_IN);
971 int is_out = is_attr(var->attrs, ATTR_OUT);
973 if (is_return) is_out = TRUE;
974 else if (!is_in && !is_out) is_in = TRUE;
976 *flags = 0;
977 *stack_size = get_stack_size( var, &is_byval );
978 *typestring_offset = var->typestring_offset;
980 if (is_in) *flags |= IsIn;
981 if (is_out) *flags |= IsOut;
982 if (is_return) *flags |= IsReturn;
984 if (!is_string_type( var->attrs, var->declspec.type ))
985 buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
987 switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
989 case TGT_BASIC:
990 *flags |= IsBasetype;
991 fc = get_basic_fc_signed( var->declspec.type );
992 if (fc == FC_BIND_PRIMITIVE)
994 buffer_size = 4; /* actually 0 but avoids setting MustSize */
995 fc = FC_LONG;
997 break;
998 case TGT_ENUM:
999 *flags |= IsBasetype;
1000 fc = get_enum_fc( var->declspec.type );
1001 break;
1002 case TGT_RANGE:
1003 *flags |= IsByValue;
1004 break;
1005 case TGT_STRUCT:
1006 case TGT_UNION:
1007 case TGT_USER_TYPE:
1008 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1009 break;
1010 case TGT_IFACE_POINTER:
1011 *flags |= MustFree;
1012 break;
1013 case TGT_ARRAY:
1014 *flags |= MustFree;
1015 if (type_array_is_decl_as_ptr(var->declspec.type)
1016 && type_array_get_ptr_tfsoff(var->declspec.type)
1017 && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
1019 *typestring_offset = var->declspec.type->typestring_offset;
1020 *flags |= IsSimpleRef;
1022 break;
1023 case TGT_STRING:
1024 *flags |= MustFree;
1025 if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1027 /* skip over pointer description straight to string description */
1028 if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
1029 else *typestring_offset += 2;
1030 *flags |= IsSimpleRef;
1032 break;
1033 case TGT_CTXT_HANDLE_POINTER:
1034 *flags |= IsSimpleRef;
1035 *typestring_offset += 4;
1036 /* fall through */
1037 case TGT_CTXT_HANDLE:
1038 buffer_size = 20;
1039 break;
1040 case TGT_POINTER:
1041 if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
1043 const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
1045 if (!is_string_type( var->attrs, ref ))
1046 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1048 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1050 case TGT_BASIC:
1051 *flags |= IsSimpleRef | IsBasetype;
1052 fc = get_basic_fc( ref );
1053 if (!is_in && is_out) server_size = pointer_size;
1054 break;
1055 case TGT_ENUM:
1056 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1058 *flags |= IsSimpleRef | IsBasetype;
1059 if (!is_in && is_out) server_size = pointer_size;
1061 else
1063 server_size = pointer_size;
1065 break;
1066 case TGT_UNION:
1067 case TGT_USER_TYPE:
1068 case TGT_RANGE:
1069 *flags |= MustFree | IsSimpleRef;
1070 *typestring_offset = ref->typestring_offset;
1071 if (!is_in && is_out) server_size = type_memsize( ref );
1072 break;
1073 case TGT_ARRAY:
1074 *flags |= MustFree;
1075 if (!type_array_is_decl_as_ptr(ref))
1077 *flags |= IsSimpleRef;
1078 *typestring_offset = ref->typestring_offset;
1080 if (!is_in && is_out) server_size = type_memsize( ref );
1081 break;
1082 case TGT_STRING:
1083 case TGT_POINTER:
1084 case TGT_CTXT_HANDLE:
1085 case TGT_CTXT_HANDLE_POINTER:
1086 *flags |= MustFree;
1087 server_size = pointer_size;
1088 break;
1089 case TGT_IFACE_POINTER:
1090 *flags |= MustFree;
1091 if (is_in && is_out) server_size = pointer_size;
1092 break;
1093 case TGT_STRUCT:
1094 *flags |= IsSimpleRef | MustFree;
1095 *typestring_offset = ref->typestring_offset;
1096 switch (get_struct_fc(ref))
1098 case FC_STRUCT:
1099 case FC_PSTRUCT:
1100 case FC_BOGUS_STRUCT:
1101 if (!is_in && is_out) server_size = type_memsize( ref );
1102 break;
1103 default:
1104 break;
1106 break;
1107 case TGT_INVALID:
1108 assert(0);
1111 else /* not ref pointer */
1113 *flags |= MustFree;
1115 break;
1116 case TGT_INVALID:
1117 assert(0);
1120 if (!buffer_size) *flags |= MustSize;
1122 if (server_size)
1124 server_size = (server_size + 7) / 8;
1125 if (server_size < 8) *flags |= server_size << 13;
1127 return fc;
1130 static unsigned char get_func_oi2_flags( const var_t *func )
1132 const var_t *var;
1133 var_list_t *args = type_function_get_args( func->declspec.type );
1134 var_t *retval = type_function_get_retval( func->declspec.type );
1135 unsigned char oi2_flags = 0x40; /* HasExtensions */
1136 unsigned short flags;
1137 unsigned int stack_size, typestring_offset;
1139 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1141 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1142 if (flags & MustSize)
1144 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1145 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1149 if (!is_void( retval->declspec.type ))
1151 oi2_flags |= 0x04; /* HasRet */
1152 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1153 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1155 return oi2_flags;
1158 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1159 int is_return, unsigned int *stack_offset)
1161 char buffer[128];
1162 unsigned int stack_size, typestring_offset;
1163 unsigned short flags;
1164 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1166 strcpy( buffer, "/* flags:" );
1167 if (flags & MustSize) strcat( buffer, " must size," );
1168 if (flags & MustFree) strcat( buffer, " must free," );
1169 if (flags & IsPipe) strcat( buffer, " pipe," );
1170 if (flags & IsIn) strcat( buffer, " in," );
1171 if (flags & IsOut) strcat( buffer, " out," );
1172 if (flags & IsReturn) strcat( buffer, " return," );
1173 if (flags & IsBasetype) strcat( buffer, " base type," );
1174 if (flags & IsByValue) strcat( buffer, " by value," );
1175 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1176 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1177 strcpy( buffer + strlen( buffer ) - 1, " */" );
1178 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1179 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1180 *stack_offset, *stack_offset );
1181 if (flags & IsBasetype)
1183 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1184 print_file( file, indent, "0x0,\n" );
1186 else
1187 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1188 typestring_offset, typestring_offset );
1189 *stack_offset += max( stack_size, pointer_size );
1190 return 6;
1193 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1194 int is_return, int is_interpreted)
1196 unsigned int size;
1198 int is_in = is_attr(var->attrs, ATTR_IN);
1199 int is_out = is_attr(var->attrs, ATTR_OUT);
1201 if (!is_in && !is_out) is_in = TRUE;
1203 if (type_get_type(var->declspec.type) == TYPE_BASIC ||
1204 type_get_type(var->declspec.type) == TYPE_ENUM)
1206 unsigned char fc;
1208 if (is_return)
1209 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1210 else
1211 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1213 if (type_get_type(var->declspec.type) == TYPE_ENUM)
1215 fc = get_enum_fc(var->declspec.type);
1217 else
1219 fc = get_basic_fc_signed(var->declspec.type);
1221 if (fc == FC_BIND_PRIMITIVE)
1222 fc = FC_IGNORE;
1225 print_file(file, indent, "0x%02x, /* %s */\n",
1226 fc, string_of_type(fc));
1227 size = 2; /* includes param type prefix */
1229 else
1231 unsigned short offset = var->typestring_offset;
1233 if (!is_interpreted && is_array(var->declspec.type)
1234 && type_array_is_decl_as_ptr(var->declspec.type)
1235 && type_array_get_ptr_tfsoff(var->declspec.type))
1236 offset = var->declspec.type->typestring_offset;
1238 if (is_return)
1239 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1240 else if (is_in && is_out)
1241 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1242 else if (is_out)
1243 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1244 else
1245 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1247 size = get_stack_size( var, NULL );
1248 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1249 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1250 size = 4; /* includes param type prefix */
1252 return size;
1255 int is_interpreted_func( const type_t *iface, const var_t *func )
1257 const char *str;
1258 const var_t *var;
1259 const var_list_t *args = type_function_get_args( func->declspec.type );
1260 const type_t *ret_type = type_function_get_rettype( func->declspec.type );
1262 if (type_get_type( ret_type ) == TYPE_BASIC)
1264 switch (type_basic_get_type( ret_type ))
1266 case TYPE_BASIC_INT64:
1267 case TYPE_BASIC_HYPER:
1268 /* return value must fit in a long_ptr */
1269 if (pointer_size < 8) return 0;
1270 break;
1271 case TYPE_BASIC_FLOAT:
1272 case TYPE_BASIC_DOUBLE:
1273 /* floating point values can't be returned */
1274 return 0;
1275 default:
1276 break;
1279 if (get_stub_mode() != MODE_Oif && args)
1281 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1282 switch (type_get_type( var->declspec.type ))
1284 case TYPE_BASIC:
1285 switch (type_basic_get_type( var->declspec.type ))
1287 /* floating point arguments are not supported in Oi mode */
1288 case TYPE_BASIC_FLOAT: return 0;
1289 case TYPE_BASIC_DOUBLE: return 0;
1290 default: break;
1292 break;
1293 /* unions passed by value are not supported in Oi mode */
1294 case TYPE_UNION: return 0;
1295 case TYPE_ENCAPSULATED_UNION: return 0;
1296 default: break;
1300 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1301 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1302 return (get_stub_mode() != MODE_Os);
1305 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1306 const var_t *func, unsigned int *offset,
1307 unsigned short num_proc )
1309 var_t *var;
1310 var_list_t *args = type_function_get_args( func->declspec.type );
1311 unsigned char explicit_fc, implicit_fc;
1312 unsigned char handle_flags;
1313 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1314 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1315 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1316 unsigned int nb_args = 0;
1317 unsigned int stack_size = 0;
1318 unsigned short param_num = 0;
1319 unsigned short handle_stack_offset = 0;
1320 unsigned short handle_param_num = 0;
1322 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1323 if (is_object( iface ))
1325 oi_flags |= Oi_OBJECT_PROC;
1326 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1327 stack_size += pointer_size;
1330 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1332 if (var == handle_var)
1334 handle_stack_offset = stack_size;
1335 handle_param_num = param_num;
1337 stack_size += get_stack_size( var, NULL );
1338 param_num++;
1339 nb_args++;
1341 if (!is_void( type_function_get_rettype( func->declspec.type )))
1343 stack_size += pointer_size;
1344 nb_args++;
1347 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1348 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1349 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1350 print_file( file, indent, "0x%02x,\n", oi_flags );
1351 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1352 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1353 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1354 *offset += 10;
1356 if (!implicit_fc)
1358 switch (explicit_fc)
1360 case FC_BIND_PRIMITIVE:
1361 handle_flags = 0;
1362 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1363 print_file( file, indent, "0x%02x,\n", handle_flags );
1364 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1365 handle_stack_offset, handle_stack_offset );
1366 *offset += 4;
1367 break;
1368 case FC_BIND_GENERIC:
1369 handle_flags = type_memsize( handle_var->declspec.type );
1370 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1371 print_file( file, indent, "0x%02x,\n", handle_flags );
1372 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1373 handle_stack_offset, handle_stack_offset );
1374 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
1375 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1376 *offset += 6;
1377 break;
1378 case FC_BIND_CONTEXT:
1379 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
1380 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1381 print_file( file, indent, "0x%02x,\n", handle_flags );
1382 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1383 handle_stack_offset, handle_stack_offset );
1384 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
1385 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1386 *offset += 6;
1387 break;
1391 if (get_stub_mode() == MODE_Oif)
1393 unsigned char oi2_flags = get_func_oi2_flags( func );
1394 unsigned char ext_flags = 0;
1395 unsigned int size;
1397 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1398 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1399 if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
1401 size = get_function_buffer_size( func, PASS_IN );
1402 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1403 size = get_function_buffer_size( func, PASS_OUT );
1404 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1405 print_file( file, indent, "0x%02x,\n", oi2_flags );
1406 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1407 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1408 print_file( file, indent, "0x%02x,\n", ext_flags );
1409 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1410 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1411 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1412 *offset += 14;
1413 if (pointer_size == 8)
1415 unsigned short pos = 0, fpu_mask = 0;
1417 if (is_object( iface )) pos += 2;
1418 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1420 if (type_get_type( var->declspec.type ) == TYPE_BASIC)
1422 switch (type_basic_get_type( var->declspec.type ))
1424 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1425 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1426 default: break;
1429 pos += 2;
1430 if (pos >= 16) break;
1432 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1433 *offset += 2;
1438 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1439 const var_t *func, unsigned int *offset,
1440 unsigned short num_proc )
1442 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1443 int is_interpreted = is_interpreted_func( iface, func );
1444 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1445 var_t *retval = type_function_get_retval( func->declspec.type );
1447 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1449 /* emit argument data */
1450 if (type_function_get_args(func->declspec.type))
1452 const var_t *var;
1453 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
1455 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1456 if (is_new_style)
1457 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1458 else
1459 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1463 /* emit return value data */
1464 if (is_void(retval->declspec.type))
1466 if (!is_new_style)
1468 print_file(file, 0, "/* %u (void) */\n", *offset);
1469 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1470 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1471 *offset += 2;
1474 else
1476 print_file( file, 0, "/* %u (return value) */\n", *offset );
1477 if (is_new_style)
1478 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1479 else
1480 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1484 static void for_each_iface(const statement_list_t *stmts,
1485 void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
1486 type_pred_t pred, FILE *file, int indent, unsigned int *offset)
1488 const statement_t *stmt;
1489 type_t *iface;
1491 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1493 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
1494 continue;
1495 iface = stmt->u.type;
1496 if (!pred(iface)) continue;
1497 proc(iface, file, indent, offset);
1498 if (type_iface_get_async_iface(iface))
1499 proc(type_iface_get_async_iface(iface), file, indent, offset);
1503 static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
1505 const statement_t *stmt;
1506 const type_t *parent = type_iface_get_inherit( iface );
1507 int count = parent ? count_methods( parent ) : 0;
1509 STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
1511 var_t *func = stmt->u.var;
1512 if (is_local(func->attrs)) continue;
1513 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1517 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1519 int indent = 0;
1520 unsigned int offset = 0;
1522 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1523 print_file(file, indent, "{\n");
1524 indent++;
1525 print_file(file, indent, "0,\n");
1526 print_file(file, indent, "{\n");
1527 indent++;
1529 for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
1531 print_file(file, indent, "0x0\n");
1532 indent--;
1533 print_file(file, indent, "}\n");
1534 indent--;
1535 print_file(file, indent, "};\n");
1536 print_file(file, indent, "\n");
1539 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1541 const statement_t *stmt;
1542 int indent = 0;
1544 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1545 iface->name );
1546 print_file( file, indent, "{\n" );
1547 indent++;
1548 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1550 var_t *func = stmt->u.var;
1551 if (is_local( func->attrs )) continue;
1552 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1554 indent--;
1555 print_file( file, indent, "};\n\n" );
1558 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1560 unsigned char fc;
1562 if (type_get_type(type) == TYPE_BASIC)
1563 fc = get_basic_fc_signed(type);
1564 else if (type_get_type(type) == TYPE_ENUM)
1565 fc = get_enum_fc(type);
1566 else
1567 return 0;
1569 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1570 *typestring_offset += 1;
1571 return 1;
1574 /* write conformance / variance descriptor */
1575 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1576 unsigned int baseoff, const type_t *type,
1577 const expr_t *expr)
1579 unsigned char operator_type = 0;
1580 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1581 const char *conftype_string = "field";
1582 const expr_t *subexpr;
1583 const type_t *iface = NULL;
1584 const char *name;
1586 if (!expr)
1588 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1589 return 4;
1592 if (expr->is_const)
1594 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1595 error("write_conf_or_var_desc: constant value %d is greater than "
1596 "the maximum constant size of %d\n", expr->cval,
1597 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1599 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1600 FC_CONSTANT_CONFORMANCE, expr->cval);
1601 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1602 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1604 return 4;
1607 if (!cont_type) /* top-level conformance */
1609 conftype = FC_TOP_LEVEL_CONFORMANCE;
1610 conftype_string = "parameter";
1611 cont_type = current_func->declspec.type;
1612 name = current_func->name;
1613 iface = current_iface;
1615 else
1617 name = cont_type->name;
1618 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1620 conftype = FC_POINTER_CONFORMANCE;
1621 conftype_string = "field pointer";
1625 subexpr = expr;
1626 switch (subexpr->type)
1628 case EXPR_PPTR:
1629 subexpr = subexpr->ref;
1630 operator_type = FC_DEREFERENCE;
1631 break;
1632 case EXPR_DIV:
1633 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1635 subexpr = subexpr->ref;
1636 operator_type = FC_DIV_2;
1638 break;
1639 case EXPR_MUL:
1640 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1642 subexpr = subexpr->ref;
1643 operator_type = FC_MULT_2;
1645 break;
1646 case EXPR_SUB:
1647 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1649 subexpr = subexpr->ref;
1650 operator_type = FC_SUB_1;
1652 break;
1653 case EXPR_ADD:
1654 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1656 subexpr = subexpr->ref;
1657 operator_type = FC_ADD_1;
1659 break;
1660 default:
1661 break;
1664 if (subexpr->type == EXPR_IDENTIFIER)
1666 const type_t *correlation_variable = NULL;
1667 unsigned char param_type = 0;
1668 unsigned int offset = 0;
1669 const var_t *var;
1670 struct expr_loc expr_loc;
1672 if (type_get_type(cont_type) == TYPE_FUNCTION)
1674 var_list_t *args = type_function_get_args( cont_type );
1676 if (is_object( iface )) offset += pointer_size;
1677 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1679 if (var->name && !strcmp(var->name, subexpr->u.sval))
1681 expr_loc.v = var;
1682 correlation_variable = var->declspec.type;
1683 break;
1685 offset += get_stack_size( var, NULL );
1688 else
1690 var_list_t *fields = type_struct_get_fields( cont_type );
1692 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1694 unsigned int size = field_memsize( var->declspec.type, &offset );
1695 if (var->name && !strcmp(var->name, subexpr->u.sval))
1697 expr_loc.v = var;
1698 correlation_variable = var->declspec.type;
1699 break;
1701 offset += size;
1705 if (!correlation_variable)
1706 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1707 expr_loc.attr = NULL;
1708 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1710 offset -= baseoff;
1712 if (type_get_type(correlation_variable) == TYPE_BASIC)
1714 switch (get_basic_fc(correlation_variable))
1716 case FC_CHAR:
1717 case FC_SMALL:
1718 param_type = FC_SMALL;
1719 break;
1720 case FC_BYTE:
1721 case FC_USMALL:
1722 param_type = FC_USMALL;
1723 break;
1724 case FC_WCHAR:
1725 case FC_SHORT:
1726 param_type = FC_SHORT;
1727 break;
1728 case FC_USHORT:
1729 param_type = FC_USHORT;
1730 break;
1731 case FC_LONG:
1732 param_type = FC_LONG;
1733 break;
1734 case FC_ULONG:
1735 param_type = FC_ULONG;
1736 break;
1737 default:
1738 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1739 get_basic_fc(correlation_variable));
1742 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1744 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1745 param_type = FC_LONG;
1746 else
1747 param_type = FC_SHORT;
1749 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1751 if (pointer_size == 8)
1752 param_type = FC_HYPER;
1753 else
1754 param_type = FC_LONG;
1756 else
1758 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1759 subexpr->u.sval);
1760 return 0;
1763 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1764 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1765 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1766 operator_type ? string_of_type(operator_type) : "no operators");
1767 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1768 (unsigned short)offset, offset);
1770 else if (!iface || is_interpreted_func( iface, current_func ))
1772 unsigned int callback_offset = 0;
1773 struct expr_eval_routine *eval;
1774 int found = 0;
1776 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1778 if (eval->cont_type == cont_type ||
1779 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1780 eval->iface == iface &&
1781 eval->name && name && !strcmp(eval->name, name) &&
1782 !compare_expr(eval->expr, expr)))
1784 found = 1;
1785 break;
1787 callback_offset++;
1790 if (!found)
1792 eval = xmalloc (sizeof(*eval));
1793 eval->iface = iface;
1794 eval->cont_type = cont_type;
1795 eval->name = xstrdup( name );
1796 eval->baseoff = baseoff;
1797 eval->expr = expr;
1798 list_add_tail (&expr_eval_routines, &eval->entry);
1801 if (callback_offset > USHRT_MAX)
1802 error("Maximum number of callback routines reached\n");
1804 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1805 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1806 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1808 else /* output a dummy corr desc that isn't used */
1810 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1811 print_file(file, 2, "0x0,\n" );
1812 print_file(file, 2, "NdrFcShort(0x0),\n" );
1814 return 4;
1817 /* return size and start offset of a data field based on current offset */
1818 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1820 unsigned int align = 0;
1821 unsigned int size = type_memsize_and_alignment( type, &align );
1823 *offset = ROUND_SIZE( *offset, align );
1824 return size;
1827 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1829 unsigned int size = 0;
1830 unsigned int max_align;
1831 const var_t *v;
1833 if (!fields) return 0;
1834 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1836 unsigned int falign = 0;
1837 unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
1838 if (*align < falign) *align = falign;
1839 falign = clamp_align(falign);
1840 size = ROUND_SIZE(size, falign);
1841 size += fsize;
1844 max_align = clamp_align(*align);
1845 size = ROUND_SIZE(size, max_align);
1847 return size;
1850 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1852 unsigned int size, maxs = 0;
1853 unsigned int align = *pmaxa;
1854 const var_t *v;
1856 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1858 /* we could have an empty default field with NULL type */
1859 if (v->declspec.type)
1861 size = type_memsize_and_alignment(v->declspec.type, &align);
1862 if (maxs < size) maxs = size;
1863 if (*pmaxa < align) *pmaxa = align;
1867 return maxs;
1870 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1872 unsigned int size = 0;
1874 switch (type_get_type(t))
1876 case TYPE_BASIC:
1877 switch (get_basic_fc(t))
1879 case FC_BYTE:
1880 case FC_CHAR:
1881 case FC_USMALL:
1882 case FC_SMALL:
1883 size = 1;
1884 if (size > *align) *align = size;
1885 break;
1886 case FC_WCHAR:
1887 case FC_USHORT:
1888 case FC_SHORT:
1889 size = 2;
1890 if (size > *align) *align = size;
1891 break;
1892 case FC_ULONG:
1893 case FC_LONG:
1894 case FC_ERROR_STATUS_T:
1895 case FC_FLOAT:
1896 size = 4;
1897 if (size > *align) *align = size;
1898 break;
1899 case FC_HYPER:
1900 case FC_DOUBLE:
1901 size = 8;
1902 if (size > *align) *align = size;
1903 break;
1904 case FC_INT3264:
1905 case FC_UINT3264:
1906 case FC_BIND_PRIMITIVE:
1907 assert( pointer_size );
1908 size = pointer_size;
1909 if (size > *align) *align = size;
1910 break;
1911 default:
1912 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1913 size = 0;
1915 break;
1916 case TYPE_ENUM:
1917 switch (get_enum_fc(t))
1919 case FC_ENUM16:
1920 case FC_ENUM32:
1921 size = 4;
1922 if (size > *align) *align = size;
1923 break;
1924 default:
1925 error("type_memsize: Unknown enum type\n");
1926 size = 0;
1928 break;
1929 case TYPE_STRUCT:
1930 size = fields_memsize(type_struct_get_fields(t), align);
1931 break;
1932 case TYPE_ENCAPSULATED_UNION:
1933 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1934 break;
1935 case TYPE_UNION:
1936 size = union_memsize(type_union_get_cases(t), align);
1937 break;
1938 case TYPE_POINTER:
1939 case TYPE_INTERFACE:
1940 assert( pointer_size );
1941 size = pointer_size;
1942 if (size > *align) *align = size;
1943 break;
1944 case TYPE_ARRAY:
1945 if (!type_array_is_decl_as_ptr(t))
1947 if (is_conformant_array(t))
1949 type_memsize_and_alignment(type_array_get_element_type(t), align);
1950 size = 0;
1952 else
1953 size = type_array_get_dim(t) *
1954 type_memsize_and_alignment(type_array_get_element_type(t), align);
1956 else /* declared as a pointer */
1958 assert( pointer_size );
1959 size = pointer_size;
1960 if (size > *align) *align = size;
1962 break;
1963 case TYPE_ALIAS:
1964 case TYPE_VOID:
1965 case TYPE_COCLASS:
1966 case TYPE_MODULE:
1967 case TYPE_FUNCTION:
1968 case TYPE_BITFIELD:
1969 /* these types should not be encountered here due to language
1970 * restrictions (interface, void, coclass, module), logical
1971 * restrictions (alias - due to type_get_type call above) or
1972 * checking restrictions (function, bitfield). */
1973 assert(0);
1976 return size;
1979 unsigned int type_memsize(const type_t *t)
1981 unsigned int align = 0;
1982 return type_memsize_and_alignment( t, &align );
1985 static unsigned int type_buffer_alignment(const type_t *t)
1987 const var_list_t *fields;
1988 const var_t *var;
1989 unsigned int max = 0, align;
1991 switch (type_get_type(t))
1993 case TYPE_BASIC:
1994 switch (get_basic_fc(t))
1996 case FC_BYTE:
1997 case FC_CHAR:
1998 case FC_USMALL:
1999 case FC_SMALL:
2000 return 1;
2001 case FC_WCHAR:
2002 case FC_USHORT:
2003 case FC_SHORT:
2004 return 2;
2005 case FC_ULONG:
2006 case FC_LONG:
2007 case FC_ERROR_STATUS_T:
2008 case FC_FLOAT:
2009 case FC_INT3264:
2010 case FC_UINT3264:
2011 return 4;
2012 case FC_HYPER:
2013 case FC_DOUBLE:
2014 return 8;
2015 default:
2016 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
2018 break;
2019 case TYPE_ENUM:
2020 switch (get_enum_fc(t))
2022 case FC_ENUM16:
2023 return 2;
2024 case FC_ENUM32:
2025 return 4;
2026 default:
2027 error("type_buffer_alignment: Unknown enum type\n");
2029 break;
2030 case TYPE_STRUCT:
2031 if (!(fields = type_struct_get_fields(t))) break;
2032 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2034 if (!var->declspec.type) continue;
2035 align = type_buffer_alignment( var->declspec.type );
2036 if (max < align) max = align;
2038 break;
2039 case TYPE_ENCAPSULATED_UNION:
2040 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2041 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2043 if (!var->declspec.type) continue;
2044 align = type_buffer_alignment( var->declspec.type );
2045 if (max < align) max = align;
2047 break;
2048 case TYPE_UNION:
2049 if (!(fields = type_union_get_cases(t))) break;
2050 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2052 if (!var->declspec.type) continue;
2053 align = type_buffer_alignment( var->declspec.type );
2054 if (max < align) max = align;
2056 break;
2057 case TYPE_ARRAY:
2058 if (!type_array_is_decl_as_ptr(t))
2059 return type_buffer_alignment( type_array_get_element_type(t) );
2060 /* else fall through */
2061 case TYPE_POINTER:
2062 return 4;
2063 case TYPE_INTERFACE:
2064 case TYPE_ALIAS:
2065 case TYPE_VOID:
2066 case TYPE_COCLASS:
2067 case TYPE_MODULE:
2068 case TYPE_FUNCTION:
2069 case TYPE_BITFIELD:
2070 /* these types should not be encountered here due to language
2071 * restrictions (interface, void, coclass, module), logical
2072 * restrictions (alias - due to type_get_type call above) or
2073 * checking restrictions (function, bitfield). */
2074 assert(0);
2076 return max;
2079 int is_full_pointer_function(const var_t *func)
2081 const var_t *var;
2082 if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
2083 return TRUE;
2084 if (!type_function_get_args(func->declspec.type))
2085 return FALSE;
2086 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
2087 if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
2088 return TRUE;
2089 return FALSE;
2092 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2094 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2095 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2096 fprintf(file, "\n");
2099 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2101 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2102 fprintf(file, "\n");
2105 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2106 const type_t *type,
2107 enum type_context context,
2108 unsigned int offset,
2109 unsigned int *typeformat_offset)
2111 unsigned int start_offset = *typeformat_offset;
2112 short reloff = offset - (*typeformat_offset + 2);
2113 int in_attr, out_attr;
2114 int pointer_type;
2115 unsigned char flags = 0;
2117 pointer_type = get_pointer_fc_context(type, attrs, context);
2119 in_attr = is_attr(attrs, ATTR_IN);
2120 out_attr = is_attr(attrs, ATTR_OUT);
2121 if (!in_attr && !out_attr) in_attr = 1;
2123 if (!is_interpreted_func(current_iface, current_func))
2125 if (out_attr && !in_attr && pointer_type == FC_RP)
2126 flags |= FC_ALLOCED_ON_STACK;
2128 else if (get_stub_mode() == MODE_Oif)
2130 if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
2132 switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
2134 case TGT_STRING:
2135 case TGT_POINTER:
2136 case TGT_CTXT_HANDLE:
2137 case TGT_CTXT_HANDLE_POINTER:
2138 case TGT_ARRAY:
2139 flags |= FC_ALLOCED_ON_STACK;
2140 break;
2141 case TGT_IFACE_POINTER:
2142 if (in_attr && out_attr)
2143 flags |= FC_ALLOCED_ON_STACK;
2144 break;
2145 default:
2146 break;
2151 if (is_ptr(type))
2153 type_t *ref = type_pointer_get_ref_type(type);
2154 if(is_declptr(ref) && !is_user_type(ref))
2155 flags |= FC_POINTER_DEREF;
2156 if (pointer_type != FC_RP) {
2157 flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
2161 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2162 pointer_type,
2163 flags,
2164 string_of_type(pointer_type));
2165 if (file)
2167 if (flags & FC_ALLOCED_ON_STACK)
2168 fprintf(file, " [allocated_on_stack]");
2169 if (flags & FC_POINTER_DEREF)
2170 fprintf(file, " [pointer_deref]");
2171 if (flags & FC_DONT_FREE)
2172 fprintf(file, " [dont_free]");
2173 if (flags & FC_ALLOCATE_ALL_NODES)
2174 fprintf(file, " [all_nodes]");
2175 fprintf(file, " */\n");
2178 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2179 *typeformat_offset += 4;
2181 return start_offset;
2184 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2185 const type_t *type, enum type_context context)
2187 unsigned char fc;
2188 unsigned char pointer_fc;
2189 const type_t *ref;
2190 int in_attr = is_attr(attrs, ATTR_IN);
2191 int out_attr = is_attr(attrs, ATTR_OUT);
2192 unsigned char flags = FC_SIMPLE_POINTER;
2194 /* for historical reasons, write_simple_pointer also handled string types,
2195 * but no longer does. catch bad uses of the function with this check */
2196 if (is_string_type(attrs, type))
2197 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2199 pointer_fc = get_pointer_fc_context(type, attrs, context);
2201 ref = type_pointer_get_ref_type(type);
2202 if (type_get_type(ref) == TYPE_ENUM)
2203 fc = get_enum_fc(ref);
2204 else
2205 fc = get_basic_fc(ref);
2207 if (!is_interpreted_func(current_iface, current_func))
2209 if (out_attr && !in_attr && pointer_fc == FC_RP)
2210 flags |= FC_ALLOCED_ON_STACK;
2212 else if (get_stub_mode() == MODE_Oif)
2214 if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
2215 flags |= FC_ALLOCED_ON_STACK;
2218 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2219 pointer_fc, flags, string_of_type(pointer_fc),
2220 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2221 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2222 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2223 return 4;
2226 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2228 const decl_spec_t ds = {.type = t};
2229 print_file(file, 0, "/* %u (", tfsoff);
2230 write_type_decl(file, &ds, NULL);
2231 print_file(file, 0, ") */\n");
2234 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2235 type_t *type, unsigned int ref_offset,
2236 enum type_context context,
2237 unsigned int *typestring_offset)
2239 unsigned int offset = *typestring_offset;
2240 type_t *ref = type_pointer_get_ref_type(type);
2242 print_start_tfs_comment(file, type, offset);
2243 update_tfsoff(type, offset, file);
2245 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2247 case TGT_BASIC:
2248 case TGT_ENUM:
2249 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2250 break;
2251 default:
2252 if (ref_offset)
2253 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2254 break;
2257 return offset;
2260 static int processed(const type_t *type)
2262 return type->typestring_offset && !type->tfswrite;
2265 static int user_type_has_variable_size(const type_t *t)
2267 if (is_ptr(t))
2268 return TRUE;
2269 else if (type_get_type(t) == TYPE_STRUCT)
2271 switch (get_struct_fc(t))
2273 case FC_PSTRUCT:
2274 case FC_CSTRUCT:
2275 case FC_CPSTRUCT:
2276 case FC_CVSTRUCT:
2277 return TRUE;
2280 /* Note: Since this only applies to user types, we can't have a conformant
2281 array here, and strings should get filed under pointer in this case. */
2282 return FALSE;
2285 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2287 unsigned int start, absoff, flags;
2288 const char *name = NULL;
2289 type_t *utype = get_user_type(type, &name);
2290 unsigned int usize = type_memsize(utype);
2291 unsigned int ualign = type_buffer_alignment(utype);
2292 unsigned int size = type_memsize(type);
2293 unsigned short funoff = user_type_offset(name);
2294 short reloff;
2296 if (processed(type)) return type->typestring_offset;
2298 guard_rec(type);
2300 if(user_type_has_variable_size(utype)) usize = 0;
2302 if (type_get_type(utype) == TYPE_BASIC ||
2303 type_get_type(utype) == TYPE_ENUM)
2305 unsigned char fc;
2307 if (type_get_type(utype) == TYPE_ENUM)
2308 fc = get_enum_fc(utype);
2309 else
2310 fc = get_basic_fc(utype);
2312 absoff = *tfsoff;
2313 print_start_tfs_comment(file, utype, absoff);
2314 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2315 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2316 *tfsoff += 2;
2318 else
2320 if (!processed(utype))
2321 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2322 absoff = utype->typestring_offset;
2325 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2326 flags = 0x40;
2327 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2328 flags = 0x80;
2329 else
2330 flags = 0;
2332 start = *tfsoff;
2333 update_tfsoff(type, start, file);
2334 print_start_tfs_comment(file, type, start);
2335 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2336 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2337 flags | (ualign - 1), ualign - 1, flags);
2338 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2339 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2340 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2341 *tfsoff += 8;
2342 reloff = absoff - *tfsoff;
2343 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2344 *tfsoff += 2;
2345 return start;
2348 static void write_member_type(FILE *file, const type_t *cont,
2349 int cont_is_complex, const attr_list_t *attrs,
2350 const type_t *type, unsigned int *corroff,
2351 unsigned int *tfsoff)
2353 if (is_embedded_complex(type) && !is_conformant_array(type))
2355 unsigned int absoff;
2356 short reloff;
2358 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2360 absoff = *corroff;
2361 *corroff += 8;
2363 else
2365 absoff = type->typestring_offset;
2367 reloff = absoff - (*tfsoff + 2);
2369 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2370 /* padding is represented using FC_STRUCTPAD* types, so presumably
2371 * this is left over in the format for historical purposes in MIDL
2372 * or rpcrt4. */
2373 print_file(file, 2, "0x0,\n");
2374 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2375 reloff, reloff, absoff);
2376 *tfsoff += 4;
2378 else if (is_ptr(type) || is_conformant_array(type))
2380 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2381 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2382 *tfsoff += 1;
2384 else if (!write_base_type(file, type, tfsoff))
2385 error("Unsupported member type %d\n", type_get_type(type));
2388 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2389 int cont_is_complex, unsigned int *tfsoff)
2391 type_t *elem = type_array_get_element_type(type);
2393 if (!is_embedded_complex(elem) && is_ptr(elem))
2395 type_t *ref = type_pointer_get_ref_type(elem);
2397 if (processed(ref))
2399 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2400 ref->typestring_offset, tfsoff);
2401 return;
2403 if (cont_is_complex && is_string_type(attrs, elem))
2405 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2406 return;
2408 if (!is_string_type(NULL, elem) &&
2409 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2411 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2412 return;
2415 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2418 static void write_end(FILE *file, unsigned int *tfsoff)
2420 if (*tfsoff % 2 == 0)
2422 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2423 *tfsoff += 1;
2425 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2426 *tfsoff += 1;
2429 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2431 unsigned int offset = 0;
2432 var_list_t *fs = type_struct_get_fields(type);
2433 var_t *f;
2435 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2437 type_t *ft = f->declspec.type;
2438 unsigned int size = field_memsize( ft, &offset );
2439 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2441 short reloff;
2442 unsigned int absoff = ft->typestring_offset;
2443 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2444 absoff += 8; /* we already have a corr descr, skip it */
2445 reloff = absoff - (*tfsoff + 6);
2446 print_file(file, 0, "/* %d */\n", *tfsoff);
2447 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2448 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2449 write_conf_or_var_desc(file, current_structure, offset, ft,
2450 get_attrp(f->attrs, ATTR_SWITCHIS));
2451 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2452 (unsigned short)reloff, reloff, absoff);
2453 *tfsoff += 8;
2455 offset += size;
2459 static int write_pointer_description_offsets(
2460 FILE *file, const attr_list_t *attrs, type_t *type,
2461 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2462 unsigned int *typestring_offset)
2464 int written = 0;
2466 if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
2467 (is_array(type) && type_array_is_decl_as_ptr(type)))
2469 if (offset_in_memory && offset_in_buffer)
2471 unsigned int memsize;
2473 /* pointer instance
2475 * note that MSDN states that for pointer layouts in structures,
2476 * this is a negative offset from the end of the structure, but
2477 * this statement is incorrect. all offsets are positive */
2478 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2479 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2481 memsize = type_memsize(type);
2482 *offset_in_memory += memsize;
2483 /* increment these separately as in the case of conformant (varying)
2484 * structures these start at different values */
2485 *offset_in_buffer += memsize;
2487 *typestring_offset += 4;
2489 if (is_ptr(type))
2491 type_t *ref = type_pointer_get_ref_type(type);
2493 if (is_string_type(attrs, type))
2494 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2495 else if (processed(ref))
2496 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2497 ref->typestring_offset, typestring_offset);
2498 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2499 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2500 else
2501 error("write_pointer_description_offsets: type format string unknown\n");
2503 else
2505 unsigned int offset = type->typestring_offset;
2506 /* skip over the pointer that is written for strings, since a
2507 * pointer has to be written in-place here */
2508 if (is_string_type(attrs, type))
2509 offset += 4;
2510 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2513 return 1;
2516 if (is_array(type))
2518 return write_pointer_description_offsets(
2519 file, attrs, type_array_get_element_type(type), offset_in_memory,
2520 offset_in_buffer, typestring_offset);
2522 else if (is_non_complex_struct(type))
2524 /* otherwise search for interesting fields to parse */
2525 const var_t *v;
2526 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2528 if (offset_in_memory && offset_in_buffer)
2530 unsigned int padding;
2531 unsigned int align = 0;
2532 type_memsize_and_alignment(v->declspec.type, &align);
2533 padding = ROUNDING(*offset_in_memory, align);
2534 *offset_in_memory += padding;
2535 *offset_in_buffer += padding;
2537 written += write_pointer_description_offsets(
2538 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2539 typestring_offset);
2542 else
2544 if (offset_in_memory && offset_in_buffer)
2546 unsigned int memsize = type_memsize(type);
2547 *offset_in_memory += memsize;
2548 /* increment these separately as in the case of conformant (varying)
2549 * structures these start at different values */
2550 *offset_in_buffer += memsize;
2554 return written;
2557 static int write_no_repeat_pointer_descriptions(
2558 FILE *file, const attr_list_t *attrs, type_t *type,
2559 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2560 unsigned int *typestring_offset)
2562 int written = 0;
2564 if (is_ptr(type) ||
2565 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2567 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2568 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2569 *typestring_offset += 2;
2571 return write_pointer_description_offsets(file, attrs, type,
2572 offset_in_memory, offset_in_buffer, typestring_offset);
2575 if (is_non_complex_struct(type))
2577 const var_t *v;
2578 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2580 if (offset_in_memory && offset_in_buffer)
2582 unsigned int padding;
2583 unsigned int align = 0;
2584 type_memsize_and_alignment(v->declspec.type, &align);
2585 padding = ROUNDING(*offset_in_memory, align);
2586 *offset_in_memory += padding;
2587 *offset_in_buffer += padding;
2589 written += write_no_repeat_pointer_descriptions(
2590 file, v->attrs, v->declspec.type,
2591 offset_in_memory, offset_in_buffer, typestring_offset);
2594 else
2596 unsigned int memsize = type_memsize(type);
2597 *offset_in_memory += memsize;
2598 /* increment these separately as in the case of conformant (varying)
2599 * structures these start at different values */
2600 *offset_in_buffer += memsize;
2603 return written;
2606 /* Note: if file is NULL return value is number of pointers to write, else
2607 * it is the number of type format characters written */
2608 static int write_fixed_array_pointer_descriptions(
2609 FILE *file, const attr_list_t *attrs, type_t *type,
2610 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2611 unsigned int *typestring_offset)
2613 int pointer_count = 0;
2615 if (type_get_type(type) == TYPE_ARRAY &&
2616 !type_array_has_conformance(type) && !type_array_has_variance(type))
2618 unsigned int temp = 0;
2619 /* unfortunately, this needs to be done in two passes to avoid
2620 * writing out redundant FC_FIXED_REPEAT descriptions */
2621 pointer_count = write_pointer_description_offsets(
2622 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2623 if (pointer_count > 0)
2625 unsigned int increment_size;
2626 unsigned int offset_of_array_pointer_mem = 0;
2627 unsigned int offset_of_array_pointer_buf = 0;
2629 increment_size = type_memsize(type_array_get_element_type(type));
2631 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2632 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2633 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2634 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2635 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2636 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2637 *typestring_offset += 10;
2639 pointer_count = write_pointer_description_offsets(
2640 file, attrs, type, &offset_of_array_pointer_mem,
2641 &offset_of_array_pointer_buf, typestring_offset);
2644 else if (type_get_type(type) == TYPE_STRUCT)
2646 const var_t *v;
2647 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2649 if (offset_in_memory && offset_in_buffer)
2651 unsigned int padding;
2652 unsigned int align = 0;
2653 type_memsize_and_alignment(v->declspec.type, &align);
2654 padding = ROUNDING(*offset_in_memory, align);
2655 *offset_in_memory += padding;
2656 *offset_in_buffer += padding;
2658 pointer_count += write_fixed_array_pointer_descriptions(
2659 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2660 typestring_offset);
2663 else
2665 if (offset_in_memory && offset_in_buffer)
2667 unsigned int memsize;
2668 memsize = type_memsize(type);
2669 *offset_in_memory += memsize;
2670 /* increment these separately as in the case of conformant (varying)
2671 * structures these start at different values */
2672 *offset_in_buffer += memsize;
2676 return pointer_count;
2679 /* Note: if file is NULL return value is number of pointers to write, else
2680 * it is the number of type format characters written */
2681 static int write_conformant_array_pointer_descriptions(
2682 FILE *file, const attr_list_t *attrs, type_t *type,
2683 unsigned int offset_in_memory, unsigned int *typestring_offset)
2685 int pointer_count = 0;
2687 if (is_conformant_array(type) && !type_array_has_variance(type))
2689 unsigned int temp = 0;
2690 /* unfortunately, this needs to be done in two passes to avoid
2691 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2692 pointer_count = write_pointer_description_offsets(
2693 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2694 if (pointer_count > 0)
2696 unsigned int increment_size;
2697 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2698 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2700 increment_size = type_memsize(type_array_get_element_type(type));
2702 if (increment_size > USHRT_MAX)
2703 error("array size of %u bytes is too large\n", increment_size);
2705 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2706 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2707 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2708 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2709 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2710 *typestring_offset += 8;
2712 pointer_count = write_pointer_description_offsets(
2713 file, attrs, type_array_get_element_type(type),
2714 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2715 typestring_offset);
2719 return pointer_count;
2722 /* Note: if file is NULL return value is number of pointers to write, else
2723 * it is the number of type format characters written */
2724 static int write_varying_array_pointer_descriptions(
2725 FILE *file, const attr_list_t *attrs, type_t *type,
2726 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2727 unsigned int *typestring_offset)
2729 int pointer_count = 0;
2731 if (is_array(type) && type_array_has_variance(type))
2733 unsigned int temp = 0;
2734 /* unfortunately, this needs to be done in two passes to avoid
2735 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2736 pointer_count = write_pointer_description_offsets(
2737 NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
2738 if (pointer_count > 0)
2740 unsigned int increment_size;
2742 increment_size = type_memsize(type_array_get_element_type(type));
2744 if (increment_size > USHRT_MAX)
2745 error("array size of %u bytes is too large\n", increment_size);
2747 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2748 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2749 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2750 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2751 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2752 *typestring_offset += 8;
2754 pointer_count = write_pointer_description_offsets(
2755 file, attrs, type_array_get_element_type(type), offset_in_memory,
2756 offset_in_buffer, typestring_offset);
2759 else if (type_get_type(type) == TYPE_STRUCT)
2761 const var_t *v;
2762 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2764 if (offset_in_memory && offset_in_buffer)
2766 unsigned int align = 0, padding;
2768 if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
2770 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2771 /* skip over variance and offset in buffer */
2772 *offset_in_buffer += 8;
2775 type_memsize_and_alignment(v->declspec.type, &align);
2776 padding = ROUNDING(*offset_in_memory, align);
2777 *offset_in_memory += padding;
2778 *offset_in_buffer += padding;
2780 pointer_count += write_varying_array_pointer_descriptions(
2781 file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
2782 typestring_offset);
2785 else
2787 if (offset_in_memory && offset_in_buffer)
2789 unsigned int memsize = type_memsize(type);
2790 *offset_in_memory += memsize;
2791 /* increment these separately as in the case of conformant (varying)
2792 * structures these start at different values */
2793 *offset_in_buffer += memsize;
2797 return pointer_count;
2800 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2801 unsigned int *typestring_offset)
2803 unsigned int offset_in_buffer;
2804 unsigned int offset_in_memory;
2806 /* pass 1: search for single instance of a pointer (i.e. don't descend
2807 * into arrays) */
2808 if (!is_array(type))
2810 offset_in_memory = 0;
2811 offset_in_buffer = 0;
2812 write_no_repeat_pointer_descriptions(
2813 file, NULL, type,
2814 &offset_in_memory, &offset_in_buffer, typestring_offset);
2817 /* pass 2: search for pointers in fixed arrays */
2818 offset_in_memory = 0;
2819 offset_in_buffer = 0;
2820 write_fixed_array_pointer_descriptions(
2821 file, NULL, type,
2822 &offset_in_memory, &offset_in_buffer, typestring_offset);
2824 /* pass 3: search for pointers in conformant only arrays (but don't descend
2825 * into conformant varying or varying arrays) */
2826 if (is_conformant_array(type) &&
2827 (type_array_is_decl_as_ptr(type) || !current_structure))
2828 write_conformant_array_pointer_descriptions(
2829 file, attrs, type, 0, typestring_offset);
2830 else if (type_get_type(type) == TYPE_STRUCT &&
2831 get_struct_fc(type) == FC_CPSTRUCT)
2833 type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
2834 write_conformant_array_pointer_descriptions( file, NULL, carray,
2835 type_memsize(type), typestring_offset);
2838 /* pass 4: search for pointers in varying arrays */
2839 offset_in_memory = 0;
2840 offset_in_buffer = 0;
2841 write_varying_array_pointer_descriptions(
2842 file, NULL, type,
2843 &offset_in_memory, &offset_in_buffer, typestring_offset);
2846 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2847 type_t *type, enum type_context context,
2848 const char *name, unsigned int *typestring_offset)
2850 unsigned int start_offset;
2851 unsigned char rtype;
2852 type_t *elem_type;
2853 int is_processed = processed(type);
2855 start_offset = *typestring_offset;
2857 if (is_declptr(type))
2859 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2860 int pointer_type = get_pointer_fc_context(type, attrs, context);
2861 if (!pointer_type)
2862 pointer_type = FC_RP;
2863 print_start_tfs_comment(file, type, *typestring_offset);
2864 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2865 pointer_type, flag, string_of_type(pointer_type),
2866 flag ? " [simple_pointer]" : "");
2867 *typestring_offset += 2;
2868 if (!flag)
2870 print_file(file, 2, "NdrFcShort(0x2),\n");
2871 *typestring_offset += 2;
2873 is_processed = FALSE;
2876 if (is_array(type))
2877 elem_type = type_array_get_element_type(type);
2878 else
2879 elem_type = type_pointer_get_ref_type(type);
2881 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2882 return write_array_tfs(file, attrs, type, name, typestring_offset);
2884 if (type_get_type(elem_type) != TYPE_BASIC)
2886 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2887 return start_offset;
2890 rtype = get_basic_fc(elem_type);
2891 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2893 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2894 return start_offset;
2897 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2899 unsigned int dim = type_array_get_dim(type);
2901 if (is_processed) return start_offset;
2903 /* FIXME: multi-dimensional array */
2904 if (0xffffu < dim)
2905 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2906 name, 0xffffu, dim - 0xffffu);
2908 if (rtype == FC_WCHAR)
2909 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2910 else
2911 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2912 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2913 *typestring_offset += 2;
2915 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2916 *typestring_offset += 2;
2918 update_tfsoff(type, start_offset, file);
2919 return start_offset;
2921 else if (is_conformant_array(type))
2923 if (rtype == FC_WCHAR)
2924 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2925 else
2926 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2927 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2928 *typestring_offset += 2;
2930 *typestring_offset += write_conf_or_var_desc(
2931 file, current_structure,
2932 (!type_array_is_decl_as_ptr(type) && current_structure
2933 ? type_memsize(current_structure)
2934 : 0),
2935 type, type_array_get_conformance(type));
2937 update_tfsoff(type, start_offset, file);
2938 return start_offset;
2940 else
2942 if (is_processed) return start_offset;
2944 if (rtype == FC_WCHAR)
2945 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2946 else
2947 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2948 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2949 *typestring_offset += 2;
2951 update_tfsoff(type, start_offset, file);
2952 return start_offset;
2956 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2957 const char *name, unsigned int *typestring_offset)
2959 const expr_t *length_is = type_array_get_variance(type);
2960 const expr_t *size_is = type_array_get_conformance(type);
2961 unsigned int align;
2962 unsigned int size;
2963 unsigned int start_offset;
2964 unsigned char fc;
2965 unsigned int baseoff
2966 = !type_array_is_decl_as_ptr(type) && current_structure
2967 ? type_memsize(current_structure)
2968 : 0;
2970 if (!is_string_type(attrs, type_array_get_element_type(type)))
2971 write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
2973 size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2974 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
2975 fc = get_array_fc(type);
2977 start_offset = *typestring_offset;
2978 update_tfsoff(type, start_offset, file);
2979 print_start_tfs_comment(file, type, start_offset);
2980 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2981 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2982 *typestring_offset += 2;
2984 align = 0;
2985 if (fc != FC_BOGUS_ARRAY)
2987 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
2989 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2990 *typestring_offset += 4;
2992 else
2994 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2995 *typestring_offset += 2;
2998 if (is_conformant_array(type))
2999 *typestring_offset
3000 += write_conf_or_var_desc(file, current_structure, baseoff,
3001 type, size_is);
3003 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
3005 unsigned int elsize = type_memsize(type_array_get_element_type(type));
3006 unsigned int dim = type_array_get_dim(type);
3008 if (fc == FC_LGVARRAY)
3010 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
3011 *typestring_offset += 4;
3013 else
3015 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3016 *typestring_offset += 2;
3019 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
3020 *typestring_offset += 2;
3023 if (length_is)
3024 *typestring_offset
3025 += write_conf_or_var_desc(file, current_structure, baseoff,
3026 type, length_is);
3028 if (type_has_pointers(type_array_get_element_type(type)) &&
3029 (type_array_is_decl_as_ptr(type) || !current_structure))
3031 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3032 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3033 *typestring_offset += 2;
3034 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
3035 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3036 *typestring_offset += 1;
3039 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
3040 write_end(file, typestring_offset);
3042 else
3044 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
3045 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
3046 *typestring_offset += 2;
3047 *typestring_offset
3048 += write_conf_or_var_desc(file, current_structure, baseoff,
3049 type, size_is);
3050 *typestring_offset
3051 += write_conf_or_var_desc(file, current_structure, baseoff,
3052 type, length_is);
3054 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
3055 write_end(file, typestring_offset);
3058 return start_offset;
3061 static const var_t *find_array_or_string_in_struct(const type_t *type)
3063 const var_list_t *fields = type_struct_get_fields(type);
3064 const var_t *last_field;
3065 const type_t *ft;
3067 if (!fields || list_empty(fields))
3068 return NULL;
3070 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3071 ft = last_field->declspec.type;
3073 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3074 return last_field;
3076 if (type_get_type(ft) == TYPE_STRUCT)
3077 return find_array_or_string_in_struct(ft);
3078 else
3079 return NULL;
3082 static void write_struct_members(FILE *file, const type_t *type,
3083 int is_complex, unsigned int *corroff,
3084 unsigned int *typestring_offset)
3086 const var_t *field;
3087 unsigned short offset = 0;
3088 unsigned int salign = 1;
3089 int padding;
3090 var_list_t *fields = type_struct_get_fields(type);
3092 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3094 type_t *ft = field->declspec.type;
3095 unsigned int align = 0;
3096 unsigned int size = type_memsize_and_alignment(ft, &align);
3097 align = clamp_align(align);
3098 if (salign < align) salign = align;
3100 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3102 if ((align - 1) & offset)
3104 unsigned char fc = 0;
3105 switch (align)
3107 case 2:
3108 fc = FC_ALIGNM2;
3109 break;
3110 case 4:
3111 fc = FC_ALIGNM4;
3112 break;
3113 case 8:
3114 fc = FC_ALIGNM8;
3115 break;
3116 default:
3117 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3119 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3120 offset = ROUND_SIZE(offset, align);
3121 *typestring_offset += 1;
3123 write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
3124 typestring_offset);
3125 offset += size;
3129 padding = ROUNDING(offset, salign);
3130 if (padding)
3132 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3133 FC_STRUCTPAD1 + padding - 1,
3134 padding);
3135 *typestring_offset += 1;
3138 write_end(file, typestring_offset);
3141 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3142 const char *name, unsigned int *tfsoff)
3144 const type_t *save_current_structure = current_structure;
3145 unsigned int total_size;
3146 const var_t *array;
3147 unsigned int start_offset;
3148 unsigned int align;
3149 unsigned int corroff;
3150 var_t *f;
3151 unsigned char fc = get_struct_fc(type);
3152 var_list_t *fields = type_struct_get_fields(type);
3154 if (processed(type)) return type->typestring_offset;
3156 guard_rec(type);
3157 current_structure = type;
3159 total_size = type_memsize(type);
3160 align = type_buffer_alignment(type);
3161 if (total_size > USHRT_MAX)
3162 error("structure size for %s exceeds %d bytes by %d bytes\n",
3163 name, USHRT_MAX, total_size - USHRT_MAX);
3165 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3166 write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
3168 array = find_array_or_string_in_struct(type);
3169 if (array && !processed(array->declspec.type))
3171 if(is_string_type(array->attrs, array->declspec.type))
3172 write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3173 else
3174 write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
3177 corroff = *tfsoff;
3178 write_descriptors(file, type, tfsoff);
3180 start_offset = *tfsoff;
3181 update_tfsoff(type, start_offset, file);
3182 print_start_tfs_comment(file, type, start_offset);
3183 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3184 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3185 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3186 *tfsoff += 4;
3188 if (array)
3190 unsigned int absoff = array->declspec.type->typestring_offset;
3191 short reloff = absoff - *tfsoff;
3192 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3193 reloff, reloff, absoff);
3194 *tfsoff += 2;
3196 else if (fc == FC_BOGUS_STRUCT)
3198 print_file(file, 2, "NdrFcShort(0x0),\n");
3199 *tfsoff += 2;
3202 if (fc == FC_BOGUS_STRUCT)
3204 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3205 nothing is written to file yet. On the actual writing pass,
3206 this will have been updated. */
3207 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3208 int reloff = absoff - *tfsoff;
3209 assert( reloff >= 0 );
3210 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3211 (unsigned short)reloff, reloff, absoff);
3212 *tfsoff += 2;
3214 else if ((fc == FC_PSTRUCT) ||
3215 (fc == FC_CPSTRUCT) ||
3216 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3218 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3219 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3220 *tfsoff += 2;
3221 write_pointer_description(file, NULL, type, tfsoff);
3222 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3223 *tfsoff += 1;
3226 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3227 tfsoff);
3229 if (fc == FC_BOGUS_STRUCT)
3231 const var_t *f;
3233 type->ptrdesc = *tfsoff;
3234 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3236 type_t *ft = f->declspec.type;
3237 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3239 case TGT_POINTER:
3240 if (is_string_type(f->attrs, ft))
3241 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3242 else
3243 write_pointer_tfs(file, f->attrs, ft,
3244 type_pointer_get_ref_type(ft)->typestring_offset,
3245 TYPE_CONTEXT_CONTAINER, tfsoff);
3246 break;
3247 case TGT_ARRAY:
3248 if (type_array_is_decl_as_ptr(ft))
3250 unsigned int offset;
3252 print_file(file, 0, "/* %d */\n", *tfsoff);
3254 offset = ft->typestring_offset;
3255 /* skip over the pointer that is written for strings, since a
3256 * pointer has to be written in-place here */
3257 if (is_string_type(f->attrs, ft))
3258 offset += 4;
3259 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3261 break;
3262 default:
3263 break;
3266 if (type->ptrdesc == *tfsoff)
3267 type->ptrdesc = 0;
3270 current_structure = save_current_structure;
3271 return start_offset;
3274 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3276 if (t == NULL)
3278 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3280 else
3282 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3284 unsigned char fc;
3285 if (type_get_type(t) == TYPE_BASIC)
3286 fc = get_basic_fc(t);
3287 else
3288 fc = get_enum_fc(t);
3289 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3290 fc, string_of_type(fc));
3292 else if (t->typestring_offset)
3294 short reloff = t->typestring_offset - *tfsoff;
3295 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3296 reloff, reloff, t->typestring_offset);
3298 else
3299 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3302 *tfsoff += 2;
3305 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3306 type_t *type, unsigned int *tfsoff)
3308 unsigned int start_offset;
3309 unsigned int size;
3310 var_list_t *fields;
3311 unsigned int nbranch = 0;
3312 type_t *deftype = NULL;
3313 short nodeftype = 0xffff;
3314 unsigned int dummy;
3315 var_t *f;
3317 if (processed(type) &&
3318 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3319 return type->typestring_offset;
3321 guard_rec(type);
3323 fields = type_union_get_cases(type);
3325 size = union_memsize(fields, &dummy);
3327 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3329 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3330 if (cases)
3331 nbranch += list_count(cases);
3332 if (f->declspec.type)
3333 write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
3336 start_offset = *tfsoff;
3337 update_tfsoff(type, start_offset, file);
3338 print_start_tfs_comment(file, type, start_offset);
3339 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3341 const var_t *sv = type_union_get_switch_value(type);
3342 const type_t *st = sv->declspec.type;
3343 unsigned int align = 0;
3344 unsigned char fc;
3346 if (type_get_type(st) == TYPE_BASIC)
3348 fc = get_basic_fc(st);
3349 switch (fc)
3351 case FC_CHAR:
3352 case FC_SMALL:
3353 case FC_BYTE:
3354 case FC_USMALL:
3355 case FC_WCHAR:
3356 case FC_SHORT:
3357 case FC_USHORT:
3358 case FC_LONG:
3359 case FC_ULONG:
3360 break;
3361 default:
3362 fc = 0;
3363 error("union switch type must be an integer, char, or enum\n");
3366 else if (type_get_type(st) == TYPE_ENUM)
3367 fc = get_enum_fc(st);
3368 else
3369 error("union switch type must be an integer, char, or enum\n");
3371 type_memsize_and_alignment(st, &align);
3372 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3374 if (f->declspec.type)
3375 type_memsize_and_alignment(f->declspec.type, &align);
3378 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3379 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3380 (align << 4) | fc, string_of_type(fc));
3381 *tfsoff += 2;
3383 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3385 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3386 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3387 unsigned char fc;
3389 if (type_get_type(st) == TYPE_BASIC)
3391 fc = get_basic_fc(st);
3392 switch (fc)
3394 case FC_CHAR:
3395 case FC_SMALL:
3396 case FC_USMALL:
3397 case FC_SHORT:
3398 case FC_USHORT:
3399 case FC_LONG:
3400 case FC_ULONG:
3401 case FC_ENUM16:
3402 case FC_ENUM32:
3403 break;
3404 default:
3405 fc = 0;
3406 error("union switch type must be an integer, char, or enum\n");
3409 else if (type_get_type(st) == TYPE_ENUM)
3410 fc = get_enum_fc(st);
3411 else
3412 error("union switch type must be an integer, char, or enum\n");
3414 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3415 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3416 fc, string_of_type(fc));
3417 *tfsoff += 2;
3418 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3419 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3420 *tfsoff += 2;
3421 print_file(file, 0, "/* %u */\n", *tfsoff);
3424 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3425 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3426 *tfsoff += 4;
3428 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3430 type_t *ft = f->declspec.type;
3431 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3432 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3433 expr_t *c;
3435 if (cases == NULL && !deflt)
3436 error("union field %s with neither case nor default attribute\n", f->name);
3438 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3440 /* MIDL doesn't check for duplicate cases, even though that seems
3441 like a reasonable thing to do, it just dumps them to the TFS
3442 like we're going to do here. */
3443 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3444 *tfsoff += 4;
3445 write_branch_type(file, ft, tfsoff);
3448 /* MIDL allows multiple default branches, even though that seems
3449 illogical, it just chooses the last one, which is what we will
3450 do. */
3451 if (deflt)
3453 deftype = ft;
3454 nodeftype = 0;
3458 if (deftype)
3460 write_branch_type(file, deftype, tfsoff);
3462 else
3464 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3465 *tfsoff += 2;
3468 return start_offset;
3471 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3472 unsigned int *typeformat_offset)
3474 unsigned int i;
3475 unsigned int start_offset = *typeformat_offset;
3476 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3478 if (!iid && processed(type)) return type->typestring_offset;
3480 print_start_tfs_comment(file, type, start_offset);
3481 update_tfsoff(type, start_offset, file);
3483 if (iid)
3485 print_file(file, 2, "0x2f, /* FC_IP */\n");
3486 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3487 *typeformat_offset
3488 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3490 else
3492 const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
3493 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3495 if (! uuid)
3496 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3498 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3499 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3500 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3501 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3502 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3503 for (i = 0; i < 8; ++i)
3504 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3506 if (file)
3507 fprintf(file, "\n");
3509 *typeformat_offset += 18;
3511 return start_offset;
3514 static unsigned int write_contexthandle_tfs(FILE *file,
3515 const attr_list_t *attrs,
3516 type_t *type,
3517 enum type_context context,
3518 unsigned int *typeformat_offset)
3520 unsigned int start_offset = *typeformat_offset;
3521 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
3523 print_start_tfs_comment(file, type, start_offset);
3525 if (flags & 0x80) /* via ptr */
3527 int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
3528 if (!pointer_type) pointer_type = FC_RP;
3529 *typeformat_offset += 4;
3530 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3531 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3532 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3535 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3536 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3537 if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
3538 print_file(file, 0, "can't be null, ");
3539 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3540 print_file(file, 0, "serialize, ");
3541 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3542 print_file(file, 0, "no serialize, ");
3543 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3544 print_file(file, 0, "strict, ");
3545 if (flags & HANDLE_PARAM_IS_RETURN)
3546 print_file(file, 0, "return, ");
3547 if (flags & HANDLE_PARAM_IS_OUT)
3548 print_file(file, 0, "out, ");
3549 if (flags & HANDLE_PARAM_IS_IN)
3550 print_file(file, 0, "in, ");
3551 if (flags & HANDLE_PARAM_IS_VIA_PTR)
3552 print_file(file, 0, "via ptr, ");
3553 print_file(file, 0, "*/\n");
3554 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3555 print_file(file, 2, "0, /* FIXME: param num */\n");
3556 *typeformat_offset += 4;
3558 update_tfsoff( type, start_offset, file );
3559 return start_offset;
3562 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3563 type_t *type, expr_list_t *range_list,
3564 unsigned int *typeformat_offset)
3566 unsigned char fc;
3567 unsigned int start_offset = *typeformat_offset;
3568 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3569 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3571 if (type_get_type(type) == TYPE_BASIC)
3572 fc = get_basic_fc(type);
3573 else
3574 fc = get_enum_fc(type);
3576 /* fc must fit in lower 4-bits of 8-bit field below */
3577 assert(fc <= 0xf);
3579 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3580 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3581 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3582 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3583 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3584 update_tfsoff( type, start_offset, file );
3585 *typeformat_offset += 10;
3587 return start_offset;
3590 static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
3591 type_t *type, const char *name,
3592 enum type_context context,
3593 unsigned int *typeformat_offset)
3595 unsigned int offset;
3597 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3599 case TGT_CTXT_HANDLE:
3600 case TGT_CTXT_HANDLE_POINTER:
3601 return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
3602 case TGT_USER_TYPE:
3603 return write_user_tfs(file, type, typeformat_offset);
3604 case TGT_STRING:
3605 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3606 case TGT_ARRAY:
3608 unsigned int off;
3609 /* conformant and pointer arrays are handled specially */
3610 if ((context != TYPE_CONTEXT_CONTAINER &&
3611 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3612 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3613 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3614 else
3615 off = 0;
3616 if (context != TYPE_CONTEXT_CONTAINER &&
3617 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3619 int ptr_type;
3620 ptr_type = get_pointer_fc_context(type, attrs, context);
3621 if (type_array_is_decl_as_ptr(type)
3622 || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
3624 unsigned int absoff = type->typestring_offset;
3625 short reloff = absoff - (*typeformat_offset + 2);
3626 off = *typeformat_offset;
3627 print_file(file, 0, "/* %d */\n", off);
3628 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3629 string_of_type(ptr_type));
3630 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3631 reloff, reloff, absoff);
3632 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3633 *typeformat_offset += 4;
3635 type_array_set_ptr_tfsoff(type, off);
3637 return off;
3639 case TGT_STRUCT:
3640 return write_struct_tfs(file, type, name, typeformat_offset);
3641 case TGT_UNION:
3642 return write_union_tfs(file, attrs, type, typeformat_offset);
3643 case TGT_ENUM:
3644 case TGT_BASIC:
3645 /* nothing to do */
3646 return 0;
3647 case TGT_RANGE:
3649 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3650 if (!range_list)
3651 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3652 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3654 case TGT_IFACE_POINTER:
3655 return write_ip_tfs(file, attrs, type, typeformat_offset);
3656 case TGT_POINTER:
3658 enum type_context ref_context;
3659 type_t *ref = type_pointer_get_ref_type(type);
3661 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3662 ref_context = TYPE_CONTEXT_PARAM;
3663 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3664 ref_context = TYPE_CONTEXT_CONTAINER;
3665 else
3666 ref_context = context;
3668 if (is_string_type(attrs, ref))
3670 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3671 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3673 offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
3674 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3675 return 0;
3676 return offset;
3679 offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
3680 ref_context, typeformat_offset);
3681 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3682 return 0;
3683 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3685 case TGT_INVALID:
3686 break;
3688 error("invalid type %s for var %s\n", type->name, name);
3689 return 0;
3692 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3693 const char *name, int write_ptr, unsigned int *tfsoff)
3695 return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3698 static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
3700 const statement_list_t *stmts = type_iface_get_stmts(iface);
3701 const statement_t *stmt;
3702 var_t *var;
3704 current_iface = iface;
3705 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
3707 switch(stmt->type)
3709 case STMT_DECLARATION:
3711 const var_t *func = stmt->u.var;
3713 if(stmt->u.var->declspec.stgclass != STG_NONE
3714 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
3715 continue;
3717 current_func = func;
3718 if (is_local(func->attrs)) continue;
3720 var = type_function_get_retval(func->declspec.type);
3721 if (!is_void(var->declspec.type))
3722 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
3723 TYPE_CONTEXT_RETVAL, offset);
3725 if (type_function_get_args(func->declspec.type))
3726 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
3727 var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
3728 TYPE_CONTEXT_TOPLEVELPARAM, offset );
3729 break;
3732 case STMT_TYPEDEF:
3734 const type_list_t *type_entry;
3735 for (type_entry = stmt->u.type_list; type_entry; type_entry = type_entry->next)
3737 if (is_attr(type_entry->type->attrs, ATTR_ENCODE)
3738 || is_attr(type_entry->type->attrs, ATTR_DECODE))
3739 type_entry->type->typestring_offset = write_type_tfs( file,
3740 type_entry->type->attrs, type_entry->type, type_entry->type->name,
3741 TYPE_CONTEXT_CONTAINER, offset);
3743 break;
3745 default:
3746 break;
3751 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3753 unsigned int typeformat_offset = 2;
3754 for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
3755 return typeformat_offset + 1;
3759 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3761 int indent = 0;
3763 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3764 print_file(file, indent, "{\n");
3765 indent++;
3766 print_file(file, indent, "0,\n");
3767 print_file(file, indent, "{\n");
3768 indent++;
3769 print_file(file, indent, "NdrFcShort(0x0),\n");
3771 set_all_tfswrite(TRUE);
3772 process_tfs(file, stmts, pred);
3774 print_file(file, indent, "0x0\n");
3775 indent--;
3776 print_file(file, indent, "}\n");
3777 indent--;
3778 print_file(file, indent, "};\n");
3779 print_file(file, indent, "\n");
3782 static unsigned int get_required_buffer_size_type(
3783 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3785 *alignment = 0;
3786 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3788 case TGT_USER_TYPE:
3790 const char *uname = NULL;
3791 const type_t *utype = get_user_type(type, &uname);
3792 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3794 case TGT_BASIC:
3795 switch (get_basic_fc(type))
3797 case FC_BYTE:
3798 case FC_CHAR:
3799 case FC_USMALL:
3800 case FC_SMALL:
3801 *alignment = 4;
3802 return 1;
3804 case FC_WCHAR:
3805 case FC_USHORT:
3806 case FC_SHORT:
3807 *alignment = 4;
3808 return 2;
3810 case FC_ULONG:
3811 case FC_LONG:
3812 case FC_FLOAT:
3813 case FC_ERROR_STATUS_T:
3814 *alignment = 4;
3815 return 4;
3817 case FC_HYPER:
3818 case FC_DOUBLE:
3819 *alignment = 8;
3820 return 8;
3822 case FC_INT3264:
3823 case FC_UINT3264:
3824 assert( pointer_size );
3825 *alignment = pointer_size;
3826 return pointer_size;
3828 case FC_IGNORE:
3829 case FC_BIND_PRIMITIVE:
3830 return 0;
3832 default:
3833 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3834 get_basic_fc(type));
3835 return 0;
3837 break;
3839 case TGT_ENUM:
3840 switch (get_enum_fc(type))
3842 case FC_ENUM32:
3843 *alignment = 4;
3844 return 4;
3845 case FC_ENUM16:
3846 *alignment = 4;
3847 return 2;
3849 break;
3851 case TGT_STRUCT:
3852 if (get_struct_fc(type) == FC_STRUCT)
3854 if (!type_struct_get_fields(type)) return 0;
3855 return fields_memsize(type_struct_get_fields(type), alignment);
3857 break;
3859 case TGT_POINTER:
3861 unsigned int size, align;
3862 const type_t *ref = type_pointer_get_ref_type(type);
3863 if (is_string_type( attrs, ref )) break;
3864 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3865 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3867 size += 4 + align;
3868 align = 4;
3870 *alignment = align;
3871 return size;
3874 case TGT_ARRAY:
3875 switch (get_array_fc(type))
3877 case FC_SMFARRAY:
3878 case FC_LGFARRAY:
3879 return type_array_get_dim(type) *
3880 get_required_buffer_size_type(type_array_get_element_type(type), name,
3881 NULL, FALSE, alignment);
3883 break;
3885 default:
3886 break;
3888 return 0;
3891 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3893 int in_attr = is_attr(var->attrs, ATTR_IN);
3894 int out_attr = is_attr(var->attrs, ATTR_OUT);
3896 if (!in_attr && !out_attr)
3897 in_attr = 1;
3899 *alignment = 0;
3901 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3902 pass == PASS_RETURN)
3904 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3906 *alignment = 4;
3907 return 20;
3910 if (!is_string_type(var->attrs, var->declspec.type))
3911 return get_required_buffer_size_type(var->declspec.type, var->name,
3912 var->attrs, TRUE, alignment);
3914 return 0;
3917 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3919 const var_t *var;
3920 unsigned int total_size = 0, alignment;
3922 if (type_function_get_args(func->declspec.type))
3924 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
3926 total_size += get_required_buffer_size(var, &alignment, pass);
3927 total_size += alignment;
3931 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
3933 var_t v = *func;
3934 v.declspec.type = type_function_get_rettype(func->declspec.type);
3935 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3936 total_size += alignment;
3938 return total_size;
3941 static void print_phase_function(FILE *file, int indent, const char *type,
3942 const char *local_var_prefix, enum remoting_phase phase,
3943 const var_t *var, unsigned int type_offset)
3945 const char *function;
3946 switch (phase)
3948 case PHASE_BUFFERSIZE:
3949 function = "BufferSize";
3950 break;
3951 case PHASE_MARSHAL:
3952 function = "Marshall";
3953 break;
3954 case PHASE_UNMARSHAL:
3955 function = "Unmarshall";
3956 break;
3957 case PHASE_FREE:
3958 function = "Free";
3959 break;
3960 default:
3961 assert(0);
3962 return;
3965 print_file(file, indent, "Ndr%s%s(\n", type, function);
3966 indent++;
3967 print_file(file, indent, "&__frame->_StubMsg,\n");
3968 print_file(file, indent, "%s%s%s%s%s,\n",
3969 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3970 (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
3971 local_var_prefix,
3972 (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
3973 var->name);
3974 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3975 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3976 if (phase == PHASE_UNMARSHAL)
3977 print_file(file, indent, "0);\n");
3978 indent--;
3981 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3982 enum remoting_phase phase, enum pass pass, const var_t *var,
3983 const char *varname)
3985 type_t *type = var->declspec.type;
3986 unsigned int alignment = 0;
3988 /* no work to do for other phases, buffer sizing is done elsewhere */
3989 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3990 return;
3992 if (type_get_type(type) == TYPE_ENUM ||
3993 (type_get_type(type) == TYPE_BASIC &&
3994 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3995 pointer_size != 4))
3997 unsigned char fc;
3999 if (type_get_type(type) == TYPE_ENUM)
4000 fc = get_enum_fc(type);
4001 else
4002 fc = get_basic_fc(type);
4004 if (phase == PHASE_MARSHAL)
4005 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
4006 else
4007 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
4008 print_file(file, indent+1, "&__frame->_StubMsg,\n");
4009 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
4010 local_var_prefix,
4011 var->name);
4012 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
4014 else
4016 const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
4018 switch (get_basic_fc(ref->type))
4020 case FC_BYTE:
4021 case FC_CHAR:
4022 case FC_SMALL:
4023 case FC_USMALL:
4024 alignment = 1;
4025 break;
4027 case FC_WCHAR:
4028 case FC_USHORT:
4029 case FC_SHORT:
4030 alignment = 2;
4031 break;
4033 case FC_ULONG:
4034 case FC_LONG:
4035 case FC_FLOAT:
4036 case FC_ERROR_STATUS_T:
4037 /* pointer_size must be 4 if we got here in these two cases */
4038 case FC_INT3264:
4039 case FC_UINT3264:
4040 alignment = 4;
4041 break;
4043 case FC_HYPER:
4044 case FC_DOUBLE:
4045 alignment = 8;
4046 break;
4048 case FC_IGNORE:
4049 case FC_BIND_PRIMITIVE:
4050 /* no marshalling needed */
4051 return;
4053 default:
4054 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
4055 var->name, get_basic_fc(ref->type));
4058 if (phase == PHASE_MARSHAL && alignment > 1)
4059 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
4060 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
4061 alignment - 1, alignment - 1);
4063 if (phase == PHASE_MARSHAL)
4065 print_file(file, indent, "*(");
4066 write_type_decl(file, ref, NULL);
4067 if (is_ptr(type))
4068 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
4069 else
4070 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4071 fprintf(file, "%s%s", local_var_prefix, varname);
4072 fprintf(file, ";\n");
4074 else if (phase == PHASE_UNMARSHAL)
4076 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4077 write_type_decl(file, ref, NULL);
4078 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4079 print_file(file, indent, "{\n");
4080 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4081 print_file(file, indent, "}\n");
4082 print_file(file, indent, "%s%s%s",
4083 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4084 local_var_prefix, varname);
4085 if (pass == PASS_IN && is_ptr(type))
4086 fprintf(file, " = (");
4087 else
4088 fprintf(file, " = *(");
4089 write_type_decl(file, ref, NULL);
4090 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4093 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4094 write_type_decl(file, ref, NULL);
4095 fprintf(file, ");\n");
4099 /* returns whether the MaxCount, Offset or ActualCount members need to be
4100 * filled in for the specified phase */
4101 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4103 return (phase != PHASE_UNMARSHAL);
4106 expr_t *get_size_is_expr(const type_t *t, const char *name)
4108 expr_t *x = NULL;
4110 for ( ; is_array(t); t = type_array_get_element_type(t))
4111 if (type_array_has_conformance(t) &&
4112 type_array_get_conformance(t)->type != EXPR_VOID)
4114 if (!x)
4115 x = type_array_get_conformance(t);
4116 else
4117 error("%s: multidimensional conformant"
4118 " arrays not supported at the top level\n",
4119 name);
4122 return x;
4125 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4126 enum remoting_phase phase, const var_t *var, int valid_variance)
4128 const type_t *type = var->declspec.type;
4129 /* get fundamental type for the argument */
4130 for (;;)
4132 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4134 case TGT_ARRAY:
4135 if (is_conformance_needed_for_phase(phase))
4137 if (type_array_has_conformance(type) &&
4138 type_array_get_conformance(type)->type != EXPR_VOID)
4140 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4141 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4142 fprintf(file, ";\n\n");
4144 if (type_array_has_variance(type))
4146 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4147 if (valid_variance)
4149 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4150 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4151 fprintf(file, ";\n\n");
4153 else
4154 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4157 break;
4158 case TGT_UNION:
4159 if (type_get_type(type) == TYPE_UNION &&
4160 is_conformance_needed_for_phase(phase))
4162 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4163 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4164 fprintf(file, ";\n\n");
4166 break;
4167 case TGT_IFACE_POINTER:
4169 expr_t *iid;
4171 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4173 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4174 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4175 fprintf( file, ";\n\n" );
4177 break;
4179 case TGT_POINTER:
4180 type = type_pointer_get_ref_type(type);
4181 continue;
4182 case TGT_INVALID:
4183 case TGT_USER_TYPE:
4184 case TGT_CTXT_HANDLE:
4185 case TGT_CTXT_HANDLE_POINTER:
4186 case TGT_STRING:
4187 case TGT_BASIC:
4188 case TGT_ENUM:
4189 case TGT_STRUCT:
4190 case TGT_RANGE:
4191 break;
4193 break;
4197 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4198 enum pass pass, enum remoting_phase phase, const var_t *var)
4200 int in_attr, out_attr, pointer_type;
4201 const char *type_str = NULL;
4202 const type_t *type = var->declspec.type;
4203 unsigned int alignment, start_offset = type->typestring_offset;
4205 if (is_ptr(type) || is_array(type))
4206 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4207 else
4208 pointer_type = 0;
4210 in_attr = is_attr(var->attrs, ATTR_IN);
4211 out_attr = is_attr(var->attrs, ATTR_OUT);
4212 if (!in_attr && !out_attr)
4213 in_attr = 1;
4215 if (phase != PHASE_FREE)
4216 switch (pass)
4218 case PASS_IN:
4219 if (!in_attr) return;
4220 break;
4221 case PASS_OUT:
4222 if (!out_attr) return;
4223 break;
4224 case PASS_RETURN:
4225 break;
4228 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4230 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4232 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4234 case TGT_CTXT_HANDLE:
4235 case TGT_CTXT_HANDLE_POINTER:
4236 if (phase == PHASE_MARSHAL)
4238 if (pass == PASS_IN)
4240 /* if the context_handle attribute appears in the chain of types
4241 * without pointers being followed, then the context handle must
4242 * be direct, otherwise it is a pointer */
4243 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4244 print_file(file, indent, "NdrClientContextMarshall(\n");
4245 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4246 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4247 var->name);
4248 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4250 else
4252 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4253 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4254 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4255 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
4256 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4259 else if (phase == PHASE_UNMARSHAL)
4261 if (pass == PASS_OUT || pass == PASS_RETURN)
4263 if (!in_attr)
4264 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4265 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4266 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4267 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
4268 pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
4269 print_file(file, indent + 1, "__frame->_Handle);\n");
4271 else
4273 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4274 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4275 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4278 break;
4279 case TGT_USER_TYPE:
4280 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4281 break;
4282 case TGT_STRING:
4283 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4284 pointer_type != FC_RP)
4286 /* strings returned are assumed to be global and hence don't
4287 * need freeing */
4288 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4289 print_phase_function(file, indent, "Pointer", local_var_prefix,
4290 phase, var, start_offset);
4291 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4292 !in_attr && is_conformant_array(type))
4294 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4295 indent++;
4296 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4299 else
4301 unsigned int real_start_offset = start_offset;
4302 /* skip over pointer description straight to string description */
4303 if (is_declptr(type))
4305 if (is_conformant_array(type))
4306 real_start_offset += 4;
4307 else
4308 real_start_offset += 2;
4310 if (is_array(type) && !is_conformant_array(type))
4311 print_phase_function(file, indent, "NonConformantString",
4312 local_var_prefix, phase, var,
4313 real_start_offset);
4314 else
4315 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4316 phase, var, real_start_offset);
4318 break;
4319 case TGT_ARRAY:
4321 unsigned char tc = get_array_fc(type);
4322 const char *array_type = NULL;
4324 /* We already have the size_is expression since it's at the
4325 top level, but do checks for multidimensional conformant
4326 arrays. When we handle them, we'll need to extend this
4327 function to return a list, and then we'll actually use
4328 the return value. */
4329 get_size_is_expr(type, var->name);
4331 switch (tc)
4333 case FC_SMFARRAY:
4334 case FC_LGFARRAY:
4335 array_type = "FixedArray";
4336 break;
4337 case FC_SMVARRAY:
4338 case FC_LGVARRAY:
4339 array_type = "VaryingArray";
4340 break;
4341 case FC_CARRAY:
4342 array_type = "ConformantArray";
4343 break;
4344 case FC_CVARRAY:
4345 array_type = "ConformantVaryingArray";
4346 break;
4347 case FC_BOGUS_ARRAY:
4348 array_type = "ComplexArray";
4349 break;
4352 if (pointer_type != FC_RP) array_type = "Pointer";
4354 if (phase == PHASE_FREE && pointer_type == FC_RP)
4356 /* these are all unmarshalled by allocating memory */
4357 if (tc == FC_BOGUS_ARRAY ||
4358 tc == FC_CVARRAY ||
4359 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4360 (tc == FC_CARRAY && !in_attr))
4362 if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
4364 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4365 type_array_get_ptr_tfsoff(type));
4366 break;
4368 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4369 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4370 indent++;
4371 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4372 break;
4375 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4376 break;
4378 case TGT_BASIC:
4379 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4380 break;
4381 case TGT_ENUM:
4382 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4383 break;
4384 case TGT_RANGE:
4385 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4386 /* Note: this goes beyond what MIDL does - it only supports arguments
4387 * with the [range] attribute in Oicf mode */
4388 if (phase == PHASE_UNMARSHAL)
4390 const expr_t *range_min;
4391 const expr_t *range_max;
4392 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4393 if (!range_list)
4394 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4395 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4396 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4398 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4399 write_type_decl(file, &var->declspec, NULL);
4400 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4401 write_type_decl(file, &var->declspec, NULL);
4402 fprintf(file, ")0x%x))\n", range_max->cval);
4403 print_file(file, indent, "{\n");
4404 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4405 print_file(file, indent, "}\n");
4407 break;
4408 case TGT_STRUCT:
4409 switch (get_struct_fc(type))
4411 case FC_STRUCT:
4412 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4413 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4414 break;
4415 case FC_PSTRUCT:
4416 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4417 break;
4418 case FC_CSTRUCT:
4419 case FC_CPSTRUCT:
4420 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4421 break;
4422 case FC_CVSTRUCT:
4423 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4424 break;
4425 case FC_BOGUS_STRUCT:
4426 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4427 break;
4428 default:
4429 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4431 break;
4432 case TGT_UNION:
4434 const char *union_type = NULL;
4436 if (type_get_type(type) == TYPE_UNION)
4437 union_type = "NonEncapsulatedUnion";
4438 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4439 union_type = "EncapsulatedUnion";
4441 print_phase_function(file, indent, union_type, local_var_prefix,
4442 phase, var, start_offset);
4443 break;
4445 case TGT_POINTER:
4447 const type_t *ref = type_pointer_get_ref_type(type);
4448 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4450 case TGT_BASIC:
4451 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4452 break;
4453 case TGT_ENUM:
4454 /* base types have known sizes, so don't need a sizing pass
4455 * and don't have any memory to free and so don't need a
4456 * freeing pass */
4457 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4458 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4459 break;
4460 case TGT_STRUCT:
4461 switch (get_struct_fc(ref))
4463 case FC_STRUCT:
4464 /* simple structs have known sizes, so don't need a sizing
4465 * pass and don't have any memory to free and so don't
4466 * need a freeing pass */
4467 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4468 type_str = "SimpleStruct";
4469 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4471 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4472 indent++;
4473 print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
4474 indent--;
4476 break;
4477 case FC_PSTRUCT:
4478 type_str = "SimpleStruct";
4479 break;
4480 case FC_CSTRUCT:
4481 case FC_CPSTRUCT:
4482 type_str = "ConformantStruct";
4483 break;
4484 case FC_CVSTRUCT:
4485 type_str = "ConformantVaryingStruct";
4486 break;
4487 case FC_BOGUS_STRUCT:
4488 type_str = "ComplexStruct";
4489 break;
4490 default:
4491 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4494 if (type_str)
4496 if (phase == PHASE_FREE)
4497 type_str = "Pointer";
4498 else
4499 start_offset = ref->typestring_offset;
4500 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4502 break;
4503 case TGT_UNION:
4504 if (phase == PHASE_FREE)
4505 type_str = "Pointer";
4506 else
4508 if (type_get_type(ref) == TYPE_UNION)
4509 type_str = "NonEncapsulatedUnion";
4510 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4511 type_str = "EncapsulatedUnion";
4513 start_offset = ref->typestring_offset;
4516 print_phase_function(file, indent, type_str, local_var_prefix,
4517 phase, var, start_offset);
4518 break;
4519 case TGT_USER_TYPE:
4520 if (phase != PHASE_FREE)
4522 type_str = "UserMarshal";
4523 start_offset = ref->typestring_offset;
4525 else type_str = "Pointer";
4527 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4528 break;
4529 case TGT_STRING:
4530 case TGT_POINTER:
4531 case TGT_ARRAY:
4532 case TGT_RANGE:
4533 case TGT_IFACE_POINTER:
4534 case TGT_CTXT_HANDLE:
4535 case TGT_CTXT_HANDLE_POINTER:
4536 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4537 break;
4538 case TGT_INVALID:
4539 assert(0);
4540 break;
4542 else
4543 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4544 break;
4546 case TGT_IFACE_POINTER:
4547 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4548 break;
4549 case TGT_INVALID:
4550 assert(0);
4551 break;
4553 fprintf(file, "\n");
4556 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4557 enum pass pass, enum remoting_phase phase)
4559 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4561 unsigned int size = get_function_buffer_size( func, pass );
4562 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4565 if (pass == PASS_RETURN)
4567 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4568 type_function_get_retval(func->declspec.type) );
4570 else
4572 const var_t *var;
4573 if (!type_function_get_args(func->declspec.type))
4574 return;
4575 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4576 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4581 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4583 unsigned int offset = 0;
4584 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4585 return offset;
4588 static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
4590 const statement_t *stmt;
4591 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
4593 const var_t *func = stmt->u.var;
4594 if (!is_local(func->attrs))
4595 *size += get_size_procformatstring_func( iface, func );
4599 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4601 unsigned int size = 1;
4602 for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
4603 return size;
4606 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4608 set_all_tfswrite(FALSE);
4609 return process_tfs(NULL, stmts, pred);
4612 void declare_stub_args( FILE *file, int indent, const var_t *func )
4614 int in_attr, out_attr;
4615 int i = 0;
4616 var_t *var = type_function_get_retval(func->declspec.type);
4618 /* declare return value */
4619 if (!is_void(var->declspec.type))
4621 if (is_context_handle(var->declspec.type))
4622 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4623 else
4625 print_file(file, indent, "%s", "");
4626 write_type_decl(file, &var->declspec, var->name);
4627 fprintf(file, ";\n");
4631 if (!type_function_get_args(func->declspec.type))
4632 return;
4634 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
4636 in_attr = is_attr(var->attrs, ATTR_IN);
4637 out_attr = is_attr(var->attrs, ATTR_OUT);
4638 if (!out_attr && !in_attr)
4639 in_attr = 1;
4641 if (is_context_handle(var->declspec.type))
4642 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4643 else
4645 if (!in_attr && !is_conformant_array(var->declspec.type))
4647 const decl_spec_t *type_to_print;
4648 char name[16];
4649 print_file(file, indent, "%s", "");
4650 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4651 !type_array_is_decl_as_ptr(var->declspec.type))
4652 type_to_print = &var->declspec;
4653 else
4654 type_to_print = type_pointer_get_ref(var->declspec.type);
4655 sprintf(name, "_W%u", i++);
4656 write_type_decl(file, type_to_print, name);
4657 fprintf(file, ";\n");
4660 print_file(file, indent, "%s", "");
4661 write_type_decl_left(file, &var->declspec);
4662 fprintf(file, " ");
4663 if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
4664 !type_array_is_decl_as_ptr(var->declspec.type)) {
4665 fprintf(file, "(*%s)", var->name);
4666 } else
4667 fprintf(file, "%s", var->name);
4668 write_type_right(file, var->declspec.type, FALSE);
4669 fprintf(file, ";\n");
4671 if (decl_indirect(var->declspec.type))
4672 print_file(file, indent, "void *_p_%s;\n", var->name);
4678 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4680 int in_attr, out_attr;
4681 int i = 0, sep = 0;
4682 const var_t *var;
4683 type_t *ref;
4685 if (!type_function_get_args(func->declspec.type))
4686 return;
4688 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
4690 in_attr = is_attr(var->attrs, ATTR_IN);
4691 out_attr = is_attr(var->attrs, ATTR_OUT);
4692 if (!out_attr && !in_attr)
4693 in_attr = 1;
4695 if (!in_attr)
4697 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4699 switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
4701 case TGT_CTXT_HANDLE_POINTER:
4702 fprintf(file, " = NdrContextHandleInitialize(\n");
4703 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4704 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4705 var->typestring_offset);
4706 break;
4707 case TGT_ARRAY:
4708 if (type_array_has_conformance(var->declspec.type))
4710 unsigned int size;
4711 type_t *type;
4713 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4714 for (type = var->declspec.type;
4715 is_array(type) && type_array_has_conformance(type);
4716 type = type_array_get_element_type(type))
4718 write_expr(file, type_array_get_conformance(type), TRUE,
4719 TRUE, NULL, NULL, local_var_prefix);
4720 fprintf(file, " * ");
4722 size = type_memsize(type);
4723 fprintf(file, "%u);\n", size);
4725 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4726 for (type = var->declspec.type;
4727 is_array(type) && type_array_has_conformance(type);
4728 type = type_array_get_element_type(type))
4730 write_expr(file, type_array_get_conformance(type), TRUE,
4731 TRUE, NULL, NULL, local_var_prefix);
4732 fprintf(file, " * ");
4734 size = type_memsize(type);
4735 fprintf(file, "%u);\n", size);
4737 else
4738 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4739 break;
4740 case TGT_POINTER:
4741 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4742 ref = type_pointer_get_ref_type(var->declspec.type);
4743 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4745 case TGT_BASIC:
4746 case TGT_ENUM:
4747 case TGT_POINTER:
4748 case TGT_RANGE:
4749 case TGT_IFACE_POINTER:
4750 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4751 break;
4752 case TGT_USER_TYPE:
4753 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4754 local_var_prefix, i, local_var_prefix, i);
4755 break;
4756 case TGT_ARRAY:
4757 if (type_array_is_decl_as_ptr(ref))
4759 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4760 break;
4762 ref = type_array_get_element_type(ref);
4763 /* fall through */
4764 case TGT_STRUCT:
4765 case TGT_UNION:
4766 if (type_has_pointers(ref))
4767 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4768 local_var_prefix, i, local_var_prefix, i);
4769 break;
4770 case TGT_CTXT_HANDLE:
4771 case TGT_CTXT_HANDLE_POINTER:
4772 case TGT_INVALID:
4773 case TGT_STRING:
4774 /* not initialised */
4775 break;
4777 i++;
4778 break;
4779 default:
4780 break;
4783 sep = 1;
4786 if (sep)
4787 fprintf(file, "\n");
4791 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4792 const char *var_decl, int add_retval )
4794 var_t *retval = type_function_get_retval( func );
4795 const var_list_t *args = type_function_get_args( func );
4796 const var_t *arg;
4797 int needs_packing;
4798 unsigned int align = 0;
4800 if (args)
4801 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4802 if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
4804 needs_packing = (align > pointer_size);
4806 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4807 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4808 print_file(file, 1, "{\n" );
4809 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4811 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4813 print_file(file, 2, "%s", "");
4814 write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
4815 if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
4816 if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
4818 /* FIXME: should check for large args being passed by pointer */
4819 align = 0;
4820 if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
4821 else type_memsize_and_alignment( arg->declspec.type, &align );
4823 if (align < pointer_size)
4824 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4825 fprintf( file, "%s;\n", arg->name );
4827 if (add_retval && !is_void( retval->declspec.type ))
4829 print_file(file, 2, "%s", "");
4830 write_type_left( file, &retval->declspec, NAME_DEFAULT, TRUE, TRUE );
4831 if (needs_space_after( retval->declspec.type )) fputc( ' ', file );
4832 if (!is_array( retval->declspec.type ) && !is_ptr( retval->declspec.type ) &&
4833 type_memsize( retval->declspec.type ) != pointer_size)
4835 fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
4837 fprintf( file, "%s;\n", retval->name );
4839 print_file(file, 1, "} %s;\n", var_decl );
4840 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4841 print_file( file, 0, "\n" );
4844 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4846 const var_list_t *args = type_function_get_args( func->declspec.type );
4847 const var_t *var;
4849 if (!args) return;
4851 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4852 if (cant_be_null( var ))
4853 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4856 int write_expr_eval_routines(FILE *file, const char *iface)
4858 static const char *var_name = "pS";
4859 static const char *var_name_expr = "pS->";
4860 int result = 0;
4861 struct expr_eval_routine *eval;
4862 unsigned short callback_offset = 0;
4864 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4866 const char *name = eval->name;
4867 result = 1;
4869 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4870 eval->iface ? eval->iface->name : iface, name, callback_offset);
4871 print_file(file, 0, "{\n");
4872 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4874 write_func_param_struct( file, eval->iface, eval->cont_type,
4875 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4877 else
4879 decl_spec_t ds = {.type = (type_t *)eval->cont_type};
4880 print_file(file, 1, "%s", "");
4881 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4882 fprintf(file, " *%s = (", var_name);
4883 write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
4884 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4886 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4887 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4888 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4889 fprintf(file, ";\n");
4890 print_file(file, 0, "}\n\n");
4891 callback_offset++;
4893 return result;
4896 void write_expr_eval_routine_list(FILE *file, const char *iface)
4898 struct expr_eval_routine *eval;
4899 struct expr_eval_routine *cursor;
4900 unsigned short callback_offset = 0;
4902 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4903 fprintf(file, "{\n");
4905 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4907 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4908 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4909 callback_offset++;
4910 list_remove(&eval->entry);
4911 free(eval->name);
4912 free(eval);
4915 fprintf(file, "};\n\n");
4918 void write_user_quad_list(FILE *file)
4920 user_type_t *ut;
4922 if (list_empty(&user_type_list))
4923 return;
4925 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4926 fprintf(file, "{\n");
4927 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4929 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4930 print_file(file, 1, "{\n");
4931 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4932 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4933 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4934 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4935 print_file(file, 1, "}%s\n", sep);
4937 fprintf(file, "};\n\n");
4940 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4942 const struct str_list_entry_t *endpoint;
4943 const char *p;
4945 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4946 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4947 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4949 print_file( f, 1, "{ (const unsigned char *)\"" );
4950 for (p = endpoint->str; *p && *p != ':'; p++)
4952 if (*p == '"' || *p == '\\') fputc( '\\', f );
4953 fputc( *p, f );
4955 if (!*p) goto error;
4956 if (p[1] != '[') goto error;
4958 fprintf( f, "\", (const unsigned char *)\"" );
4959 for (p += 2; *p && *p != ']'; p++)
4961 if (*p == '"' || *p == '\\') fputc( '\\', f );
4962 fputc( *p, f );
4964 if (*p != ']') goto error;
4965 fprintf( f, "\" },\n" );
4967 print_file( f, 0, "};\n\n" );
4968 return;
4970 error:
4971 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4974 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4975 const char *prefix, unsigned int proc_offset )
4977 const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
4978 int has_ret = !is_void( rettype->type );
4979 const var_list_t *args = type_function_get_args( func->declspec.type );
4980 const var_t *arg;
4981 int len, needs_params = 0;
4983 /* we need a param structure if we have more than one arg */
4984 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4986 print_file( file, 0, "{\n");
4987 if (needs_params)
4989 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4990 write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
4991 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4992 if (args)
4993 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4994 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4996 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4998 len = fprintf( file, " %s%s( ",
4999 has_ret ? "_RetVal = " : "",
5000 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
5001 fprintf( file, "&%s_StubDesc,", prefix );
5002 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
5003 if (needs_params)
5005 fprintf( file, ",\n%*s&__params", len, "" );
5007 else if (pointer_size == 8)
5009 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
5010 if (args)
5011 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
5012 fprintf( file, ",\n%*s%s", len, "", arg->name );
5014 else
5016 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
5017 else if (args)
5019 arg = LIST_ENTRY( list_head(args), const var_t, entry );
5020 fprintf( file, ",\n%*s&%s", len, "", arg->name );
5023 fprintf( file, " );\n" );
5024 if (has_ret)
5026 print_file( file, 1, "return (" );
5027 write_type_decl_left(file, rettype);
5028 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
5030 print_file( file, 0, "}\n\n");
5033 void write_exceptions( FILE *file )
5035 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
5036 fprintf( file, "\n");
5037 fprintf( file, "#include \"wine/exception.h\"\n");
5038 fprintf( file, "#undef RpcTryExcept\n");
5039 fprintf( file, "#undef RpcExcept\n");
5040 fprintf( file, "#undef RpcEndExcept\n");
5041 fprintf( file, "#undef RpcTryFinally\n");
5042 fprintf( file, "#undef RpcFinally\n");
5043 fprintf( file, "#undef RpcEndFinally\n");
5044 fprintf( file, "#undef RpcExceptionCode\n");
5045 fprintf( file, "#undef RpcAbnormalTermination\n");
5046 fprintf( file, "\n");
5047 fprintf( file, "struct __exception_frame;\n");
5048 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
5049 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
5050 fprintf( file, "\n");
5051 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5052 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
5053 fprintf( file, " __filter_func filter; \\\n");
5054 fprintf( file, " __finally_func finally; \\\n");
5055 fprintf( file, " __wine_jmp_buf jmp; \\\n");
5056 fprintf( file, " DWORD code; \\\n");
5057 fprintf( file, " unsigned char abnormal_termination; \\\n");
5058 fprintf( file, " unsigned char filter_level; \\\n");
5059 fprintf( file, " unsigned char finally_level;\n");
5060 fprintf( file, "\n");
5061 fprintf( file, "struct __exception_frame\n{\n");
5062 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
5063 fprintf( file, "};\n");
5064 fprintf( file, "\n");
5065 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
5066 fprintf( file, "{\n");
5067 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
5068 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
5069 fprintf( file, " {\n");
5070 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5071 fprintf( file, " exc_frame->finally( exc_frame );\n");
5072 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5073 fprintf( file, " }\n");
5074 fprintf( file, " exc_frame->filter_level = 0;\n");
5075 fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
5076 fprintf( file, "}\n");
5077 fprintf( file, "\n");
5078 fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5079 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5080 fprintf( file, " CONTEXT *context,\n");
5081 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5082 fprintf( file, "{\n");
5083 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5084 fprintf( file, "\n");
5085 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5086 fprintf( file, " {\n" );
5087 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5088 fprintf( file, " {\n" );
5089 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5090 fprintf( file, " exc_frame->finally( exc_frame );\n");
5091 fprintf( file, " }\n" );
5092 fprintf( file, " return ExceptionContinueSearch;\n");
5093 fprintf( file, " }\n" );
5094 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5095 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5096 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5097 fprintf( file, " return ExceptionContinueSearch;\n");
5098 fprintf( file, "}\n");
5099 fprintf( file, "\n");
5100 fprintf( file, "#define RpcTryExcept \\\n");
5101 fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
5102 fprintf( file, " { \\\n");
5103 fprintf( file, " if (!__frame->finally_level) \\\n" );
5104 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5105 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5106 fprintf( file, "\n");
5107 fprintf( file, "#define RpcExcept(expr) \\\n");
5108 fprintf( file, " if (!__frame->finally_level) \\\n" );
5109 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5110 fprintf( file, " __frame->filter_level = 0; \\\n" );
5111 fprintf( file, " } \\\n");
5112 fprintf( file, " else \\\n");
5113 fprintf( file, "\n");
5114 fprintf( file, "#define RpcEndExcept\n");
5115 fprintf( file, "\n");
5116 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5117 fprintf( file, "\n");
5118 fprintf( file, "#define RpcTryFinally \\\n");
5119 fprintf( file, " if (!__frame->filter_level) \\\n");
5120 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5121 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5122 fprintf( file, "\n");
5123 fprintf( file, "#define RpcFinally \\\n");
5124 fprintf( file, " if (!__frame->filter_level) \\\n");
5125 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5126 fprintf( file, " __frame->finally_level = 0;\n");
5127 fprintf( file, "\n");
5128 fprintf( file, "#define RpcEndFinally\n");
5129 fprintf( file, "\n");
5130 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5131 fprintf( file, "\n");
5132 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5133 fprintf( file, " do { \\\n");
5134 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5135 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5136 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5137 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5138 fprintf( file, " __frame->filter_level = 0; \\\n");
5139 fprintf( file, " __frame->finally_level = 0; \\\n");
5140 fprintf( file, " } while (0)\n");
5141 fprintf( file, "\n");
5142 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5143 fprintf( file, "\n");
5144 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5145 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5146 fprintf( file, "\n");
5147 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5148 fprintf( file, " DWORD code;\n");
5149 fprintf( file, "\n");
5150 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");