user32: Scale window dimensions in SetWindowPos() based on DPI awareness.
[wine.git] / tools / widl / typegen.c
blob5b498bf8e9389a53717838d789d9ba4adf4d07e1
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,
72 /* parameter flags in Oif mode */
73 static const unsigned short MustSize = 0x0001;
74 static const unsigned short MustFree = 0x0002;
75 static const unsigned short IsPipe = 0x0004;
76 static const unsigned short IsIn = 0x0008;
77 static const unsigned short IsOut = 0x0010;
78 static const unsigned short IsReturn = 0x0020;
79 static const unsigned short IsBasetype = 0x0040;
80 static const unsigned short IsByValue = 0x0080;
81 static const unsigned short IsSimpleRef = 0x0100;
82 /* static const unsigned short IsDontCallFreeInst = 0x0200; */
83 /* static const unsigned short SaveForAsyncFinish = 0x0400; */
85 static unsigned int field_memsize(const type_t *type, unsigned int *offset);
86 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
87 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
88 const char *name, unsigned int *typestring_offset);
89 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
90 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
91 const char *name, int write_ptr, unsigned int *tfsoff);
92 static const var_t *find_array_or_string_in_struct(const type_t *type);
93 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
94 type_t *type, enum type_context context,
95 const char *name, unsigned int *typestring_offset);
96 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
97 const attr_list_t *attrs, int toplevel_param,
98 unsigned int *alignment );
99 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
101 static const char *string_of_type(unsigned char type)
103 switch (type)
105 case FC_BYTE: return "FC_BYTE";
106 case FC_CHAR: return "FC_CHAR";
107 case FC_SMALL: return "FC_SMALL";
108 case FC_USMALL: return "FC_USMALL";
109 case FC_WCHAR: return "FC_WCHAR";
110 case FC_SHORT: return "FC_SHORT";
111 case FC_USHORT: return "FC_USHORT";
112 case FC_LONG: return "FC_LONG";
113 case FC_ULONG: return "FC_ULONG";
114 case FC_FLOAT: return "FC_FLOAT";
115 case FC_HYPER: return "FC_HYPER";
116 case FC_DOUBLE: return "FC_DOUBLE";
117 case FC_ENUM16: return "FC_ENUM16";
118 case FC_ENUM32: return "FC_ENUM32";
119 case FC_IGNORE: return "FC_IGNORE";
120 case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
121 case FC_RP: return "FC_RP";
122 case FC_UP: return "FC_UP";
123 case FC_OP: return "FC_OP";
124 case FC_FP: return "FC_FP";
125 case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
126 case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
127 case FC_STRUCT: return "FC_STRUCT";
128 case FC_PSTRUCT: return "FC_PSTRUCT";
129 case FC_CSTRUCT: return "FC_CSTRUCT";
130 case FC_CPSTRUCT: return "FC_CPSTRUCT";
131 case FC_CVSTRUCT: return "FC_CVSTRUCT";
132 case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
133 case FC_SMFARRAY: return "FC_SMFARRAY";
134 case FC_LGFARRAY: return "FC_LGFARRAY";
135 case FC_SMVARRAY: return "FC_SMVARRAY";
136 case FC_LGVARRAY: return "FC_LGVARRAY";
137 case FC_CARRAY: return "FC_CARRAY";
138 case FC_CVARRAY: return "FC_CVARRAY";
139 case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
140 case FC_ALIGNM2: return "FC_ALIGNM2";
141 case FC_ALIGNM4: return "FC_ALIGNM4";
142 case FC_ALIGNM8: return "FC_ALIGNM8";
143 case FC_POINTER: return "FC_POINTER";
144 case FC_C_CSTRING: return "FC_C_CSTRING";
145 case FC_C_WSTRING: return "FC_C_WSTRING";
146 case FC_CSTRING: return "FC_CSTRING";
147 case FC_WSTRING: return "FC_WSTRING";
148 case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
149 case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
150 case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
151 case FC_IP: return "FC_IP";
152 case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
153 case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
154 case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
155 case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
156 case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
157 case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
158 case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
159 case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
160 case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
161 case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
162 case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
163 case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
164 case FC_STRING_SIZED: return "FC_STRING_SIZED";
165 case FC_NO_REPEAT: return "FC_NO_REPEAT";
166 case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
167 case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
168 case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
169 case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
170 case FC_PP: return "FC_PP";
171 case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
172 case FC_DEREFERENCE: return "FC_DEREFERENCE";
173 case FC_DIV_2: return "FC_DIV_2";
174 case FC_MULT_2: return "FC_MULT_2";
175 case FC_ADD_1: return "FC_ADD_1";
176 case FC_SUB_1: return "FC_SUB_1";
177 case FC_CALLBACK: return "FC_CALLBACK";
178 case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
179 case FC_END: return "FC_END";
180 case FC_PAD: return "FC_PAD";
181 case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
182 case FC_RANGE: return "FC_RANGE";
183 case FC_INT3264: return "FC_INT3264";
184 case FC_UINT3264: return "FC_UINT3264";
185 default:
186 error("string_of_type: unknown type 0x%02x\n", type);
187 return NULL;
191 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
193 const type_t *t = type;
194 for (;;)
196 if (is_attr(t->attrs, attr))
197 return get_attrp(t->attrs, attr);
198 else if (type_is_alias(t))
199 t = type_alias_get_aliasee(t);
200 else return NULL;
204 unsigned char get_basic_fc(const type_t *type)
206 int sign = type_basic_get_sign(type);
207 switch (type_basic_get_type(type))
209 case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
210 case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
211 case TYPE_BASIC_INT32: return (sign <= 0 ? FC_LONG : FC_ULONG);
212 case TYPE_BASIC_INT64: return FC_HYPER;
213 case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
214 case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
215 case TYPE_BASIC_BYTE: return FC_BYTE;
216 case TYPE_BASIC_CHAR: return FC_CHAR;
217 case TYPE_BASIC_WCHAR: return FC_WCHAR;
218 case TYPE_BASIC_HYPER: return FC_HYPER;
219 case TYPE_BASIC_FLOAT: return FC_FLOAT;
220 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
221 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
222 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
224 return 0;
227 static unsigned char get_basic_fc_signed(const type_t *type)
229 switch (type_basic_get_type(type))
231 case TYPE_BASIC_INT8: return FC_SMALL;
232 case TYPE_BASIC_INT16: return FC_SHORT;
233 case TYPE_BASIC_INT32: return FC_LONG;
234 case TYPE_BASIC_INT64: return FC_HYPER;
235 case TYPE_BASIC_INT: return FC_LONG;
236 case TYPE_BASIC_INT3264: return FC_INT3264;
237 case TYPE_BASIC_BYTE: return FC_BYTE;
238 case TYPE_BASIC_CHAR: return FC_CHAR;
239 case TYPE_BASIC_WCHAR: return FC_WCHAR;
240 case TYPE_BASIC_HYPER: return FC_HYPER;
241 case TYPE_BASIC_FLOAT: return FC_FLOAT;
242 case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
243 case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
244 case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
246 return 0;
249 static inline unsigned int clamp_align(unsigned int align)
251 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
252 if(align > packing) align = packing;
253 return align;
256 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
258 const type_t *t;
259 int pointer_type;
261 assert(is_ptr(type) || is_array(type));
263 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
264 if (pointer_type)
265 return pointer_type;
267 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
269 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
270 if (pointer_type)
271 return pointer_type;
274 if (toplevel_param)
275 return FC_RP;
276 else if (is_ptr(type))
277 return type_pointer_get_default_fc(type);
278 else
279 return type_array_get_ptr_default_fc(type);
282 static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
283 enum type_context context )
285 int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
287 if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
288 context == TYPE_CONTEXT_PARAM && is_object( current_iface ))
289 pointer_fc = FC_OP;
291 return pointer_fc;
294 static unsigned char get_enum_fc(const type_t *type)
296 assert(type_get_type(type) == TYPE_ENUM);
297 if (is_aliaschain_attr(type, ATTR_V1ENUM))
298 return FC_ENUM32;
299 else
300 return FC_ENUM16;
303 static type_t *get_user_type(const type_t *t, const char **pname)
305 for (;;)
307 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
308 if (ut)
310 if (pname)
311 *pname = t->name;
312 return ut;
315 if (type_is_alias(t))
316 t = type_alias_get_aliasee(t);
317 else
318 return NULL;
322 static int is_user_type(const type_t *t)
324 return get_user_type(t, NULL) != NULL;
327 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
329 if (is_user_type(type))
330 return TGT_USER_TYPE;
332 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
333 return TGT_CTXT_HANDLE;
335 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
336 return TGT_STRING;
338 switch (type_get_type(type))
340 case TYPE_BASIC:
341 if (!(flags & TDT_IGNORE_RANGES) &&
342 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
343 return TGT_RANGE;
344 return TGT_BASIC;
345 case TYPE_ENUM:
346 if (!(flags & TDT_IGNORE_RANGES) &&
347 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
348 return TGT_RANGE;
349 return TGT_ENUM;
350 case TYPE_POINTER:
351 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
352 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
353 return TGT_IFACE_POINTER;
354 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
355 return TGT_CTXT_HANDLE_POINTER;
356 else
357 return TGT_POINTER;
358 case TYPE_STRUCT:
359 return TGT_STRUCT;
360 case TYPE_ENCAPSULATED_UNION:
361 case TYPE_UNION:
362 return TGT_UNION;
363 case TYPE_ARRAY:
364 return TGT_ARRAY;
365 case TYPE_FUNCTION:
366 case TYPE_COCLASS:
367 case TYPE_INTERFACE:
368 case TYPE_MODULE:
369 case TYPE_VOID:
370 case TYPE_ALIAS:
371 case TYPE_BITFIELD:
372 break;
374 return TGT_INVALID;
377 static int cant_be_null(const var_t *v)
379 switch (typegen_detect_type(v->type, v->attrs, TDT_IGNORE_STRINGS))
381 case TGT_ARRAY:
382 if (!type_array_is_decl_as_ptr( v->type )) return 0;
383 /* fall through */
384 case TGT_POINTER:
385 return (get_pointer_fc(v->type, v->attrs, TRUE) == FC_RP);
386 case TGT_CTXT_HANDLE_POINTER:
387 return TRUE;
388 default:
389 return 0;
394 static int get_padding(const var_list_t *fields)
396 unsigned short offset = 0;
397 unsigned int salign = 1;
398 const var_t *f;
400 if (!fields)
401 return 0;
403 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
405 type_t *ft = f->type;
406 unsigned int align = 0;
407 unsigned int size = type_memsize_and_alignment(ft, &align);
408 align = clamp_align(align);
409 if (align > salign) salign = align;
410 offset = ROUND_SIZE(offset, align);
411 offset += size;
414 return ROUNDING(offset, salign);
417 static unsigned int get_stack_size( const var_t *var, int *by_value )
419 unsigned int stack_size;
420 int by_val;
422 switch (typegen_detect_type( var->type, var->attrs, TDT_ALL_TYPES ))
424 case TGT_BASIC:
425 case TGT_ENUM:
426 case TGT_RANGE:
427 case TGT_STRUCT:
428 case TGT_UNION:
429 case TGT_USER_TYPE:
430 stack_size = type_memsize( var->type );
431 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
432 break;
433 default:
434 by_val = 0;
435 break;
437 if (!by_val) stack_size = pointer_size;
438 if (by_value) *by_value = by_val;
439 return ROUND_SIZE( stack_size, pointer_size );
442 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
443 const type_t *type )
445 unsigned char flags = 0;
447 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
449 if (is_ptr(type) &&
450 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
451 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
452 flags |= 0x80;
454 if (is_attr(attrs, ATTR_IN))
456 flags |= 0x40;
457 if (!is_attr(attrs, ATTR_OUT)) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
459 if (is_attr(attrs, ATTR_OUT)) flags |= 0x20;
461 return flags;
464 static unsigned int get_rpc_flags( const attr_list_t *attrs )
466 unsigned int flags = 0;
468 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
469 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
470 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
471 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
472 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
473 return flags;
476 unsigned char get_struct_fc(const type_t *type)
478 int has_pointer = 0;
479 int has_conformance = 0;
480 int has_variance = 0;
481 var_t *field;
482 var_list_t *fields;
484 fields = type_struct_get_fields(type);
486 if (get_padding(fields))
487 return FC_BOGUS_STRUCT;
489 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
491 type_t *t = field->type;
492 enum typegen_type typegen_type;
494 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
496 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
498 if (is_string_type(field->attrs, field->type))
500 if (is_conformant_array(t))
501 has_conformance = 1;
502 has_variance = 1;
503 continue;
506 if (is_array(type_array_get_element(field->type)))
507 return FC_BOGUS_STRUCT;
509 if (type_array_has_conformance(field->type))
511 has_conformance = 1;
512 if (list_next(fields, &field->entry))
513 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
514 field->name);
516 if (type_array_has_variance(t))
517 has_variance = 1;
519 t = type_array_get_element(t);
520 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
523 switch (typegen_type)
525 case TGT_USER_TYPE:
526 case TGT_IFACE_POINTER:
527 return FC_BOGUS_STRUCT;
528 case TGT_BASIC:
529 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
530 return FC_BOGUS_STRUCT;
531 break;
532 case TGT_ENUM:
533 if (get_enum_fc(t) == FC_ENUM16)
534 return FC_BOGUS_STRUCT;
535 break;
536 case TGT_POINTER:
537 case TGT_ARRAY:
538 if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
539 return FC_BOGUS_STRUCT;
540 has_pointer = 1;
541 break;
542 case TGT_UNION:
543 return FC_BOGUS_STRUCT;
544 case TGT_STRUCT:
546 unsigned char fc = get_struct_fc(t);
547 switch (fc)
549 case FC_STRUCT:
550 break;
551 case FC_CVSTRUCT:
552 has_conformance = 1;
553 has_variance = 1;
554 has_pointer = 1;
555 break;
557 case FC_CPSTRUCT:
558 has_conformance = 1;
559 if (list_next( fields, &field->entry ))
560 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
561 field->name);
562 has_pointer = 1;
563 break;
565 case FC_CSTRUCT:
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 break;
572 case FC_PSTRUCT:
573 has_pointer = 1;
574 break;
576 default:
577 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
578 /* fallthru - treat it as complex */
580 /* as soon as we see one of these these members, it's bogus... */
581 case FC_BOGUS_STRUCT:
582 return FC_BOGUS_STRUCT;
584 break;
586 case TGT_RANGE:
587 return FC_BOGUS_STRUCT;
588 case TGT_STRING:
589 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
590 case TGT_INVALID:
591 case TGT_CTXT_HANDLE:
592 case TGT_CTXT_HANDLE_POINTER:
593 /* checking after parsing should mean that we don't get here. if we do,
594 * it's a checker bug */
595 assert(0);
599 if( has_variance )
601 if ( has_conformance )
602 return FC_CVSTRUCT;
603 else
604 return FC_BOGUS_STRUCT;
606 if( has_conformance && has_pointer )
607 return FC_CPSTRUCT;
608 if( has_conformance )
609 return FC_CSTRUCT;
610 if( has_pointer )
611 return FC_PSTRUCT;
612 return FC_STRUCT;
615 static unsigned char get_array_fc(const type_t *type)
617 unsigned char fc;
618 const expr_t *size_is;
619 const type_t *elem_type;
621 elem_type = type_array_get_element(type);
622 size_is = type_array_get_conformance(type);
624 if (!size_is)
626 unsigned int size = type_memsize(elem_type);
627 if (size * type_array_get_dim(type) > 0xffffuL)
628 fc = FC_LGFARRAY;
629 else
630 fc = FC_SMFARRAY;
632 else
633 fc = FC_CARRAY;
635 if (type_array_has_variance(type))
637 if (fc == FC_SMFARRAY)
638 fc = FC_SMVARRAY;
639 else if (fc == FC_LGFARRAY)
640 fc = FC_LGVARRAY;
641 else if (fc == FC_CARRAY)
642 fc = FC_CVARRAY;
645 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
647 case TGT_USER_TYPE:
648 fc = FC_BOGUS_ARRAY;
649 break;
650 case TGT_BASIC:
651 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
652 pointer_size != 4)
653 fc = FC_BOGUS_ARRAY;
654 break;
655 case TGT_STRUCT:
656 switch (get_struct_fc(elem_type))
658 case FC_BOGUS_STRUCT:
659 fc = FC_BOGUS_ARRAY;
660 break;
662 break;
663 case TGT_ENUM:
664 /* is 16-bit enum - if so, wire size differs from mem size and so
665 * the array cannot be block copied, which means the array is complex */
666 if (get_enum_fc(elem_type) == FC_ENUM16)
667 fc = FC_BOGUS_ARRAY;
668 break;
669 case TGT_UNION:
670 case TGT_IFACE_POINTER:
671 fc = FC_BOGUS_ARRAY;
672 break;
673 case TGT_POINTER:
674 /* ref pointers cannot just be block copied. unique pointers to
675 * interfaces need special treatment. either case means the array is
676 * complex */
677 if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
678 fc = FC_BOGUS_ARRAY;
679 break;
680 case TGT_RANGE:
681 fc = FC_BOGUS_ARRAY;
682 break;
683 case TGT_CTXT_HANDLE:
684 case TGT_CTXT_HANDLE_POINTER:
685 case TGT_STRING:
686 case TGT_INVALID:
687 case TGT_ARRAY:
688 /* nothing to do for everything else */
689 break;
692 return fc;
695 static int is_non_complex_struct(const type_t *type)
697 return (type_get_type(type) == TYPE_STRUCT &&
698 get_struct_fc(type) != FC_BOGUS_STRUCT);
701 static int type_has_pointers(const type_t *type)
703 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
705 case TGT_USER_TYPE:
706 return FALSE;
707 case TGT_POINTER:
708 return TRUE;
709 case TGT_ARRAY:
710 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
711 case TGT_STRUCT:
713 var_list_t *fields = type_struct_get_fields(type);
714 const var_t *field;
715 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
717 if (type_has_pointers(field->type))
718 return TRUE;
720 break;
722 case TGT_UNION:
724 var_list_t *fields;
725 const var_t *field;
726 fields = type_union_get_cases(type);
727 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
729 if (field->type && type_has_pointers(field->type))
730 return TRUE;
732 break;
734 case TGT_CTXT_HANDLE:
735 case TGT_CTXT_HANDLE_POINTER:
736 case TGT_STRING:
737 case TGT_IFACE_POINTER:
738 case TGT_BASIC:
739 case TGT_ENUM:
740 case TGT_RANGE:
741 case TGT_INVALID:
742 break;
745 return FALSE;
748 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
749 int toplevel_param)
751 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
753 case TGT_USER_TYPE:
754 return FALSE;
755 case TGT_POINTER:
756 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
757 return TRUE;
758 else
759 return FALSE;
760 case TGT_ARRAY:
761 if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
762 return TRUE;
763 else
764 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
765 case TGT_STRUCT:
767 var_list_t *fields = type_struct_get_fields(type);
768 const var_t *field;
769 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
771 if (type_has_full_pointer(field->type, field->attrs, FALSE))
772 return TRUE;
774 break;
776 case TGT_UNION:
778 var_list_t *fields;
779 const var_t *field;
780 fields = type_union_get_cases(type);
781 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
783 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
784 return TRUE;
786 break;
788 case TGT_CTXT_HANDLE:
789 case TGT_CTXT_HANDLE_POINTER:
790 case TGT_STRING:
791 case TGT_IFACE_POINTER:
792 case TGT_BASIC:
793 case TGT_ENUM:
794 case TGT_RANGE:
795 case TGT_INVALID:
796 break;
799 return FALSE;
802 static unsigned short user_type_offset(const char *name)
804 user_type_t *ut;
805 unsigned short off = 0;
806 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
808 if (strcmp(name, ut->name) == 0)
809 return off;
810 ++off;
812 error("user_type_offset: couldn't find type (%s)\n", name);
813 return 0;
816 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
818 type->typestring_offset = offset;
819 if (file) type->tfswrite = FALSE;
822 static void guard_rec(type_t *type)
824 /* types that contain references to themselves (like a linked list),
825 need to be shielded from infinite recursion when writing embedded
826 types */
827 if (type->typestring_offset)
828 type->tfswrite = FALSE;
829 else
830 type->typestring_offset = 1;
833 static int is_embedded_complex(const type_t *type)
835 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
837 case TGT_USER_TYPE:
838 case TGT_STRUCT:
839 case TGT_UNION:
840 case TGT_ARRAY:
841 case TGT_IFACE_POINTER:
842 return TRUE;
843 default:
844 return FALSE;
848 static const char *get_context_handle_type_name(const type_t *type)
850 const type_t *t;
851 for (t = type;
852 is_ptr(t) || type_is_alias(t);
853 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
854 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
855 return t->name;
856 assert(0);
857 return NULL;
860 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
861 do { \
862 if (file) \
863 fprintf(file, "/* %2u */\n", typestring_offset); \
864 print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
866 while (0)
868 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
869 static void print_file(FILE *file, int indent, const char *format, ...)
871 va_list va;
872 va_start(va, format);
873 print(file, indent, format, va);
874 va_end(va);
877 void print(FILE *file, int indent, const char *format, va_list va)
879 if (file)
881 if (format[0] != '\n')
882 while (0 < indent--)
883 fprintf(file, " ");
884 vfprintf(file, format, va);
889 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
891 if (decl_indirect(t))
893 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
894 local_var_prefix, n, local_var_prefix, n);
895 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
897 else if (is_ptr(t) || is_array(t))
898 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
901 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
903 const var_t *var = type_function_get_retval(func->type);
905 if (!is_void(var->type))
906 write_var_init(file, indent, var->type, var->name, local_var_prefix);
908 if (!type_get_function_args(func->type))
909 return;
911 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
912 write_var_init(file, indent, var->type, var->name, local_var_prefix);
914 fprintf(file, "\n");
917 static void write_formatdesc(FILE *f, int indent, const char *str)
919 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
920 print_file(f, indent, "{\n");
921 print_file(f, indent + 1, "short Pad;\n");
922 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
923 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
924 print_file(f, indent, "\n");
927 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
929 clear_all_offsets();
931 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
932 get_size_typeformatstring(stmts, pred));
934 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
935 get_size_procformatstring(stmts, pred));
937 fprintf(f, "\n");
938 write_formatdesc(f, indent, "TYPE");
939 write_formatdesc(f, indent, "PROC");
940 fprintf(f, "\n");
941 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
942 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
943 print_file(f, indent, "\n");
946 int decl_indirect(const type_t *t)
948 if (is_user_type(t))
949 return TRUE;
950 return (type_get_type(t) != TYPE_BASIC &&
951 type_get_type(t) != TYPE_ENUM &&
952 type_get_type(t) != TYPE_POINTER &&
953 type_get_type(t) != TYPE_ARRAY);
956 static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
957 unsigned int *stack_size, unsigned int *typestring_offset )
959 unsigned int alignment, server_size = 0, buffer_size = 0;
960 unsigned char fc = 0;
961 int is_byval;
962 int is_in = is_attr(var->attrs, ATTR_IN);
963 int is_out = is_attr(var->attrs, ATTR_OUT);
965 if (is_return) is_out = TRUE;
966 else if (!is_in && !is_out) is_in = TRUE;
968 *flags = 0;
969 *stack_size = get_stack_size( var, &is_byval );
970 *typestring_offset = var->typestring_offset;
972 if (is_in) *flags |= IsIn;
973 if (is_out) *flags |= IsOut;
974 if (is_return) *flags |= IsReturn;
976 if (!is_string_type( var->attrs, var->type ))
977 buffer_size = get_required_buffer_size_type( var->type, NULL, var->attrs, TRUE, &alignment );
979 switch (typegen_detect_type( var->type, var->attrs, TDT_ALL_TYPES ))
981 case TGT_BASIC:
982 *flags |= IsBasetype;
983 fc = get_basic_fc_signed( var->type );
984 if (fc == FC_BIND_PRIMITIVE)
986 buffer_size = 4; /* actually 0 but avoids setting MustSize */
987 fc = FC_LONG;
989 break;
990 case TGT_ENUM:
991 *flags |= IsBasetype;
992 fc = get_enum_fc( var->type );
993 break;
994 case TGT_RANGE:
995 *flags |= IsByValue;
996 break;
997 case TGT_STRUCT:
998 case TGT_UNION:
999 case TGT_USER_TYPE:
1000 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
1001 break;
1002 case TGT_IFACE_POINTER:
1003 *flags |= MustFree;
1004 break;
1005 case TGT_ARRAY:
1006 *flags |= MustFree;
1007 if (type_array_is_decl_as_ptr(var->type) && var->type->details.array.ptr_tfsoff &&
1008 get_pointer_fc( var->type, var->attrs, !is_return ) == FC_RP)
1010 *typestring_offset = var->type->typestring_offset;
1011 *flags |= IsSimpleRef;
1013 break;
1014 case TGT_STRING:
1015 *flags |= MustFree;
1016 if (is_declptr( var->type ) && get_pointer_fc( var->type, var->attrs, !is_return ) == FC_RP)
1018 /* skip over pointer description straight to string description */
1019 if (is_conformant_array( var->type )) *typestring_offset += 4;
1020 else *typestring_offset += 2;
1021 *flags |= IsSimpleRef;
1023 break;
1024 case TGT_CTXT_HANDLE_POINTER:
1025 *flags |= IsSimpleRef;
1026 *typestring_offset += 4;
1027 /* fall through */
1028 case TGT_CTXT_HANDLE:
1029 buffer_size = 20;
1030 break;
1031 case TGT_POINTER:
1032 if (get_pointer_fc( var->type, var->attrs, !is_return ) == FC_RP)
1034 const type_t *ref = type_pointer_get_ref( var->type );
1036 if (!is_string_type( var->attrs, ref ))
1037 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1039 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1041 case TGT_BASIC:
1042 *flags |= IsSimpleRef | IsBasetype;
1043 fc = get_basic_fc( ref );
1044 if (!is_in && is_out) server_size = pointer_size;
1045 break;
1046 case TGT_ENUM:
1047 if ((fc = get_enum_fc( ref )) == FC_ENUM32)
1049 *flags |= IsSimpleRef | IsBasetype;
1050 if (!is_in && is_out) server_size = pointer_size;
1052 else
1054 server_size = pointer_size;
1056 break;
1057 case TGT_UNION:
1058 case TGT_USER_TYPE:
1059 case TGT_RANGE:
1060 *flags |= IsSimpleRef | MustFree;
1061 *typestring_offset = ref->typestring_offset;
1062 if (!is_in && is_out) server_size = type_memsize( ref );
1063 break;
1064 case TGT_STRING:
1065 case TGT_POINTER:
1066 case TGT_ARRAY:
1067 case TGT_CTXT_HANDLE:
1068 case TGT_CTXT_HANDLE_POINTER:
1069 *flags |= MustFree;
1070 server_size = pointer_size;
1071 break;
1072 case TGT_IFACE_POINTER:
1073 *flags |= MustFree;
1074 if (is_in && is_out) server_size = pointer_size;
1075 break;
1076 case TGT_STRUCT:
1077 *flags |= IsSimpleRef | MustFree;
1078 *typestring_offset = ref->typestring_offset;
1079 switch (get_struct_fc(ref))
1081 case FC_STRUCT:
1082 case FC_PSTRUCT:
1083 case FC_BOGUS_STRUCT:
1084 if (!is_in && is_out) server_size = type_memsize( ref );
1085 break;
1086 default:
1087 break;
1089 break;
1090 case TGT_INVALID:
1091 assert(0);
1094 else /* not ref pointer */
1096 *flags |= MustFree;
1098 break;
1099 case TGT_INVALID:
1100 assert(0);
1103 if (!buffer_size) *flags |= MustSize;
1105 if (server_size)
1107 server_size = (server_size + 7) / 8;
1108 if (server_size < 8) *flags |= server_size << 13;
1110 return fc;
1113 static unsigned char get_func_oi2_flags( const var_t *func )
1115 const var_t *var;
1116 var_list_t *args = type_get_function_args( func->type );
1117 var_t *retval = type_function_get_retval( func->type );
1118 unsigned char oi2_flags = 0x40; /* HasExtensions */
1119 unsigned short flags;
1120 unsigned int stack_size, typestring_offset;
1122 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1124 get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
1125 if (flags & MustSize)
1127 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1128 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1132 if (!is_void( retval->type ))
1134 oi2_flags |= 0x04; /* HasRet */
1135 get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
1136 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1138 return oi2_flags;
1141 static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
1142 int is_return, unsigned int *stack_offset)
1144 char buffer[128];
1145 unsigned int stack_size, typestring_offset;
1146 unsigned short flags;
1147 unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
1149 strcpy( buffer, "/* flags:" );
1150 if (flags & MustSize) strcat( buffer, " must size," );
1151 if (flags & MustFree) strcat( buffer, " must free," );
1152 if (flags & IsPipe) strcat( buffer, " pipe," );
1153 if (flags & IsIn) strcat( buffer, " in," );
1154 if (flags & IsOut) strcat( buffer, " out," );
1155 if (flags & IsReturn) strcat( buffer, " return," );
1156 if (flags & IsBasetype) strcat( buffer, " base type," );
1157 if (flags & IsByValue) strcat( buffer, " by value," );
1158 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1159 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1160 strcpy( buffer + strlen( buffer ) - 1, " */" );
1161 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1162 print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1163 *stack_offset, *stack_offset );
1164 if (flags & IsBasetype)
1166 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1167 print_file( file, indent, "0x0,\n" );
1169 else
1170 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1171 typestring_offset, typestring_offset );
1172 *stack_offset += max( stack_size, pointer_size );
1173 return 6;
1176 static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
1177 int is_return, int is_interpreted)
1179 unsigned int size;
1181 int is_in = is_attr(var->attrs, ATTR_IN);
1182 int is_out = is_attr(var->attrs, ATTR_OUT);
1184 if (!is_in && !is_out) is_in = TRUE;
1186 if (type_get_type(var->type) == TYPE_BASIC ||
1187 type_get_type(var->type) == TYPE_ENUM)
1189 unsigned char fc;
1191 if (is_return)
1192 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1193 else
1194 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1196 if (type_get_type(var->type) == TYPE_ENUM)
1198 fc = get_enum_fc(var->type);
1200 else
1202 fc = get_basic_fc_signed(var->type);
1204 if (fc == FC_BIND_PRIMITIVE)
1205 fc = FC_IGNORE;
1208 print_file(file, indent, "0x%02x, /* %s */\n",
1209 fc, string_of_type(fc));
1210 size = 2; /* includes param type prefix */
1212 else
1214 unsigned short offset = var->typestring_offset;
1216 if (!is_interpreted && is_array(var->type) &&
1217 type_array_is_decl_as_ptr(var->type) &&
1218 var->type->details.array.ptr_tfsoff)
1219 offset = var->type->typestring_offset;
1221 if (is_return)
1222 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1223 else if (is_in && is_out)
1224 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1225 else if (is_out)
1226 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1227 else
1228 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1230 size = get_stack_size( var, NULL );
1231 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1232 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1233 size = 4; /* includes param type prefix */
1235 return size;
1238 int is_interpreted_func( const type_t *iface, const var_t *func )
1240 const char *str;
1241 const var_t *var;
1242 const var_list_t *args = type_get_function_args( func->type );
1243 const type_t *ret_type = type_function_get_rettype( func->type );
1245 if (type_get_type( ret_type ) == TYPE_BASIC)
1247 switch (type_basic_get_type( ret_type ))
1249 case TYPE_BASIC_INT64:
1250 case TYPE_BASIC_HYPER:
1251 /* return value must fit in a long_ptr */
1252 if (pointer_size < 8) return 0;
1253 break;
1254 case TYPE_BASIC_FLOAT:
1255 case TYPE_BASIC_DOUBLE:
1256 /* floating point values can't be returned */
1257 return 0;
1258 default:
1259 break;
1262 if (get_stub_mode() != MODE_Oif && args)
1264 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1265 switch (type_get_type( var->type ))
1267 case TYPE_BASIC:
1268 switch (type_basic_get_type( var->type ))
1270 /* floating point arguments are not supported in Oi mode */
1271 case TYPE_BASIC_FLOAT: return 0;
1272 case TYPE_BASIC_DOUBLE: return 0;
1273 default: break;
1275 break;
1276 /* unions passed by value are not supported in Oi mode */
1277 case TYPE_UNION: return 0;
1278 case TYPE_ENCAPSULATED_UNION: return 0;
1279 default: break;
1283 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1284 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1285 return (get_stub_mode() != MODE_Os);
1288 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1289 const var_t *func, unsigned int *offset,
1290 unsigned short num_proc )
1292 var_t *var;
1293 var_list_t *args = type_get_function_args( func->type );
1294 unsigned char explicit_fc, implicit_fc;
1295 unsigned char handle_flags;
1296 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1297 unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
1298 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1299 unsigned int nb_args = 0;
1300 unsigned int stack_size = 0;
1301 unsigned short param_num = 0;
1302 unsigned short handle_stack_offset = 0;
1303 unsigned short handle_param_num = 0;
1305 if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
1306 if (is_object( iface ))
1308 oi_flags |= Oi_OBJECT_PROC;
1309 if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
1310 stack_size += pointer_size;
1313 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1315 if (var == handle_var)
1317 handle_stack_offset = stack_size;
1318 handle_param_num = param_num;
1320 stack_size += get_stack_size( var, NULL );
1321 param_num++;
1322 nb_args++;
1324 if (!is_void( type_function_get_rettype( func->type )))
1326 stack_size += pointer_size;
1327 nb_args++;
1330 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1331 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1332 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1333 print_file( file, indent, "0x%02x,\n", oi_flags );
1334 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1335 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1336 print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
1337 *offset += 10;
1339 if (!implicit_fc)
1341 switch (explicit_fc)
1343 case FC_BIND_PRIMITIVE:
1344 handle_flags = 0;
1345 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1346 print_file( file, indent, "0x%02x,\n", handle_flags );
1347 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1348 handle_stack_offset, handle_stack_offset );
1349 *offset += 4;
1350 break;
1351 case FC_BIND_GENERIC:
1352 handle_flags = type_memsize( handle_var->type );
1353 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1354 print_file( file, indent, "0x%02x,\n", handle_flags );
1355 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1356 handle_stack_offset, handle_stack_offset );
1357 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->type ) );
1358 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
1359 *offset += 6;
1360 break;
1361 case FC_BIND_CONTEXT:
1362 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->type );
1363 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1364 print_file( file, indent, "0x%02x,\n", handle_flags );
1365 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1366 handle_stack_offset, handle_stack_offset );
1367 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->type ) );
1368 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1369 *offset += 6;
1370 break;
1374 if (get_stub_mode() == MODE_Oif)
1376 unsigned char oi2_flags = get_func_oi2_flags( func );
1377 unsigned char ext_flags = 0;
1378 unsigned int size;
1380 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1381 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1383 size = get_function_buffer_size( func, PASS_IN );
1384 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
1385 size = get_function_buffer_size( func, PASS_OUT );
1386 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
1387 print_file( file, indent, "0x%02x,\n", oi2_flags );
1388 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1389 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1390 print_file( file, indent, "0x%02x,\n", ext_flags );
1391 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1392 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1393 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1394 *offset += 14;
1395 if (pointer_size == 8)
1397 unsigned short pos = 0, fpu_mask = 0;
1399 if (is_object( iface )) pos += 2;
1400 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1402 if (type_get_type( var->type ) == TYPE_BASIC)
1404 switch (type_basic_get_type( var->type ))
1406 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1407 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1408 default: break;
1411 pos += 2;
1412 if (pos >= 16) break;
1414 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1415 *offset += 2;
1420 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1421 const var_t *func, unsigned int *offset,
1422 unsigned short num_proc )
1424 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1425 int is_interpreted = is_interpreted_func( iface, func );
1426 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1427 var_t *retval = type_function_get_retval( func->type );
1429 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1431 /* emit argument data */
1432 if (type_get_function_args(func->type))
1434 const var_t *var;
1435 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1437 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1438 if (is_new_style)
1439 *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
1440 else
1441 *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
1445 /* emit return value data */
1446 if (is_void(retval->type))
1448 if (!is_new_style)
1450 print_file(file, 0, "/* %u (void) */\n", *offset);
1451 print_file(file, indent, "0x5b,\t/* FC_END */\n");
1452 print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
1453 *offset += 2;
1456 else
1458 print_file( file, 0, "/* %u (return value) */\n", *offset );
1459 if (is_new_style)
1460 *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
1461 else
1462 *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
1466 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1467 type_pred_t pred, unsigned int *offset)
1469 const statement_t *stmt;
1470 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1472 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1474 const statement_t *stmt_func;
1475 const type_t *iface = stmt->u.type;
1476 const type_t *parent = type_iface_get_inherit( iface );
1477 int count = parent ? count_methods( parent ) : 0;
1479 if (!pred(iface)) continue;
1480 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1482 var_t *func = stmt_func->u.var;
1483 if (is_local(func->attrs)) continue;
1484 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1490 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1492 int indent = 0;
1493 unsigned int offset = 0;
1495 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1496 print_file(file, indent, "{\n");
1497 indent++;
1498 print_file(file, indent, "0,\n");
1499 print_file(file, indent, "{\n");
1500 indent++;
1502 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1504 print_file(file, indent, "0x0\n");
1505 indent--;
1506 print_file(file, indent, "}\n");
1507 indent--;
1508 print_file(file, indent, "};\n");
1509 print_file(file, indent, "\n");
1512 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1514 const statement_t *stmt;
1515 int indent = 0;
1517 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1518 iface->name );
1519 print_file( file, indent, "{\n" );
1520 indent++;
1521 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1523 var_t *func = stmt->u.var;
1524 if (is_local( func->attrs )) continue;
1525 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1527 indent--;
1528 print_file( file, indent, "};\n\n" );
1531 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1533 unsigned char fc;
1535 if (type_get_type(type) == TYPE_BASIC)
1536 fc = get_basic_fc_signed(type);
1537 else if (type_get_type(type) == TYPE_ENUM)
1538 fc = get_enum_fc(type);
1539 else
1540 return 0;
1542 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1543 *typestring_offset += 1;
1544 return 1;
1547 /* write conformance / variance descriptor */
1548 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1549 unsigned int baseoff, const type_t *type,
1550 const expr_t *expr)
1552 unsigned char operator_type = 0;
1553 unsigned char conftype = FC_NORMAL_CONFORMANCE;
1554 const char *conftype_string = "field";
1555 const expr_t *subexpr;
1556 const type_t *iface = NULL;
1557 const char *name;
1559 if (!expr)
1561 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1562 return 4;
1565 if (expr->is_const)
1567 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1568 error("write_conf_or_var_desc: constant value %d is greater than "
1569 "the maximum constant size of %d\n", expr->cval,
1570 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1572 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1573 FC_CONSTANT_CONFORMANCE, expr->cval);
1574 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1575 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1577 return 4;
1580 if (!cont_type) /* top-level conformance */
1582 conftype = FC_TOP_LEVEL_CONFORMANCE;
1583 conftype_string = "parameter";
1584 cont_type = current_func->type;
1585 name = current_func->name;
1586 iface = current_iface;
1588 else
1590 name = cont_type->name;
1591 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1593 conftype = FC_POINTER_CONFORMANCE;
1594 conftype_string = "field pointer";
1598 subexpr = expr;
1599 switch (subexpr->type)
1601 case EXPR_PPTR:
1602 subexpr = subexpr->ref;
1603 operator_type = FC_DEREFERENCE;
1604 break;
1605 case EXPR_DIV:
1606 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1608 subexpr = subexpr->ref;
1609 operator_type = FC_DIV_2;
1611 break;
1612 case EXPR_MUL:
1613 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1615 subexpr = subexpr->ref;
1616 operator_type = FC_MULT_2;
1618 break;
1619 case EXPR_SUB:
1620 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1622 subexpr = subexpr->ref;
1623 operator_type = FC_SUB_1;
1625 break;
1626 case EXPR_ADD:
1627 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1629 subexpr = subexpr->ref;
1630 operator_type = FC_ADD_1;
1632 break;
1633 default:
1634 break;
1637 if (subexpr->type == EXPR_IDENTIFIER)
1639 const type_t *correlation_variable = NULL;
1640 unsigned char param_type = 0;
1641 unsigned int offset = 0;
1642 const var_t *var;
1643 struct expr_loc expr_loc;
1645 if (type_get_type(cont_type) == TYPE_FUNCTION)
1647 var_list_t *args = type_get_function_args( cont_type );
1649 if (is_object( iface )) offset += pointer_size;
1650 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1652 if (var->name && !strcmp(var->name, subexpr->u.sval))
1654 expr_loc.v = var;
1655 correlation_variable = var->type;
1656 break;
1658 offset += get_stack_size( var, NULL );
1661 else
1663 var_list_t *fields = type_struct_get_fields( cont_type );
1665 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1667 unsigned int size = field_memsize( var->type, &offset );
1668 if (var->name && !strcmp(var->name, subexpr->u.sval))
1670 expr_loc.v = var;
1671 correlation_variable = var->type;
1672 break;
1674 offset += size;
1678 if (!correlation_variable)
1679 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1680 expr_loc.attr = NULL;
1681 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1683 offset -= baseoff;
1685 if (type_get_type(correlation_variable) == TYPE_BASIC)
1687 switch (get_basic_fc(correlation_variable))
1689 case FC_CHAR:
1690 case FC_SMALL:
1691 param_type = FC_SMALL;
1692 break;
1693 case FC_BYTE:
1694 case FC_USMALL:
1695 param_type = FC_USMALL;
1696 break;
1697 case FC_WCHAR:
1698 case FC_SHORT:
1699 param_type = FC_SHORT;
1700 break;
1701 case FC_USHORT:
1702 param_type = FC_USHORT;
1703 break;
1704 case FC_LONG:
1705 param_type = FC_LONG;
1706 break;
1707 case FC_ULONG:
1708 param_type = FC_ULONG;
1709 break;
1710 default:
1711 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1712 get_basic_fc(correlation_variable));
1715 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1717 if (get_enum_fc(correlation_variable) == FC_ENUM32)
1718 param_type = FC_LONG;
1719 else
1720 param_type = FC_SHORT;
1722 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1724 if (pointer_size == 8)
1725 param_type = FC_HYPER;
1726 else
1727 param_type = FC_LONG;
1729 else
1731 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1732 subexpr->u.sval);
1733 return 0;
1736 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1737 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1738 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1739 operator_type ? string_of_type(operator_type) : "no operators");
1740 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1741 (unsigned short)offset, offset);
1743 else if (!iface || is_interpreted_func( iface, current_func ))
1745 unsigned int callback_offset = 0;
1746 struct expr_eval_routine *eval;
1747 int found = 0;
1749 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1751 if (eval->cont_type == cont_type ||
1752 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1753 eval->iface == iface &&
1754 eval->name && name && !strcmp(eval->name, name) &&
1755 !compare_expr(eval->expr, expr)))
1757 found = 1;
1758 break;
1760 callback_offset++;
1763 if (!found)
1765 eval = xmalloc (sizeof(*eval));
1766 eval->iface = iface;
1767 eval->cont_type = cont_type;
1768 eval->name = xstrdup( name );
1769 eval->baseoff = baseoff;
1770 eval->expr = expr;
1771 list_add_tail (&expr_eval_routines, &eval->entry);
1774 if (callback_offset > USHRT_MAX)
1775 error("Maximum number of callback routines reached\n");
1777 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1778 print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
1779 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1781 else /* output a dummy corr desc that isn't used */
1783 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1784 print_file(file, 2, "0x0,\n" );
1785 print_file(file, 2, "NdrFcShort(0x0),\n" );
1787 return 4;
1790 /* return size and start offset of a data field based on current offset */
1791 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1793 unsigned int align = 0;
1794 unsigned int size = type_memsize_and_alignment( type, &align );
1796 *offset = ROUND_SIZE( *offset, align );
1797 return size;
1800 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1802 unsigned int size = 0;
1803 unsigned int max_align;
1804 const var_t *v;
1806 if (!fields) return 0;
1807 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1809 unsigned int falign = 0;
1810 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1811 if (*align < falign) *align = falign;
1812 falign = clamp_align(falign);
1813 size = ROUND_SIZE(size, falign);
1814 size += fsize;
1817 max_align = clamp_align(*align);
1818 size = ROUND_SIZE(size, max_align);
1820 return size;
1823 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1825 unsigned int size, maxs = 0;
1826 unsigned int align = *pmaxa;
1827 const var_t *v;
1829 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1831 /* we could have an empty default field with NULL type */
1832 if (v->type)
1834 size = type_memsize_and_alignment(v->type, &align);
1835 if (maxs < size) maxs = size;
1836 if (*pmaxa < align) *pmaxa = align;
1840 return maxs;
1843 unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1845 unsigned int size = 0;
1847 switch (type_get_type(t))
1849 case TYPE_BASIC:
1850 switch (get_basic_fc(t))
1852 case FC_BYTE:
1853 case FC_CHAR:
1854 case FC_USMALL:
1855 case FC_SMALL:
1856 size = 1;
1857 if (size > *align) *align = size;
1858 break;
1859 case FC_WCHAR:
1860 case FC_USHORT:
1861 case FC_SHORT:
1862 size = 2;
1863 if (size > *align) *align = size;
1864 break;
1865 case FC_ULONG:
1866 case FC_LONG:
1867 case FC_ERROR_STATUS_T:
1868 case FC_FLOAT:
1869 size = 4;
1870 if (size > *align) *align = size;
1871 break;
1872 case FC_HYPER:
1873 case FC_DOUBLE:
1874 size = 8;
1875 if (size > *align) *align = size;
1876 break;
1877 case FC_INT3264:
1878 case FC_UINT3264:
1879 case FC_BIND_PRIMITIVE:
1880 assert( pointer_size );
1881 size = pointer_size;
1882 if (size > *align) *align = size;
1883 break;
1884 default:
1885 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1886 size = 0;
1888 break;
1889 case TYPE_ENUM:
1890 switch (get_enum_fc(t))
1892 case FC_ENUM16:
1893 case FC_ENUM32:
1894 size = 4;
1895 if (size > *align) *align = size;
1896 break;
1897 default:
1898 error("type_memsize: Unknown enum type\n");
1899 size = 0;
1901 break;
1902 case TYPE_STRUCT:
1903 size = fields_memsize(type_struct_get_fields(t), align);
1904 break;
1905 case TYPE_ENCAPSULATED_UNION:
1906 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1907 break;
1908 case TYPE_UNION:
1909 size = union_memsize(type_union_get_cases(t), align);
1910 break;
1911 case TYPE_POINTER:
1912 case TYPE_INTERFACE:
1913 assert( pointer_size );
1914 size = pointer_size;
1915 if (size > *align) *align = size;
1916 break;
1917 case TYPE_ARRAY:
1918 if (!type_array_is_decl_as_ptr(t))
1920 if (is_conformant_array(t))
1922 type_memsize_and_alignment(type_array_get_element(t), align);
1923 size = 0;
1925 else
1926 size = type_array_get_dim(t) *
1927 type_memsize_and_alignment(type_array_get_element(t), align);
1929 else /* declared as a pointer */
1931 assert( pointer_size );
1932 size = pointer_size;
1933 if (size > *align) *align = size;
1935 break;
1936 case TYPE_ALIAS:
1937 case TYPE_VOID:
1938 case TYPE_COCLASS:
1939 case TYPE_MODULE:
1940 case TYPE_FUNCTION:
1941 case TYPE_BITFIELD:
1942 /* these types should not be encountered here due to language
1943 * restrictions (interface, void, coclass, module), logical
1944 * restrictions (alias - due to type_get_type call above) or
1945 * checking restrictions (function, bitfield). */
1946 assert(0);
1949 return size;
1952 unsigned int type_memsize(const type_t *t)
1954 unsigned int align = 0;
1955 return type_memsize_and_alignment( t, &align );
1958 static unsigned int type_buffer_alignment(const type_t *t)
1960 const var_list_t *fields;
1961 const var_t *var;
1962 unsigned int max = 0, align;
1964 switch (type_get_type(t))
1966 case TYPE_BASIC:
1967 switch (get_basic_fc(t))
1969 case FC_BYTE:
1970 case FC_CHAR:
1971 case FC_USMALL:
1972 case FC_SMALL:
1973 return 1;
1974 case FC_WCHAR:
1975 case FC_USHORT:
1976 case FC_SHORT:
1977 return 2;
1978 case FC_ULONG:
1979 case FC_LONG:
1980 case FC_ERROR_STATUS_T:
1981 case FC_FLOAT:
1982 case FC_INT3264:
1983 case FC_UINT3264:
1984 return 4;
1985 case FC_HYPER:
1986 case FC_DOUBLE:
1987 return 8;
1988 default:
1989 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1991 break;
1992 case TYPE_ENUM:
1993 switch (get_enum_fc(t))
1995 case FC_ENUM16:
1996 return 2;
1997 case FC_ENUM32:
1998 return 4;
1999 default:
2000 error("type_buffer_alignment: Unknown enum type\n");
2002 break;
2003 case TYPE_STRUCT:
2004 if (!(fields = type_struct_get_fields(t))) break;
2005 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2007 if (!var->type) continue;
2008 align = type_buffer_alignment( var->type );
2009 if (max < align) max = align;
2011 break;
2012 case TYPE_ENCAPSULATED_UNION:
2013 if (!(fields = type_encapsulated_union_get_fields(t))) break;
2014 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2016 if (!var->type) continue;
2017 align = type_buffer_alignment( var->type );
2018 if (max < align) max = align;
2020 break;
2021 case TYPE_UNION:
2022 if (!(fields = type_union_get_cases(t))) break;
2023 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
2025 if (!var->type) continue;
2026 align = type_buffer_alignment( var->type );
2027 if (max < align) max = align;
2029 break;
2030 case TYPE_ARRAY:
2031 if (!type_array_is_decl_as_ptr(t))
2032 return type_buffer_alignment( type_array_get_element(t) );
2033 /* else fall through */
2034 case TYPE_POINTER:
2035 return 4;
2036 case TYPE_INTERFACE:
2037 case TYPE_ALIAS:
2038 case TYPE_VOID:
2039 case TYPE_COCLASS:
2040 case TYPE_MODULE:
2041 case TYPE_FUNCTION:
2042 case TYPE_BITFIELD:
2043 /* these types should not be encountered here due to language
2044 * restrictions (interface, void, coclass, module), logical
2045 * restrictions (alias - due to type_get_type call above) or
2046 * checking restrictions (function, bitfield). */
2047 assert(0);
2049 return max;
2052 int is_full_pointer_function(const var_t *func)
2054 const var_t *var;
2055 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2056 return TRUE;
2057 if (!type_get_function_args(func->type))
2058 return FALSE;
2059 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2060 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2061 return TRUE;
2062 return FALSE;
2065 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2067 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2068 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2069 fprintf(file, "\n");
2072 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2074 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2075 fprintf(file, "\n");
2078 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2079 const type_t *type,
2080 enum type_context context,
2081 unsigned int offset,
2082 unsigned int *typeformat_offset)
2084 unsigned int start_offset = *typeformat_offset;
2085 short reloff = offset - (*typeformat_offset + 2);
2086 int in_attr, out_attr;
2087 int pointer_type;
2088 unsigned char flags = 0;
2090 pointer_type = get_pointer_fc_context(type, attrs, context);
2092 in_attr = is_attr(attrs, ATTR_IN);
2093 out_attr = is_attr(attrs, ATTR_OUT);
2094 if (!in_attr && !out_attr) in_attr = 1;
2096 if (out_attr && !in_attr && pointer_type == FC_RP)
2097 flags |= FC_ALLOCED_ON_STACK;
2099 if (is_ptr(type))
2101 type_t *ref = type_pointer_get_ref(type);
2102 if(is_declptr(ref) && !is_user_type(ref))
2103 flags |= FC_POINTER_DEREF;
2106 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2107 pointer_type,
2108 flags,
2109 string_of_type(pointer_type));
2110 if (file)
2112 if (flags & FC_ALLOCED_ON_STACK)
2113 fprintf(file, " [allocated_on_stack]");
2114 if (flags & FC_POINTER_DEREF)
2115 fprintf(file, " [pointer_deref]");
2116 fprintf(file, " */\n");
2119 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2120 *typeformat_offset += 4;
2122 return start_offset;
2125 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
2126 const type_t *type, enum type_context context)
2128 unsigned char fc;
2129 unsigned char pointer_fc;
2130 const type_t *ref;
2131 int in_attr = is_attr(attrs, ATTR_IN);
2132 int out_attr = is_attr(attrs, ATTR_OUT);
2133 unsigned char flags = FC_SIMPLE_POINTER;
2135 /* for historical reasons, write_simple_pointer also handled string types,
2136 * but no longer does. catch bad uses of the function with this check */
2137 if (is_string_type(attrs, type))
2138 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2140 pointer_fc = get_pointer_fc_context(type, attrs, context);
2142 ref = type_pointer_get_ref(type);
2143 if (type_get_type(ref) == TYPE_ENUM)
2144 fc = get_enum_fc(ref);
2145 else
2146 fc = get_basic_fc(ref);
2148 if (out_attr && !in_attr)
2149 flags |= FC_ALLOCED_ON_STACK;
2151 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2152 pointer_fc, flags, string_of_type(pointer_fc),
2153 flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
2154 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2155 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2156 return 4;
2159 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2161 print_file(file, 0, "/* %u (", tfsoff);
2162 write_type_decl(file, t, NULL);
2163 print_file(file, 0, ") */\n");
2166 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2167 type_t *type, unsigned int ref_offset,
2168 enum type_context context,
2169 unsigned int *typestring_offset)
2171 unsigned int offset = *typestring_offset;
2172 type_t *ref = type_pointer_get_ref(type);
2174 print_start_tfs_comment(file, type, offset);
2175 update_tfsoff(type, offset, file);
2177 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2179 case TGT_BASIC:
2180 case TGT_ENUM:
2181 *typestring_offset += write_simple_pointer(file, attrs, type, context);
2182 break;
2183 default:
2184 if (ref_offset)
2185 write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
2186 break;
2189 return offset;
2192 static int processed(const type_t *type)
2194 return type->typestring_offset && !type->tfswrite;
2197 static int user_type_has_variable_size(const type_t *t)
2199 if (is_ptr(t))
2200 return TRUE;
2201 else if (type_get_type(t) == TYPE_STRUCT)
2203 switch (get_struct_fc(t))
2205 case FC_PSTRUCT:
2206 case FC_CSTRUCT:
2207 case FC_CPSTRUCT:
2208 case FC_CVSTRUCT:
2209 return TRUE;
2212 /* Note: Since this only applies to user types, we can't have a conformant
2213 array here, and strings should get filed under pointer in this case. */
2214 return FALSE;
2217 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2219 unsigned int start, absoff, flags;
2220 const char *name = NULL;
2221 type_t *utype = get_user_type(type, &name);
2222 unsigned int usize = type_memsize(utype);
2223 unsigned int ualign = type_buffer_alignment(utype);
2224 unsigned int size = type_memsize(type);
2225 unsigned short funoff = user_type_offset(name);
2226 short reloff;
2228 if (processed(type)) return type->typestring_offset;
2230 guard_rec(type);
2232 if(user_type_has_variable_size(utype)) usize = 0;
2234 if (type_get_type(utype) == TYPE_BASIC ||
2235 type_get_type(utype) == TYPE_ENUM)
2237 unsigned char fc;
2239 if (type_get_type(utype) == TYPE_ENUM)
2240 fc = get_enum_fc(utype);
2241 else
2242 fc = get_basic_fc(utype);
2244 absoff = *tfsoff;
2245 print_start_tfs_comment(file, utype, absoff);
2246 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2247 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2248 *tfsoff += 2;
2250 else
2252 if (!processed(utype))
2253 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2254 absoff = utype->typestring_offset;
2257 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
2258 flags = 0x40;
2259 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
2260 flags = 0x80;
2261 else
2262 flags = 0;
2264 start = *tfsoff;
2265 update_tfsoff(type, start, file);
2266 print_start_tfs_comment(file, type, start);
2267 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
2268 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2269 flags | (ualign - 1), ualign - 1, flags);
2270 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2271 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2272 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2273 *tfsoff += 8;
2274 reloff = absoff - *tfsoff;
2275 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2276 *tfsoff += 2;
2277 return start;
2280 static void write_member_type(FILE *file, const type_t *cont,
2281 int cont_is_complex, const attr_list_t *attrs,
2282 const type_t *type, unsigned int *corroff,
2283 unsigned int *tfsoff)
2285 if (is_embedded_complex(type) && !is_conformant_array(type))
2287 unsigned int absoff;
2288 short reloff;
2290 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2292 absoff = *corroff;
2293 *corroff += 8;
2295 else
2297 absoff = type->typestring_offset;
2299 reloff = absoff - (*tfsoff + 2);
2301 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2302 /* padding is represented using FC_STRUCTPAD* types, so presumably
2303 * this is left over in the format for historical purposes in MIDL
2304 * or rpcrt4. */
2305 print_file(file, 2, "0x0,\n");
2306 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2307 reloff, reloff, absoff);
2308 *tfsoff += 4;
2310 else if (is_ptr(type) || is_conformant_array(type))
2312 unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
2313 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2314 *tfsoff += 1;
2316 else if (!write_base_type(file, type, tfsoff))
2317 error("Unsupported member type %d\n", type_get_type(type));
2320 static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
2321 int cont_is_complex, unsigned int *tfsoff)
2323 type_t *elem = type_array_get_element(type);
2325 if (!is_embedded_complex(elem) && is_ptr(elem))
2327 type_t *ref = type_pointer_get_ref(elem);
2329 if (processed(ref))
2331 write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
2332 ref->typestring_offset, tfsoff);
2333 return;
2335 if (cont_is_complex && is_string_type(attrs, elem))
2337 write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
2338 return;
2340 if (!is_string_type(NULL, elem) &&
2341 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2343 *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
2344 return;
2347 write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2350 static void write_end(FILE *file, unsigned int *tfsoff)
2352 if (*tfsoff % 2 == 0)
2354 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2355 *tfsoff += 1;
2357 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2358 *tfsoff += 1;
2361 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2363 unsigned int offset = 0;
2364 var_list_t *fs = type_struct_get_fields(type);
2365 var_t *f;
2367 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2369 type_t *ft = f->type;
2370 unsigned int size = field_memsize( ft, &offset );
2371 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2373 short reloff;
2374 unsigned int absoff = ft->typestring_offset;
2375 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2376 absoff += 8; /* we already have a corr descr, skip it */
2377 reloff = absoff - (*tfsoff + 6);
2378 print_file(file, 0, "/* %d */\n", *tfsoff);
2379 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
2380 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
2381 write_conf_or_var_desc(file, current_structure, offset, ft,
2382 get_attrp(f->attrs, ATTR_SWITCHIS));
2383 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2384 (unsigned short)reloff, reloff, absoff);
2385 *tfsoff += 8;
2387 offset += size;
2391 static int write_pointer_description_offsets(
2392 FILE *file, const attr_list_t *attrs, type_t *type,
2393 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2394 unsigned int *typestring_offset)
2396 int written = 0;
2398 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2399 (is_array(type) && type_array_is_decl_as_ptr(type)))
2401 if (offset_in_memory && offset_in_buffer)
2403 unsigned int memsize;
2405 /* pointer instance
2407 * note that MSDN states that for pointer layouts in structures,
2408 * this is a negative offset from the end of the structure, but
2409 * this statement is incorrect. all offsets are positive */
2410 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2411 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2413 memsize = type_memsize(type);
2414 *offset_in_memory += memsize;
2415 /* increment these separately as in the case of conformant (varying)
2416 * structures these start at different values */
2417 *offset_in_buffer += memsize;
2419 *typestring_offset += 4;
2421 if (is_ptr(type))
2423 type_t *ref = type_pointer_get_ref(type);
2425 if (is_string_type(attrs, type))
2426 write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
2427 else if (processed(ref))
2428 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
2429 ref->typestring_offset, typestring_offset);
2430 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2431 *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
2432 else
2433 error("write_pointer_description_offsets: type format string unknown\n");
2435 else
2437 unsigned int offset = type->typestring_offset;
2438 /* skip over the pointer that is written for strings, since a
2439 * pointer has to be written in-place here */
2440 if (is_string_type(attrs, type))
2441 offset += 4;
2442 write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
2445 return 1;
2448 if (is_array(type))
2450 return write_pointer_description_offsets(
2451 file, attrs, type_array_get_element(type), offset_in_memory,
2452 offset_in_buffer, typestring_offset);
2454 else if (is_non_complex_struct(type))
2456 /* otherwise search for interesting fields to parse */
2457 const var_t *v;
2458 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2460 if (offset_in_memory && offset_in_buffer)
2462 unsigned int padding;
2463 unsigned int align = 0;
2464 type_memsize_and_alignment(v->type, &align);
2465 padding = ROUNDING(*offset_in_memory, align);
2466 *offset_in_memory += padding;
2467 *offset_in_buffer += padding;
2469 written += write_pointer_description_offsets(
2470 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2471 typestring_offset);
2474 else
2476 if (offset_in_memory && offset_in_buffer)
2478 unsigned int memsize = type_memsize(type);
2479 *offset_in_memory += memsize;
2480 /* increment these separately as in the case of conformant (varying)
2481 * structures these start at different values */
2482 *offset_in_buffer += memsize;
2486 return written;
2489 static int write_no_repeat_pointer_descriptions(
2490 FILE *file, const attr_list_t *attrs, type_t *type,
2491 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2492 unsigned int *typestring_offset)
2494 int written = 0;
2496 if (is_ptr(type) ||
2497 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2499 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
2500 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2501 *typestring_offset += 2;
2503 return write_pointer_description_offsets(file, attrs, type,
2504 offset_in_memory, offset_in_buffer, typestring_offset);
2507 if (is_non_complex_struct(type))
2509 const var_t *v;
2510 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2512 if (offset_in_memory && offset_in_buffer)
2514 unsigned int padding;
2515 unsigned int align = 0;
2516 type_memsize_and_alignment(v->type, &align);
2517 padding = ROUNDING(*offset_in_memory, align);
2518 *offset_in_memory += padding;
2519 *offset_in_buffer += padding;
2521 written += write_no_repeat_pointer_descriptions(
2522 file, v->attrs, v->type,
2523 offset_in_memory, offset_in_buffer, typestring_offset);
2526 else
2528 unsigned int memsize = type_memsize(type);
2529 *offset_in_memory += memsize;
2530 /* increment these separately as in the case of conformant (varying)
2531 * structures these start at different values */
2532 *offset_in_buffer += memsize;
2535 return written;
2538 /* Note: if file is NULL return value is number of pointers to write, else
2539 * it is the number of type format characters written */
2540 static int write_fixed_array_pointer_descriptions(
2541 FILE *file, const attr_list_t *attrs, type_t *type,
2542 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2543 unsigned int *typestring_offset)
2545 int pointer_count = 0;
2547 if (type_get_type(type) == TYPE_ARRAY &&
2548 !type_array_has_conformance(type) && !type_array_has_variance(type))
2550 unsigned int temp = 0;
2551 /* unfortunately, this needs to be done in two passes to avoid
2552 * writing out redundant FC_FIXED_REPEAT descriptions */
2553 pointer_count = write_pointer_description_offsets(
2554 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2555 if (pointer_count > 0)
2557 unsigned int increment_size;
2558 unsigned int offset_of_array_pointer_mem = 0;
2559 unsigned int offset_of_array_pointer_buf = 0;
2561 increment_size = type_memsize(type_array_get_element(type));
2563 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
2564 print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
2565 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2566 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2567 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2568 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2569 *typestring_offset += 10;
2571 pointer_count = write_pointer_description_offsets(
2572 file, attrs, type, &offset_of_array_pointer_mem,
2573 &offset_of_array_pointer_buf, typestring_offset);
2576 else if (type_get_type(type) == TYPE_STRUCT)
2578 const var_t *v;
2579 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2581 if (offset_in_memory && offset_in_buffer)
2583 unsigned int padding;
2584 unsigned int align = 0;
2585 type_memsize_and_alignment(v->type, &align);
2586 padding = ROUNDING(*offset_in_memory, align);
2587 *offset_in_memory += padding;
2588 *offset_in_buffer += padding;
2590 pointer_count += write_fixed_array_pointer_descriptions(
2591 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2592 typestring_offset);
2595 else
2597 if (offset_in_memory && offset_in_buffer)
2599 unsigned int memsize;
2600 memsize = type_memsize(type);
2601 *offset_in_memory += memsize;
2602 /* increment these separately as in the case of conformant (varying)
2603 * structures these start at different values */
2604 *offset_in_buffer += memsize;
2608 return pointer_count;
2611 /* Note: if file is NULL return value is number of pointers to write, else
2612 * it is the number of type format characters written */
2613 static int write_conformant_array_pointer_descriptions(
2614 FILE *file, const attr_list_t *attrs, type_t *type,
2615 unsigned int offset_in_memory, unsigned int *typestring_offset)
2617 int pointer_count = 0;
2619 if (is_conformant_array(type) && !type_array_has_variance(type))
2621 unsigned int temp = 0;
2622 /* unfortunately, this needs to be done in two passes to avoid
2623 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2624 pointer_count = write_pointer_description_offsets(
2625 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2626 if (pointer_count > 0)
2628 unsigned int increment_size;
2629 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2630 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2632 increment_size = type_memsize(type_array_get_element(type));
2634 if (increment_size > USHRT_MAX)
2635 error("array size of %u bytes is too large\n", increment_size);
2637 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2638 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
2639 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2640 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2641 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2642 *typestring_offset += 8;
2644 pointer_count = write_pointer_description_offsets(
2645 file, attrs, type_array_get_element(type),
2646 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2647 typestring_offset);
2651 return pointer_count;
2654 /* Note: if file is NULL return value is number of pointers to write, else
2655 * it is the number of type format characters written */
2656 static int write_varying_array_pointer_descriptions(
2657 FILE *file, const attr_list_t *attrs, type_t *type,
2658 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2659 unsigned int *typestring_offset)
2661 int pointer_count = 0;
2663 if (is_array(type) && type_array_has_variance(type))
2665 unsigned int temp = 0;
2666 /* unfortunately, this needs to be done in two passes to avoid
2667 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2668 pointer_count = write_pointer_description_offsets(
2669 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2670 if (pointer_count > 0)
2672 unsigned int increment_size;
2674 increment_size = type_memsize(type_array_get_element(type));
2676 if (increment_size > USHRT_MAX)
2677 error("array size of %u bytes is too large\n", increment_size);
2679 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
2680 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
2681 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2682 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2683 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2684 *typestring_offset += 8;
2686 pointer_count = write_pointer_description_offsets(
2687 file, attrs, type_array_get_element(type), offset_in_memory,
2688 offset_in_buffer, typestring_offset);
2691 else if (type_get_type(type) == TYPE_STRUCT)
2693 const var_t *v;
2694 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2696 if (offset_in_memory && offset_in_buffer)
2698 unsigned int align = 0, padding;
2700 if (is_array(v->type) && type_array_has_variance(v->type))
2702 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2703 /* skip over variance and offset in buffer */
2704 *offset_in_buffer += 8;
2707 type_memsize_and_alignment(v->type, &align);
2708 padding = ROUNDING(*offset_in_memory, align);
2709 *offset_in_memory += padding;
2710 *offset_in_buffer += padding;
2712 pointer_count += write_varying_array_pointer_descriptions(
2713 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2714 typestring_offset);
2717 else
2719 if (offset_in_memory && offset_in_buffer)
2721 unsigned int memsize = type_memsize(type);
2722 *offset_in_memory += memsize;
2723 /* increment these separately as in the case of conformant (varying)
2724 * structures these start at different values */
2725 *offset_in_buffer += memsize;
2729 return pointer_count;
2732 static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
2733 unsigned int *typestring_offset)
2735 unsigned int offset_in_buffer;
2736 unsigned int offset_in_memory;
2738 /* pass 1: search for single instance of a pointer (i.e. don't descend
2739 * into arrays) */
2740 if (!is_array(type))
2742 offset_in_memory = 0;
2743 offset_in_buffer = 0;
2744 write_no_repeat_pointer_descriptions(
2745 file, NULL, type,
2746 &offset_in_memory, &offset_in_buffer, typestring_offset);
2749 /* pass 2: search for pointers in fixed arrays */
2750 offset_in_memory = 0;
2751 offset_in_buffer = 0;
2752 write_fixed_array_pointer_descriptions(
2753 file, NULL, type,
2754 &offset_in_memory, &offset_in_buffer, typestring_offset);
2756 /* pass 3: search for pointers in conformant only arrays (but don't descend
2757 * into conformant varying or varying arrays) */
2758 if (is_conformant_array(type) &&
2759 (type_array_is_decl_as_ptr(type) || !current_structure))
2760 write_conformant_array_pointer_descriptions(
2761 file, attrs, type, 0, typestring_offset);
2762 else if (type_get_type(type) == TYPE_STRUCT &&
2763 get_struct_fc(type) == FC_CPSTRUCT)
2765 type_t *carray = find_array_or_string_in_struct(type)->type;
2766 write_conformant_array_pointer_descriptions( file, NULL, carray,
2767 type_memsize(type), typestring_offset);
2770 /* pass 4: search for pointers in varying arrays */
2771 offset_in_memory = 0;
2772 offset_in_buffer = 0;
2773 write_varying_array_pointer_descriptions(
2774 file, NULL, type,
2775 &offset_in_memory, &offset_in_buffer, typestring_offset);
2778 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2779 type_t *type, enum type_context context,
2780 const char *name, unsigned int *typestring_offset)
2782 unsigned int start_offset;
2783 unsigned char rtype;
2784 type_t *elem_type;
2785 int is_processed = processed(type);
2787 start_offset = *typestring_offset;
2789 if (is_declptr(type))
2791 unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
2792 int pointer_type = get_pointer_fc_context(type, attrs, context);
2793 if (!pointer_type)
2794 pointer_type = FC_RP;
2795 print_start_tfs_comment(file, type, *typestring_offset);
2796 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2797 pointer_type, flag, string_of_type(pointer_type),
2798 flag ? " [simple_pointer]" : "");
2799 *typestring_offset += 2;
2800 if (!flag)
2802 print_file(file, 2, "NdrFcShort(0x2),\n");
2803 *typestring_offset += 2;
2805 is_processed = FALSE;
2808 if (is_array(type))
2809 elem_type = type_array_get_element(type);
2810 else
2811 elem_type = type_pointer_get_ref(type);
2813 if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
2814 return write_array_tfs(file, attrs, type, name, typestring_offset);
2816 if (type_get_type(elem_type) != TYPE_BASIC)
2818 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2819 return start_offset;
2822 rtype = get_basic_fc(elem_type);
2823 if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
2825 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2826 return start_offset;
2829 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2831 unsigned int dim = type_array_get_dim(type);
2833 if (is_processed) return start_offset;
2835 /* FIXME: multi-dimensional array */
2836 if (0xffffu < dim)
2837 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2838 name, 0xffffu, dim - 0xffffu);
2840 if (rtype == FC_WCHAR)
2841 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2842 else
2843 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2844 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2845 *typestring_offset += 2;
2847 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2848 *typestring_offset += 2;
2850 update_tfsoff(type, start_offset, file);
2851 return start_offset;
2853 else if (is_conformant_array(type))
2855 if (rtype == FC_WCHAR)
2856 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2857 else
2858 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2859 print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
2860 *typestring_offset += 2;
2862 *typestring_offset += write_conf_or_var_desc(
2863 file, current_structure,
2864 (!type_array_is_decl_as_ptr(type) && current_structure
2865 ? type_memsize(current_structure)
2866 : 0),
2867 type, type_array_get_conformance(type));
2869 update_tfsoff(type, start_offset, file);
2870 return start_offset;
2872 else
2874 if (is_processed) return start_offset;
2876 if (rtype == FC_WCHAR)
2877 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2878 else
2879 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2880 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2881 *typestring_offset += 2;
2883 update_tfsoff(type, start_offset, file);
2884 return start_offset;
2888 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2889 const char *name, unsigned int *typestring_offset)
2891 const expr_t *length_is = type_array_get_variance(type);
2892 const expr_t *size_is = type_array_get_conformance(type);
2893 unsigned int align;
2894 unsigned int size;
2895 unsigned int start_offset;
2896 unsigned char fc;
2897 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2898 unsigned int baseoff
2899 = !type_array_is_decl_as_ptr(type) && current_structure
2900 ? type_memsize(current_structure)
2901 : 0;
2903 if (!pointer_type)
2904 pointer_type = FC_RP;
2906 if (!is_string_type(attrs, type_array_get_element(type)))
2907 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2909 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2910 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2911 fc = get_array_fc(type);
2913 start_offset = *typestring_offset;
2914 update_tfsoff(type, start_offset, file);
2915 print_start_tfs_comment(file, type, start_offset);
2916 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2917 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2918 *typestring_offset += 2;
2920 align = 0;
2921 if (fc != FC_BOGUS_ARRAY)
2923 if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
2925 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2926 *typestring_offset += 4;
2928 else
2930 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2931 *typestring_offset += 2;
2934 if (is_conformant_array(type))
2935 *typestring_offset
2936 += write_conf_or_var_desc(file, current_structure, baseoff,
2937 type, size_is);
2939 if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
2941 unsigned int elsize = type_memsize(type_array_get_element(type));
2942 unsigned int dim = type_array_get_dim(type);
2944 if (fc == FC_LGVARRAY)
2946 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2947 *typestring_offset += 4;
2949 else
2951 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2952 *typestring_offset += 2;
2955 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2956 *typestring_offset += 2;
2959 if (length_is)
2960 *typestring_offset
2961 += write_conf_or_var_desc(file, current_structure, baseoff,
2962 type, length_is);
2964 if (type_has_pointers(type_array_get_element(type)) &&
2965 (type_array_is_decl_as_ptr(type) || !current_structure))
2967 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
2968 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
2969 *typestring_offset += 2;
2970 write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
2971 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
2972 *typestring_offset += 1;
2975 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
2976 write_end(file, typestring_offset);
2978 else
2980 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2981 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2982 *typestring_offset += 2;
2983 *typestring_offset
2984 += write_conf_or_var_desc(file, current_structure, baseoff,
2985 type, size_is);
2986 *typestring_offset
2987 += write_conf_or_var_desc(file, current_structure, baseoff,
2988 type, length_is);
2990 write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
2991 write_end(file, typestring_offset);
2994 return start_offset;
2997 static const var_t *find_array_or_string_in_struct(const type_t *type)
2999 const var_list_t *fields = type_struct_get_fields(type);
3000 const var_t *last_field;
3001 const type_t *ft;
3003 if (!fields || list_empty(fields))
3004 return NULL;
3006 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
3007 ft = last_field->type;
3009 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
3010 return last_field;
3012 if (type_get_type(ft) == TYPE_STRUCT)
3013 return find_array_or_string_in_struct(ft);
3014 else
3015 return NULL;
3018 static void write_struct_members(FILE *file, const type_t *type,
3019 int is_complex, unsigned int *corroff,
3020 unsigned int *typestring_offset)
3022 const var_t *field;
3023 unsigned short offset = 0;
3024 unsigned int salign = 1;
3025 int padding;
3026 var_list_t *fields = type_struct_get_fields(type);
3028 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
3030 type_t *ft = field->type;
3031 unsigned int align = 0;
3032 unsigned int size = type_memsize_and_alignment(ft, &align);
3033 align = clamp_align(align);
3034 if (salign < align) salign = align;
3036 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
3038 if ((align - 1) & offset)
3040 unsigned char fc = 0;
3041 switch (align)
3043 case 2:
3044 fc = FC_ALIGNM2;
3045 break;
3046 case 4:
3047 fc = FC_ALIGNM4;
3048 break;
3049 case 8:
3050 fc = FC_ALIGNM8;
3051 break;
3052 default:
3053 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3055 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3056 offset = ROUND_SIZE(offset, align);
3057 *typestring_offset += 1;
3059 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3060 typestring_offset);
3061 offset += size;
3065 padding = ROUNDING(offset, salign);
3066 if (padding)
3068 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3069 FC_STRUCTPAD1 + padding - 1,
3070 padding);
3071 *typestring_offset += 1;
3074 write_end(file, typestring_offset);
3077 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3078 const char *name, unsigned int *tfsoff)
3080 const type_t *save_current_structure = current_structure;
3081 unsigned int total_size;
3082 const var_t *array;
3083 unsigned int start_offset;
3084 unsigned int align;
3085 unsigned int corroff;
3086 var_t *f;
3087 unsigned char fc = get_struct_fc(type);
3088 var_list_t *fields = type_struct_get_fields(type);
3090 if (processed(type)) return type->typestring_offset;
3092 guard_rec(type);
3093 current_structure = type;
3095 total_size = type_memsize(type);
3096 align = type_buffer_alignment(type);
3097 if (total_size > USHRT_MAX)
3098 error("structure size for %s exceeds %d bytes by %d bytes\n",
3099 name, USHRT_MAX, total_size - USHRT_MAX);
3101 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3102 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3104 array = find_array_or_string_in_struct(type);
3105 if (array && !processed(array->type))
3107 if(is_string_type(array->attrs, array->type))
3108 write_string_tfs(file, array->attrs, array->type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
3109 else
3110 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3113 corroff = *tfsoff;
3114 write_descriptors(file, type, tfsoff);
3116 start_offset = *tfsoff;
3117 update_tfsoff(type, start_offset, file);
3118 print_start_tfs_comment(file, type, start_offset);
3119 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3120 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3121 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3122 *tfsoff += 4;
3124 if (array)
3126 unsigned int absoff = array->type->typestring_offset;
3127 short reloff = absoff - *tfsoff;
3128 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3129 reloff, reloff, absoff);
3130 *tfsoff += 2;
3132 else if (fc == FC_BOGUS_STRUCT)
3134 print_file(file, 2, "NdrFcShort(0x0),\n");
3135 *tfsoff += 2;
3138 if (fc == FC_BOGUS_STRUCT)
3140 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3141 nothing is written to file yet. On the actual writing pass,
3142 this will have been updated. */
3143 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3144 int reloff = absoff - *tfsoff;
3145 assert( reloff >= 0 );
3146 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3147 (unsigned short)reloff, reloff, absoff);
3148 *tfsoff += 2;
3150 else if ((fc == FC_PSTRUCT) ||
3151 (fc == FC_CPSTRUCT) ||
3152 (fc == FC_CVSTRUCT && type_has_pointers(type)))
3154 print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
3155 print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
3156 *tfsoff += 2;
3157 write_pointer_description(file, NULL, type, tfsoff);
3158 print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
3159 *tfsoff += 1;
3162 write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
3163 tfsoff);
3165 if (fc == FC_BOGUS_STRUCT)
3167 const var_t *f;
3169 type->ptrdesc = *tfsoff;
3170 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3172 type_t *ft = f->type;
3173 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3175 case TGT_POINTER:
3176 if (is_string_type(f->attrs, ft))
3177 write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
3178 else
3179 write_pointer_tfs(file, f->attrs, ft,
3180 type_pointer_get_ref(ft)->typestring_offset,
3181 TYPE_CONTEXT_CONTAINER, tfsoff);
3182 break;
3183 case TGT_ARRAY:
3184 if (type_array_is_decl_as_ptr(ft))
3186 unsigned int offset;
3188 print_file(file, 0, "/* %d */\n", *tfsoff);
3190 offset = ft->typestring_offset;
3191 /* skip over the pointer that is written for strings, since a
3192 * pointer has to be written in-place here */
3193 if (is_string_type(f->attrs, ft))
3194 offset += 4;
3195 write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
3197 break;
3198 default:
3199 break;
3202 if (type->ptrdesc == *tfsoff)
3203 type->ptrdesc = 0;
3206 current_structure = save_current_structure;
3207 return start_offset;
3210 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3212 if (t == NULL)
3214 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3216 else
3218 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3220 unsigned char fc;
3221 if (type_get_type(t) == TYPE_BASIC)
3222 fc = get_basic_fc(t);
3223 else
3224 fc = get_enum_fc(t);
3225 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3226 fc, string_of_type(fc));
3228 else if (t->typestring_offset)
3230 short reloff = t->typestring_offset - *tfsoff;
3231 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3232 reloff, reloff, t->typestring_offset);
3234 else
3235 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3238 *tfsoff += 2;
3241 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3242 type_t *type, unsigned int *tfsoff)
3244 unsigned int start_offset;
3245 unsigned int size;
3246 var_list_t *fields;
3247 unsigned int nbranch = 0;
3248 type_t *deftype = NULL;
3249 short nodeftype = 0xffff;
3250 unsigned int dummy;
3251 var_t *f;
3253 if (processed(type) &&
3254 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3255 return type->typestring_offset;
3257 guard_rec(type);
3259 fields = type_union_get_cases(type);
3261 size = union_memsize(fields, &dummy);
3263 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3265 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3266 if (cases)
3267 nbranch += list_count(cases);
3268 if (f->type)
3269 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3272 start_offset = *tfsoff;
3273 update_tfsoff(type, start_offset, file);
3274 print_start_tfs_comment(file, type, start_offset);
3275 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3277 const var_t *sv = type_union_get_switch_value(type);
3278 const type_t *st = sv->type;
3279 unsigned int align = 0;
3280 unsigned char fc;
3282 if (type_get_type(st) == TYPE_BASIC)
3284 fc = get_basic_fc(st);
3285 switch (fc)
3287 case FC_CHAR:
3288 case FC_SMALL:
3289 case FC_BYTE:
3290 case FC_USMALL:
3291 case FC_WCHAR:
3292 case FC_SHORT:
3293 case FC_USHORT:
3294 case FC_LONG:
3295 case FC_ULONG:
3296 break;
3297 default:
3298 fc = 0;
3299 error("union switch type must be an integer, char, or enum\n");
3302 else if (type_get_type(st) == TYPE_ENUM)
3303 fc = get_enum_fc(st);
3304 else
3305 error("union switch type must be an integer, char, or enum\n");
3307 type_memsize_and_alignment(st, &align);
3308 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3310 if (f->type)
3311 type_memsize_and_alignment(f->type, &align);
3314 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
3315 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3316 (align << 4) | fc, string_of_type(fc));
3317 *tfsoff += 2;
3319 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3321 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3322 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3323 unsigned char fc;
3325 if (type_get_type(st) == TYPE_BASIC)
3327 fc = get_basic_fc(st);
3328 switch (fc)
3330 case FC_CHAR:
3331 case FC_SMALL:
3332 case FC_USMALL:
3333 case FC_SHORT:
3334 case FC_USHORT:
3335 case FC_LONG:
3336 case FC_ULONG:
3337 case FC_ENUM16:
3338 case FC_ENUM32:
3339 break;
3340 default:
3341 fc = 0;
3342 error("union switch type must be an integer, char, or enum\n");
3345 else if (type_get_type(st) == TYPE_ENUM)
3346 fc = get_enum_fc(st);
3347 else
3348 error("union switch type must be an integer, char, or enum\n");
3350 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
3351 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3352 fc, string_of_type(fc));
3353 *tfsoff += 2;
3354 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3355 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3356 *tfsoff += 2;
3357 print_file(file, 0, "/* %u */\n", *tfsoff);
3360 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3361 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3362 *tfsoff += 4;
3364 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3366 type_t *ft = f->type;
3367 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3368 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3369 expr_t *c;
3371 if (cases == NULL && !deflt)
3372 error("union field %s with neither case nor default attribute\n", f->name);
3374 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3376 /* MIDL doesn't check for duplicate cases, even though that seems
3377 like a reasonable thing to do, it just dumps them to the TFS
3378 like we're going to do here. */
3379 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3380 *tfsoff += 4;
3381 write_branch_type(file, ft, tfsoff);
3384 /* MIDL allows multiple default branches, even though that seems
3385 illogical, it just chooses the last one, which is what we will
3386 do. */
3387 if (deflt)
3389 deftype = ft;
3390 nodeftype = 0;
3394 if (deftype)
3396 write_branch_type(file, deftype, tfsoff);
3398 else
3400 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3401 *tfsoff += 2;
3404 return start_offset;
3407 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3408 unsigned int *typeformat_offset)
3410 unsigned int i;
3411 unsigned int start_offset = *typeformat_offset;
3412 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3414 if (!iid && processed(type)) return type->typestring_offset;
3416 print_start_tfs_comment(file, type, start_offset);
3417 update_tfsoff(type, start_offset, file);
3419 if (iid)
3421 print_file(file, 2, "0x2f, /* FC_IP */\n");
3422 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3423 *typeformat_offset
3424 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3426 else
3428 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3429 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3431 if (! uuid)
3432 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3434 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3435 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3436 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3437 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3438 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3439 for (i = 0; i < 8; ++i)
3440 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3442 if (file)
3443 fprintf(file, "\n");
3445 *typeformat_offset += 18;
3447 return start_offset;
3450 static unsigned int write_contexthandle_tfs(FILE *file,
3451 const attr_list_t *attrs,
3452 type_t *type,
3453 int toplevel_param,
3454 unsigned int *typeformat_offset)
3456 unsigned int start_offset = *typeformat_offset;
3457 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3459 print_start_tfs_comment(file, type, start_offset);
3461 if (flags & 0x80) /* via ptr */
3463 int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3464 if (!pointer_type) pointer_type = FC_RP;
3465 *typeformat_offset += 4;
3466 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3467 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3468 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3471 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
3472 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3473 /* return and can't be null values overlap */
3474 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3475 print_file(file, 0, "can't be null, ");
3476 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3477 print_file(file, 0, "serialize, ");
3478 if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
3479 print_file(file, 0, "no serialize, ");
3480 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3481 print_file(file, 0, "strict, ");
3482 if ((flags & 0x21) == 0x20)
3483 print_file(file, 0, "out, ");
3484 if ((flags & 0x21) == 0x21)
3485 print_file(file, 0, "return, ");
3486 if (flags & 0x40)
3487 print_file(file, 0, "in, ");
3488 if (flags & 0x80)
3489 print_file(file, 0, "via ptr, ");
3490 print_file(file, 0, "*/\n");
3491 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3492 print_file(file, 2, "0, /* FIXME: param num */\n");
3493 *typeformat_offset += 4;
3495 update_tfsoff( type, start_offset, file );
3496 return start_offset;
3499 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3500 type_t *type, expr_list_t *range_list,
3501 unsigned int *typeformat_offset)
3503 unsigned char fc;
3504 unsigned int start_offset = *typeformat_offset;
3505 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3506 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3508 if (type_get_type(type) == TYPE_BASIC)
3509 fc = get_basic_fc(type);
3510 else
3511 fc = get_enum_fc(type);
3513 /* fc must fit in lower 4-bits of 8-bit field below */
3514 assert(fc <= 0xf);
3516 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3517 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
3518 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3519 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3520 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3521 update_tfsoff( type, start_offset, file );
3522 *typeformat_offset += 10;
3524 return start_offset;
3527 static unsigned int write_type_tfs(FILE *file, int indent,
3528 const attr_list_t *attrs, type_t *type,
3529 const char *name,
3530 enum type_context context,
3531 unsigned int *typeformat_offset)
3533 unsigned int offset;
3535 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3537 case TGT_CTXT_HANDLE:
3538 case TGT_CTXT_HANDLE_POINTER:
3539 return write_contexthandle_tfs(file, attrs, type,
3540 context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3541 case TGT_USER_TYPE:
3542 return write_user_tfs(file, type, typeformat_offset);
3543 case TGT_STRING:
3544 return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
3545 case TGT_ARRAY:
3547 unsigned int off;
3548 /* conformant and pointer arrays are handled specially */
3549 if ((context != TYPE_CONTEXT_CONTAINER &&
3550 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3551 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3552 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3553 else
3554 off = 0;
3555 if (context != TYPE_CONTEXT_CONTAINER &&
3556 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3558 int ptr_type;
3559 ptr_type = get_pointer_fc(type, attrs,
3560 context == TYPE_CONTEXT_TOPLEVELPARAM);
3561 if (ptr_type != FC_RP || type_array_is_decl_as_ptr(type))
3563 unsigned int absoff = type->typestring_offset;
3564 short reloff = absoff - (*typeformat_offset + 2);
3565 off = *typeformat_offset;
3566 print_file(file, 0, "/* %d */\n", off);
3567 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3568 string_of_type(ptr_type));
3569 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3570 reloff, reloff, absoff);
3571 if (ptr_type != FC_RP) update_tfsoff( type, off, file );
3572 *typeformat_offset += 4;
3574 type->details.array.ptr_tfsoff = off;
3576 return off;
3578 case TGT_STRUCT:
3579 return write_struct_tfs(file, type, name, typeformat_offset);
3580 case TGT_UNION:
3581 return write_union_tfs(file, attrs, type, typeformat_offset);
3582 case TGT_ENUM:
3583 case TGT_BASIC:
3584 /* nothing to do */
3585 return 0;
3586 case TGT_RANGE:
3588 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3589 if (!range_list)
3590 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3591 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3593 case TGT_IFACE_POINTER:
3594 return write_ip_tfs(file, attrs, type, typeformat_offset);
3595 case TGT_POINTER:
3597 enum type_context ref_context;
3598 type_t *ref = type_pointer_get_ref(type);
3600 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3601 ref_context = TYPE_CONTEXT_PARAM;
3602 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3603 ref_context = TYPE_CONTEXT_CONTAINER;
3604 else
3605 ref_context = context;
3607 if (is_string_type(attrs, ref))
3609 if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3610 write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
3612 offset = write_type_tfs(file, indent, attrs, ref, name, ref_context, typeformat_offset);
3613 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3614 return 0;
3615 return offset;
3618 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3619 ref_context, typeformat_offset);
3620 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3621 return 0;
3622 return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
3624 case TGT_INVALID:
3625 break;
3627 error("invalid type %s for var %s\n", type->name, name);
3628 return 0;
3631 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3632 const char *name, int write_ptr, unsigned int *tfsoff)
3634 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3637 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3638 type_pred_t pred, unsigned int *typeformat_offset)
3640 var_t *var;
3641 const statement_t *stmt;
3643 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3645 const type_t *iface;
3646 const statement_t *stmt_func;
3648 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3649 continue;
3651 iface = stmt->u.type;
3652 if (!pred(iface))
3653 continue;
3655 current_iface = iface;
3656 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3658 const var_t *func = stmt_func->u.var;
3659 current_func = func;
3660 if (is_local(func->attrs)) continue;
3662 var = type_function_get_retval(func->type);
3663 if (!is_void(var->type))
3664 var->typestring_offset = write_type_tfs( file, 2, func->attrs, var->type, func->name,
3665 TYPE_CONTEXT_PARAM, typeformat_offset);
3667 if (type_get_function_args(func->type))
3668 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), var_t, entry )
3669 var->typestring_offset = write_type_tfs( file, 2, var->attrs, var->type, var->name,
3670 TYPE_CONTEXT_TOPLEVELPARAM,
3671 typeformat_offset );
3675 return *typeformat_offset + 1;
3678 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3680 unsigned int typeformat_offset = 2;
3682 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3686 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3688 int indent = 0;
3690 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3691 print_file(file, indent, "{\n");
3692 indent++;
3693 print_file(file, indent, "0,\n");
3694 print_file(file, indent, "{\n");
3695 indent++;
3696 print_file(file, indent, "NdrFcShort(0x0),\n");
3698 set_all_tfswrite(TRUE);
3699 process_tfs(file, stmts, pred);
3701 print_file(file, indent, "0x0\n");
3702 indent--;
3703 print_file(file, indent, "}\n");
3704 indent--;
3705 print_file(file, indent, "};\n");
3706 print_file(file, indent, "\n");
3709 static unsigned int get_required_buffer_size_type(
3710 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3712 *alignment = 0;
3713 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3715 case TGT_USER_TYPE:
3717 const char *uname = NULL;
3718 const type_t *utype = get_user_type(type, &uname);
3719 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3721 case TGT_BASIC:
3722 switch (get_basic_fc(type))
3724 case FC_BYTE:
3725 case FC_CHAR:
3726 case FC_USMALL:
3727 case FC_SMALL:
3728 *alignment = 4;
3729 return 1;
3731 case FC_WCHAR:
3732 case FC_USHORT:
3733 case FC_SHORT:
3734 *alignment = 4;
3735 return 2;
3737 case FC_ULONG:
3738 case FC_LONG:
3739 case FC_FLOAT:
3740 case FC_ERROR_STATUS_T:
3741 *alignment = 4;
3742 return 4;
3744 case FC_HYPER:
3745 case FC_DOUBLE:
3746 *alignment = 8;
3747 return 8;
3749 case FC_INT3264:
3750 case FC_UINT3264:
3751 assert( pointer_size );
3752 *alignment = pointer_size;
3753 return pointer_size;
3755 case FC_IGNORE:
3756 case FC_BIND_PRIMITIVE:
3757 return 0;
3759 default:
3760 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3761 get_basic_fc(type));
3762 return 0;
3764 break;
3766 case TGT_ENUM:
3767 switch (get_enum_fc(type))
3769 case FC_ENUM32:
3770 *alignment = 4;
3771 return 4;
3772 case FC_ENUM16:
3773 *alignment = 4;
3774 return 2;
3776 break;
3778 case TGT_STRUCT:
3779 if (get_struct_fc(type) == FC_STRUCT)
3781 if (!type_struct_get_fields(type)) return 0;
3782 return fields_memsize(type_struct_get_fields(type), alignment);
3784 break;
3786 case TGT_POINTER:
3788 unsigned int size, align;
3789 const type_t *ref = type_pointer_get_ref(type);
3790 if (is_string_type( attrs, ref )) break;
3791 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3792 if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
3794 size += 4 + align;
3795 align = 4;
3797 *alignment = align;
3798 return size;
3801 case TGT_ARRAY:
3802 if (get_pointer_fc(type, attrs, toplevel_param) == FC_RP)
3804 switch (get_array_fc(type))
3806 case FC_SMFARRAY:
3807 case FC_LGFARRAY:
3808 return type_array_get_dim(type) *
3809 get_required_buffer_size_type(type_array_get_element(type), name,
3810 NULL, FALSE, alignment);
3813 break;
3815 default:
3816 break;
3818 return 0;
3821 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3823 int in_attr = is_attr(var->attrs, ATTR_IN);
3824 int out_attr = is_attr(var->attrs, ATTR_OUT);
3826 if (!in_attr && !out_attr)
3827 in_attr = 1;
3829 *alignment = 0;
3831 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3832 pass == PASS_RETURN)
3834 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3836 *alignment = 4;
3837 return 20;
3840 if (!is_string_type(var->attrs, var->type))
3841 return get_required_buffer_size_type(var->type, var->name,
3842 var->attrs, TRUE, alignment);
3844 return 0;
3847 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3849 const var_t *var;
3850 unsigned int total_size = 0, alignment;
3852 if (type_get_function_args(func->type))
3854 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3856 total_size += get_required_buffer_size(var, &alignment, pass);
3857 total_size += alignment;
3861 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3863 var_t v = *func;
3864 v.type = type_function_get_rettype(func->type);
3865 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3866 total_size += alignment;
3868 return total_size;
3871 static void print_phase_function(FILE *file, int indent, const char *type,
3872 const char *local_var_prefix, enum remoting_phase phase,
3873 const var_t *var, unsigned int type_offset)
3875 const char *function;
3876 switch (phase)
3878 case PHASE_BUFFERSIZE:
3879 function = "BufferSize";
3880 break;
3881 case PHASE_MARSHAL:
3882 function = "Marshall";
3883 break;
3884 case PHASE_UNMARSHAL:
3885 function = "Unmarshall";
3886 break;
3887 case PHASE_FREE:
3888 function = "Free";
3889 break;
3890 default:
3891 assert(0);
3892 return;
3895 print_file(file, indent, "Ndr%s%s(\n", type, function);
3896 indent++;
3897 print_file(file, indent, "&__frame->_StubMsg,\n");
3898 print_file(file, indent, "%s%s%s%s%s,\n",
3899 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3900 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3901 local_var_prefix,
3902 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3903 var->name);
3904 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3905 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3906 if (phase == PHASE_UNMARSHAL)
3907 print_file(file, indent, "0);\n");
3908 indent--;
3911 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3912 enum remoting_phase phase, enum pass pass, const var_t *var,
3913 const char *varname)
3915 type_t *type = var->type;
3916 unsigned int alignment = 0;
3918 /* no work to do for other phases, buffer sizing is done elsewhere */
3919 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3920 return;
3922 if (type_get_type(type) == TYPE_ENUM ||
3923 (type_get_type(type) == TYPE_BASIC &&
3924 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3925 pointer_size != 4))
3927 unsigned char fc;
3929 if (type_get_type(type) == TYPE_ENUM)
3930 fc = get_enum_fc(type);
3931 else
3932 fc = get_basic_fc(type);
3934 if (phase == PHASE_MARSHAL)
3935 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3936 else
3937 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3938 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3939 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3940 local_var_prefix,
3941 var->name);
3942 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3944 else
3946 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3947 switch (get_basic_fc(ref))
3949 case FC_BYTE:
3950 case FC_CHAR:
3951 case FC_SMALL:
3952 case FC_USMALL:
3953 alignment = 1;
3954 break;
3956 case FC_WCHAR:
3957 case FC_USHORT:
3958 case FC_SHORT:
3959 alignment = 2;
3960 break;
3962 case FC_ULONG:
3963 case FC_LONG:
3964 case FC_FLOAT:
3965 case FC_ERROR_STATUS_T:
3966 /* pointer_size must be 4 if we got here in these two cases */
3967 case FC_INT3264:
3968 case FC_UINT3264:
3969 alignment = 4;
3970 break;
3972 case FC_HYPER:
3973 case FC_DOUBLE:
3974 alignment = 8;
3975 break;
3977 case FC_IGNORE:
3978 case FC_BIND_PRIMITIVE:
3979 /* no marshalling needed */
3980 return;
3982 default:
3983 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3984 var->name, get_basic_fc(ref));
3987 if (phase == PHASE_MARSHAL && alignment > 1)
3988 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3989 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3990 alignment - 1, alignment - 1);
3992 if (phase == PHASE_MARSHAL)
3994 print_file(file, indent, "*(");
3995 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3996 if (is_ptr(type))
3997 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3998 else
3999 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
4000 fprintf(file, "%s%s", local_var_prefix, varname);
4001 fprintf(file, ";\n");
4003 else if (phase == PHASE_UNMARSHAL)
4005 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
4006 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4007 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
4008 print_file(file, indent, "{\n");
4009 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
4010 print_file(file, indent, "}\n");
4011 print_file(file, indent, "%s%s%s",
4012 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
4013 local_var_prefix, varname);
4014 if (pass == PASS_IN && is_ptr(type))
4015 fprintf(file, " = (");
4016 else
4017 fprintf(file, " = *(");
4018 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4019 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
4022 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
4023 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
4024 fprintf(file, ");\n");
4028 /* returns whether the MaxCount, Offset or ActualCount members need to be
4029 * filled in for the specified phase */
4030 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
4032 return (phase != PHASE_UNMARSHAL);
4035 expr_t *get_size_is_expr(const type_t *t, const char *name)
4037 expr_t *x = NULL;
4039 for ( ; is_array(t); t = type_array_get_element(t))
4040 if (type_array_has_conformance(t) &&
4041 type_array_get_conformance(t)->type != EXPR_VOID)
4043 if (!x)
4044 x = type_array_get_conformance(t);
4045 else
4046 error("%s: multidimensional conformant"
4047 " arrays not supported at the top level\n",
4048 name);
4051 return x;
4054 void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4055 enum remoting_phase phase, const var_t *var, int valid_variance)
4057 const type_t *type = var->type;
4058 /* get fundamental type for the argument */
4059 for (;;)
4061 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4063 case TGT_ARRAY:
4064 if (is_conformance_needed_for_phase(phase))
4066 if (type_array_has_conformance(type) &&
4067 type_array_get_conformance(type)->type != EXPR_VOID)
4069 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4070 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4071 fprintf(file, ";\n\n");
4073 if (type_array_has_variance(type))
4075 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4076 if (valid_variance)
4078 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4079 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4080 fprintf(file, ";\n\n");
4082 else
4083 print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4086 break;
4087 case TGT_UNION:
4088 if (type_get_type(type) == TYPE_UNION &&
4089 is_conformance_needed_for_phase(phase))
4091 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4092 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4093 fprintf(file, ";\n\n");
4095 break;
4096 case TGT_IFACE_POINTER:
4098 expr_t *iid;
4100 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4102 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4103 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4104 fprintf( file, ";\n\n" );
4106 break;
4108 case TGT_POINTER:
4109 type = type_pointer_get_ref(type);
4110 continue;
4111 case TGT_INVALID:
4112 case TGT_USER_TYPE:
4113 case TGT_CTXT_HANDLE:
4114 case TGT_CTXT_HANDLE_POINTER:
4115 case TGT_STRING:
4116 case TGT_BASIC:
4117 case TGT_ENUM:
4118 case TGT_STRUCT:
4119 case TGT_RANGE:
4120 break;
4122 break;
4126 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4127 enum pass pass, enum remoting_phase phase, const var_t *var)
4129 int in_attr, out_attr, pointer_type;
4130 const char *type_str = NULL;
4131 const type_t *type = var->type;
4132 unsigned int alignment, start_offset = type->typestring_offset;
4134 if (is_ptr(type) || is_array(type))
4135 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4136 else
4137 pointer_type = 0;
4139 in_attr = is_attr(var->attrs, ATTR_IN);
4140 out_attr = is_attr(var->attrs, ATTR_OUT);
4141 if (!in_attr && !out_attr)
4142 in_attr = 1;
4144 if (phase != PHASE_FREE)
4145 switch (pass)
4147 case PASS_IN:
4148 if (!in_attr) return;
4149 break;
4150 case PASS_OUT:
4151 if (!out_attr) return;
4152 break;
4153 case PASS_RETURN:
4154 break;
4157 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4159 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
4161 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4163 case TGT_CTXT_HANDLE:
4164 case TGT_CTXT_HANDLE_POINTER:
4165 if (phase == PHASE_MARSHAL)
4167 if (pass == PASS_IN)
4169 /* if the context_handle attribute appears in the chain of types
4170 * without pointers being followed, then the context handle must
4171 * be direct, otherwise it is a pointer */
4172 const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
4173 print_file(file, indent, "NdrClientContextMarshall(\n");
4174 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4175 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
4176 var->name);
4177 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4179 else
4181 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4182 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4183 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4184 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4185 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4188 else if (phase == PHASE_UNMARSHAL)
4190 if (pass == PASS_OUT)
4192 if (!in_attr)
4193 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4194 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4195 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4196 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4197 print_file(file, indent + 1, "__frame->_Handle);\n");
4199 else
4201 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4202 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4203 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4206 break;
4207 case TGT_USER_TYPE:
4208 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4209 break;
4210 case TGT_STRING:
4211 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4212 pointer_type != FC_RP)
4214 /* strings returned are assumed to be global and hence don't
4215 * need freeing */
4216 if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
4217 print_phase_function(file, indent, "Pointer", local_var_prefix,
4218 phase, var, start_offset);
4219 else if (pointer_type == FC_RP && phase == PHASE_FREE &&
4220 !in_attr && is_conformant_array(type))
4222 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4223 indent++;
4224 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4227 else
4229 unsigned int real_start_offset = start_offset;
4230 /* skip over pointer description straight to string description */
4231 if (is_declptr(type))
4233 if (is_conformant_array(type))
4234 real_start_offset += 4;
4235 else
4236 real_start_offset += 2;
4238 if (is_array(type) && !is_conformant_array(type))
4239 print_phase_function(file, indent, "NonConformantString",
4240 local_var_prefix, phase, var,
4241 real_start_offset);
4242 else
4243 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4244 phase, var, real_start_offset);
4246 break;
4247 case TGT_ARRAY:
4249 unsigned char tc = get_array_fc(type);
4250 const char *array_type = NULL;
4252 /* We already have the size_is expression since it's at the
4253 top level, but do checks for multidimensional conformant
4254 arrays. When we handle them, we'll need to extend this
4255 function to return a list, and then we'll actually use
4256 the return value. */
4257 get_size_is_expr(type, var->name);
4259 switch (tc)
4261 case FC_SMFARRAY:
4262 case FC_LGFARRAY:
4263 array_type = "FixedArray";
4264 break;
4265 case FC_SMVARRAY:
4266 case FC_LGVARRAY:
4267 array_type = "VaryingArray";
4268 break;
4269 case FC_CARRAY:
4270 array_type = "ConformantArray";
4271 break;
4272 case FC_CVARRAY:
4273 array_type = "ConformantVaryingArray";
4274 break;
4275 case FC_BOGUS_ARRAY:
4276 array_type = "ComplexArray";
4277 break;
4280 if (pointer_type != FC_RP) array_type = "Pointer";
4282 if (phase == PHASE_FREE && pointer_type == FC_RP)
4284 /* these are all unmarshalled by allocating memory */
4285 if (tc == FC_BOGUS_ARRAY ||
4286 tc == FC_CVARRAY ||
4287 ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
4288 (tc == FC_CARRAY && !in_attr))
4290 if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff)
4292 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
4293 type->details.array.ptr_tfsoff);
4294 break;
4296 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4297 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4298 indent++;
4299 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4300 break;
4303 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4304 break;
4306 case TGT_BASIC:
4307 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4308 break;
4309 case TGT_ENUM:
4310 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4311 break;
4312 case TGT_RANGE:
4313 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4314 /* Note: this goes beyond what MIDL does - it only supports arguments
4315 * with the [range] attribute in Oicf mode */
4316 if (phase == PHASE_UNMARSHAL)
4318 const expr_t *range_min;
4319 const expr_t *range_max;
4320 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4321 if (!range_list)
4322 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4323 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4324 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4326 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4327 write_type_decl(file, var->type, NULL);
4328 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4329 write_type_decl(file, var->type, NULL);
4330 fprintf(file, ")0x%x))\n", range_max->cval);
4331 print_file(file, indent, "{\n");
4332 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4333 print_file(file, indent, "}\n");
4335 break;
4336 case TGT_STRUCT:
4337 switch (get_struct_fc(type))
4339 case FC_STRUCT:
4340 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4341 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4342 break;
4343 case FC_PSTRUCT:
4344 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4345 break;
4346 case FC_CSTRUCT:
4347 case FC_CPSTRUCT:
4348 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4349 break;
4350 case FC_CVSTRUCT:
4351 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4352 break;
4353 case FC_BOGUS_STRUCT:
4354 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4355 break;
4356 default:
4357 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4359 break;
4360 case TGT_UNION:
4362 const char *union_type = NULL;
4364 if (type_get_type(type) == TYPE_UNION)
4365 union_type = "NonEncapsulatedUnion";
4366 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4367 union_type = "EncapsulatedUnion";
4369 print_phase_function(file, indent, union_type, local_var_prefix,
4370 phase, var, start_offset);
4371 break;
4373 case TGT_POINTER:
4375 const type_t *ref = type_pointer_get_ref(type);
4376 if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
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 /* base types have known sizes, so don't need a sizing pass
4383 * and don't have any memory to free and so don't need a
4384 * freeing pass */
4385 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4386 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4387 break;
4388 case TGT_STRUCT:
4389 switch (get_struct_fc(ref))
4391 case FC_STRUCT:
4392 /* simple structs have known sizes, so don't need a sizing
4393 * pass and don't have any memory to free and so don't
4394 * need a freeing pass */
4395 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4396 type_str = "SimpleStruct";
4397 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4399 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4400 indent++;
4401 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4402 indent--;
4404 break;
4405 case FC_PSTRUCT:
4406 type_str = "SimpleStruct";
4407 break;
4408 case FC_CSTRUCT:
4409 case FC_CPSTRUCT:
4410 type_str = "ConformantStruct";
4411 break;
4412 case FC_CVSTRUCT:
4413 type_str = "ConformantVaryingStruct";
4414 break;
4415 case FC_BOGUS_STRUCT:
4416 type_str = "ComplexStruct";
4417 break;
4418 default:
4419 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4422 if (type_str)
4424 if (phase == PHASE_FREE)
4425 type_str = "Pointer";
4426 else
4427 start_offset = ref->typestring_offset;
4428 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4430 break;
4431 case TGT_UNION:
4432 if (phase == PHASE_FREE)
4433 type_str = "Pointer";
4434 else
4436 if (type_get_type(ref) == TYPE_UNION)
4437 type_str = "NonEncapsulatedUnion";
4438 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4439 type_str = "EncapsulatedUnion";
4441 start_offset = ref->typestring_offset;
4444 print_phase_function(file, indent, type_str, local_var_prefix,
4445 phase, var, start_offset);
4446 break;
4447 case TGT_USER_TYPE:
4448 if (phase != PHASE_FREE)
4450 type_str = "UserMarshal";
4451 start_offset = ref->typestring_offset;
4453 else type_str = "Pointer";
4455 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4456 break;
4457 case TGT_STRING:
4458 case TGT_POINTER:
4459 case TGT_ARRAY:
4460 case TGT_RANGE:
4461 case TGT_IFACE_POINTER:
4462 case TGT_CTXT_HANDLE:
4463 case TGT_CTXT_HANDLE_POINTER:
4464 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4465 break;
4466 case TGT_INVALID:
4467 assert(0);
4468 break;
4470 else
4471 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4472 break;
4474 case TGT_IFACE_POINTER:
4475 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4476 break;
4477 case TGT_INVALID:
4478 assert(0);
4479 break;
4481 fprintf(file, "\n");
4484 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4485 enum pass pass, enum remoting_phase phase)
4487 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4489 unsigned int size = get_function_buffer_size( func, pass );
4490 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4493 if (pass == PASS_RETURN)
4495 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
4496 type_function_get_retval(func->type) );
4498 else
4500 const var_t *var;
4501 if (!type_get_function_args(func->type))
4502 return;
4503 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4504 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4509 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4511 unsigned int offset = 0;
4512 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4513 return offset;
4516 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4518 const statement_t *stmt;
4519 unsigned int size = 1;
4521 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4523 const type_t *iface;
4524 const statement_t *stmt_func;
4526 if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4527 continue;
4529 iface = stmt->u.type;
4530 if (!pred(iface))
4531 continue;
4533 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4535 const var_t *func = stmt_func->u.var;
4536 if (!is_local(func->attrs))
4537 size += get_size_procformatstring_func( iface, func );
4540 return size;
4543 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4545 set_all_tfswrite(FALSE);
4546 return process_tfs(NULL, stmts, pred);
4549 void declare_stub_args( FILE *file, int indent, const var_t *func )
4551 int in_attr, out_attr;
4552 int i = 0;
4553 const var_t *var = type_function_get_retval(func->type);
4555 /* declare return value */
4556 if (!is_void(var->type))
4558 print_file(file, indent, "%s", "");
4559 write_type_decl(file, var->type, var->name);
4560 fprintf(file, ";\n");
4563 if (!type_get_function_args(func->type))
4564 return;
4566 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4568 in_attr = is_attr(var->attrs, ATTR_IN);
4569 out_attr = is_attr(var->attrs, ATTR_OUT);
4570 if (!out_attr && !in_attr)
4571 in_attr = 1;
4573 if (is_context_handle(var->type))
4574 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4575 else
4577 if (!in_attr && !is_conformant_array(var->type))
4579 type_t *type_to_print;
4580 char name[16];
4581 print_file(file, indent, "%s", "");
4582 if (type_get_type(var->type) == TYPE_ARRAY &&
4583 !type_array_is_decl_as_ptr(var->type))
4584 type_to_print = var->type;
4585 else
4586 type_to_print = type_pointer_get_ref(var->type);
4587 sprintf(name, "_W%u", i++);
4588 write_type_decl(file, type_to_print, name);
4589 fprintf(file, ";\n");
4592 print_file(file, indent, "%s", "");
4593 write_type_decl_left(file, var->type);
4594 fprintf(file, " ");
4595 if (type_get_type(var->type) == TYPE_ARRAY &&
4596 !type_array_is_decl_as_ptr(var->type)) {
4597 fprintf(file, "(*%s)", var->name);
4598 } else
4599 fprintf(file, "%s", var->name);
4600 write_type_right(file, var->type, FALSE);
4601 fprintf(file, ";\n");
4603 if (decl_indirect(var->type))
4604 print_file(file, indent, "void *_p_%s;\n", var->name);
4610 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4612 int in_attr, out_attr;
4613 int i = 0, sep = 0;
4614 const var_t *var;
4615 type_t *ref;
4617 if (!type_get_function_args(func->type))
4618 return;
4620 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4622 in_attr = is_attr(var->attrs, ATTR_IN);
4623 out_attr = is_attr(var->attrs, ATTR_OUT);
4624 if (!out_attr && !in_attr)
4625 in_attr = 1;
4627 if (!in_attr)
4629 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4631 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4633 case TGT_CTXT_HANDLE_POINTER:
4634 fprintf(file, " = NdrContextHandleInitialize(\n");
4635 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4636 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4637 var->typestring_offset);
4638 break;
4639 case TGT_ARRAY:
4640 if (type_array_has_conformance(var->type))
4642 unsigned int size;
4643 type_t *type;
4645 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4646 for (type = var->type;
4647 is_array(type) && type_array_has_conformance(type);
4648 type = type_array_get_element(type))
4650 write_expr(file, type_array_get_conformance(type), TRUE,
4651 TRUE, NULL, NULL, local_var_prefix);
4652 fprintf(file, " * ");
4654 size = type_memsize(type);
4655 fprintf(file, "%u);\n", size);
4657 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4658 for (type = var->type;
4659 is_array(type) && type_array_has_conformance(type);
4660 type = type_array_get_element(type))
4662 write_expr(file, type_array_get_conformance(type), TRUE,
4663 TRUE, NULL, NULL, local_var_prefix);
4664 fprintf(file, " * ");
4666 size = type_memsize(type);
4667 fprintf(file, "%u);\n", size);
4669 else
4670 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4671 break;
4672 case TGT_POINTER:
4673 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4674 ref = type_pointer_get_ref(var->type);
4675 switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
4677 case TGT_BASIC:
4678 case TGT_ENUM:
4679 case TGT_POINTER:
4680 case TGT_RANGE:
4681 case TGT_IFACE_POINTER:
4682 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4683 break;
4684 case TGT_USER_TYPE:
4685 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4686 local_var_prefix, i, local_var_prefix, i);
4687 break;
4688 case TGT_ARRAY:
4689 if (type_array_is_decl_as_ptr(ref))
4691 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4692 break;
4694 ref = type_array_get_element(ref);
4695 /* fall through */
4696 case TGT_STRUCT:
4697 case TGT_UNION:
4698 if (type_has_pointers(ref))
4699 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4700 local_var_prefix, i, local_var_prefix, i);
4701 break;
4702 case TGT_CTXT_HANDLE:
4703 case TGT_CTXT_HANDLE_POINTER:
4704 case TGT_INVALID:
4705 case TGT_STRING:
4706 /* not initialised */
4707 break;
4709 i++;
4710 break;
4711 default:
4712 break;
4715 sep = 1;
4718 if (sep)
4719 fprintf(file, "\n");
4723 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
4724 const char *var_decl, int add_retval )
4726 var_t *retval = type_function_get_retval( func );
4727 const var_list_t *args = type_get_function_args( func );
4728 const var_t *arg;
4729 int needs_packing;
4730 unsigned int align = 0;
4732 if (args)
4733 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4734 if (!is_array( arg->type )) type_memsize_and_alignment( arg->type, &align );
4736 needs_packing = (align > pointer_size);
4738 if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
4739 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4740 print_file(file, 1, "{\n" );
4741 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4743 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4745 print_file(file, 2, "%s", "");
4746 write_type_left( file, (type_t *)arg->type, NAME_DEFAULT, TRUE );
4747 if (needs_space_after( arg->type )) fputc( ' ', file );
4748 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4750 /* FIXME: should check for large args being passed by pointer */
4751 align = 0;
4752 if (is_array( arg->type ) || is_ptr( arg->type )) align = pointer_size;
4753 else type_memsize_and_alignment( arg->type, &align );
4755 if (align >= pointer_size)
4756 fprintf( file, "%s;\n", arg->name );
4757 else
4758 fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4760 if (add_retval && !is_void( retval->type ))
4762 print_file(file, 2, "%s", "");
4763 write_type_decl( file, retval->type, retval->name );
4764 if (is_array( retval->type ) || is_ptr( retval->type ) ||
4765 type_memsize( retval->type ) == pointer_size)
4766 fprintf( file, ";\n" );
4767 else
4768 fprintf( file, " DECLSPEC_ALIGN(%u);\n", pointer_size );
4770 print_file(file, 1, "} %s;\n", var_decl );
4771 if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
4772 print_file( file, 0, "\n" );
4775 void write_pointer_checks( FILE *file, int indent, const var_t *func )
4777 const var_list_t *args = type_get_function_args( func->type );
4778 const var_t *var;
4780 if (!args) return;
4782 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
4783 if (cant_be_null( var ))
4784 print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
4787 int write_expr_eval_routines(FILE *file, const char *iface)
4789 static const char *var_name = "pS";
4790 static const char *var_name_expr = "pS->";
4791 int result = 0;
4792 struct expr_eval_routine *eval;
4793 unsigned short callback_offset = 0;
4795 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4797 const char *name = eval->name;
4798 result = 1;
4800 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4801 eval->iface ? eval->iface->name : iface, name, callback_offset);
4802 print_file(file, 0, "{\n");
4803 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4805 write_func_param_struct( file, eval->iface, eval->cont_type,
4806 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
4808 else
4810 print_file(file, 1, "%s", "");
4811 write_type_left(file, (type_t *)eval->cont_type, NAME_DEFAULT, TRUE);
4812 fprintf(file, " *%s = (", var_name);
4813 write_type_left(file, (type_t *)eval->cont_type, NAME_DEFAULT, TRUE);
4814 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4816 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4817 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4818 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4819 fprintf(file, ";\n");
4820 print_file(file, 0, "}\n\n");
4821 callback_offset++;
4823 return result;
4826 void write_expr_eval_routine_list(FILE *file, const char *iface)
4828 struct expr_eval_routine *eval;
4829 struct expr_eval_routine *cursor;
4830 unsigned short callback_offset = 0;
4832 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4833 fprintf(file, "{\n");
4835 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4837 print_file(file, 1, "%s_%sExprEval_%04u,\n",
4838 eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
4839 callback_offset++;
4840 list_remove(&eval->entry);
4841 free(eval->name);
4842 free(eval);
4845 fprintf(file, "};\n\n");
4848 void write_user_quad_list(FILE *file)
4850 user_type_t *ut;
4852 if (list_empty(&user_type_list))
4853 return;
4855 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4856 fprintf(file, "{\n");
4857 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4859 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4860 print_file(file, 1, "{\n");
4861 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4862 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4863 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4864 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4865 print_file(file, 1, "}%s\n", sep);
4867 fprintf(file, "};\n\n");
4870 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4872 const struct str_list_entry_t *endpoint;
4873 const char *p;
4875 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4876 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4877 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4879 print_file( f, 1, "{ (const unsigned char *)\"" );
4880 for (p = endpoint->str; *p && *p != ':'; p++)
4882 if (*p == '"' || *p == '\\') fputc( '\\', f );
4883 fputc( *p, f );
4885 if (!*p) goto error;
4886 if (p[1] != '[') goto error;
4888 fprintf( f, "\", (const unsigned char *)\"" );
4889 for (p += 2; *p && *p != ']'; p++)
4891 if (*p == '"' || *p == '\\') fputc( '\\', f );
4892 fputc( *p, f );
4894 if (*p != ']') goto error;
4895 fprintf( f, "\" },\n" );
4897 print_file( f, 0, "};\n\n" );
4898 return;
4900 error:
4901 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4904 void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
4905 const char *prefix, unsigned int proc_offset )
4907 type_t *rettype = type_function_get_rettype( func->type );
4908 int has_ret = !is_void( rettype );
4909 const var_list_t *args = type_get_function_args( func->type );
4910 const var_t *arg;
4911 int len, needs_params = 0;
4913 /* we need a param structure if we have more than one arg */
4914 if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
4916 print_file( file, 0, "{\n");
4917 if (needs_params)
4919 if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4920 write_func_param_struct( file, iface, func->type, "__params", FALSE );
4921 if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
4922 if (args)
4923 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4924 print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
4926 else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4928 len = fprintf( file, " %s%s( ",
4929 has_ret ? "_RetVal = " : "",
4930 get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
4931 fprintf( file, "&%s_StubDesc,", prefix );
4932 fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
4933 if (needs_params)
4935 fprintf( file, ",\n%*s&__params", len, "" );
4937 else if (pointer_size == 8)
4939 if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
4940 if (args)
4941 LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4942 fprintf( file, ",\n%*s%s", len, "", arg->name );
4944 else
4946 if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
4947 else if (args)
4949 arg = LIST_ENTRY( list_head(args), const var_t, entry );
4950 fprintf( file, ",\n%*s&%s", len, "", arg->name );
4953 fprintf( file, " );\n" );
4954 if (has_ret)
4956 print_file( file, 1, "return (" );
4957 write_type_decl_left(file, rettype);
4958 fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
4960 print_file( file, 0, "}\n\n");
4963 void write_exceptions( FILE *file )
4965 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4966 fprintf( file, "\n");
4967 fprintf( file, "#include \"wine/exception.h\"\n");
4968 fprintf( file, "#undef RpcTryExcept\n");
4969 fprintf( file, "#undef RpcExcept\n");
4970 fprintf( file, "#undef RpcEndExcept\n");
4971 fprintf( file, "#undef RpcTryFinally\n");
4972 fprintf( file, "#undef RpcFinally\n");
4973 fprintf( file, "#undef RpcEndFinally\n");
4974 fprintf( file, "#undef RpcExceptionCode\n");
4975 fprintf( file, "#undef RpcAbnormalTermination\n");
4976 fprintf( file, "\n");
4977 fprintf( file, "struct __exception_frame;\n");
4978 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4979 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4980 fprintf( file, "\n");
4981 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4982 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4983 fprintf( file, " __filter_func filter; \\\n");
4984 fprintf( file, " __finally_func finally; \\\n");
4985 fprintf( file, " sigjmp_buf jmp; \\\n");
4986 fprintf( file, " DWORD code; \\\n");
4987 fprintf( file, " unsigned char abnormal_termination; \\\n");
4988 fprintf( file, " unsigned char filter_level; \\\n");
4989 fprintf( file, " unsigned char finally_level;\n");
4990 fprintf( file, "\n");
4991 fprintf( file, "struct __exception_frame\n{\n");
4992 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4993 fprintf( file, "};\n");
4994 fprintf( file, "\n");
4995 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4996 fprintf( file, "{\n");
4997 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4998 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4999 fprintf( file, " {\n");
5000 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5001 fprintf( file, " exc_frame->finally( exc_frame );\n");
5002 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
5003 fprintf( file, " }\n");
5004 fprintf( file, " exc_frame->filter_level = 0;\n");
5005 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
5006 fprintf( file, "}\n");
5007 fprintf( file, "\n");
5008 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
5009 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
5010 fprintf( file, " CONTEXT *context,\n");
5011 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
5012 fprintf( file, "{\n");
5013 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
5014 fprintf( file, "\n");
5015 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
5016 fprintf( file, " {\n" );
5017 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
5018 fprintf( file, " {\n" );
5019 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
5020 fprintf( file, " exc_frame->finally( exc_frame );\n");
5021 fprintf( file, " }\n" );
5022 fprintf( file, " return ExceptionContinueSearch;\n");
5023 fprintf( file, " }\n" );
5024 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
5025 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5026 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5027 fprintf( file, " return ExceptionContinueSearch;\n");
5028 fprintf( file, "}\n");
5029 fprintf( file, "\n");
5030 fprintf( file, "#define RpcTryExcept \\\n");
5031 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
5032 fprintf( file, " { \\\n");
5033 fprintf( file, " if (!__frame->finally_level) \\\n" );
5034 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5035 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
5036 fprintf( file, "\n");
5037 fprintf( file, "#define RpcExcept(expr) \\\n");
5038 fprintf( file, " if (!__frame->finally_level) \\\n" );
5039 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5040 fprintf( file, " __frame->filter_level = 0; \\\n" );
5041 fprintf( file, " } \\\n");
5042 fprintf( file, " else \\\n");
5043 fprintf( file, "\n");
5044 fprintf( file, "#define RpcEndExcept\n");
5045 fprintf( file, "\n");
5046 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
5047 fprintf( file, "\n");
5048 fprintf( file, "#define RpcTryFinally \\\n");
5049 fprintf( file, " if (!__frame->filter_level) \\\n");
5050 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
5051 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
5052 fprintf( file, "\n");
5053 fprintf( file, "#define RpcFinally \\\n");
5054 fprintf( file, " if (!__frame->filter_level) \\\n");
5055 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
5056 fprintf( file, " __frame->finally_level = 0;\n");
5057 fprintf( file, "\n");
5058 fprintf( file, "#define RpcEndFinally\n");
5059 fprintf( file, "\n");
5060 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5061 fprintf( file, "\n");
5062 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5063 fprintf( file, " do { \\\n");
5064 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5065 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5066 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
5067 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
5068 fprintf( file, " __frame->filter_level = 0; \\\n");
5069 fprintf( file, " __frame->finally_level = 0; \\\n");
5070 fprintf( file, " } while (0)\n");
5071 fprintf( file, "\n");
5072 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5073 fprintf( file, "\n");
5074 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5075 fprintf( file, " do { (void)(filter_func); } while(0)\n");
5076 fprintf( file, "\n");
5077 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
5078 fprintf( file, " DWORD code;\n");
5079 fprintf( file, "\n");
5080 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");