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 var_t
*current_func
;
50 static const type_t
*current_structure
;
51 static const type_t
*current_iface
;
53 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
54 struct expr_eval_routine
57 const type_t
*structure
;
62 static unsigned int field_memsize(const type_t
*type
, unsigned int *offset
);
63 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
);
64 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
65 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
66 const char *name
, int write_ptr
, unsigned int *tfsoff
);
67 static const var_t
*find_array_or_string_in_struct(const type_t
*type
);
68 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
69 type_t
*type
, int toplevel_param
,
70 const char *name
, unsigned int *typestring_offset
);
72 const char *string_of_type(unsigned char type
)
76 case RPC_FC_BYTE
: return "FC_BYTE";
77 case RPC_FC_CHAR
: return "FC_CHAR";
78 case RPC_FC_SMALL
: return "FC_SMALL";
79 case RPC_FC_USMALL
: return "FC_USMALL";
80 case RPC_FC_WCHAR
: return "FC_WCHAR";
81 case RPC_FC_SHORT
: return "FC_SHORT";
82 case RPC_FC_USHORT
: return "FC_USHORT";
83 case RPC_FC_LONG
: return "FC_LONG";
84 case RPC_FC_ULONG
: return "FC_ULONG";
85 case RPC_FC_FLOAT
: return "FC_FLOAT";
86 case RPC_FC_HYPER
: return "FC_HYPER";
87 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
88 case RPC_FC_ENUM16
: return "FC_ENUM16";
89 case RPC_FC_ENUM32
: return "FC_ENUM32";
90 case RPC_FC_IGNORE
: return "FC_IGNORE";
91 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
92 case RPC_FC_RP
: return "FC_RP";
93 case RPC_FC_UP
: return "FC_UP";
94 case RPC_FC_OP
: return "FC_OP";
95 case RPC_FC_FP
: return "FC_FP";
96 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
97 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
98 case RPC_FC_STRUCT
: return "FC_STRUCT";
99 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
100 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
101 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
102 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
103 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
104 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
105 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
106 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
107 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
108 case RPC_FC_CARRAY
: return "FC_CARRAY";
109 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
110 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
111 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
112 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
113 case RPC_FC_POINTER
: return "FC_POINTER";
114 case RPC_FC_C_CSTRING
: return "FC_C_CSTRING";
115 case RPC_FC_C_WSTRING
: return "FC_C_WSTRING";
116 case RPC_FC_CSTRING
: return "FC_CSTRING";
117 case RPC_FC_WSTRING
: return "FC_WSTRING";
119 error("string_of_type: unknown type 0x%02x\n", type
);
124 unsigned char get_basic_fc(const type_t
*type
)
126 int sign
= type_basic_get_sign(type
);
127 switch (type_basic_get_type(type
))
129 case TYPE_BASIC_INT8
: return (sign
<= 0 ? RPC_FC_SMALL
: RPC_FC_USMALL
);
130 case TYPE_BASIC_INT16
: return (sign
<= 0 ? RPC_FC_SHORT
: RPC_FC_USHORT
);
131 case TYPE_BASIC_INT32
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
132 case TYPE_BASIC_INT64
: return RPC_FC_HYPER
;
133 case TYPE_BASIC_INT
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
134 case TYPE_BASIC_BYTE
: return RPC_FC_BYTE
;
135 case TYPE_BASIC_CHAR
: return RPC_FC_CHAR
;
136 case TYPE_BASIC_WCHAR
: return RPC_FC_WCHAR
;
137 case TYPE_BASIC_HYPER
: return RPC_FC_HYPER
;
138 case TYPE_BASIC_FLOAT
: return RPC_FC_FLOAT
;
139 case TYPE_BASIC_DOUBLE
: return RPC_FC_DOUBLE
;
140 case TYPE_BASIC_ERROR_STATUS_T
: return RPC_FC_ERROR_STATUS_T
;
141 case TYPE_BASIC_HANDLE
: return RPC_FC_BIND_PRIMITIVE
;
146 unsigned char get_pointer_fc(const type_t
*type
, const attr_list_t
*attrs
, int toplevel_param
)
151 assert(is_ptr(type
) || is_array(type
));
153 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
157 for (t
= type
; type_is_alias(t
); t
= type_alias_get_aliasee(t
))
159 pointer_type
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
166 else if (is_ptr(type
))
167 return type_pointer_get_default_fc(type
);
169 return type_array_get_ptr_default_fc(type
);
172 static unsigned char get_enum_fc(const type_t
*type
)
174 assert(type_get_type(type
) == TYPE_ENUM
);
175 if (is_aliaschain_attr(type
, ATTR_V1ENUM
))
176 return RPC_FC_ENUM32
;
178 return RPC_FC_ENUM16
;
181 enum typegen_type
typegen_detect_type(const type_t
*type
, const attr_list_t
*attrs
, unsigned int flags
)
183 if (is_user_type(type
))
184 return TGT_USER_TYPE
;
186 if (is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
))
187 return TGT_CTXT_HANDLE
;
189 if (!(flags
& TDT_IGNORE_STRINGS
) && is_string_type(attrs
, type
))
192 switch (type_get_type(type
))
199 if (type_get_type(type_pointer_get_ref(type
)) == TYPE_INTERFACE
||
200 (type_get_type(type_pointer_get_ref(type
)) == TYPE_VOID
&& is_attr(attrs
, ATTR_IIDIS
)))
201 return TGT_IFACE_POINTER
;
202 else if (is_aliaschain_attr(type_pointer_get_ref(type
), ATTR_CONTEXTHANDLE
))
203 return TGT_CTXT_HANDLE_POINTER
;
208 case TYPE_ENCAPSULATED_UNION
:
224 unsigned char get_struct_fc(const type_t
*type
)
227 int has_conformance
= 0;
228 int has_variance
= 0;
232 fields
= type_struct_get_fields(type
);
234 if (get_padding(fields
))
235 return RPC_FC_BOGUS_STRUCT
;
237 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, var_t
, entry
)
239 type_t
*t
= field
->type
;
240 enum typegen_type typegen_type
;
242 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
244 if (typegen_type
== TGT_ARRAY
&& !type_array_is_decl_as_ptr(t
))
246 if (is_string_type(field
->attrs
, field
->type
))
248 if (is_conformant_array(t
))
254 if (is_array(type_array_get_element(field
->type
)))
255 return RPC_FC_BOGUS_STRUCT
;
257 if (type_array_has_conformance(field
->type
))
260 if (list_next(fields
, &field
->entry
))
261 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
264 if (type_array_has_variance(t
))
267 t
= type_array_get_element(t
);
268 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
271 switch (typegen_type
)
274 case TGT_IFACE_POINTER
:
275 return RPC_FC_BOGUS_STRUCT
;
279 if (get_enum_fc(t
) == RPC_FC_ENUM16
)
280 return RPC_FC_BOGUS_STRUCT
;
284 if (get_pointer_fc(t
, field
->attrs
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
285 return RPC_FC_BOGUS_STRUCT
;
289 return RPC_FC_BOGUS_STRUCT
;
292 unsigned char fc
= get_struct_fc(t
);
297 case RPC_FC_CVSTRUCT
:
303 case RPC_FC_CPSTRUCT
:
305 if (list_next( fields
, &field
->entry
))
306 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
313 if (list_next( fields
, &field
->entry
))
314 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
323 error_loc("Unknown struct member %s with type (0x%02x)\n", field
->name
, fc
);
324 /* fallthru - treat it as complex */
326 /* as soon as we see one of these these members, it's bogus... */
327 case RPC_FC_BOGUS_STRUCT
:
328 return RPC_FC_BOGUS_STRUCT
;
333 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
335 case TGT_CTXT_HANDLE
:
336 case TGT_CTXT_HANDLE_POINTER
:
337 /* checking after parsing should mean that we don't get here. if we do,
338 * it's a checker bug */
345 if ( has_conformance
)
346 return RPC_FC_CVSTRUCT
;
348 return RPC_FC_BOGUS_STRUCT
;
350 if( has_conformance
&& has_pointer
)
351 return RPC_FC_CPSTRUCT
;
352 if( has_conformance
)
353 return RPC_FC_CSTRUCT
;
355 return RPC_FC_PSTRUCT
;
356 return RPC_FC_STRUCT
;
359 unsigned char get_array_fc(const type_t
*type
)
362 const expr_t
*size_is
;
363 const type_t
*elem_type
;
365 elem_type
= type_array_get_element(type
);
366 size_is
= type_array_get_conformance(type
);
370 unsigned int align
= 0;
371 unsigned int size
= type_memsize(elem_type
, &align
);
372 if (size
* type_array_get_dim(type
) > 0xffffuL
)
373 fc
= RPC_FC_LGFARRAY
;
375 fc
= RPC_FC_SMFARRAY
;
380 if (type_array_has_variance(type
))
382 if (fc
== RPC_FC_SMFARRAY
)
383 fc
= RPC_FC_SMVARRAY
;
384 else if (fc
== RPC_FC_LGFARRAY
)
385 fc
= RPC_FC_LGVARRAY
;
386 else if (fc
== RPC_FC_CARRAY
)
390 switch (typegen_detect_type(elem_type
, NULL
, TDT_IGNORE_STRINGS
))
393 fc
= RPC_FC_BOGUS_ARRAY
;
396 switch (get_struct_fc(elem_type
))
398 case RPC_FC_BOGUS_STRUCT
:
399 fc
= RPC_FC_BOGUS_ARRAY
;
404 /* is 16-bit enum - if so, wire size differs from mem size and so
405 * the array cannot be block copied, which means the array is complex */
406 if (get_enum_fc(elem_type
) == RPC_FC_ENUM16
)
407 fc
= RPC_FC_BOGUS_ARRAY
;
410 case TGT_IFACE_POINTER
:
411 fc
= RPC_FC_BOGUS_ARRAY
;
414 /* ref pointers cannot just be block copied. unique pointers to
415 * interfaces need special treatment. either case means the array is
417 if (get_pointer_fc(elem_type
, NULL
, FALSE
) == RPC_FC_RP
)
418 fc
= RPC_FC_BOGUS_ARRAY
;
421 case TGT_CTXT_HANDLE
:
422 case TGT_CTXT_HANDLE_POINTER
:
426 /* nothing to do for everything else */
433 int is_struct(unsigned char type
)
440 case RPC_FC_CPSTRUCT
:
441 case RPC_FC_CVSTRUCT
:
442 case RPC_FC_BOGUS_STRUCT
:
449 static int is_non_complex_struct(const type_t
*type
)
451 return (type_get_type(type
) == TYPE_STRUCT
&&
452 get_struct_fc(type
) != RPC_FC_BOGUS_STRUCT
);
455 static int type_has_pointers(const type_t
*type
)
457 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
464 /* FIXME: array can be pointer */
465 return type_has_pointers(type_array_get_element(type
));
468 var_list_t
*fields
= type_struct_get_fields(type
);
470 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
472 if (type_has_pointers(field
->type
))
481 fields
= type_union_get_cases(type
);
482 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
484 if (field
->type
&& type_has_pointers(field
->type
))
489 case TGT_CTXT_HANDLE
:
490 case TGT_CTXT_HANDLE_POINTER
:
492 case TGT_IFACE_POINTER
:
502 static int type_has_full_pointer(const type_t
*type
, const attr_list_t
*attrs
,
505 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
510 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
515 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
518 return type_has_full_pointer(type_array_get_element(type
), NULL
, FALSE
);
521 var_list_t
*fields
= type_struct_get_fields(type
);
523 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
525 if (type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
534 fields
= type_union_get_cases(type
);
535 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
537 if (field
->type
&& type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
542 case TGT_CTXT_HANDLE
:
543 case TGT_CTXT_HANDLE_POINTER
:
545 case TGT_IFACE_POINTER
:
555 static unsigned short user_type_offset(const char *name
)
558 unsigned short off
= 0;
559 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
561 if (strcmp(name
, ut
->name
) == 0)
565 error("user_type_offset: couldn't find type (%s)\n", name
);
569 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
571 type
->typestring_offset
= offset
;
572 if (file
) type
->tfswrite
= FALSE
;
575 static void guard_rec(type_t
*type
)
577 /* types that contain references to themselves (like a linked list),
578 need to be shielded from infinite recursion when writing embedded
580 if (type
->typestring_offset
)
581 type
->tfswrite
= FALSE
;
583 type
->typestring_offset
= 1;
586 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
590 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
598 if (type_is_alias(t
))
599 t
= type_alias_get_aliasee(t
);
605 int is_user_type(const type_t
*t
)
607 return get_user_type(t
, NULL
) != NULL
;
610 static int is_embedded_complex(const type_t
*type
)
612 switch (typegen_detect_type(type
, NULL
, TDT_ALL_TYPES
))
618 case TGT_IFACE_POINTER
:
625 static const char *get_context_handle_type_name(const type_t
*type
)
629 is_ptr(t
) || type_is_alias(t
);
630 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
631 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
637 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
640 fprintf(file, "/* %2u */\n", typestring_offset); \
641 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
645 static void print_file(FILE *file
, int indent
, const char *format
, ...) __attribute__((format (printf
, 3, 4)));
646 static void print_file(FILE *file
, int indent
, const char *format
, ...)
649 va_start(va
, format
);
650 print(file
, indent
, format
, va
);
654 void print(FILE *file
, int indent
, const char *format
, va_list va
)
658 if (format
[0] != '\n')
661 vfprintf(file
, format
, va
);
666 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
, const char *local_var_prefix
)
668 if (decl_indirect(t
))
670 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
671 local_var_prefix
, n
, local_var_prefix
, n
);
672 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
674 else if (is_ptr(t
) || is_array(t
))
675 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
678 void write_parameters_init(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
682 if (!is_void(type_function_get_rettype(func
->type
)))
683 write_var_init(file
, indent
, type_function_get_rettype(func
->type
), "_RetVal", local_var_prefix
);
685 if (!type_get_function_args(func
->type
))
688 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
689 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
694 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
696 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
697 print_file(f
, indent
, "{\n");
698 print_file(f
, indent
+ 1, "short Pad;\n");
699 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
700 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
701 print_file(f
, indent
, "\n");
704 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
708 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
709 get_size_typeformatstring(stmts
, pred
));
711 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
712 get_size_procformatstring(stmts
, pred
));
715 write_formatdesc(f
, indent
, "TYPE");
716 write_formatdesc(f
, indent
, "PROC");
718 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
719 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
720 print_file(f
, indent
, "\n");
723 int decl_indirect(const type_t
*t
)
727 return (type_get_type(t
) != TYPE_BASIC
&&
728 type_get_type(t
) != TYPE_ENUM
&&
729 type_get_type(t
) != TYPE_POINTER
&&
730 type_get_type(t
) != TYPE_ARRAY
);
733 static unsigned int write_procformatstring_type(FILE *file
, int indent
,
736 const attr_list_t
*attrs
,
741 int is_in
= is_attr(attrs
, ATTR_IN
);
742 int is_out
= is_attr(attrs
, ATTR_OUT
);
744 if (!is_in
&& !is_out
) is_in
= TRUE
;
746 if (type_get_type(type
) == TYPE_BASIC
||
747 type_get_type(type
) == TYPE_ENUM
)
752 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
754 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
756 if (type_get_type(type
) == TYPE_ENUM
)
758 fc
= get_enum_fc(type
);
762 fc
= get_basic_fc(type
);
764 if (fc
== RPC_FC_BIND_PRIMITIVE
)
768 print_file(file
, indent
, "0x%02x, /* %s */\n",
769 fc
, string_of_type(fc
));
770 size
= 2; /* includes param type prefix */
775 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
776 else if (is_in
&& is_out
)
777 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
779 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
781 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
783 print_file(file
, indent
, "0x01,\n");
784 print_file(file
, indent
, "NdrFcShort(0x%hx),\n", type
->typestring_offset
);
785 size
= 4; /* includes param type prefix */
790 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
792 const statement_t
*stmt
;
793 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
795 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
797 const statement_t
*stmt_func
;
798 if (!pred(stmt
->u
.type
))
800 STATEMENTS_FOR_EACH_FUNC(stmt_func
, type_iface_get_stmts(stmt
->u
.type
))
802 const var_t
*func
= stmt_func
->u
.var
;
803 if (is_local(func
->attrs
)) continue;
804 /* emit argument data */
805 if (type_get_function_args(func
->type
))
808 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
809 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
812 /* emit return value data */
813 if (is_void(type_function_get_rettype(func
->type
)))
815 print_file(file
, indent
, "0x5b, /* FC_END */\n");
816 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
819 write_procformatstring_type(file
, indent
, "return value", type_function_get_rettype(func
->type
), NULL
, TRUE
);
822 else if (stmt
->type
== STMT_LIBRARY
)
823 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
827 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
831 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
832 print_file(file
, indent
, "{\n");
834 print_file(file
, indent
, "0,\n");
835 print_file(file
, indent
, "{\n");
838 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
840 print_file(file
, indent
, "0x0\n");
842 print_file(file
, indent
, "}\n");
844 print_file(file
, indent
, "};\n");
845 print_file(file
, indent
, "\n");
848 static int write_base_type(FILE *file
, const type_t
*type
, int convert_to_signed_type
, unsigned int *typestring_offset
)
852 if (type_get_type(type
) == TYPE_BASIC
)
853 fc
= get_basic_fc(type
);
854 else if (type_get_type(type
) == TYPE_ENUM
)
855 fc
= get_enum_fc(type
);
859 if (convert_to_signed_type
)
875 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
876 *typestring_offset
+= 1;
880 /* write conformance / variance descriptor */
881 static unsigned int write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
882 unsigned int baseoff
, const type_t
*type
,
885 unsigned char operator_type
= 0;
886 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
887 const char *conftype_string
= "";
888 const char *operator_string
= "no operators";
889 const expr_t
*subexpr
;
893 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
899 /* Top-level conformance calculations are done inline. */
900 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
901 RPC_FC_TOP_LEVEL_CONFORMANCE
);
902 print_file (file
, 2, "0x0,\n");
903 print_file (file
, 2, "NdrFcShort(0x0),\n");
909 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
910 error("write_conf_or_var_desc: constant value %ld is greater than "
911 "the maximum constant size of %d\n", expr
->cval
,
912 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
914 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
915 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
916 print_file(file
, 2, "0x%lx,\n", expr
->cval
>> 16);
917 print_file(file
, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr
->cval
);
922 if (is_ptr(type
) || (is_array(type
) && type_array_is_decl_as_ptr(type
)))
924 conftype
= RPC_FC_POINTER_CONFORMANCE
;
925 conftype_string
= "field pointer, ";
929 switch (subexpr
->type
)
932 subexpr
= subexpr
->ref
;
933 operator_type
= RPC_FC_DEREFERENCE
;
934 operator_string
= "FC_DEREFERENCE";
937 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
939 subexpr
= subexpr
->ref
;
940 operator_type
= RPC_FC_DIV_2
;
941 operator_string
= "FC_DIV_2";
945 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
947 subexpr
= subexpr
->ref
;
948 operator_type
= RPC_FC_MULT_2
;
949 operator_string
= "FC_MULT_2";
953 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
955 subexpr
= subexpr
->ref
;
956 operator_type
= RPC_FC_SUB_1
;
957 operator_string
= "FC_SUB_1";
961 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
963 subexpr
= subexpr
->ref
;
964 operator_type
= RPC_FC_ADD_1
;
965 operator_string
= "FC_ADD_1";
972 if (subexpr
->type
== EXPR_IDENTIFIER
)
974 const type_t
*correlation_variable
= NULL
;
975 unsigned char param_type
= 0;
976 unsigned int offset
= 0;
978 var_list_t
*fields
= type_struct_get_fields(structure
);
980 if (fields
) LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
982 unsigned int size
= field_memsize( var
->type
, &offset
);
983 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
985 correlation_variable
= var
->type
;
990 if (!correlation_variable
)
991 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
994 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
998 if (type_get_type(correlation_variable
) == TYPE_BASIC
)
1000 switch (get_basic_fc(correlation_variable
))
1004 param_type
= RPC_FC_SMALL
;
1008 param_type
= RPC_FC_USMALL
;
1012 param_type
= RPC_FC_SHORT
;
1015 param_type
= RPC_FC_USHORT
;
1018 param_type
= RPC_FC_LONG
;
1021 param_type
= RPC_FC_ULONG
;
1024 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1025 get_basic_fc(correlation_variable
));
1028 else if (type_get_type(correlation_variable
) == TYPE_ENUM
)
1030 if (get_enum_fc(correlation_variable
) == RPC_FC_ENUM32
)
1031 param_type
= RPC_FC_LONG
;
1033 param_type
= RPC_FC_SHORT
;
1037 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1042 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
1043 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
1044 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
1045 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1050 unsigned int callback_offset
= 0;
1051 struct expr_eval_routine
*eval
;
1054 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
1056 if (!strcmp (eval
->structure
->name
, structure
->name
)
1057 && !compare_expr (eval
->expr
, expr
))
1067 eval
= xmalloc (sizeof(*eval
));
1068 eval
->structure
= structure
;
1069 eval
->baseoff
= baseoff
;
1071 list_add_tail (&expr_eval_routines
, &eval
->entry
);
1074 if (callback_offset
> USHRT_MAX
)
1075 error("Maximum number of callback routines reached\n");
1077 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
1078 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
1079 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset
, callback_offset
);
1084 /* return size and start offset of a data field based on current offset */
1085 static unsigned int field_memsize(const type_t
*type
, unsigned int *offset
)
1087 unsigned int align
= 0;
1088 unsigned int size
= type_memsize( type
, &align
);
1090 *offset
= ROUND_SIZE( *offset
, align
);
1094 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
)
1096 unsigned int size
= 0;
1099 if (!fields
) return 0;
1100 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1102 unsigned int falign
= 0;
1103 unsigned int fsize
= type_memsize(v
->type
, &falign
);
1104 if (*align
< falign
) *align
= falign
;
1105 size
= ROUND_SIZE(size
, falign
);
1109 size
= ROUND_SIZE(size
, *align
);
1113 static unsigned int union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
1115 unsigned int size
, maxs
= 0;
1116 unsigned int align
= *pmaxa
;
1119 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1121 /* we could have an empty default field with NULL type */
1124 size
= type_memsize(v
->type
, &align
);
1125 if (maxs
< size
) maxs
= size
;
1126 if (*pmaxa
< align
) *pmaxa
= align
;
1133 int get_padding(const var_list_t
*fields
)
1135 unsigned short offset
= 0;
1136 unsigned int salign
= 1;
1142 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
1144 type_t
*ft
= f
->type
;
1145 unsigned int align
= 0;
1146 unsigned int size
= type_memsize(ft
, &align
);
1147 if (align
> salign
) salign
= align
;
1148 offset
= ROUND_SIZE(offset
, align
);
1152 return ROUNDING(offset
, salign
);
1155 unsigned int type_memsize(const type_t
*t
, unsigned int *align
)
1157 unsigned int size
= 0;
1159 switch (type_get_type(t
))
1162 switch (get_basic_fc(t
))
1169 if (size
> *align
) *align
= size
;
1175 if (size
> *align
) *align
= size
;
1179 case RPC_FC_ERROR_STATUS_T
:
1182 if (size
> *align
) *align
= size
;
1187 if (size
> *align
) *align
= size
;
1190 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t
));
1195 switch (get_enum_fc(t
))
1200 if (size
> *align
) *align
= size
;
1203 error("type_memsize: Unknown enum type\n");
1208 size
= fields_memsize(type_struct_get_fields(t
), align
);
1210 case TYPE_ENCAPSULATED_UNION
:
1211 size
= fields_memsize(type_encapsulated_union_get_fields(t
), align
);
1214 size
= union_memsize(type_union_get_cases(t
), align
);
1217 assert( pointer_size
);
1218 size
= pointer_size
;
1219 if (size
> *align
) *align
= size
;
1222 if (!type_array_is_decl_as_ptr(t
))
1224 if (is_conformant_array(t
))
1226 type_memsize(type_array_get_element(t
), align
);
1230 size
= type_array_get_dim(t
) *
1231 type_memsize(type_array_get_element(t
), align
);
1233 else /* declared as a pointer */
1235 assert( pointer_size
);
1236 size
= pointer_size
;
1237 if (size
> *align
) *align
= size
;
1240 case TYPE_INTERFACE
:
1246 /* these types should not be encountered here due to language
1247 * restrictions (interface, void, coclass, module), logical
1248 * restrictions (alias - due to type_get_type call above) or
1249 * checking restrictions (function). */
1256 int is_full_pointer_function(const var_t
*func
)
1259 if (type_has_full_pointer(type_function_get_rettype(func
->type
), func
->attrs
, TRUE
))
1261 if (!type_get_function_args(func
->type
))
1263 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
1264 if (type_has_full_pointer( var
->type
, var
->attrs
, TRUE
))
1269 void write_full_pointer_init(FILE *file
, int indent
, const var_t
*func
, int is_server
)
1271 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1272 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
1273 fprintf(file
, "\n");
1276 void write_full_pointer_free(FILE *file
, int indent
, const var_t
*func
)
1278 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1279 fprintf(file
, "\n");
1282 static unsigned int write_nonsimple_pointer(FILE *file
, const attr_list_t
*attrs
,
1285 unsigned int offset
,
1286 unsigned int *typeformat_offset
)
1288 unsigned int start_offset
= *typeformat_offset
;
1289 short reloff
= offset
- (*typeformat_offset
+ 2);
1290 int in_attr
, out_attr
;
1292 unsigned char flags
= 0;
1294 pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
1296 in_attr
= is_attr(attrs
, ATTR_IN
);
1297 out_attr
= is_attr(attrs
, ATTR_OUT
);
1298 if (!in_attr
&& !out_attr
) in_attr
= 1;
1300 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1301 flags
|= RPC_FC_P_ONSTACK
;
1303 if (is_ptr(type
) && !last_ptr(type
))
1304 flags
|= RPC_FC_P_DEREF
;
1306 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1309 string_of_type(pointer_type
));
1312 if (flags
& RPC_FC_P_ONSTACK
)
1313 fprintf(file
, " [allocated_on_stack]");
1314 if (flags
& RPC_FC_P_DEREF
)
1315 fprintf(file
, " [pointer_deref]");
1316 fprintf(file
, " */\n");
1319 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, offset
);
1320 *typeformat_offset
+= 4;
1322 return start_offset
;
1325 static unsigned int write_simple_pointer(FILE *file
, const attr_list_t
*attrs
, const type_t
*type
, int toplevel_param
)
1328 unsigned char pointer_fc
;
1331 /* for historical reasons, write_simple_pointer also handled string types,
1332 * but no longer does. catch bad uses of the function with this check */
1333 if (is_string_type(attrs
, type
))
1334 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
1336 pointer_fc
= get_pointer_fc(type
, attrs
, toplevel_param
);
1338 ref
= type_pointer_get_ref(type
);
1339 if (type_get_type(ref
) == TYPE_ENUM
)
1340 fc
= get_enum_fc(ref
);
1342 fc
= get_basic_fc(ref
);
1344 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1345 pointer_fc
, RPC_FC_P_SIMPLEPOINTER
, string_of_type(pointer_fc
));
1346 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1347 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1351 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
1353 print_file(file
, 0, "/* %u (", tfsoff
);
1354 write_type_decl(file
, t
, NULL
);
1355 print_file(file
, 0, ") */\n");
1358 static unsigned int write_pointer_tfs(FILE *file
, const attr_list_t
*attrs
,
1359 type_t
*type
, int toplevel_param
,
1360 unsigned int *typestring_offset
)
1362 unsigned int offset
= *typestring_offset
;
1363 type_t
*ref
= type_pointer_get_ref(type
);
1365 print_start_tfs_comment(file
, type
, offset
);
1366 update_tfsoff(type
, offset
, file
);
1368 if (ref
->typestring_offset
)
1369 write_nonsimple_pointer(file
, attrs
, type
,
1371 type_pointer_get_ref(type
)->typestring_offset
,
1373 else if (type_get_type(ref
) == TYPE_BASIC
||
1374 type_get_type(ref
) == TYPE_ENUM
)
1375 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
,
1381 static int processed(const type_t
*type
)
1383 return type
->typestring_offset
&& !type
->tfswrite
;
1386 static int user_type_has_variable_size(const type_t
*t
)
1390 else if (type_get_type(t
) == TYPE_STRUCT
)
1392 switch (get_struct_fc(t
))
1394 case RPC_FC_PSTRUCT
:
1395 case RPC_FC_CSTRUCT
:
1396 case RPC_FC_CPSTRUCT
:
1397 case RPC_FC_CVSTRUCT
:
1401 /* Note: Since this only applies to user types, we can't have a conformant
1402 array here, and strings should get filed under pointer in this case. */
1406 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1408 unsigned int start
, absoff
, flags
;
1409 unsigned int align
= 0, ualign
= 0;
1410 const char *name
= NULL
;
1411 type_t
*utype
= get_user_type(type
, &name
);
1412 unsigned int usize
= user_type_has_variable_size(utype
) ? 0 : type_memsize(utype
, &ualign
);
1413 unsigned int size
= type_memsize(type
, &align
);
1414 unsigned short funoff
= user_type_offset(name
);
1419 if (type_get_type(utype
) == TYPE_BASIC
||
1420 type_get_type(utype
) == TYPE_ENUM
)
1424 if (type_get_type(utype
) == TYPE_ENUM
)
1425 fc
= get_enum_fc(utype
);
1427 fc
= get_basic_fc(utype
);
1430 print_start_tfs_comment(file
, utype
, absoff
);
1431 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1432 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1437 if (!processed(utype
))
1438 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1439 absoff
= utype
->typestring_offset
;
1442 if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_RP
)
1444 else if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_UP
)
1450 update_tfsoff(type
, start
, file
);
1451 print_start_tfs_comment(file
, type
, start
);
1452 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1453 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1454 flags
| (align
- 1), align
- 1, flags
);
1455 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1456 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
1457 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize
, usize
);
1459 reloff
= absoff
- *tfsoff
;
1460 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, absoff
);
1464 static void write_member_type(FILE *file
, const type_t
*cont
,
1465 int cont_is_complex
, const attr_list_t
*attrs
,
1466 const type_t
*type
, unsigned int *corroff
,
1467 unsigned int *tfsoff
)
1469 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1471 unsigned int absoff
;
1474 if (type_get_type(type
) == TYPE_UNION
&& is_attr(attrs
, ATTR_SWITCHIS
))
1481 absoff
= type
->typestring_offset
;
1483 reloff
= absoff
- (*tfsoff
+ 2);
1485 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1486 /* FIXME: actually compute necessary padding */
1487 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1488 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1489 reloff
, reloff
, absoff
);
1492 else if (is_ptr(type
) || is_conformant_array(type
))
1494 unsigned char fc
= cont_is_complex
? RPC_FC_POINTER
: RPC_FC_LONG
;
1495 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1498 else if (!write_base_type(file
, type
, TRUE
, tfsoff
))
1499 error("Unsupported member type %d\n", type_get_type(type
));
1502 static void write_end(FILE *file
, unsigned int *tfsoff
)
1504 if (*tfsoff
% 2 == 0)
1506 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1509 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1513 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1515 unsigned int offset
= 0;
1516 var_list_t
*fs
= type_struct_get_fields(type
);
1519 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1521 type_t
*ft
= f
->type
;
1522 unsigned int size
= field_memsize( ft
, &offset
);
1523 if (type_get_type(ft
) == TYPE_UNION
&& is_attr(f
->attrs
, ATTR_SWITCHIS
))
1526 unsigned int absoff
= ft
->typestring_offset
;
1527 if (is_attr(ft
->attrs
, ATTR_SWITCHTYPE
))
1528 absoff
+= 8; /* we already have a corr descr, skip it */
1529 reloff
= absoff
- (*tfsoff
+ 6);
1530 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1531 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
1532 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1533 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1534 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1535 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1536 reloff
, reloff
, absoff
);
1543 static int write_no_repeat_pointer_descriptions(
1544 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1545 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1546 unsigned int *typestring_offset
)
1552 (is_conformant_array(type
) && type_array_is_decl_as_ptr(type
)))
1554 unsigned int memsize
;
1556 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1557 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1559 /* pointer instance */
1560 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1561 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1562 *typestring_offset
+= 6;
1566 if (is_string_type(attrs
, type
))
1567 write_string_tfs(file
, attrs
, type
, FALSE
, NULL
, typestring_offset
);
1569 write_pointer_tfs(file
, attrs
, type
, FALSE
, typestring_offset
);
1573 unsigned int offset
= type
->typestring_offset
;
1574 /* skip over the pointer that is written for strings, since a
1575 * pointer has to be written in-place here */
1576 if (is_string_type(attrs
, type
))
1578 write_nonsimple_pointer(file
, attrs
, type
, FALSE
, offset
, typestring_offset
);
1582 memsize
= type_memsize(type
, &align
);
1583 *offset_in_memory
+= memsize
;
1584 /* increment these separately as in the case of conformant (varying)
1585 * structures these start at different values */
1586 *offset_in_buffer
+= memsize
;
1591 if (is_non_complex_struct(type
))
1594 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1596 if (offset_in_memory
&& offset_in_buffer
)
1598 unsigned int padding
;
1600 type_memsize(v
->type
, &align
);
1601 padding
= ROUNDING(*offset_in_memory
, align
);
1602 *offset_in_memory
+= padding
;
1603 *offset_in_buffer
+= padding
;
1605 written
+= write_no_repeat_pointer_descriptions(
1606 file
, v
->attrs
, v
->type
,
1607 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1612 unsigned int memsize
;
1614 memsize
= type_memsize(type
, &align
);
1615 *offset_in_memory
+= memsize
;
1616 /* increment these separately as in the case of conformant (varying)
1617 * structures these start at different values */
1618 *offset_in_buffer
+= memsize
;
1624 static int write_pointer_description_offsets(
1625 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1626 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1627 unsigned int *typestring_offset
)
1632 if (is_ptr(type
) && type_get_type(type_pointer_get_ref(type
)) != TYPE_INTERFACE
)
1634 type_t
*ref
= type_pointer_get_ref(type
);
1636 if (offset_in_memory
&& offset_in_buffer
)
1638 unsigned int memsize
;
1640 /* pointer instance */
1641 /* FIXME: sometimes from end of structure, sometimes from beginning */
1642 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1643 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1646 memsize
= type_memsize(type
, &align
);
1647 *offset_in_memory
+= memsize
;
1648 /* increment these separately as in the case of conformant (varying)
1649 * structures these start at different values */
1650 *offset_in_buffer
+= memsize
;
1652 *typestring_offset
+= 4;
1654 if (is_string_type(attrs
, type
))
1655 write_string_tfs(file
, attrs
, type
, FALSE
, NULL
, typestring_offset
);
1656 else if (processed(ref
) || type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
)
1657 write_pointer_tfs(file
, attrs
, type
, FALSE
, typestring_offset
);
1659 error("write_pointer_description_offsets: type format string unknown\n");
1666 return write_pointer_description_offsets(
1667 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1668 offset_in_buffer
, typestring_offset
);
1670 else if (is_non_complex_struct(type
))
1672 /* otherwise search for interesting fields to parse */
1674 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1676 if (offset_in_memory
&& offset_in_buffer
)
1678 unsigned int padding
;
1680 type_memsize(v
->type
, &align
);
1681 padding
= ROUNDING(*offset_in_memory
, align
);
1682 *offset_in_memory
+= padding
;
1683 *offset_in_buffer
+= padding
;
1685 written
+= write_pointer_description_offsets(
1686 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1692 if (offset_in_memory
&& offset_in_buffer
)
1694 unsigned int memsize
;
1696 memsize
= type_memsize(type
, &align
);
1697 *offset_in_memory
+= memsize
;
1698 /* increment these separately as in the case of conformant (varying)
1699 * structures these start at different values */
1700 *offset_in_buffer
+= memsize
;
1707 /* Note: if file is NULL return value is number of pointers to write, else
1708 * it is the number of type format characters written */
1709 static int write_fixed_array_pointer_descriptions(
1710 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1711 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1712 unsigned int *typestring_offset
)
1715 int pointer_count
= 0;
1717 if (type_get_type(type
) == TYPE_ARRAY
&&
1718 !type_array_has_conformance(type
) && !type_array_has_variance(type
))
1720 unsigned int temp
= 0;
1721 /* unfortunately, this needs to be done in two passes to avoid
1722 * writing out redundant FC_FIXED_REPEAT descriptions */
1723 pointer_count
= write_pointer_description_offsets(
1724 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1725 if (pointer_count
> 0)
1727 unsigned int increment_size
;
1728 unsigned int offset_of_array_pointer_mem
= 0;
1729 unsigned int offset_of_array_pointer_buf
= 0;
1732 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1734 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1735 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1736 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type
), type_array_get_dim(type
));
1737 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1738 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1739 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1740 *typestring_offset
+= 10;
1742 pointer_count
= write_pointer_description_offsets(
1743 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1744 &offset_of_array_pointer_buf
, typestring_offset
);
1747 else if (type_get_type(type
) == TYPE_STRUCT
)
1750 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1752 if (offset_in_memory
&& offset_in_buffer
)
1754 unsigned int padding
;
1756 type_memsize(v
->type
, &align
);
1757 padding
= ROUNDING(*offset_in_memory
, align
);
1758 *offset_in_memory
+= padding
;
1759 *offset_in_buffer
+= padding
;
1761 pointer_count
+= write_fixed_array_pointer_descriptions(
1762 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1768 if (offset_in_memory
&& offset_in_buffer
)
1770 unsigned int memsize
;
1772 memsize
= type_memsize(type
, &align
);
1773 *offset_in_memory
+= memsize
;
1774 /* increment these separately as in the case of conformant (varying)
1775 * structures these start at different values */
1776 *offset_in_buffer
+= memsize
;
1780 return pointer_count
;
1783 /* Note: if file is NULL return value is number of pointers to write, else
1784 * it is the number of type format characters written */
1785 static int write_conformant_array_pointer_descriptions(
1786 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1787 unsigned int offset_in_memory
, unsigned int *typestring_offset
)
1790 int pointer_count
= 0;
1792 if (is_conformant_array(type
) && !type_array_has_variance(type
))
1794 unsigned int temp
= 0;
1795 /* unfortunately, this needs to be done in two passes to avoid
1796 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1797 pointer_count
= write_pointer_description_offsets(
1798 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1799 if (pointer_count
> 0)
1801 unsigned int increment_size
;
1802 unsigned int offset_of_array_pointer_mem
= offset_in_memory
;
1803 unsigned int offset_of_array_pointer_buf
= offset_in_memory
;
1806 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1808 if (increment_size
> USHRT_MAX
)
1809 error("array size of %u bytes is too large\n", increment_size
);
1811 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1812 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1813 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1814 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1815 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1816 *typestring_offset
+= 8;
1818 pointer_count
= write_pointer_description_offsets(
1819 file
, attrs
, type_array_get_element(type
),
1820 &offset_of_array_pointer_mem
, &offset_of_array_pointer_buf
,
1825 return pointer_count
;
1828 /* Note: if file is NULL return value is number of pointers to write, else
1829 * it is the number of type format characters written */
1830 static int write_varying_array_pointer_descriptions(
1831 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1832 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1833 unsigned int *typestring_offset
)
1836 int pointer_count
= 0;
1838 if (is_array(type
) && type_array_has_variance(type
))
1840 unsigned int temp
= 0;
1841 /* unfortunately, this needs to be done in two passes to avoid
1842 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1843 pointer_count
= write_pointer_description_offsets(
1844 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1845 if (pointer_count
> 0)
1847 unsigned int increment_size
;
1850 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1852 if (increment_size
> USHRT_MAX
)
1853 error("array size of %u bytes is too large\n", increment_size
);
1855 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1856 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1857 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1858 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1859 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1860 *typestring_offset
+= 8;
1862 pointer_count
= write_pointer_description_offsets(
1863 file
, attrs
, type
, offset_in_memory
,
1864 offset_in_buffer
, typestring_offset
);
1867 else if (type_get_type(type
) == TYPE_STRUCT
)
1870 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1872 if (offset_in_memory
&& offset_in_buffer
)
1874 unsigned int padding
;
1876 if (is_array(v
->type
) && type_array_has_variance(v
->type
))
1878 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
1879 /* skip over variance and offset in buffer */
1880 *offset_in_buffer
+= 8;
1884 type_memsize(v
->type
, &align
);
1885 padding
= ROUNDING(*offset_in_memory
, align
);
1886 *offset_in_memory
+= padding
;
1887 *offset_in_buffer
+= padding
;
1889 pointer_count
+= write_varying_array_pointer_descriptions(
1890 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1896 if (offset_in_memory
&& offset_in_buffer
)
1898 unsigned int memsize
;
1900 memsize
= type_memsize(type
, &align
);
1901 *offset_in_memory
+= memsize
;
1902 /* increment these separately as in the case of conformant (varying)
1903 * structures these start at different values */
1904 *offset_in_buffer
+= memsize
;
1908 return pointer_count
;
1911 static void write_pointer_description(FILE *file
, type_t
*type
,
1912 unsigned int *typestring_offset
)
1914 unsigned int offset_in_buffer
;
1915 unsigned int offset_in_memory
;
1917 /* pass 1: search for single instance of a pointer (i.e. don't descend
1919 if (!is_array(type
))
1921 offset_in_memory
= 0;
1922 offset_in_buffer
= 0;
1923 write_no_repeat_pointer_descriptions(
1925 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1928 /* pass 2: search for pointers in fixed arrays */
1929 offset_in_memory
= 0;
1930 offset_in_buffer
= 0;
1931 write_fixed_array_pointer_descriptions(
1933 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1935 /* pass 3: search for pointers in conformant only arrays (but don't descend
1936 * into conformant varying or varying arrays) */
1937 if (is_conformant_array(type
) &&
1938 (type_array_is_decl_as_ptr(type
) || !current_structure
))
1939 write_conformant_array_pointer_descriptions(
1940 file
, NULL
, type
, 0, typestring_offset
);
1941 else if (type_get_type(type
) == TYPE_STRUCT
&&
1942 get_struct_fc(type
) == RPC_FC_CPSTRUCT
)
1944 unsigned int align
= 0;
1945 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
1946 write_conformant_array_pointer_descriptions(
1948 type_memsize(type
, &align
),
1952 /* pass 4: search for pointers in varying arrays */
1953 offset_in_memory
= 0;
1954 offset_in_buffer
= 0;
1955 write_varying_array_pointer_descriptions(
1957 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1960 int is_declptr(const type_t
*t
)
1962 return is_ptr(t
) || (type_get_type(t
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(t
));
1965 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1966 type_t
*type
, int toplevel_param
,
1967 const char *name
, unsigned int *typestring_offset
)
1969 unsigned int start_offset
;
1970 unsigned char rtype
;
1973 start_offset
= *typestring_offset
;
1974 update_tfsoff(type
, start_offset
, file
);
1976 if (is_declptr(type
))
1978 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1979 int pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
1981 pointer_type
= RPC_FC_RP
;
1982 print_start_tfs_comment(file
, type
, *typestring_offset
);
1983 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1984 pointer_type
, flag
, string_of_type(pointer_type
),
1985 flag
? " [simple_pointer]" : "");
1986 *typestring_offset
+= 2;
1989 print_file(file
, 2, "NdrFcShort(0x2),\n");
1990 *typestring_offset
+= 2;
1995 elem_type
= type_array_get_element(type
);
1997 elem_type
= type_pointer_get_ref(type
);
1999 if (type_get_type(elem_type
) != TYPE_BASIC
)
2001 error("write_string_tfs: Unimplemented for non-basic type %s\n", name
);
2002 return start_offset
;
2005 rtype
= get_basic_fc(elem_type
);
2006 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
2008 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
2009 return start_offset
;
2012 if (type_get_type(type
) == TYPE_ARRAY
&& !type_array_has_conformance(type
))
2014 unsigned int dim
= type_array_get_dim(type
);
2016 /* FIXME: multi-dimensional array */
2018 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2019 name
, 0xffffu
, dim
- 0xffffu
);
2021 if (rtype
== RPC_FC_WCHAR
)
2022 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
2024 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
2025 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2026 *typestring_offset
+= 2;
2028 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim
, dim
);
2029 *typestring_offset
+= 2;
2031 return start_offset
;
2033 else if (is_conformant_array(type
))
2035 unsigned int align
= 0;
2037 if (rtype
== RPC_FC_WCHAR
)
2038 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2040 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2041 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
2042 *typestring_offset
+= 2;
2044 *typestring_offset
+= write_conf_or_var_desc(
2045 file
, current_structure
,
2046 (!type_array_is_decl_as_ptr(type
) && current_structure
2047 ? type_memsize(current_structure
, &align
)
2049 type
, type_array_get_conformance(type
));
2051 return start_offset
;
2055 if (rtype
== RPC_FC_WCHAR
)
2056 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2058 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2059 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2060 *typestring_offset
+= 2;
2062 return start_offset
;
2066 static unsigned int write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2067 const char *name
, unsigned int *typestring_offset
)
2069 const expr_t
*length_is
= type_array_get_variance(type
);
2070 const expr_t
*size_is
= type_array_get_conformance(type
);
2071 unsigned int align
= 0;
2073 unsigned int start_offset
;
2076 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
2077 unsigned int baseoff
2078 = !type_array_is_decl_as_ptr(type
) && current_structure
2079 ? type_memsize(current_structure
, &align
)
2083 pointer_type
= RPC_FC_RP
;
2085 if (write_embedded_types(file
, attrs
, type_array_get_element(type
), name
, FALSE
, typestring_offset
))
2088 has_pointer
= type_has_pointers(type_array_get_element(type
));
2091 size
= type_memsize((is_conformant_array(type
) ? type_array_get_element(type
) : type
), &align
);
2092 fc
= get_array_fc(type
);
2094 start_offset
= *typestring_offset
;
2095 update_tfsoff(type
, start_offset
, file
);
2096 print_start_tfs_comment(file
, type
, start_offset
);
2097 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
2098 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2099 *typestring_offset
+= 2;
2102 if (fc
!= RPC_FC_BOGUS_ARRAY
)
2104 if (fc
== RPC_FC_LGFARRAY
|| fc
== RPC_FC_LGVARRAY
)
2106 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", size
, size
);
2107 *typestring_offset
+= 4;
2111 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
2112 *typestring_offset
+= 2;
2115 if (is_conformant_array(type
))
2117 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2120 if (fc
== RPC_FC_SMVARRAY
|| fc
== RPC_FC_LGVARRAY
)
2122 unsigned int elalign
= 0;
2123 unsigned int elsize
= type_memsize(type_array_get_element(type
), &elalign
);
2124 unsigned int dim
= type_array_get_dim(type
);
2126 if (fc
== RPC_FC_LGVARRAY
)
2128 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim
, dim
);
2129 *typestring_offset
+= 4;
2133 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2134 *typestring_offset
+= 2;
2137 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize
, elsize
);
2138 *typestring_offset
+= 2;
2143 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2146 if (has_pointer
&& (type_array_is_decl_as_ptr(type
) || !current_structure
))
2148 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2149 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2150 *typestring_offset
+= 2;
2151 write_pointer_description(file
, type
, typestring_offset
);
2152 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2153 *typestring_offset
+= 1;
2156 write_member_type(file
, type
, FALSE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2157 write_end(file
, typestring_offset
);
2161 unsigned int dim
= size_is
? 0 : type_array_get_dim(type
);
2162 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2163 *typestring_offset
+= 2;
2165 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2168 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2170 write_member_type(file
, type
, TRUE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2171 write_end(file
, typestring_offset
);
2174 return start_offset
;
2177 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
2179 const var_list_t
*fields
= type_struct_get_fields(type
);
2180 const var_t
*last_field
;
2183 if (!fields
|| list_empty(fields
))
2186 last_field
= LIST_ENTRY( list_tail(fields
), const var_t
, entry
);
2187 ft
= last_field
->type
;
2189 if (is_conformant_array(ft
) && !type_array_is_decl_as_ptr(ft
))
2192 if (type_get_type(ft
) == TYPE_STRUCT
)
2193 return find_array_or_string_in_struct(ft
);
2198 static void write_struct_members(FILE *file
, const type_t
*type
,
2199 int is_complex
, unsigned int *corroff
,
2200 unsigned int *typestring_offset
)
2203 unsigned short offset
= 0;
2204 unsigned int salign
= 1;
2206 var_list_t
*fields
= type_struct_get_fields(type
);
2208 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2210 type_t
*ft
= field
->type
;
2211 unsigned int align
= 0;
2212 unsigned int size
= type_memsize(ft
, &align
);
2213 if (salign
< align
) salign
= align
;
2215 if (!is_conformant_array(ft
) || type_array_is_decl_as_ptr(ft
))
2217 if ((align
- 1) & offset
)
2219 unsigned char fc
= 0;
2223 fc
= RPC_FC_ALIGNM4
;
2226 fc
= RPC_FC_ALIGNM8
;
2229 error("write_struct_members: cannot align type %d\n", type_get_type(ft
));
2231 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2232 offset
= ROUND_SIZE(offset
, align
);
2233 *typestring_offset
+= 1;
2235 write_member_type(file
, type
, is_complex
, field
->attrs
, field
->type
, corroff
,
2241 padding
= ROUNDING(offset
, salign
);
2244 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2245 RPC_FC_STRUCTPAD1
+ padding
- 1,
2247 *typestring_offset
+= 1;
2250 write_end(file
, typestring_offset
);
2253 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
,
2254 const char *name
, unsigned int *tfsoff
)
2256 const type_t
*save_current_structure
= current_structure
;
2257 unsigned int total_size
;
2259 unsigned int start_offset
;
2260 unsigned int array_offset
;
2261 int has_pointers
= 0;
2262 unsigned int align
= 0;
2263 unsigned int corroff
;
2265 unsigned char fc
= get_struct_fc(type
);
2266 var_list_t
*fields
= type_struct_get_fields(type
);
2269 current_structure
= type
;
2271 total_size
= type_memsize(type
, &align
);
2272 if (total_size
> USHRT_MAX
)
2273 error("structure size for %s exceeds %d bytes by %d bytes\n",
2274 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
2276 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2277 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
2279 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
2281 array
= find_array_or_string_in_struct(type
);
2282 if (array
&& !processed(array
->type
))
2284 = is_string_type(array
->attrs
, array
->type
)
2285 ? write_string_tfs(file
, array
->attrs
, array
->type
, FALSE
, array
->name
, tfsoff
)
2286 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
2289 write_descriptors(file
, type
, tfsoff
);
2291 start_offset
= *tfsoff
;
2292 update_tfsoff(type
, start_offset
, file
);
2293 print_start_tfs_comment(file
, type
, start_offset
);
2294 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2295 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2296 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size
, total_size
);
2301 unsigned int absoff
= array
->type
->typestring_offset
;
2302 short reloff
= absoff
- *tfsoff
;
2303 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2304 reloff
, reloff
, absoff
);
2307 else if (fc
== RPC_FC_BOGUS_STRUCT
)
2309 print_file(file
, 2, "NdrFcShort(0x0),\n");
2313 if (fc
== RPC_FC_BOGUS_STRUCT
)
2315 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2316 nothing is written to file yet. On the actual writing pass,
2317 this will have been updated. */
2318 unsigned int absoff
= type_get_real_type(type
)->ptrdesc
?
2319 type_get_real_type(type
)->ptrdesc
: *tfsoff
;
2320 int reloff
= absoff
- *tfsoff
;
2321 assert( reloff
>= 0 );
2322 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2323 reloff
, reloff
, absoff
);
2326 else if ((fc
== RPC_FC_PSTRUCT
) ||
2327 (fc
== RPC_FC_CPSTRUCT
) ||
2328 (fc
== RPC_FC_CVSTRUCT
&& has_pointers
))
2330 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2331 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2333 write_pointer_description(file
, type
, tfsoff
);
2334 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2338 write_struct_members(file
, type
, fc
== RPC_FC_BOGUS_STRUCT
, &corroff
,
2341 if (fc
== RPC_FC_BOGUS_STRUCT
)
2345 type_get_real_type(type
)->ptrdesc
= *tfsoff
;
2346 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
2348 type_t
*ft
= f
->type
;
2351 if (is_string_type(f
->attrs
, ft
))
2352 write_string_tfs(file
, f
->attrs
, ft
, FALSE
, f
->name
, tfsoff
);
2354 write_pointer_tfs(file
, f
->attrs
, ft
, FALSE
, tfsoff
);
2356 else if (type_get_type(ft
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(ft
))
2358 unsigned int offset
;
2360 print_file(file
, 0, "/* %d */\n", *tfsoff
);
2362 offset
= ft
->typestring_offset
;
2363 /* skip over the pointer that is written for strings, since a
2364 * pointer has to be written in-place here */
2365 if (is_string_type(f
->attrs
, ft
))
2367 write_nonsimple_pointer(file
, f
->attrs
, ft
, FALSE
, offset
, tfsoff
);
2370 if (type_get_real_type(type
)->ptrdesc
== *tfsoff
)
2371 type_get_real_type(type
)->ptrdesc
= 0;
2374 current_structure
= save_current_structure
;
2375 return start_offset
;
2378 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
2382 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
2386 if (type_get_type(t
) == TYPE_BASIC
|| type_get_type(t
) == TYPE_ENUM
)
2389 if (type_get_type(t
) == TYPE_BASIC
)
2390 fc
= get_basic_fc(t
);
2392 fc
= get_enum_fc(t
);
2393 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2394 fc
, string_of_type(fc
));
2396 else if (t
->typestring_offset
)
2398 short reloff
= t
->typestring_offset
- *tfsoff
;
2399 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2400 reloff
, reloff
, t
->typestring_offset
);
2403 error("write_branch_type: type unimplemented %d\n", type_get_type(t
));
2409 static unsigned int write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2412 unsigned int start_offset
;
2415 unsigned int nbranch
= 0;
2416 type_t
*deftype
= NULL
;
2417 short nodeftype
= 0xffff;
2423 size
= type_memsize(type
, &align
);
2425 fields
= type_union_get_cases(type
);
2427 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2429 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2431 nbranch
+= list_count(cases
);
2433 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2436 start_offset
= *tfsoff
;
2437 update_tfsoff(type
, start_offset
, file
);
2438 print_start_tfs_comment(file
, type
, start_offset
);
2439 if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
2441 const var_t
*sv
= type_union_get_switch_value(type
);
2442 const type_t
*st
= sv
->type
;
2445 if (type_get_type(st
) == TYPE_BASIC
)
2447 switch (get_basic_fc(st
))
2458 fc
= get_basic_fc(st
);
2462 error("union switch type must be an integer, char, or enum\n");
2465 else if (type_get_type(st
) == TYPE_ENUM
)
2466 fc
= get_enum_fc(st
);
2468 error("union switch type must be an integer, char, or enum\n");
2470 print_file(file
, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION
);
2471 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2472 0x40 | fc
, string_of_type(fc
));
2475 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
2477 static const expr_t dummy_expr
; /* FIXME */
2478 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
2481 if (type_get_type(st
) == TYPE_BASIC
)
2483 switch (get_basic_fc(st
))
2494 fc
= get_basic_fc(st
);
2498 error("union switch type must be an integer, char, or enum\n");
2501 else if (type_get_type(st
) == TYPE_ENUM
)
2502 fc
= get_enum_fc(st
);
2504 error("union switch type must be an integer, char, or enum\n");
2506 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
2507 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2508 fc
, string_of_type(fc
));
2511 *tfsoff
+= write_conf_or_var_desc(file
, NULL
, *tfsoff
, st
, &dummy_expr
);
2512 print_file(file
, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff
+ 2);
2514 print_file(file
, 0, "/* %u */\n", *tfsoff
);
2517 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size
, size
);
2518 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch
, nbranch
);
2521 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2523 type_t
*ft
= f
->type
;
2524 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2525 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2528 if (cases
== NULL
&& !deflt
)
2529 error("union field %s with neither case nor default attribute\n", f
->name
);
2531 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2533 /* MIDL doesn't check for duplicate cases, even though that seems
2534 like a reasonable thing to do, it just dumps them to the TFS
2535 like we're going to do here. */
2536 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c
->cval
, c
->cval
);
2538 write_branch_type(file
, ft
, tfsoff
);
2541 /* MIDL allows multiple default branches, even though that seems
2542 illogical, it just chooses the last one, which is what we will
2553 write_branch_type(file
, deftype
, tfsoff
);
2557 print_file(file
, 2, "NdrFcShort(0x%hx),\n", nodeftype
);
2561 return start_offset
;
2564 static unsigned int write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2565 unsigned int *typeformat_offset
)
2568 unsigned int start_offset
= *typeformat_offset
;
2569 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2573 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2574 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2576 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2580 const type_t
*base
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
2581 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2584 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2586 update_tfsoff(type
, start_offset
, file
);
2587 print_start_tfs_comment(file
, type
, start_offset
);
2588 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2589 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2590 print_file(file
, 2, "NdrFcLong(0x%08x),\n", uuid
->Data1
);
2591 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2592 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2593 for (i
= 0; i
< 8; ++i
)
2594 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2597 fprintf(file
, "\n");
2599 *typeformat_offset
+= 18;
2601 return start_offset
;
2604 static unsigned int write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2606 unsigned int *typeformat_offset
)
2608 unsigned int start_offset
= *typeformat_offset
;
2609 unsigned char flags
= 0;
2611 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2612 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2616 if (is_attr(var
->attrs
, ATTR_IN
))
2619 if (!is_attr(var
->attrs
, ATTR_OUT
))
2620 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2622 if (is_attr(var
->attrs
, ATTR_OUT
))
2625 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2626 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2627 /* return and can't be null values overlap */
2628 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2629 print_file(file
, 0, "can't be null, ");
2630 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2631 print_file(file
, 0, "serialize, ");
2632 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2633 print_file(file
, 0, "no serialize, ");
2634 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2635 print_file(file
, 0, "strict, ");
2636 if ((flags
& 0x21) == 0x20)
2637 print_file(file
, 0, "out, ");
2638 if ((flags
& 0x21) == 0x21)
2639 print_file(file
, 0, "return, ");
2641 print_file(file
, 0, "in, ");
2643 print_file(file
, 0, "via ptr, ");
2644 print_file(file
, 0, "*/\n");
2645 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2646 print_file(file
, 2, "0, /* FIXME: param num */\n");
2647 *typeformat_offset
+= 4;
2649 return start_offset
;
2652 static unsigned int write_typeformatstring_var(FILE *file
, int indent
, const var_t
*func
,
2653 type_t
*type
, const var_t
*var
,
2655 unsigned int *typeformat_offset
)
2657 unsigned int offset
;
2659 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
2661 case TGT_CTXT_HANDLE
:
2662 case TGT_CTXT_HANDLE_POINTER
:
2663 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2665 write_user_tfs(file
, type
, typeformat_offset
);
2666 return type
->typestring_offset
;
2668 return write_string_tfs(file
, var
->attrs
, type
, toplevel_param
, var
->name
, typeformat_offset
);
2673 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2674 ptr_type
= get_pointer_fc(type
, var
->attrs
, toplevel_param
);
2675 if (ptr_type
!= RPC_FC_RP
)
2677 unsigned int absoff
= type
->typestring_offset
;
2678 short reloff
= absoff
- (*typeformat_offset
+ 2);
2679 off
= *typeformat_offset
;
2680 print_file(file
, 0, "/* %d */\n", off
);
2681 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2682 string_of_type(ptr_type
));
2683 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2684 reloff
, reloff
, absoff
);
2685 *typeformat_offset
+= 4;
2690 if (processed(type
)) return type
->typestring_offset
;
2691 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2693 if (processed(type
)) return type
->typestring_offset
;
2694 return write_union_tfs(file
, type
, typeformat_offset
);
2699 case TGT_IFACE_POINTER
:
2700 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2704 size_t start_offset
= *typeformat_offset
;
2705 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2706 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2707 const type_t
*ref
= type_pointer_get_ref(type
);
2709 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
2711 /* special case for pointers to base types */
2717 if (type_get_type(ref
) == TYPE_ENUM
)
2718 fc
= get_enum_fc(ref
);
2720 fc
= get_basic_fc(ref
);
2722 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2723 get_pointer_fc(type
, var
->attrs
, toplevel_param
),
2724 (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2725 string_of_type(get_pointer_fc(type
, var
->attrs
, toplevel_param
)),
2726 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2727 print_file(file
, indent
, "0x%02x, /* %s */\n",
2728 fc
, string_of_type(fc
));
2729 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2730 *typeformat_offset
+= 4;
2731 return start_offset
;
2738 offset
= write_typeformatstring_var(file
, indent
, func
,
2739 type_pointer_get_ref(type
), var
,
2740 FALSE
, typeformat_offset
);
2742 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2743 return write_nonsimple_pointer(file
, var
->attrs
, type
,
2745 offset
, typeformat_offset
);
2749 error("invalid type %s for var %s\n", type
->name
, var
->name
);
2753 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2754 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2758 switch (typegen_detect_type(type
, attrs
, TDT_ALL_TYPES
))
2761 write_user_tfs(file
, type
, tfsoff
);
2764 write_string_tfs(file
, attrs
, type
, FALSE
, name
, tfsoff
);
2766 case TGT_IFACE_POINTER
:
2767 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2771 type_t
*ref
= type_pointer_get_ref(type
);
2773 if (!processed(ref
) && type_get_type(ref
) != TYPE_BASIC
)
2774 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2777 write_pointer_tfs(file
, attrs
, type
, FALSE
, tfsoff
);
2783 /* conformant arrays and strings are handled specially */
2784 if (!is_conformant_array(type
) || type_array_is_decl_as_ptr(type
) )
2786 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2787 if (is_conformant_array(type
))
2792 if (!processed(type
))
2793 write_struct_tfs(file
, type
, name
, tfsoff
);
2796 if (!processed(type
))
2797 write_union_tfs(file
, type
, tfsoff
);
2803 case TGT_CTXT_HANDLE
:
2804 case TGT_CTXT_HANDLE_POINTER
:
2806 error("invalid type %s for var %s\n", type
->name
, name
);
2813 static unsigned int process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2814 type_pred_t pred
, unsigned int *typeformat_offset
)
2817 const statement_t
*stmt
;
2819 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2821 const type_t
*iface
;
2822 const statement_t
*stmt_func
;
2824 if (stmt
->type
== STMT_LIBRARY
)
2826 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2829 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
2832 iface
= stmt
->u
.type
;
2836 current_iface
= iface
;
2837 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
2839 const var_t
*func
= stmt_func
->u
.var
;
2840 if (is_local(func
->attrs
)) continue;
2842 if (!is_void(type_function_get_rettype(func
->type
)))
2845 v
.type
= type_function_get_rettype(func
->type
);
2846 update_tfsoff(type_function_get_rettype(func
->type
),
2847 write_typeformatstring_var(
2849 type_function_get_rettype(func
->type
),
2850 &v
, FALSE
, typeformat_offset
),
2854 current_func
= func
;
2855 if (type_get_function_args(func
->type
))
2856 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2859 write_typeformatstring_var(
2860 file
, 2, func
, var
->type
, var
,
2861 TRUE
, typeformat_offset
),
2866 return *typeformat_offset
+ 1;
2869 static unsigned int process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2871 unsigned int typeformat_offset
= 2;
2873 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2877 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2881 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2882 print_file(file
, indent
, "{\n");
2884 print_file(file
, indent
, "0,\n");
2885 print_file(file
, indent
, "{\n");
2887 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2889 set_all_tfswrite(TRUE
);
2890 process_tfs(file
, stmts
, pred
);
2892 print_file(file
, indent
, "0x0\n");
2894 print_file(file
, indent
, "}\n");
2896 print_file(file
, indent
, "};\n");
2897 print_file(file
, indent
, "\n");
2900 static unsigned int get_required_buffer_size_type(
2901 const type_t
*type
, const char *name
, const attr_list_t
*attrs
, int toplevel_param
, unsigned int *alignment
)
2904 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
2909 const type_t
*utype
= get_user_type(type
, &uname
);
2910 return get_required_buffer_size_type(utype
, uname
, NULL
, FALSE
, alignment
);
2913 switch (get_basic_fc(type
))
2931 case RPC_FC_ERROR_STATUS_T
:
2941 case RPC_FC_BIND_PRIMITIVE
:
2945 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2946 get_basic_fc(type
));
2952 switch (get_enum_fc(type
))
2964 if (get_struct_fc(type
) == RPC_FC_STRUCT
)
2966 if (!type_struct_get_fields(type
)) return 0;
2967 return fields_memsize(type_struct_get_fields(type
), alignment
);
2972 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_RP
)
2974 const type_t
*ref
= type_pointer_get_ref(type
);
2975 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
2979 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
2981 if (get_struct_fc(ref
) == RPC_FC_STRUCT
)
2982 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
2985 case TGT_CTXT_HANDLE
:
2986 case TGT_CTXT_HANDLE_POINTER
:
2990 case TGT_IFACE_POINTER
:
2999 /* FIXME: depends on pointer type */
3000 return type_array_get_dim(type
) *
3001 get_required_buffer_size_type(type_array_get_element(type
), name
, NULL
, FALSE
, alignment
);
3009 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
3011 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3012 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3014 if (!in_attr
&& !out_attr
)
3019 if ((pass
== PASS_IN
&& in_attr
) || (pass
== PASS_OUT
&& out_attr
) ||
3020 pass
== PASS_RETURN
)
3022 if (is_ptrchain_attr(var
, ATTR_CONTEXTHANDLE
))
3028 if (!is_string_type(var
->attrs
, var
->type
))
3029 return get_required_buffer_size_type(var
->type
, var
->name
,
3030 var
->attrs
, TRUE
, alignment
);
3035 static unsigned int get_function_buffer_size( const var_t
*func
, enum pass pass
)
3038 unsigned int total_size
= 0, alignment
;
3040 if (type_get_function_args(func
->type
))
3042 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3044 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
3045 total_size
+= alignment
;
3049 if (pass
== PASS_OUT
&& !is_void(type_function_get_rettype(func
->type
)))
3052 v
.type
= type_function_get_rettype(func
->type
);
3053 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
3054 total_size
+= alignment
;
3059 static void print_phase_function(FILE *file
, int indent
, const char *type
,
3060 const char *local_var_prefix
, enum remoting_phase phase
,
3061 const var_t
*var
, unsigned int type_offset
)
3063 const char *function
;
3066 case PHASE_BUFFERSIZE
:
3067 function
= "BufferSize";
3070 function
= "Marshall";
3072 case PHASE_UNMARSHAL
:
3073 function
= "Unmarshall";
3083 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
3085 print_file(file
, indent
, "&__frame->_StubMsg,\n");
3086 print_file(file
, indent
, "%s%s%s%s%s,\n",
3087 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
3088 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
3090 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
3092 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3093 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
3094 if (phase
== PHASE_UNMARSHAL
)
3095 print_file(file
, indent
, "0);\n");
3099 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
3100 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
3101 const char *varname
)
3103 type_t
*type
= var
->type
;
3105 unsigned int alignment
= 0;
3108 /* no work to do for other phases, buffer sizing is done elsewhere */
3109 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
3112 ref
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3113 if (type_get_type(ref
) == TYPE_ENUM
)
3115 if (get_enum_fc(ref
) == RPC_FC_ENUM32
)
3120 else /* RPC_FC_ENUM16 */
3128 switch (get_basic_fc(ref
))
3148 case RPC_FC_ERROR_STATUS_T
:
3160 case RPC_FC_BIND_PRIMITIVE
:
3161 /* no marshalling needed */
3165 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3166 var
->name
, get_basic_fc(ref
));
3171 if (phase
== PHASE_MARSHAL
)
3172 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
3173 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3174 alignment
- 1, alignment
- 1);
3176 if (phase
== PHASE_MARSHAL
)
3178 print_file(file
, indent
, "*(");
3179 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3181 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
3183 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
3184 fprintf(file
, "%s%s", local_var_prefix
, varname
);
3185 fprintf(file
, ";\n");
3187 else if (phase
== PHASE_UNMARSHAL
)
3189 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
3190 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3191 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
3192 print_file(file
, indent
, "{\n");
3193 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3194 print_file(file
, indent
, "}\n");
3195 print_file(file
, indent
, "%s%s%s",
3196 (pass
== PASS_IN
|| pass
== PASS_RETURN
) ? "" : "*",
3197 local_var_prefix
, varname
);
3198 if (pass
== PASS_IN
&& is_ptr(type
))
3199 fprintf(file
, " = (");
3201 fprintf(file
, " = *(");
3202 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3203 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
3206 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
3207 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3208 fprintf(file
, ");\n");
3211 /* returns whether the MaxCount, Offset or ActualCount members need to be
3212 * filled in for the specified phase */
3213 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
3215 return (phase
!= PHASE_UNMARSHAL
);
3218 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
3222 for ( ; is_array(t
); t
= type_array_get_element(t
))
3223 if (type_array_has_conformance(t
))
3226 x
= type_array_get_conformance(t
);
3228 error("%s: multidimensional conformant"
3229 " arrays not supported at the top level\n",
3236 static void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
3237 enum remoting_phase phase
, const var_t
*var
)
3239 const type_t
*type
= var
->type
;
3240 /* get fundamental type for the argument */
3243 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
3245 else if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
))
3247 else if (type_is_alias(type
))
3248 type
= type_alias_get_aliasee(type
);
3249 else if (is_array(type
))
3251 if (is_conformance_needed_for_phase(phase
) && is_array(type
))
3253 if (type_array_has_conformance(type
))
3255 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3256 write_expr(file
, type_array_get_conformance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3257 fprintf(file
, ";\n\n");
3259 if (type_array_has_variance(type
))
3261 print_file(file
, indent
, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3262 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3263 write_expr(file
, type_array_get_variance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3264 fprintf(file
, ";\n\n");
3269 else if (type_get_type(type
) == TYPE_UNION
)
3271 if (is_conformance_needed_for_phase(phase
))
3273 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3274 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
3275 fprintf(file
, ";\n\n");
3279 else if (type_get_type(type
) == TYPE_INTERFACE
|| is_void(type
))
3283 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3285 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3286 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
3287 fprintf( file
, ";\n\n" );
3291 else if (is_ptr(type
))
3292 type
= type_pointer_get_ref(type
);
3298 static void write_remoting_arg(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3299 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
3301 int in_attr
, out_attr
, pointer_type
;
3302 const type_t
*type
= var
->type
;
3303 unsigned int start_offset
= type
->typestring_offset
;
3305 if (is_ptr(type
) || is_array(type
))
3306 pointer_type
= get_pointer_fc(type
, var
->attrs
, pass
!= PASS_RETURN
);
3310 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3311 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3312 if (!in_attr
&& !out_attr
)
3315 if (phase
!= PHASE_FREE
)
3319 if (!in_attr
) return;
3322 if (!out_attr
) return;
3328 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
);
3330 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
3332 case TGT_CTXT_HANDLE
:
3333 case TGT_CTXT_HANDLE_POINTER
:
3334 if (phase
== PHASE_MARSHAL
)
3336 if (pass
== PASS_IN
)
3338 /* if the context_handle attribute appears in the chain of types
3339 * without pointers being followed, then the context handle must
3340 * be direct, otherwise it is a pointer */
3341 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
3342 print_file(file
, indent
, "NdrClientContextMarshall(\n");
3343 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3344 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr
? "*" : "", local_var_prefix
, var
->name
);
3345 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
3349 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
3350 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3351 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
3352 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
3353 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3356 else if (phase
== PHASE_UNMARSHAL
)
3358 if (pass
== PASS_OUT
)
3361 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
3362 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
3363 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3364 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
3365 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
3369 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
3370 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3371 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3376 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
3379 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
||
3380 pointer_type
!= RPC_FC_RP
)
3382 if (pointer_type
== RPC_FC_RP
&& phase
== PHASE_FREE
&&
3383 !in_attr
&& is_conformant_array(type
))
3385 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3387 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3389 /* strings returned are assumed to be global and hence don't
3391 else if (is_declptr(type
) &&
3392 !(phase
== PHASE_FREE
&& pass
== PASS_RETURN
))
3393 print_phase_function(file
, indent
, "Pointer", local_var_prefix
,
3394 phase
, var
, start_offset
);
3398 unsigned int real_start_offset
= start_offset
;
3399 /* skip over pointer description straight to string description */
3400 if (is_declptr(type
))
3402 if (is_conformant_array(type
))
3403 real_start_offset
+= 4;
3405 real_start_offset
+= 2;
3407 if (is_array(type
) && !is_conformant_array(type
))
3408 print_phase_function(file
, indent
, "NonConformantString",
3409 local_var_prefix
, phase
, var
,
3412 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
3413 phase
, var
, real_start_offset
);
3418 unsigned char tc
= get_array_fc(type
);
3419 const char *array_type
= "FixedArray";
3421 /* We already have the size_is expression since it's at the
3422 top level, but do checks for multidimensional conformant
3423 arrays. When we handle them, we'll need to extend this
3424 function to return a list, and then we'll actually use
3425 the return value. */
3426 get_size_is_expr(type
, var
->name
);
3428 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
3430 array_type
= "VaryingArray";
3432 else if (tc
== RPC_FC_CARRAY
)
3434 array_type
= "ConformantArray";
3436 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
3438 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
3440 : "ConformantVaryingArray");
3443 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
3444 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
3445 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
3447 /* these are all unmarshalled by allocating memory */
3448 if (tc
== RPC_FC_BOGUS_ARRAY
||
3449 tc
== RPC_FC_CVARRAY
||
3450 ((tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
) && in_attr
) ||
3451 (tc
== RPC_FC_CARRAY
&& !in_attr
))
3453 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3455 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3461 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3462 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3465 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3467 if (phase
== PHASE_MARSHAL
)
3468 print_file(file
, indent
, "NdrSimpleTypeMarshall(\n");
3470 print_file(file
, indent
, "NdrSimpleTypeUnmarshall(\n");
3471 print_file(file
, indent
+1, "&__frame->_StubMsg,\n");
3472 print_file(file
, indent
+1, "(unsigned char *)&%s%s,\n",
3475 print_file(file
, indent
+1, "0x%02x /* %s */);\n", get_enum_fc(type
), string_of_type(get_enum_fc(type
)));
3479 switch (get_struct_fc(type
))
3482 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3483 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3485 case RPC_FC_PSTRUCT
:
3486 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3488 case RPC_FC_CSTRUCT
:
3489 case RPC_FC_CPSTRUCT
:
3490 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
3492 case RPC_FC_CVSTRUCT
:
3493 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
3495 case RPC_FC_BOGUS_STRUCT
:
3496 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
3499 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(type
));
3504 const char *union_type
= NULL
;
3506 if (type_get_type(type
) == TYPE_UNION
)
3507 union_type
= "NonEncapsulatedUnion";
3508 else if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
3509 union_type
= "EncapsulatedUnion";
3511 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3512 phase
, var
, start_offset
);
3517 const type_t
*ref
= type_pointer_get_ref(type
);
3518 if (pointer_type
== RPC_FC_RP
&& !is_user_type(ref
)) switch (type_get_type(ref
))
3521 /* base types have known sizes, so don't need a sizing pass
3522 * and don't have any memory to free and so don't need a
3524 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3525 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3528 /* base types have known sizes, so don't need a sizing pass
3529 * and don't have any memory to free and so don't need a
3531 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3532 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3536 const char *struct_type
= NULL
;
3537 switch (get_struct_fc(ref
))
3540 /* simple structs have known sizes, so don't need a sizing
3541 * pass and don't have any memory to free and so don't
3542 * need a freeing pass */
3543 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3544 struct_type
= "SimpleStruct";
3545 else if (phase
== PHASE_FREE
&& pass
== PASS_RETURN
)
3547 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3549 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3553 case RPC_FC_PSTRUCT
:
3554 struct_type
= "SimpleStruct";
3556 case RPC_FC_CSTRUCT
:
3557 case RPC_FC_CPSTRUCT
:
3558 struct_type
= "ConformantStruct";
3560 case RPC_FC_CVSTRUCT
:
3561 struct_type
= "ConformantVaryingStruct";
3563 case RPC_FC_BOGUS_STRUCT
:
3564 struct_type
= "ComplexStruct";
3567 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(ref
));
3572 if (phase
== PHASE_FREE
)
3573 struct_type
= "Pointer";
3575 start_offset
= ref
->typestring_offset
;
3576 print_phase_function(file
, indent
, struct_type
, local_var_prefix
, phase
, var
, start_offset
);
3581 case TYPE_ENCAPSULATED_UNION
:
3583 const char *union_type
= NULL
;
3584 if (phase
== PHASE_FREE
)
3585 union_type
= "Pointer";
3588 if (type_get_type(ref
) == TYPE_UNION
)
3589 union_type
= "NonEncapsulatedUnion";
3590 else if (type_get_type(ref
) == TYPE_ENCAPSULATED_UNION
)
3591 union_type
= "EncapsulatedUnion";
3593 start_offset
= ref
->typestring_offset
;
3596 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3597 phase
, var
, start_offset
);
3602 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3609 case TYPE_INTERFACE
:
3614 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3617 case TGT_IFACE_POINTER
:
3618 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3624 fprintf(file
, "\n");
3627 void write_remoting_arguments(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3628 enum pass pass
, enum remoting_phase phase
)
3630 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3632 unsigned int size
= get_function_buffer_size( func
, pass
);
3633 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
3636 if (pass
== PASS_RETURN
)
3640 var
.type
= type_function_get_rettype(func
->type
);
3641 var
.name
= xstrdup( "_RetVal" );
3642 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, &var
);
3648 if (!type_get_function_args(func
->type
))
3650 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3651 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
3656 unsigned int get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3658 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3662 unsigned int get_size_procformatstring_func(const var_t
*func
)
3665 unsigned int size
= 0;
3667 /* argument list size */
3668 if (type_get_function_args(func
->type
))
3669 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3670 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3672 /* return value size */
3673 if (is_void(type_function_get_rettype(func
->type
)))
3674 size
+= 2; /* FC_END and FC_PAD */
3676 size
+= get_size_procformatstring_type("return value", type_function_get_rettype(func
->type
), NULL
);
3681 unsigned int get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3683 const statement_t
*stmt
;
3684 unsigned int size
= 1;
3686 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3688 const type_t
*iface
;
3689 const statement_t
*stmt_func
;
3691 if (stmt
->type
== STMT_LIBRARY
)
3693 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3696 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
3699 iface
= stmt
->u
.type
;
3703 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
3705 const var_t
*func
= stmt_func
->u
.var
;
3706 if (!is_local(func
->attrs
))
3707 size
+= get_size_procformatstring_func( func
);
3713 unsigned int get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3715 set_all_tfswrite(FALSE
);
3716 return process_tfs(NULL
, stmts
, pred
);
3719 void declare_stub_args( FILE *file
, int indent
, const var_t
*func
)
3721 int in_attr
, out_attr
;
3725 /* declare return value '_RetVal' */
3726 if (!is_void(type_function_get_rettype(func
->type
)))
3728 print_file(file
, indent
, "%s", "");
3729 write_type_decl_left(file
, type_function_get_rettype(func
->type
));
3730 fprintf(file
, " _RetVal;\n");
3733 if (!type_get_function_args(func
->type
))
3736 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3738 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3739 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3740 if (!out_attr
&& !in_attr
)
3743 if (is_context_handle(var
->type
))
3744 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3747 if (!in_attr
&& !is_conformant_array(var
->type
))
3749 type_t
*type_to_print
;
3751 print_file(file
, indent
, "%s", "");
3752 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3753 !type_array_is_decl_as_ptr(var
->type
))
3754 type_to_print
= var
->type
;
3756 type_to_print
= type_pointer_get_ref(var
->type
);
3757 sprintf(name
, "_W%u", i
++);
3758 write_type_decl(file
, type_to_print
, name
);
3759 fprintf(file
, ";\n");
3762 print_file(file
, indent
, "%s", "");
3763 write_type_decl_left(file
, var
->type
);
3765 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3766 !type_array_is_decl_as_ptr(var
->type
)) {
3767 fprintf(file
, "(*%s)", var
->name
);
3769 fprintf(file
, "%s", var
->name
);
3770 write_type_right(file
, var
->type
, FALSE
);
3771 fprintf(file
, ";\n");
3773 if (decl_indirect(var
->type
))
3774 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
3780 void assign_stub_out_args( FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
3782 int in_attr
, out_attr
;
3786 if (!type_get_function_args(func
->type
))
3789 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3791 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3792 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3793 if (!out_attr
&& !in_attr
)
3798 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
3800 if (is_context_handle(var
->type
))
3802 fprintf(file
, " = NdrContextHandleInitialize(\n");
3803 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3804 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3805 var
->type
->typestring_offset
);
3807 else if (is_array(var
->type
) &&
3808 type_array_has_conformance(var
->type
))
3810 unsigned int size
, align
= 0;
3811 type_t
*type
= var
->type
;
3813 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
3815 is_array(type
) && type_array_has_conformance(type
);
3816 type
= type_array_get_element(type
))
3818 write_expr(file
, type_array_get_conformance(type
), TRUE
,
3819 TRUE
, NULL
, NULL
, local_var_prefix
);
3820 fprintf(file
, " * ");
3822 size
= type_memsize(type
, &align
);
3823 fprintf(file
, "%u);\n", size
);
3827 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
3828 switch (typegen_detect_type(type_pointer_get_ref(var
->type
), var
->attrs
, TDT_IGNORE_STRINGS
))
3833 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
3838 case TGT_IFACE_POINTER
:
3840 case TGT_CTXT_HANDLE
:
3841 case TGT_CTXT_HANDLE_POINTER
:
3844 /* not initialised */
3854 fprintf(file
, "\n");
3858 int write_expr_eval_routines(FILE *file
, const char *iface
)
3860 static const char *var_name
= "pS";
3861 static const char *var_name_expr
= "pS->";
3863 struct expr_eval_routine
*eval
;
3864 unsigned short callback_offset
= 0;
3866 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3868 const char *name
= eval
->structure
->name
;
3871 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3872 iface
, name
, callback_offset
);
3873 print_file(file
, 0, "{\n");
3874 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3875 name
, var_name
, name
, eval
->baseoff
);
3876 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3877 print_file(file
, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3878 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
, "");
3879 fprintf(file
, ";\n");
3880 print_file(file
, 0, "}\n\n");
3886 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3888 struct expr_eval_routine
*eval
;
3889 struct expr_eval_routine
*cursor
;
3890 unsigned short callback_offset
= 0;
3892 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3893 fprintf(file
, "{\n");
3895 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3897 const char *name
= eval
->structure
->name
;
3898 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3900 list_remove(&eval
->entry
);
3904 fprintf(file
, "};\n\n");
3907 void write_user_quad_list(FILE *file
)
3911 if (list_empty(&user_type_list
))
3914 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3915 fprintf(file
, "{\n");
3916 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3918 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3919 print_file(file
, 1, "{\n");
3920 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3921 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3922 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3923 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3924 print_file(file
, 1, "}%s\n", sep
);
3926 fprintf(file
, "};\n\n");
3929 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3931 const struct str_list_entry_t
*endpoint
;
3934 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3935 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3936 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3938 print_file( f
, 1, "{ (const unsigned char *)\"" );
3939 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3941 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3944 if (!*p
) goto error
;
3945 if (p
[1] != '[') goto error
;
3947 fprintf( f
, "\", (const unsigned char *)\"" );
3948 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3950 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3953 if (*p
!= ']') goto error
;
3954 fprintf( f
, "\" },\n" );
3956 print_file( f
, 0, "};\n\n" );
3960 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
3963 void write_exceptions( FILE *file
)
3965 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3966 fprintf( file
, "\n");
3967 fprintf( file
, "#include \"wine/exception.h\"\n");
3968 fprintf( file
, "#undef RpcTryExcept\n");
3969 fprintf( file
, "#undef RpcExcept\n");
3970 fprintf( file
, "#undef RpcEndExcept\n");
3971 fprintf( file
, "#undef RpcTryFinally\n");
3972 fprintf( file
, "#undef RpcFinally\n");
3973 fprintf( file
, "#undef RpcEndFinally\n");
3974 fprintf( file
, "#undef RpcExceptionCode\n");
3975 fprintf( file
, "#undef RpcAbnormalTermination\n");
3976 fprintf( file
, "\n");
3977 fprintf( file
, "struct __exception_frame;\n");
3978 fprintf( file
, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3979 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3980 fprintf( file
, "\n");
3981 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
3982 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3983 fprintf( file
, " __filter_func filter; \\\n");
3984 fprintf( file
, " __finally_func finally; \\\n");
3985 fprintf( file
, " sigjmp_buf jmp; \\\n");
3986 fprintf( file
, " DWORD code; \\\n");
3987 fprintf( file
, " unsigned char abnormal_termination; \\\n");
3988 fprintf( file
, " unsigned char filter_level; \\\n");
3989 fprintf( file
, " unsigned char finally_level;\n");
3990 fprintf( file
, "\n");
3991 fprintf( file
, "struct __exception_frame\n{\n");
3992 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
3993 fprintf( file
, "};\n");
3994 fprintf( file
, "\n");
3995 fprintf( file
, "static inline void __widl_unwind_target(void)\n" );
3996 fprintf( file
, "{\n");
3997 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
3998 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3999 fprintf( file
, " {\n");
4000 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4001 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4002 fprintf( file
, " __wine_pop_frame( &exc_frame->frame );\n");
4003 fprintf( file
, " }\n");
4004 fprintf( file
, " exc_frame->filter_level = 0;\n");
4005 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
4006 fprintf( file
, "}\n");
4007 fprintf( file
, "\n");
4008 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4009 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4010 fprintf( file
, " CONTEXT *context,\n");
4011 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4012 fprintf( file
, "{\n");
4013 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4014 fprintf( file
, "\n");
4015 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4016 fprintf( file
, " {\n" );
4017 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4018 fprintf( file
, " {\n" );
4019 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4020 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4021 fprintf( file
, " }\n" );
4022 fprintf( file
, " return ExceptionContinueSearch;\n");
4023 fprintf( file
, " }\n" );
4024 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
4025 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4026 fprintf( file
, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4027 fprintf( file
, " return ExceptionContinueSearch;\n");
4028 fprintf( file
, "}\n");
4029 fprintf( file
, "\n");
4030 fprintf( file
, "#define RpcTryExcept \\\n");
4031 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4032 fprintf( file
, " { \\\n");
4033 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4034 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4035 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
4036 fprintf( file
, "\n");
4037 fprintf( file
, "#define RpcExcept(expr) \\\n");
4038 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4039 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4040 fprintf( file
, " __frame->filter_level = 0; \\\n" );
4041 fprintf( file
, " } \\\n");
4042 fprintf( file
, " else \\\n");
4043 fprintf( file
, "\n");
4044 fprintf( file
, "#define RpcEndExcept\n");
4045 fprintf( file
, "\n");
4046 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
4047 fprintf( file
, "\n");
4048 fprintf( file
, "#define RpcTryFinally \\\n");
4049 fprintf( file
, " if (!__frame->filter_level) \\\n");
4050 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4051 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
4052 fprintf( file
, "\n");
4053 fprintf( file
, "#define RpcFinally \\\n");
4054 fprintf( file
, " if (!__frame->filter_level) \\\n");
4055 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4056 fprintf( file
, " __frame->finally_level = 0;\n");
4057 fprintf( file
, "\n");
4058 fprintf( file
, "#define RpcEndFinally\n");
4059 fprintf( file
, "\n");
4060 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4061 fprintf( file
, "\n");
4062 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4063 fprintf( file
, " do { \\\n");
4064 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4065 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4066 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
4067 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
4068 fprintf( file
, " __frame->filter_level = 0; \\\n");
4069 fprintf( file
, " __frame->finally_level = 0; \\\n");
4070 fprintf( file
, " } while (0)\n");
4071 fprintf( file
, "\n");
4072 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4073 fprintf( file
, "\n");
4074 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4075 fprintf( file
, " do { (void)(filter_func); } while(0)\n");
4076 fprintf( file
, "\n");
4077 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4078 fprintf( file
, " DWORD code;\n");
4079 fprintf( file
, "\n");
4080 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");