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
23 #include "wine/port.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
58 const type_t *cont_type;
66 TYPE_CONTEXT_TOPLEVELPARAM,
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 type_memsize_and_alignment(const type_t *t, unsigned int *align);
88 static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
89 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
90 const char *name, int write_ptr, unsigned int *tfsoff);
91 static const var_t *find_array_or_string_in_struct(const type_t *type);
92 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
93 type_t *type, int toplevel_param,
94 const char *name, unsigned int *typestring_offset);
95 static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
96 const attr_list_t *attrs, int toplevel_param,
97 unsigned int *alignment );
98 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
100 static const char *string_of_type(unsigned char type)
104 case RPC_FC_BYTE: return "FC_BYTE";
105 case RPC_FC_CHAR: return "FC_CHAR";
106 case RPC_FC_SMALL: return "FC_SMALL";
107 case RPC_FC_USMALL: return "FC_USMALL";
108 case RPC_FC_WCHAR: return "FC_WCHAR";
109 case RPC_FC_SHORT: return "FC_SHORT";
110 case RPC_FC_USHORT: return "FC_USHORT";
111 case RPC_FC_LONG: return "FC_LONG";
112 case RPC_FC_ULONG: return "FC_ULONG";
113 case RPC_FC_FLOAT: return "FC_FLOAT";
114 case RPC_FC_HYPER: return "FC_HYPER";
115 case RPC_FC_DOUBLE: return "FC_DOUBLE";
116 case RPC_FC_ENUM16: return "FC_ENUM16";
117 case RPC_FC_ENUM32: return "FC_ENUM32";
118 case RPC_FC_IGNORE: return "FC_IGNORE";
119 case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
120 case RPC_FC_RP: return "FC_RP";
121 case RPC_FC_UP: return "FC_UP";
122 case RPC_FC_OP: return "FC_OP";
123 case RPC_FC_FP: return "FC_FP";
124 case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
125 case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
126 case RPC_FC_STRUCT: return "FC_STRUCT";
127 case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
128 case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
129 case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
130 case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
131 case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
132 case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
133 case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
134 case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
135 case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
136 case RPC_FC_CARRAY: return "FC_CARRAY";
137 case RPC_FC_CVARRAY: return "FC_CVARRAY";
138 case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
139 case RPC_FC_ALIGNM2: return "FC_ALIGNM2";
140 case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
141 case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
142 case RPC_FC_POINTER: return "FC_POINTER";
143 case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
144 case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
145 case RPC_FC_CSTRING: return "FC_CSTRING";
146 case RPC_FC_WSTRING: return "FC_WSTRING";
147 case RPC_FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
148 case RPC_FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
149 case RPC_FC_REPRESENT_AS: return "FC_REPRESENT_AS";
150 case RPC_FC_IP: return "FC_IP";
151 case RPC_FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
152 case RPC_FC_BIND_GENERIC: return "FC_BIND_GENERIC";
153 case RPC_FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
154 case RPC_FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
155 case RPC_FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
156 case RPC_FC_STRUCTPAD1: return "FC_STRUCTPAD1";
157 case RPC_FC_STRUCTPAD2: return "FC_STRUCTPAD2";
158 case RPC_FC_STRUCTPAD3: return "FC_STRUCTPAD3";
159 case RPC_FC_STRUCTPAD4: return "FC_STRUCTPAD4";
160 case RPC_FC_STRUCTPAD5: return "FC_STRUCTPAD5";
161 case RPC_FC_STRUCTPAD6: return "FC_STRUCTPAD6";
162 case RPC_FC_STRUCTPAD7: return "FC_STRUCTPAD7";
163 case RPC_FC_STRING_SIZED: return "FC_STRING_SIZED";
164 case RPC_FC_NO_REPEAT: return "FC_NO_REPEAT";
165 case RPC_FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
166 case RPC_FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
167 case RPC_FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
168 case RPC_FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
169 case RPC_FC_PP: return "FC_PP";
170 case RPC_FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
171 case RPC_FC_DEREFERENCE: return "FC_DEREFERENCE";
172 case RPC_FC_DIV_2: return "FC_DIV_2";
173 case RPC_FC_MULT_2: return "FC_MULT_2";
174 case RPC_FC_ADD_1: return "FC_ADD_1";
175 case RPC_FC_SUB_1: return "FC_SUB_1";
176 case RPC_FC_CALLBACK: return "FC_CALLBACK";
177 case RPC_FC_CONSTANT_IID: return "FC_CONSTANT_IID";
178 case RPC_FC_END: return "FC_END";
179 case RPC_FC_PAD: return "FC_PAD";
180 case RPC_FC_USER_MARSHAL: return "FC_USER_MARSHAL";
181 case RPC_FC_RANGE: return "FC_RANGE";
182 case RPC_FC_INT3264: return "FC_INT3264";
183 case RPC_FC_UINT3264: return "FC_UINT3264";
185 error("string_of_type: unknown type 0x%02x\n", type);
190 static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
192 const type_t *t = type;
195 if (is_attr(t->attrs, attr))
196 return get_attrp(t->attrs, attr);
197 else if (type_is_alias(t))
198 t = type_alias_get_aliasee(t);
203 unsigned char get_basic_fc(const type_t *type)
205 int sign = type_basic_get_sign(type);
206 switch (type_basic_get_type(type))
208 case TYPE_BASIC_INT8: return (sign <= 0 ? RPC_FC_SMALL : RPC_FC_USMALL);
209 case TYPE_BASIC_INT16: return (sign <= 0 ? RPC_FC_SHORT : RPC_FC_USHORT);
210 case TYPE_BASIC_INT32: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
211 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
212 case TYPE_BASIC_INT: return (sign <= 0 ? RPC_FC_LONG : RPC_FC_ULONG);
213 case TYPE_BASIC_INT3264: return (sign <= 0 ? RPC_FC_INT3264 : RPC_FC_UINT3264);
214 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
215 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
216 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
217 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
218 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
219 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
220 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
221 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
226 static unsigned char get_basic_fc_signed(const type_t *type)
228 switch (type_basic_get_type(type))
230 case TYPE_BASIC_INT8: return RPC_FC_SMALL;
231 case TYPE_BASIC_INT16: return RPC_FC_SHORT;
232 case TYPE_BASIC_INT32: return RPC_FC_LONG;
233 case TYPE_BASIC_INT64: return RPC_FC_HYPER;
234 case TYPE_BASIC_INT: return RPC_FC_LONG;
235 case TYPE_BASIC_INT3264: return RPC_FC_INT3264;
236 case TYPE_BASIC_BYTE: return RPC_FC_BYTE;
237 case TYPE_BASIC_CHAR: return RPC_FC_CHAR;
238 case TYPE_BASIC_WCHAR: return RPC_FC_WCHAR;
239 case TYPE_BASIC_HYPER: return RPC_FC_HYPER;
240 case TYPE_BASIC_FLOAT: return RPC_FC_FLOAT;
241 case TYPE_BASIC_DOUBLE: return RPC_FC_DOUBLE;
242 case TYPE_BASIC_ERROR_STATUS_T: return RPC_FC_ERROR_STATUS_T;
243 case TYPE_BASIC_HANDLE: return RPC_FC_BIND_PRIMITIVE;
248 static inline unsigned int clamp_align(unsigned int align)
250 unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
251 if(align > packing) align = packing;
255 unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
260 assert(is_ptr(type) || is_array(type));
262 pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
266 for (t = type; type_is_alias(t); t = type_alias_get_aliasee(t))
268 pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
275 else if (is_ptr(type))
276 return type_pointer_get_default_fc(type);
278 return type_array_get_ptr_default_fc(type);
281 static unsigned char get_enum_fc(const type_t *type)
283 assert(type_get_type(type) == TYPE_ENUM);
284 if (is_aliaschain_attr(type, ATTR_V1ENUM))
285 return RPC_FC_ENUM32;
287 return RPC_FC_ENUM16;
290 static type_t *get_user_type(const type_t *t, const char **pname)
294 type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
302 if (type_is_alias(t))
303 t = type_alias_get_aliasee(t);
309 static int is_user_type(const type_t *t)
311 return get_user_type(t, NULL) != NULL;
314 enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
316 if (is_user_type(type))
317 return TGT_USER_TYPE;
319 if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
320 return TGT_CTXT_HANDLE;
322 if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
325 switch (type_get_type(type))
328 if (!(flags & TDT_IGNORE_RANGES) &&
329 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
333 if (!(flags & TDT_IGNORE_RANGES) &&
334 (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
338 if (type_get_type(type_pointer_get_ref(type)) == TYPE_INTERFACE ||
339 (type_get_type(type_pointer_get_ref(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
340 return TGT_IFACE_POINTER;
341 else if (is_aliaschain_attr(type_pointer_get_ref(type), ATTR_CONTEXTHANDLE))
342 return TGT_CTXT_HANDLE_POINTER;
347 case TYPE_ENCAPSULATED_UNION:
364 static int get_padding(const var_list_t *fields)
366 unsigned short offset = 0;
367 unsigned int salign = 1;
373 LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
375 type_t *ft = f->type;
376 unsigned int align = 0;
377 unsigned int size = type_memsize_and_alignment(ft, &align);
378 align = clamp_align(align);
379 if (align > salign) salign = align;
380 offset = ROUND_SIZE(offset, align);
384 return ROUNDING(offset, salign);
387 static unsigned int get_stack_size( const type_t *type, const attr_list_t *attrs, int *by_value )
389 unsigned int stack_size;
392 switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
400 stack_size = type_memsize( type );
401 by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
407 if (!by_val) stack_size = pointer_size;
408 if (by_value) *by_value = by_val;
409 return ROUND_SIZE( stack_size, pointer_size );
412 static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
415 unsigned char flags = 0;
417 if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
420 !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
421 !is_attr( attrs, ATTR_CONTEXTHANDLE ))
424 if (is_attr(attrs, ATTR_IN))
427 if (!is_attr(attrs, ATTR_OUT)) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
429 if (is_attr(attrs, ATTR_OUT)) flags |= 0x20;
434 static unsigned int get_rpc_flags( const attr_list_t *attrs )
436 unsigned int flags = 0;
438 if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
439 if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
440 if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
441 if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
442 if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
446 unsigned char get_struct_fc(const type_t *type)
449 int has_conformance = 0;
450 int has_variance = 0;
454 fields = type_struct_get_fields(type);
456 if (get_padding(fields))
457 return RPC_FC_BOGUS_STRUCT;
459 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
461 type_t *t = field->type;
462 enum typegen_type typegen_type;
464 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
466 if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
468 if (is_string_type(field->attrs, field->type))
470 if (is_conformant_array(t))
476 if (is_array(type_array_get_element(field->type)))
477 return RPC_FC_BOGUS_STRUCT;
479 if (type_array_has_conformance(field->type))
482 if (list_next(fields, &field->entry))
483 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
486 if (type_array_has_variance(t))
489 t = type_array_get_element(t);
490 typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
493 switch (typegen_type)
496 case TGT_IFACE_POINTER:
497 return RPC_FC_BOGUS_STRUCT;
499 if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
500 return RPC_FC_BOGUS_STRUCT;
503 if (get_enum_fc(t) == RPC_FC_ENUM16)
504 return RPC_FC_BOGUS_STRUCT;
508 if (get_pointer_fc(t, field->attrs, FALSE) == RPC_FC_RP || pointer_size != 4)
509 return RPC_FC_BOGUS_STRUCT;
513 return RPC_FC_BOGUS_STRUCT;
516 unsigned char fc = get_struct_fc(t);
521 case RPC_FC_CVSTRUCT:
527 case RPC_FC_CPSTRUCT:
529 if (list_next( fields, &field->entry ))
530 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
537 if (list_next( fields, &field->entry ))
538 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
547 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
548 /* fallthru - treat it as complex */
550 /* as soon as we see one of these these members, it's bogus... */
551 case RPC_FC_BOGUS_STRUCT:
552 return RPC_FC_BOGUS_STRUCT;
557 return RPC_FC_BOGUS_STRUCT;
559 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
561 case TGT_CTXT_HANDLE:
562 case TGT_CTXT_HANDLE_POINTER:
563 /* checking after parsing should mean that we don't get here. if we do,
564 * it's a checker bug */
571 if ( has_conformance )
572 return RPC_FC_CVSTRUCT;
574 return RPC_FC_BOGUS_STRUCT;
576 if( has_conformance && has_pointer )
577 return RPC_FC_CPSTRUCT;
578 if( has_conformance )
579 return RPC_FC_CSTRUCT;
581 return RPC_FC_PSTRUCT;
582 return RPC_FC_STRUCT;
585 static unsigned char get_array_fc(const type_t *type)
588 const expr_t *size_is;
589 const type_t *elem_type;
591 elem_type = type_array_get_element(type);
592 size_is = type_array_get_conformance(type);
596 unsigned int size = type_memsize(elem_type);
597 if (size * type_array_get_dim(type) > 0xffffuL)
598 fc = RPC_FC_LGFARRAY;
600 fc = RPC_FC_SMFARRAY;
605 if (type_array_has_variance(type))
607 if (fc == RPC_FC_SMFARRAY)
608 fc = RPC_FC_SMVARRAY;
609 else if (fc == RPC_FC_LGFARRAY)
610 fc = RPC_FC_LGVARRAY;
611 else if (fc == RPC_FC_CARRAY)
615 switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
618 fc = RPC_FC_BOGUS_ARRAY;
621 if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
623 fc = RPC_FC_BOGUS_ARRAY;
626 switch (get_struct_fc(elem_type))
628 case RPC_FC_BOGUS_STRUCT:
629 fc = RPC_FC_BOGUS_ARRAY;
634 /* is 16-bit enum - if so, wire size differs from mem size and so
635 * the array cannot be block copied, which means the array is complex */
636 if (get_enum_fc(elem_type) == RPC_FC_ENUM16)
637 fc = RPC_FC_BOGUS_ARRAY;
640 case TGT_IFACE_POINTER:
641 fc = RPC_FC_BOGUS_ARRAY;
644 /* ref pointers cannot just be block copied. unique pointers to
645 * interfaces need special treatment. either case means the array is
647 if (get_pointer_fc(elem_type, NULL, FALSE) == RPC_FC_RP || pointer_size != 4)
648 fc = RPC_FC_BOGUS_ARRAY;
651 fc = RPC_FC_BOGUS_ARRAY;
653 case TGT_CTXT_HANDLE:
654 case TGT_CTXT_HANDLE_POINTER:
658 /* nothing to do for everything else */
665 static int is_non_complex_struct(const type_t *type)
667 return (type_get_type(type) == TYPE_STRUCT &&
668 get_struct_fc(type) != RPC_FC_BOGUS_STRUCT);
671 static int type_has_pointers(const type_t *type)
673 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
680 return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element(type));
683 var_list_t *fields = type_struct_get_fields(type);
685 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
687 if (type_has_pointers(field->type))
696 fields = type_union_get_cases(type);
697 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
699 if (field->type && type_has_pointers(field->type))
704 case TGT_CTXT_HANDLE:
705 case TGT_CTXT_HANDLE_POINTER:
707 case TGT_IFACE_POINTER:
718 static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
721 switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
726 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
731 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_FP)
734 return type_has_full_pointer(type_array_get_element(type), NULL, FALSE);
737 var_list_t *fields = type_struct_get_fields(type);
739 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
741 if (type_has_full_pointer(field->type, field->attrs, FALSE))
750 fields = type_union_get_cases(type);
751 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
753 if (field->type && type_has_full_pointer(field->type, field->attrs, FALSE))
758 case TGT_CTXT_HANDLE:
759 case TGT_CTXT_HANDLE_POINTER:
761 case TGT_IFACE_POINTER:
772 static unsigned short user_type_offset(const char *name)
775 unsigned short off = 0;
776 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
778 if (strcmp(name, ut->name) == 0)
782 error("user_type_offset: couldn't find type (%s)\n", name);
786 static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
788 type->typestring_offset = offset;
789 if (file) type->tfswrite = FALSE;
792 static void guard_rec(type_t *type)
794 /* types that contain references to themselves (like a linked list),
795 need to be shielded from infinite recursion when writing embedded
797 if (type->typestring_offset)
798 type->tfswrite = FALSE;
800 type->typestring_offset = 1;
803 static int is_embedded_complex(const type_t *type)
805 switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
811 case TGT_IFACE_POINTER:
818 static const char *get_context_handle_type_name(const type_t *type)
822 is_ptr(t) || type_is_alias(t);
823 t = type_is_alias(t) ? type_alias_get_aliasee(t) : type_pointer_get_ref(t))
824 if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
830 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
833 fprintf(file, "/* %2u */\n", typestring_offset); \
834 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
838 static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
839 static void print_file(FILE *file, int indent, const char *format, ...)
842 va_start(va, format);
843 print(file, indent, format, va);
847 void print(FILE *file, int indent, const char *format, va_list va)
851 if (format[0] != '\n')
854 vfprintf(file, format, va);
859 static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
861 if (decl_indirect(t))
863 print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
864 local_var_prefix, n, local_var_prefix, n);
865 print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
867 else if (is_ptr(t) || is_array(t))
868 print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
871 void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
875 if (!is_void(type_function_get_rettype(func->type)))
876 write_var_init(file, indent, type_function_get_rettype(func->type), "_RetVal", local_var_prefix);
878 if (!type_get_function_args(func->type))
881 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
882 write_var_init(file, indent, var->type, var->name, local_var_prefix);
887 static void write_formatdesc(FILE *f, int indent, const char *str)
889 print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
890 print_file(f, indent, "{\n");
891 print_file(f, indent + 1, "short Pad;\n");
892 print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
893 print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
894 print_file(f, indent, "\n");
897 void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
901 print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
902 get_size_typeformatstring(stmts, pred));
904 print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
905 get_size_procformatstring(stmts, pred));
908 write_formatdesc(f, indent, "TYPE");
909 write_formatdesc(f, indent, "PROC");
911 print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
912 print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
913 print_file(f, indent, "\n");
916 int decl_indirect(const type_t *t)
920 return (type_get_type(t) != TYPE_BASIC &&
921 type_get_type(t) != TYPE_ENUM &&
922 type_get_type(t) != TYPE_POINTER &&
923 type_get_type(t) != TYPE_ARRAY);
926 static unsigned char get_parameter_fc( const type_t *type, const attr_list_t *attrs, int is_return,
927 unsigned short *flags, unsigned int *stack_size,
928 unsigned int *typestring_offset )
930 unsigned int alignment, server_size = 0, buffer_size = 0;
931 unsigned char fc = 0;
933 int is_in = is_attr(attrs, ATTR_IN);
934 int is_out = is_attr(attrs, ATTR_OUT);
936 if (is_return) is_out = TRUE;
937 else if (!is_in && !is_out) is_in = TRUE;
940 *stack_size = get_stack_size( type, attrs, &is_byval );
941 *typestring_offset = type->typestring_offset;
943 if (is_in) *flags |= IsIn;
944 if (is_out) *flags |= IsOut;
945 if (is_return) *flags |= IsReturn;
947 if (!is_string_type( attrs, type ))
948 buffer_size = get_required_buffer_size_type( type, NULL, attrs, TRUE, &alignment );
950 switch (typegen_detect_type( type, attrs, TDT_ALL_TYPES ))
953 *flags |= IsBasetype;
954 fc = get_basic_fc_signed( type );
955 if (fc == RPC_FC_BIND_PRIMITIVE)
957 buffer_size = 4; /* actually 0 but avoids setting MustSize */
962 *flags |= IsBasetype;
963 fc = get_enum_fc( type );
971 *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
973 case TGT_IFACE_POINTER:
978 if (type_array_is_decl_as_ptr(type) && type->details.array.ptr_tfsoff &&
979 get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
980 *flags |= IsSimpleRef;
984 if (is_declptr( type ) && get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
986 /* skip over pointer description straight to string description */
987 if (is_conformant_array( type )) typestring_offset += 4;
988 else typestring_offset += 2;
989 *flags |= IsSimpleRef;
992 case TGT_CTXT_HANDLE_POINTER:
993 *flags |= IsSimpleRef;
995 case TGT_CTXT_HANDLE:
999 if (get_pointer_fc( type, attrs, !is_return ) == RPC_FC_RP)
1001 const type_t *ref = type_pointer_get_ref( type );
1003 if (!is_string_type( attrs, ref ))
1004 buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
1006 switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
1009 *flags |= IsSimpleRef | IsBasetype;
1010 fc = get_basic_fc( ref );
1011 if (!is_in && is_out) server_size = pointer_size;
1014 if ((fc = get_enum_fc( ref )) == RPC_FC_ENUM32)
1016 *flags |= IsSimpleRef | IsBasetype;
1017 if (!is_in && is_out) server_size = pointer_size;
1021 server_size = pointer_size;
1027 *flags |= IsSimpleRef | MustFree;
1028 *typestring_offset = ref->typestring_offset;
1029 if (!is_in && is_out) server_size = type_memsize( ref );
1034 case TGT_CTXT_HANDLE:
1035 case TGT_CTXT_HANDLE_POINTER:
1037 server_size = pointer_size;
1039 case TGT_IFACE_POINTER:
1041 if (is_in && is_out) server_size = pointer_size;
1044 *flags |= IsSimpleRef | MustFree;
1045 *typestring_offset = ref->typestring_offset;
1046 switch (get_struct_fc(ref))
1049 case RPC_FC_PSTRUCT:
1050 case RPC_FC_BOGUS_STRUCT:
1051 if (!is_in && is_out) server_size = type_memsize( ref );
1061 else /* not ref pointer */
1070 if (!buffer_size) *flags |= MustSize;
1074 server_size = (server_size + 7) / 8;
1075 if (server_size < 8) *flags |= server_size << 13;
1080 static unsigned char get_func_oi2_flags( const var_t *func )
1083 var_list_t *args = type_get_function_args( func->type );
1084 type_t *ret_type = type_function_get_rettype( func->type );
1085 unsigned char oi2_flags = 0x40; /* HasExtensions */
1086 unsigned short flags;
1087 unsigned int stack_size, typestring_offset;
1089 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1091 get_parameter_fc( var->type, var->attrs, 0, &flags, &stack_size, &typestring_offset );
1092 if (flags & MustSize)
1094 if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
1095 if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
1099 if (!is_void( ret_type ))
1101 oi2_flags |= 0x04; /* HasRet */
1102 get_parameter_fc( ret_type, NULL, 1, &flags, &stack_size, &typestring_offset );
1103 if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
1108 static unsigned int write_new_procformatstring_type(FILE *file, int indent,
1110 const attr_list_t *attrs,
1111 int is_return, unsigned int *stack_offset)
1114 unsigned int stack_size, typestring_offset;
1115 unsigned short flags;
1116 unsigned char fc = get_parameter_fc( type, attrs, is_return, &flags, &stack_size, &typestring_offset );
1118 strcpy( buffer, "/* flags:" );
1119 if (flags & MustSize) strcat( buffer, " must size," );
1120 if (flags & MustFree) strcat( buffer, " must free," );
1121 if (flags & IsPipe) strcat( buffer, " pipe," );
1122 if (flags & IsIn) strcat( buffer, " in," );
1123 if (flags & IsOut) strcat( buffer, " out," );
1124 if (flags & IsReturn) strcat( buffer, " return," );
1125 if (flags & IsBasetype) strcat( buffer, " base type," );
1126 if (flags & IsByValue) strcat( buffer, " by value," );
1127 if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
1128 if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
1129 strcpy( buffer + strlen( buffer ) - 1, " */" );
1130 print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
1131 print_file( file, indent, "NdrFcShort(0x%hx), /* stack offset = %hu */\n",
1132 *stack_offset, *stack_offset );
1133 if (flags & IsBasetype)
1135 print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
1136 print_file( file, indent, "0x0,\n" );
1139 print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
1140 typestring_offset, typestring_offset );
1141 *stack_offset += max( stack_size, pointer_size );
1145 static unsigned int write_old_procformatstring_type(FILE *file, int indent,
1147 const attr_list_t *attrs,
1148 int is_return, int is_interpreted)
1152 int is_in = is_attr(attrs, ATTR_IN);
1153 int is_out = is_attr(attrs, ATTR_OUT);
1155 if (!is_in && !is_out) is_in = TRUE;
1157 if (type_get_type(type) == TYPE_BASIC ||
1158 type_get_type(type) == TYPE_ENUM)
1163 print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1165 print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1167 if (type_get_type(type) == TYPE_ENUM)
1169 fc = get_enum_fc(type);
1173 fc = get_basic_fc_signed(type);
1175 if (fc == RPC_FC_BIND_PRIMITIVE)
1179 print_file(file, indent, "0x%02x, /* %s */\n",
1180 fc, string_of_type(fc));
1181 size = 2; /* includes param type prefix */
1185 unsigned short offset = type->typestring_offset;
1187 if (is_interpreted && is_array(type) &&
1188 type_array_is_decl_as_ptr(type) &&
1189 type->details.array.ptr_tfsoff)
1190 offset = type->details.array.ptr_tfsoff;
1193 print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
1194 else if (is_in && is_out)
1195 print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
1197 print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
1199 print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
1201 size = get_stack_size( type, attrs, NULL );
1202 print_file(file, indent, "0x%02x,\n", size / pointer_size );
1203 print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
1204 size = 4; /* includes param type prefix */
1209 int is_interpreted_func( const type_t *iface, const var_t *func )
1213 const var_list_t *args = type_get_function_args( func->type );
1214 const type_t *ret_type = type_function_get_rettype( func->type );
1216 if (type_get_type( ret_type ) == TYPE_BASIC)
1218 switch (type_basic_get_type( ret_type ))
1220 case TYPE_BASIC_INT64:
1221 case TYPE_BASIC_HYPER:
1222 /* return value must fit in a long_ptr */
1223 if (pointer_size < 8) return 0;
1225 case TYPE_BASIC_FLOAT:
1226 case TYPE_BASIC_DOUBLE:
1227 /* floating point values can't be returned */
1233 /* unions passed by value are not supported in Oi mode */
1234 if (get_stub_mode() != MODE_Oif && args)
1235 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1237 if (type_get_type( var->type ) == TYPE_UNION ||
1238 type_get_type( var->type ) == TYPE_ENCAPSULATED_UNION)
1242 if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1243 if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
1244 return (get_stub_mode() != MODE_Os);
1247 static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
1248 const var_t *func, unsigned int *offset,
1249 unsigned short num_proc )
1252 var_list_t *args = type_get_function_args( func->type );
1253 unsigned char explicit_fc, implicit_fc;
1254 unsigned char handle_flags;
1255 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
1256 unsigned char oi_flags = RPC_FC_PROC_OIF_RPCFLAGS | RPC_FC_PROC_OIF_NEWINIT;
1257 unsigned int rpc_flags = get_rpc_flags( func->attrs );
1258 unsigned int nb_args = 0;
1259 unsigned int stack_size = 0;
1260 unsigned short param_num = 0;
1261 unsigned short handle_stack_offset = 0;
1262 unsigned short handle_param_num = 0;
1264 if (is_full_pointer_function( func )) oi_flags |= RPC_FC_PROC_OIF_FULLPTR;
1265 if (is_object( iface ))
1267 oi_flags |= RPC_FC_PROC_OIF_OBJECT;
1268 if (get_stub_mode() == MODE_Oif) oi_flags |= RPC_FC_PROC_OIF_OBJ_V2;
1269 stack_size += pointer_size;
1272 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1274 if (var == handle_var)
1276 handle_stack_offset = stack_size;
1277 handle_param_num = param_num;
1279 stack_size += get_stack_size( var->type, var->attrs, NULL );
1283 if (!is_void( type_function_get_rettype( func->type )))
1285 stack_size += pointer_size;
1289 print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
1290 print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
1291 implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
1292 print_file( file, indent, "0x%02x,\n", oi_flags );
1293 print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
1294 print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
1295 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack size = %hu */\n", stack_size, stack_size );
1300 switch (explicit_fc)
1302 case RPC_FC_BIND_PRIMITIVE:
1304 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1305 print_file( file, indent, "0x%02x,\n", handle_flags );
1306 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1307 handle_stack_offset, handle_stack_offset );
1310 case RPC_FC_BIND_GENERIC:
1311 handle_flags = type_memsize( handle_var->type );
1312 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1313 print_file( file, indent, "0x%02x,\n", handle_flags );
1314 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1315 handle_stack_offset, handle_stack_offset );
1316 print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->type ) );
1317 print_file( file, indent, "0x%x,\t/* FC_PAD */\n", RPC_FC_PAD);
1320 case RPC_FC_BIND_CONTEXT:
1321 handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->type );
1322 print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
1323 print_file( file, indent, "0x%02x,\n", handle_flags );
1324 print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1325 handle_stack_offset, handle_stack_offset );
1326 print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->type ) );
1327 print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
1333 if (get_stub_mode() == MODE_Oif)
1335 unsigned char oi2_flags = get_func_oi2_flags( func );
1336 unsigned char ext_flags = 0;
1339 if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
1340 if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
1342 size = get_function_buffer_size( func, PASS_IN );
1343 print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %hu */\n", size, size );
1344 size = get_function_buffer_size( func, PASS_OUT );
1345 print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %hu */\n", size, size );
1346 print_file( file, indent, "0x%02x,\n", oi2_flags );
1347 print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
1348 print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
1349 print_file( file, indent, "0x%02x,\n", ext_flags );
1350 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
1351 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
1352 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1354 if (pointer_size == 8)
1356 unsigned short pos = 0, fpu_mask = 0;
1358 if (is_object( iface )) pos += 2;
1359 if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
1361 if (type_get_type( var->type ) == TYPE_BASIC)
1363 switch (type_basic_get_type( var->type ))
1365 case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
1366 case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
1371 if (pos >= 16) break;
1373 print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
1379 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1380 const var_t *func, unsigned int *offset,
1381 unsigned short num_proc )
1383 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1384 int is_interpreted = is_interpreted_func( iface, func );
1385 int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
1387 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1389 /* emit argument data */
1390 if (type_get_function_args(func->type))
1393 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1395 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1397 *offset += write_new_procformatstring_type(file, indent, var->type, var->attrs,
1398 FALSE, &stack_offset);
1400 *offset += write_old_procformatstring_type(file, indent, var->type, var->attrs,
1401 FALSE, is_interpreted);
1405 /* emit return value data */
1406 if (is_void(type_function_get_rettype(func->type)))
1410 print_file(file, 0, "/* %u (void) */\n", *offset);
1411 print_file(file, indent, "0x5b, /* FC_END */\n");
1412 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1418 print_file( file, 0, "/* %u (return value) */\n", *offset );
1420 *offset += write_new_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1421 NULL, TRUE, &stack_offset);
1423 *offset += write_old_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1424 NULL, TRUE, is_interpreted);
1428 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1429 type_pred_t pred, unsigned int *offset)
1431 const statement_t *stmt;
1432 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1434 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1436 const statement_t *stmt_func;
1437 const type_t *iface = stmt->u.type;
1438 const type_t *parent = type_iface_get_inherit( iface );
1439 int count = parent ? count_methods( parent ) : 0;
1441 if (!pred(iface)) continue;
1442 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1444 var_t *func = stmt_func->u.var;
1445 if (is_local(func->attrs)) continue;
1446 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1449 else if (stmt->type == STMT_LIBRARY)
1450 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred, offset);
1454 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1457 unsigned int offset = 0;
1459 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1460 print_file(file, indent, "{\n");
1462 print_file(file, indent, "0,\n");
1463 print_file(file, indent, "{\n");
1466 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1468 print_file(file, indent, "0x0\n");
1470 print_file(file, indent, "}\n");
1472 print_file(file, indent, "};\n");
1473 print_file(file, indent, "\n");
1476 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1478 const statement_t *stmt;
1481 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1483 print_file( file, indent, "{\n" );
1485 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1487 var_t *func = stmt->u.var;
1488 if (is_local( func->attrs )) continue;
1489 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1492 print_file( file, indent, "};\n\n" );
1495 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1499 if (type_get_type(type) == TYPE_BASIC)
1500 fc = get_basic_fc_signed(type);
1501 else if (type_get_type(type) == TYPE_ENUM)
1502 fc = get_enum_fc(type);
1506 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1507 *typestring_offset += 1;
1511 /* write conformance / variance descriptor */
1512 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1513 unsigned int baseoff, const type_t *type,
1516 unsigned char operator_type = 0;
1517 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1518 const char *conftype_string = "field";
1519 const expr_t *subexpr;
1520 const type_t *iface = NULL;
1525 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1531 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1532 error("write_conf_or_var_desc: constant value %d is greater than "
1533 "the maximum constant size of %d\n", expr->cval,
1534 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1536 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1537 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1538 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1539 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1544 if (!cont_type) /* top-level conformance */
1546 conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1547 conftype_string = "parameter";
1548 cont_type = current_func->type;
1549 name = current_func->name;
1550 iface = current_iface;
1554 name = cont_type->name;
1555 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1557 conftype = RPC_FC_POINTER_CONFORMANCE;
1558 conftype_string = "field pointer";
1563 switch (subexpr->type)
1566 subexpr = subexpr->ref;
1567 operator_type = RPC_FC_DEREFERENCE;
1570 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1572 subexpr = subexpr->ref;
1573 operator_type = RPC_FC_DIV_2;
1577 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1579 subexpr = subexpr->ref;
1580 operator_type = RPC_FC_MULT_2;
1584 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1586 subexpr = subexpr->ref;
1587 operator_type = RPC_FC_SUB_1;
1591 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1593 subexpr = subexpr->ref;
1594 operator_type = RPC_FC_ADD_1;
1601 if (subexpr->type == EXPR_IDENTIFIER)
1603 const type_t *correlation_variable = NULL;
1604 unsigned char param_type = 0;
1605 unsigned int offset = 0;
1607 struct expr_loc expr_loc;
1609 if (type_get_type(cont_type) == TYPE_FUNCTION)
1611 var_list_t *args = type_get_function_args( cont_type );
1613 if (is_object( iface )) offset += pointer_size;
1614 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1616 if (var->name && !strcmp(var->name, subexpr->u.sval))
1619 correlation_variable = var->type;
1622 offset += get_stack_size( var->type, var->attrs, NULL );
1627 var_list_t *fields = type_struct_get_fields( cont_type );
1629 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1631 unsigned int size = field_memsize( var->type, &offset );
1632 if (var->name && !strcmp(var->name, subexpr->u.sval))
1635 correlation_variable = var->type;
1642 if (!correlation_variable)
1643 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1644 expr_loc.attr = NULL;
1645 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1649 if (type_get_type(correlation_variable) == TYPE_BASIC)
1651 switch (get_basic_fc(correlation_variable))
1655 param_type = RPC_FC_SMALL;
1659 param_type = RPC_FC_USMALL;
1663 param_type = RPC_FC_SHORT;
1666 param_type = RPC_FC_USHORT;
1669 param_type = RPC_FC_LONG;
1672 param_type = RPC_FC_ULONG;
1675 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1676 get_basic_fc(correlation_variable));
1679 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1681 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1682 param_type = RPC_FC_LONG;
1684 param_type = RPC_FC_SHORT;
1686 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1688 if (pointer_size == 8)
1689 param_type = RPC_FC_HYPER;
1691 param_type = RPC_FC_LONG;
1695 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1700 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1701 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1702 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1703 operator_type ? string_of_type(operator_type) : "no operators");
1704 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1705 (unsigned short)offset, offset);
1707 else if (!iface || is_interpreted_func( iface, current_func ))
1709 unsigned int callback_offset = 0;
1710 struct expr_eval_routine *eval;
1713 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1715 if (eval->cont_type == cont_type ||
1716 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1717 eval->iface == iface &&
1718 eval->name && name && !strcmp(eval->name, name) &&
1719 !compare_expr(eval->expr, expr)))
1729 eval = xmalloc (sizeof(*eval));
1730 eval->iface = iface;
1731 eval->cont_type = cont_type;
1732 eval->name = xstrdup( name );
1733 eval->baseoff = baseoff;
1735 list_add_tail (&expr_eval_routines, &eval->entry);
1738 if (callback_offset > USHRT_MAX)
1739 error("Maximum number of callback routines reached\n");
1741 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1742 print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1743 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1745 else /* output a dummy corr desc that isn't used */
1747 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1748 print_file(file, 2, "0x0,\n" );
1749 print_file(file, 2, "NdrFcShort(0x0),\n" );
1754 /* return size and start offset of a data field based on current offset */
1755 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1757 unsigned int align = 0;
1758 unsigned int size = type_memsize_and_alignment( type, &align );
1760 *offset = ROUND_SIZE( *offset, align );
1764 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1766 unsigned int size = 0;
1767 unsigned int max_align;
1770 if (!fields) return 0;
1771 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1773 unsigned int falign = 0;
1774 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1775 if (*align < falign) *align = falign;
1776 falign = clamp_align(falign);
1777 size = ROUND_SIZE(size, falign);
1781 max_align = clamp_align(*align);
1782 size = ROUND_SIZE(size, max_align);
1787 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1789 unsigned int size, maxs = 0;
1790 unsigned int align = *pmaxa;
1793 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1795 /* we could have an empty default field with NULL type */
1798 size = type_memsize_and_alignment(v->type, &align);
1799 if (maxs < size) maxs = size;
1800 if (*pmaxa < align) *pmaxa = align;
1807 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1809 unsigned int size = 0;
1811 switch (type_get_type(t))
1814 switch (get_basic_fc(t))
1821 if (size > *align) *align = size;
1827 if (size > *align) *align = size;
1831 case RPC_FC_ERROR_STATUS_T:
1834 if (size > *align) *align = size;
1839 if (size > *align) *align = size;
1841 case RPC_FC_INT3264:
1842 case RPC_FC_UINT3264:
1843 case RPC_FC_BIND_PRIMITIVE:
1844 assert( pointer_size );
1845 size = pointer_size;
1846 if (size > *align) *align = size;
1849 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1854 switch (get_enum_fc(t))
1859 if (size > *align) *align = size;
1862 error("type_memsize: Unknown enum type\n");
1867 size = fields_memsize(type_struct_get_fields(t), align);
1869 case TYPE_ENCAPSULATED_UNION:
1870 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1873 size = union_memsize(type_union_get_cases(t), align);
1876 assert( pointer_size );
1877 size = pointer_size;
1878 if (size > *align) *align = size;
1881 if (!type_array_is_decl_as_ptr(t))
1883 if (is_conformant_array(t))
1885 type_memsize_and_alignment(type_array_get_element(t), align);
1889 size = type_array_get_dim(t) *
1890 type_memsize_and_alignment(type_array_get_element(t), align);
1892 else /* declared as a pointer */
1894 assert( pointer_size );
1895 size = pointer_size;
1896 if (size > *align) *align = size;
1899 case TYPE_INTERFACE:
1906 /* these types should not be encountered here due to language
1907 * restrictions (interface, void, coclass, module), logical
1908 * restrictions (alias - due to type_get_type call above) or
1909 * checking restrictions (function, bitfield). */
1916 unsigned int type_memsize(const type_t *t)
1918 unsigned int align = 0;
1919 return type_memsize_and_alignment( t, &align );
1922 static unsigned int type_buffer_alignment(const type_t *t)
1924 const var_list_t *fields;
1926 unsigned int max = 0, align;
1928 switch (type_get_type(t))
1931 switch (get_basic_fc(t))
1944 case RPC_FC_ERROR_STATUS_T:
1946 case RPC_FC_INT3264:
1947 case RPC_FC_UINT3264:
1953 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1957 switch (get_enum_fc(t))
1964 error("type_buffer_alignment: Unknown enum type\n");
1968 if (!(fields = type_struct_get_fields(t))) break;
1969 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1971 if (!var->type) continue;
1972 align = type_buffer_alignment( var->type );
1973 if (max < align) max = align;
1976 case TYPE_ENCAPSULATED_UNION:
1977 if (!(fields = type_encapsulated_union_get_fields(t))) break;
1978 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1980 if (!var->type) continue;
1981 align = type_buffer_alignment( var->type );
1982 if (max < align) max = align;
1986 if (!(fields = type_union_get_cases(t))) break;
1987 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1989 if (!var->type) continue;
1990 align = type_buffer_alignment( var->type );
1991 if (max < align) max = align;
1995 if (!type_array_is_decl_as_ptr(t))
1996 return type_buffer_alignment( type_array_get_element(t) );
1997 /* else fall through */
2000 case TYPE_INTERFACE:
2007 /* these types should not be encountered here due to language
2008 * restrictions (interface, void, coclass, module), logical
2009 * restrictions (alias - due to type_get_type call above) or
2010 * checking restrictions (function, bitfield). */
2016 int is_full_pointer_function(const var_t *func)
2019 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2021 if (!type_get_function_args(func->type))
2023 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2024 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2029 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2031 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2032 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2033 fprintf(file, "\n");
2036 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2038 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2039 fprintf(file, "\n");
2042 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2045 unsigned int offset,
2046 unsigned int *typeformat_offset)
2048 unsigned int start_offset = *typeformat_offset;
2049 short reloff = offset - (*typeformat_offset + 2);
2050 int in_attr, out_attr;
2052 unsigned char flags = 0;
2054 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2056 in_attr = is_attr(attrs, ATTR_IN);
2057 out_attr = is_attr(attrs, ATTR_OUT);
2058 if (!in_attr && !out_attr) in_attr = 1;
2060 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2061 flags |= RPC_FC_P_ONSTACK;
2065 type_t *ref = type_pointer_get_ref(type);
2066 if(is_declptr(ref) && !is_user_type(ref))
2067 flags |= RPC_FC_P_DEREF;
2070 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2073 string_of_type(pointer_type));
2076 if (flags & RPC_FC_P_ONSTACK)
2077 fprintf(file, " [allocated_on_stack]");
2078 if (flags & RPC_FC_P_DEREF)
2079 fprintf(file, " [pointer_deref]");
2080 fprintf(file, " */\n");
2083 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2084 *typeformat_offset += 4;
2086 return start_offset;
2089 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
2092 unsigned char pointer_fc;
2094 int in_attr = is_attr(attrs, ATTR_IN);
2095 int out_attr = is_attr(attrs, ATTR_OUT);
2096 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
2098 /* for historical reasons, write_simple_pointer also handled string types,
2099 * but no longer does. catch bad uses of the function with this check */
2100 if (is_string_type(attrs, type))
2101 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2103 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
2105 ref = type_pointer_get_ref(type);
2106 if (type_get_type(ref) == TYPE_ENUM)
2107 fc = get_enum_fc(ref);
2109 fc = get_basic_fc(ref);
2111 if (out_attr && !in_attr)
2112 flags |= RPC_FC_P_ONSTACK;
2114 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2115 pointer_fc, flags, string_of_type(pointer_fc),
2116 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
2117 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2118 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2122 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2124 print_file(file, 0, "/* %u (", tfsoff);
2125 write_type_decl(file, t, NULL);
2126 print_file(file, 0, ") */\n");
2129 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2130 type_t *type, unsigned int ref_offset,
2132 unsigned int *typestring_offset)
2134 unsigned int offset = *typestring_offset;
2135 type_t *ref = type_pointer_get_ref(type);
2137 print_start_tfs_comment(file, type, offset);
2138 update_tfsoff(type, offset, file);
2140 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2144 *typestring_offset += write_simple_pointer(file, attrs, type,
2149 write_nonsimple_pointer(file, attrs, type,
2159 static int processed(const type_t *type)
2161 return type->typestring_offset && !type->tfswrite;
2164 static int user_type_has_variable_size(const type_t *t)
2168 else if (type_get_type(t) == TYPE_STRUCT)
2170 switch (get_struct_fc(t))
2172 case RPC_FC_PSTRUCT:
2173 case RPC_FC_CSTRUCT:
2174 case RPC_FC_CPSTRUCT:
2175 case RPC_FC_CVSTRUCT:
2179 /* Note: Since this only applies to user types, we can't have a conformant
2180 array here, and strings should get filed under pointer in this case. */
2184 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2186 unsigned int start, absoff, flags;
2187 const char *name = NULL;
2188 type_t *utype = get_user_type(type, &name);
2189 unsigned int usize = type_memsize(utype);
2190 unsigned int ualign = type_buffer_alignment(utype);
2191 unsigned int size = type_memsize(type);
2192 unsigned short funoff = user_type_offset(name);
2195 if (processed(type)) return type->typestring_offset;
2199 if(user_type_has_variable_size(utype)) usize = 0;
2201 if (type_get_type(utype) == TYPE_BASIC ||
2202 type_get_type(utype) == TYPE_ENUM)
2206 if (type_get_type(utype) == TYPE_ENUM)
2207 fc = get_enum_fc(utype);
2209 fc = get_basic_fc(utype);
2212 print_start_tfs_comment(file, utype, absoff);
2213 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2214 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2219 if (!processed(utype))
2220 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2221 absoff = utype->typestring_offset;
2224 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
2226 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
2232 update_tfsoff(type, start, file);
2233 print_start_tfs_comment(file, type, start);
2234 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
2235 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2236 flags | (ualign - 1), ualign - 1, flags);
2237 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2238 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2239 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2241 reloff = absoff - *tfsoff;
2242 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2247 static void write_member_type(FILE *file, const type_t *cont,
2248 int cont_is_complex, const attr_list_t *attrs,
2249 const type_t *type, unsigned int *corroff,
2250 unsigned int *tfsoff)
2252 if (is_embedded_complex(type) && !is_conformant_array(type))
2254 unsigned int absoff;
2257 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2264 absoff = type->typestring_offset;
2266 reloff = absoff - (*tfsoff + 2);
2268 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2269 /* padding is represented using FC_STRUCTPAD* types, so presumably
2270 * this is left over in the format for historical purposes in MIDL
2272 print_file(file, 2, "0x0,\n");
2273 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2274 reloff, reloff, absoff);
2277 else if (is_ptr(type) || is_conformant_array(type))
2279 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
2280 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2283 else if (!write_base_type(file, type, tfsoff))
2284 error("Unsupported member type %d\n", type_get_type(type));
2287 static void write_array_element_type(FILE *file, const type_t *type,
2288 int cont_is_complex, unsigned int *tfsoff)
2290 type_t *elem = type_array_get_element(type);
2292 if (!is_embedded_complex(elem) && is_ptr(elem))
2294 type_t *ref = type_pointer_get_ref(elem);
2298 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
2301 if (!is_string_type(NULL, elem) &&
2302 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2304 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
2308 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2311 static void write_end(FILE *file, unsigned int *tfsoff)
2313 if (*tfsoff % 2 == 0)
2315 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
2318 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
2322 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2324 unsigned int offset = 0;
2325 var_list_t *fs = type_struct_get_fields(type);
2328 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2330 type_t *ft = f->type;
2331 unsigned int size = field_memsize( ft, &offset );
2332 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2335 unsigned int absoff = ft->typestring_offset;
2336 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2337 absoff += 8; /* we already have a corr descr, skip it */
2338 reloff = absoff - (*tfsoff + 6);
2339 print_file(file, 0, "/* %d */\n", *tfsoff);
2340 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2341 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
2342 write_conf_or_var_desc(file, current_structure, offset, ft,
2343 get_attrp(f->attrs, ATTR_SWITCHIS));
2344 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
2345 reloff, reloff, absoff);
2352 static int write_pointer_description_offsets(
2353 FILE *file, const attr_list_t *attrs, type_t *type,
2354 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2355 unsigned int *typestring_offset)
2359 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2360 (is_array(type) && type_array_is_decl_as_ptr(type)))
2362 if (offset_in_memory && offset_in_buffer)
2364 unsigned int memsize;
2368 * note that MSDN states that for pointer layouts in structures,
2369 * this is a negative offset from the end of the structure, but
2370 * this statement is incorrect. all offsets are positive */
2371 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2372 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2374 memsize = type_memsize(type);
2375 *offset_in_memory += memsize;
2376 /* increment these separately as in the case of conformant (varying)
2377 * structures these start at different values */
2378 *offset_in_buffer += memsize;
2380 *typestring_offset += 4;
2384 type_t *ref = type_pointer_get_ref(type);
2386 if (is_string_type(attrs, type))
2387 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
2388 else if (processed(ref))
2389 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
2390 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2391 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
2393 error("write_pointer_description_offsets: type format string unknown\n");
2397 unsigned int offset = type->typestring_offset;
2398 /* skip over the pointer that is written for strings, since a
2399 * pointer has to be written in-place here */
2400 if (is_string_type(attrs, type))
2402 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
2410 return write_pointer_description_offsets(
2411 file, attrs, type_array_get_element(type), offset_in_memory,
2412 offset_in_buffer, typestring_offset);
2414 else if (is_non_complex_struct(type))
2416 /* otherwise search for interesting fields to parse */
2418 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2420 if (offset_in_memory && offset_in_buffer)
2422 unsigned int padding;
2423 unsigned int align = 0;
2424 type_memsize_and_alignment(v->type, &align);
2425 padding = ROUNDING(*offset_in_memory, align);
2426 *offset_in_memory += padding;
2427 *offset_in_buffer += padding;
2429 written += write_pointer_description_offsets(
2430 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2436 if (offset_in_memory && offset_in_buffer)
2438 unsigned int memsize = type_memsize(type);
2439 *offset_in_memory += memsize;
2440 /* increment these separately as in the case of conformant (varying)
2441 * structures these start at different values */
2442 *offset_in_buffer += memsize;
2449 static int write_no_repeat_pointer_descriptions(
2450 FILE *file, const attr_list_t *attrs, type_t *type,
2451 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2452 unsigned int *typestring_offset)
2457 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2459 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
2460 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2461 *typestring_offset += 2;
2463 return write_pointer_description_offsets(file, attrs, type,
2464 offset_in_memory, offset_in_buffer, typestring_offset);
2467 if (is_non_complex_struct(type))
2470 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2472 if (offset_in_memory && offset_in_buffer)
2474 unsigned int padding;
2475 unsigned int align = 0;
2476 type_memsize_and_alignment(v->type, &align);
2477 padding = ROUNDING(*offset_in_memory, align);
2478 *offset_in_memory += padding;
2479 *offset_in_buffer += padding;
2481 written += write_no_repeat_pointer_descriptions(
2482 file, v->attrs, v->type,
2483 offset_in_memory, offset_in_buffer, typestring_offset);
2488 unsigned int memsize = type_memsize(type);
2489 *offset_in_memory += memsize;
2490 /* increment these separately as in the case of conformant (varying)
2491 * structures these start at different values */
2492 *offset_in_buffer += memsize;
2498 /* Note: if file is NULL return value is number of pointers to write, else
2499 * it is the number of type format characters written */
2500 static int write_fixed_array_pointer_descriptions(
2501 FILE *file, const attr_list_t *attrs, type_t *type,
2502 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2503 unsigned int *typestring_offset)
2505 int pointer_count = 0;
2507 if (type_get_type(type) == TYPE_ARRAY &&
2508 !type_array_has_conformance(type) && !type_array_has_variance(type))
2510 unsigned int temp = 0;
2511 /* unfortunately, this needs to be done in two passes to avoid
2512 * writing out redundant FC_FIXED_REPEAT descriptions */
2513 pointer_count = write_pointer_description_offsets(
2514 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2515 if (pointer_count > 0)
2517 unsigned int increment_size;
2518 unsigned int offset_of_array_pointer_mem = 0;
2519 unsigned int offset_of_array_pointer_buf = 0;
2521 increment_size = type_memsize(type_array_get_element(type));
2523 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2524 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2525 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2526 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2527 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2528 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2529 *typestring_offset += 10;
2531 pointer_count = write_pointer_description_offsets(
2532 file, attrs, type, &offset_of_array_pointer_mem,
2533 &offset_of_array_pointer_buf, typestring_offset);
2536 else if (type_get_type(type) == TYPE_STRUCT)
2539 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2541 if (offset_in_memory && offset_in_buffer)
2543 unsigned int padding;
2544 unsigned int align = 0;
2545 type_memsize_and_alignment(v->type, &align);
2546 padding = ROUNDING(*offset_in_memory, align);
2547 *offset_in_memory += padding;
2548 *offset_in_buffer += padding;
2550 pointer_count += write_fixed_array_pointer_descriptions(
2551 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2557 if (offset_in_memory && offset_in_buffer)
2559 unsigned int memsize;
2560 memsize = type_memsize(type);
2561 *offset_in_memory += memsize;
2562 /* increment these separately as in the case of conformant (varying)
2563 * structures these start at different values */
2564 *offset_in_buffer += memsize;
2568 return pointer_count;
2571 /* Note: if file is NULL return value is number of pointers to write, else
2572 * it is the number of type format characters written */
2573 static int write_conformant_array_pointer_descriptions(
2574 FILE *file, const attr_list_t *attrs, type_t *type,
2575 unsigned int offset_in_memory, unsigned int *typestring_offset)
2577 int pointer_count = 0;
2579 if (is_conformant_array(type) && !type_array_has_variance(type))
2581 unsigned int temp = 0;
2582 /* unfortunately, this needs to be done in two passes to avoid
2583 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2584 pointer_count = write_pointer_description_offsets(
2585 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2586 if (pointer_count > 0)
2588 unsigned int increment_size;
2589 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2590 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2592 increment_size = type_memsize(type_array_get_element(type));
2594 if (increment_size > USHRT_MAX)
2595 error("array size of %u bytes is too large\n", increment_size);
2597 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2598 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2599 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2600 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2601 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2602 *typestring_offset += 8;
2604 pointer_count = write_pointer_description_offsets(
2605 file, attrs, type_array_get_element(type),
2606 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2611 return pointer_count;
2614 /* Note: if file is NULL return value is number of pointers to write, else
2615 * it is the number of type format characters written */
2616 static int write_varying_array_pointer_descriptions(
2617 FILE *file, const attr_list_t *attrs, type_t *type,
2618 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2619 unsigned int *typestring_offset)
2621 int pointer_count = 0;
2623 if (is_array(type) && type_array_has_variance(type))
2625 unsigned int temp = 0;
2626 /* unfortunately, this needs to be done in two passes to avoid
2627 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2628 pointer_count = write_pointer_description_offsets(
2629 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2630 if (pointer_count > 0)
2632 unsigned int increment_size;
2634 increment_size = type_memsize(type_array_get_element(type));
2636 if (increment_size > USHRT_MAX)
2637 error("array size of %u bytes is too large\n", increment_size);
2639 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2640 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2641 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2642 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2643 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2644 *typestring_offset += 8;
2646 pointer_count = write_pointer_description_offsets(
2647 file, attrs, type_array_get_element(type), offset_in_memory,
2648 offset_in_buffer, typestring_offset);
2651 else if (type_get_type(type) == TYPE_STRUCT)
2654 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2656 if (offset_in_memory && offset_in_buffer)
2658 unsigned int align = 0, padding;
2660 if (is_array(v->type) && type_array_has_variance(v->type))
2662 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2663 /* skip over variance and offset in buffer */
2664 *offset_in_buffer += 8;
2667 type_memsize_and_alignment(v->type, &align);
2668 padding = ROUNDING(*offset_in_memory, align);
2669 *offset_in_memory += padding;
2670 *offset_in_buffer += padding;
2672 pointer_count += write_varying_array_pointer_descriptions(
2673 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2679 if (offset_in_memory && offset_in_buffer)
2681 unsigned int memsize = type_memsize(type);
2682 *offset_in_memory += memsize;
2683 /* increment these separately as in the case of conformant (varying)
2684 * structures these start at different values */
2685 *offset_in_buffer += memsize;
2689 return pointer_count;
2692 static void write_pointer_description(FILE *file, type_t *type,
2693 unsigned int *typestring_offset)
2695 unsigned int offset_in_buffer;
2696 unsigned int offset_in_memory;
2698 /* pass 1: search for single instance of a pointer (i.e. don't descend
2700 if (!is_array(type))
2702 offset_in_memory = 0;
2703 offset_in_buffer = 0;
2704 write_no_repeat_pointer_descriptions(
2706 &offset_in_memory, &offset_in_buffer, typestring_offset);
2709 /* pass 2: search for pointers in fixed arrays */
2710 offset_in_memory = 0;
2711 offset_in_buffer = 0;
2712 write_fixed_array_pointer_descriptions(
2714 &offset_in_memory, &offset_in_buffer, typestring_offset);
2716 /* pass 3: search for pointers in conformant only arrays (but don't descend
2717 * into conformant varying or varying arrays) */
2718 if (is_conformant_array(type) &&
2719 (type_array_is_decl_as_ptr(type) || !current_structure))
2720 write_conformant_array_pointer_descriptions(
2721 file, NULL, type, 0, typestring_offset);
2722 else if (type_get_type(type) == TYPE_STRUCT &&
2723 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2725 type_t *carray = find_array_or_string_in_struct(type)->type;
2726 write_conformant_array_pointer_descriptions( file, NULL, carray,
2727 type_memsize(type), typestring_offset);
2730 /* pass 4: search for pointers in varying arrays */
2731 offset_in_memory = 0;
2732 offset_in_buffer = 0;
2733 write_varying_array_pointer_descriptions(
2735 &offset_in_memory, &offset_in_buffer, typestring_offset);
2738 int is_declptr(const type_t *t)
2740 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2743 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2744 type_t *type, int toplevel_param,
2745 const char *name, unsigned int *typestring_offset)
2747 unsigned int start_offset;
2748 unsigned char rtype;
2750 int is_processed = processed(type);
2752 start_offset = *typestring_offset;
2754 if (is_declptr(type))
2756 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2757 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2759 pointer_type = RPC_FC_RP;
2760 print_start_tfs_comment(file, type, *typestring_offset);
2761 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2762 pointer_type, flag, string_of_type(pointer_type),
2763 flag ? " [simple_pointer]" : "");
2764 *typestring_offset += 2;
2767 print_file(file, 2, "NdrFcShort(0x2),\n");
2768 *typestring_offset += 2;
2770 is_processed = FALSE;
2774 elem_type = type_array_get_element(type);
2776 elem_type = type_pointer_get_ref(type);
2778 if (type_get_type(elem_type) != TYPE_BASIC)
2780 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2781 return start_offset;
2784 rtype = get_basic_fc(elem_type);
2785 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2787 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2788 return start_offset;
2791 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2793 unsigned int dim = type_array_get_dim(type);
2795 if (is_processed) return start_offset;
2797 /* FIXME: multi-dimensional array */
2799 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2800 name, 0xffffu, dim - 0xffffu);
2802 if (rtype == RPC_FC_WCHAR)
2803 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2805 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2806 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2807 *typestring_offset += 2;
2809 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2810 *typestring_offset += 2;
2812 update_tfsoff(type, start_offset, file);
2813 return start_offset;
2815 else if (is_conformant_array(type))
2817 if (rtype == RPC_FC_WCHAR)
2818 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2820 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2821 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2822 *typestring_offset += 2;
2824 *typestring_offset += write_conf_or_var_desc(
2825 file, current_structure,
2826 (!type_array_is_decl_as_ptr(type) && current_structure
2827 ? type_memsize(current_structure)
2829 type, type_array_get_conformance(type));
2831 update_tfsoff(type, start_offset, file);
2832 return start_offset;
2836 if (is_processed) return start_offset;
2838 if (rtype == RPC_FC_WCHAR)
2839 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2841 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2842 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2843 *typestring_offset += 2;
2845 update_tfsoff(type, start_offset, file);
2846 return start_offset;
2850 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2851 const char *name, unsigned int *typestring_offset)
2853 const expr_t *length_is = type_array_get_variance(type);
2854 const expr_t *size_is = type_array_get_conformance(type);
2857 unsigned int start_offset;
2859 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2860 unsigned int baseoff
2861 = !type_array_is_decl_as_ptr(type) && current_structure
2862 ? type_memsize(current_structure)
2866 pointer_type = RPC_FC_RP;
2868 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2870 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2871 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2872 fc = get_array_fc(type);
2874 start_offset = *typestring_offset;
2875 update_tfsoff(type, start_offset, file);
2876 print_start_tfs_comment(file, type, start_offset);
2877 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2878 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2879 *typestring_offset += 2;
2882 if (fc != RPC_FC_BOGUS_ARRAY)
2884 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2886 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2887 *typestring_offset += 4;
2891 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2892 *typestring_offset += 2;
2895 if (is_conformant_array(type))
2897 += write_conf_or_var_desc(file, current_structure, baseoff,
2900 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2902 unsigned int elsize = type_memsize(type_array_get_element(type));
2903 unsigned int dim = type_array_get_dim(type);
2905 if (fc == RPC_FC_LGVARRAY)
2907 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2908 *typestring_offset += 4;
2912 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2913 *typestring_offset += 2;
2916 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2917 *typestring_offset += 2;
2922 += write_conf_or_var_desc(file, current_structure, baseoff,
2925 if (type_has_pointers(type_array_get_element(type)) &&
2926 (type_array_is_decl_as_ptr(type) || !current_structure))
2928 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2929 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2930 *typestring_offset += 2;
2931 write_pointer_description(file, type, typestring_offset);
2932 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2933 *typestring_offset += 1;
2936 write_array_element_type(file, type, FALSE, typestring_offset);
2937 write_end(file, typestring_offset);
2941 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2942 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2943 *typestring_offset += 2;
2945 += write_conf_or_var_desc(file, current_structure, baseoff,
2948 += write_conf_or_var_desc(file, current_structure, baseoff,
2951 write_array_element_type(file, type, TRUE, typestring_offset);
2952 write_end(file, typestring_offset);
2955 return start_offset;
2958 static const var_t *find_array_or_string_in_struct(const type_t *type)
2960 const var_list_t *fields = type_struct_get_fields(type);
2961 const var_t *last_field;
2964 if (!fields || list_empty(fields))
2967 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2968 ft = last_field->type;
2970 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2973 if (type_get_type(ft) == TYPE_STRUCT)
2974 return find_array_or_string_in_struct(ft);
2979 static void write_struct_members(FILE *file, const type_t *type,
2980 int is_complex, unsigned int *corroff,
2981 unsigned int *typestring_offset)
2984 unsigned short offset = 0;
2985 unsigned int salign = 1;
2987 var_list_t *fields = type_struct_get_fields(type);
2989 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2991 type_t *ft = field->type;
2992 unsigned int align = 0;
2993 unsigned int size = type_memsize_and_alignment(ft, &align);
2994 align = clamp_align(align);
2995 if (salign < align) salign = align;
2997 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2999 if ((align - 1) & offset)
3001 unsigned char fc = 0;
3005 fc = RPC_FC_ALIGNM2;
3008 fc = RPC_FC_ALIGNM4;
3011 fc = RPC_FC_ALIGNM8;
3014 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
3016 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3017 offset = ROUND_SIZE(offset, align);
3018 *typestring_offset += 1;
3020 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3026 padding = ROUNDING(offset, salign);
3029 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3030 RPC_FC_STRUCTPAD1 + padding - 1,
3032 *typestring_offset += 1;
3035 write_end(file, typestring_offset);
3038 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3039 const char *name, unsigned int *tfsoff)
3041 const type_t *save_current_structure = current_structure;
3042 unsigned int total_size;
3044 unsigned int start_offset;
3046 unsigned int corroff;
3048 unsigned char fc = get_struct_fc(type);
3049 var_list_t *fields = type_struct_get_fields(type);
3051 if (processed(type)) return type->typestring_offset;
3054 current_structure = type;
3056 total_size = type_memsize(type);
3057 align = type_buffer_alignment(type);
3058 if (total_size > USHRT_MAX)
3059 error("structure size for %s exceeds %d bytes by %d bytes\n",
3060 name, USHRT_MAX, total_size - USHRT_MAX);
3062 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3063 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3065 array = find_array_or_string_in_struct(type);
3066 if (array && !processed(array->type))
3068 if(is_string_type(array->attrs, array->type))
3069 write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff);
3071 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3075 write_descriptors(file, type, tfsoff);
3077 start_offset = *tfsoff;
3078 update_tfsoff(type, start_offset, file);
3079 print_start_tfs_comment(file, type, start_offset);
3080 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3081 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3082 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3087 unsigned int absoff = array->type->typestring_offset;
3088 short reloff = absoff - *tfsoff;
3089 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3090 reloff, reloff, absoff);
3093 else if (fc == RPC_FC_BOGUS_STRUCT)
3095 print_file(file, 2, "NdrFcShort(0x0),\n");
3099 if (fc == RPC_FC_BOGUS_STRUCT)
3101 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3102 nothing is written to file yet. On the actual writing pass,
3103 this will have been updated. */
3104 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3105 int reloff = absoff - *tfsoff;
3106 assert( reloff >= 0 );
3107 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3108 (unsigned short)reloff, reloff, absoff);
3111 else if ((fc == RPC_FC_PSTRUCT) ||
3112 (fc == RPC_FC_CPSTRUCT) ||
3113 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
3115 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
3116 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
3118 write_pointer_description(file, type, tfsoff);
3119 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
3123 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
3126 if (fc == RPC_FC_BOGUS_STRUCT)
3130 type->ptrdesc = *tfsoff;
3131 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3133 type_t *ft = f->type;
3134 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3137 if (is_string_type(f->attrs, ft))
3138 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
3140 write_pointer_tfs(file, f->attrs, ft,
3141 type_pointer_get_ref(ft)->typestring_offset,
3145 if (type_array_is_decl_as_ptr(ft))
3147 unsigned int offset;
3149 print_file(file, 0, "/* %d */\n", *tfsoff);
3151 offset = ft->typestring_offset;
3152 /* skip over the pointer that is written for strings, since a
3153 * pointer has to be written in-place here */
3154 if (is_string_type(f->attrs, ft))
3156 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
3163 if (type->ptrdesc == *tfsoff)
3167 current_structure = save_current_structure;
3168 return start_offset;
3171 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3175 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3179 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3182 if (type_get_type(t) == TYPE_BASIC)
3183 fc = get_basic_fc(t);
3185 fc = get_enum_fc(t);
3186 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3187 fc, string_of_type(fc));
3189 else if (t->typestring_offset)
3191 short reloff = t->typestring_offset - *tfsoff;
3192 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3193 reloff, reloff, t->typestring_offset);
3196 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3202 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3203 type_t *type, unsigned int *tfsoff)
3205 unsigned int start_offset;
3208 unsigned int nbranch = 0;
3209 type_t *deftype = NULL;
3210 short nodeftype = 0xffff;
3213 if (processed(type) &&
3214 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3215 return type->typestring_offset;
3219 size = type_memsize(type);
3221 fields = type_union_get_cases(type);
3223 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3225 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3227 nbranch += list_count(cases);
3229 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3232 start_offset = *tfsoff;
3233 update_tfsoff(type, start_offset, file);
3234 print_start_tfs_comment(file, type, start_offset);
3235 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3237 const var_t *sv = type_union_get_switch_value(type);
3238 const type_t *st = sv->type;
3241 if (type_get_type(st) == TYPE_BASIC)
3243 fc = get_basic_fc(st);
3258 error("union switch type must be an integer, char, or enum\n");
3261 else if (type_get_type(st) == TYPE_ENUM)
3262 fc = get_enum_fc(st);
3264 error("union switch type must be an integer, char, or enum\n");
3266 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
3267 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3268 0x40 | fc, string_of_type(fc));
3271 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3273 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3274 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3277 if (type_get_type(st) == TYPE_BASIC)
3279 fc = get_basic_fc(st);
3294 error("union switch type must be an integer, char, or enum\n");
3297 else if (type_get_type(st) == TYPE_ENUM)
3298 fc = get_enum_fc(st);
3300 error("union switch type must be an integer, char, or enum\n");
3302 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
3303 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3304 fc, string_of_type(fc));
3306 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3307 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3309 print_file(file, 0, "/* %u */\n", *tfsoff);
3312 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3313 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3316 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3318 type_t *ft = f->type;
3319 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3320 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3323 if (cases == NULL && !deflt)
3324 error("union field %s with neither case nor default attribute\n", f->name);
3326 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3328 /* MIDL doesn't check for duplicate cases, even though that seems
3329 like a reasonable thing to do, it just dumps them to the TFS
3330 like we're going to do here. */
3331 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3333 write_branch_type(file, ft, tfsoff);
3336 /* MIDL allows multiple default branches, even though that seems
3337 illogical, it just chooses the last one, which is what we will
3348 write_branch_type(file, deftype, tfsoff);
3352 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3356 return start_offset;
3359 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3360 unsigned int *typeformat_offset)
3363 unsigned int start_offset = *typeformat_offset;
3364 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3366 if (!iid && processed(type)) return type->typestring_offset;
3368 print_start_tfs_comment(file, type, start_offset);
3369 update_tfsoff(type, start_offset, file);
3373 print_file(file, 2, "0x2f, /* FC_IP */\n");
3374 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3376 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3380 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3381 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3384 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3386 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3387 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3388 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3389 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3390 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3391 for (i = 0; i < 8; ++i)
3392 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3395 fprintf(file, "\n");
3397 *typeformat_offset += 18;
3399 return start_offset;
3402 static unsigned int write_contexthandle_tfs(FILE *file,
3403 const attr_list_t *attrs,
3406 unsigned int *typeformat_offset)
3408 unsigned int start_offset = *typeformat_offset;
3409 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3411 print_start_tfs_comment(file, type, start_offset);
3413 if (flags & 0x80) /* via ptr */
3415 int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3416 if (!pointer_type) pointer_type = RPC_FC_RP;
3417 *typeformat_offset += 4;
3418 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3419 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3420 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3423 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT);
3424 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3425 /* return and can't be null values overlap */
3426 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3427 print_file(file, 0, "can't be null, ");
3428 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3429 print_file(file, 0, "serialize, ");
3430 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
3431 print_file(file, 0, "no serialize, ");
3432 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3433 print_file(file, 0, "strict, ");
3434 if ((flags & 0x21) == 0x20)
3435 print_file(file, 0, "out, ");
3436 if ((flags & 0x21) == 0x21)
3437 print_file(file, 0, "return, ");
3439 print_file(file, 0, "in, ");
3441 print_file(file, 0, "via ptr, ");
3442 print_file(file, 0, "*/\n");
3443 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3444 print_file(file, 2, "0, /* FIXME: param num */\n");
3445 *typeformat_offset += 4;
3447 update_tfsoff( type, start_offset, file );
3448 return start_offset;
3451 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3452 type_t *type, expr_list_t *range_list,
3453 unsigned int *typeformat_offset)
3456 unsigned int start_offset = *typeformat_offset;
3457 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3458 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3460 if (type_get_type(type) == TYPE_BASIC)
3461 fc = get_basic_fc(type);
3463 fc = get_enum_fc(type);
3465 /* fc must fit in lower 4-bits of 8-bit field below */
3468 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3469 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3470 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3471 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3472 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3473 update_tfsoff( type, start_offset, file );
3474 *typeformat_offset += 10;
3476 return start_offset;
3479 static unsigned int write_type_tfs(FILE *file, int indent,
3480 const attr_list_t *attrs, type_t *type,
3482 enum type_context context,
3483 unsigned int *typeformat_offset)
3485 unsigned int offset;
3487 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3489 case TGT_CTXT_HANDLE:
3490 case TGT_CTXT_HANDLE_POINTER:
3491 return write_contexthandle_tfs(file, attrs, type,
3492 context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3494 return write_user_tfs(file, type, typeformat_offset);
3496 return write_string_tfs(file, attrs, type,
3497 context == TYPE_CONTEXT_TOPLEVELPARAM,
3498 name, typeformat_offset);
3502 /* conformant and pointer arrays are handled specially */
3503 if ((context != TYPE_CONTEXT_CONTAINER &&
3504 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3505 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3506 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3509 if (context != TYPE_CONTEXT_CONTAINER &&
3510 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3513 ptr_type = get_pointer_fc(type, attrs,
3514 context == TYPE_CONTEXT_TOPLEVELPARAM);
3515 if (ptr_type != RPC_FC_RP || type_array_is_decl_as_ptr(type))
3517 unsigned int absoff = type->typestring_offset;
3518 short reloff = absoff - (*typeformat_offset + 2);
3519 off = *typeformat_offset;
3520 print_file(file, 0, "/* %d */\n", off);
3521 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3522 string_of_type(ptr_type));
3523 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3524 reloff, reloff, absoff);
3525 if (ptr_type != RPC_FC_RP) update_tfsoff( type, off, file );
3526 *typeformat_offset += 4;
3528 type->details.array.ptr_tfsoff = off;
3533 return write_struct_tfs(file, type, name, typeformat_offset);
3535 return write_union_tfs(file, attrs, type, typeformat_offset);
3542 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3544 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3545 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3547 case TGT_IFACE_POINTER:
3548 return write_ip_tfs(file, attrs, type, typeformat_offset);
3551 enum type_context ref_context;
3552 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3553 ref_context = TYPE_CONTEXT_PARAM;
3554 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3555 ref_context = TYPE_CONTEXT_CONTAINER;
3557 ref_context = context;
3558 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3559 ref_context, typeformat_offset);
3560 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3562 return write_pointer_tfs(file, attrs, type, offset,
3563 context == TYPE_CONTEXT_TOPLEVELPARAM,
3569 error("invalid type %s for var %s\n", type->name, name);
3573 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3574 const char *name, int write_ptr, unsigned int *tfsoff)
3576 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3579 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3580 type_pred_t pred, unsigned int *typeformat_offset)
3583 const statement_t *stmt;
3585 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3587 const type_t *iface;
3588 const statement_t *stmt_func;
3590 if (stmt->type == STMT_LIBRARY)
3592 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
3595 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3598 iface = stmt->u.type;
3602 current_iface = iface;
3603 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3605 const var_t *func = stmt_func->u.var;
3606 current_func = func;
3607 if (is_local(func->attrs)) continue;
3609 if (!is_void(type_function_get_rettype(func->type)))
3611 write_type_tfs( file, 2, func->attrs, type_function_get_rettype(func->type),
3612 func->name, TYPE_CONTEXT_PARAM, typeformat_offset);
3615 if (type_get_function_args(func->type))
3616 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3617 write_type_tfs( file, 2, var->attrs, var->type, var->name,
3618 TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset );
3622 return *typeformat_offset + 1;
3625 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3627 unsigned int typeformat_offset = 2;
3629 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3633 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3637 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3638 print_file(file, indent, "{\n");
3640 print_file(file, indent, "0,\n");
3641 print_file(file, indent, "{\n");
3643 print_file(file, indent, "NdrFcShort(0x0),\n");
3645 set_all_tfswrite(TRUE);
3646 process_tfs(file, stmts, pred);
3648 print_file(file, indent, "0x0\n");
3650 print_file(file, indent, "}\n");
3652 print_file(file, indent, "};\n");
3653 print_file(file, indent, "\n");
3656 static unsigned int get_required_buffer_size_type(
3657 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3660 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3665 const type_t *utype = get_user_type(type, &uname);
3666 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3669 switch (get_basic_fc(type))
3687 case RPC_FC_ERROR_STATUS_T:
3696 case RPC_FC_INT3264:
3697 case RPC_FC_UINT3264:
3698 assert( pointer_size );
3699 *alignment = pointer_size;
3700 return pointer_size;
3703 case RPC_FC_BIND_PRIMITIVE:
3707 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3708 get_basic_fc(type));
3714 switch (get_enum_fc(type))
3726 if (get_struct_fc(type) == RPC_FC_STRUCT)
3728 if (!type_struct_get_fields(type)) return 0;
3729 return fields_memsize(type_struct_get_fields(type), alignment);
3735 unsigned int size, align;
3736 const type_t *ref = type_pointer_get_ref(type);
3737 if (is_string_type( attrs, ref )) break;
3738 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3739 if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3749 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3751 switch (get_array_fc(type))
3753 case RPC_FC_SMFARRAY:
3754 case RPC_FC_LGFARRAY:
3755 return type_array_get_dim(type) *
3756 get_required_buffer_size_type(type_array_get_element(type), name,
3757 NULL, FALSE, alignment);
3768 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3770 int in_attr = is_attr(var->attrs, ATTR_IN);
3771 int out_attr = is_attr(var->attrs, ATTR_OUT);
3773 if (!in_attr && !out_attr)
3778 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3779 pass == PASS_RETURN)
3781 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3787 if (!is_string_type(var->attrs, var->type))
3788 return get_required_buffer_size_type(var->type, var->name,
3789 var->attrs, TRUE, alignment);
3794 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3797 unsigned int total_size = 0, alignment;
3799 if (type_get_function_args(func->type))
3801 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3803 total_size += get_required_buffer_size(var, &alignment, pass);
3804 total_size += alignment;
3808 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3811 v.type = type_function_get_rettype(func->type);
3812 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3813 total_size += alignment;
3818 static void print_phase_function(FILE *file, int indent, const char *type,
3819 const char *local_var_prefix, enum remoting_phase phase,
3820 const var_t *var, unsigned int type_offset)
3822 const char *function;
3825 case PHASE_BUFFERSIZE:
3826 function = "BufferSize";
3829 function = "Marshall";
3831 case PHASE_UNMARSHAL:
3832 function = "Unmarshall";
3842 print_file(file, indent, "Ndr%s%s(\n", type, function);
3844 print_file(file, indent, "&__frame->_StubMsg,\n");
3845 print_file(file, indent, "%s%s%s%s%s,\n",
3846 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3847 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3849 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3851 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3852 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3853 if (phase == PHASE_UNMARSHAL)
3854 print_file(file, indent, "0);\n");
3858 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3859 enum remoting_phase phase, enum pass pass, const var_t *var,
3860 const char *varname)
3862 type_t *type = var->type;
3863 unsigned int alignment = 0;
3865 /* no work to do for other phases, buffer sizing is done elsewhere */
3866 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3869 if (type_get_type(type) == TYPE_ENUM ||
3870 (type_get_type(type) == TYPE_BASIC &&
3871 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3876 if (type_get_type(type) == TYPE_ENUM)
3877 fc = get_enum_fc(type);
3879 fc = get_basic_fc(type);
3881 if (phase == PHASE_MARSHAL)
3882 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3884 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3885 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3886 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3889 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3893 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3894 switch (get_basic_fc(ref))
3912 case RPC_FC_ERROR_STATUS_T:
3913 /* pointer_size must be 4 if we got here in these two cases */
3914 case RPC_FC_INT3264:
3915 case RPC_FC_UINT3264:
3925 case RPC_FC_BIND_PRIMITIVE:
3926 /* no marshalling needed */
3930 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3931 var->name, get_basic_fc(ref));
3934 if (phase == PHASE_MARSHAL && alignment > 1)
3935 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3936 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3937 alignment - 1, alignment - 1);
3939 if (phase == PHASE_MARSHAL)
3941 print_file(file, indent, "*(");
3942 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3944 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3946 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3947 fprintf(file, "%s%s", local_var_prefix, varname);
3948 fprintf(file, ";\n");
3950 else if (phase == PHASE_UNMARSHAL)
3952 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3953 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3954 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3955 print_file(file, indent, "{\n");
3956 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3957 print_file(file, indent, "}\n");
3958 print_file(file, indent, "%s%s%s",
3959 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3960 local_var_prefix, varname);
3961 if (pass == PASS_IN && is_ptr(type))
3962 fprintf(file, " = (");
3964 fprintf(file, " = *(");
3965 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3966 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3969 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3970 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3971 fprintf(file, ");\n");
3975 /* returns whether the MaxCount, Offset or ActualCount members need to be
3976 * filled in for the specified phase */
3977 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3979 return (phase != PHASE_UNMARSHAL);
3982 expr_t *get_size_is_expr(const type_t *t, const char *name)
3986 for ( ; is_array(t); t = type_array_get_element(t))
3987 if (type_array_has_conformance(t) &&
3988 type_array_get_conformance(t)->type != EXPR_VOID)
3991 x = type_array_get_conformance(t);
3993 error("%s: multidimensional conformant"
3994 " arrays not supported at the top level\n",
4001 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
4002 enum remoting_phase phase, const var_t *var)
4004 const type_t *type = var->type;
4005 /* get fundamental type for the argument */
4008 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
4011 if (is_conformance_needed_for_phase(phase))
4013 if (type_array_has_conformance(type) &&
4014 type_array_get_conformance(type)->type != EXPR_VOID)
4016 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4017 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4018 fprintf(file, ";\n\n");
4020 if (type_array_has_variance(type))
4022 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4023 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4024 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4025 fprintf(file, ";\n\n");
4030 if (type_get_type(type) == TYPE_UNION &&
4031 is_conformance_needed_for_phase(phase))
4033 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4034 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4035 fprintf(file, ";\n\n");
4038 case TGT_IFACE_POINTER:
4042 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4044 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4045 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4046 fprintf( file, ";\n\n" );
4051 type = type_pointer_get_ref(type);
4055 case TGT_CTXT_HANDLE:
4056 case TGT_CTXT_HANDLE_POINTER:
4068 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4069 enum pass pass, enum remoting_phase phase, const var_t *var)
4071 int in_attr, out_attr, pointer_type;
4072 const char *type_str = NULL;
4073 const type_t *type = var->type;
4074 unsigned int alignment, start_offset = type->typestring_offset;
4076 if (is_ptr(type) || is_array(type))
4077 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4081 in_attr = is_attr(var->attrs, ATTR_IN);
4082 out_attr = is_attr(var->attrs, ATTR_OUT);
4083 if (!in_attr && !out_attr)
4086 if (phase != PHASE_FREE)
4090 if (!in_attr) return;
4093 if (!out_attr) return;
4099 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4101 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
4103 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4105 case TGT_CTXT_HANDLE:
4106 case TGT_CTXT_HANDLE_POINTER:
4107 if (phase == PHASE_MARSHAL)
4109 if (pass == PASS_IN)
4111 /* if the context_handle attribute appears in the chain of types
4112 * without pointers being followed, then the context handle must
4113 * be direct, otherwise it is a pointer */
4114 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
4115 print_file(file, indent, "NdrClientContextMarshall(\n");
4116 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4117 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
4118 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4122 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4123 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4124 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4125 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4126 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4129 else if (phase == PHASE_UNMARSHAL)
4131 if (pass == PASS_OUT)
4134 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4135 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4136 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4137 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4138 print_file(file, indent + 1, "__frame->_Handle);\n");
4142 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4143 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4144 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4149 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4152 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4153 pointer_type != RPC_FC_RP)
4155 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
4156 !in_attr && is_conformant_array(type))
4158 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4160 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4162 /* strings returned are assumed to be global and hence don't
4164 else if (is_declptr(type) &&
4165 !(phase == PHASE_FREE && pass == PASS_RETURN))
4166 print_phase_function(file, indent, "Pointer", local_var_prefix,
4167 phase, var, start_offset);
4171 unsigned int real_start_offset = start_offset;
4172 /* skip over pointer description straight to string description */
4173 if (is_declptr(type))
4175 if (is_conformant_array(type))
4176 real_start_offset += 4;
4178 real_start_offset += 2;
4180 if (is_array(type) && !is_conformant_array(type))
4181 print_phase_function(file, indent, "NonConformantString",
4182 local_var_prefix, phase, var,
4185 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4186 phase, var, real_start_offset);
4191 unsigned char tc = get_array_fc(type);
4192 const char *array_type = NULL;
4194 /* We already have the size_is expression since it's at the
4195 top level, but do checks for multidimensional conformant
4196 arrays. When we handle them, we'll need to extend this
4197 function to return a list, and then we'll actually use
4198 the return value. */
4199 get_size_is_expr(type, var->name);
4203 case RPC_FC_SMFARRAY:
4204 case RPC_FC_LGFARRAY:
4205 array_type = "FixedArray";
4207 case RPC_FC_SMVARRAY:
4208 case RPC_FC_LGVARRAY:
4209 array_type = "VaryingArray";
4212 array_type = "ConformantArray";
4214 case RPC_FC_CVARRAY:
4215 array_type = "ConformantVaryingArray";
4217 case RPC_FC_BOGUS_ARRAY:
4218 array_type = "ComplexArray";
4222 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
4223 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4224 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
4226 /* these are all unmarshalled by allocating memory */
4227 if (tc == RPC_FC_BOGUS_ARRAY ||
4228 tc == RPC_FC_CVARRAY ||
4229 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
4230 (tc == RPC_FC_CARRAY && !in_attr))
4232 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4234 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4240 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4243 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4246 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4247 /* Note: this goes beyond what MIDL does - it only supports arguments
4248 * with the [range] attribute in Oicf mode */
4249 if (phase == PHASE_UNMARSHAL)
4251 const expr_t *range_min;
4252 const expr_t *range_max;
4253 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4255 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4256 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4257 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4259 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4260 write_type_decl(file, var->type, NULL);
4261 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4262 write_type_decl(file, var->type, NULL);
4263 fprintf(file, ")0x%x))\n", range_max->cval);
4264 print_file(file, indent, "{\n");
4265 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4266 print_file(file, indent, "}\n");
4270 switch (get_struct_fc(type))
4273 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4274 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4276 case RPC_FC_PSTRUCT:
4277 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4279 case RPC_FC_CSTRUCT:
4280 case RPC_FC_CPSTRUCT:
4281 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4283 case RPC_FC_CVSTRUCT:
4284 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4286 case RPC_FC_BOGUS_STRUCT:
4287 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4290 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4295 const char *union_type = NULL;
4297 if (type_get_type(type) == TYPE_UNION)
4298 union_type = "NonEncapsulatedUnion";
4299 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4300 union_type = "EncapsulatedUnion";
4302 print_phase_function(file, indent, union_type, local_var_prefix,
4303 phase, var, start_offset);
4308 const type_t *ref = type_pointer_get_ref(type);
4309 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4312 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4315 /* base types have known sizes, so don't need a sizing pass
4316 * and don't have any memory to free and so don't need a
4318 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4319 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4322 switch (get_struct_fc(ref))
4325 /* simple structs have known sizes, so don't need a sizing
4326 * pass and don't have any memory to free and so don't
4327 * need a freeing pass */
4328 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4329 type_str = "SimpleStruct";
4330 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4332 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4334 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4338 case RPC_FC_PSTRUCT:
4339 type_str = "SimpleStruct";
4341 case RPC_FC_CSTRUCT:
4342 case RPC_FC_CPSTRUCT:
4343 type_str = "ConformantStruct";
4345 case RPC_FC_CVSTRUCT:
4346 type_str = "ConformantVaryingStruct";
4348 case RPC_FC_BOGUS_STRUCT:
4349 type_str = "ComplexStruct";
4352 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4357 if (phase == PHASE_FREE)
4358 type_str = "Pointer";
4360 start_offset = ref->typestring_offset;
4361 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4365 if (phase == PHASE_FREE)
4366 type_str = "Pointer";
4369 if (type_get_type(ref) == TYPE_UNION)
4370 type_str = "NonEncapsulatedUnion";
4371 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4372 type_str = "EncapsulatedUnion";
4374 start_offset = ref->typestring_offset;
4377 print_phase_function(file, indent, type_str, local_var_prefix,
4378 phase, var, start_offset);
4381 if (phase != PHASE_FREE)
4383 type_str = "UserMarshal";
4384 start_offset = ref->typestring_offset;
4386 else type_str = "Pointer";
4388 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4394 case TGT_IFACE_POINTER:
4395 case TGT_CTXT_HANDLE:
4396 case TGT_CTXT_HANDLE_POINTER:
4397 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4404 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4407 case TGT_IFACE_POINTER:
4408 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4414 fprintf(file, "\n");
4417 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4418 enum pass pass, enum remoting_phase phase)
4420 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4422 unsigned int size = get_function_buffer_size( func, pass );
4423 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4426 if (pass == PASS_RETURN)
4430 var.type = type_function_get_rettype(func->type);
4431 var.name = xstrdup( "_RetVal" );
4432 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
4438 if (!type_get_function_args(func->type))
4440 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4441 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4446 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4448 unsigned int offset = 0;
4449 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4453 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4455 const statement_t *stmt;
4456 unsigned int size = 1;
4458 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4460 const type_t *iface;
4461 const statement_t *stmt_func;
4463 if (stmt->type == STMT_LIBRARY)
4465 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
4468 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4471 iface = stmt->u.type;
4475 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4477 const var_t *func = stmt_func->u.var;
4478 if (!is_local(func->attrs))
4479 size += get_size_procformatstring_func( iface, func );
4485 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4487 set_all_tfswrite(FALSE);
4488 return process_tfs(NULL, stmts, pred);
4491 void declare_stub_args( FILE *file, int indent, const var_t *func )
4493 int in_attr, out_attr;
4497 /* declare return value '_RetVal' */
4498 if (!is_void(type_function_get_rettype(func->type)))
4500 print_file(file, indent, "%s", "");
4501 write_type_decl_left(file, type_function_get_rettype(func->type));
4502 fprintf(file, " _RetVal;\n");
4505 if (!type_get_function_args(func->type))
4508 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4510 in_attr = is_attr(var->attrs, ATTR_IN);
4511 out_attr = is_attr(var->attrs, ATTR_OUT);
4512 if (!out_attr && !in_attr)
4515 if (is_context_handle(var->type))
4516 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4519 if (!in_attr && !is_conformant_array(var->type))
4521 type_t *type_to_print;
4523 print_file(file, indent, "%s", "");
4524 if (type_get_type(var->type) == TYPE_ARRAY &&
4525 !type_array_is_decl_as_ptr(var->type))
4526 type_to_print = var->type;
4528 type_to_print = type_pointer_get_ref(var->type);
4529 sprintf(name, "_W%u", i++);
4530 write_type_decl(file, type_to_print, name);
4531 fprintf(file, ";\n");
4534 print_file(file, indent, "%s", "");
4535 write_type_decl_left(file, var->type);
4537 if (type_get_type(var->type) == TYPE_ARRAY &&
4538 !type_array_is_decl_as_ptr(var->type)) {
4539 fprintf(file, "(*%s)", var->name);
4541 fprintf(file, "%s", var->name);
4542 write_type_right(file, var->type, FALSE);
4543 fprintf(file, ";\n");
4545 if (decl_indirect(var->type))
4546 print_file(file, indent, "void *_p_%s;\n", var->name);
4552 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4554 int in_attr, out_attr;
4558 if (!type_get_function_args(func->type))
4561 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4563 in_attr = is_attr(var->attrs, ATTR_IN);
4564 out_attr = is_attr(var->attrs, ATTR_OUT);
4565 if (!out_attr && !in_attr)
4570 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4572 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4574 case TGT_CTXT_HANDLE_POINTER:
4575 fprintf(file, " = NdrContextHandleInitialize(\n");
4576 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4577 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4578 var->type->typestring_offset);
4581 if (type_array_has_conformance(var->type))
4586 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4587 for (type = var->type;
4588 is_array(type) && type_array_has_conformance(type);
4589 type = type_array_get_element(type))
4591 write_expr(file, type_array_get_conformance(type), TRUE,
4592 TRUE, NULL, NULL, local_var_prefix);
4593 fprintf(file, " * ");
4595 size = type_memsize(type);
4596 fprintf(file, "%u);\n", size);
4598 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4599 for (type = var->type;
4600 is_array(type) && type_array_has_conformance(type);
4601 type = type_array_get_element(type))
4603 write_expr(file, type_array_get_conformance(type), TRUE,
4604 TRUE, NULL, NULL, local_var_prefix);
4605 fprintf(file, " * ");
4607 size = type_memsize(type);
4608 fprintf(file, "%u);\n", size);
4611 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4614 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4615 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
4621 case TGT_IFACE_POINTER:
4622 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4625 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4626 local_var_prefix, i, local_var_prefix, i);
4631 case TGT_CTXT_HANDLE:
4632 case TGT_CTXT_HANDLE_POINTER:
4635 /* not initialised */
4648 fprintf(file, "\n");
4652 void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func, const char *var_name )
4654 const var_list_t *args = type_get_function_args( func );
4657 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4658 print_file(file, 1, "{\n" );
4659 if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
4661 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4663 print_file(file, 2, "%s", "");
4664 write_type_left( file, (type_t *)arg->type, TRUE );
4665 if (needs_space_after( arg->type )) fputc( ' ', file );
4666 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4667 /* FIXME: should check for large args being passed by pointer */
4668 if (is_array( arg->type ) || is_ptr( arg->type ) || type_memsize( arg->type ) == pointer_size)
4669 fprintf( file, "%s;\n", arg->name );
4671 fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4673 if (!is_void( type_function_get_rettype( func )))
4675 print_file(file, 2, "%s", "");
4676 write_type_decl( file, type_function_get_rettype( func ), "_RetVal" );
4677 fprintf( file, ";\n" );
4679 print_file(file, 1, "} *%s = (struct _PARAM_STRUCT *)pStubMsg->StackTop;\n\n", var_name );
4682 int write_expr_eval_routines(FILE *file, const char *iface)
4684 static const char *var_name = "pS";
4685 static const char *var_name_expr = "pS->";
4687 struct expr_eval_routine *eval;
4688 unsigned short callback_offset = 0;
4690 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4692 const char *name = eval->name;
4695 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4696 iface, name, callback_offset);
4697 print_file(file, 0, "{\n");
4698 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4700 write_func_param_struct( file, eval->iface, eval->cont_type, "pS" );
4704 print_file(file, 1, "%s", "");
4705 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4706 fprintf(file, " *%s = (", var_name);
4707 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4708 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4710 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4711 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4712 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4713 fprintf(file, ";\n");
4714 print_file(file, 0, "}\n\n");
4720 void write_expr_eval_routine_list(FILE *file, const char *iface)
4722 struct expr_eval_routine *eval;
4723 struct expr_eval_routine *cursor;
4724 unsigned short callback_offset = 0;
4726 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4727 fprintf(file, "{\n");
4729 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4731 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, eval->name, callback_offset);
4733 list_remove(&eval->entry);
4738 fprintf(file, "};\n\n");
4741 void write_user_quad_list(FILE *file)
4745 if (list_empty(&user_type_list))
4748 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4749 fprintf(file, "{\n");
4750 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4752 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4753 print_file(file, 1, "{\n");
4754 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4755 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4756 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4757 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4758 print_file(file, 1, "}%s\n", sep);
4760 fprintf(file, "};\n\n");
4763 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4765 const struct str_list_entry_t *endpoint;
4768 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4769 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4770 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4772 print_file( f, 1, "{ (const unsigned char *)\"" );
4773 for (p = endpoint->str; *p && *p != ':'; p++)
4775 if (*p == '"' || *p == '\\') fputc( '\\', f );
4778 if (!*p) goto error;
4779 if (p[1] != '[') goto error;
4781 fprintf( f, "\", (const unsigned char *)\"" );
4782 for (p += 2; *p && *p != ']'; p++)
4784 if (*p == '"' || *p == '\\') fputc( '\\', f );
4787 if (*p != ']') goto error;
4788 fprintf( f, "\" },\n" );
4790 print_file( f, 0, "};\n\n" );
4794 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4797 void write_exceptions( FILE *file )
4799 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4800 fprintf( file, "\n");
4801 fprintf( file, "#include \"wine/exception.h\"\n");
4802 fprintf( file, "#undef RpcTryExcept\n");
4803 fprintf( file, "#undef RpcExcept\n");
4804 fprintf( file, "#undef RpcEndExcept\n");
4805 fprintf( file, "#undef RpcTryFinally\n");
4806 fprintf( file, "#undef RpcFinally\n");
4807 fprintf( file, "#undef RpcEndFinally\n");
4808 fprintf( file, "#undef RpcExceptionCode\n");
4809 fprintf( file, "#undef RpcAbnormalTermination\n");
4810 fprintf( file, "\n");
4811 fprintf( file, "struct __exception_frame;\n");
4812 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4813 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4814 fprintf( file, "\n");
4815 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4816 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4817 fprintf( file, " __filter_func filter; \\\n");
4818 fprintf( file, " __finally_func finally; \\\n");
4819 fprintf( file, " sigjmp_buf jmp; \\\n");
4820 fprintf( file, " DWORD code; \\\n");
4821 fprintf( file, " unsigned char abnormal_termination; \\\n");
4822 fprintf( file, " unsigned char filter_level; \\\n");
4823 fprintf( file, " unsigned char finally_level;\n");
4824 fprintf( file, "\n");
4825 fprintf( file, "struct __exception_frame\n{\n");
4826 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4827 fprintf( file, "};\n");
4828 fprintf( file, "\n");
4829 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4830 fprintf( file, "{\n");
4831 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4832 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4833 fprintf( file, " {\n");
4834 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4835 fprintf( file, " exc_frame->finally( exc_frame );\n");
4836 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4837 fprintf( file, " }\n");
4838 fprintf( file, " exc_frame->filter_level = 0;\n");
4839 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4840 fprintf( file, "}\n");
4841 fprintf( file, "\n");
4842 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4843 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4844 fprintf( file, " CONTEXT *context,\n");
4845 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4846 fprintf( file, "{\n");
4847 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4848 fprintf( file, "\n");
4849 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4850 fprintf( file, " {\n" );
4851 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4852 fprintf( file, " {\n" );
4853 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4854 fprintf( file, " exc_frame->finally( exc_frame );\n");
4855 fprintf( file, " }\n" );
4856 fprintf( file, " return ExceptionContinueSearch;\n");
4857 fprintf( file, " }\n" );
4858 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4859 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4860 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4861 fprintf( file, " return ExceptionContinueSearch;\n");
4862 fprintf( file, "}\n");
4863 fprintf( file, "\n");
4864 fprintf( file, "#define RpcTryExcept \\\n");
4865 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4866 fprintf( file, " { \\\n");
4867 fprintf( file, " if (!__frame->finally_level) \\\n" );
4868 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4869 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4870 fprintf( file, "\n");
4871 fprintf( file, "#define RpcExcept(expr) \\\n");
4872 fprintf( file, " if (!__frame->finally_level) \\\n" );
4873 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4874 fprintf( file, " __frame->filter_level = 0; \\\n" );
4875 fprintf( file, " } \\\n");
4876 fprintf( file, " else \\\n");
4877 fprintf( file, "\n");
4878 fprintf( file, "#define RpcEndExcept\n");
4879 fprintf( file, "\n");
4880 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4881 fprintf( file, "\n");
4882 fprintf( file, "#define RpcTryFinally \\\n");
4883 fprintf( file, " if (!__frame->filter_level) \\\n");
4884 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4885 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4886 fprintf( file, "\n");
4887 fprintf( file, "#define RpcFinally \\\n");
4888 fprintf( file, " if (!__frame->filter_level) \\\n");
4889 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4890 fprintf( file, " __frame->finally_level = 0;\n");
4891 fprintf( file, "\n");
4892 fprintf( file, "#define RpcEndFinally\n");
4893 fprintf( file, "\n");
4894 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4895 fprintf( file, "\n");
4896 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4897 fprintf( file, " do { \\\n");
4898 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4899 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4900 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4901 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4902 fprintf( file, " __frame->filter_level = 0; \\\n");
4903 fprintf( file, " __frame->finally_level = 0; \\\n");
4904 fprintf( file, " } while (0)\n");
4905 fprintf( file, "\n");
4906 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4907 fprintf( file, "\n");
4908 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4909 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4910 fprintf( file, "\n");
4911 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4912 fprintf( file, " DWORD code;\n");
4913 fprintf( file, "\n");
4914 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");