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 (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 (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 (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 (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 print_file( file, indent, "NdrFcShort(0x0),\n" ); /* floating point mask */
1362 static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
1363 const var_t *func, unsigned int *offset,
1364 unsigned short num_proc )
1366 unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
1367 int is_interpreted = is_interpreted_func( iface, func );
1368 int is_new_style = is_interpreted && (stub_mode == MODE_Oif);
1370 if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
1372 /* emit argument data */
1373 if (type_get_function_args(func->type))
1376 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
1378 print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
1380 *offset += write_new_procformatstring_type(file, indent, var->type, var->attrs,
1381 FALSE, &stack_offset);
1383 *offset += write_old_procformatstring_type(file, indent, var->type, var->attrs,
1384 FALSE, is_interpreted);
1388 /* emit return value data */
1389 if (is_void(type_function_get_rettype(func->type)))
1393 print_file(file, 0, "/* %u (void) */\n", *offset);
1394 print_file(file, indent, "0x5b, /* FC_END */\n");
1395 print_file(file, indent, "0x5c, /* FC_PAD */\n");
1401 print_file( file, 0, "/* %u (return value) */\n", *offset );
1403 *offset += write_new_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1404 NULL, TRUE, &stack_offset);
1406 *offset += write_old_procformatstring_type(file, indent, type_function_get_rettype(func->type),
1407 NULL, TRUE, is_interpreted);
1411 static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts,
1412 type_pred_t pred, unsigned int *offset)
1414 const statement_t *stmt;
1415 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
1417 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
1419 const statement_t *stmt_func;
1420 const type_t *iface = stmt->u.type;
1421 const type_t *parent = type_iface_get_inherit( iface );
1422 int count = parent ? count_methods( parent ) : 0;
1424 if (!pred(iface)) continue;
1425 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(iface))
1427 var_t *func = stmt_func->u.var;
1428 if (is_local(func->attrs)) continue;
1429 write_procformatstring_func( file, indent, iface, func, offset, count++ );
1432 else if (stmt->type == STMT_LIBRARY)
1433 write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred, offset);
1437 void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
1440 unsigned int offset = 0;
1442 print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1443 print_file(file, indent, "{\n");
1445 print_file(file, indent, "0,\n");
1446 print_file(file, indent, "{\n");
1449 write_procformatstring_stmts(file, indent, stmts, pred, &offset);
1451 print_file(file, indent, "0x0\n");
1453 print_file(file, indent, "}\n");
1455 print_file(file, indent, "};\n");
1456 print_file(file, indent, "\n");
1459 void write_procformatstring_offsets( FILE *file, const type_t *iface )
1461 const statement_t *stmt;
1464 print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1466 print_file( file, indent, "{\n" );
1468 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
1470 var_t *func = stmt->u.var;
1471 if (is_local( func->attrs )) continue;
1472 print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
1475 print_file( file, indent, "};\n\n" );
1478 static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
1482 if (type_get_type(type) == TYPE_BASIC)
1483 fc = get_basic_fc_signed(type);
1484 else if (type_get_type(type) == TYPE_ENUM)
1485 fc = get_enum_fc(type);
1489 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
1490 *typestring_offset += 1;
1494 /* write conformance / variance descriptor */
1495 static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
1496 unsigned int baseoff, const type_t *type,
1499 unsigned char operator_type = 0;
1500 unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
1501 const char *conftype_string = "field";
1502 const expr_t *subexpr;
1503 const type_t *iface = NULL;
1508 print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1514 if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
1515 error("write_conf_or_var_desc: constant value %d is greater than "
1516 "the maximum constant size of %d\n", expr->cval,
1517 UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
1519 print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1520 RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
1521 print_file(file, 2, "0x%x,\n", expr->cval >> 16);
1522 print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
1527 if (!cont_type) /* top-level conformance */
1529 conftype = RPC_FC_TOP_LEVEL_CONFORMANCE;
1530 conftype_string = "parameter";
1531 cont_type = current_func->type;
1532 name = current_func->name;
1533 iface = current_iface;
1537 name = cont_type->name;
1538 if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
1540 conftype = RPC_FC_POINTER_CONFORMANCE;
1541 conftype_string = "field pointer";
1546 switch (subexpr->type)
1549 subexpr = subexpr->ref;
1550 operator_type = RPC_FC_DEREFERENCE;
1553 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1555 subexpr = subexpr->ref;
1556 operator_type = RPC_FC_DIV_2;
1560 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
1562 subexpr = subexpr->ref;
1563 operator_type = RPC_FC_MULT_2;
1567 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1569 subexpr = subexpr->ref;
1570 operator_type = RPC_FC_SUB_1;
1574 if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
1576 subexpr = subexpr->ref;
1577 operator_type = RPC_FC_ADD_1;
1584 if (subexpr->type == EXPR_IDENTIFIER)
1586 const type_t *correlation_variable = NULL;
1587 unsigned char param_type = 0;
1588 unsigned int offset = 0;
1590 struct expr_loc expr_loc;
1592 if (type_get_type(cont_type) == TYPE_FUNCTION)
1594 var_list_t *args = type_get_function_args( cont_type );
1596 if (is_object( iface )) offset += pointer_size;
1597 if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
1599 if (var->name && !strcmp(var->name, subexpr->u.sval))
1602 correlation_variable = var->type;
1605 offset += get_stack_size( var->type, var->attrs, NULL );
1610 var_list_t *fields = type_struct_get_fields( cont_type );
1612 if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1614 unsigned int size = field_memsize( var->type, &offset );
1615 if (var->name && !strcmp(var->name, subexpr->u.sval))
1618 correlation_variable = var->type;
1625 if (!correlation_variable)
1626 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
1627 expr_loc.attr = NULL;
1628 correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
1632 if (type_get_type(correlation_variable) == TYPE_BASIC)
1634 switch (get_basic_fc(correlation_variable))
1638 param_type = RPC_FC_SMALL;
1642 param_type = RPC_FC_USMALL;
1646 param_type = RPC_FC_SHORT;
1649 param_type = RPC_FC_USHORT;
1652 param_type = RPC_FC_LONG;
1655 param_type = RPC_FC_ULONG;
1658 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1659 get_basic_fc(correlation_variable));
1662 else if (type_get_type(correlation_variable) == TYPE_ENUM)
1664 if (get_enum_fc(correlation_variable) == RPC_FC_ENUM32)
1665 param_type = RPC_FC_LONG;
1667 param_type = RPC_FC_SHORT;
1669 else if (type_get_type(correlation_variable) == TYPE_POINTER)
1671 if (pointer_size == 8)
1672 param_type = RPC_FC_HYPER;
1674 param_type = RPC_FC_LONG;
1678 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1683 print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1684 conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
1685 print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
1686 operator_type ? string_of_type(operator_type) : "no operators");
1687 print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1688 (unsigned short)offset, offset);
1690 else if (!iface || is_interpreted_func( iface, current_func ))
1692 unsigned int callback_offset = 0;
1693 struct expr_eval_routine *eval;
1696 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
1698 if (eval->cont_type == cont_type ||
1699 (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
1700 eval->iface == iface &&
1701 eval->name && name && !strcmp(eval->name, name) &&
1702 !compare_expr(eval->expr, expr)))
1712 eval = xmalloc (sizeof(*eval));
1713 eval->iface = iface;
1714 eval->cont_type = cont_type;
1715 eval->name = xstrdup( name );
1716 eval->baseoff = baseoff;
1718 list_add_tail (&expr_eval_routines, &eval->entry);
1721 if (callback_offset > USHRT_MAX)
1722 error("Maximum number of callback routines reached\n");
1724 print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
1725 print_file(file, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
1726 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
1728 else /* output a dummy corr desc that isn't used */
1730 print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
1731 print_file(file, 2, "0x0,\n" );
1732 print_file(file, 2, "NdrFcShort(0x0),\n" );
1737 /* return size and start offset of a data field based on current offset */
1738 static unsigned int field_memsize(const type_t *type, unsigned int *offset)
1740 unsigned int align = 0;
1741 unsigned int size = type_memsize_and_alignment( type, &align );
1743 *offset = ROUND_SIZE( *offset, align );
1747 static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
1749 unsigned int size = 0;
1750 unsigned int max_align;
1753 if (!fields) return 0;
1754 LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1756 unsigned int falign = 0;
1757 unsigned int fsize = type_memsize_and_alignment(v->type, &falign);
1758 if (*align < falign) *align = falign;
1759 falign = clamp_align(falign);
1760 size = ROUND_SIZE(size, falign);
1764 max_align = clamp_align(*align);
1765 size = ROUND_SIZE(size, max_align);
1770 static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
1772 unsigned int size, maxs = 0;
1773 unsigned int align = *pmaxa;
1776 if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
1778 /* we could have an empty default field with NULL type */
1781 size = type_memsize_and_alignment(v->type, &align);
1782 if (maxs < size) maxs = size;
1783 if (*pmaxa < align) *pmaxa = align;
1790 static unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
1792 unsigned int size = 0;
1794 switch (type_get_type(t))
1797 switch (get_basic_fc(t))
1804 if (size > *align) *align = size;
1810 if (size > *align) *align = size;
1814 case RPC_FC_ERROR_STATUS_T:
1817 if (size > *align) *align = size;
1822 if (size > *align) *align = size;
1824 case RPC_FC_INT3264:
1825 case RPC_FC_UINT3264:
1826 case RPC_FC_BIND_PRIMITIVE:
1827 assert( pointer_size );
1828 size = pointer_size;
1829 if (size > *align) *align = size;
1832 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
1837 switch (get_enum_fc(t))
1842 if (size > *align) *align = size;
1845 error("type_memsize: Unknown enum type\n");
1850 size = fields_memsize(type_struct_get_fields(t), align);
1852 case TYPE_ENCAPSULATED_UNION:
1853 size = fields_memsize(type_encapsulated_union_get_fields(t), align);
1856 size = union_memsize(type_union_get_cases(t), align);
1859 assert( pointer_size );
1860 size = pointer_size;
1861 if (size > *align) *align = size;
1864 if (!type_array_is_decl_as_ptr(t))
1866 if (is_conformant_array(t))
1868 type_memsize_and_alignment(type_array_get_element(t), align);
1872 size = type_array_get_dim(t) *
1873 type_memsize_and_alignment(type_array_get_element(t), align);
1875 else /* declared as a pointer */
1877 assert( pointer_size );
1878 size = pointer_size;
1879 if (size > *align) *align = size;
1882 case TYPE_INTERFACE:
1889 /* these types should not be encountered here due to language
1890 * restrictions (interface, void, coclass, module), logical
1891 * restrictions (alias - due to type_get_type call above) or
1892 * checking restrictions (function, bitfield). */
1899 unsigned int type_memsize(const type_t *t)
1901 unsigned int align = 0;
1902 return type_memsize_and_alignment( t, &align );
1905 static unsigned int type_buffer_alignment(const type_t *t)
1907 const var_list_t *fields;
1909 unsigned int max = 0, align;
1911 switch (type_get_type(t))
1914 switch (get_basic_fc(t))
1927 case RPC_FC_ERROR_STATUS_T:
1929 case RPC_FC_INT3264:
1930 case RPC_FC_UINT3264:
1936 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
1940 switch (get_enum_fc(t))
1947 error("type_buffer_alignment: Unknown enum type\n");
1951 if (!(fields = type_struct_get_fields(t))) break;
1952 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1954 if (!var->type) continue;
1955 align = type_buffer_alignment( var->type );
1956 if (max < align) max = align;
1959 case TYPE_ENCAPSULATED_UNION:
1960 if (!(fields = type_encapsulated_union_get_fields(t))) break;
1961 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1963 if (!var->type) continue;
1964 align = type_buffer_alignment( var->type );
1965 if (max < align) max = align;
1969 if (!(fields = type_union_get_cases(t))) break;
1970 LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
1972 if (!var->type) continue;
1973 align = type_buffer_alignment( var->type );
1974 if (max < align) max = align;
1978 if (!type_array_is_decl_as_ptr(t))
1979 return type_buffer_alignment( type_array_get_element(t) );
1980 /* else fall through */
1983 case TYPE_INTERFACE:
1990 /* these types should not be encountered here due to language
1991 * restrictions (interface, void, coclass, module), logical
1992 * restrictions (alias - due to type_get_type call above) or
1993 * checking restrictions (function, bitfield). */
1999 int is_full_pointer_function(const var_t *func)
2002 if (type_has_full_pointer(type_function_get_rettype(func->type), func->attrs, TRUE))
2004 if (!type_get_function_args(func->type))
2006 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
2007 if (type_has_full_pointer( var->type, var->attrs, TRUE ))
2012 void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
2014 print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2015 is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
2016 fprintf(file, "\n");
2019 void write_full_pointer_free(FILE *file, int indent, const var_t *func)
2021 print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2022 fprintf(file, "\n");
2025 static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
2028 unsigned int offset,
2029 unsigned int *typeformat_offset)
2031 unsigned int start_offset = *typeformat_offset;
2032 short reloff = offset - (*typeformat_offset + 2);
2033 int in_attr, out_attr;
2035 unsigned char flags = 0;
2037 pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2039 in_attr = is_attr(attrs, ATTR_IN);
2040 out_attr = is_attr(attrs, ATTR_OUT);
2041 if (!in_attr && !out_attr) in_attr = 1;
2043 if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
2044 flags |= RPC_FC_P_ONSTACK;
2048 type_t *ref = type_pointer_get_ref(type);
2049 if(is_declptr(ref) && !is_user_type(ref))
2050 flags |= RPC_FC_P_DEREF;
2053 print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
2056 string_of_type(pointer_type));
2059 if (flags & RPC_FC_P_ONSTACK)
2060 fprintf(file, " [allocated_on_stack]");
2061 if (flags & RPC_FC_P_DEREF)
2062 fprintf(file, " [pointer_deref]");
2063 fprintf(file, " */\n");
2066 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
2067 *typeformat_offset += 4;
2069 return start_offset;
2072 static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs, const type_t *type, int toplevel_param)
2075 unsigned char pointer_fc;
2077 int in_attr = is_attr(attrs, ATTR_IN);
2078 int out_attr = is_attr(attrs, ATTR_OUT);
2079 unsigned char flags = RPC_FC_P_SIMPLEPOINTER;
2081 /* for historical reasons, write_simple_pointer also handled string types,
2082 * but no longer does. catch bad uses of the function with this check */
2083 if (is_string_type(attrs, type))
2084 error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
2086 pointer_fc = get_pointer_fc(type, attrs, toplevel_param);
2088 ref = type_pointer_get_ref(type);
2089 if (type_get_type(ref) == TYPE_ENUM)
2090 fc = get_enum_fc(ref);
2092 fc = get_basic_fc(ref);
2094 if (out_attr && !in_attr)
2095 flags |= RPC_FC_P_ONSTACK;
2097 print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2098 pointer_fc, flags, string_of_type(pointer_fc),
2099 flags & RPC_FC_P_ONSTACK ? "[allocated_on_stack] " : "");
2100 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2101 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2105 static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
2107 print_file(file, 0, "/* %u (", tfsoff);
2108 write_type_decl(file, t, NULL);
2109 print_file(file, 0, ") */\n");
2112 static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
2113 type_t *type, unsigned int ref_offset,
2115 unsigned int *typestring_offset)
2117 unsigned int offset = *typestring_offset;
2118 type_t *ref = type_pointer_get_ref(type);
2120 print_start_tfs_comment(file, type, offset);
2121 update_tfsoff(type, offset, file);
2123 switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
2127 *typestring_offset += write_simple_pointer(file, attrs, type,
2132 write_nonsimple_pointer(file, attrs, type,
2142 static int processed(const type_t *type)
2144 return type->typestring_offset && !type->tfswrite;
2147 static int user_type_has_variable_size(const type_t *t)
2151 else if (type_get_type(t) == TYPE_STRUCT)
2153 switch (get_struct_fc(t))
2155 case RPC_FC_PSTRUCT:
2156 case RPC_FC_CSTRUCT:
2157 case RPC_FC_CPSTRUCT:
2158 case RPC_FC_CVSTRUCT:
2162 /* Note: Since this only applies to user types, we can't have a conformant
2163 array here, and strings should get filed under pointer in this case. */
2167 static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
2169 unsigned int start, absoff, flags;
2170 const char *name = NULL;
2171 type_t *utype = get_user_type(type, &name);
2172 unsigned int usize = type_memsize(utype);
2173 unsigned int ualign = type_buffer_alignment(utype);
2174 unsigned int size = type_memsize(type);
2175 unsigned short funoff = user_type_offset(name);
2178 if (processed(type)) return type->typestring_offset;
2182 if(user_type_has_variable_size(utype)) usize = 0;
2184 if (type_get_type(utype) == TYPE_BASIC ||
2185 type_get_type(utype) == TYPE_ENUM)
2189 if (type_get_type(utype) == TYPE_ENUM)
2190 fc = get_enum_fc(utype);
2192 fc = get_basic_fc(utype);
2195 print_start_tfs_comment(file, utype, absoff);
2196 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2197 print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
2202 if (!processed(utype))
2203 write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
2204 absoff = utype->typestring_offset;
2207 if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_RP)
2209 else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == RPC_FC_UP)
2215 update_tfsoff(type, start, file);
2216 print_start_tfs_comment(file, type, start);
2217 print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
2218 print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2219 flags | (ualign - 1), ualign - 1, flags);
2220 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
2221 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2222 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
2224 reloff = absoff - *tfsoff;
2225 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
2230 static void write_member_type(FILE *file, const type_t *cont,
2231 int cont_is_complex, const attr_list_t *attrs,
2232 const type_t *type, unsigned int *corroff,
2233 unsigned int *tfsoff)
2235 if (is_embedded_complex(type) && !is_conformant_array(type))
2237 unsigned int absoff;
2240 if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
2247 absoff = type->typestring_offset;
2249 reloff = absoff - (*tfsoff + 2);
2251 print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2252 /* padding is represented using FC_STRUCTPAD* types, so presumably
2253 * this is left over in the format for historical purposes in MIDL
2255 print_file(file, 2, "0x0,\n");
2256 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2257 reloff, reloff, absoff);
2260 else if (is_ptr(type) || is_conformant_array(type))
2262 unsigned char fc = cont_is_complex ? RPC_FC_POINTER : RPC_FC_LONG;
2263 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
2266 else if (!write_base_type(file, type, tfsoff))
2267 error("Unsupported member type %d\n", type_get_type(type));
2270 static void write_array_element_type(FILE *file, const type_t *type,
2271 int cont_is_complex, unsigned int *tfsoff)
2273 type_t *elem = type_array_get_element(type);
2275 if (!is_embedded_complex(elem) && is_ptr(elem))
2277 type_t *ref = type_pointer_get_ref(elem);
2281 write_nonsimple_pointer(file, NULL, elem, FALSE, ref->typestring_offset, tfsoff);
2284 if (!is_string_type(NULL, elem) &&
2285 (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
2287 *tfsoff += write_simple_pointer(file, NULL, elem, FALSE);
2291 return write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
2294 static void write_end(FILE *file, unsigned int *tfsoff)
2296 if (*tfsoff % 2 == 0)
2298 print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
2301 print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
2305 static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
2307 unsigned int offset = 0;
2308 var_list_t *fs = type_struct_get_fields(type);
2311 if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
2313 type_t *ft = f->type;
2314 unsigned int size = field_memsize( ft, &offset );
2315 if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
2318 unsigned int absoff = ft->typestring_offset;
2319 if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
2320 absoff += 8; /* we already have a corr descr, skip it */
2321 reloff = absoff - (*tfsoff + 6);
2322 print_file(file, 0, "/* %d */\n", *tfsoff);
2323 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
2324 print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
2325 write_conf_or_var_desc(file, current_structure, offset, ft,
2326 get_attrp(f->attrs, ATTR_SWITCHIS));
2327 print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
2328 reloff, reloff, absoff);
2335 static int write_pointer_description_offsets(
2336 FILE *file, const attr_list_t *attrs, type_t *type,
2337 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2338 unsigned int *typestring_offset)
2342 if ((is_ptr(type) && type_get_type(type_pointer_get_ref(type)) != TYPE_INTERFACE) ||
2343 (is_array(type) && type_array_is_decl_as_ptr(type)))
2345 if (offset_in_memory && offset_in_buffer)
2347 unsigned int memsize;
2351 * note that MSDN states that for pointer layouts in structures,
2352 * this is a negative offset from the end of the structure, but
2353 * this statement is incorrect. all offsets are positive */
2354 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2355 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
2357 memsize = type_memsize(type);
2358 *offset_in_memory += memsize;
2359 /* increment these separately as in the case of conformant (varying)
2360 * structures these start at different values */
2361 *offset_in_buffer += memsize;
2363 *typestring_offset += 4;
2367 type_t *ref = type_pointer_get_ref(type);
2369 if (is_string_type(attrs, type))
2370 write_string_tfs(file, attrs, type, FALSE, NULL, typestring_offset);
2371 else if (processed(ref))
2372 write_nonsimple_pointer(file, attrs, type, FALSE, ref->typestring_offset, typestring_offset);
2373 else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
2374 *typestring_offset += write_simple_pointer(file, attrs, type, FALSE);
2376 error("write_pointer_description_offsets: type format string unknown\n");
2380 unsigned int offset = type->typestring_offset;
2381 /* skip over the pointer that is written for strings, since a
2382 * pointer has to be written in-place here */
2383 if (is_string_type(attrs, type))
2385 write_nonsimple_pointer(file, attrs, type, FALSE, offset, typestring_offset);
2393 return write_pointer_description_offsets(
2394 file, attrs, type_array_get_element(type), offset_in_memory,
2395 offset_in_buffer, typestring_offset);
2397 else if (is_non_complex_struct(type))
2399 /* otherwise search for interesting fields to parse */
2401 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2403 if (offset_in_memory && offset_in_buffer)
2405 unsigned int padding;
2406 unsigned int align = 0;
2407 type_memsize_and_alignment(v->type, &align);
2408 padding = ROUNDING(*offset_in_memory, align);
2409 *offset_in_memory += padding;
2410 *offset_in_buffer += padding;
2412 written += write_pointer_description_offsets(
2413 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2419 if (offset_in_memory && offset_in_buffer)
2421 unsigned int memsize = type_memsize(type);
2422 *offset_in_memory += memsize;
2423 /* increment these separately as in the case of conformant (varying)
2424 * structures these start at different values */
2425 *offset_in_buffer += memsize;
2432 static int write_no_repeat_pointer_descriptions(
2433 FILE *file, const attr_list_t *attrs, type_t *type,
2434 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2435 unsigned int *typestring_offset)
2440 (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
2442 print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
2443 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2444 *typestring_offset += 2;
2446 return write_pointer_description_offsets(file, attrs, type,
2447 offset_in_memory, offset_in_buffer, typestring_offset);
2450 if (is_non_complex_struct(type))
2453 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2455 if (offset_in_memory && offset_in_buffer)
2457 unsigned int padding;
2458 unsigned int align = 0;
2459 type_memsize_and_alignment(v->type, &align);
2460 padding = ROUNDING(*offset_in_memory, align);
2461 *offset_in_memory += padding;
2462 *offset_in_buffer += padding;
2464 written += write_no_repeat_pointer_descriptions(
2465 file, v->attrs, v->type,
2466 offset_in_memory, offset_in_buffer, typestring_offset);
2471 unsigned int memsize = type_memsize(type);
2472 *offset_in_memory += memsize;
2473 /* increment these separately as in the case of conformant (varying)
2474 * structures these start at different values */
2475 *offset_in_buffer += memsize;
2481 /* Note: if file is NULL return value is number of pointers to write, else
2482 * it is the number of type format characters written */
2483 static int write_fixed_array_pointer_descriptions(
2484 FILE *file, const attr_list_t *attrs, type_t *type,
2485 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2486 unsigned int *typestring_offset)
2488 int pointer_count = 0;
2490 if (type_get_type(type) == TYPE_ARRAY &&
2491 !type_array_has_conformance(type) && !type_array_has_variance(type))
2493 unsigned int temp = 0;
2494 /* unfortunately, this needs to be done in two passes to avoid
2495 * writing out redundant FC_FIXED_REPEAT descriptions */
2496 pointer_count = write_pointer_description_offsets(
2497 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2498 if (pointer_count > 0)
2500 unsigned int increment_size;
2501 unsigned int offset_of_array_pointer_mem = 0;
2502 unsigned int offset_of_array_pointer_buf = 0;
2504 increment_size = type_memsize(type_array_get_element(type));
2506 print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
2507 print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
2508 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
2509 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2510 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2511 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2512 *typestring_offset += 10;
2514 pointer_count = write_pointer_description_offsets(
2515 file, attrs, type, &offset_of_array_pointer_mem,
2516 &offset_of_array_pointer_buf, typestring_offset);
2519 else if (type_get_type(type) == TYPE_STRUCT)
2522 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2524 if (offset_in_memory && offset_in_buffer)
2526 unsigned int padding;
2527 unsigned int align = 0;
2528 type_memsize_and_alignment(v->type, &align);
2529 padding = ROUNDING(*offset_in_memory, align);
2530 *offset_in_memory += padding;
2531 *offset_in_buffer += padding;
2533 pointer_count += write_fixed_array_pointer_descriptions(
2534 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2540 if (offset_in_memory && offset_in_buffer)
2542 unsigned int memsize;
2543 memsize = type_memsize(type);
2544 *offset_in_memory += memsize;
2545 /* increment these separately as in the case of conformant (varying)
2546 * structures these start at different values */
2547 *offset_in_buffer += memsize;
2551 return pointer_count;
2554 /* Note: if file is NULL return value is number of pointers to write, else
2555 * it is the number of type format characters written */
2556 static int write_conformant_array_pointer_descriptions(
2557 FILE *file, const attr_list_t *attrs, type_t *type,
2558 unsigned int offset_in_memory, unsigned int *typestring_offset)
2560 int pointer_count = 0;
2562 if (is_conformant_array(type) && !type_array_has_variance(type))
2564 unsigned int temp = 0;
2565 /* unfortunately, this needs to be done in two passes to avoid
2566 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2567 pointer_count = write_pointer_description_offsets(
2568 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2569 if (pointer_count > 0)
2571 unsigned int increment_size;
2572 unsigned int offset_of_array_pointer_mem = offset_in_memory;
2573 unsigned int offset_of_array_pointer_buf = offset_in_memory;
2575 increment_size = type_memsize(type_array_get_element(type));
2577 if (increment_size > USHRT_MAX)
2578 error("array size of %u bytes is too large\n", increment_size);
2580 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2581 print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
2582 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2583 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
2584 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2585 *typestring_offset += 8;
2587 pointer_count = write_pointer_description_offsets(
2588 file, attrs, type_array_get_element(type),
2589 &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
2594 return pointer_count;
2597 /* Note: if file is NULL return value is number of pointers to write, else
2598 * it is the number of type format characters written */
2599 static int write_varying_array_pointer_descriptions(
2600 FILE *file, const attr_list_t *attrs, type_t *type,
2601 unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
2602 unsigned int *typestring_offset)
2604 int pointer_count = 0;
2606 if (is_array(type) && type_array_has_variance(type))
2608 unsigned int temp = 0;
2609 /* unfortunately, this needs to be done in two passes to avoid
2610 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2611 pointer_count = write_pointer_description_offsets(
2612 NULL, attrs, type_array_get_element(type), NULL, NULL, &temp);
2613 if (pointer_count > 0)
2615 unsigned int increment_size;
2617 increment_size = type_memsize(type_array_get_element(type));
2619 if (increment_size > USHRT_MAX)
2620 error("array size of %u bytes is too large\n", increment_size);
2622 print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
2623 print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
2624 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
2625 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
2626 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
2627 *typestring_offset += 8;
2629 pointer_count = write_pointer_description_offsets(
2630 file, attrs, type_array_get_element(type), offset_in_memory,
2631 offset_in_buffer, typestring_offset);
2634 else if (type_get_type(type) == TYPE_STRUCT)
2637 LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
2639 if (offset_in_memory && offset_in_buffer)
2641 unsigned int align = 0, padding;
2643 if (is_array(v->type) && type_array_has_variance(v->type))
2645 *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
2646 /* skip over variance and offset in buffer */
2647 *offset_in_buffer += 8;
2650 type_memsize_and_alignment(v->type, &align);
2651 padding = ROUNDING(*offset_in_memory, align);
2652 *offset_in_memory += padding;
2653 *offset_in_buffer += padding;
2655 pointer_count += write_varying_array_pointer_descriptions(
2656 file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
2662 if (offset_in_memory && offset_in_buffer)
2664 unsigned int memsize = type_memsize(type);
2665 *offset_in_memory += memsize;
2666 /* increment these separately as in the case of conformant (varying)
2667 * structures these start at different values */
2668 *offset_in_buffer += memsize;
2672 return pointer_count;
2675 static void write_pointer_description(FILE *file, type_t *type,
2676 unsigned int *typestring_offset)
2678 unsigned int offset_in_buffer;
2679 unsigned int offset_in_memory;
2681 /* pass 1: search for single instance of a pointer (i.e. don't descend
2683 if (!is_array(type))
2685 offset_in_memory = 0;
2686 offset_in_buffer = 0;
2687 write_no_repeat_pointer_descriptions(
2689 &offset_in_memory, &offset_in_buffer, typestring_offset);
2692 /* pass 2: search for pointers in fixed arrays */
2693 offset_in_memory = 0;
2694 offset_in_buffer = 0;
2695 write_fixed_array_pointer_descriptions(
2697 &offset_in_memory, &offset_in_buffer, typestring_offset);
2699 /* pass 3: search for pointers in conformant only arrays (but don't descend
2700 * into conformant varying or varying arrays) */
2701 if (is_conformant_array(type) &&
2702 (type_array_is_decl_as_ptr(type) || !current_structure))
2703 write_conformant_array_pointer_descriptions(
2704 file, NULL, type, 0, typestring_offset);
2705 else if (type_get_type(type) == TYPE_STRUCT &&
2706 get_struct_fc(type) == RPC_FC_CPSTRUCT)
2708 type_t *carray = find_array_or_string_in_struct(type)->type;
2709 write_conformant_array_pointer_descriptions( file, NULL, carray,
2710 type_memsize(type), typestring_offset);
2713 /* pass 4: search for pointers in varying arrays */
2714 offset_in_memory = 0;
2715 offset_in_buffer = 0;
2716 write_varying_array_pointer_descriptions(
2718 &offset_in_memory, &offset_in_buffer, typestring_offset);
2721 int is_declptr(const type_t *t)
2723 return is_ptr(t) || (type_get_type(t) == TYPE_ARRAY && type_array_is_decl_as_ptr(t));
2726 static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
2727 type_t *type, int toplevel_param,
2728 const char *name, unsigned int *typestring_offset)
2730 unsigned int start_offset;
2731 unsigned char rtype;
2733 int is_processed = processed(type);
2735 start_offset = *typestring_offset;
2737 if (is_declptr(type))
2739 unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
2740 int pointer_type = get_pointer_fc(type, attrs, toplevel_param);
2742 pointer_type = RPC_FC_RP;
2743 print_start_tfs_comment(file, type, *typestring_offset);
2744 print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2745 pointer_type, flag, string_of_type(pointer_type),
2746 flag ? " [simple_pointer]" : "");
2747 *typestring_offset += 2;
2750 print_file(file, 2, "NdrFcShort(0x2),\n");
2751 *typestring_offset += 2;
2753 is_processed = FALSE;
2757 elem_type = type_array_get_element(type);
2759 elem_type = type_pointer_get_ref(type);
2761 if (type_get_type(elem_type) != TYPE_BASIC)
2763 error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
2764 return start_offset;
2767 rtype = get_basic_fc(elem_type);
2768 if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
2770 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
2771 return start_offset;
2774 if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
2776 unsigned int dim = type_array_get_dim(type);
2778 if (is_processed) return start_offset;
2780 /* FIXME: multi-dimensional array */
2782 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2783 name, 0xffffu, dim - 0xffffu);
2785 if (rtype == RPC_FC_WCHAR)
2786 WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
2788 WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
2789 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2790 *typestring_offset += 2;
2792 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
2793 *typestring_offset += 2;
2795 update_tfsoff(type, start_offset, file);
2796 return start_offset;
2798 else if (is_conformant_array(type))
2800 if (rtype == RPC_FC_WCHAR)
2801 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2803 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2804 print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
2805 *typestring_offset += 2;
2807 *typestring_offset += write_conf_or_var_desc(
2808 file, current_structure,
2809 (!type_array_is_decl_as_ptr(type) && current_structure
2810 ? type_memsize(current_structure)
2812 type, type_array_get_conformance(type));
2814 update_tfsoff(type, start_offset, file);
2815 return start_offset;
2819 if (is_processed) return start_offset;
2821 if (rtype == RPC_FC_WCHAR)
2822 WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
2824 WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
2825 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2826 *typestring_offset += 2;
2828 update_tfsoff(type, start_offset, file);
2829 return start_offset;
2833 static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
2834 const char *name, unsigned int *typestring_offset)
2836 const expr_t *length_is = type_array_get_variance(type);
2837 const expr_t *size_is = type_array_get_conformance(type);
2840 unsigned int start_offset;
2842 int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
2843 unsigned int baseoff
2844 = !type_array_is_decl_as_ptr(type) && current_structure
2845 ? type_memsize(current_structure)
2849 pointer_type = RPC_FC_RP;
2851 write_embedded_types(file, attrs, type_array_get_element(type), name, FALSE, typestring_offset);
2853 size = type_memsize(is_conformant_array(type) ? type_array_get_element(type) : type);
2854 align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element(type) : type);
2855 fc = get_array_fc(type);
2857 start_offset = *typestring_offset;
2858 update_tfsoff(type, start_offset, file);
2859 print_start_tfs_comment(file, type, start_offset);
2860 print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
2861 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
2862 *typestring_offset += 2;
2865 if (fc != RPC_FC_BOGUS_ARRAY)
2867 if (fc == RPC_FC_LGFARRAY || fc == RPC_FC_LGVARRAY)
2869 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
2870 *typestring_offset += 4;
2874 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
2875 *typestring_offset += 2;
2878 if (is_conformant_array(type))
2880 += write_conf_or_var_desc(file, current_structure, baseoff,
2883 if (fc == RPC_FC_SMVARRAY || fc == RPC_FC_LGVARRAY)
2885 unsigned int elsize = type_memsize(type_array_get_element(type));
2886 unsigned int dim = type_array_get_dim(type);
2888 if (fc == RPC_FC_LGVARRAY)
2890 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
2891 *typestring_offset += 4;
2895 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2896 *typestring_offset += 2;
2899 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
2900 *typestring_offset += 2;
2905 += write_conf_or_var_desc(file, current_structure, baseoff,
2908 if (type_has_pointers(type_array_get_element(type)) &&
2909 (type_array_is_decl_as_ptr(type) || !current_structure))
2911 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
2912 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
2913 *typestring_offset += 2;
2914 write_pointer_description(file, type, typestring_offset);
2915 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
2916 *typestring_offset += 1;
2919 write_array_element_type(file, type, FALSE, typestring_offset);
2920 write_end(file, typestring_offset);
2924 unsigned int dim = size_is ? 0 : type_array_get_dim(type);
2925 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
2926 *typestring_offset += 2;
2928 += write_conf_or_var_desc(file, current_structure, baseoff,
2931 += write_conf_or_var_desc(file, current_structure, baseoff,
2934 write_array_element_type(file, type, TRUE, typestring_offset);
2935 write_end(file, typestring_offset);
2938 return start_offset;
2941 static const var_t *find_array_or_string_in_struct(const type_t *type)
2943 const var_list_t *fields = type_struct_get_fields(type);
2944 const var_t *last_field;
2947 if (!fields || list_empty(fields))
2950 last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
2951 ft = last_field->type;
2953 if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
2956 if (type_get_type(ft) == TYPE_STRUCT)
2957 return find_array_or_string_in_struct(ft);
2962 static void write_struct_members(FILE *file, const type_t *type,
2963 int is_complex, unsigned int *corroff,
2964 unsigned int *typestring_offset)
2967 unsigned short offset = 0;
2968 unsigned int salign = 1;
2970 var_list_t *fields = type_struct_get_fields(type);
2972 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2974 type_t *ft = field->type;
2975 unsigned int align = 0;
2976 unsigned int size = type_memsize_and_alignment(ft, &align);
2977 align = clamp_align(align);
2978 if (salign < align) salign = align;
2980 if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
2982 if ((align - 1) & offset)
2984 unsigned char fc = 0;
2988 fc = RPC_FC_ALIGNM2;
2991 fc = RPC_FC_ALIGNM4;
2994 fc = RPC_FC_ALIGNM8;
2997 error("write_struct_members: cannot align type %d\n", type_get_type(ft));
2999 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3000 offset = ROUND_SIZE(offset, align);
3001 *typestring_offset += 1;
3003 write_member_type(file, type, is_complex, field->attrs, field->type, corroff,
3009 padding = ROUNDING(offset, salign);
3012 print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3013 RPC_FC_STRUCTPAD1 + padding - 1,
3015 *typestring_offset += 1;
3018 write_end(file, typestring_offset);
3021 static unsigned int write_struct_tfs(FILE *file, type_t *type,
3022 const char *name, unsigned int *tfsoff)
3024 const type_t *save_current_structure = current_structure;
3025 unsigned int total_size;
3027 unsigned int start_offset;
3029 unsigned int corroff;
3031 unsigned char fc = get_struct_fc(type);
3032 var_list_t *fields = type_struct_get_fields(type);
3034 if (processed(type)) return type->typestring_offset;
3037 current_structure = type;
3039 total_size = type_memsize(type);
3040 align = type_buffer_alignment(type);
3041 if (total_size > USHRT_MAX)
3042 error("structure size for %s exceeds %d bytes by %d bytes\n",
3043 name, USHRT_MAX, total_size - USHRT_MAX);
3045 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3046 write_embedded_types(file, f->attrs, f->type, f->name, FALSE, tfsoff);
3048 array = find_array_or_string_in_struct(type);
3049 if (array && !processed(array->type))
3051 if(is_string_type(array->attrs, array->type))
3052 write_string_tfs(file, array->attrs, array->type, FALSE, array->name, tfsoff);
3054 write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
3058 write_descriptors(file, type, tfsoff);
3060 start_offset = *tfsoff;
3061 update_tfsoff(type, start_offset, file);
3062 print_start_tfs_comment(file, type, start_offset);
3063 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3064 print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
3065 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
3070 unsigned int absoff = array->type->typestring_offset;
3071 short reloff = absoff - *tfsoff;
3072 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3073 reloff, reloff, absoff);
3076 else if (fc == RPC_FC_BOGUS_STRUCT)
3078 print_file(file, 2, "NdrFcShort(0x0),\n");
3082 if (fc == RPC_FC_BOGUS_STRUCT)
3084 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3085 nothing is written to file yet. On the actual writing pass,
3086 this will have been updated. */
3087 unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
3088 int reloff = absoff - *tfsoff;
3089 assert( reloff >= 0 );
3090 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3091 (unsigned short)reloff, reloff, absoff);
3094 else if ((fc == RPC_FC_PSTRUCT) ||
3095 (fc == RPC_FC_CPSTRUCT) ||
3096 (fc == RPC_FC_CVSTRUCT && type_has_pointers(type)))
3098 print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
3099 print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
3101 write_pointer_description(file, type, tfsoff);
3102 print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
3106 write_struct_members(file, type, fc == RPC_FC_BOGUS_STRUCT, &corroff,
3109 if (fc == RPC_FC_BOGUS_STRUCT)
3113 type->ptrdesc = *tfsoff;
3114 if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
3116 type_t *ft = f->type;
3117 switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
3120 if (is_string_type(f->attrs, ft))
3121 write_string_tfs(file, f->attrs, ft, FALSE, f->name, tfsoff);
3123 write_pointer_tfs(file, f->attrs, ft,
3124 type_pointer_get_ref(ft)->typestring_offset,
3128 if (type_array_is_decl_as_ptr(ft))
3130 unsigned int offset;
3132 print_file(file, 0, "/* %d */\n", *tfsoff);
3134 offset = ft->typestring_offset;
3135 /* skip over the pointer that is written for strings, since a
3136 * pointer has to be written in-place here */
3137 if (is_string_type(f->attrs, ft))
3139 write_nonsimple_pointer(file, f->attrs, ft, FALSE, offset, tfsoff);
3146 if (type->ptrdesc == *tfsoff)
3150 current_structure = save_current_structure;
3151 return start_offset;
3154 static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
3158 print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
3162 if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
3165 if (type_get_type(t) == TYPE_BASIC)
3166 fc = get_basic_fc(t);
3168 fc = get_enum_fc(t);
3169 print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3170 fc, string_of_type(fc));
3172 else if (t->typestring_offset)
3174 short reloff = t->typestring_offset - *tfsoff;
3175 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3176 reloff, reloff, t->typestring_offset);
3179 error("write_branch_type: type unimplemented %d\n", type_get_type(t));
3185 static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
3186 type_t *type, unsigned int *tfsoff)
3188 unsigned int start_offset;
3191 unsigned int nbranch = 0;
3192 type_t *deftype = NULL;
3193 short nodeftype = 0xffff;
3196 if (processed(type) &&
3197 (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
3198 return type->typestring_offset;
3202 size = type_memsize(type);
3204 fields = type_union_get_cases(type);
3206 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3208 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3210 nbranch += list_count(cases);
3212 write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
3215 start_offset = *tfsoff;
3216 update_tfsoff(type, start_offset, file);
3217 print_start_tfs_comment(file, type, start_offset);
3218 if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
3220 const var_t *sv = type_union_get_switch_value(type);
3221 const type_t *st = sv->type;
3224 if (type_get_type(st) == TYPE_BASIC)
3226 fc = get_basic_fc(st);
3241 error("union switch type must be an integer, char, or enum\n");
3244 else if (type_get_type(st) == TYPE_ENUM)
3245 fc = get_enum_fc(st);
3247 error("union switch type must be an integer, char, or enum\n");
3249 print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION);
3250 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3251 0x40 | fc, string_of_type(fc));
3254 else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
3256 const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
3257 const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
3260 if (type_get_type(st) == TYPE_BASIC)
3262 fc = get_basic_fc(st);
3277 error("union switch type must be an integer, char, or enum\n");
3280 else if (type_get_type(st) == TYPE_ENUM)
3281 fc = get_enum_fc(st);
3283 error("union switch type must be an integer, char, or enum\n");
3285 print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION);
3286 print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
3287 fc, string_of_type(fc));
3289 *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
3290 print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
3292 print_file(file, 0, "/* %u */\n", *tfsoff);
3295 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
3296 print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
3299 if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
3301 type_t *ft = f->type;
3302 expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
3303 int deflt = is_attr(f->attrs, ATTR_DEFAULT);
3306 if (cases == NULL && !deflt)
3307 error("union field %s with neither case nor default attribute\n", f->name);
3309 if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
3311 /* MIDL doesn't check for duplicate cases, even though that seems
3312 like a reasonable thing to do, it just dumps them to the TFS
3313 like we're going to do here. */
3314 print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
3316 write_branch_type(file, ft, tfsoff);
3319 /* MIDL allows multiple default branches, even though that seems
3320 illogical, it just chooses the last one, which is what we will
3331 write_branch_type(file, deftype, tfsoff);
3335 print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
3339 return start_offset;
3342 static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
3343 unsigned int *typeformat_offset)
3346 unsigned int start_offset = *typeformat_offset;
3347 expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
3349 if (!iid && processed(type)) return type->typestring_offset;
3351 print_start_tfs_comment(file, type, start_offset);
3352 update_tfsoff(type, start_offset, file);
3356 print_file(file, 2, "0x2f, /* FC_IP */\n");
3357 print_file(file, 2, "0x5c, /* FC_PAD */\n");
3359 += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
3363 const type_t *base = is_ptr(type) ? type_pointer_get_ref(type) : type;
3364 const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
3367 error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
3369 print_file(file, 2, "0x2f,\t/* FC_IP */\n");
3370 print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3371 print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
3372 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
3373 print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
3374 for (i = 0; i < 8; ++i)
3375 print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
3378 fprintf(file, "\n");
3380 *typeformat_offset += 18;
3382 return start_offset;
3385 static unsigned int write_contexthandle_tfs(FILE *file,
3386 const attr_list_t *attrs,
3389 unsigned int *typeformat_offset)
3391 unsigned int start_offset = *typeformat_offset;
3392 unsigned char flags = get_contexthandle_flags( current_iface, attrs, type );
3394 print_start_tfs_comment(file, type, start_offset);
3396 if (flags & 0x80) /* via ptr */
3398 int pointer_type = get_pointer_fc( type, attrs, toplevel_param );
3399 if (!pointer_type) pointer_type = RPC_FC_RP;
3400 *typeformat_offset += 4;
3401 print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
3402 print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
3403 print_file(file, 0, "/* %2u */\n", *typeformat_offset);
3406 print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT);
3407 print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
3408 /* return and can't be null values overlap */
3409 if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
3410 print_file(file, 0, "can't be null, ");
3411 if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
3412 print_file(file, 0, "serialize, ");
3413 if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
3414 print_file(file, 0, "no serialize, ");
3415 if (flags & NDR_STRICT_CONTEXT_HANDLE)
3416 print_file(file, 0, "strict, ");
3417 if ((flags & 0x21) == 0x20)
3418 print_file(file, 0, "out, ");
3419 if ((flags & 0x21) == 0x21)
3420 print_file(file, 0, "return, ");
3422 print_file(file, 0, "in, ");
3424 print_file(file, 0, "via ptr, ");
3425 print_file(file, 0, "*/\n");
3426 print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
3427 print_file(file, 2, "0, /* FIXME: param num */\n");
3428 *typeformat_offset += 4;
3430 update_tfsoff( type, start_offset, file );
3431 return start_offset;
3434 static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
3435 type_t *type, expr_list_t *range_list,
3436 unsigned int *typeformat_offset)
3439 unsigned int start_offset = *typeformat_offset;
3440 const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
3441 const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
3443 if (type_get_type(type) == TYPE_BASIC)
3444 fc = get_basic_fc(type);
3446 fc = get_enum_fc(type);
3448 /* fc must fit in lower 4-bits of 8-bit field below */
3451 print_file(file, 0, "/* %u */\n", *typeformat_offset);
3452 print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE);
3453 print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
3454 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
3455 print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
3456 update_tfsoff( type, start_offset, file );
3457 *typeformat_offset += 10;
3459 return start_offset;
3462 static unsigned int write_type_tfs(FILE *file, int indent,
3463 const attr_list_t *attrs, type_t *type,
3465 enum type_context context,
3466 unsigned int *typeformat_offset)
3468 unsigned int offset;
3470 switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
3472 case TGT_CTXT_HANDLE:
3473 case TGT_CTXT_HANDLE_POINTER:
3474 return write_contexthandle_tfs(file, attrs, type,
3475 context == TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset);
3477 return write_user_tfs(file, type, typeformat_offset);
3479 return write_string_tfs(file, attrs, type,
3480 context == TYPE_CONTEXT_TOPLEVELPARAM,
3481 name, typeformat_offset);
3485 /* conformant and pointer arrays are handled specially */
3486 if ((context != TYPE_CONTEXT_CONTAINER &&
3487 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
3488 !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
3489 off = write_array_tfs(file, attrs, type, name, typeformat_offset);
3492 if (context != TYPE_CONTEXT_CONTAINER &&
3493 context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3496 ptr_type = get_pointer_fc(type, attrs,
3497 context == TYPE_CONTEXT_TOPLEVELPARAM);
3498 if (ptr_type != RPC_FC_RP || type_array_is_decl_as_ptr(type))
3500 unsigned int absoff = type->typestring_offset;
3501 short reloff = absoff - (*typeformat_offset + 2);
3502 off = *typeformat_offset;
3503 print_file(file, 0, "/* %d */\n", off);
3504 print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
3505 string_of_type(ptr_type));
3506 print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3507 reloff, reloff, absoff);
3508 if (ptr_type != RPC_FC_RP) update_tfsoff( type, off, file );
3509 *typeformat_offset += 4;
3511 type->details.array.ptr_tfsoff = off;
3516 return write_struct_tfs(file, type, name, typeformat_offset);
3518 return write_union_tfs(file, attrs, type, typeformat_offset);
3525 expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
3527 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
3528 return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
3530 case TGT_IFACE_POINTER:
3531 return write_ip_tfs(file, attrs, type, typeformat_offset);
3534 enum type_context ref_context;
3535 if (context == TYPE_CONTEXT_TOPLEVELPARAM)
3536 ref_context = TYPE_CONTEXT_PARAM;
3537 else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3538 ref_context = TYPE_CONTEXT_CONTAINER;
3540 ref_context = context;
3541 offset = write_type_tfs( file, indent, attrs, type_pointer_get_ref(type), name,
3542 ref_context, typeformat_offset);
3543 if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
3545 return write_pointer_tfs(file, attrs, type, offset,
3546 context == TYPE_CONTEXT_TOPLEVELPARAM,
3552 error("invalid type %s for var %s\n", type->name, name);
3556 static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
3557 const char *name, int write_ptr, unsigned int *tfsoff)
3559 return write_type_tfs(file, 2, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
3562 static unsigned int process_tfs_stmts(FILE *file, const statement_list_t *stmts,
3563 type_pred_t pred, unsigned int *typeformat_offset)
3566 const statement_t *stmt;
3568 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3570 const type_t *iface;
3571 const statement_t *stmt_func;
3573 if (stmt->type == STMT_LIBRARY)
3575 process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
3578 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
3581 iface = stmt->u.type;
3585 current_iface = iface;
3586 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
3588 const var_t *func = stmt_func->u.var;
3589 current_func = func;
3590 if (is_local(func->attrs)) continue;
3592 if (!is_void(type_function_get_rettype(func->type)))
3594 write_type_tfs( file, 2, func->attrs, type_function_get_rettype(func->type),
3595 func->name, TYPE_CONTEXT_PARAM, typeformat_offset);
3598 if (type_get_function_args(func->type))
3599 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3600 write_type_tfs( file, 2, var->attrs, var->type, var->name,
3601 TYPE_CONTEXT_TOPLEVELPARAM, typeformat_offset );
3605 return *typeformat_offset + 1;
3608 static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3610 unsigned int typeformat_offset = 2;
3612 return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
3616 void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
3620 print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3621 print_file(file, indent, "{\n");
3623 print_file(file, indent, "0,\n");
3624 print_file(file, indent, "{\n");
3626 print_file(file, indent, "NdrFcShort(0x0),\n");
3628 set_all_tfswrite(TRUE);
3629 process_tfs(file, stmts, pred);
3631 print_file(file, indent, "0x0\n");
3633 print_file(file, indent, "}\n");
3635 print_file(file, indent, "};\n");
3636 print_file(file, indent, "\n");
3639 static unsigned int get_required_buffer_size_type(
3640 const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
3643 switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
3648 const type_t *utype = get_user_type(type, &uname);
3649 return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
3652 switch (get_basic_fc(type))
3670 case RPC_FC_ERROR_STATUS_T:
3679 case RPC_FC_INT3264:
3680 case RPC_FC_UINT3264:
3681 assert( pointer_size );
3682 *alignment = pointer_size;
3683 return pointer_size;
3686 case RPC_FC_BIND_PRIMITIVE:
3690 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3691 get_basic_fc(type));
3697 switch (get_enum_fc(type))
3709 if (get_struct_fc(type) == RPC_FC_STRUCT)
3711 if (!type_struct_get_fields(type)) return 0;
3712 return fields_memsize(type_struct_get_fields(type), alignment);
3718 unsigned int size, align;
3719 const type_t *ref = type_pointer_get_ref(type);
3720 if (is_string_type( attrs, ref )) break;
3721 if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
3722 if (get_pointer_fc(type, attrs, toplevel_param) != RPC_FC_RP)
3732 if (get_pointer_fc(type, attrs, toplevel_param) == RPC_FC_RP)
3734 switch (get_array_fc(type))
3736 case RPC_FC_SMFARRAY:
3737 case RPC_FC_LGFARRAY:
3738 return type_array_get_dim(type) *
3739 get_required_buffer_size_type(type_array_get_element(type), name,
3740 NULL, FALSE, alignment);
3751 static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
3753 int in_attr = is_attr(var->attrs, ATTR_IN);
3754 int out_attr = is_attr(var->attrs, ATTR_OUT);
3756 if (!in_attr && !out_attr)
3761 if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
3762 pass == PASS_RETURN)
3764 if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
3770 if (!is_string_type(var->attrs, var->type))
3771 return get_required_buffer_size_type(var->type, var->name,
3772 var->attrs, TRUE, alignment);
3777 static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
3780 unsigned int total_size = 0, alignment;
3782 if (type_get_function_args(func->type))
3784 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
3786 total_size += get_required_buffer_size(var, &alignment, pass);
3787 total_size += alignment;
3791 if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->type)))
3794 v.type = type_function_get_rettype(func->type);
3795 total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
3796 total_size += alignment;
3801 static void print_phase_function(FILE *file, int indent, const char *type,
3802 const char *local_var_prefix, enum remoting_phase phase,
3803 const var_t *var, unsigned int type_offset)
3805 const char *function;
3808 case PHASE_BUFFERSIZE:
3809 function = "BufferSize";
3812 function = "Marshall";
3814 case PHASE_UNMARSHAL:
3815 function = "Unmarshall";
3825 print_file(file, indent, "Ndr%s%s(\n", type, function);
3827 print_file(file, indent, "&__frame->_StubMsg,\n");
3828 print_file(file, indent, "%s%s%s%s%s,\n",
3829 (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
3830 (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
3832 (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
3834 print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3835 type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
3836 if (phase == PHASE_UNMARSHAL)
3837 print_file(file, indent, "0);\n");
3841 void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
3842 enum remoting_phase phase, enum pass pass, const var_t *var,
3843 const char *varname)
3845 type_t *type = var->type;
3846 unsigned int alignment = 0;
3848 /* no work to do for other phases, buffer sizing is done elsewhere */
3849 if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
3852 if (type_get_type(type) == TYPE_ENUM ||
3853 (type_get_type(type) == TYPE_BASIC &&
3854 type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
3859 if (type_get_type(type) == TYPE_ENUM)
3860 fc = get_enum_fc(type);
3862 fc = get_basic_fc(type);
3864 if (phase == PHASE_MARSHAL)
3865 print_file(file, indent, "NdrSimpleTypeMarshall(\n");
3867 print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
3868 print_file(file, indent+1, "&__frame->_StubMsg,\n");
3869 print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
3872 print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
3876 const type_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : type;
3877 switch (get_basic_fc(ref))
3895 case RPC_FC_ERROR_STATUS_T:
3896 /* pointer_size must be 4 if we got here in these two cases */
3897 case RPC_FC_INT3264:
3898 case RPC_FC_UINT3264:
3908 case RPC_FC_BIND_PRIMITIVE:
3909 /* no marshalling needed */
3913 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3914 var->name, get_basic_fc(ref));
3917 if (phase == PHASE_MARSHAL && alignment > 1)
3918 print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
3919 print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3920 alignment - 1, alignment - 1);
3922 if (phase == PHASE_MARSHAL)
3924 print_file(file, indent, "*(");
3925 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3927 fprintf(file, " *)__frame->_StubMsg.Buffer = *");
3929 fprintf(file, " *)__frame->_StubMsg.Buffer = ");
3930 fprintf(file, "%s%s", local_var_prefix, varname);
3931 fprintf(file, ";\n");
3933 else if (phase == PHASE_UNMARSHAL)
3935 print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
3936 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3937 fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
3938 print_file(file, indent, "{\n");
3939 print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3940 print_file(file, indent, "}\n");
3941 print_file(file, indent, "%s%s%s",
3942 (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
3943 local_var_prefix, varname);
3944 if (pass == PASS_IN && is_ptr(type))
3945 fprintf(file, " = (");
3947 fprintf(file, " = *(");
3948 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3949 fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
3952 print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
3953 write_type_decl(file, is_ptr(type) ? type_pointer_get_ref(type) : type, NULL);
3954 fprintf(file, ");\n");
3958 /* returns whether the MaxCount, Offset or ActualCount members need to be
3959 * filled in for the specified phase */
3960 static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
3962 return (phase != PHASE_UNMARSHAL);
3965 expr_t *get_size_is_expr(const type_t *t, const char *name)
3969 for ( ; is_array(t); t = type_array_get_element(t))
3970 if (type_array_has_conformance(t) &&
3971 type_array_get_conformance(t)->type != EXPR_VOID)
3974 x = type_array_get_conformance(t);
3976 error("%s: multidimensional conformant"
3977 " arrays not supported at the top level\n",
3984 static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
3985 enum remoting_phase phase, const var_t *var)
3987 const type_t *type = var->type;
3988 /* get fundamental type for the argument */
3991 switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
3994 if (is_conformance_needed_for_phase(phase))
3996 if (type_array_has_conformance(type) &&
3997 type_array_get_conformance(type)->type != EXPR_VOID)
3999 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4000 write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
4001 fprintf(file, ";\n\n");
4003 if (type_array_has_variance(type))
4005 print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4006 print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4007 write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
4008 fprintf(file, ";\n\n");
4013 if (type_get_type(type) == TYPE_UNION &&
4014 is_conformance_needed_for_phase(phase))
4016 print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4017 write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
4018 fprintf(file, ";\n\n");
4021 case TGT_IFACE_POINTER:
4025 if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
4027 print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4028 write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
4029 fprintf( file, ";\n\n" );
4034 type = type_pointer_get_ref(type);
4038 case TGT_CTXT_HANDLE:
4039 case TGT_CTXT_HANDLE_POINTER:
4051 static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4052 enum pass pass, enum remoting_phase phase, const var_t *var)
4054 int in_attr, out_attr, pointer_type;
4055 const char *type_str = NULL;
4056 const type_t *type = var->type;
4057 unsigned int alignment, start_offset = type->typestring_offset;
4059 if (is_ptr(type) || is_array(type))
4060 pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
4064 in_attr = is_attr(var->attrs, ATTR_IN);
4065 out_attr = is_attr(var->attrs, ATTR_OUT);
4066 if (!in_attr && !out_attr)
4069 if (phase != PHASE_FREE)
4073 if (!in_attr) return;
4076 if (!out_attr) return;
4082 if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
4084 write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
4086 switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
4088 case TGT_CTXT_HANDLE:
4089 case TGT_CTXT_HANDLE_POINTER:
4090 if (phase == PHASE_MARSHAL)
4092 if (pass == PASS_IN)
4094 /* if the context_handle attribute appears in the chain of types
4095 * without pointers being followed, then the context handle must
4096 * be direct, otherwise it is a pointer */
4097 int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
4098 print_file(file, indent, "NdrClientContextMarshall(\n");
4099 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4100 print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
4101 print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
4105 print_file(file, indent, "NdrServerContextNewMarshall(\n");
4106 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4107 print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
4108 print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
4109 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4112 else if (phase == PHASE_UNMARSHAL)
4114 if (pass == PASS_OUT)
4117 print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
4118 print_file(file, indent, "NdrClientContextUnmarshall(\n");
4119 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4120 print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
4121 print_file(file, indent + 1, "__frame->_Handle);\n");
4125 print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
4126 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4127 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
4132 print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
4135 if (phase == PHASE_FREE || pass == PASS_RETURN ||
4136 pointer_type != RPC_FC_RP)
4138 if (pointer_type == RPC_FC_RP && phase == PHASE_FREE &&
4139 !in_attr && is_conformant_array(type))
4141 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4143 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4145 /* strings returned are assumed to be global and hence don't
4147 else if (is_declptr(type) &&
4148 !(phase == PHASE_FREE && pass == PASS_RETURN))
4149 print_phase_function(file, indent, "Pointer", local_var_prefix,
4150 phase, var, start_offset);
4154 unsigned int real_start_offset = start_offset;
4155 /* skip over pointer description straight to string description */
4156 if (is_declptr(type))
4158 if (is_conformant_array(type))
4159 real_start_offset += 4;
4161 real_start_offset += 2;
4163 if (is_array(type) && !is_conformant_array(type))
4164 print_phase_function(file, indent, "NonConformantString",
4165 local_var_prefix, phase, var,
4168 print_phase_function(file, indent, "ConformantString", local_var_prefix,
4169 phase, var, real_start_offset);
4174 unsigned char tc = get_array_fc(type);
4175 const char *array_type = NULL;
4177 /* We already have the size_is expression since it's at the
4178 top level, but do checks for multidimensional conformant
4179 arrays. When we handle them, we'll need to extend this
4180 function to return a list, and then we'll actually use
4181 the return value. */
4182 get_size_is_expr(type, var->name);
4186 case RPC_FC_SMFARRAY:
4187 case RPC_FC_LGFARRAY:
4188 array_type = "FixedArray";
4190 case RPC_FC_SMVARRAY:
4191 case RPC_FC_LGVARRAY:
4192 array_type = "VaryingArray";
4195 array_type = "ConformantArray";
4197 case RPC_FC_CVARRAY:
4198 array_type = "ConformantVaryingArray";
4200 case RPC_FC_BOGUS_ARRAY:
4201 array_type = "ComplexArray";
4205 if (pointer_type != RPC_FC_RP) array_type = "Pointer";
4206 print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
4207 if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
4209 /* these are all unmarshalled by allocating memory */
4210 if (tc == RPC_FC_BOGUS_ARRAY ||
4211 tc == RPC_FC_CVARRAY ||
4212 ((tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY) && in_attr) ||
4213 (tc == RPC_FC_CARRAY && !in_attr))
4215 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4217 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4223 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4226 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4229 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4230 /* Note: this goes beyond what MIDL does - it only supports arguments
4231 * with the [range] attribute in Oicf mode */
4232 if (phase == PHASE_UNMARSHAL)
4234 const expr_t *range_min;
4235 const expr_t *range_max;
4236 expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
4238 range_list = get_aliaschain_attrp(type, ATTR_RANGE);
4239 range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
4240 range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
4242 print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
4243 write_type_decl(file, var->type, NULL);
4244 fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
4245 write_type_decl(file, var->type, NULL);
4246 fprintf(file, ")0x%x))\n", range_max->cval);
4247 print_file(file, indent, "{\n");
4248 print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4249 print_file(file, indent, "}\n");
4253 switch (get_struct_fc(type))
4256 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4257 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4259 case RPC_FC_PSTRUCT:
4260 print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
4262 case RPC_FC_CSTRUCT:
4263 case RPC_FC_CPSTRUCT:
4264 print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
4266 case RPC_FC_CVSTRUCT:
4267 print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
4269 case RPC_FC_BOGUS_STRUCT:
4270 print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
4273 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
4278 const char *union_type = NULL;
4280 if (type_get_type(type) == TYPE_UNION)
4281 union_type = "NonEncapsulatedUnion";
4282 else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
4283 union_type = "EncapsulatedUnion";
4285 print_phase_function(file, indent, union_type, local_var_prefix,
4286 phase, var, start_offset);
4291 const type_t *ref = type_pointer_get_ref(type);
4292 if (pointer_type == RPC_FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
4295 print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
4298 /* base types have known sizes, so don't need a sizing pass
4299 * and don't have any memory to free and so don't need a
4301 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4302 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4305 switch (get_struct_fc(ref))
4308 /* simple structs have known sizes, so don't need a sizing
4309 * pass and don't have any memory to free and so don't
4310 * need a freeing pass */
4311 if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
4312 type_str = "SimpleStruct";
4313 else if (phase == PHASE_FREE && pass == PASS_RETURN)
4315 print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
4317 print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
4321 case RPC_FC_PSTRUCT:
4322 type_str = "SimpleStruct";
4324 case RPC_FC_CSTRUCT:
4325 case RPC_FC_CPSTRUCT:
4326 type_str = "ConformantStruct";
4328 case RPC_FC_CVSTRUCT:
4329 type_str = "ConformantVaryingStruct";
4331 case RPC_FC_BOGUS_STRUCT:
4332 type_str = "ComplexStruct";
4335 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
4340 if (phase == PHASE_FREE)
4341 type_str = "Pointer";
4343 start_offset = ref->typestring_offset;
4344 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4348 if (phase == PHASE_FREE)
4349 type_str = "Pointer";
4352 if (type_get_type(ref) == TYPE_UNION)
4353 type_str = "NonEncapsulatedUnion";
4354 else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
4355 type_str = "EncapsulatedUnion";
4357 start_offset = ref->typestring_offset;
4360 print_phase_function(file, indent, type_str, local_var_prefix,
4361 phase, var, start_offset);
4364 if (phase != PHASE_FREE)
4366 type_str = "UserMarshal";
4367 start_offset = ref->typestring_offset;
4369 else type_str = "Pointer";
4371 print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
4377 case TGT_IFACE_POINTER:
4378 case TGT_CTXT_HANDLE:
4379 case TGT_CTXT_HANDLE_POINTER:
4380 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4387 print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
4390 case TGT_IFACE_POINTER:
4391 print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
4397 fprintf(file, "\n");
4400 void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
4401 enum pass pass, enum remoting_phase phase)
4403 if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
4405 unsigned int size = get_function_buffer_size( func, pass );
4406 print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
4409 if (pass == PASS_RETURN)
4413 var.type = type_function_get_rettype(func->type);
4414 var.name = xstrdup( "_RetVal" );
4415 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
4421 if (!type_get_function_args(func->type))
4423 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4424 write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
4429 unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
4431 unsigned int offset = 0;
4432 write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
4436 unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
4438 const statement_t *stmt;
4439 unsigned int size = 1;
4441 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
4443 const type_t *iface;
4444 const statement_t *stmt_func;
4446 if (stmt->type == STMT_LIBRARY)
4448 size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
4451 else if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
4454 iface = stmt->u.type;
4458 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(iface) )
4460 const var_t *func = stmt_func->u.var;
4461 if (!is_local(func->attrs))
4462 size += get_size_procformatstring_func( iface, func );
4468 unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
4470 set_all_tfswrite(FALSE);
4471 return process_tfs(NULL, stmts, pred);
4474 void declare_stub_args( FILE *file, int indent, const var_t *func )
4476 int in_attr, out_attr;
4480 /* declare return value '_RetVal' */
4481 if (!is_void(type_function_get_rettype(func->type)))
4483 print_file(file, indent, "%s", "");
4484 write_type_decl_left(file, type_function_get_rettype(func->type));
4485 fprintf(file, " _RetVal;\n");
4488 if (!type_get_function_args(func->type))
4491 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4493 in_attr = is_attr(var->attrs, ATTR_IN);
4494 out_attr = is_attr(var->attrs, ATTR_OUT);
4495 if (!out_attr && !in_attr)
4498 if (is_context_handle(var->type))
4499 print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
4502 if (!in_attr && !is_conformant_array(var->type))
4504 type_t *type_to_print;
4506 print_file(file, indent, "%s", "");
4507 if (type_get_type(var->type) == TYPE_ARRAY &&
4508 !type_array_is_decl_as_ptr(var->type))
4509 type_to_print = var->type;
4511 type_to_print = type_pointer_get_ref(var->type);
4512 sprintf(name, "_W%u", i++);
4513 write_type_decl(file, type_to_print, name);
4514 fprintf(file, ";\n");
4517 print_file(file, indent, "%s", "");
4518 write_type_decl_left(file, var->type);
4520 if (type_get_type(var->type) == TYPE_ARRAY &&
4521 !type_array_is_decl_as_ptr(var->type)) {
4522 fprintf(file, "(*%s)", var->name);
4524 fprintf(file, "%s", var->name);
4525 write_type_right(file, var->type, FALSE);
4526 fprintf(file, ";\n");
4528 if (decl_indirect(var->type))
4529 print_file(file, indent, "void *_p_%s;\n", var->name);
4535 void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
4537 int in_attr, out_attr;
4541 if (!type_get_function_args(func->type))
4544 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
4546 in_attr = is_attr(var->attrs, ATTR_IN);
4547 out_attr = is_attr(var->attrs, ATTR_OUT);
4548 if (!out_attr && !in_attr)
4553 print_file(file, indent, "%s%s", local_var_prefix, var->name);
4555 switch (typegen_detect_type(var->type, var->attrs, TDT_IGNORE_STRINGS))
4557 case TGT_CTXT_HANDLE_POINTER:
4558 fprintf(file, " = NdrContextHandleInitialize(\n");
4559 print_file(file, indent + 1, "&__frame->_StubMsg,\n");
4560 print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4561 var->type->typestring_offset);
4564 if (type_array_has_conformance(var->type))
4569 fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
4570 for (type = var->type;
4571 is_array(type) && type_array_has_conformance(type);
4572 type = type_array_get_element(type))
4574 write_expr(file, type_array_get_conformance(type), TRUE,
4575 TRUE, NULL, NULL, local_var_prefix);
4576 fprintf(file, " * ");
4578 size = type_memsize(type);
4579 fprintf(file, "%u);\n", size);
4581 print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
4582 for (type = var->type;
4583 is_array(type) && type_array_has_conformance(type);
4584 type = type_array_get_element(type))
4586 write_expr(file, type_array_get_conformance(type), TRUE,
4587 TRUE, NULL, NULL, local_var_prefix);
4588 fprintf(file, " * ");
4590 size = type_memsize(type);
4591 fprintf(file, "%u);\n", size);
4594 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
4597 fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
4598 switch (typegen_detect_type(type_pointer_get_ref(var->type), var->attrs, TDT_IGNORE_STRINGS))
4604 case TGT_IFACE_POINTER:
4605 print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
4608 print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4609 local_var_prefix, i, local_var_prefix, i);
4614 case TGT_CTXT_HANDLE:
4615 case TGT_CTXT_HANDLE_POINTER:
4618 /* not initialised */
4631 fprintf(file, "\n");
4635 int write_expr_eval_routines(FILE *file, const char *iface)
4637 static const char *var_name = "pS";
4638 static const char *var_name_expr = "pS->";
4640 struct expr_eval_routine *eval;
4641 unsigned short callback_offset = 0;
4643 LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
4645 const char *name = eval->name;
4648 print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4649 iface, name, callback_offset);
4650 print_file(file, 0, "{\n");
4651 if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
4653 const var_list_t *args = type_get_function_args( eval->cont_type );
4656 print_file(file, 1, "struct _PARAM_STRUCT\n" );
4657 print_file(file, 1, "{\n" );
4658 if (is_object( eval->iface )) print_file(file, 2, "%s *This;\n", eval->iface->name );
4660 if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
4662 print_file(file, 2, "%s", "");
4663 write_type_left( file, (type_t *)arg->type, TRUE );
4664 if (needs_space_after( arg->type )) fputc( ' ', file );
4665 if (is_array( arg->type ) && !type_array_is_decl_as_ptr( arg->type )) fputc( '*', file );
4666 /* FIXME: should check for large args being passed by pointer */
4667 if (is_array( arg->type ) || is_ptr( arg->type ) || type_memsize( arg->type ) == pointer_size)
4668 fprintf( file, "%s;\n", arg->name );
4670 fprintf( file, "%s DECLSPEC_ALIGN(%u);\n", arg->name, pointer_size );
4672 print_file(file, 1, "} *pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop;\n" );
4676 print_file(file, 1, "%s", "");
4677 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4678 fprintf(file, " *%s = (", var_name);
4679 write_type_left(file, (type_t *)eval->cont_type, TRUE);
4680 fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
4682 print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4683 print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4684 write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
4685 fprintf(file, ";\n");
4686 print_file(file, 0, "}\n\n");
4692 void write_expr_eval_routine_list(FILE *file, const char *iface)
4694 struct expr_eval_routine *eval;
4695 struct expr_eval_routine *cursor;
4696 unsigned short callback_offset = 0;
4698 fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4699 fprintf(file, "{\n");
4701 LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
4703 print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, eval->name, callback_offset);
4705 list_remove(&eval->entry);
4710 fprintf(file, "};\n\n");
4713 void write_user_quad_list(FILE *file)
4717 if (list_empty(&user_type_list))
4720 fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4721 fprintf(file, "{\n");
4722 LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
4724 const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
4725 print_file(file, 1, "{\n");
4726 print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
4727 print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
4728 print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
4729 print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
4730 print_file(file, 1, "}%s\n", sep);
4732 fprintf(file, "};\n\n");
4735 void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
4737 const struct str_list_entry_t *endpoint;
4740 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4741 print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
4742 LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
4744 print_file( f, 1, "{ (const unsigned char *)\"" );
4745 for (p = endpoint->str; *p && *p != ':'; p++)
4747 if (*p == '"' || *p == '\\') fputc( '\\', f );
4750 if (!*p) goto error;
4751 if (p[1] != '[') goto error;
4753 fprintf( f, "\", (const unsigned char *)\"" );
4754 for (p += 2; *p && *p != ']'; p++)
4756 if (*p == '"' || *p == '\\') fputc( '\\', f );
4759 if (*p != ']') goto error;
4760 fprintf( f, "\" },\n" );
4762 print_file( f, 0, "};\n\n" );
4766 error("Invalid endpoint syntax '%s'\n", endpoint->str);
4769 void write_exceptions( FILE *file )
4771 fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4772 fprintf( file, "\n");
4773 fprintf( file, "#include \"wine/exception.h\"\n");
4774 fprintf( file, "#undef RpcTryExcept\n");
4775 fprintf( file, "#undef RpcExcept\n");
4776 fprintf( file, "#undef RpcEndExcept\n");
4777 fprintf( file, "#undef RpcTryFinally\n");
4778 fprintf( file, "#undef RpcFinally\n");
4779 fprintf( file, "#undef RpcEndFinally\n");
4780 fprintf( file, "#undef RpcExceptionCode\n");
4781 fprintf( file, "#undef RpcAbnormalTermination\n");
4782 fprintf( file, "\n");
4783 fprintf( file, "struct __exception_frame;\n");
4784 fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4785 fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4786 fprintf( file, "\n");
4787 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4788 fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4789 fprintf( file, " __filter_func filter; \\\n");
4790 fprintf( file, " __finally_func finally; \\\n");
4791 fprintf( file, " sigjmp_buf jmp; \\\n");
4792 fprintf( file, " DWORD code; \\\n");
4793 fprintf( file, " unsigned char abnormal_termination; \\\n");
4794 fprintf( file, " unsigned char filter_level; \\\n");
4795 fprintf( file, " unsigned char finally_level;\n");
4796 fprintf( file, "\n");
4797 fprintf( file, "struct __exception_frame\n{\n");
4798 fprintf( file, " __DECL_EXCEPTION_FRAME\n");
4799 fprintf( file, "};\n");
4800 fprintf( file, "\n");
4801 fprintf( file, "static inline void __widl_unwind_target(void)\n" );
4802 fprintf( file, "{\n");
4803 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4804 fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4805 fprintf( file, " {\n");
4806 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4807 fprintf( file, " exc_frame->finally( exc_frame );\n");
4808 fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
4809 fprintf( file, " }\n");
4810 fprintf( file, " exc_frame->filter_level = 0;\n");
4811 fprintf( file, " siglongjmp( exc_frame->jmp, 1 );\n");
4812 fprintf( file, "}\n");
4813 fprintf( file, "\n");
4814 fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4815 fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4816 fprintf( file, " CONTEXT *context,\n");
4817 fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4818 fprintf( file, "{\n");
4819 fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4820 fprintf( file, "\n");
4821 fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4822 fprintf( file, " {\n" );
4823 fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4824 fprintf( file, " {\n" );
4825 fprintf( file, " exc_frame->abnormal_termination = 1;\n");
4826 fprintf( file, " exc_frame->finally( exc_frame );\n");
4827 fprintf( file, " }\n" );
4828 fprintf( file, " return ExceptionContinueSearch;\n");
4829 fprintf( file, " }\n" );
4830 fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
4831 fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4832 fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4833 fprintf( file, " return ExceptionContinueSearch;\n");
4834 fprintf( file, "}\n");
4835 fprintf( file, "\n");
4836 fprintf( file, "#define RpcTryExcept \\\n");
4837 fprintf( file, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4838 fprintf( file, " { \\\n");
4839 fprintf( file, " if (!__frame->finally_level) \\\n" );
4840 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4841 fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
4842 fprintf( file, "\n");
4843 fprintf( file, "#define RpcExcept(expr) \\\n");
4844 fprintf( file, " if (!__frame->finally_level) \\\n" );
4845 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4846 fprintf( file, " __frame->filter_level = 0; \\\n" );
4847 fprintf( file, " } \\\n");
4848 fprintf( file, " else \\\n");
4849 fprintf( file, "\n");
4850 fprintf( file, "#define RpcEndExcept\n");
4851 fprintf( file, "\n");
4852 fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
4853 fprintf( file, "\n");
4854 fprintf( file, "#define RpcTryFinally \\\n");
4855 fprintf( file, " if (!__frame->filter_level) \\\n");
4856 fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
4857 fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
4858 fprintf( file, "\n");
4859 fprintf( file, "#define RpcFinally \\\n");
4860 fprintf( file, " if (!__frame->filter_level) \\\n");
4861 fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
4862 fprintf( file, " __frame->finally_level = 0;\n");
4863 fprintf( file, "\n");
4864 fprintf( file, "#define RpcEndFinally\n");
4865 fprintf( file, "\n");
4866 fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4867 fprintf( file, "\n");
4868 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4869 fprintf( file, " do { \\\n");
4870 fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4871 fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4872 fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
4873 fprintf( file, " __frame->abnormal_termination = 0; \\\n");
4874 fprintf( file, " __frame->filter_level = 0; \\\n");
4875 fprintf( file, " __frame->finally_level = 0; \\\n");
4876 fprintf( file, " } while (0)\n");
4877 fprintf( file, "\n");
4878 fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4879 fprintf( file, "\n");
4880 fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4881 fprintf( file, " do { (void)(filter_func); } while(0)\n");
4882 fprintf( file, "\n");
4883 fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
4884 fprintf( file, " DWORD code;\n");
4885 fprintf( file, "\n");
4886 fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");