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
, enum type_context context
,
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_pointer_fc_context( const type_t
*type
, const attr_list_t
*attrs
,
282 enum type_context context
)
284 int pointer_fc
= get_pointer_fc(type
, attrs
, context
== TYPE_CONTEXT_TOPLEVELPARAM
);
286 if (pointer_fc
== RPC_FC_UP
&& is_attr( attrs
, ATTR_OUT
) &&
287 context
== TYPE_CONTEXT_PARAM
&& is_object( current_iface
))
288 pointer_fc
= RPC_FC_OP
;
293 static unsigned char get_enum_fc(const type_t
*type
)
295 assert(type_get_type(type
) == TYPE_ENUM
);
296 if (is_aliaschain_attr(type
, ATTR_V1ENUM
))
297 return RPC_FC_ENUM32
;
299 return RPC_FC_ENUM16
;
302 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
306 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
314 if (type_is_alias(t
))
315 t
= type_alias_get_aliasee(t
);
321 static int is_user_type(const type_t
*t
)
323 return get_user_type(t
, NULL
) != NULL
;
326 enum typegen_type
typegen_detect_type(const type_t
*type
, const attr_list_t
*attrs
, unsigned int flags
)
328 if (is_user_type(type
))
329 return TGT_USER_TYPE
;
331 if (is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
))
332 return TGT_CTXT_HANDLE
;
334 if (!(flags
& TDT_IGNORE_STRINGS
) && is_string_type(attrs
, type
))
337 switch (type_get_type(type
))
340 if (!(flags
& TDT_IGNORE_RANGES
) &&
341 (is_attr(attrs
, ATTR_RANGE
) || is_aliaschain_attr(type
, ATTR_RANGE
)))
345 if (!(flags
& TDT_IGNORE_RANGES
) &&
346 (is_attr(attrs
, ATTR_RANGE
) || is_aliaschain_attr(type
, ATTR_RANGE
)))
350 if (type_get_type(type_pointer_get_ref(type
)) == TYPE_INTERFACE
||
351 (type_get_type(type_pointer_get_ref(type
)) == TYPE_VOID
&& is_attr(attrs
, ATTR_IIDIS
)))
352 return TGT_IFACE_POINTER
;
353 else if (is_aliaschain_attr(type_pointer_get_ref(type
), ATTR_CONTEXTHANDLE
))
354 return TGT_CTXT_HANDLE_POINTER
;
359 case TYPE_ENCAPSULATED_UNION
:
376 static int cant_be_null(const var_t
*v
)
378 switch (typegen_detect_type(v
->type
, v
->attrs
, TDT_IGNORE_STRINGS
))
381 if (!type_array_is_decl_as_ptr( v
->type
)) return 0;
384 return (get_pointer_fc(v
->type
, v
->attrs
, TRUE
) == RPC_FC_RP
);
385 case TGT_CTXT_HANDLE_POINTER
:
393 static int get_padding(const var_list_t
*fields
)
395 unsigned short offset
= 0;
396 unsigned int salign
= 1;
402 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
404 type_t
*ft
= f
->type
;
405 unsigned int align
= 0;
406 unsigned int size
= type_memsize_and_alignment(ft
, &align
);
407 align
= clamp_align(align
);
408 if (align
> salign
) salign
= align
;
409 offset
= ROUND_SIZE(offset
, align
);
413 return ROUNDING(offset
, salign
);
416 static unsigned int get_stack_size( const var_t
*var
, int *by_value
)
418 unsigned int stack_size
;
421 switch (typegen_detect_type( var
->type
, var
->attrs
, TDT_ALL_TYPES
))
429 stack_size
= type_memsize( var
->type
);
430 by_val
= (pointer_size
< 8 || stack_size
<= pointer_size
); /* FIXME: should be platform-specific */
436 if (!by_val
) stack_size
= pointer_size
;
437 if (by_value
) *by_value
= by_val
;
438 return ROUND_SIZE( stack_size
, pointer_size
);
441 static unsigned char get_contexthandle_flags( const type_t
*iface
, const attr_list_t
*attrs
,
444 unsigned char flags
= 0;
446 if (is_attr(iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
)) flags
|= NDR_STRICT_CONTEXT_HANDLE
;
449 !is_attr( type
->attrs
, ATTR_CONTEXTHANDLE
) &&
450 !is_attr( attrs
, ATTR_CONTEXTHANDLE
))
453 if (is_attr(attrs
, ATTR_IN
))
456 if (!is_attr(attrs
, ATTR_OUT
)) flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
458 if (is_attr(attrs
, ATTR_OUT
)) flags
|= 0x20;
463 static unsigned int get_rpc_flags( const attr_list_t
*attrs
)
465 unsigned int flags
= 0;
467 if (is_attr( attrs
, ATTR_IDEMPOTENT
)) flags
|= 0x0001;
468 if (is_attr( attrs
, ATTR_BROADCAST
)) flags
|= 0x0002;
469 if (is_attr( attrs
, ATTR_MAYBE
)) flags
|= 0x0004;
470 if (is_attr( attrs
, ATTR_MESSAGE
)) flags
|= 0x0100;
471 if (is_attr( attrs
, ATTR_ASYNC
)) flags
|= 0x4000;
475 unsigned char get_struct_fc(const type_t
*type
)
478 int has_conformance
= 0;
479 int has_variance
= 0;
483 fields
= type_struct_get_fields(type
);
485 if (get_padding(fields
))
486 return RPC_FC_BOGUS_STRUCT
;
488 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, var_t
, entry
)
490 type_t
*t
= field
->type
;
491 enum typegen_type typegen_type
;
493 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
495 if (typegen_type
== TGT_ARRAY
&& !type_array_is_decl_as_ptr(t
))
497 if (is_string_type(field
->attrs
, field
->type
))
499 if (is_conformant_array(t
))
505 if (is_array(type_array_get_element(field
->type
)))
506 return RPC_FC_BOGUS_STRUCT
;
508 if (type_array_has_conformance(field
->type
))
511 if (list_next(fields
, &field
->entry
))
512 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
515 if (type_array_has_variance(t
))
518 t
= type_array_get_element(t
);
519 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
522 switch (typegen_type
)
525 case TGT_IFACE_POINTER
:
526 return RPC_FC_BOGUS_STRUCT
;
528 if (type_basic_get_type(t
) == TYPE_BASIC_INT3264
&& pointer_size
!= 4)
529 return RPC_FC_BOGUS_STRUCT
;
532 if (get_enum_fc(t
) == RPC_FC_ENUM16
)
533 return RPC_FC_BOGUS_STRUCT
;
537 if (get_pointer_fc(t
, field
->attrs
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
538 return RPC_FC_BOGUS_STRUCT
;
542 return RPC_FC_BOGUS_STRUCT
;
545 unsigned char fc
= get_struct_fc(t
);
550 case RPC_FC_CVSTRUCT
:
556 case RPC_FC_CPSTRUCT
:
558 if (list_next( fields
, &field
->entry
))
559 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
566 if (list_next( fields
, &field
->entry
))
567 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
576 error_loc("Unknown struct member %s with type (0x%02x)\n", field
->name
, fc
);
577 /* fallthru - treat it as complex */
579 /* as soon as we see one of these these members, it's bogus... */
580 case RPC_FC_BOGUS_STRUCT
:
581 return RPC_FC_BOGUS_STRUCT
;
586 return RPC_FC_BOGUS_STRUCT
;
588 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
590 case TGT_CTXT_HANDLE
:
591 case TGT_CTXT_HANDLE_POINTER
:
592 /* checking after parsing should mean that we don't get here. if we do,
593 * it's a checker bug */
600 if ( has_conformance
)
601 return RPC_FC_CVSTRUCT
;
603 return RPC_FC_BOGUS_STRUCT
;
605 if( has_conformance
&& has_pointer
)
606 return RPC_FC_CPSTRUCT
;
607 if( has_conformance
)
608 return RPC_FC_CSTRUCT
;
610 return RPC_FC_PSTRUCT
;
611 return RPC_FC_STRUCT
;
614 static unsigned char get_array_fc(const type_t
*type
)
617 const expr_t
*size_is
;
618 const type_t
*elem_type
;
620 elem_type
= type_array_get_element(type
);
621 size_is
= type_array_get_conformance(type
);
625 unsigned int size
= type_memsize(elem_type
);
626 if (size
* type_array_get_dim(type
) > 0xffffuL
)
627 fc
= RPC_FC_LGFARRAY
;
629 fc
= RPC_FC_SMFARRAY
;
634 if (type_array_has_variance(type
))
636 if (fc
== RPC_FC_SMFARRAY
)
637 fc
= RPC_FC_SMVARRAY
;
638 else if (fc
== RPC_FC_LGFARRAY
)
639 fc
= RPC_FC_LGVARRAY
;
640 else if (fc
== RPC_FC_CARRAY
)
644 switch (typegen_detect_type(elem_type
, NULL
, TDT_IGNORE_STRINGS
))
647 fc
= RPC_FC_BOGUS_ARRAY
;
650 if (type_basic_get_type(elem_type
) == TYPE_BASIC_INT3264
&&
652 fc
= RPC_FC_BOGUS_ARRAY
;
655 switch (get_struct_fc(elem_type
))
657 case RPC_FC_BOGUS_STRUCT
:
658 fc
= RPC_FC_BOGUS_ARRAY
;
663 /* is 16-bit enum - if so, wire size differs from mem size and so
664 * the array cannot be block copied, which means the array is complex */
665 if (get_enum_fc(elem_type
) == RPC_FC_ENUM16
)
666 fc
= RPC_FC_BOGUS_ARRAY
;
669 case TGT_IFACE_POINTER
:
670 fc
= RPC_FC_BOGUS_ARRAY
;
673 /* ref pointers cannot just be block copied. unique pointers to
674 * interfaces need special treatment. either case means the array is
676 if (get_pointer_fc(elem_type
, NULL
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
677 fc
= RPC_FC_BOGUS_ARRAY
;
680 fc
= RPC_FC_BOGUS_ARRAY
;
682 case TGT_CTXT_HANDLE
:
683 case TGT_CTXT_HANDLE_POINTER
:
687 /* nothing to do for everything else */
694 static int is_non_complex_struct(const type_t
*type
)
696 return (type_get_type(type
) == TYPE_STRUCT
&&
697 get_struct_fc(type
) != RPC_FC_BOGUS_STRUCT
);
700 static int type_has_pointers(const type_t
*type
)
702 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
709 return type_array_is_decl_as_ptr(type
) || type_has_pointers(type_array_get_element(type
));
712 var_list_t
*fields
= type_struct_get_fields(type
);
714 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
716 if (type_has_pointers(field
->type
))
725 fields
= type_union_get_cases(type
);
726 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
728 if (field
->type
&& type_has_pointers(field
->type
))
733 case TGT_CTXT_HANDLE
:
734 case TGT_CTXT_HANDLE_POINTER
:
736 case TGT_IFACE_POINTER
:
747 static int type_has_full_pointer(const type_t
*type
, const attr_list_t
*attrs
,
750 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
755 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
760 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
763 return type_has_full_pointer(type_array_get_element(type
), NULL
, FALSE
);
766 var_list_t
*fields
= type_struct_get_fields(type
);
768 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
770 if (type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
779 fields
= type_union_get_cases(type
);
780 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
782 if (field
->type
&& type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
787 case TGT_CTXT_HANDLE
:
788 case TGT_CTXT_HANDLE_POINTER
:
790 case TGT_IFACE_POINTER
:
801 static unsigned short user_type_offset(const char *name
)
804 unsigned short off
= 0;
805 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
807 if (strcmp(name
, ut
->name
) == 0)
811 error("user_type_offset: couldn't find type (%s)\n", name
);
815 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
817 type
->typestring_offset
= offset
;
818 if (file
) type
->tfswrite
= FALSE
;
821 static void guard_rec(type_t
*type
)
823 /* types that contain references to themselves (like a linked list),
824 need to be shielded from infinite recursion when writing embedded
826 if (type
->typestring_offset
)
827 type
->tfswrite
= FALSE
;
829 type
->typestring_offset
= 1;
832 static int is_embedded_complex(const type_t
*type
)
834 switch (typegen_detect_type(type
, NULL
, TDT_ALL_TYPES
))
840 case TGT_IFACE_POINTER
:
847 static const char *get_context_handle_type_name(const type_t
*type
)
851 is_ptr(t
) || type_is_alias(t
);
852 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
853 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
859 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
862 fprintf(file, "/* %2u */\n", typestring_offset); \
863 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
867 static void print_file(FILE *file
, int indent
, const char *format
, ...) __attribute__((format (printf
, 3, 4)));
868 static void print_file(FILE *file
, int indent
, const char *format
, ...)
871 va_start(va
, format
);
872 print(file
, indent
, format
, va
);
876 void print(FILE *file
, int indent
, const char *format
, va_list va
)
880 if (format
[0] != '\n')
883 vfprintf(file
, format
, va
);
888 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
, const char *local_var_prefix
)
890 if (decl_indirect(t
))
892 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
893 local_var_prefix
, n
, local_var_prefix
, n
);
894 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
896 else if (is_ptr(t
) || is_array(t
))
897 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
900 void write_parameters_init(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
902 const var_t
*var
= type_function_get_retval(func
->type
);
904 if (!is_void(var
->type
))
905 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
907 if (!type_get_function_args(func
->type
))
910 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
911 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
916 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
918 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
919 print_file(f
, indent
, "{\n");
920 print_file(f
, indent
+ 1, "short Pad;\n");
921 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
922 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
923 print_file(f
, indent
, "\n");
926 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
930 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
931 get_size_typeformatstring(stmts
, pred
));
933 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
934 get_size_procformatstring(stmts
, pred
));
937 write_formatdesc(f
, indent
, "TYPE");
938 write_formatdesc(f
, indent
, "PROC");
940 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
941 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
942 print_file(f
, indent
, "\n");
945 int decl_indirect(const type_t
*t
)
949 return (type_get_type(t
) != TYPE_BASIC
&&
950 type_get_type(t
) != TYPE_ENUM
&&
951 type_get_type(t
) != TYPE_POINTER
&&
952 type_get_type(t
) != TYPE_ARRAY
);
955 static unsigned char get_parameter_fc( const var_t
*var
, int is_return
, unsigned short *flags
,
956 unsigned int *stack_size
, unsigned int *typestring_offset
)
958 unsigned int alignment
, server_size
= 0, buffer_size
= 0;
959 unsigned char fc
= 0;
961 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
962 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
964 if (is_return
) is_out
= TRUE
;
965 else if (!is_in
&& !is_out
) is_in
= TRUE
;
968 *stack_size
= get_stack_size( var
, &is_byval
);
969 *typestring_offset
= var
->typestring_offset
;
971 if (is_in
) *flags
|= IsIn
;
972 if (is_out
) *flags
|= IsOut
;
973 if (is_return
) *flags
|= IsReturn
;
975 if (!is_string_type( var
->attrs
, var
->type
))
976 buffer_size
= get_required_buffer_size_type( var
->type
, NULL
, var
->attrs
, TRUE
, &alignment
);
978 switch (typegen_detect_type( var
->type
, var
->attrs
, TDT_ALL_TYPES
))
981 *flags
|= IsBasetype
;
982 fc
= get_basic_fc_signed( var
->type
);
983 if (fc
== RPC_FC_BIND_PRIMITIVE
)
985 buffer_size
= 4; /* actually 0 but avoids setting MustSize */
990 *flags
|= IsBasetype
;
991 fc
= get_enum_fc( var
->type
);
999 *flags
|= MustFree
| (is_byval
? IsByValue
: IsSimpleRef
);
1001 case TGT_IFACE_POINTER
:
1006 if (type_array_is_decl_as_ptr(var
->type
) && var
->type
->details
.array
.ptr_tfsoff
&&
1007 get_pointer_fc( var
->type
, var
->attrs
, !is_return
) == RPC_FC_RP
)
1009 *typestring_offset
= var
->type
->typestring_offset
;
1010 *flags
|= IsSimpleRef
;
1015 if (is_declptr( var
->type
) && get_pointer_fc( var
->type
, var
->attrs
, !is_return
) == RPC_FC_RP
)
1017 /* skip over pointer description straight to string description */
1018 if (is_conformant_array( var
->type
)) *typestring_offset
+= 4;
1019 else *typestring_offset
+= 2;
1020 *flags
|= IsSimpleRef
;
1023 case TGT_CTXT_HANDLE_POINTER
:
1024 *flags
|= IsSimpleRef
;
1025 *typestring_offset
+= 4;
1027 case TGT_CTXT_HANDLE
:
1031 if (get_pointer_fc( var
->type
, var
->attrs
, !is_return
) == RPC_FC_RP
)
1033 const type_t
*ref
= type_pointer_get_ref( var
->type
);
1035 if (!is_string_type( var
->attrs
, ref
))
1036 buffer_size
= get_required_buffer_size_type( ref
, NULL
, NULL
, TRUE
, &alignment
);
1038 switch (typegen_detect_type( ref
, NULL
, TDT_ALL_TYPES
))
1041 *flags
|= IsSimpleRef
| IsBasetype
;
1042 fc
= get_basic_fc( ref
);
1043 if (!is_in
&& is_out
) server_size
= pointer_size
;
1046 if ((fc
= get_enum_fc( ref
)) == RPC_FC_ENUM32
)
1048 *flags
|= IsSimpleRef
| IsBasetype
;
1049 if (!is_in
&& is_out
) server_size
= pointer_size
;
1053 server_size
= pointer_size
;
1059 *flags
|= IsSimpleRef
| MustFree
;
1060 *typestring_offset
= ref
->typestring_offset
;
1061 if (!is_in
&& is_out
) server_size
= type_memsize( ref
);
1066 case TGT_CTXT_HANDLE
:
1067 case TGT_CTXT_HANDLE_POINTER
:
1069 server_size
= pointer_size
;
1071 case TGT_IFACE_POINTER
:
1073 if (is_in
&& is_out
) server_size
= pointer_size
;
1076 *flags
|= IsSimpleRef
| MustFree
;
1077 *typestring_offset
= ref
->typestring_offset
;
1078 switch (get_struct_fc(ref
))
1081 case RPC_FC_PSTRUCT
:
1082 case RPC_FC_BOGUS_STRUCT
:
1083 if (!is_in
&& is_out
) server_size
= type_memsize( ref
);
1093 else /* not ref pointer */
1102 if (!buffer_size
) *flags
|= MustSize
;
1106 server_size
= (server_size
+ 7) / 8;
1107 if (server_size
< 8) *flags
|= server_size
<< 13;
1112 static unsigned char get_func_oi2_flags( const var_t
*func
)
1115 var_list_t
*args
= type_get_function_args( func
->type
);
1116 var_t
*retval
= type_function_get_retval( func
->type
);
1117 unsigned char oi2_flags
= 0x40; /* HasExtensions */
1118 unsigned short flags
;
1119 unsigned int stack_size
, typestring_offset
;
1121 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
1123 get_parameter_fc( var
, 0, &flags
, &stack_size
, &typestring_offset
);
1124 if (flags
& MustSize
)
1126 if (flags
& IsIn
) oi2_flags
|= 0x02; /* ClientMustSize */
1127 if (flags
& IsOut
) oi2_flags
|= 0x01; /* ServerMustSize */
1131 if (!is_void( retval
->type
))
1133 oi2_flags
|= 0x04; /* HasRet */
1134 get_parameter_fc( retval
, 1, &flags
, &stack_size
, &typestring_offset
);
1135 if (flags
& MustSize
) oi2_flags
|= 0x01; /* ServerMustSize */
1140 static unsigned int write_new_procformatstring_type(FILE *file
, int indent
, const var_t
*var
,
1141 int is_return
, unsigned int *stack_offset
)
1144 unsigned int stack_size
, typestring_offset
;
1145 unsigned short flags
;
1146 unsigned char fc
= get_parameter_fc( var
, is_return
, &flags
, &stack_size
, &typestring_offset
);
1148 strcpy( buffer
, "/* flags:" );
1149 if (flags
& MustSize
) strcat( buffer
, " must size," );
1150 if (flags
& MustFree
) strcat( buffer
, " must free," );
1151 if (flags
& IsPipe
) strcat( buffer
, " pipe," );
1152 if (flags
& IsIn
) strcat( buffer
, " in," );
1153 if (flags
& IsOut
) strcat( buffer
, " out," );
1154 if (flags
& IsReturn
) strcat( buffer
, " return," );
1155 if (flags
& IsBasetype
) strcat( buffer
, " base type," );
1156 if (flags
& IsByValue
) strcat( buffer
, " by value," );
1157 if (flags
& IsSimpleRef
) strcat( buffer
, " simple ref," );
1158 if (flags
>> 13) sprintf( buffer
+ strlen(buffer
), " srv size=%u,", (flags
>> 13) * 8 );
1159 strcpy( buffer
+ strlen( buffer
) - 1, " */" );
1160 print_file( file
, indent
, "NdrFcShort(0x%hx),\t%s\n", flags
, buffer
);
1161 print_file( file
, indent
, "NdrFcShort(0x%x), /* stack offset = %u */\n",
1162 *stack_offset
, *stack_offset
);
1163 if (flags
& IsBasetype
)
1165 print_file( file
, indent
, "0x%02x, /* %s */\n", fc
, string_of_type(fc
) );
1166 print_file( file
, indent
, "0x0,\n" );
1169 print_file( file
, indent
, "NdrFcShort(0x%x), /* type offset = %u */\n",
1170 typestring_offset
, typestring_offset
);
1171 *stack_offset
+= max( stack_size
, pointer_size
);
1175 static unsigned int write_old_procformatstring_type(FILE *file
, int indent
, const var_t
*var
,
1176 int is_return
, int is_interpreted
)
1180 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
1181 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
1183 if (!is_in
&& !is_out
) is_in
= TRUE
;
1185 if (type_get_type(var
->type
) == TYPE_BASIC
||
1186 type_get_type(var
->type
) == TYPE_ENUM
)
1191 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
1193 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
1195 if (type_get_type(var
->type
) == TYPE_ENUM
)
1197 fc
= get_enum_fc(var
->type
);
1201 fc
= get_basic_fc_signed(var
->type
);
1203 if (fc
== RPC_FC_BIND_PRIMITIVE
)
1207 print_file(file
, indent
, "0x%02x, /* %s */\n",
1208 fc
, string_of_type(fc
));
1209 size
= 2; /* includes param type prefix */
1213 unsigned short offset
= var
->typestring_offset
;
1215 if (!is_interpreted
&& is_array(var
->type
) &&
1216 type_array_is_decl_as_ptr(var
->type
) &&
1217 var
->type
->details
.array
.ptr_tfsoff
)
1218 offset
= var
->type
->typestring_offset
;
1221 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
1222 else if (is_in
&& is_out
)
1223 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
1225 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
1227 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
1229 size
= get_stack_size( var
, NULL
);
1230 print_file(file
, indent
, "0x%02x,\n", size
/ pointer_size
);
1231 print_file(file
, indent
, "NdrFcShort(0x%x), /* type offset = %u */\n", offset
, offset
);
1232 size
= 4; /* includes param type prefix */
1237 int is_interpreted_func( const type_t
*iface
, const var_t
*func
)
1241 const var_list_t
*args
= type_get_function_args( func
->type
);
1242 const type_t
*ret_type
= type_function_get_rettype( func
->type
);
1244 if (type_get_type( ret_type
) == TYPE_BASIC
)
1246 switch (type_basic_get_type( ret_type
))
1248 case TYPE_BASIC_INT64
:
1249 case TYPE_BASIC_HYPER
:
1250 /* return value must fit in a long_ptr */
1251 if (pointer_size
< 8) return 0;
1253 case TYPE_BASIC_FLOAT
:
1254 case TYPE_BASIC_DOUBLE
:
1255 /* floating point values can't be returned */
1261 if (get_stub_mode() != MODE_Oif
&& args
)
1263 LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
1264 switch (type_get_type( var
->type
))
1267 switch (type_basic_get_type( var
->type
))
1269 /* floating point arguments are not supported in Oi mode */
1270 case TYPE_BASIC_FLOAT
: return 0;
1271 case TYPE_BASIC_DOUBLE
: return 0;
1275 /* unions passed by value are not supported in Oi mode */
1276 case TYPE_UNION
: return 0;
1277 case TYPE_ENCAPSULATED_UNION
: return 0;
1282 if ((str
= get_attrp( func
->attrs
, ATTR_OPTIMIZE
))) return !strcmp( str
, "i" );
1283 if ((str
= get_attrp( iface
->attrs
, ATTR_OPTIMIZE
))) return !strcmp( str
, "i" );
1284 return (get_stub_mode() != MODE_Os
);
1287 static void write_proc_func_header( FILE *file
, int indent
, const type_t
*iface
,
1288 const var_t
*func
, unsigned int *offset
,
1289 unsigned short num_proc
)
1292 var_list_t
*args
= type_get_function_args( func
->type
);
1293 unsigned char explicit_fc
, implicit_fc
;
1294 unsigned char handle_flags
;
1295 const var_t
*handle_var
= get_func_handle_var( iface
, func
, &explicit_fc
, &implicit_fc
);
1296 unsigned char oi_flags
= RPC_FC_PROC_OIF_RPCFLAGS
| RPC_FC_PROC_OIF_NEWINIT
;
1297 unsigned int rpc_flags
= get_rpc_flags( func
->attrs
);
1298 unsigned int nb_args
= 0;
1299 unsigned int stack_size
= 0;
1300 unsigned short param_num
= 0;
1301 unsigned short handle_stack_offset
= 0;
1302 unsigned short handle_param_num
= 0;
1304 if (is_full_pointer_function( func
)) oi_flags
|= RPC_FC_PROC_OIF_FULLPTR
;
1305 if (is_object( iface
))
1307 oi_flags
|= RPC_FC_PROC_OIF_OBJECT
;
1308 if (get_stub_mode() == MODE_Oif
) oi_flags
|= RPC_FC_PROC_OIF_OBJ_V2
;
1309 stack_size
+= pointer_size
;
1312 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, var_t
, entry
)
1314 if (var
== handle_var
)
1316 handle_stack_offset
= stack_size
;
1317 handle_param_num
= param_num
;
1319 stack_size
+= get_stack_size( var
, NULL
);
1323 if (!is_void( type_function_get_rettype( func
->type
)))
1325 stack_size
+= pointer_size
;
1329 print_file( file
, 0, "/* %u (procedure %s::%s) */\n", *offset
, iface
->name
, func
->name
);
1330 print_file( file
, indent
, "0x%02x,\t/* %s */\n", implicit_fc
,
1331 implicit_fc
? string_of_type(implicit_fc
) : "explicit handle" );
1332 print_file( file
, indent
, "0x%02x,\n", oi_flags
);
1333 print_file( file
, indent
, "NdrFcLong(0x%x),\n", rpc_flags
);
1334 print_file( file
, indent
, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc
, num_proc
);
1335 print_file( file
, indent
, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size
, stack_size
);
1340 switch (explicit_fc
)
1342 case RPC_FC_BIND_PRIMITIVE
:
1344 print_file( file
, indent
, "0x%02x,\t/* %s */\n", explicit_fc
, string_of_type(explicit_fc
) );
1345 print_file( file
, indent
, "0x%02x,\n", handle_flags
);
1346 print_file( file
, indent
, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1347 handle_stack_offset
, handle_stack_offset
);
1350 case RPC_FC_BIND_GENERIC
:
1351 handle_flags
= type_memsize( handle_var
->type
);
1352 print_file( file
, indent
, "0x%02x,\t/* %s */\n", explicit_fc
, string_of_type(explicit_fc
) );
1353 print_file( file
, indent
, "0x%02x,\n", handle_flags
);
1354 print_file( file
, indent
, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1355 handle_stack_offset
, handle_stack_offset
);
1356 print_file( file
, indent
, "0x%02x,\n", get_generic_handle_offset( handle_var
->type
) );
1357 print_file( file
, indent
, "0x%x,\t/* FC_PAD */\n", RPC_FC_PAD
);
1360 case RPC_FC_BIND_CONTEXT
:
1361 handle_flags
= get_contexthandle_flags( iface
, handle_var
->attrs
, handle_var
->type
);
1362 print_file( file
, indent
, "0x%02x,\t/* %s */\n", explicit_fc
, string_of_type(explicit_fc
) );
1363 print_file( file
, indent
, "0x%02x,\n", handle_flags
);
1364 print_file( file
, indent
, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
1365 handle_stack_offset
, handle_stack_offset
);
1366 print_file( file
, indent
, "0x%02x,\n", get_context_handle_offset( handle_var
->type
) );
1367 print_file( file
, indent
, "0x%02x,\t/* param %hu */\n", handle_param_num
, handle_param_num
);
1373 if (get_stub_mode() == MODE_Oif
)
1375 unsigned char oi2_flags
= get_func_oi2_flags( func
);
1376 unsigned char ext_flags
= 0;
1379 if (is_attr( func
->attrs
, ATTR_NOTIFY
)) ext_flags
|= 0x08; /* HasNotify */
1380 if (is_attr( func
->attrs
, ATTR_NOTIFYFLAG
)) ext_flags
|= 0x10; /* HasNotify2 */
1382 size
= get_function_buffer_size( func
, PASS_IN
);
1383 print_file( file
, indent
, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size
, size
);
1384 size
= get_function_buffer_size( func
, PASS_OUT
);
1385 print_file( file
, indent
, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size
, size
);
1386 print_file( file
, indent
, "0x%02x,\n", oi2_flags
);
1387 print_file( file
, indent
, "0x%02x,\t/* %u params */\n", nb_args
, nb_args
);
1388 print_file( file
, indent
, "0x%02x,\n", pointer_size
== 8 ? 10 : 8 );
1389 print_file( file
, indent
, "0x%02x,\n", ext_flags
);
1390 print_file( file
, indent
, "NdrFcShort(0x0),\n" ); /* server corr hint */
1391 print_file( file
, indent
, "NdrFcShort(0x0),\n" ); /* client corr hint */
1392 print_file( file
, indent
, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
1394 if (pointer_size
== 8)
1396 unsigned short pos
= 0, fpu_mask
= 0;
1398 if (is_object( iface
)) pos
+= 2;
1399 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, var_t
, entry
)
1401 if (type_get_type( var
->type
) == TYPE_BASIC
)
1403 switch (type_basic_get_type( var
->type
))
1405 case TYPE_BASIC_FLOAT
: fpu_mask
|= 1 << pos
; break;
1406 case TYPE_BASIC_DOUBLE
: fpu_mask
|= 2 << pos
; break;
1411 if (pos
>= 16) break;
1413 print_file( file
, indent
, "NdrFcShort(0x%x),\n", fpu_mask
); /* floating point mask */
1419 static void write_procformatstring_func( FILE *file
, int indent
, const type_t
*iface
,
1420 const var_t
*func
, unsigned int *offset
,
1421 unsigned short num_proc
)
1423 unsigned int stack_offset
= is_object( iface
) ? pointer_size
: 0;
1424 int is_interpreted
= is_interpreted_func( iface
, func
);
1425 int is_new_style
= is_interpreted
&& (get_stub_mode() == MODE_Oif
);
1426 var_t
*retval
= type_function_get_retval( func
->type
);
1428 if (is_interpreted
) write_proc_func_header( file
, indent
, iface
, func
, offset
, num_proc
);
1430 /* emit argument data */
1431 if (type_get_function_args(func
->type
))
1434 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
1436 print_file( file
, 0, "/* %u (parameter %s) */\n", *offset
, var
->name
);
1438 *offset
+= write_new_procformatstring_type(file
, indent
, var
, FALSE
, &stack_offset
);
1440 *offset
+= write_old_procformatstring_type(file
, indent
, var
, FALSE
, is_interpreted
);
1444 /* emit return value data */
1445 if (is_void(retval
->type
))
1449 print_file(file
, 0, "/* %u (void) */\n", *offset
);
1450 print_file(file
, indent
, "0x5b, /* FC_END */\n");
1451 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
1457 print_file( file
, 0, "/* %u (return value) */\n", *offset
);
1459 *offset
+= write_new_procformatstring_type(file
, indent
, retval
, TRUE
, &stack_offset
);
1461 *offset
+= write_old_procformatstring_type(file
, indent
, retval
, TRUE
, is_interpreted
);
1465 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
,
1466 type_pred_t pred
, unsigned int *offset
)
1468 const statement_t
*stmt
;
1469 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
1471 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
1473 const statement_t
*stmt_func
;
1474 const type_t
*iface
= stmt
->u
.type
;
1475 const type_t
*parent
= type_iface_get_inherit( iface
);
1476 int count
= parent
? count_methods( parent
) : 0;
1478 if (!pred(iface
)) continue;
1479 STATEMENTS_FOR_EACH_FUNC(stmt_func
, type_iface_get_stmts(iface
))
1481 var_t
*func
= stmt_func
->u
.var
;
1482 if (is_local(func
->attrs
)) continue;
1483 write_procformatstring_func( file
, indent
, iface
, func
, offset
, count
++ );
1489 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
1492 unsigned int offset
= 0;
1494 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
1495 print_file(file
, indent
, "{\n");
1497 print_file(file
, indent
, "0,\n");
1498 print_file(file
, indent
, "{\n");
1501 write_procformatstring_stmts(file
, indent
, stmts
, pred
, &offset
);
1503 print_file(file
, indent
, "0x0\n");
1505 print_file(file
, indent
, "}\n");
1507 print_file(file
, indent
, "};\n");
1508 print_file(file
, indent
, "\n");
1511 void write_procformatstring_offsets( FILE *file
, const type_t
*iface
)
1513 const statement_t
*stmt
;
1516 print_file( file
, indent
, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
1518 print_file( file
, indent
, "{\n" );
1520 STATEMENTS_FOR_EACH_FUNC( stmt
, type_iface_get_stmts(iface
) )
1522 var_t
*func
= stmt
->u
.var
;
1523 if (is_local( func
->attrs
)) continue;
1524 print_file( file
, indent
, "%u, /* %s */\n", func
->procstring_offset
, func
->name
);
1527 print_file( file
, indent
, "};\n\n" );
1530 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
1534 if (type_get_type(type
) == TYPE_BASIC
)
1535 fc
= get_basic_fc_signed(type
);
1536 else if (type_get_type(type
) == TYPE_ENUM
)
1537 fc
= get_enum_fc(type
);
1541 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1542 *typestring_offset
+= 1;
1546 /* write conformance / variance descriptor */
1547 static unsigned int write_conf_or_var_desc(FILE *file
, const type_t
*cont_type
,
1548 unsigned int baseoff
, const type_t
*type
,
1551 unsigned char operator_type
= 0;
1552 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
1553 const char *conftype_string
= "field";
1554 const expr_t
*subexpr
;
1555 const type_t
*iface
= NULL
;
1560 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
1566 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
1567 error("write_conf_or_var_desc: constant value %d is greater than "
1568 "the maximum constant size of %d\n", expr
->cval
,
1569 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
1571 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
1572 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
1573 print_file(file
, 2, "0x%x,\n", expr
->cval
>> 16);
1574 print_file(file
, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr
->cval
);
1579 if (!cont_type
) /* top-level conformance */
1581 conftype
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
1582 conftype_string
= "parameter";
1583 cont_type
= current_func
->type
;
1584 name
= current_func
->name
;
1585 iface
= current_iface
;
1589 name
= cont_type
->name
;
1590 if (is_ptr(type
) || (is_array(type
) && type_array_is_decl_as_ptr(type
)))
1592 conftype
= RPC_FC_POINTER_CONFORMANCE
;
1593 conftype_string
= "field pointer";
1598 switch (subexpr
->type
)
1601 subexpr
= subexpr
->ref
;
1602 operator_type
= RPC_FC_DEREFERENCE
;
1605 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
1607 subexpr
= subexpr
->ref
;
1608 operator_type
= RPC_FC_DIV_2
;
1612 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
1614 subexpr
= subexpr
->ref
;
1615 operator_type
= RPC_FC_MULT_2
;
1619 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
1621 subexpr
= subexpr
->ref
;
1622 operator_type
= RPC_FC_SUB_1
;
1626 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
1628 subexpr
= subexpr
->ref
;
1629 operator_type
= RPC_FC_ADD_1
;
1636 if (subexpr
->type
== EXPR_IDENTIFIER
)
1638 const type_t
*correlation_variable
= NULL
;
1639 unsigned char param_type
= 0;
1640 unsigned int offset
= 0;
1642 struct expr_loc expr_loc
;
1644 if (type_get_type(cont_type
) == TYPE_FUNCTION
)
1646 var_list_t
*args
= type_get_function_args( cont_type
);
1648 if (is_object( iface
)) offset
+= pointer_size
;
1649 if (args
) LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
1651 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
1654 correlation_variable
= var
->type
;
1657 offset
+= get_stack_size( var
, NULL
);
1662 var_list_t
*fields
= type_struct_get_fields( cont_type
);
1664 if (fields
) LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
1666 unsigned int size
= field_memsize( var
->type
, &offset
);
1667 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
1670 correlation_variable
= var
->type
;
1677 if (!correlation_variable
)
1678 error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr
->u
.sval
, name
);
1679 expr_loc
.attr
= NULL
;
1680 correlation_variable
= expr_resolve_type(&expr_loc
, cont_type
, expr
);
1684 if (type_get_type(correlation_variable
) == TYPE_BASIC
)
1686 switch (get_basic_fc(correlation_variable
))
1690 param_type
= RPC_FC_SMALL
;
1694 param_type
= RPC_FC_USMALL
;
1698 param_type
= RPC_FC_SHORT
;
1701 param_type
= RPC_FC_USHORT
;
1704 param_type
= RPC_FC_LONG
;
1707 param_type
= RPC_FC_ULONG
;
1710 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1711 get_basic_fc(correlation_variable
));
1714 else if (type_get_type(correlation_variable
) == TYPE_ENUM
)
1716 if (get_enum_fc(correlation_variable
) == RPC_FC_ENUM32
)
1717 param_type
= RPC_FC_LONG
;
1719 param_type
= RPC_FC_SHORT
;
1721 else if (type_get_type(correlation_variable
) == TYPE_POINTER
)
1723 if (pointer_size
== 8)
1724 param_type
= RPC_FC_HYPER
;
1726 param_type
= RPC_FC_LONG
;
1730 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1735 print_file(file
, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
1736 conftype
| param_type
, conftype_string
, subexpr
->u
.sval
, string_of_type(param_type
));
1737 print_file(file
, 2, "0x%x,\t/* %s */\n", operator_type
,
1738 operator_type
? string_of_type(operator_type
) : "no operators");
1739 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1740 (unsigned short)offset
, offset
);
1742 else if (!iface
|| is_interpreted_func( iface
, current_func
))
1744 unsigned int callback_offset
= 0;
1745 struct expr_eval_routine
*eval
;
1748 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
1750 if (eval
->cont_type
== cont_type
||
1751 (type_get_type( eval
->cont_type
) == type_get_type( cont_type
) &&
1752 eval
->iface
== iface
&&
1753 eval
->name
&& name
&& !strcmp(eval
->name
, name
) &&
1754 !compare_expr(eval
->expr
, expr
)))
1764 eval
= xmalloc (sizeof(*eval
));
1765 eval
->iface
= iface
;
1766 eval
->cont_type
= cont_type
;
1767 eval
->name
= xstrdup( name
);
1768 eval
->baseoff
= baseoff
;
1770 list_add_tail (&expr_eval_routines
, &eval
->entry
);
1773 if (callback_offset
> USHRT_MAX
)
1774 error("Maximum number of callback routines reached\n");
1776 print_file(file
, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype
, conftype_string
, name
);
1777 print_file(file
, 2, "0x%x,\t/* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
1778 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset
, callback_offset
);
1780 else /* output a dummy corr desc that isn't used */
1782 print_file(file
, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype
, name
);
1783 print_file(file
, 2, "0x0,\n" );
1784 print_file(file
, 2, "NdrFcShort(0x0),\n" );
1789 /* return size and start offset of a data field based on current offset */
1790 static unsigned int field_memsize(const type_t
*type
, unsigned int *offset
)
1792 unsigned int align
= 0;
1793 unsigned int size
= type_memsize_and_alignment( type
, &align
);
1795 *offset
= ROUND_SIZE( *offset
, align
);
1799 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
)
1801 unsigned int size
= 0;
1802 unsigned int max_align
;
1805 if (!fields
) return 0;
1806 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1808 unsigned int falign
= 0;
1809 unsigned int fsize
= type_memsize_and_alignment(v
->type
, &falign
);
1810 if (*align
< falign
) *align
= falign
;
1811 falign
= clamp_align(falign
);
1812 size
= ROUND_SIZE(size
, falign
);
1816 max_align
= clamp_align(*align
);
1817 size
= ROUND_SIZE(size
, max_align
);
1822 static unsigned int union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
1824 unsigned int size
, maxs
= 0;
1825 unsigned int align
= *pmaxa
;
1828 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1830 /* we could have an empty default field with NULL type */
1833 size
= type_memsize_and_alignment(v
->type
, &align
);
1834 if (maxs
< size
) maxs
= size
;
1835 if (*pmaxa
< align
) *pmaxa
= align
;
1842 static unsigned int type_memsize_and_alignment(const type_t
*t
, unsigned int *align
)
1844 unsigned int size
= 0;
1846 switch (type_get_type(t
))
1849 switch (get_basic_fc(t
))
1856 if (size
> *align
) *align
= size
;
1862 if (size
> *align
) *align
= size
;
1866 case RPC_FC_ERROR_STATUS_T
:
1869 if (size
> *align
) *align
= size
;
1874 if (size
> *align
) *align
= size
;
1876 case RPC_FC_INT3264
:
1877 case RPC_FC_UINT3264
:
1878 case RPC_FC_BIND_PRIMITIVE
:
1879 assert( pointer_size
);
1880 size
= pointer_size
;
1881 if (size
> *align
) *align
= size
;
1884 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t
));
1889 switch (get_enum_fc(t
))
1894 if (size
> *align
) *align
= size
;
1897 error("type_memsize: Unknown enum type\n");
1902 size
= fields_memsize(type_struct_get_fields(t
), align
);
1904 case TYPE_ENCAPSULATED_UNION
:
1905 size
= fields_memsize(type_encapsulated_union_get_fields(t
), align
);
1908 size
= union_memsize(type_union_get_cases(t
), align
);
1911 assert( pointer_size
);
1912 size
= pointer_size
;
1913 if (size
> *align
) *align
= size
;
1916 if (!type_array_is_decl_as_ptr(t
))
1918 if (is_conformant_array(t
))
1920 type_memsize_and_alignment(type_array_get_element(t
), align
);
1924 size
= type_array_get_dim(t
) *
1925 type_memsize_and_alignment(type_array_get_element(t
), align
);
1927 else /* declared as a pointer */
1929 assert( pointer_size
);
1930 size
= pointer_size
;
1931 if (size
> *align
) *align
= size
;
1934 case TYPE_INTERFACE
:
1941 /* these types should not be encountered here due to language
1942 * restrictions (interface, void, coclass, module), logical
1943 * restrictions (alias - due to type_get_type call above) or
1944 * checking restrictions (function, bitfield). */
1951 unsigned int type_memsize(const type_t
*t
)
1953 unsigned int align
= 0;
1954 return type_memsize_and_alignment( t
, &align
);
1957 static unsigned int type_buffer_alignment(const type_t
*t
)
1959 const var_list_t
*fields
;
1961 unsigned int max
= 0, align
;
1963 switch (type_get_type(t
))
1966 switch (get_basic_fc(t
))
1979 case RPC_FC_ERROR_STATUS_T
:
1981 case RPC_FC_INT3264
:
1982 case RPC_FC_UINT3264
:
1988 error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t
));
1992 switch (get_enum_fc(t
))
1999 error("type_buffer_alignment: Unknown enum type\n");
2003 if (!(fields
= type_struct_get_fields(t
))) break;
2004 LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
2006 if (!var
->type
) continue;
2007 align
= type_buffer_alignment( var
->type
);
2008 if (max
< align
) max
= align
;
2011 case TYPE_ENCAPSULATED_UNION
:
2012 if (!(fields
= type_encapsulated_union_get_fields(t
))) break;
2013 LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
2015 if (!var
->type
) continue;
2016 align
= type_buffer_alignment( var
->type
);
2017 if (max
< align
) max
= align
;
2021 if (!(fields
= type_union_get_cases(t
))) break;
2022 LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
2024 if (!var
->type
) continue;
2025 align
= type_buffer_alignment( var
->type
);
2026 if (max
< align
) max
= align
;
2030 if (!type_array_is_decl_as_ptr(t
))
2031 return type_buffer_alignment( type_array_get_element(t
) );
2032 /* else fall through */
2035 case TYPE_INTERFACE
:
2042 /* these types should not be encountered here due to language
2043 * restrictions (interface, void, coclass, module), logical
2044 * restrictions (alias - due to type_get_type call above) or
2045 * checking restrictions (function, bitfield). */
2051 int is_full_pointer_function(const var_t
*func
)
2054 if (type_has_full_pointer(type_function_get_rettype(func
->type
), func
->attrs
, TRUE
))
2056 if (!type_get_function_args(func
->type
))
2058 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2059 if (type_has_full_pointer( var
->type
, var
->attrs
, TRUE
))
2064 void write_full_pointer_init(FILE *file
, int indent
, const var_t
*func
, int is_server
)
2066 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
2067 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
2068 fprintf(file
, "\n");
2071 void write_full_pointer_free(FILE *file
, int indent
, const var_t
*func
)
2073 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
2074 fprintf(file
, "\n");
2077 static unsigned int write_nonsimple_pointer(FILE *file
, const attr_list_t
*attrs
,
2079 enum type_context context
,
2080 unsigned int offset
,
2081 unsigned int *typeformat_offset
)
2083 unsigned int start_offset
= *typeformat_offset
;
2084 short reloff
= offset
- (*typeformat_offset
+ 2);
2085 int in_attr
, out_attr
;
2087 unsigned char flags
= 0;
2089 pointer_type
= get_pointer_fc_context(type
, attrs
, context
);
2091 in_attr
= is_attr(attrs
, ATTR_IN
);
2092 out_attr
= is_attr(attrs
, ATTR_OUT
);
2093 if (!in_attr
&& !out_attr
) in_attr
= 1;
2095 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
2096 flags
|= RPC_FC_P_ONSTACK
;
2100 type_t
*ref
= type_pointer_get_ref(type
);
2101 if(is_declptr(ref
) && !is_user_type(ref
))
2102 flags
|= RPC_FC_P_DEREF
;
2105 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
2108 string_of_type(pointer_type
));
2111 if (flags
& RPC_FC_P_ONSTACK
)
2112 fprintf(file
, " [allocated_on_stack]");
2113 if (flags
& RPC_FC_P_DEREF
)
2114 fprintf(file
, " [pointer_deref]");
2115 fprintf(file
, " */\n");
2118 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, offset
);
2119 *typeformat_offset
+= 4;
2121 return start_offset
;
2124 static unsigned int write_simple_pointer(FILE *file
, const attr_list_t
*attrs
,
2125 const type_t
*type
, enum type_context context
)
2128 unsigned char pointer_fc
;
2130 int in_attr
= is_attr(attrs
, ATTR_IN
);
2131 int out_attr
= is_attr(attrs
, ATTR_OUT
);
2132 unsigned char flags
= RPC_FC_P_SIMPLEPOINTER
;
2134 /* for historical reasons, write_simple_pointer also handled string types,
2135 * but no longer does. catch bad uses of the function with this check */
2136 if (is_string_type(attrs
, type
))
2137 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
2139 pointer_fc
= get_pointer_fc_context(type
, attrs
, context
);
2141 ref
= type_pointer_get_ref(type
);
2142 if (type_get_type(ref
) == TYPE_ENUM
)
2143 fc
= get_enum_fc(ref
);
2145 fc
= get_basic_fc(ref
);
2147 if (out_attr
&& !in_attr
)
2148 flags
|= RPC_FC_P_ONSTACK
;
2150 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
2151 pointer_fc
, flags
, string_of_type(pointer_fc
),
2152 flags
& RPC_FC_P_ONSTACK
? "[allocated_on_stack] " : "");
2153 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
2154 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
2158 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
2160 print_file(file
, 0, "/* %u (", tfsoff
);
2161 write_type_decl(file
, t
, NULL
);
2162 print_file(file
, 0, ") */\n");
2165 static unsigned int write_pointer_tfs(FILE *file
, const attr_list_t
*attrs
,
2166 type_t
*type
, unsigned int ref_offset
,
2167 enum type_context context
,
2168 unsigned int *typestring_offset
)
2170 unsigned int offset
= *typestring_offset
;
2171 type_t
*ref
= type_pointer_get_ref(type
);
2173 print_start_tfs_comment(file
, type
, offset
);
2174 update_tfsoff(type
, offset
, file
);
2176 switch (typegen_detect_type(ref
, attrs
, TDT_ALL_TYPES
))
2180 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
, context
);
2184 write_nonsimple_pointer(file
, attrs
, type
, context
, ref_offset
, typestring_offset
);
2191 static int processed(const type_t
*type
)
2193 return type
->typestring_offset
&& !type
->tfswrite
;
2196 static int user_type_has_variable_size(const type_t
*t
)
2200 else if (type_get_type(t
) == TYPE_STRUCT
)
2202 switch (get_struct_fc(t
))
2204 case RPC_FC_PSTRUCT
:
2205 case RPC_FC_CSTRUCT
:
2206 case RPC_FC_CPSTRUCT
:
2207 case RPC_FC_CVSTRUCT
:
2211 /* Note: Since this only applies to user types, we can't have a conformant
2212 array here, and strings should get filed under pointer in this case. */
2216 static unsigned int write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2218 unsigned int start
, absoff
, flags
;
2219 const char *name
= NULL
;
2220 type_t
*utype
= get_user_type(type
, &name
);
2221 unsigned int usize
= type_memsize(utype
);
2222 unsigned int ualign
= type_buffer_alignment(utype
);
2223 unsigned int size
= type_memsize(type
);
2224 unsigned short funoff
= user_type_offset(name
);
2227 if (processed(type
)) return type
->typestring_offset
;
2231 if(user_type_has_variable_size(utype
)) usize
= 0;
2233 if (type_get_type(utype
) == TYPE_BASIC
||
2234 type_get_type(utype
) == TYPE_ENUM
)
2238 if (type_get_type(utype
) == TYPE_ENUM
)
2239 fc
= get_enum_fc(utype
);
2241 fc
= get_basic_fc(utype
);
2244 print_start_tfs_comment(file
, utype
, absoff
);
2245 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2246 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
2251 if (!processed(utype
))
2252 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
2253 absoff
= utype
->typestring_offset
;
2256 if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_RP
)
2258 else if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_UP
)
2264 update_tfsoff(type
, start
, file
);
2265 print_start_tfs_comment(file
, type
, start
);
2266 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
2267 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
2268 flags
| (ualign
- 1), ualign
- 1, flags
);
2269 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
2270 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size
, size
);
2271 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize
, usize
);
2273 reloff
= absoff
- *tfsoff
;
2274 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, absoff
);
2279 static void write_member_type(FILE *file
, const type_t
*cont
,
2280 int cont_is_complex
, const attr_list_t
*attrs
,
2281 const type_t
*type
, unsigned int *corroff
,
2282 unsigned int *tfsoff
)
2284 if (is_embedded_complex(type
) && !is_conformant_array(type
))
2286 unsigned int absoff
;
2289 if (type_get_type(type
) == TYPE_UNION
&& is_attr(attrs
, ATTR_SWITCHIS
))
2296 absoff
= type
->typestring_offset
;
2298 reloff
= absoff
- (*tfsoff
+ 2);
2300 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
2301 /* padding is represented using FC_STRUCTPAD* types, so presumably
2302 * this is left over in the format for historical purposes in MIDL
2304 print_file(file
, 2, "0x0,\n");
2305 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2306 reloff
, reloff
, absoff
);
2309 else if (is_ptr(type
) || is_conformant_array(type
))
2311 unsigned char fc
= cont_is_complex
? RPC_FC_POINTER
: RPC_FC_LONG
;
2312 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2315 else if (!write_base_type(file
, type
, tfsoff
))
2316 error("Unsupported member type %d\n", type_get_type(type
));
2319 static void write_array_element_type(FILE *file
, const type_t
*type
,
2320 int cont_is_complex
, unsigned int *tfsoff
)
2322 type_t
*elem
= type_array_get_element(type
);
2324 if (!is_embedded_complex(elem
) && is_ptr(elem
))
2326 type_t
*ref
= type_pointer_get_ref(elem
);
2330 write_nonsimple_pointer(file
, NULL
, elem
, TYPE_CONTEXT_CONTAINER
,
2331 ref
->typestring_offset
, tfsoff
);
2334 if (cont_is_complex
&& is_string_type(NULL
, elem
))
2336 write_string_tfs(file
, NULL
, elem
, TYPE_CONTEXT_CONTAINER
, NULL
, tfsoff
);
2339 if (!is_string_type(NULL
, elem
) &&
2340 (type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
))
2342 *tfsoff
+= write_simple_pointer(file
, NULL
, elem
, TYPE_CONTEXT_CONTAINER
);
2346 write_member_type(file
, type
, cont_is_complex
, NULL
, elem
, NULL
, tfsoff
);
2349 static void write_end(FILE *file
, unsigned int *tfsoff
)
2351 if (*tfsoff
% 2 == 0)
2353 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
2356 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
2360 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2362 unsigned int offset
= 0;
2363 var_list_t
*fs
= type_struct_get_fields(type
);
2366 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
2368 type_t
*ft
= f
->type
;
2369 unsigned int size
= field_memsize( ft
, &offset
);
2370 if (type_get_type(ft
) == TYPE_UNION
&& is_attr(f
->attrs
, ATTR_SWITCHIS
))
2373 unsigned int absoff
= ft
->typestring_offset
;
2374 if (is_attr(ft
->attrs
, ATTR_SWITCHTYPE
))
2375 absoff
+= 8; /* we already have a corr descr, skip it */
2376 reloff
= absoff
- (*tfsoff
+ 6);
2377 print_file(file
, 0, "/* %d */\n", *tfsoff
);
2378 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
2379 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
2380 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
2381 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
2382 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2383 (unsigned short)reloff
, reloff
, absoff
);
2390 static int write_pointer_description_offsets(
2391 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2392 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
2393 unsigned int *typestring_offset
)
2397 if ((is_ptr(type
) && type_get_type(type_pointer_get_ref(type
)) != TYPE_INTERFACE
) ||
2398 (is_array(type
) && type_array_is_decl_as_ptr(type
)))
2400 if (offset_in_memory
&& offset_in_buffer
)
2402 unsigned int memsize
;
2406 * note that MSDN states that for pointer layouts in structures,
2407 * this is a negative offset from the end of the structure, but
2408 * this statement is incorrect. all offsets are positive */
2409 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory
, *offset_in_memory
);
2410 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer
, *offset_in_buffer
);
2412 memsize
= type_memsize(type
);
2413 *offset_in_memory
+= memsize
;
2414 /* increment these separately as in the case of conformant (varying)
2415 * structures these start at different values */
2416 *offset_in_buffer
+= memsize
;
2418 *typestring_offset
+= 4;
2422 type_t
*ref
= type_pointer_get_ref(type
);
2424 if (is_string_type(attrs
, type
))
2425 write_string_tfs(file
, attrs
, type
, TYPE_CONTEXT_CONTAINER
, NULL
, typestring_offset
);
2426 else if (processed(ref
))
2427 write_nonsimple_pointer(file
, attrs
, type
, TYPE_CONTEXT_CONTAINER
,
2428 ref
->typestring_offset
, typestring_offset
);
2429 else if (type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
)
2430 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
, TYPE_CONTEXT_CONTAINER
);
2432 error("write_pointer_description_offsets: type format string unknown\n");
2436 unsigned int offset
= type
->typestring_offset
;
2437 /* skip over the pointer that is written for strings, since a
2438 * pointer has to be written in-place here */
2439 if (is_string_type(attrs
, type
))
2441 write_nonsimple_pointer(file
, attrs
, type
, TYPE_CONTEXT_CONTAINER
, offset
, typestring_offset
);
2449 return write_pointer_description_offsets(
2450 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
2451 offset_in_buffer
, typestring_offset
);
2453 else if (is_non_complex_struct(type
))
2455 /* otherwise search for interesting fields to parse */
2457 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
2459 if (offset_in_memory
&& offset_in_buffer
)
2461 unsigned int padding
;
2462 unsigned int align
= 0;
2463 type_memsize_and_alignment(v
->type
, &align
);
2464 padding
= ROUNDING(*offset_in_memory
, align
);
2465 *offset_in_memory
+= padding
;
2466 *offset_in_buffer
+= padding
;
2468 written
+= write_pointer_description_offsets(
2469 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
2475 if (offset_in_memory
&& offset_in_buffer
)
2477 unsigned int memsize
= type_memsize(type
);
2478 *offset_in_memory
+= memsize
;
2479 /* increment these separately as in the case of conformant (varying)
2480 * structures these start at different values */
2481 *offset_in_buffer
+= memsize
;
2488 static int write_no_repeat_pointer_descriptions(
2489 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2490 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
2491 unsigned int *typestring_offset
)
2496 (is_conformant_array(type
) && type_array_is_decl_as_ptr(type
)))
2498 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
2499 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
2500 *typestring_offset
+= 2;
2502 return write_pointer_description_offsets(file
, attrs
, type
,
2503 offset_in_memory
, offset_in_buffer
, typestring_offset
);
2506 if (is_non_complex_struct(type
))
2509 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
2511 if (offset_in_memory
&& offset_in_buffer
)
2513 unsigned int padding
;
2514 unsigned int align
= 0;
2515 type_memsize_and_alignment(v
->type
, &align
);
2516 padding
= ROUNDING(*offset_in_memory
, align
);
2517 *offset_in_memory
+= padding
;
2518 *offset_in_buffer
+= padding
;
2520 written
+= write_no_repeat_pointer_descriptions(
2521 file
, v
->attrs
, v
->type
,
2522 offset_in_memory
, offset_in_buffer
, typestring_offset
);
2527 unsigned int memsize
= type_memsize(type
);
2528 *offset_in_memory
+= memsize
;
2529 /* increment these separately as in the case of conformant (varying)
2530 * structures these start at different values */
2531 *offset_in_buffer
+= memsize
;
2537 /* Note: if file is NULL return value is number of pointers to write, else
2538 * it is the number of type format characters written */
2539 static int write_fixed_array_pointer_descriptions(
2540 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2541 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
2542 unsigned int *typestring_offset
)
2544 int pointer_count
= 0;
2546 if (type_get_type(type
) == TYPE_ARRAY
&&
2547 !type_array_has_conformance(type
) && !type_array_has_variance(type
))
2549 unsigned int temp
= 0;
2550 /* unfortunately, this needs to be done in two passes to avoid
2551 * writing out redundant FC_FIXED_REPEAT descriptions */
2552 pointer_count
= write_pointer_description_offsets(
2553 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
2554 if (pointer_count
> 0)
2556 unsigned int increment_size
;
2557 unsigned int offset_of_array_pointer_mem
= 0;
2558 unsigned int offset_of_array_pointer_buf
= 0;
2560 increment_size
= type_memsize(type_array_get_element(type
));
2562 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
2563 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
2564 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type
), type_array_get_dim(type
));
2565 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size
, increment_size
);
2566 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory
, *offset_in_memory
);
2567 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count
, pointer_count
);
2568 *typestring_offset
+= 10;
2570 pointer_count
= write_pointer_description_offsets(
2571 file
, attrs
, type
, &offset_of_array_pointer_mem
,
2572 &offset_of_array_pointer_buf
, typestring_offset
);
2575 else if (type_get_type(type
) == TYPE_STRUCT
)
2578 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
2580 if (offset_in_memory
&& offset_in_buffer
)
2582 unsigned int padding
;
2583 unsigned int align
= 0;
2584 type_memsize_and_alignment(v
->type
, &align
);
2585 padding
= ROUNDING(*offset_in_memory
, align
);
2586 *offset_in_memory
+= padding
;
2587 *offset_in_buffer
+= padding
;
2589 pointer_count
+= write_fixed_array_pointer_descriptions(
2590 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
2596 if (offset_in_memory
&& offset_in_buffer
)
2598 unsigned int memsize
;
2599 memsize
= type_memsize(type
);
2600 *offset_in_memory
+= memsize
;
2601 /* increment these separately as in the case of conformant (varying)
2602 * structures these start at different values */
2603 *offset_in_buffer
+= memsize
;
2607 return pointer_count
;
2610 /* Note: if file is NULL return value is number of pointers to write, else
2611 * it is the number of type format characters written */
2612 static int write_conformant_array_pointer_descriptions(
2613 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2614 unsigned int offset_in_memory
, unsigned int *typestring_offset
)
2616 int pointer_count
= 0;
2618 if (is_conformant_array(type
) && !type_array_has_variance(type
))
2620 unsigned int temp
= 0;
2621 /* unfortunately, this needs to be done in two passes to avoid
2622 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2623 pointer_count
= write_pointer_description_offsets(
2624 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
2625 if (pointer_count
> 0)
2627 unsigned int increment_size
;
2628 unsigned int offset_of_array_pointer_mem
= offset_in_memory
;
2629 unsigned int offset_of_array_pointer_buf
= offset_in_memory
;
2631 increment_size
= type_memsize(type_array_get_element(type
));
2633 if (increment_size
> USHRT_MAX
)
2634 error("array size of %u bytes is too large\n", increment_size
);
2636 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
2637 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
2638 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size
, increment_size
);
2639 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory
, offset_in_memory
);
2640 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count
, pointer_count
);
2641 *typestring_offset
+= 8;
2643 pointer_count
= write_pointer_description_offsets(
2644 file
, attrs
, type_array_get_element(type
),
2645 &offset_of_array_pointer_mem
, &offset_of_array_pointer_buf
,
2650 return pointer_count
;
2653 /* Note: if file is NULL return value is number of pointers to write, else
2654 * it is the number of type format characters written */
2655 static int write_varying_array_pointer_descriptions(
2656 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2657 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
2658 unsigned int *typestring_offset
)
2660 int pointer_count
= 0;
2662 if (is_array(type
) && type_array_has_variance(type
))
2664 unsigned int temp
= 0;
2665 /* unfortunately, this needs to be done in two passes to avoid
2666 * writing out redundant FC_VARIABLE_REPEAT descriptions */
2667 pointer_count
= write_pointer_description_offsets(
2668 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
2669 if (pointer_count
> 0)
2671 unsigned int increment_size
;
2673 increment_size
= type_memsize(type_array_get_element(type
));
2675 if (increment_size
> USHRT_MAX
)
2676 error("array size of %u bytes is too large\n", increment_size
);
2678 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
2679 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
2680 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size
, increment_size
);
2681 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory
, *offset_in_memory
);
2682 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count
, pointer_count
);
2683 *typestring_offset
+= 8;
2685 pointer_count
= write_pointer_description_offsets(
2686 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
2687 offset_in_buffer
, typestring_offset
);
2690 else if (type_get_type(type
) == TYPE_STRUCT
)
2693 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
2695 if (offset_in_memory
&& offset_in_buffer
)
2697 unsigned int align
= 0, padding
;
2699 if (is_array(v
->type
) && type_array_has_variance(v
->type
))
2701 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
2702 /* skip over variance and offset in buffer */
2703 *offset_in_buffer
+= 8;
2706 type_memsize_and_alignment(v
->type
, &align
);
2707 padding
= ROUNDING(*offset_in_memory
, align
);
2708 *offset_in_memory
+= padding
;
2709 *offset_in_buffer
+= padding
;
2711 pointer_count
+= write_varying_array_pointer_descriptions(
2712 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
2718 if (offset_in_memory
&& offset_in_buffer
)
2720 unsigned int memsize
= type_memsize(type
);
2721 *offset_in_memory
+= memsize
;
2722 /* increment these separately as in the case of conformant (varying)
2723 * structures these start at different values */
2724 *offset_in_buffer
+= memsize
;
2728 return pointer_count
;
2731 static void write_pointer_description(FILE *file
, type_t
*type
,
2732 unsigned int *typestring_offset
)
2734 unsigned int offset_in_buffer
;
2735 unsigned int offset_in_memory
;
2737 /* pass 1: search for single instance of a pointer (i.e. don't descend
2739 if (!is_array(type
))
2741 offset_in_memory
= 0;
2742 offset_in_buffer
= 0;
2743 write_no_repeat_pointer_descriptions(
2745 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
2748 /* pass 2: search for pointers in fixed arrays */
2749 offset_in_memory
= 0;
2750 offset_in_buffer
= 0;
2751 write_fixed_array_pointer_descriptions(
2753 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
2755 /* pass 3: search for pointers in conformant only arrays (but don't descend
2756 * into conformant varying or varying arrays) */
2757 if (is_conformant_array(type
) &&
2758 (type_array_is_decl_as_ptr(type
) || !current_structure
))
2759 write_conformant_array_pointer_descriptions(
2760 file
, NULL
, type
, 0, typestring_offset
);
2761 else if (type_get_type(type
) == TYPE_STRUCT
&&
2762 get_struct_fc(type
) == RPC_FC_CPSTRUCT
)
2764 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
2765 write_conformant_array_pointer_descriptions( file
, NULL
, carray
,
2766 type_memsize(type
), typestring_offset
);
2769 /* pass 4: search for pointers in varying arrays */
2770 offset_in_memory
= 0;
2771 offset_in_buffer
= 0;
2772 write_varying_array_pointer_descriptions(
2774 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
2777 int is_declptr(const type_t
*t
)
2779 return is_ptr(t
) || (type_get_type(t
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(t
));
2782 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
2783 type_t
*type
, enum type_context context
,
2784 const char *name
, unsigned int *typestring_offset
)
2786 unsigned int start_offset
;
2787 unsigned char rtype
;
2789 int is_processed
= processed(type
);
2791 start_offset
= *typestring_offset
;
2793 if (is_declptr(type
))
2795 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
2796 int pointer_type
= get_pointer_fc_context(type
, attrs
, context
);
2798 pointer_type
= RPC_FC_RP
;
2799 print_start_tfs_comment(file
, type
, *typestring_offset
);
2800 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2801 pointer_type
, flag
, string_of_type(pointer_type
),
2802 flag
? " [simple_pointer]" : "");
2803 *typestring_offset
+= 2;
2806 print_file(file
, 2, "NdrFcShort(0x2),\n");
2807 *typestring_offset
+= 2;
2809 is_processed
= FALSE
;
2813 elem_type
= type_array_get_element(type
);
2815 elem_type
= type_pointer_get_ref(type
);
2817 if (type_get_type(elem_type
) != TYPE_BASIC
)
2819 error("write_string_tfs: Unimplemented for non-basic type %s\n", name
);
2820 return start_offset
;
2823 rtype
= get_basic_fc(elem_type
);
2824 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
2826 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
2827 return start_offset
;
2830 if (type_get_type(type
) == TYPE_ARRAY
&& !type_array_has_conformance(type
))
2832 unsigned int dim
= type_array_get_dim(type
);
2834 if (is_processed
) return start_offset
;
2836 /* FIXME: multi-dimensional array */
2838 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2839 name
, 0xffffu
, dim
- 0xffffu
);
2841 if (rtype
== RPC_FC_WCHAR
)
2842 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
2844 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
2845 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2846 *typestring_offset
+= 2;
2848 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim
, dim
);
2849 *typestring_offset
+= 2;
2851 update_tfsoff(type
, start_offset
, file
);
2852 return start_offset
;
2854 else if (is_conformant_array(type
))
2856 if (rtype
== RPC_FC_WCHAR
)
2857 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2859 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2860 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
2861 *typestring_offset
+= 2;
2863 *typestring_offset
+= write_conf_or_var_desc(
2864 file
, current_structure
,
2865 (!type_array_is_decl_as_ptr(type
) && current_structure
2866 ? type_memsize(current_structure
)
2868 type
, type_array_get_conformance(type
));
2870 update_tfsoff(type
, start_offset
, file
);
2871 return start_offset
;
2875 if (is_processed
) return start_offset
;
2877 if (rtype
== RPC_FC_WCHAR
)
2878 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2880 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2881 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2882 *typestring_offset
+= 2;
2884 update_tfsoff(type
, start_offset
, file
);
2885 return start_offset
;
2889 static unsigned int write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2890 const char *name
, unsigned int *typestring_offset
)
2892 const expr_t
*length_is
= type_array_get_variance(type
);
2893 const expr_t
*size_is
= type_array_get_conformance(type
);
2896 unsigned int start_offset
;
2898 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
2899 unsigned int baseoff
2900 = !type_array_is_decl_as_ptr(type
) && current_structure
2901 ? type_memsize(current_structure
)
2905 pointer_type
= RPC_FC_RP
;
2907 write_embedded_types(file
, attrs
, type_array_get_element(type
), name
, FALSE
, typestring_offset
);
2909 size
= type_memsize(is_conformant_array(type
) ? type_array_get_element(type
) : type
);
2910 align
= type_buffer_alignment(is_conformant_array(type
) ? type_array_get_element(type
) : type
);
2911 fc
= get_array_fc(type
);
2913 start_offset
= *typestring_offset
;
2914 update_tfsoff(type
, start_offset
, file
);
2915 print_start_tfs_comment(file
, type
, start_offset
);
2916 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
2917 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2918 *typestring_offset
+= 2;
2921 if (fc
!= RPC_FC_BOGUS_ARRAY
)
2923 if (fc
== RPC_FC_LGFARRAY
|| fc
== RPC_FC_LGVARRAY
)
2925 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", size
, size
);
2926 *typestring_offset
+= 4;
2930 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size
, size
);
2931 *typestring_offset
+= 2;
2934 if (is_conformant_array(type
))
2936 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2939 if (fc
== RPC_FC_SMVARRAY
|| fc
== RPC_FC_LGVARRAY
)
2941 unsigned int elsize
= type_memsize(type_array_get_element(type
));
2942 unsigned int dim
= type_array_get_dim(type
);
2944 if (fc
== RPC_FC_LGVARRAY
)
2946 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim
, dim
);
2947 *typestring_offset
+= 4;
2951 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim
, dim
);
2952 *typestring_offset
+= 2;
2955 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize
, elsize
);
2956 *typestring_offset
+= 2;
2961 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2964 if (type_has_pointers(type_array_get_element(type
)) &&
2965 (type_array_is_decl_as_ptr(type
) || !current_structure
))
2967 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2968 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2969 *typestring_offset
+= 2;
2970 write_pointer_description(file
, type
, typestring_offset
);
2971 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2972 *typestring_offset
+= 1;
2975 write_array_element_type(file
, type
, FALSE
, typestring_offset
);
2976 write_end(file
, typestring_offset
);
2980 unsigned int dim
= size_is
? 0 : type_array_get_dim(type
);
2981 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim
, dim
);
2982 *typestring_offset
+= 2;
2984 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2987 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2990 write_array_element_type(file
, type
, TRUE
, typestring_offset
);
2991 write_end(file
, typestring_offset
);
2994 return start_offset
;
2997 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
2999 const var_list_t
*fields
= type_struct_get_fields(type
);
3000 const var_t
*last_field
;
3003 if (!fields
|| list_empty(fields
))
3006 last_field
= LIST_ENTRY( list_tail(fields
), const var_t
, entry
);
3007 ft
= last_field
->type
;
3009 if (is_conformant_array(ft
) && !type_array_is_decl_as_ptr(ft
))
3012 if (type_get_type(ft
) == TYPE_STRUCT
)
3013 return find_array_or_string_in_struct(ft
);
3018 static void write_struct_members(FILE *file
, const type_t
*type
,
3019 int is_complex
, unsigned int *corroff
,
3020 unsigned int *typestring_offset
)
3023 unsigned short offset
= 0;
3024 unsigned int salign
= 1;
3026 var_list_t
*fields
= type_struct_get_fields(type
);
3028 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
3030 type_t
*ft
= field
->type
;
3031 unsigned int align
= 0;
3032 unsigned int size
= type_memsize_and_alignment(ft
, &align
);
3033 align
= clamp_align(align
);
3034 if (salign
< align
) salign
= align
;
3036 if (!is_conformant_array(ft
) || type_array_is_decl_as_ptr(ft
))
3038 if ((align
- 1) & offset
)
3040 unsigned char fc
= 0;
3044 fc
= RPC_FC_ALIGNM2
;
3047 fc
= RPC_FC_ALIGNM4
;
3050 fc
= RPC_FC_ALIGNM8
;
3053 error("write_struct_members: cannot align type %d\n", type_get_type(ft
));
3055 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
3056 offset
= ROUND_SIZE(offset
, align
);
3057 *typestring_offset
+= 1;
3059 write_member_type(file
, type
, is_complex
, field
->attrs
, field
->type
, corroff
,
3065 padding
= ROUNDING(offset
, salign
);
3068 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
3069 RPC_FC_STRUCTPAD1
+ padding
- 1,
3071 *typestring_offset
+= 1;
3074 write_end(file
, typestring_offset
);
3077 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
,
3078 const char *name
, unsigned int *tfsoff
)
3080 const type_t
*save_current_structure
= current_structure
;
3081 unsigned int total_size
;
3083 unsigned int start_offset
;
3085 unsigned int corroff
;
3087 unsigned char fc
= get_struct_fc(type
);
3088 var_list_t
*fields
= type_struct_get_fields(type
);
3090 if (processed(type
)) return type
->typestring_offset
;
3093 current_structure
= type
;
3095 total_size
= type_memsize(type
);
3096 align
= type_buffer_alignment(type
);
3097 if (total_size
> USHRT_MAX
)
3098 error("structure size for %s exceeds %d bytes by %d bytes\n",
3099 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
3101 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
3102 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, FALSE
, tfsoff
);
3104 array
= find_array_or_string_in_struct(type
);
3105 if (array
&& !processed(array
->type
))
3107 if(is_string_type(array
->attrs
, array
->type
))
3108 write_string_tfs(file
, array
->attrs
, array
->type
, TYPE_CONTEXT_CONTAINER
, array
->name
, tfsoff
);
3110 write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
3114 write_descriptors(file
, type
, tfsoff
);
3116 start_offset
= *tfsoff
;
3117 update_tfsoff(type
, start_offset
, file
);
3118 print_start_tfs_comment(file
, type
, start_offset
);
3119 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
3120 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
3121 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size
, total_size
);
3126 unsigned int absoff
= array
->type
->typestring_offset
;
3127 short reloff
= absoff
- *tfsoff
;
3128 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3129 reloff
, reloff
, absoff
);
3132 else if (fc
== RPC_FC_BOGUS_STRUCT
)
3134 print_file(file
, 2, "NdrFcShort(0x0),\n");
3138 if (fc
== RPC_FC_BOGUS_STRUCT
)
3140 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
3141 nothing is written to file yet. On the actual writing pass,
3142 this will have been updated. */
3143 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
3144 int reloff
= absoff
- *tfsoff
;
3145 assert( reloff
>= 0 );
3146 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
3147 (unsigned short)reloff
, reloff
, absoff
);
3150 else if ((fc
== RPC_FC_PSTRUCT
) ||
3151 (fc
== RPC_FC_CPSTRUCT
) ||
3152 (fc
== RPC_FC_CVSTRUCT
&& type_has_pointers(type
)))
3154 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
3155 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
3157 write_pointer_description(file
, type
, tfsoff
);
3158 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
3162 write_struct_members(file
, type
, fc
== RPC_FC_BOGUS_STRUCT
, &corroff
,
3165 if (fc
== RPC_FC_BOGUS_STRUCT
)
3169 type
->ptrdesc
= *tfsoff
;
3170 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
3172 type_t
*ft
= f
->type
;
3173 switch (typegen_detect_type(ft
, f
->attrs
, TDT_IGNORE_STRINGS
))
3176 if (is_string_type(f
->attrs
, ft
))
3177 write_string_tfs(file
, f
->attrs
, ft
, TYPE_CONTEXT_CONTAINER
, f
->name
, tfsoff
);
3179 write_pointer_tfs(file
, f
->attrs
, ft
,
3180 type_pointer_get_ref(ft
)->typestring_offset
,
3181 TYPE_CONTEXT_CONTAINER
, tfsoff
);
3184 if (type_array_is_decl_as_ptr(ft
))
3186 unsigned int offset
;
3188 print_file(file
, 0, "/* %d */\n", *tfsoff
);
3190 offset
= ft
->typestring_offset
;
3191 /* skip over the pointer that is written for strings, since a
3192 * pointer has to be written in-place here */
3193 if (is_string_type(f
->attrs
, ft
))
3195 write_nonsimple_pointer(file
, f
->attrs
, ft
, TYPE_CONTEXT_CONTAINER
, offset
, tfsoff
);
3202 if (type
->ptrdesc
== *tfsoff
)
3206 current_structure
= save_current_structure
;
3207 return start_offset
;
3210 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
3214 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
3218 if (type_get_type(t
) == TYPE_BASIC
|| type_get_type(t
) == TYPE_ENUM
)
3221 if (type_get_type(t
) == TYPE_BASIC
)
3222 fc
= get_basic_fc(t
);
3224 fc
= get_enum_fc(t
);
3225 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
3226 fc
, string_of_type(fc
));
3228 else if (t
->typestring_offset
)
3230 short reloff
= t
->typestring_offset
- *tfsoff
;
3231 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
3232 reloff
, reloff
, t
->typestring_offset
);
3235 error("write_branch_type: type unimplemented %d\n", type_get_type(t
));
3241 static unsigned int write_union_tfs(FILE *file
, const attr_list_t
*attrs
,
3242 type_t
*type
, unsigned int *tfsoff
)
3244 unsigned int start_offset
;
3247 unsigned int nbranch
= 0;
3248 type_t
*deftype
= NULL
;
3249 short nodeftype
= 0xffff;
3252 if (processed(type
) &&
3253 (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
|| !is_attr(type
->attrs
, ATTR_SWITCHTYPE
)))
3254 return type
->typestring_offset
;
3258 size
= type_memsize(type
);
3260 fields
= type_union_get_cases(type
);
3262 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
3264 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
3266 nbranch
+= list_count(cases
);
3268 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
3271 start_offset
= *tfsoff
;
3272 update_tfsoff(type
, start_offset
, file
);
3273 print_start_tfs_comment(file
, type
, start_offset
);
3274 if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
3276 const var_t
*sv
= type_union_get_switch_value(type
);
3277 const type_t
*st
= sv
->type
;
3280 if (type_get_type(st
) == TYPE_BASIC
)
3282 fc
= get_basic_fc(st
);
3297 error("union switch type must be an integer, char, or enum\n");
3300 else if (type_get_type(st
) == TYPE_ENUM
)
3301 fc
= get_enum_fc(st
);
3303 error("union switch type must be an integer, char, or enum\n");
3305 print_file(file
, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION
);
3306 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
3307 0x40 | fc
, string_of_type(fc
));
3310 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
3312 const expr_t
*switch_is
= get_attrp(attrs
, ATTR_SWITCHIS
);
3313 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
3316 if (type_get_type(st
) == TYPE_BASIC
)
3318 fc
= get_basic_fc(st
);
3333 error("union switch type must be an integer, char, or enum\n");
3336 else if (type_get_type(st
) == TYPE_ENUM
)
3337 fc
= get_enum_fc(st
);
3339 error("union switch type must be an integer, char, or enum\n");
3341 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
3342 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
3343 fc
, string_of_type(fc
));
3345 *tfsoff
+= write_conf_or_var_desc(file
, current_structure
, 0, st
, switch_is
);
3346 print_file(file
, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff
+ 2);
3348 print_file(file
, 0, "/* %u */\n", *tfsoff
);
3351 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size
, size
);
3352 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch
, nbranch
);
3355 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
3357 type_t
*ft
= f
->type
;
3358 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
3359 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
3362 if (cases
== NULL
&& !deflt
)
3363 error("union field %s with neither case nor default attribute\n", f
->name
);
3365 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
3367 /* MIDL doesn't check for duplicate cases, even though that seems
3368 like a reasonable thing to do, it just dumps them to the TFS
3369 like we're going to do here. */
3370 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
3372 write_branch_type(file
, ft
, tfsoff
);
3375 /* MIDL allows multiple default branches, even though that seems
3376 illogical, it just chooses the last one, which is what we will
3387 write_branch_type(file
, deftype
, tfsoff
);
3391 print_file(file
, 2, "NdrFcShort(0x%hx),\n", nodeftype
);
3395 return start_offset
;
3398 static unsigned int write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
3399 unsigned int *typeformat_offset
)
3402 unsigned int start_offset
= *typeformat_offset
;
3403 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
3405 if (!iid
&& processed(type
)) return type
->typestring_offset
;
3407 print_start_tfs_comment(file
, type
, start_offset
);
3408 update_tfsoff(type
, start_offset
, file
);
3412 print_file(file
, 2, "0x2f, /* FC_IP */\n");
3413 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
3415 += write_conf_or_var_desc(file
, current_structure
, 0, type
, iid
) + 2;
3419 const type_t
*base
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3420 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
3423 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
3425 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
3426 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
3427 print_file(file
, 2, "NdrFcLong(0x%08x),\n", uuid
->Data1
);
3428 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
3429 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
3430 for (i
= 0; i
< 8; ++i
)
3431 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
3434 fprintf(file
, "\n");
3436 *typeformat_offset
+= 18;
3438 return start_offset
;
3441 static unsigned int write_contexthandle_tfs(FILE *file
,
3442 const attr_list_t
*attrs
,
3445 unsigned int *typeformat_offset
)
3447 unsigned int start_offset
= *typeformat_offset
;
3448 unsigned char flags
= get_contexthandle_flags( current_iface
, attrs
, type
);
3450 print_start_tfs_comment(file
, type
, start_offset
);
3452 if (flags
& 0x80) /* via ptr */
3454 int pointer_type
= get_pointer_fc( type
, attrs
, toplevel_param
);
3455 if (!pointer_type
) pointer_type
= RPC_FC_RP
;
3456 *typeformat_offset
+= 4;
3457 print_file(file
, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type
, string_of_type(pointer_type
) );
3458 print_file(file
, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset
);
3459 print_file(file
, 0, "/* %2u */\n", *typeformat_offset
);
3462 print_file(file
, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", RPC_FC_BIND_CONTEXT
);
3463 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
3464 /* return and can't be null values overlap */
3465 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
3466 print_file(file
, 0, "can't be null, ");
3467 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
3468 print_file(file
, 0, "serialize, ");
3469 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
3470 print_file(file
, 0, "no serialize, ");
3471 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
3472 print_file(file
, 0, "strict, ");
3473 if ((flags
& 0x21) == 0x20)
3474 print_file(file
, 0, "out, ");
3475 if ((flags
& 0x21) == 0x21)
3476 print_file(file
, 0, "return, ");
3478 print_file(file
, 0, "in, ");
3480 print_file(file
, 0, "via ptr, ");
3481 print_file(file
, 0, "*/\n");
3482 print_file(file
, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type
));
3483 print_file(file
, 2, "0, /* FIXME: param num */\n");
3484 *typeformat_offset
+= 4;
3486 update_tfsoff( type
, start_offset
, file
);
3487 return start_offset
;
3490 static unsigned int write_range_tfs(FILE *file
, const attr_list_t
*attrs
,
3491 type_t
*type
, expr_list_t
*range_list
,
3492 unsigned int *typeformat_offset
)
3495 unsigned int start_offset
= *typeformat_offset
;
3496 const expr_t
*range_min
= LIST_ENTRY(list_head(range_list
), const expr_t
, entry
);
3497 const expr_t
*range_max
= LIST_ENTRY(list_next(range_list
, list_head(range_list
)), const expr_t
, entry
);
3499 if (type_get_type(type
) == TYPE_BASIC
)
3500 fc
= get_basic_fc(type
);
3502 fc
= get_enum_fc(type
);
3504 /* fc must fit in lower 4-bits of 8-bit field below */
3507 print_file(file
, 0, "/* %u */\n", *typeformat_offset
);
3508 print_file(file
, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE
);
3509 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
3510 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min
->cval
, range_min
->cval
);
3511 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max
->cval
, range_max
->cval
);
3512 update_tfsoff( type
, start_offset
, file
);
3513 *typeformat_offset
+= 10;
3515 return start_offset
;
3518 static unsigned int write_type_tfs(FILE *file
, int indent
,
3519 const attr_list_t
*attrs
, type_t
*type
,
3521 enum type_context context
,
3522 unsigned int *typeformat_offset
)
3524 unsigned int offset
;
3526 switch (typegen_detect_type(type
, attrs
, TDT_ALL_TYPES
))
3528 case TGT_CTXT_HANDLE
:
3529 case TGT_CTXT_HANDLE_POINTER
:
3530 return write_contexthandle_tfs(file
, attrs
, type
,
3531 context
== TYPE_CONTEXT_TOPLEVELPARAM
, typeformat_offset
);
3533 return write_user_tfs(file
, type
, typeformat_offset
);
3535 return write_string_tfs(file
, attrs
, type
, context
, name
, typeformat_offset
);
3539 /* conformant and pointer arrays are handled specially */
3540 if ((context
!= TYPE_CONTEXT_CONTAINER
&&
3541 context
!= TYPE_CONTEXT_CONTAINER_NO_POINTERS
) ||
3542 !is_conformant_array(type
) || type_array_is_decl_as_ptr(type
))
3543 off
= write_array_tfs(file
, attrs
, type
, name
, typeformat_offset
);
3546 if (context
!= TYPE_CONTEXT_CONTAINER
&&
3547 context
!= TYPE_CONTEXT_CONTAINER_NO_POINTERS
)
3550 ptr_type
= get_pointer_fc(type
, attrs
,
3551 context
== TYPE_CONTEXT_TOPLEVELPARAM
);
3552 if (ptr_type
!= RPC_FC_RP
|| type_array_is_decl_as_ptr(type
))
3554 unsigned int absoff
= type
->typestring_offset
;
3555 short reloff
= absoff
- (*typeformat_offset
+ 2);
3556 off
= *typeformat_offset
;
3557 print_file(file
, 0, "/* %d */\n", off
);
3558 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
3559 string_of_type(ptr_type
));
3560 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
3561 reloff
, reloff
, absoff
);
3562 if (ptr_type
!= RPC_FC_RP
) update_tfsoff( type
, off
, file
);
3563 *typeformat_offset
+= 4;
3565 type
->details
.array
.ptr_tfsoff
= off
;
3570 return write_struct_tfs(file
, type
, name
, typeformat_offset
);
3572 return write_union_tfs(file
, attrs
, type
, typeformat_offset
);
3579 expr_list_t
*range_list
= get_attrp(attrs
, ATTR_RANGE
);
3581 range_list
= get_aliaschain_attrp(type
, ATTR_RANGE
);
3582 return write_range_tfs(file
, attrs
, type
, range_list
, typeformat_offset
);
3584 case TGT_IFACE_POINTER
:
3585 return write_ip_tfs(file
, attrs
, type
, typeformat_offset
);
3588 enum type_context ref_context
;
3589 if (context
== TYPE_CONTEXT_TOPLEVELPARAM
)
3590 ref_context
= TYPE_CONTEXT_PARAM
;
3591 else if (context
== TYPE_CONTEXT_CONTAINER_NO_POINTERS
)
3592 ref_context
= TYPE_CONTEXT_CONTAINER
;
3594 ref_context
= context
;
3595 offset
= write_type_tfs( file
, indent
, attrs
, type_pointer_get_ref(type
), name
,
3596 ref_context
, typeformat_offset
);
3597 if (context
== TYPE_CONTEXT_CONTAINER_NO_POINTERS
)
3599 return write_pointer_tfs(file
, attrs
, type
, offset
, context
, typeformat_offset
);
3604 error("invalid type %s for var %s\n", type
->name
, name
);
3608 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
3609 const char *name
, int write_ptr
, unsigned int *tfsoff
)
3611 return write_type_tfs(file
, 2, attrs
, type
, name
, write_ptr
? TYPE_CONTEXT_CONTAINER
: TYPE_CONTEXT_CONTAINER_NO_POINTERS
, tfsoff
);
3614 static unsigned int process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
3615 type_pred_t pred
, unsigned int *typeformat_offset
)
3618 const statement_t
*stmt
;
3620 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3622 const type_t
*iface
;
3623 const statement_t
*stmt_func
;
3625 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
3628 iface
= stmt
->u
.type
;
3632 current_iface
= iface
;
3633 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
3635 const var_t
*func
= stmt_func
->u
.var
;
3636 current_func
= func
;
3637 if (is_local(func
->attrs
)) continue;
3639 var
= type_function_get_retval(func
->type
);
3640 if (!is_void(var
->type
))
3641 var
->typestring_offset
= write_type_tfs( file
, 2, func
->attrs
, var
->type
, func
->name
,
3642 TYPE_CONTEXT_PARAM
, typeformat_offset
);
3644 if (type_get_function_args(func
->type
))
3645 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), var_t
, entry
)
3646 var
->typestring_offset
= write_type_tfs( file
, 2, var
->attrs
, var
->type
, var
->name
,
3647 TYPE_CONTEXT_TOPLEVELPARAM
,
3648 typeformat_offset
);
3652 return *typeformat_offset
+ 1;
3655 static unsigned int process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
3657 unsigned int typeformat_offset
= 2;
3659 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
3663 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
3667 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
3668 print_file(file
, indent
, "{\n");
3670 print_file(file
, indent
, "0,\n");
3671 print_file(file
, indent
, "{\n");
3673 print_file(file
, indent
, "NdrFcShort(0x0),\n");
3675 set_all_tfswrite(TRUE
);
3676 process_tfs(file
, stmts
, pred
);
3678 print_file(file
, indent
, "0x0\n");
3680 print_file(file
, indent
, "}\n");
3682 print_file(file
, indent
, "};\n");
3683 print_file(file
, indent
, "\n");
3686 static unsigned int get_required_buffer_size_type(
3687 const type_t
*type
, const char *name
, const attr_list_t
*attrs
, int toplevel_param
, unsigned int *alignment
)
3690 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_RANGES
))
3695 const type_t
*utype
= get_user_type(type
, &uname
);
3696 return get_required_buffer_size_type(utype
, uname
, NULL
, FALSE
, alignment
);
3699 switch (get_basic_fc(type
))
3717 case RPC_FC_ERROR_STATUS_T
:
3726 case RPC_FC_INT3264
:
3727 case RPC_FC_UINT3264
:
3728 assert( pointer_size
);
3729 *alignment
= pointer_size
;
3730 return pointer_size
;
3733 case RPC_FC_BIND_PRIMITIVE
:
3737 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3738 get_basic_fc(type
));
3744 switch (get_enum_fc(type
))
3756 if (get_struct_fc(type
) == RPC_FC_STRUCT
)
3758 if (!type_struct_get_fields(type
)) return 0;
3759 return fields_memsize(type_struct_get_fields(type
), alignment
);
3765 unsigned int size
, align
;
3766 const type_t
*ref
= type_pointer_get_ref(type
);
3767 if (is_string_type( attrs
, ref
)) break;
3768 if (!(size
= get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, &align
))) break;
3769 if (get_pointer_fc(type
, attrs
, toplevel_param
) != RPC_FC_RP
)
3779 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_RP
)
3781 switch (get_array_fc(type
))
3783 case RPC_FC_SMFARRAY
:
3784 case RPC_FC_LGFARRAY
:
3785 return type_array_get_dim(type
) *
3786 get_required_buffer_size_type(type_array_get_element(type
), name
,
3787 NULL
, FALSE
, alignment
);
3798 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
3800 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3801 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3803 if (!in_attr
&& !out_attr
)
3808 if ((pass
== PASS_IN
&& in_attr
) || (pass
== PASS_OUT
&& out_attr
) ||
3809 pass
== PASS_RETURN
)
3811 if (is_ptrchain_attr(var
, ATTR_CONTEXTHANDLE
))
3817 if (!is_string_type(var
->attrs
, var
->type
))
3818 return get_required_buffer_size_type(var
->type
, var
->name
,
3819 var
->attrs
, TRUE
, alignment
);
3824 static unsigned int get_function_buffer_size( const var_t
*func
, enum pass pass
)
3827 unsigned int total_size
= 0, alignment
;
3829 if (type_get_function_args(func
->type
))
3831 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3833 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
3834 total_size
+= alignment
;
3838 if (pass
== PASS_OUT
&& !is_void(type_function_get_rettype(func
->type
)))
3841 v
.type
= type_function_get_rettype(func
->type
);
3842 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
3843 total_size
+= alignment
;
3848 static void print_phase_function(FILE *file
, int indent
, const char *type
,
3849 const char *local_var_prefix
, enum remoting_phase phase
,
3850 const var_t
*var
, unsigned int type_offset
)
3852 const char *function
;
3855 case PHASE_BUFFERSIZE
:
3856 function
= "BufferSize";
3859 function
= "Marshall";
3861 case PHASE_UNMARSHAL
:
3862 function
= "Unmarshall";
3872 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
3874 print_file(file
, indent
, "&__frame->_StubMsg,\n");
3875 print_file(file
, indent
, "%s%s%s%s%s,\n",
3876 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
3877 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
3879 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
3881 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3882 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
3883 if (phase
== PHASE_UNMARSHAL
)
3884 print_file(file
, indent
, "0);\n");
3888 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
3889 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
3890 const char *varname
)
3892 type_t
*type
= var
->type
;
3893 unsigned int alignment
= 0;
3895 /* no work to do for other phases, buffer sizing is done elsewhere */
3896 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
3899 if (type_get_type(type
) == TYPE_ENUM
||
3900 (type_get_type(type
) == TYPE_BASIC
&&
3901 type_basic_get_type(type
) == TYPE_BASIC_INT3264
&&
3906 if (type_get_type(type
) == TYPE_ENUM
)
3907 fc
= get_enum_fc(type
);
3909 fc
= get_basic_fc(type
);
3911 if (phase
== PHASE_MARSHAL
)
3912 print_file(file
, indent
, "NdrSimpleTypeMarshall(\n");
3914 print_file(file
, indent
, "NdrSimpleTypeUnmarshall(\n");
3915 print_file(file
, indent
+1, "&__frame->_StubMsg,\n");
3916 print_file(file
, indent
+1, "(unsigned char *)&%s%s,\n",
3919 print_file(file
, indent
+1, "0x%02x /* %s */);\n", fc
, string_of_type(fc
));
3923 const type_t
*ref
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3924 switch (get_basic_fc(ref
))
3942 case RPC_FC_ERROR_STATUS_T
:
3943 /* pointer_size must be 4 if we got here in these two cases */
3944 case RPC_FC_INT3264
:
3945 case RPC_FC_UINT3264
:
3955 case RPC_FC_BIND_PRIMITIVE
:
3956 /* no marshalling needed */
3960 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3961 var
->name
, get_basic_fc(ref
));
3964 if (phase
== PHASE_MARSHAL
&& alignment
> 1)
3965 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
3966 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3967 alignment
- 1, alignment
- 1);
3969 if (phase
== PHASE_MARSHAL
)
3971 print_file(file
, indent
, "*(");
3972 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3974 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
3976 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
3977 fprintf(file
, "%s%s", local_var_prefix
, varname
);
3978 fprintf(file
, ";\n");
3980 else if (phase
== PHASE_UNMARSHAL
)
3982 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
3983 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3984 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
3985 print_file(file
, indent
, "{\n");
3986 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3987 print_file(file
, indent
, "}\n");
3988 print_file(file
, indent
, "%s%s%s",
3989 (pass
== PASS_IN
|| pass
== PASS_RETURN
) ? "" : "*",
3990 local_var_prefix
, varname
);
3991 if (pass
== PASS_IN
&& is_ptr(type
))
3992 fprintf(file
, " = (");
3994 fprintf(file
, " = *(");
3995 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3996 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
3999 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
4000 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
4001 fprintf(file
, ");\n");
4005 /* returns whether the MaxCount, Offset or ActualCount members need to be
4006 * filled in for the specified phase */
4007 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
4009 return (phase
!= PHASE_UNMARSHAL
);
4012 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
4016 for ( ; is_array(t
); t
= type_array_get_element(t
))
4017 if (type_array_has_conformance(t
) &&
4018 type_array_get_conformance(t
)->type
!= EXPR_VOID
)
4021 x
= type_array_get_conformance(t
);
4023 error("%s: multidimensional conformant"
4024 " arrays not supported at the top level\n",
4031 void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
4032 enum remoting_phase phase
, const var_t
*var
, int valid_variance
)
4034 const type_t
*type
= var
->type
;
4035 /* get fundamental type for the argument */
4038 switch (typegen_detect_type(type
, var
->attrs
, TDT_IGNORE_STRINGS
|TDT_IGNORE_RANGES
))
4041 if (is_conformance_needed_for_phase(phase
))
4043 if (type_array_has_conformance(type
) &&
4044 type_array_get_conformance(type
)->type
!= EXPR_VOID
)
4046 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4047 write_expr(file
, type_array_get_conformance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
4048 fprintf(file
, ";\n\n");
4050 if (type_array_has_variance(type
))
4052 print_file(file
, indent
, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
4055 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
4056 write_expr(file
, type_array_get_variance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
4057 fprintf(file
, ";\n\n");
4060 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
4065 if (type_get_type(type
) == TYPE_UNION
&&
4066 is_conformance_needed_for_phase(phase
))
4068 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
4069 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
4070 fprintf(file
, ";\n\n");
4073 case TGT_IFACE_POINTER
:
4077 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
4079 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
4080 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
4081 fprintf( file
, ";\n\n" );
4086 type
= type_pointer_get_ref(type
);
4090 case TGT_CTXT_HANDLE
:
4091 case TGT_CTXT_HANDLE_POINTER
:
4103 static void write_remoting_arg(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
4104 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
4106 int in_attr
, out_attr
, pointer_type
;
4107 const char *type_str
= NULL
;
4108 const type_t
*type
= var
->type
;
4109 unsigned int alignment
, start_offset
= type
->typestring_offset
;
4111 if (is_ptr(type
) || is_array(type
))
4112 pointer_type
= get_pointer_fc(type
, var
->attrs
, pass
!= PASS_RETURN
);
4116 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
4117 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
4118 if (!in_attr
&& !out_attr
)
4121 if (phase
!= PHASE_FREE
)
4125 if (!in_attr
) return;
4128 if (!out_attr
) return;
4134 if (phase
== PHASE_BUFFERSIZE
&& get_required_buffer_size( var
, &alignment
, pass
)) return;
4136 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
, TRUE
);
4138 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
4140 case TGT_CTXT_HANDLE
:
4141 case TGT_CTXT_HANDLE_POINTER
:
4142 if (phase
== PHASE_MARSHAL
)
4144 if (pass
== PASS_IN
)
4146 /* if the context_handle attribute appears in the chain of types
4147 * without pointers being followed, then the context handle must
4148 * be direct, otherwise it is a pointer */
4149 const char *ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? "" : "*";
4150 print_file(file
, indent
, "NdrClientContextMarshall(\n");
4151 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
4152 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr
, local_var_prefix
,
4154 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
4158 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
4159 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
4160 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
4161 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
4162 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
4165 else if (phase
== PHASE_UNMARSHAL
)
4167 if (pass
== PASS_OUT
)
4170 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
4171 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
4172 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
4173 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
4174 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
4178 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
4179 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
4180 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
4185 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
4188 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
||
4189 pointer_type
!= RPC_FC_RP
)
4191 /* strings returned are assumed to be global and hence don't
4193 if (is_declptr(type
) && !(phase
== PHASE_FREE
&& pass
== PASS_RETURN
))
4194 print_phase_function(file
, indent
, "Pointer", local_var_prefix
,
4195 phase
, var
, start_offset
);
4196 else if (pointer_type
== RPC_FC_RP
&& phase
== PHASE_FREE
&&
4197 !in_attr
&& is_conformant_array(type
))
4199 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
4201 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
4206 unsigned int real_start_offset
= start_offset
;
4207 /* skip over pointer description straight to string description */
4208 if (is_declptr(type
))
4210 if (is_conformant_array(type
))
4211 real_start_offset
+= 4;
4213 real_start_offset
+= 2;
4215 if (is_array(type
) && !is_conformant_array(type
))
4216 print_phase_function(file
, indent
, "NonConformantString",
4217 local_var_prefix
, phase
, var
,
4220 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
4221 phase
, var
, real_start_offset
);
4226 unsigned char tc
= get_array_fc(type
);
4227 const char *array_type
= NULL
;
4229 /* We already have the size_is expression since it's at the
4230 top level, but do checks for multidimensional conformant
4231 arrays. When we handle them, we'll need to extend this
4232 function to return a list, and then we'll actually use
4233 the return value. */
4234 get_size_is_expr(type
, var
->name
);
4238 case RPC_FC_SMFARRAY
:
4239 case RPC_FC_LGFARRAY
:
4240 array_type
= "FixedArray";
4242 case RPC_FC_SMVARRAY
:
4243 case RPC_FC_LGVARRAY
:
4244 array_type
= "VaryingArray";
4247 array_type
= "ConformantArray";
4249 case RPC_FC_CVARRAY
:
4250 array_type
= "ConformantVaryingArray";
4252 case RPC_FC_BOGUS_ARRAY
:
4253 array_type
= "ComplexArray";
4257 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
4259 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
4261 /* these are all unmarshalled by allocating memory */
4262 if (tc
== RPC_FC_BOGUS_ARRAY
||
4263 tc
== RPC_FC_CVARRAY
||
4264 ((tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
) && in_attr
) ||
4265 (tc
== RPC_FC_CARRAY
&& !in_attr
))
4267 if (type_array_is_decl_as_ptr(type
) && type
->details
.array
.ptr_tfsoff
)
4269 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
,
4270 type
->details
.array
.ptr_tfsoff
);
4273 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
4274 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
4276 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
4280 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
4284 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
4287 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
4290 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
4291 /* Note: this goes beyond what MIDL does - it only supports arguments
4292 * with the [range] attribute in Oicf mode */
4293 if (phase
== PHASE_UNMARSHAL
)
4295 const expr_t
*range_min
;
4296 const expr_t
*range_max
;
4297 expr_list_t
*range_list
= get_attrp(var
->attrs
, ATTR_RANGE
);
4299 range_list
= get_aliaschain_attrp(type
, ATTR_RANGE
);
4300 range_min
= LIST_ENTRY(list_head(range_list
), const expr_t
, entry
);
4301 range_max
= LIST_ENTRY(list_next(range_list
, list_head(range_list
)), const expr_t
, entry
);
4303 print_file(file
, indent
, "if ((%s%s < (", local_var_prefix
, var
->name
);
4304 write_type_decl(file
, var
->type
, NULL
);
4305 fprintf(file
, ")0x%x) || (%s%s > (", range_min
->cval
, local_var_prefix
, var
->name
);
4306 write_type_decl(file
, var
->type
, NULL
);
4307 fprintf(file
, ")0x%x))\n", range_max
->cval
);
4308 print_file(file
, indent
, "{\n");
4309 print_file(file
, indent
+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
4310 print_file(file
, indent
, "}\n");
4314 switch (get_struct_fc(type
))
4317 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
4318 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
4320 case RPC_FC_PSTRUCT
:
4321 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
4323 case RPC_FC_CSTRUCT
:
4324 case RPC_FC_CPSTRUCT
:
4325 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
4327 case RPC_FC_CVSTRUCT
:
4328 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
4330 case RPC_FC_BOGUS_STRUCT
:
4331 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
4334 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(type
));
4339 const char *union_type
= NULL
;
4341 if (type_get_type(type
) == TYPE_UNION
)
4342 union_type
= "NonEncapsulatedUnion";
4343 else if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
4344 union_type
= "EncapsulatedUnion";
4346 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
4347 phase
, var
, start_offset
);
4352 const type_t
*ref
= type_pointer_get_ref(type
);
4353 if (pointer_type
== RPC_FC_RP
) switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
4356 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
4359 /* base types have known sizes, so don't need a sizing pass
4360 * and don't have any memory to free and so don't need a
4362 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
4363 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
4366 switch (get_struct_fc(ref
))
4369 /* simple structs have known sizes, so don't need a sizing
4370 * pass and don't have any memory to free and so don't
4371 * need a freeing pass */
4372 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
4373 type_str
= "SimpleStruct";
4374 else if (phase
== PHASE_FREE
&& pass
== PASS_RETURN
)
4376 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
4378 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
4382 case RPC_FC_PSTRUCT
:
4383 type_str
= "SimpleStruct";
4385 case RPC_FC_CSTRUCT
:
4386 case RPC_FC_CPSTRUCT
:
4387 type_str
= "ConformantStruct";
4389 case RPC_FC_CVSTRUCT
:
4390 type_str
= "ConformantVaryingStruct";
4392 case RPC_FC_BOGUS_STRUCT
:
4393 type_str
= "ComplexStruct";
4396 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(ref
));
4401 if (phase
== PHASE_FREE
)
4402 type_str
= "Pointer";
4404 start_offset
= ref
->typestring_offset
;
4405 print_phase_function(file
, indent
, type_str
, local_var_prefix
, phase
, var
, start_offset
);
4409 if (phase
== PHASE_FREE
)
4410 type_str
= "Pointer";
4413 if (type_get_type(ref
) == TYPE_UNION
)
4414 type_str
= "NonEncapsulatedUnion";
4415 else if (type_get_type(ref
) == TYPE_ENCAPSULATED_UNION
)
4416 type_str
= "EncapsulatedUnion";
4418 start_offset
= ref
->typestring_offset
;
4421 print_phase_function(file
, indent
, type_str
, local_var_prefix
,
4422 phase
, var
, start_offset
);
4425 if (phase
!= PHASE_FREE
)
4427 type_str
= "UserMarshal";
4428 start_offset
= ref
->typestring_offset
;
4430 else type_str
= "Pointer";
4432 print_phase_function(file
, indent
, type_str
, local_var_prefix
, phase
, var
, start_offset
);
4438 case TGT_IFACE_POINTER
:
4439 case TGT_CTXT_HANDLE
:
4440 case TGT_CTXT_HANDLE_POINTER
:
4441 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
4448 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
4451 case TGT_IFACE_POINTER
:
4452 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
4458 fprintf(file
, "\n");
4461 void write_remoting_arguments(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
4462 enum pass pass
, enum remoting_phase phase
)
4464 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
4466 unsigned int size
= get_function_buffer_size( func
, pass
);
4467 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
4470 if (pass
== PASS_RETURN
)
4472 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
,
4473 type_function_get_retval(func
->type
) );
4478 if (!type_get_function_args(func
->type
))
4480 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
4481 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
4486 unsigned int get_size_procformatstring_func(const type_t
*iface
, const var_t
*func
)
4488 unsigned int offset
= 0;
4489 write_procformatstring_func( NULL
, 0, iface
, func
, &offset
, 0 );
4493 unsigned int get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
4495 const statement_t
*stmt
;
4496 unsigned int size
= 1;
4498 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
4500 const type_t
*iface
;
4501 const statement_t
*stmt_func
;
4503 if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
4506 iface
= stmt
->u
.type
;
4510 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
4512 const var_t
*func
= stmt_func
->u
.var
;
4513 if (!is_local(func
->attrs
))
4514 size
+= get_size_procformatstring_func( iface
, func
);
4520 unsigned int get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
4522 set_all_tfswrite(FALSE
);
4523 return process_tfs(NULL
, stmts
, pred
);
4526 void declare_stub_args( FILE *file
, int indent
, const var_t
*func
)
4528 int in_attr
, out_attr
;
4530 const var_t
*var
= type_function_get_retval(func
->type
);
4532 /* declare return value */
4533 if (!is_void(var
->type
))
4535 print_file(file
, indent
, "%s", "");
4536 write_type_decl(file
, var
->type
, var
->name
);
4537 fprintf(file
, ";\n");
4540 if (!type_get_function_args(func
->type
))
4543 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
4545 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
4546 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
4547 if (!out_attr
&& !in_attr
)
4550 if (is_context_handle(var
->type
))
4551 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
4554 if (!in_attr
&& !is_conformant_array(var
->type
))
4556 type_t
*type_to_print
;
4558 print_file(file
, indent
, "%s", "");
4559 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
4560 !type_array_is_decl_as_ptr(var
->type
))
4561 type_to_print
= var
->type
;
4563 type_to_print
= type_pointer_get_ref(var
->type
);
4564 sprintf(name
, "_W%u", i
++);
4565 write_type_decl(file
, type_to_print
, name
);
4566 fprintf(file
, ";\n");
4569 print_file(file
, indent
, "%s", "");
4570 write_type_decl_left(file
, var
->type
);
4572 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
4573 !type_array_is_decl_as_ptr(var
->type
)) {
4574 fprintf(file
, "(*%s)", var
->name
);
4576 fprintf(file
, "%s", var
->name
);
4577 write_type_right(file
, var
->type
, FALSE
);
4578 fprintf(file
, ";\n");
4580 if (decl_indirect(var
->type
))
4581 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
4587 void assign_stub_out_args( FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
4589 int in_attr
, out_attr
;
4594 if (!type_get_function_args(func
->type
))
4597 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
4599 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
4600 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
4601 if (!out_attr
&& !in_attr
)
4606 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
4608 switch (typegen_detect_type(var
->type
, var
->attrs
, TDT_IGNORE_STRINGS
))
4610 case TGT_CTXT_HANDLE_POINTER
:
4611 fprintf(file
, " = NdrContextHandleInitialize(\n");
4612 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
4613 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
4614 var
->typestring_offset
);
4617 if (type_array_has_conformance(var
->type
))
4622 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
4623 for (type
= var
->type
;
4624 is_array(type
) && type_array_has_conformance(type
);
4625 type
= type_array_get_element(type
))
4627 write_expr(file
, type_array_get_conformance(type
), TRUE
,
4628 TRUE
, NULL
, NULL
, local_var_prefix
);
4629 fprintf(file
, " * ");
4631 size
= type_memsize(type
);
4632 fprintf(file
, "%u);\n", size
);
4634 print_file(file
, indent
, "memset(%s%s, 0, ", local_var_prefix
, var
->name
);
4635 for (type
= var
->type
;
4636 is_array(type
) && type_array_has_conformance(type
);
4637 type
= type_array_get_element(type
))
4639 write_expr(file
, type_array_get_conformance(type
), TRUE
,
4640 TRUE
, NULL
, NULL
, local_var_prefix
);
4641 fprintf(file
, " * ");
4643 size
= type_memsize(type
);
4644 fprintf(file
, "%u);\n", size
);
4647 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
++);
4650 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
4651 ref
= type_pointer_get_ref(var
->type
);
4652 switch (typegen_detect_type(ref
, var
->attrs
, TDT_IGNORE_STRINGS
))
4658 case TGT_IFACE_POINTER
:
4659 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
4662 print_file(file
, indent
, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4663 local_var_prefix
, i
, local_var_prefix
, i
);
4666 if (type_array_is_decl_as_ptr(ref
))
4668 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
4671 ref
= type_array_get_element(ref
);
4675 if (type_has_pointers(ref
))
4676 print_file(file
, indent
, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
4677 local_var_prefix
, i
, local_var_prefix
, i
);
4679 case TGT_CTXT_HANDLE
:
4680 case TGT_CTXT_HANDLE_POINTER
:
4683 /* not initialised */
4696 fprintf(file
, "\n");
4700 void write_func_param_struct( FILE *file
, const type_t
*iface
, const type_t
*func
,
4701 const char *var_decl
, int add_retval
)
4703 var_t
*retval
= type_function_get_retval( func
);
4704 const var_list_t
*args
= type_get_function_args( func
);
4707 unsigned int align
= 0;
4710 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
4711 if (!is_array( arg
->type
)) type_memsize_and_alignment( arg
->type
, &align
);
4713 needs_packing
= (align
> pointer_size
);
4715 if (needs_packing
) print_file( file
, 0, "#include <pshpack%u.h>\n", pointer_size
);
4716 print_file(file
, 1, "struct _PARAM_STRUCT\n" );
4717 print_file(file
, 1, "{\n" );
4718 if (is_object( iface
)) print_file(file
, 2, "%s *This;\n", iface
->name
);
4720 if (args
) LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
4722 print_file(file
, 2, "%s", "");
4723 write_type_left( file
, (type_t
*)arg
->type
, TRUE
);
4724 if (needs_space_after( arg
->type
)) fputc( ' ', file
);
4725 if (is_array( arg
->type
) && !type_array_is_decl_as_ptr( arg
->type
)) fputc( '*', file
);
4727 /* FIXME: should check for large args being passed by pointer */
4729 if (is_array( arg
->type
) || is_ptr( arg
->type
)) align
= pointer_size
;
4730 else type_memsize_and_alignment( arg
->type
, &align
);
4732 if (align
>= pointer_size
)
4733 fprintf( file
, "%s;\n", arg
->name
);
4735 fprintf( file
, "%s DECLSPEC_ALIGN(%u);\n", arg
->name
, pointer_size
);
4737 if (add_retval
&& !is_void( retval
->type
))
4739 print_file(file
, 2, "%s", "");
4740 write_type_decl( file
, retval
->type
, retval
->name
);
4741 if (is_array( retval
->type
) || is_ptr( retval
->type
) ||
4742 type_memsize( retval
->type
) == pointer_size
)
4743 fprintf( file
, ";\n" );
4745 fprintf( file
, " DECLSPEC_ALIGN(%u);\n", pointer_size
);
4747 print_file(file
, 1, "} %s;\n", var_decl
);
4748 if (needs_packing
) print_file( file
, 0, "#include <poppack.h>\n" );
4749 print_file( file
, 0, "\n" );
4752 void write_pointer_checks( FILE *file
, int indent
, const var_t
*func
)
4754 const var_list_t
*args
= type_get_function_args( func
->type
);
4759 LIST_FOR_EACH_ENTRY( var
, args
, const var_t
, entry
)
4760 if (cant_be_null( var
))
4761 print_file( file
, indent
, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var
->name
);
4764 int write_expr_eval_routines(FILE *file
, const char *iface
)
4766 static const char *var_name
= "pS";
4767 static const char *var_name_expr
= "pS->";
4769 struct expr_eval_routine
*eval
;
4770 unsigned short callback_offset
= 0;
4772 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
4774 const char *name
= eval
->name
;
4777 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4778 eval
->iface
? eval
->iface
->name
: iface
, name
, callback_offset
);
4779 print_file(file
, 0, "{\n");
4780 if (type_get_type( eval
->cont_type
) == TYPE_FUNCTION
)
4782 write_func_param_struct( file
, eval
->iface
, eval
->cont_type
,
4783 "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE
);
4787 print_file(file
, 1, "%s", "");
4788 write_type_left(file
, (type_t
*)eval
->cont_type
, TRUE
);
4789 fprintf(file
, " *%s = (", var_name
);
4790 write_type_left(file
, (type_t
*)eval
->cont_type
, TRUE
);
4791 fprintf(file
, " *)(pStubMsg->StackTop - %u);\n", eval
->baseoff
);
4793 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4794 print_file(file
, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4795 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->cont_type
, "");
4796 fprintf(file
, ";\n");
4797 print_file(file
, 0, "}\n\n");
4803 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
4805 struct expr_eval_routine
*eval
;
4806 struct expr_eval_routine
*cursor
;
4807 unsigned short callback_offset
= 0;
4809 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4810 fprintf(file
, "{\n");
4812 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
4814 print_file(file
, 1, "%s_%sExprEval_%04u,\n",
4815 eval
->iface
? eval
->iface
->name
: iface
, eval
->name
, callback_offset
);
4817 list_remove(&eval
->entry
);
4822 fprintf(file
, "};\n\n");
4825 void write_user_quad_list(FILE *file
)
4829 if (list_empty(&user_type_list
))
4832 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4833 fprintf(file
, "{\n");
4834 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
4836 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
4837 print_file(file
, 1, "{\n");
4838 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
4839 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
4840 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
4841 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
4842 print_file(file
, 1, "}%s\n", sep
);
4844 fprintf(file
, "};\n\n");
4847 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
4849 const struct str_list_entry_t
*endpoint
;
4852 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4853 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
4854 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
4856 print_file( f
, 1, "{ (const unsigned char *)\"" );
4857 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
4859 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
4862 if (!*p
) goto error
;
4863 if (p
[1] != '[') goto error
;
4865 fprintf( f
, "\", (const unsigned char *)\"" );
4866 for (p
+= 2; *p
&& *p
!= ']'; p
++)
4868 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
4871 if (*p
!= ']') goto error
;
4872 fprintf( f
, "\" },\n" );
4874 print_file( f
, 0, "};\n\n" );
4878 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
4881 void write_client_call_routine( FILE *file
, const type_t
*iface
, const var_t
*func
,
4882 const char *prefix
, unsigned int proc_offset
)
4884 type_t
*rettype
= type_function_get_rettype( func
->type
);
4885 int has_ret
= !is_void( rettype
);
4886 const var_list_t
*args
= type_get_function_args( func
->type
);
4888 int len
, needs_params
= 0;
4890 /* we need a param structure if we have more than one arg */
4891 if (pointer_size
== 4 && args
) needs_params
= is_object( iface
) || list_count( args
) > 1;
4893 print_file( file
, 0, "{\n");
4896 if (has_ret
) print_file( file
, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
4897 write_func_param_struct( file
, iface
, func
->type
, "__params", FALSE
);
4898 if (is_object( iface
)) print_file( file
, 1, "__params.This = This;\n" );
4900 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
4901 print_file( file
, 1, "__params.%s = %s;\n", arg
->name
, arg
->name
);
4903 else if (has_ret
) print_file( file
, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
4905 len
= fprintf( file
, " %s%s( ",
4906 has_ret
? "_RetVal = " : "",
4907 get_stub_mode() == MODE_Oif
? "NdrClientCall2" : "NdrClientCall" );
4908 fprintf( file
, "&%s_StubDesc,", prefix
);
4909 fprintf( file
, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len
, "", proc_offset
);
4912 fprintf( file
, ",\n%*s&__params", len
, "" );
4914 else if (pointer_size
== 8)
4916 if (is_object( iface
)) fprintf( file
, ",\n%*sThis", len
, "" );
4918 LIST_FOR_EACH_ENTRY( arg
, args
, const var_t
, entry
)
4919 fprintf( file
, ",\n%*s%s", len
, "", arg
->name
);
4923 if (is_object( iface
)) fprintf( file
, ",\n%*s&This", len
, "" );
4926 arg
= LIST_ENTRY( list_head(args
), const var_t
, entry
);
4927 fprintf( file
, ",\n%*s&%s", len
, "", arg
->name
);
4930 fprintf( file
, " );\n" );
4933 print_file( file
, 1, "return (" );
4934 write_type_decl_left(file
, rettype
);
4935 fprintf( file
, ")%s;\n", pointer_size
== 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
4937 print_file( file
, 0, "}\n\n");
4940 void write_exceptions( FILE *file
)
4942 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4943 fprintf( file
, "\n");
4944 fprintf( file
, "#include \"wine/exception.h\"\n");
4945 fprintf( file
, "#undef RpcTryExcept\n");
4946 fprintf( file
, "#undef RpcExcept\n");
4947 fprintf( file
, "#undef RpcEndExcept\n");
4948 fprintf( file
, "#undef RpcTryFinally\n");
4949 fprintf( file
, "#undef RpcFinally\n");
4950 fprintf( file
, "#undef RpcEndFinally\n");
4951 fprintf( file
, "#undef RpcExceptionCode\n");
4952 fprintf( file
, "#undef RpcAbnormalTermination\n");
4953 fprintf( file
, "\n");
4954 fprintf( file
, "struct __exception_frame;\n");
4955 fprintf( file
, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4956 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4957 fprintf( file
, "\n");
4958 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4959 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4960 fprintf( file
, " __filter_func filter; \\\n");
4961 fprintf( file
, " __finally_func finally; \\\n");
4962 fprintf( file
, " sigjmp_buf jmp; \\\n");
4963 fprintf( file
, " DWORD code; \\\n");
4964 fprintf( file
, " unsigned char abnormal_termination; \\\n");
4965 fprintf( file
, " unsigned char filter_level; \\\n");
4966 fprintf( file
, " unsigned char finally_level;\n");
4967 fprintf( file
, "\n");
4968 fprintf( file
, "struct __exception_frame\n{\n");
4969 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
4970 fprintf( file
, "};\n");
4971 fprintf( file
, "\n");
4972 fprintf( file
, "static inline void __widl_unwind_target(void)\n" );
4973 fprintf( file
, "{\n");
4974 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4975 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4976 fprintf( file
, " {\n");
4977 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4978 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4979 fprintf( file
, " __wine_pop_frame( &exc_frame->frame );\n");
4980 fprintf( file
, " }\n");
4981 fprintf( file
, " exc_frame->filter_level = 0;\n");
4982 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
4983 fprintf( file
, "}\n");
4984 fprintf( file
, "\n");
4985 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4986 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4987 fprintf( file
, " CONTEXT *context,\n");
4988 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4989 fprintf( file
, "{\n");
4990 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4991 fprintf( file
, "\n");
4992 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4993 fprintf( file
, " {\n" );
4994 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4995 fprintf( file
, " {\n" );
4996 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4997 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4998 fprintf( file
, " }\n" );
4999 fprintf( file
, " return ExceptionContinueSearch;\n");
5000 fprintf( file
, " }\n" );
5001 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
5002 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
5003 fprintf( file
, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
5004 fprintf( file
, " return ExceptionContinueSearch;\n");
5005 fprintf( file
, "}\n");
5006 fprintf( file
, "\n");
5007 fprintf( file
, "#define RpcTryExcept \\\n");
5008 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
5009 fprintf( file
, " { \\\n");
5010 fprintf( file
, " if (!__frame->finally_level) \\\n" );
5011 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
5012 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
5013 fprintf( file
, "\n");
5014 fprintf( file
, "#define RpcExcept(expr) \\\n");
5015 fprintf( file
, " if (!__frame->finally_level) \\\n" );
5016 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
5017 fprintf( file
, " __frame->filter_level = 0; \\\n" );
5018 fprintf( file
, " } \\\n");
5019 fprintf( file
, " else \\\n");
5020 fprintf( file
, "\n");
5021 fprintf( file
, "#define RpcEndExcept\n");
5022 fprintf( file
, "\n");
5023 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
5024 fprintf( file
, "\n");
5025 fprintf( file
, "#define RpcTryFinally \\\n");
5026 fprintf( file
, " if (!__frame->filter_level) \\\n");
5027 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
5028 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
5029 fprintf( file
, "\n");
5030 fprintf( file
, "#define RpcFinally \\\n");
5031 fprintf( file
, " if (!__frame->filter_level) \\\n");
5032 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
5033 fprintf( file
, " __frame->finally_level = 0;\n");
5034 fprintf( file
, "\n");
5035 fprintf( file
, "#define RpcEndFinally\n");
5036 fprintf( file
, "\n");
5037 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
5038 fprintf( file
, "\n");
5039 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5040 fprintf( file
, " do { \\\n");
5041 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
5042 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
5043 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
5044 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
5045 fprintf( file
, " __frame->filter_level = 0; \\\n");
5046 fprintf( file
, " __frame->finally_level = 0; \\\n");
5047 fprintf( file
, " } while (0)\n");
5048 fprintf( file
, "\n");
5049 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
5050 fprintf( file
, "\n");
5051 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
5052 fprintf( file
, " do { (void)(filter_func); } while(0)\n");
5053 fprintf( file
, "\n");
5054 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
5055 fprintf( file
, " DWORD code;\n");
5056 fprintf( file
, "\n");
5057 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");