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 fields_memsize(const var_list_t
*fields
, unsigned int *align
);
63 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
64 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
65 const char *name
, int write_ptr
, unsigned int *tfsoff
);
66 static const var_t
*find_array_or_string_in_struct(const type_t
*type
);
67 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
68 type_t
*type
, int toplevel_param
,
69 const char *name
, unsigned int *typestring_offset
);
71 const char *string_of_type(unsigned char type
)
75 case RPC_FC_BYTE
: return "FC_BYTE";
76 case RPC_FC_CHAR
: return "FC_CHAR";
77 case RPC_FC_SMALL
: return "FC_SMALL";
78 case RPC_FC_USMALL
: return "FC_USMALL";
79 case RPC_FC_WCHAR
: return "FC_WCHAR";
80 case RPC_FC_SHORT
: return "FC_SHORT";
81 case RPC_FC_USHORT
: return "FC_USHORT";
82 case RPC_FC_LONG
: return "FC_LONG";
83 case RPC_FC_ULONG
: return "FC_ULONG";
84 case RPC_FC_FLOAT
: return "FC_FLOAT";
85 case RPC_FC_HYPER
: return "FC_HYPER";
86 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
87 case RPC_FC_ENUM16
: return "FC_ENUM16";
88 case RPC_FC_ENUM32
: return "FC_ENUM32";
89 case RPC_FC_IGNORE
: return "FC_IGNORE";
90 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
91 case RPC_FC_RP
: return "FC_RP";
92 case RPC_FC_UP
: return "FC_UP";
93 case RPC_FC_OP
: return "FC_OP";
94 case RPC_FC_FP
: return "FC_FP";
95 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
96 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
97 case RPC_FC_STRUCT
: return "FC_STRUCT";
98 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
99 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
100 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
101 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
102 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
103 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
104 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
105 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
106 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
107 case RPC_FC_CARRAY
: return "FC_CARRAY";
108 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
109 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
110 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
111 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
112 case RPC_FC_POINTER
: return "FC_POINTER";
113 case RPC_FC_C_CSTRING
: return "FC_C_CSTRING";
114 case RPC_FC_C_WSTRING
: return "FC_C_WSTRING";
115 case RPC_FC_CSTRING
: return "FC_CSTRING";
116 case RPC_FC_WSTRING
: return "FC_WSTRING";
118 error("string_of_type: unknown type 0x%02x\n", type
);
123 unsigned char get_basic_fc(const type_t
*type
)
125 int sign
= type_basic_get_sign(type
);
126 switch (type_basic_get_type(type
))
128 case TYPE_BASIC_INT8
: return (sign
<= 0 ? RPC_FC_SMALL
: RPC_FC_USMALL
);
129 case TYPE_BASIC_INT16
: return (sign
<= 0 ? RPC_FC_SHORT
: RPC_FC_USHORT
);
130 case TYPE_BASIC_INT32
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
131 case TYPE_BASIC_INT64
: return RPC_FC_HYPER
;
132 case TYPE_BASIC_INT
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
133 case TYPE_BASIC_BYTE
: return RPC_FC_BYTE
;
134 case TYPE_BASIC_CHAR
: return RPC_FC_CHAR
;
135 case TYPE_BASIC_WCHAR
: return RPC_FC_WCHAR
;
136 case TYPE_BASIC_HYPER
: return RPC_FC_HYPER
;
137 case TYPE_BASIC_FLOAT
: return RPC_FC_FLOAT
;
138 case TYPE_BASIC_DOUBLE
: return RPC_FC_DOUBLE
;
139 case TYPE_BASIC_ERROR_STATUS_T
: return RPC_FC_ERROR_STATUS_T
;
140 case TYPE_BASIC_HANDLE
: return RPC_FC_BIND_PRIMITIVE
;
145 unsigned char get_pointer_fc(const type_t
*type
, const attr_list_t
*attrs
, int toplevel_param
)
150 assert(is_ptr(type
) || is_array(type
));
152 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
156 for (t
= type
; type_is_alias(t
); t
= type_alias_get_aliasee(t
))
158 pointer_type
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
165 else if (is_ptr(type
))
166 return type_pointer_get_default_fc(type
);
168 return type_array_get_ptr_default_fc(type
);
171 static unsigned char get_enum_fc(const type_t
*type
)
173 assert(type_get_type(type
) == TYPE_ENUM
);
174 if (is_aliaschain_attr(type
, ATTR_V1ENUM
))
175 return RPC_FC_ENUM32
;
177 return RPC_FC_ENUM16
;
180 enum typegen_type
typegen_detect_type(const type_t
*type
, const attr_list_t
*attrs
, unsigned int flags
)
182 if (is_user_type(type
))
183 return TGT_USER_TYPE
;
185 if (is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
))
186 return TGT_CTXT_HANDLE
;
188 if (!(flags
& TDT_IGNORE_STRINGS
) && is_string_type(attrs
, type
))
191 switch (type_get_type(type
))
198 if (type_get_type(type_pointer_get_ref(type
)) == TYPE_INTERFACE
||
199 (type_get_type(type_pointer_get_ref(type
)) == TYPE_VOID
&& is_attr(attrs
, ATTR_IIDIS
)))
200 return TGT_IFACE_POINTER
;
201 else if (is_aliaschain_attr(type_pointer_get_ref(type
), ATTR_CONTEXTHANDLE
))
202 return TGT_CTXT_HANDLE_POINTER
;
207 case TYPE_ENCAPSULATED_UNION
:
223 unsigned char get_struct_fc(const type_t
*type
)
226 int has_conformance
= 0;
227 int has_variance
= 0;
231 fields
= type_struct_get_fields(type
);
233 if (get_padding(fields
))
234 return RPC_FC_BOGUS_STRUCT
;
236 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, var_t
, entry
)
238 type_t
*t
= field
->type
;
239 enum typegen_type typegen_type
;
241 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
243 if (typegen_type
== TGT_ARRAY
&& !type_array_is_decl_as_ptr(t
))
245 if (is_string_type(field
->attrs
, field
->type
))
247 if (is_conformant_array(t
))
253 if (is_array(type_array_get_element(field
->type
)))
254 return RPC_FC_BOGUS_STRUCT
;
256 if (type_array_has_conformance(field
->type
))
259 if (list_next(fields
, &field
->entry
))
260 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
263 if (type_array_has_variance(t
))
266 t
= type_array_get_element(t
);
267 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
270 switch (typegen_type
)
273 case TGT_IFACE_POINTER
:
274 return RPC_FC_BOGUS_STRUCT
;
278 if (get_enum_fc(t
) == RPC_FC_ENUM16
)
279 return RPC_FC_BOGUS_STRUCT
;
283 if (get_pointer_fc(t
, field
->attrs
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
284 return RPC_FC_BOGUS_STRUCT
;
288 return RPC_FC_BOGUS_STRUCT
;
291 unsigned char fc
= get_struct_fc(t
);
296 case RPC_FC_CVSTRUCT
:
302 case RPC_FC_CPSTRUCT
:
304 if (list_next( fields
, &field
->entry
))
305 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
312 if (list_next( fields
, &field
->entry
))
313 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
322 error_loc("Unknown struct member %s with type (0x%02x)\n", field
->name
, fc
);
323 /* fallthru - treat it as complex */
325 /* as soon as we see one of these these members, it's bogus... */
326 case RPC_FC_BOGUS_STRUCT
:
327 return RPC_FC_BOGUS_STRUCT
;
332 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
334 case TGT_CTXT_HANDLE
:
335 case TGT_CTXT_HANDLE_POINTER
:
336 /* checking after parsing should mean that we don't get here. if we do,
337 * it's a checker bug */
344 if ( has_conformance
)
345 return RPC_FC_CVSTRUCT
;
347 return RPC_FC_BOGUS_STRUCT
;
349 if( has_conformance
&& has_pointer
)
350 return RPC_FC_CPSTRUCT
;
351 if( has_conformance
)
352 return RPC_FC_CSTRUCT
;
354 return RPC_FC_PSTRUCT
;
355 return RPC_FC_STRUCT
;
358 unsigned char get_array_fc(const type_t
*type
)
361 const expr_t
*size_is
;
362 const type_t
*elem_type
;
364 elem_type
= type_array_get_element(type
);
365 size_is
= type_array_get_conformance(type
);
369 unsigned int align
= 0;
370 unsigned int size
= type_memsize(elem_type
, &align
);
371 if (size
* type_array_get_dim(type
) > 0xffffuL
)
372 fc
= RPC_FC_LGFARRAY
;
374 fc
= RPC_FC_SMFARRAY
;
379 if (type_array_has_variance(type
))
381 if (fc
== RPC_FC_SMFARRAY
)
382 fc
= RPC_FC_SMVARRAY
;
383 else if (fc
== RPC_FC_LGFARRAY
)
384 fc
= RPC_FC_LGVARRAY
;
385 else if (fc
== RPC_FC_CARRAY
)
389 switch (typegen_detect_type(elem_type
, NULL
, TDT_IGNORE_STRINGS
))
392 fc
= RPC_FC_BOGUS_ARRAY
;
395 switch (get_struct_fc(elem_type
))
397 case RPC_FC_BOGUS_STRUCT
:
398 fc
= RPC_FC_BOGUS_ARRAY
;
403 /* is 16-bit enum - if so, wire size differs from mem size and so
404 * the array cannot be block copied, which means the array is complex */
405 if (get_enum_fc(elem_type
) == RPC_FC_ENUM16
)
406 fc
= RPC_FC_BOGUS_ARRAY
;
409 case TGT_IFACE_POINTER
:
410 fc
= RPC_FC_BOGUS_ARRAY
;
413 /* ref pointers cannot just be block copied. unique pointers to
414 * interfaces need special treatment. either case means the array is
416 if (get_pointer_fc(elem_type
, NULL
, FALSE
) == RPC_FC_RP
)
417 fc
= RPC_FC_BOGUS_ARRAY
;
420 case TGT_CTXT_HANDLE
:
421 case TGT_CTXT_HANDLE_POINTER
:
425 /* nothing to do for everything else */
432 int is_struct(unsigned char type
)
439 case RPC_FC_CPSTRUCT
:
440 case RPC_FC_CVSTRUCT
:
441 case RPC_FC_BOGUS_STRUCT
:
448 static int is_non_complex_struct(const type_t
*type
)
450 return (type_get_type(type
) == TYPE_STRUCT
&&
451 get_struct_fc(type
) != RPC_FC_BOGUS_STRUCT
);
454 static int type_has_pointers(const type_t
*type
)
456 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
463 /* FIXME: array can be pointer */
464 return type_has_pointers(type_array_get_element(type
));
467 var_list_t
*fields
= type_struct_get_fields(type
);
469 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
471 if (type_has_pointers(field
->type
))
480 fields
= type_union_get_cases(type
);
481 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
483 if (field
->type
&& type_has_pointers(field
->type
))
488 case TGT_CTXT_HANDLE
:
489 case TGT_CTXT_HANDLE_POINTER
:
491 case TGT_IFACE_POINTER
:
501 static int type_has_full_pointer(const type_t
*type
, const attr_list_t
*attrs
,
504 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
509 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
514 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
517 return type_has_full_pointer(type_array_get_element(type
), NULL
, FALSE
);
520 var_list_t
*fields
= type_struct_get_fields(type
);
522 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
524 if (type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
533 fields
= type_union_get_cases(type
);
534 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
536 if (field
->type
&& type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
541 case TGT_CTXT_HANDLE
:
542 case TGT_CTXT_HANDLE_POINTER
:
544 case TGT_IFACE_POINTER
:
554 static unsigned short user_type_offset(const char *name
)
557 unsigned short off
= 0;
558 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
560 if (strcmp(name
, ut
->name
) == 0)
564 error("user_type_offset: couldn't find type (%s)\n", name
);
568 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
570 type
->typestring_offset
= offset
;
571 if (file
) type
->tfswrite
= FALSE
;
574 static void guard_rec(type_t
*type
)
576 /* types that contain references to themselves (like a linked list),
577 need to be shielded from infinite recursion when writing embedded
579 if (type
->typestring_offset
)
580 type
->tfswrite
= FALSE
;
582 type
->typestring_offset
= 1;
585 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
589 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
597 if (type_is_alias(t
))
598 t
= type_alias_get_aliasee(t
);
604 int is_user_type(const type_t
*t
)
606 return get_user_type(t
, NULL
) != NULL
;
609 static int is_embedded_complex(const type_t
*type
)
611 switch (typegen_detect_type(type
, NULL
, TDT_ALL_TYPES
))
617 case TGT_IFACE_POINTER
:
624 static const char *get_context_handle_type_name(const type_t
*type
)
628 is_ptr(t
) || type_is_alias(t
);
629 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
630 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
636 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
639 fprintf(file, "/* %2u */\n", typestring_offset); \
640 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
644 static void print_file(FILE *file
, int indent
, const char *format
, ...) __attribute__((format (printf
, 3, 4)));
645 static void print_file(FILE *file
, int indent
, const char *format
, ...)
648 va_start(va
, format
);
649 print(file
, indent
, format
, va
);
653 void print(FILE *file
, int indent
, const char *format
, va_list va
)
657 if (format
[0] != '\n')
660 vfprintf(file
, format
, va
);
665 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
, const char *local_var_prefix
)
667 if (decl_indirect(t
))
669 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
670 local_var_prefix
, n
, local_var_prefix
, n
);
671 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
673 else if (is_ptr(t
) || is_array(t
))
674 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
677 void write_parameters_init(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
681 if (!is_void(type_function_get_rettype(func
->type
)))
682 write_var_init(file
, indent
, type_function_get_rettype(func
->type
), "_RetVal", local_var_prefix
);
684 if (!type_get_function_args(func
->type
))
687 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
688 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
693 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
695 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
696 print_file(f
, indent
, "{\n");
697 print_file(f
, indent
+ 1, "short Pad;\n");
698 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
699 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
700 print_file(f
, indent
, "\n");
703 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
707 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
708 get_size_typeformatstring(stmts
, pred
));
710 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
711 get_size_procformatstring(stmts
, pred
));
714 write_formatdesc(f
, indent
, "TYPE");
715 write_formatdesc(f
, indent
, "PROC");
717 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
718 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
719 print_file(f
, indent
, "\n");
722 int decl_indirect(const type_t
*t
)
726 return (type_get_type(t
) != TYPE_BASIC
&&
727 type_get_type(t
) != TYPE_ENUM
&&
728 type_get_type(t
) != TYPE_POINTER
&&
729 type_get_type(t
) != TYPE_ARRAY
);
732 static unsigned int write_procformatstring_type(FILE *file
, int indent
,
735 const attr_list_t
*attrs
,
740 int is_in
= is_attr(attrs
, ATTR_IN
);
741 int is_out
= is_attr(attrs
, ATTR_OUT
);
743 if (!is_in
&& !is_out
) is_in
= TRUE
;
745 if (type_get_type(type
) == TYPE_BASIC
||
746 type_get_type(type
) == TYPE_ENUM
)
751 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
753 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
755 if (type_get_type(type
) == TYPE_ENUM
)
757 fc
= get_enum_fc(type
);
761 fc
= get_basic_fc(type
);
763 if (fc
== RPC_FC_BIND_PRIMITIVE
)
767 print_file(file
, indent
, "0x%02x, /* %s */\n",
768 fc
, string_of_type(fc
));
769 size
= 2; /* includes param type prefix */
774 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
775 else if (is_in
&& is_out
)
776 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
778 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
780 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
782 print_file(file
, indent
, "0x01,\n");
783 print_file(file
, indent
, "NdrFcShort(0x%hx),\n", type
->typestring_offset
);
784 size
= 4; /* includes param type prefix */
789 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
791 const statement_t
*stmt
;
792 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
794 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
796 const statement_t
*stmt_func
;
797 if (!pred(stmt
->u
.type
))
799 STATEMENTS_FOR_EACH_FUNC(stmt_func
, type_iface_get_stmts(stmt
->u
.type
))
801 const var_t
*func
= stmt_func
->u
.var
;
802 if (is_local(func
->attrs
)) continue;
803 /* emit argument data */
804 if (type_get_function_args(func
->type
))
807 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
808 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
811 /* emit return value data */
812 if (is_void(type_function_get_rettype(func
->type
)))
814 print_file(file
, indent
, "0x5b, /* FC_END */\n");
815 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
818 write_procformatstring_type(file
, indent
, "return value", type_function_get_rettype(func
->type
), NULL
, TRUE
);
821 else if (stmt
->type
== STMT_LIBRARY
)
822 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
826 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
830 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
831 print_file(file
, indent
, "{\n");
833 print_file(file
, indent
, "0,\n");
834 print_file(file
, indent
, "{\n");
837 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
839 print_file(file
, indent
, "0x0\n");
841 print_file(file
, indent
, "}\n");
843 print_file(file
, indent
, "};\n");
844 print_file(file
, indent
, "\n");
847 static int write_base_type(FILE *file
, const type_t
*type
, int convert_to_signed_type
, unsigned int *typestring_offset
)
851 if (type_get_type(type
) == TYPE_BASIC
)
852 fc
= get_basic_fc(type
);
853 else if (type_get_type(type
) == TYPE_ENUM
)
854 fc
= get_enum_fc(type
);
858 if (convert_to_signed_type
)
874 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
875 *typestring_offset
+= 1;
879 /* write conformance / variance descriptor */
880 static unsigned int write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
881 unsigned int baseoff
, const type_t
*type
,
884 unsigned char operator_type
= 0;
885 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
886 const char *conftype_string
= "";
887 const char *operator_string
= "no operators";
888 const expr_t
*subexpr
;
892 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
898 /* Top-level conformance calculations are done inline. */
899 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
900 RPC_FC_TOP_LEVEL_CONFORMANCE
);
901 print_file (file
, 2, "0x0,\n");
902 print_file (file
, 2, "NdrFcShort(0x0),\n");
908 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
909 error("write_conf_or_var_desc: constant value %ld is greater than "
910 "the maximum constant size of %d\n", expr
->cval
,
911 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
913 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
914 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
915 print_file(file
, 2, "0x%lx,\n", expr
->cval
>> 16);
916 print_file(file
, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr
->cval
);
921 if (is_ptr(type
) || (is_array(type
) && type_array_is_decl_as_ptr(type
)))
923 conftype
= RPC_FC_POINTER_CONFORMANCE
;
924 conftype_string
= "field pointer, ";
928 switch (subexpr
->type
)
931 subexpr
= subexpr
->ref
;
932 operator_type
= RPC_FC_DEREFERENCE
;
933 operator_string
= "FC_DEREFERENCE";
936 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
938 subexpr
= subexpr
->ref
;
939 operator_type
= RPC_FC_DIV_2
;
940 operator_string
= "FC_DIV_2";
944 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
946 subexpr
= subexpr
->ref
;
947 operator_type
= RPC_FC_MULT_2
;
948 operator_string
= "FC_MULT_2";
952 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
954 subexpr
= subexpr
->ref
;
955 operator_type
= RPC_FC_SUB_1
;
956 operator_string
= "FC_SUB_1";
960 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
962 subexpr
= subexpr
->ref
;
963 operator_type
= RPC_FC_ADD_1
;
964 operator_string
= "FC_ADD_1";
971 if (subexpr
->type
== EXPR_IDENTIFIER
)
973 const type_t
*correlation_variable
= NULL
;
974 unsigned char param_type
= 0;
975 unsigned int offset
= 0;
977 var_list_t
*fields
= type_struct_get_fields(structure
);
979 if (fields
) LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
981 unsigned int align
= 0;
982 /* FIXME: take alignment into account */
983 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
985 correlation_variable
= var
->type
;
988 offset
+= type_memsize(var
->type
, &align
);
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 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
)
1086 int have_align
= FALSE
;
1087 unsigned int size
= 0;
1090 if (!fields
) return 0;
1091 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1093 unsigned int falign
= 0;
1094 unsigned int fsize
= type_memsize(v
->type
, &falign
);
1100 size
= ROUND_SIZE(size
, falign
);
1104 size
= ROUND_SIZE(size
, *align
);
1108 static unsigned int union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
1110 unsigned int size
, maxs
= 0;
1111 unsigned int align
= *pmaxa
;
1114 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1116 /* we could have an empty default field with NULL type */
1119 size
= type_memsize(v
->type
, &align
);
1120 if (maxs
< size
) maxs
= size
;
1121 if (*pmaxa
< align
) *pmaxa
= align
;
1128 int get_padding(const var_list_t
*fields
)
1130 unsigned short offset
= 0;
1137 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
1139 type_t
*ft
= f
->type
;
1140 unsigned int align
= 0;
1141 unsigned int size
= type_memsize(ft
, &align
);
1144 offset
= ROUND_SIZE(offset
, align
);
1148 return ROUNDING(offset
, salign
);
1151 unsigned int type_memsize(const type_t
*t
, unsigned int *align
)
1153 unsigned int size
= 0;
1155 switch (type_get_type(t
))
1158 switch (get_basic_fc(t
))
1165 if (size
> *align
) *align
= size
;
1171 if (size
> *align
) *align
= size
;
1175 case RPC_FC_ERROR_STATUS_T
:
1178 if (size
> *align
) *align
= size
;
1183 if (size
> *align
) *align
= size
;
1186 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t
));
1191 switch (get_enum_fc(t
))
1195 if (size
> *align
) *align
= size
;
1199 if (size
> *align
) *align
= size
;
1202 error("type_memsize: Unknown enum type\n");
1207 size
= fields_memsize(type_struct_get_fields(t
), align
);
1209 case TYPE_ENCAPSULATED_UNION
:
1210 size
= fields_memsize(type_encapsulated_union_get_fields(t
), align
);
1213 size
= union_memsize(type_union_get_cases(t
), align
);
1216 assert( pointer_size
);
1217 size
= pointer_size
;
1218 if (size
> *align
) *align
= size
;
1221 if (!type_array_is_decl_as_ptr(t
))
1223 if (is_conformant_array(t
))
1225 type_memsize(type_array_get_element(t
), align
);
1229 size
= type_array_get_dim(t
) *
1230 type_memsize(type_array_get_element(t
), align
);
1232 else /* declared as a pointer */
1234 assert( pointer_size
);
1235 size
= pointer_size
;
1236 if (size
> *align
) *align
= size
;
1239 case TYPE_INTERFACE
:
1245 /* these types should not be encountered here due to language
1246 * restrictions (interface, void, coclass, module), logical
1247 * restrictions (alias - due to type_get_type call above) or
1248 * checking restrictions (function). */
1255 int is_full_pointer_function(const var_t
*func
)
1258 if (type_has_full_pointer(type_function_get_rettype(func
->type
), func
->attrs
, TRUE
))
1260 if (!type_get_function_args(func
->type
))
1262 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
1263 if (type_has_full_pointer( var
->type
, var
->attrs
, TRUE
))
1268 void write_full_pointer_init(FILE *file
, int indent
, const var_t
*func
, int is_server
)
1270 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1271 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
1272 fprintf(file
, "\n");
1275 void write_full_pointer_free(FILE *file
, int indent
, const var_t
*func
)
1277 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1278 fprintf(file
, "\n");
1281 static unsigned int write_nonsimple_pointer(FILE *file
, const attr_list_t
*attrs
,
1284 unsigned int offset
,
1285 unsigned int *typeformat_offset
)
1287 unsigned int start_offset
= *typeformat_offset
;
1288 short reloff
= offset
- (*typeformat_offset
+ 2);
1289 int in_attr
, out_attr
;
1291 unsigned char flags
= 0;
1293 pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
1295 in_attr
= is_attr(attrs
, ATTR_IN
);
1296 out_attr
= is_attr(attrs
, ATTR_OUT
);
1297 if (!in_attr
&& !out_attr
) in_attr
= 1;
1299 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1300 flags
|= RPC_FC_P_ONSTACK
;
1302 if (is_ptr(type
) && !last_ptr(type
))
1303 flags
|= RPC_FC_P_DEREF
;
1305 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1308 string_of_type(pointer_type
));
1311 if (flags
& RPC_FC_P_ONSTACK
)
1312 fprintf(file
, " [allocated_on_stack]");
1313 if (flags
& RPC_FC_P_DEREF
)
1314 fprintf(file
, " [pointer_deref]");
1315 fprintf(file
, " */\n");
1318 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, offset
);
1319 *typeformat_offset
+= 4;
1321 return start_offset
;
1324 static unsigned int write_simple_pointer(FILE *file
, const attr_list_t
*attrs
, const type_t
*type
, int toplevel_param
)
1327 unsigned char pointer_fc
;
1330 /* for historical reasons, write_simple_pointer also handled string types,
1331 * but no longer does. catch bad uses of the function with this check */
1332 if (is_string_type(attrs
, type
))
1333 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
1335 pointer_fc
= get_pointer_fc(type
, attrs
, toplevel_param
);
1337 ref
= type_pointer_get_ref(type
);
1338 if (type_get_type(ref
) == TYPE_ENUM
)
1339 fc
= get_enum_fc(ref
);
1341 fc
= get_basic_fc(ref
);
1343 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1344 pointer_fc
, RPC_FC_P_SIMPLEPOINTER
, string_of_type(pointer_fc
));
1345 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1346 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1350 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
1352 print_file(file
, 0, "/* %u (", tfsoff
);
1353 write_type_decl(file
, t
, NULL
);
1354 print_file(file
, 0, ") */\n");
1357 static unsigned int write_pointer_tfs(FILE *file
, const attr_list_t
*attrs
,
1358 type_t
*type
, int toplevel_param
,
1359 unsigned int *typestring_offset
)
1361 unsigned int offset
= *typestring_offset
;
1362 type_t
*ref
= type_pointer_get_ref(type
);
1364 print_start_tfs_comment(file
, type
, offset
);
1365 update_tfsoff(type
, offset
, file
);
1367 if (ref
->typestring_offset
)
1368 write_nonsimple_pointer(file
, attrs
, type
,
1370 type_pointer_get_ref(type
)->typestring_offset
,
1372 else if (type_get_type(ref
) == TYPE_BASIC
||
1373 type_get_type(ref
) == TYPE_ENUM
)
1374 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
,
1380 static int processed(const type_t
*type
)
1382 return type
->typestring_offset
&& !type
->tfswrite
;
1385 static int user_type_has_variable_size(const type_t
*t
)
1389 else if (type_get_type(t
) == TYPE_STRUCT
)
1391 switch (get_struct_fc(t
))
1393 case RPC_FC_PSTRUCT
:
1394 case RPC_FC_CSTRUCT
:
1395 case RPC_FC_CPSTRUCT
:
1396 case RPC_FC_CVSTRUCT
:
1400 /* Note: Since this only applies to user types, we can't have a conformant
1401 array here, and strings should get filed under pointer in this case. */
1405 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1407 unsigned int start
, absoff
, flags
;
1408 unsigned int align
= 0, ualign
= 0;
1409 const char *name
= NULL
;
1410 type_t
*utype
= get_user_type(type
, &name
);
1411 unsigned int usize
= user_type_has_variable_size(utype
) ? 0 : type_memsize(utype
, &ualign
);
1412 unsigned int size
= type_memsize(type
, &align
);
1413 unsigned short funoff
= user_type_offset(name
);
1418 if (type_get_type(utype
) == TYPE_BASIC
||
1419 type_get_type(utype
) == TYPE_ENUM
)
1423 if (type_get_type(utype
) == TYPE_ENUM
)
1424 fc
= get_enum_fc(utype
);
1426 fc
= get_basic_fc(utype
);
1429 print_start_tfs_comment(file
, utype
, absoff
);
1430 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1431 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1436 if (!processed(utype
))
1437 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1438 absoff
= utype
->typestring_offset
;
1441 if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_RP
)
1443 else if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_UP
)
1449 update_tfsoff(type
, start
, file
);
1450 print_start_tfs_comment(file
, type
, start
);
1451 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1452 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1453 flags
| (align
- 1), align
- 1, flags
);
1454 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1455 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
1456 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize
, usize
);
1458 reloff
= absoff
- *tfsoff
;
1459 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, absoff
);
1463 static void write_member_type(FILE *file
, const type_t
*cont
,
1464 int cont_is_complex
, const attr_list_t
*attrs
,
1465 const type_t
*type
, unsigned int *corroff
,
1466 unsigned int *tfsoff
)
1468 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1470 unsigned int absoff
;
1473 if (type_get_type(type
) == TYPE_UNION
&& is_attr(attrs
, ATTR_SWITCHIS
))
1480 absoff
= type
->typestring_offset
;
1482 reloff
= absoff
- (*tfsoff
+ 2);
1484 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1485 /* FIXME: actually compute necessary padding */
1486 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1487 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1488 reloff
, reloff
, absoff
);
1491 else if (is_ptr(type
) || is_conformant_array(type
))
1493 unsigned char fc
= cont_is_complex
? RPC_FC_POINTER
: RPC_FC_LONG
;
1494 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1497 else if (!write_base_type(file
, type
, TRUE
, tfsoff
))
1498 error("Unsupported member type %d\n", type_get_type(type
));
1501 static void write_end(FILE *file
, unsigned int *tfsoff
)
1503 if (*tfsoff
% 2 == 0)
1505 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1508 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1512 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1514 unsigned int offset
= 0;
1515 var_list_t
*fs
= type_struct_get_fields(type
);
1518 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1520 unsigned int align
= 0;
1521 type_t
*ft
= f
->type
;
1522 if (type_get_type(ft
) == TYPE_UNION
&& is_attr(f
->attrs
, ATTR_SWITCHIS
))
1524 unsigned int absoff
= ft
->typestring_offset
;
1525 short reloff
= absoff
- (*tfsoff
+ 6);
1526 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1527 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
1528 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1529 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1530 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1531 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1532 reloff
, reloff
, absoff
);
1536 /* FIXME: take alignment into account */
1537 offset
+= type_memsize(ft
, &align
);
1541 static int write_no_repeat_pointer_descriptions(
1542 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1543 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1544 unsigned int *typestring_offset
)
1550 (is_conformant_array(type
) && type_array_is_decl_as_ptr(type
)))
1552 unsigned int memsize
;
1554 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1555 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1557 /* pointer instance */
1558 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1559 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1560 *typestring_offset
+= 6;
1564 if (is_string_type(attrs
, type
))
1565 write_string_tfs(file
, attrs
, type
, FALSE
, NULL
, typestring_offset
);
1567 write_pointer_tfs(file
, attrs
, type
, FALSE
, typestring_offset
);
1571 unsigned int offset
= type
->typestring_offset
;
1572 /* skip over the pointer that is written for strings, since a
1573 * pointer has to be written in-place here */
1574 if (is_string_type(attrs
, type
))
1576 write_nonsimple_pointer(file
, attrs
, type
, FALSE
, offset
, typestring_offset
);
1580 memsize
= type_memsize(type
, &align
);
1581 *offset_in_memory
+= memsize
;
1582 /* increment these separately as in the case of conformant (varying)
1583 * structures these start at different values */
1584 *offset_in_buffer
+= memsize
;
1589 if (is_non_complex_struct(type
))
1592 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1594 if (offset_in_memory
&& offset_in_buffer
)
1596 unsigned int padding
;
1598 type_memsize(v
->type
, &align
);
1599 padding
= ROUNDING(*offset_in_memory
, align
);
1600 *offset_in_memory
+= padding
;
1601 *offset_in_buffer
+= padding
;
1603 written
+= write_no_repeat_pointer_descriptions(
1604 file
, v
->attrs
, v
->type
,
1605 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1610 unsigned int memsize
;
1612 memsize
= type_memsize(type
, &align
);
1613 *offset_in_memory
+= memsize
;
1614 /* increment these separately as in the case of conformant (varying)
1615 * structures these start at different values */
1616 *offset_in_buffer
+= memsize
;
1622 static int write_pointer_description_offsets(
1623 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1624 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1625 unsigned int *typestring_offset
)
1630 if (is_ptr(type
) && type_get_type(type_pointer_get_ref(type
)) != TYPE_INTERFACE
)
1632 type_t
*ref
= type_pointer_get_ref(type
);
1634 if (offset_in_memory
&& offset_in_buffer
)
1636 unsigned int memsize
;
1638 /* pointer instance */
1639 /* FIXME: sometimes from end of structure, sometimes from beginning */
1640 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1641 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1644 memsize
= type_memsize(type
, &align
);
1645 *offset_in_memory
+= memsize
;
1646 /* increment these separately as in the case of conformant (varying)
1647 * structures these start at different values */
1648 *offset_in_buffer
+= memsize
;
1650 *typestring_offset
+= 4;
1652 if (is_string_type(attrs
, type
))
1653 write_string_tfs(file
, attrs
, type
, FALSE
, NULL
, typestring_offset
);
1654 else if (processed(ref
) || type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
)
1655 write_pointer_tfs(file
, attrs
, type
, FALSE
, typestring_offset
);
1657 error("write_pointer_description_offsets: type format string unknown\n");
1664 return write_pointer_description_offsets(
1665 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1666 offset_in_buffer
, typestring_offset
);
1668 else if (is_non_complex_struct(type
))
1670 /* otherwise search for interesting fields to parse */
1672 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1674 if (offset_in_memory
&& offset_in_buffer
)
1676 unsigned int padding
;
1678 type_memsize(v
->type
, &align
);
1679 padding
= ROUNDING(*offset_in_memory
, align
);
1680 *offset_in_memory
+= padding
;
1681 *offset_in_buffer
+= padding
;
1683 written
+= write_pointer_description_offsets(
1684 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1690 if (offset_in_memory
&& offset_in_buffer
)
1692 unsigned int memsize
;
1694 memsize
= type_memsize(type
, &align
);
1695 *offset_in_memory
+= memsize
;
1696 /* increment these separately as in the case of conformant (varying)
1697 * structures these start at different values */
1698 *offset_in_buffer
+= memsize
;
1705 /* Note: if file is NULL return value is number of pointers to write, else
1706 * it is the number of type format characters written */
1707 static int write_fixed_array_pointer_descriptions(
1708 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1709 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1710 unsigned int *typestring_offset
)
1713 int pointer_count
= 0;
1715 if (type_get_type(type
) == TYPE_ARRAY
&&
1716 !type_array_has_conformance(type
) && !type_array_has_variance(type
))
1718 unsigned int temp
= 0;
1719 /* unfortunately, this needs to be done in two passes to avoid
1720 * writing out redundant FC_FIXED_REPEAT descriptions */
1721 pointer_count
= write_pointer_description_offsets(
1722 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1723 if (pointer_count
> 0)
1725 unsigned int increment_size
;
1726 unsigned int offset_of_array_pointer_mem
= 0;
1727 unsigned int offset_of_array_pointer_buf
= 0;
1730 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1732 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1733 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1734 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type
), type_array_get_dim(type
));
1735 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1736 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1737 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1738 *typestring_offset
+= 10;
1740 pointer_count
= write_pointer_description_offsets(
1741 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1742 &offset_of_array_pointer_buf
, typestring_offset
);
1745 else if (type_get_type(type
) == TYPE_STRUCT
)
1748 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1750 if (offset_in_memory
&& offset_in_buffer
)
1752 unsigned int padding
;
1754 type_memsize(v
->type
, &align
);
1755 padding
= ROUNDING(*offset_in_memory
, align
);
1756 *offset_in_memory
+= padding
;
1757 *offset_in_buffer
+= padding
;
1759 pointer_count
+= write_fixed_array_pointer_descriptions(
1760 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1766 if (offset_in_memory
&& offset_in_buffer
)
1768 unsigned int memsize
;
1770 memsize
= type_memsize(type
, &align
);
1771 *offset_in_memory
+= memsize
;
1772 /* increment these separately as in the case of conformant (varying)
1773 * structures these start at different values */
1774 *offset_in_buffer
+= memsize
;
1778 return pointer_count
;
1781 /* Note: if file is NULL return value is number of pointers to write, else
1782 * it is the number of type format characters written */
1783 static int write_conformant_array_pointer_descriptions(
1784 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1785 unsigned int offset_in_memory
, unsigned int *typestring_offset
)
1788 int pointer_count
= 0;
1790 if (is_conformant_array(type
) && !type_array_has_variance(type
))
1792 unsigned int temp
= 0;
1793 /* unfortunately, this needs to be done in two passes to avoid
1794 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1795 pointer_count
= write_pointer_description_offsets(
1796 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1797 if (pointer_count
> 0)
1799 unsigned int increment_size
;
1800 unsigned int offset_of_array_pointer_mem
= offset_in_memory
;
1801 unsigned int offset_of_array_pointer_buf
= offset_in_memory
;
1804 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1806 if (increment_size
> USHRT_MAX
)
1807 error("array size of %u bytes is too large\n", increment_size
);
1809 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1810 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1811 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1812 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1813 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1814 *typestring_offset
+= 8;
1816 pointer_count
= write_pointer_description_offsets(
1817 file
, attrs
, type_array_get_element(type
),
1818 &offset_of_array_pointer_mem
, &offset_of_array_pointer_buf
,
1823 return pointer_count
;
1826 /* Note: if file is NULL return value is number of pointers to write, else
1827 * it is the number of type format characters written */
1828 static int write_varying_array_pointer_descriptions(
1829 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1830 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1831 unsigned int *typestring_offset
)
1834 int pointer_count
= 0;
1836 if (is_array(type
) && type_array_has_variance(type
))
1838 unsigned int temp
= 0;
1839 /* unfortunately, this needs to be done in two passes to avoid
1840 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1841 pointer_count
= write_pointer_description_offsets(
1842 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1843 if (pointer_count
> 0)
1845 unsigned int increment_size
;
1848 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1850 if (increment_size
> USHRT_MAX
)
1851 error("array size of %u bytes is too large\n", increment_size
);
1853 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1854 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1855 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1856 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1857 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1858 *typestring_offset
+= 8;
1860 pointer_count
= write_pointer_description_offsets(
1861 file
, attrs
, type
, offset_in_memory
,
1862 offset_in_buffer
, typestring_offset
);
1865 else if (type_get_type(type
) == TYPE_STRUCT
)
1868 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1870 if (offset_in_memory
&& offset_in_buffer
)
1872 unsigned int padding
;
1874 if (is_array(v
->type
) && type_array_has_variance(v
->type
))
1876 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
1877 /* skip over variance and offset in buffer */
1878 *offset_in_buffer
+= 8;
1882 type_memsize(v
->type
, &align
);
1883 padding
= ROUNDING(*offset_in_memory
, align
);
1884 *offset_in_memory
+= padding
;
1885 *offset_in_buffer
+= padding
;
1887 pointer_count
+= write_varying_array_pointer_descriptions(
1888 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1894 if (offset_in_memory
&& offset_in_buffer
)
1896 unsigned int memsize
;
1898 memsize
= type_memsize(type
, &align
);
1899 *offset_in_memory
+= memsize
;
1900 /* increment these separately as in the case of conformant (varying)
1901 * structures these start at different values */
1902 *offset_in_buffer
+= memsize
;
1906 return pointer_count
;
1909 static void write_pointer_description(FILE *file
, type_t
*type
,
1910 unsigned int *typestring_offset
)
1912 unsigned int offset_in_buffer
;
1913 unsigned int offset_in_memory
;
1915 /* pass 1: search for single instance of a pointer (i.e. don't descend
1917 if (!is_array(type
))
1919 offset_in_memory
= 0;
1920 offset_in_buffer
= 0;
1921 write_no_repeat_pointer_descriptions(
1923 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1926 /* pass 2: search for pointers in fixed arrays */
1927 offset_in_memory
= 0;
1928 offset_in_buffer
= 0;
1929 write_fixed_array_pointer_descriptions(
1931 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1933 /* pass 3: search for pointers in conformant only arrays (but don't descend
1934 * into conformant varying or varying arrays) */
1935 if (is_conformant_array(type
) &&
1936 (type_array_is_decl_as_ptr(type
) || !current_structure
))
1937 write_conformant_array_pointer_descriptions(
1938 file
, NULL
, type
, 0, typestring_offset
);
1939 else if (type_get_type(type
) == TYPE_STRUCT
&&
1940 get_struct_fc(type
) == RPC_FC_CPSTRUCT
)
1942 unsigned int align
= 0;
1943 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
1944 write_conformant_array_pointer_descriptions(
1946 type_memsize(type
, &align
),
1950 /* pass 4: search for pointers in varying arrays */
1951 offset_in_memory
= 0;
1952 offset_in_buffer
= 0;
1953 write_varying_array_pointer_descriptions(
1955 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1958 int is_declptr(const type_t
*t
)
1960 return is_ptr(t
) || (type_get_type(t
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(t
));
1963 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1964 type_t
*type
, int toplevel_param
,
1965 const char *name
, unsigned int *typestring_offset
)
1967 unsigned int start_offset
;
1968 unsigned char rtype
;
1971 start_offset
= *typestring_offset
;
1972 update_tfsoff(type
, start_offset
, file
);
1974 if (is_declptr(type
))
1976 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1977 int pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
1979 pointer_type
= RPC_FC_RP
;
1980 print_start_tfs_comment(file
, type
, *typestring_offset
);
1981 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1982 pointer_type
, flag
, string_of_type(pointer_type
),
1983 flag
? " [simple_pointer]" : "");
1984 *typestring_offset
+= 2;
1987 print_file(file
, 2, "NdrFcShort(0x2),\n");
1988 *typestring_offset
+= 2;
1993 elem_type
= type_array_get_element(type
);
1995 elem_type
= type_pointer_get_ref(type
);
1997 if (type_get_type(elem_type
) != TYPE_BASIC
)
1999 error("write_string_tfs: Unimplemented for non-basic type %s\n", name
);
2000 return start_offset
;
2003 rtype
= get_basic_fc(elem_type
);
2004 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
2006 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
2007 return start_offset
;
2010 if (type_get_type(type
) == TYPE_ARRAY
&& !type_array_has_conformance(type
))
2012 unsigned int dim
= type_array_get_dim(type
);
2014 /* FIXME: multi-dimensional array */
2016 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2017 name
, 0xffffu
, dim
- 0xffffu
);
2019 if (rtype
== RPC_FC_WCHAR
)
2020 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
2022 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
2023 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2024 *typestring_offset
+= 2;
2026 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim
, dim
);
2027 *typestring_offset
+= 2;
2029 return start_offset
;
2031 else if (is_conformant_array(type
))
2033 unsigned int align
= 0;
2035 if (rtype
== RPC_FC_WCHAR
)
2036 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2038 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2039 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
2040 *typestring_offset
+= 2;
2042 *typestring_offset
+= write_conf_or_var_desc(
2043 file
, current_structure
,
2044 (!type_array_is_decl_as_ptr(type
) && current_structure
2045 ? type_memsize(current_structure
, &align
)
2047 type
, type_array_get_conformance(type
));
2049 return start_offset
;
2053 if (rtype
== RPC_FC_WCHAR
)
2054 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2056 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2057 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2058 *typestring_offset
+= 2;
2060 return start_offset
;
2064 static unsigned int write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2065 const char *name
, unsigned int *typestring_offset
)
2067 const expr_t
*length_is
= type_array_get_variance(type
);
2068 const expr_t
*size_is
= type_array_get_conformance(type
);
2069 unsigned int align
= 0;
2071 unsigned int start_offset
;
2074 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
2075 unsigned int baseoff
2076 = !type_array_is_decl_as_ptr(type
) && current_structure
2077 ? type_memsize(current_structure
, &align
)
2081 pointer_type
= RPC_FC_RP
;
2083 if (write_embedded_types(file
, attrs
, type_array_get_element(type
), name
, FALSE
, typestring_offset
))
2086 has_pointer
= type_has_pointers(type_array_get_element(type
));
2089 size
= type_memsize((is_conformant_array(type
) ? type_array_get_element(type
) : type
), &align
);
2090 fc
= get_array_fc(type
);
2092 start_offset
= *typestring_offset
;
2093 update_tfsoff(type
, start_offset
, file
);
2094 print_start_tfs_comment(file
, type
, start_offset
);
2095 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
2096 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2097 *typestring_offset
+= 2;
2100 if (fc
!= RPC_FC_BOGUS_ARRAY
)
2102 if (fc
== RPC_FC_LGFARRAY
|| fc
== RPC_FC_LGVARRAY
)
2104 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", size
, size
);
2105 *typestring_offset
+= 4;
2109 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
2110 *typestring_offset
+= 2;
2113 if (is_conformant_array(type
))
2115 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2118 if (fc
== RPC_FC_SMVARRAY
|| fc
== RPC_FC_LGVARRAY
)
2120 unsigned int elalign
= 0;
2121 unsigned int elsize
= type_memsize(type_array_get_element(type
), &elalign
);
2122 unsigned int dim
= type_array_get_dim(type
);
2124 if (fc
== RPC_FC_LGVARRAY
)
2126 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim
, dim
);
2127 *typestring_offset
+= 4;
2131 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2132 *typestring_offset
+= 2;
2135 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize
, elsize
);
2136 *typestring_offset
+= 2;
2141 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2144 if (has_pointer
&& (type_array_is_decl_as_ptr(type
) || !current_structure
))
2146 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2147 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2148 *typestring_offset
+= 2;
2149 write_pointer_description(file
, type
, typestring_offset
);
2150 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2151 *typestring_offset
+= 1;
2154 write_member_type(file
, type
, FALSE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2155 write_end(file
, typestring_offset
);
2159 unsigned int dim
= size_is
? 0 : type_array_get_dim(type
);
2160 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2161 *typestring_offset
+= 2;
2163 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2166 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2168 write_member_type(file
, type
, TRUE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2169 write_end(file
, typestring_offset
);
2172 return start_offset
;
2175 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
2177 const var_list_t
*fields
= type_struct_get_fields(type
);
2178 const var_t
*last_field
;
2181 if (!fields
|| list_empty(fields
))
2184 last_field
= LIST_ENTRY( list_tail(fields
), const var_t
, entry
);
2185 ft
= last_field
->type
;
2187 if (is_conformant_array(ft
) && !type_array_is_decl_as_ptr(ft
))
2190 if (type_get_type(ft
) == TYPE_STRUCT
)
2191 return find_array_or_string_in_struct(ft
);
2196 static void write_struct_members(FILE *file
, const type_t
*type
,
2197 int is_complex
, unsigned int *corroff
,
2198 unsigned int *typestring_offset
)
2201 unsigned short offset
= 0;
2204 var_list_t
*fields
= type_struct_get_fields(type
);
2206 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2208 type_t
*ft
= field
->type
;
2209 if (!is_conformant_array(ft
) || type_array_is_decl_as_ptr(ft
))
2211 unsigned int align
= 0;
2212 unsigned int size
= type_memsize(ft
, &align
);
2215 if ((align
- 1) & offset
)
2217 unsigned char fc
= 0;
2221 fc
= RPC_FC_ALIGNM4
;
2224 fc
= RPC_FC_ALIGNM8
;
2227 error("write_struct_members: cannot align type %d\n", type_get_type(ft
));
2229 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2230 offset
= ROUND_SIZE(offset
, align
);
2231 *typestring_offset
+= 1;
2233 write_member_type(file
, type
, is_complex
, field
->attrs
, field
->type
, corroff
,
2239 padding
= ROUNDING(offset
, salign
);
2242 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2243 RPC_FC_STRUCTPAD1
+ padding
- 1,
2245 *typestring_offset
+= 1;
2248 write_end(file
, typestring_offset
);
2251 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
,
2252 const char *name
, unsigned int *tfsoff
)
2254 const type_t
*save_current_structure
= current_structure
;
2255 unsigned int total_size
;
2257 unsigned int start_offset
;
2258 unsigned int array_offset
;
2259 int has_pointers
= 0;
2260 unsigned int align
= 0;
2261 unsigned int corroff
;
2263 unsigned char fc
= get_struct_fc(type
);
2264 var_list_t
*fields
= type_struct_get_fields(type
);
2267 current_structure
= type
;
2269 total_size
= type_memsize(type
, &align
);
2270 if (total_size
> USHRT_MAX
)
2271 error("structure size for %s exceeds %d bytes by %d bytes\n",
2272 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
2274 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2275 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
2277 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
2279 array
= find_array_or_string_in_struct(type
);
2280 if (array
&& !processed(array
->type
))
2282 = is_string_type(array
->attrs
, array
->type
)
2283 ? write_string_tfs(file
, array
->attrs
, array
->type
, FALSE
, array
->name
, tfsoff
)
2284 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
2287 write_descriptors(file
, type
, tfsoff
);
2289 start_offset
= *tfsoff
;
2290 update_tfsoff(type
, start_offset
, file
);
2291 print_start_tfs_comment(file
, type
, start_offset
);
2292 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2293 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2294 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size
, total_size
);
2299 unsigned int absoff
= array
->type
->typestring_offset
;
2300 short reloff
= absoff
- *tfsoff
;
2301 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2302 reloff
, reloff
, absoff
);
2305 else if (fc
== RPC_FC_BOGUS_STRUCT
)
2307 print_file(file
, 2, "NdrFcShort(0x0),\n");
2311 if (fc
== RPC_FC_BOGUS_STRUCT
)
2313 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2314 nothing is written to file yet. On the actual writing pass,
2315 this will have been updated. */
2316 unsigned int absoff
= type_get_real_type(type
)->ptrdesc
?
2317 type_get_real_type(type
)->ptrdesc
: *tfsoff
;
2318 int reloff
= absoff
- *tfsoff
;
2319 assert( reloff
>= 0 );
2320 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2321 reloff
, reloff
, absoff
);
2324 else if ((fc
== RPC_FC_PSTRUCT
) ||
2325 (fc
== RPC_FC_CPSTRUCT
) ||
2326 (fc
== RPC_FC_CVSTRUCT
&& has_pointers
))
2328 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2329 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2331 write_pointer_description(file
, type
, tfsoff
);
2332 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2336 write_struct_members(file
, type
, fc
== RPC_FC_BOGUS_STRUCT
, &corroff
,
2339 if (fc
== RPC_FC_BOGUS_STRUCT
)
2343 type_get_real_type(type
)->ptrdesc
= *tfsoff
;
2344 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
2346 type_t
*ft
= f
->type
;
2349 if (is_string_type(f
->attrs
, ft
))
2350 write_string_tfs(file
, f
->attrs
, ft
, FALSE
, f
->name
, tfsoff
);
2352 write_pointer_tfs(file
, f
->attrs
, ft
, FALSE
, tfsoff
);
2354 else if (type_get_type(ft
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(ft
))
2356 unsigned int offset
;
2358 print_file(file
, 0, "/* %d */\n", *tfsoff
);
2360 offset
= ft
->typestring_offset
;
2361 /* skip over the pointer that is written for strings, since a
2362 * pointer has to be written in-place here */
2363 if (is_string_type(f
->attrs
, ft
))
2365 write_nonsimple_pointer(file
, f
->attrs
, ft
, FALSE
, offset
, tfsoff
);
2368 if (type_get_real_type(type
)->ptrdesc
== *tfsoff
)
2369 type_get_real_type(type
)->ptrdesc
= 0;
2372 current_structure
= save_current_structure
;
2373 return start_offset
;
2376 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
2380 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
2384 if (type_get_type(t
) == TYPE_BASIC
|| type_get_type(t
) == TYPE_ENUM
)
2387 if (type_get_type(t
) == TYPE_BASIC
)
2388 fc
= get_basic_fc(t
);
2390 fc
= get_enum_fc(t
);
2391 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2392 fc
, string_of_type(fc
));
2394 else if (t
->typestring_offset
)
2396 short reloff
= t
->typestring_offset
- *tfsoff
;
2397 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2398 reloff
, reloff
, t
->typestring_offset
);
2401 error("write_branch_type: type unimplemented %d\n", type_get_type(t
));
2407 static unsigned int write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2410 unsigned int start_offset
;
2413 unsigned int nbranch
= 0;
2414 type_t
*deftype
= NULL
;
2415 short nodeftype
= 0xffff;
2421 size
= type_memsize(type
, &align
);
2423 fields
= type_union_get_cases(type
);
2425 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2427 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2429 nbranch
+= list_count(cases
);
2431 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2434 start_offset
= *tfsoff
;
2435 update_tfsoff(type
, start_offset
, file
);
2436 print_start_tfs_comment(file
, type
, start_offset
);
2437 if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
2439 const var_t
*sv
= type_union_get_switch_value(type
);
2440 const type_t
*st
= sv
->type
;
2443 if (type_get_type(st
) == TYPE_BASIC
)
2445 switch (get_basic_fc(st
))
2456 fc
= get_basic_fc(st
);
2460 error("union switch type must be an integer, char, or enum\n");
2463 else if (type_get_type(st
) == TYPE_ENUM
)
2464 fc
= get_enum_fc(st
);
2466 error("union switch type must be an integer, char, or enum\n");
2468 print_file(file
, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION
);
2469 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2470 0x40 | fc
, string_of_type(fc
));
2473 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
2475 static const expr_t dummy_expr
; /* FIXME */
2476 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
2479 if (type_get_type(st
) == TYPE_BASIC
)
2481 switch (get_basic_fc(st
))
2492 fc
= get_basic_fc(st
);
2496 error("union switch type must be an integer, char, or enum\n");
2499 else if (type_get_type(st
) == TYPE_ENUM
)
2500 fc
= get_enum_fc(st
);
2502 error("union switch type must be an integer, char, or enum\n");
2504 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
2505 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2506 fc
, string_of_type(fc
));
2509 *tfsoff
+= write_conf_or_var_desc(file
, NULL
, *tfsoff
, st
, &dummy_expr
);
2510 print_file(file
, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff
+ 2);
2514 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size
, size
);
2515 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch
, nbranch
);
2518 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2520 type_t
*ft
= f
->type
;
2521 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2522 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2525 if (cases
== NULL
&& !deflt
)
2526 error("union field %s with neither case nor default attribute\n", f
->name
);
2528 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2530 /* MIDL doesn't check for duplicate cases, even though that seems
2531 like a reasonable thing to do, it just dumps them to the TFS
2532 like we're going to do here. */
2533 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c
->cval
, c
->cval
);
2535 write_branch_type(file
, ft
, tfsoff
);
2538 /* MIDL allows multiple default branches, even though that seems
2539 illogical, it just chooses the last one, which is what we will
2550 write_branch_type(file
, deftype
, tfsoff
);
2554 print_file(file
, 2, "NdrFcShort(0x%hx),\n", nodeftype
);
2558 return start_offset
;
2561 static unsigned int write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2562 unsigned int *typeformat_offset
)
2565 unsigned int start_offset
= *typeformat_offset
;
2566 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2570 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2571 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2573 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2577 const type_t
*base
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
2578 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2581 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2583 update_tfsoff(type
, start_offset
, file
);
2584 print_start_tfs_comment(file
, type
, start_offset
);
2585 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2586 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2587 print_file(file
, 2, "NdrFcLong(0x%08x),\n", uuid
->Data1
);
2588 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2589 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2590 for (i
= 0; i
< 8; ++i
)
2591 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2594 fprintf(file
, "\n");
2596 *typeformat_offset
+= 18;
2598 return start_offset
;
2601 static unsigned int write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2603 unsigned int *typeformat_offset
)
2605 unsigned int start_offset
= *typeformat_offset
;
2606 unsigned char flags
= 0;
2608 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2609 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2613 if (is_attr(var
->attrs
, ATTR_IN
))
2616 if (!is_attr(var
->attrs
, ATTR_OUT
))
2617 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2619 if (is_attr(var
->attrs
, ATTR_OUT
))
2622 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2623 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2624 /* return and can't be null values overlap */
2625 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2626 print_file(file
, 0, "can't be null, ");
2627 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2628 print_file(file
, 0, "serialize, ");
2629 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2630 print_file(file
, 0, "no serialize, ");
2631 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2632 print_file(file
, 0, "strict, ");
2633 if ((flags
& 0x21) == 0x20)
2634 print_file(file
, 0, "out, ");
2635 if ((flags
& 0x21) == 0x21)
2636 print_file(file
, 0, "return, ");
2638 print_file(file
, 0, "in, ");
2640 print_file(file
, 0, "via ptr, ");
2641 print_file(file
, 0, "*/\n");
2642 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2643 print_file(file
, 2, "0, /* FIXME: param num */\n");
2644 *typeformat_offset
+= 4;
2646 return start_offset
;
2649 static unsigned int write_typeformatstring_var(FILE *file
, int indent
, const var_t
*func
,
2650 type_t
*type
, const var_t
*var
,
2652 unsigned int *typeformat_offset
)
2654 unsigned int offset
;
2656 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
2658 case TGT_CTXT_HANDLE
:
2659 case TGT_CTXT_HANDLE_POINTER
:
2660 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2662 write_user_tfs(file
, type
, typeformat_offset
);
2663 return type
->typestring_offset
;
2665 return write_string_tfs(file
, var
->attrs
, type
, toplevel_param
, var
->name
, typeformat_offset
);
2670 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2671 ptr_type
= get_pointer_fc(type
, var
->attrs
, toplevel_param
);
2672 if (ptr_type
!= RPC_FC_RP
)
2674 unsigned int absoff
= type
->typestring_offset
;
2675 short reloff
= absoff
- (*typeformat_offset
+ 2);
2676 off
= *typeformat_offset
;
2677 print_file(file
, 0, "/* %d */\n", off
);
2678 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2679 string_of_type(ptr_type
));
2680 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2681 reloff
, reloff
, absoff
);
2682 *typeformat_offset
+= 4;
2687 if (processed(type
)) return type
->typestring_offset
;
2688 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2690 if (processed(type
)) return type
->typestring_offset
;
2691 return write_union_tfs(file
, type
, typeformat_offset
);
2696 case TGT_IFACE_POINTER
:
2697 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2701 size_t start_offset
= *typeformat_offset
;
2702 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2703 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2704 const type_t
*ref
= type_pointer_get_ref(type
);
2706 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
2708 /* special case for pointers to base types */
2714 if (type_get_type(ref
) == TYPE_ENUM
)
2715 fc
= get_enum_fc(ref
);
2717 fc
= get_basic_fc(ref
);
2719 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2720 get_pointer_fc(type
, var
->attrs
, toplevel_param
),
2721 (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2722 string_of_type(get_pointer_fc(type
, var
->attrs
, toplevel_param
)),
2723 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2724 print_file(file
, indent
, "0x%02x, /* %s */\n",
2725 fc
, string_of_type(fc
));
2726 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2727 *typeformat_offset
+= 4;
2728 return start_offset
;
2735 offset
= write_typeformatstring_var(file
, indent
, func
,
2736 type_pointer_get_ref(type
), var
,
2737 FALSE
, typeformat_offset
);
2739 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2740 return write_nonsimple_pointer(file
, var
->attrs
, type
,
2742 offset
, typeformat_offset
);
2746 error("invalid type %s for var %s\n", type
->name
, var
->name
);
2750 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2751 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2755 switch (typegen_detect_type(type
, attrs
, TDT_ALL_TYPES
))
2758 write_user_tfs(file
, type
, tfsoff
);
2761 write_string_tfs(file
, attrs
, type
, FALSE
, name
, tfsoff
);
2763 case TGT_IFACE_POINTER
:
2764 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2768 type_t
*ref
= type_pointer_get_ref(type
);
2770 if (!processed(ref
) && type_get_type(ref
) != TYPE_BASIC
)
2771 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2774 write_pointer_tfs(file
, attrs
, type
, FALSE
, tfsoff
);
2780 /* conformant arrays and strings are handled specially */
2781 if (!is_conformant_array(type
) || type_array_is_decl_as_ptr(type
) )
2783 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2784 if (is_conformant_array(type
))
2789 if (!processed(type
))
2790 write_struct_tfs(file
, type
, name
, tfsoff
);
2793 if (!processed(type
))
2794 write_union_tfs(file
, type
, tfsoff
);
2800 case TGT_CTXT_HANDLE
:
2801 case TGT_CTXT_HANDLE_POINTER
:
2803 error("invalid type %s for var %s\n", type
->name
, name
);
2810 static unsigned int process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2811 type_pred_t pred
, unsigned int *typeformat_offset
)
2814 const statement_t
*stmt
;
2816 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2818 const type_t
*iface
;
2819 const statement_t
*stmt_func
;
2821 if (stmt
->type
== STMT_LIBRARY
)
2823 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2826 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
2829 iface
= stmt
->u
.type
;
2833 current_iface
= iface
;
2834 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
2836 const var_t
*func
= stmt_func
->u
.var
;
2837 if (is_local(func
->attrs
)) continue;
2839 if (!is_void(type_function_get_rettype(func
->type
)))
2842 v
.type
= type_function_get_rettype(func
->type
);
2843 update_tfsoff(type_function_get_rettype(func
->type
),
2844 write_typeformatstring_var(
2846 type_function_get_rettype(func
->type
),
2847 &v
, FALSE
, typeformat_offset
),
2851 current_func
= func
;
2852 if (type_get_function_args(func
->type
))
2853 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2856 write_typeformatstring_var(
2857 file
, 2, func
, var
->type
, var
,
2858 TRUE
, typeformat_offset
),
2863 return *typeformat_offset
+ 1;
2866 static unsigned int process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2868 unsigned int typeformat_offset
= 2;
2870 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2874 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2878 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2879 print_file(file
, indent
, "{\n");
2881 print_file(file
, indent
, "0,\n");
2882 print_file(file
, indent
, "{\n");
2884 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2886 set_all_tfswrite(TRUE
);
2887 process_tfs(file
, stmts
, pred
);
2889 print_file(file
, indent
, "0x0\n");
2891 print_file(file
, indent
, "}\n");
2893 print_file(file
, indent
, "};\n");
2894 print_file(file
, indent
, "\n");
2897 static unsigned int get_required_buffer_size_type(
2898 const type_t
*type
, const char *name
, const attr_list_t
*attrs
, int toplevel_param
, unsigned int *alignment
)
2901 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
2906 const type_t
*utype
= get_user_type(type
, &uname
);
2907 return get_required_buffer_size_type(utype
, uname
, NULL
, FALSE
, alignment
);
2910 switch (get_basic_fc(type
))
2928 case RPC_FC_ERROR_STATUS_T
:
2938 case RPC_FC_BIND_PRIMITIVE
:
2942 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2943 get_basic_fc(type
));
2949 switch (get_enum_fc(type
))
2961 if (get_struct_fc(type
) == RPC_FC_STRUCT
)
2963 if (!type_struct_get_fields(type
)) return 0;
2964 return fields_memsize(type_struct_get_fields(type
), alignment
);
2969 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_RP
)
2971 const type_t
*ref
= type_pointer_get_ref(type
);
2972 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
2976 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
2978 if (get_struct_fc(ref
) == RPC_FC_STRUCT
)
2979 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
2982 case TGT_CTXT_HANDLE
:
2983 case TGT_CTXT_HANDLE_POINTER
:
2987 case TGT_IFACE_POINTER
:
2996 /* FIXME: depends on pointer type */
2997 return type_array_get_dim(type
) *
2998 get_required_buffer_size_type(type_array_get_element(type
), name
, NULL
, FALSE
, alignment
);
3006 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
3008 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3009 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3011 if (!in_attr
&& !out_attr
)
3016 if ((pass
== PASS_IN
&& in_attr
) || (pass
== PASS_OUT
&& out_attr
) ||
3017 pass
== PASS_RETURN
)
3019 if (is_ptrchain_attr(var
, ATTR_CONTEXTHANDLE
))
3025 if (!is_string_type(var
->attrs
, var
->type
))
3026 return get_required_buffer_size_type(var
->type
, var
->name
,
3027 var
->attrs
, TRUE
, alignment
);
3032 static unsigned int get_function_buffer_size( const var_t
*func
, enum pass pass
)
3035 unsigned int total_size
= 0, alignment
;
3037 if (type_get_function_args(func
->type
))
3039 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3041 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
3042 total_size
+= alignment
;
3046 if (pass
== PASS_OUT
&& !is_void(type_function_get_rettype(func
->type
)))
3049 v
.type
= type_function_get_rettype(func
->type
);
3050 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
3051 total_size
+= alignment
;
3056 static void print_phase_function(FILE *file
, int indent
, const char *type
,
3057 const char *local_var_prefix
, enum remoting_phase phase
,
3058 const var_t
*var
, unsigned int type_offset
)
3060 const char *function
;
3063 case PHASE_BUFFERSIZE
:
3064 function
= "BufferSize";
3067 function
= "Marshall";
3069 case PHASE_UNMARSHAL
:
3070 function
= "Unmarshall";
3080 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
3082 print_file(file
, indent
, "&__frame->_StubMsg,\n");
3083 print_file(file
, indent
, "%s%s%s%s%s,\n",
3084 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
3085 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
3087 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
3089 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3090 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
3091 if (phase
== PHASE_UNMARSHAL
)
3092 print_file(file
, indent
, "0);\n");
3096 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
3097 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
3098 const char *varname
)
3100 type_t
*type
= var
->type
;
3102 unsigned int alignment
= 0;
3105 /* no work to do for other phases, buffer sizing is done elsewhere */
3106 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
3109 ref
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3110 if (type_get_type(ref
) == TYPE_ENUM
)
3112 if (get_enum_fc(ref
) == RPC_FC_ENUM32
)
3117 else /* RPC_FC_ENUM16 */
3125 switch (get_basic_fc(ref
))
3145 case RPC_FC_ERROR_STATUS_T
:
3157 case RPC_FC_BIND_PRIMITIVE
:
3158 /* no marshalling needed */
3162 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3163 var
->name
, get_basic_fc(ref
));
3168 if (phase
== PHASE_MARSHAL
)
3169 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
3170 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3171 alignment
- 1, alignment
- 1);
3173 if (phase
== PHASE_MARSHAL
)
3175 print_file(file
, indent
, "*(");
3176 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3178 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
3180 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
3181 fprintf(file
, "%s%s", local_var_prefix
, varname
);
3182 fprintf(file
, ";\n");
3184 else if (phase
== PHASE_UNMARSHAL
)
3186 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
3187 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3188 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
3189 print_file(file
, indent
, "{\n");
3190 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3191 print_file(file
, indent
, "}\n");
3192 print_file(file
, indent
, "%s%s%s",
3193 (pass
== PASS_IN
|| pass
== PASS_RETURN
) ? "" : "*",
3194 local_var_prefix
, varname
);
3195 if (pass
== PASS_IN
&& is_ptr(type
))
3196 fprintf(file
, " = (");
3198 fprintf(file
, " = *(");
3199 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3200 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
3203 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
3204 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3205 fprintf(file
, ");\n");
3208 /* returns whether the MaxCount, Offset or ActualCount members need to be
3209 * filled in for the specified phase */
3210 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
3212 return (phase
!= PHASE_UNMARSHAL
);
3215 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
3219 for ( ; is_array(t
); t
= type_array_get_element(t
))
3220 if (type_array_has_conformance(t
))
3223 x
= type_array_get_conformance(t
);
3225 error("%s: multidimensional conformant"
3226 " arrays not supported at the top level\n",
3233 static void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
3234 enum remoting_phase phase
, const var_t
*var
)
3236 const type_t
*type
= var
->type
;
3237 /* get fundamental type for the argument */
3240 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
3242 else if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
))
3244 else if (type_is_alias(type
))
3245 type
= type_alias_get_aliasee(type
);
3246 else if (is_array(type
))
3248 if (is_conformance_needed_for_phase(phase
) && is_array(type
))
3250 if (type_array_has_conformance(type
))
3252 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3253 write_expr(file
, type_array_get_conformance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3254 fprintf(file
, ";\n\n");
3256 if (type_array_has_variance(type
))
3258 print_file(file
, indent
, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3259 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3260 write_expr(file
, type_array_get_variance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3261 fprintf(file
, ";\n\n");
3266 else if (type_get_type(type
) == TYPE_UNION
)
3268 if (is_conformance_needed_for_phase(phase
))
3270 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3271 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
3272 fprintf(file
, ";\n\n");
3276 else if (type_get_type(type
) == TYPE_INTERFACE
|| is_void(type
))
3280 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3282 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3283 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
3284 fprintf( file
, ";\n\n" );
3288 else if (is_ptr(type
))
3289 type
= type_pointer_get_ref(type
);
3295 static void write_remoting_arg(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3296 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
3298 int in_attr
, out_attr
, pointer_type
;
3299 const type_t
*type
= var
->type
;
3300 unsigned int start_offset
= type
->typestring_offset
;
3302 if (is_ptr(type
) || is_array(type
))
3303 pointer_type
= get_pointer_fc(type
, var
->attrs
, pass
!= PASS_RETURN
);
3307 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3308 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3309 if (!in_attr
&& !out_attr
)
3312 if (phase
!= PHASE_FREE
)
3316 if (!in_attr
) return;
3319 if (!out_attr
) return;
3325 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
);
3327 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
3329 case TGT_CTXT_HANDLE
:
3330 case TGT_CTXT_HANDLE_POINTER
:
3331 if (phase
== PHASE_MARSHAL
)
3333 if (pass
== PASS_IN
)
3335 /* if the context_handle attribute appears in the chain of types
3336 * without pointers being followed, then the context handle must
3337 * be direct, otherwise it is a pointer */
3338 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
3339 print_file(file
, indent
, "NdrClientContextMarshall(\n");
3340 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3341 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr
? "*" : "", local_var_prefix
, var
->name
);
3342 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
3346 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
3347 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3348 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
3349 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
3350 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3353 else if (phase
== PHASE_UNMARSHAL
)
3355 if (pass
== PASS_OUT
)
3358 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
3359 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
3360 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3361 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
3362 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
3366 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
3367 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3368 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3373 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
3376 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
||
3377 pointer_type
!= RPC_FC_RP
)
3379 if (pointer_type
== RPC_FC_RP
&& phase
== PHASE_FREE
&&
3380 !in_attr
&& is_conformant_array(type
))
3382 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3384 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3386 /* strings returned are assumed to be global and hence don't
3388 else if (is_declptr(type
) &&
3389 !(phase
== PHASE_FREE
&& pass
== PASS_RETURN
))
3390 print_phase_function(file
, indent
, "Pointer", local_var_prefix
,
3391 phase
, var
, start_offset
);
3395 unsigned int real_start_offset
= start_offset
;
3396 /* skip over pointer description straight to string description */
3397 if (is_declptr(type
))
3399 if (is_conformant_array(type
))
3400 real_start_offset
+= 4;
3402 real_start_offset
+= 2;
3404 if (is_array(type
) && !is_conformant_array(type
))
3405 print_phase_function(file
, indent
, "NonConformantString",
3406 local_var_prefix
, phase
, var
,
3409 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
3410 phase
, var
, real_start_offset
);
3415 unsigned char tc
= get_array_fc(type
);
3416 const char *array_type
= "FixedArray";
3418 /* We already have the size_is expression since it's at the
3419 top level, but do checks for multidimensional conformant
3420 arrays. When we handle them, we'll need to extend this
3421 function to return a list, and then we'll actually use
3422 the return value. */
3423 get_size_is_expr(type
, var
->name
);
3425 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
3427 array_type
= "VaryingArray";
3429 else if (tc
== RPC_FC_CARRAY
)
3431 array_type
= "ConformantArray";
3433 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
3435 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
3437 : "ConformantVaryingArray");
3440 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
3441 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
3442 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
3444 /* these are all unmarshalled by allocating memory */
3445 if (tc
== RPC_FC_BOGUS_ARRAY
||
3446 tc
== RPC_FC_CVARRAY
||
3447 ((tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
) && in_attr
) ||
3448 (tc
== RPC_FC_CARRAY
&& !in_attr
))
3450 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3452 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3458 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3459 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3462 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3464 if (phase
== PHASE_MARSHAL
)
3465 print_file(file
, indent
, "NdrSimpleTypeMarshall(\n");
3467 print_file(file
, indent
, "NdrSimpleTypeUnmarshall(\n");
3468 print_file(file
, indent
+1, "&__frame->_StubMsg,\n");
3469 print_file(file
, indent
+1, "(unsigned char *)&%s%s,\n",
3472 print_file(file
, indent
+1, "0x%02x /* %s */);\n", get_enum_fc(type
), string_of_type(get_enum_fc(type
)));
3476 switch (get_struct_fc(type
))
3479 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3480 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3482 case RPC_FC_PSTRUCT
:
3483 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3485 case RPC_FC_CSTRUCT
:
3486 case RPC_FC_CPSTRUCT
:
3487 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
3489 case RPC_FC_CVSTRUCT
:
3490 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
3492 case RPC_FC_BOGUS_STRUCT
:
3493 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
3496 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(type
));
3501 const char *union_type
= NULL
;
3503 if (type_get_type(type
) == TYPE_UNION
)
3504 union_type
= "NonEncapsulatedUnion";
3505 else if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
3506 union_type
= "EncapsulatedUnion";
3508 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3509 phase
, var
, start_offset
);
3514 const type_t
*ref
= type_pointer_get_ref(type
);
3515 if (pointer_type
== RPC_FC_RP
&& !is_user_type(ref
)) switch (type_get_type(ref
))
3518 /* base types have known sizes, so don't need a sizing pass
3519 * and don't have any memory to free and so don't need a
3521 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3522 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3525 /* base types have known sizes, so don't need a sizing pass
3526 * and don't have any memory to free and so don't need a
3528 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3529 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3533 const char *struct_type
= NULL
;
3534 switch (get_struct_fc(ref
))
3537 /* simple structs have known sizes, so don't need a sizing
3538 * pass and don't have any memory to free and so don't
3539 * need a freeing pass */
3540 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3541 struct_type
= "SimpleStruct";
3542 else if (phase
== PHASE_FREE
&& pass
== PASS_RETURN
)
3544 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3546 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3550 case RPC_FC_PSTRUCT
:
3551 struct_type
= "SimpleStruct";
3553 case RPC_FC_CSTRUCT
:
3554 case RPC_FC_CPSTRUCT
:
3555 struct_type
= "ConformantStruct";
3557 case RPC_FC_CVSTRUCT
:
3558 struct_type
= "ConformantVaryingStruct";
3560 case RPC_FC_BOGUS_STRUCT
:
3561 struct_type
= "ComplexStruct";
3564 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(ref
));
3569 if (phase
== PHASE_FREE
)
3570 struct_type
= "Pointer";
3572 start_offset
= ref
->typestring_offset
;
3573 print_phase_function(file
, indent
, struct_type
, local_var_prefix
, phase
, var
, start_offset
);
3578 case TYPE_ENCAPSULATED_UNION
:
3580 const char *union_type
= NULL
;
3581 if (phase
== PHASE_FREE
)
3582 union_type
= "Pointer";
3585 if (type_get_type(ref
) == TYPE_UNION
)
3586 union_type
= "NonEncapsulatedUnion";
3587 else if (type_get_type(ref
) == TYPE_ENCAPSULATED_UNION
)
3588 union_type
= "EncapsulatedUnion";
3590 start_offset
= ref
->typestring_offset
;
3593 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3594 phase
, var
, start_offset
);
3599 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3606 case TYPE_INTERFACE
:
3611 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3614 case TGT_IFACE_POINTER
:
3615 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3621 fprintf(file
, "\n");
3624 void write_remoting_arguments(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3625 enum pass pass
, enum remoting_phase phase
)
3627 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3629 unsigned int size
= get_function_buffer_size( func
, pass
);
3630 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
3633 if (pass
== PASS_RETURN
)
3637 var
.type
= type_function_get_rettype(func
->type
);
3638 var
.name
= xstrdup( "_RetVal" );
3639 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, &var
);
3645 if (!type_get_function_args(func
->type
))
3647 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3648 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
3653 unsigned int get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3655 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3659 unsigned int get_size_procformatstring_func(const var_t
*func
)
3662 unsigned int size
= 0;
3664 /* argument list size */
3665 if (type_get_function_args(func
->type
))
3666 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3667 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3669 /* return value size */
3670 if (is_void(type_function_get_rettype(func
->type
)))
3671 size
+= 2; /* FC_END and FC_PAD */
3673 size
+= get_size_procformatstring_type("return value", type_function_get_rettype(func
->type
), NULL
);
3678 unsigned int get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3680 const statement_t
*stmt
;
3681 unsigned int size
= 1;
3683 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3685 const type_t
*iface
;
3686 const statement_t
*stmt_func
;
3688 if (stmt
->type
== STMT_LIBRARY
)
3690 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3693 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
3696 iface
= stmt
->u
.type
;
3700 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
3702 const var_t
*func
= stmt_func
->u
.var
;
3703 if (!is_local(func
->attrs
))
3704 size
+= get_size_procformatstring_func( func
);
3710 unsigned int get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3712 set_all_tfswrite(FALSE
);
3713 return process_tfs(NULL
, stmts
, pred
);
3716 void declare_stub_args( FILE *file
, int indent
, const var_t
*func
)
3718 int in_attr
, out_attr
;
3722 /* declare return value '_RetVal' */
3723 if (!is_void(type_function_get_rettype(func
->type
)))
3725 print_file(file
, indent
, "%s", "");
3726 write_type_decl_left(file
, type_function_get_rettype(func
->type
));
3727 fprintf(file
, " _RetVal;\n");
3730 if (!type_get_function_args(func
->type
))
3733 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3735 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3736 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3737 if (!out_attr
&& !in_attr
)
3740 if (is_context_handle(var
->type
))
3741 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3744 if (!in_attr
&& !is_conformant_array(var
->type
))
3746 type_t
*type_to_print
;
3748 print_file(file
, indent
, "%s", "");
3749 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3750 !type_array_is_decl_as_ptr(var
->type
))
3751 type_to_print
= var
->type
;
3753 type_to_print
= type_pointer_get_ref(var
->type
);
3754 sprintf(name
, "_W%u", i
++);
3755 write_type_decl(file
, type_to_print
, name
);
3756 fprintf(file
, ";\n");
3759 print_file(file
, indent
, "%s", "");
3760 write_type_decl_left(file
, var
->type
);
3762 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3763 !type_array_is_decl_as_ptr(var
->type
)) {
3764 fprintf(file
, "(*%s)", var
->name
);
3766 fprintf(file
, "%s", var
->name
);
3767 write_type_right(file
, var
->type
, FALSE
);
3768 fprintf(file
, ";\n");
3770 if (decl_indirect(var
->type
))
3771 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
3777 void assign_stub_out_args( FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
3779 int in_attr
, out_attr
;
3783 if (!type_get_function_args(func
->type
))
3786 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3788 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3789 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3790 if (!out_attr
&& !in_attr
)
3795 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
3797 if (is_context_handle(var
->type
))
3799 fprintf(file
, " = NdrContextHandleInitialize(\n");
3800 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3801 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3802 var
->type
->typestring_offset
);
3804 else if (is_array(var
->type
) &&
3805 type_array_has_conformance(var
->type
))
3807 unsigned int size
, align
= 0;
3808 type_t
*type
= var
->type
;
3810 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
3812 is_array(type
) && type_array_has_conformance(type
);
3813 type
= type_array_get_element(type
))
3815 write_expr(file
, type_array_get_conformance(type
), TRUE
,
3816 TRUE
, NULL
, NULL
, local_var_prefix
);
3817 fprintf(file
, " * ");
3819 size
= type_memsize(type
, &align
);
3820 fprintf(file
, "%u);\n", size
);
3824 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
3825 switch (typegen_detect_type(type_pointer_get_ref(var
->type
), var
->attrs
, TDT_IGNORE_STRINGS
))
3830 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
3835 case TGT_IFACE_POINTER
:
3837 case TGT_CTXT_HANDLE
:
3838 case TGT_CTXT_HANDLE_POINTER
:
3841 /* not initialised */
3851 fprintf(file
, "\n");
3855 int write_expr_eval_routines(FILE *file
, const char *iface
)
3857 static const char *var_name
= "pS";
3858 static const char *var_name_expr
= "pS->";
3860 struct expr_eval_routine
*eval
;
3861 unsigned short callback_offset
= 0;
3863 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3865 const char *name
= eval
->structure
->name
;
3868 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3869 iface
, name
, callback_offset
);
3870 print_file(file
, 0, "{\n");
3871 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3872 name
, var_name
, name
, eval
->baseoff
);
3873 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3874 print_file(file
, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3875 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
, "");
3876 fprintf(file
, ";\n");
3877 print_file(file
, 0, "}\n\n");
3883 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3885 struct expr_eval_routine
*eval
;
3886 struct expr_eval_routine
*cursor
;
3887 unsigned short callback_offset
= 0;
3889 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3890 fprintf(file
, "{\n");
3892 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3894 const char *name
= eval
->structure
->name
;
3895 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3897 list_remove(&eval
->entry
);
3901 fprintf(file
, "};\n\n");
3904 void write_user_quad_list(FILE *file
)
3908 if (list_empty(&user_type_list
))
3911 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3912 fprintf(file
, "{\n");
3913 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3915 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3916 print_file(file
, 1, "{\n");
3917 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3918 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3919 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3920 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3921 print_file(file
, 1, "}%s\n", sep
);
3923 fprintf(file
, "};\n\n");
3926 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3928 const struct str_list_entry_t
*endpoint
;
3931 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3932 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3933 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3935 print_file( f
, 1, "{ (const unsigned char *)\"" );
3936 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3938 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3941 if (!*p
) goto error
;
3942 if (p
[1] != '[') goto error
;
3944 fprintf( f
, "\", (const unsigned char *)\"" );
3945 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3947 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3950 if (*p
!= ']') goto error
;
3951 fprintf( f
, "\" },\n" );
3953 print_file( f
, 0, "};\n\n" );
3957 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
3960 void write_exceptions( FILE *file
)
3962 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3963 fprintf( file
, "\n");
3964 fprintf( file
, "#include \"wine/exception.h\"\n");
3965 fprintf( file
, "#undef RpcTryExcept\n");
3966 fprintf( file
, "#undef RpcExcept\n");
3967 fprintf( file
, "#undef RpcEndExcept\n");
3968 fprintf( file
, "#undef RpcTryFinally\n");
3969 fprintf( file
, "#undef RpcFinally\n");
3970 fprintf( file
, "#undef RpcEndFinally\n");
3971 fprintf( file
, "#undef RpcExceptionCode\n");
3972 fprintf( file
, "#undef RpcAbnormalTermination\n");
3973 fprintf( file
, "\n");
3974 fprintf( file
, "struct __exception_frame;\n");
3975 fprintf( file
, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3976 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3977 fprintf( file
, "\n");
3978 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
3979 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3980 fprintf( file
, " __filter_func filter; \\\n");
3981 fprintf( file
, " __finally_func finally; \\\n");
3982 fprintf( file
, " sigjmp_buf jmp; \\\n");
3983 fprintf( file
, " DWORD code; \\\n");
3984 fprintf( file
, " unsigned char abnormal_termination; \\\n");
3985 fprintf( file
, " unsigned char filter_level; \\\n");
3986 fprintf( file
, " unsigned char finally_level;\n");
3987 fprintf( file
, "\n");
3988 fprintf( file
, "struct __exception_frame\n{\n");
3989 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
3990 fprintf( file
, "};\n");
3991 fprintf( file
, "\n");
3992 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3993 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3994 fprintf( file
, " CONTEXT *context,\n");
3995 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3996 fprintf( file
, "{\n");
3997 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3998 fprintf( file
, "\n");
3999 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4000 fprintf( file
, " {\n" );
4001 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4002 fprintf( file
, " {\n" );
4003 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4004 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4005 fprintf( file
, " }\n" );
4006 fprintf( file
, " return ExceptionContinueSearch;\n");
4007 fprintf( file
, " }\n" );
4008 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
4009 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4010 fprintf( file
, " {\n");
4011 fprintf( file
, " __wine_rtl_unwind( frame, record );\n");
4012 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4013 fprintf( file
, " {\n");
4014 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4015 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4016 fprintf( file
, " __wine_pop_frame( frame );\n");
4017 fprintf( file
, " }\n");
4018 fprintf( file
, " exc_frame->filter_level = 0;\n");
4019 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
4020 fprintf( file
, " }\n");
4021 fprintf( file
, " return ExceptionContinueSearch;\n");
4022 fprintf( file
, "}\n");
4023 fprintf( file
, "\n");
4024 fprintf( file
, "#define RpcTryExcept \\\n");
4025 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4026 fprintf( file
, " { \\\n");
4027 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4028 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4029 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
4030 fprintf( file
, "\n");
4031 fprintf( file
, "#define RpcExcept(expr) \\\n");
4032 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4033 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4034 fprintf( file
, " __frame->filter_level = 0; \\\n" );
4035 fprintf( file
, " } \\\n");
4036 fprintf( file
, " else \\\n");
4037 fprintf( file
, "\n");
4038 fprintf( file
, "#define RpcEndExcept\n");
4039 fprintf( file
, "\n");
4040 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
4041 fprintf( file
, "\n");
4042 fprintf( file
, "#define RpcTryFinally \\\n");
4043 fprintf( file
, " if (!__frame->filter_level) \\\n");
4044 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4045 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
4046 fprintf( file
, "\n");
4047 fprintf( file
, "#define RpcFinally \\\n");
4048 fprintf( file
, " if (!__frame->filter_level) \\\n");
4049 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4050 fprintf( file
, " __frame->finally_level = 0;\n");
4051 fprintf( file
, "\n");
4052 fprintf( file
, "#define RpcEndFinally\n");
4053 fprintf( file
, "\n");
4054 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4055 fprintf( file
, "\n");
4056 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4057 fprintf( file
, " do { \\\n");
4058 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4059 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4060 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
4061 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
4062 fprintf( file
, " __frame->filter_level = 0; \\\n");
4063 fprintf( file
, " __frame->finally_level = 0; \\\n");
4064 fprintf( file
, " } while (0)\n");
4065 fprintf( file
, "\n");
4066 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4067 fprintf( file
, "\n");
4068 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4069 fprintf( file
, " do { (void)(filter_func); } while(0)\n");
4070 fprintf( file
, "\n");
4071 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4072 fprintf( file
, " DWORD code;\n");
4073 fprintf( file
, "\n");
4074 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");