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
,
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_pointer_fc(const type_t
*type
)
125 assert(is_ptr(type
));
126 /* FIXME: see corresponding hack in set_type - we shouldn't be getting
127 * the pointer type from an alias, rather determining it from the
132 static unsigned char get_enum_fc(const type_t
*type
)
134 assert(type_get_type(type
) == TYPE_ENUM
);
135 if (is_aliaschain_attr(type
, ATTR_V1ENUM
))
136 return RPC_FC_ENUM32
;
138 return RPC_FC_ENUM16
;
141 unsigned char get_struct_fc(const type_t
*type
)
144 int has_conformance
= 0;
145 int has_variance
= 0;
149 fields
= type_struct_get_fields(type
);
151 if (get_padding(fields
))
152 return RPC_FC_BOGUS_STRUCT
;
154 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, var_t
, entry
)
156 type_t
*t
= field
->type
;
159 return RPC_FC_BOGUS_STRUCT
;
163 if (is_string_type(field
->attrs
, field
->type
))
165 if (is_conformant_array(t
))
171 if (is_array(type_array_get_element(field
->type
)))
172 return RPC_FC_BOGUS_STRUCT
;
174 if (type_array_has_conformance(field
->type
))
177 if (list_next(fields
, &field
->entry
))
178 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
181 if (type_array_has_variance(t
))
184 t
= type_array_get_element(t
);
188 return RPC_FC_BOGUS_STRUCT
;
190 switch (type_get_type(t
))
200 if (get_enum_fc(t
) == RPC_FC_ENUM16
)
201 return RPC_FC_BOGUS_STRUCT
;
204 if (get_pointer_fc(t
) == RPC_FC_RP
|| pointer_size
!= 4)
205 return RPC_FC_BOGUS_STRUCT
;
206 if (type_get_type(type_pointer_get_ref(t
)) == TYPE_INTERFACE
)
207 return RPC_FC_BOGUS_STRUCT
;
212 unsigned int ptr_type
= get_attrv(field
->attrs
, ATTR_POINTERTYPE
);
213 if (!ptr_type
|| ptr_type
== RPC_FC_RP
)
214 return RPC_FC_BOGUS_STRUCT
;
215 else if (pointer_size
!= 4)
216 return RPC_FC_BOGUS_STRUCT
;
222 case TYPE_ENCAPSULATED_UNION
:
225 return RPC_FC_BOGUS_STRUCT
;
228 unsigned char fc
= get_struct_fc(t
);
233 case RPC_FC_CVSTRUCT
:
239 case RPC_FC_CPSTRUCT
:
241 if (list_next( fields
, &field
->entry
))
242 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
249 if (list_next( fields
, &field
->entry
))
250 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
259 error_loc("Unknown struct member %s with type (0x%02x)\n", field
->name
, fc
);
260 /* fallthru - treat it as complex */
262 /* as soon as we see one of these these members, it's bogus... */
263 case RPC_FC_BOGUS_STRUCT
:
264 return RPC_FC_BOGUS_STRUCT
;
273 if ( has_conformance
)
274 return RPC_FC_CVSTRUCT
;
276 return RPC_FC_BOGUS_STRUCT
;
278 if( has_conformance
&& has_pointer
)
279 return RPC_FC_CPSTRUCT
;
280 if( has_conformance
)
281 return RPC_FC_CSTRUCT
;
283 return RPC_FC_PSTRUCT
;
284 return RPC_FC_STRUCT
;
287 unsigned char get_array_fc(const type_t
*type
)
290 const expr_t
*size_is
;
291 const type_t
*elem_type
;
293 elem_type
= type_array_get_element(type
);
294 size_is
= type_array_get_conformance(type
);
298 unsigned int align
= 0;
299 unsigned int size
= type_memsize(elem_type
, &align
);
300 if (size
* type_array_get_dim(type
) > 0xffffuL
)
301 fc
= RPC_FC_LGFARRAY
;
303 fc
= RPC_FC_SMFARRAY
;
308 if (type_array_has_variance(type
))
310 if (fc
== RPC_FC_SMFARRAY
)
311 fc
= RPC_FC_SMVARRAY
;
312 else if (fc
== RPC_FC_LGFARRAY
)
313 fc
= RPC_FC_LGVARRAY
;
314 else if (fc
== RPC_FC_CARRAY
)
318 if (is_user_type(elem_type
))
319 fc
= RPC_FC_BOGUS_ARRAY
;
320 else switch (type_get_type(elem_type
))
323 switch (get_struct_fc(elem_type
))
325 case RPC_FC_BOGUS_STRUCT
:
326 fc
= RPC_FC_BOGUS_ARRAY
;
331 /* is 16-bit enum - if so, wire size differs from mem size and so
332 * the array cannot be block copied, which means the array is complex */
333 if (get_enum_fc(elem_type
) == RPC_FC_ENUM16
)
334 fc
= RPC_FC_BOGUS_ARRAY
;
337 case TYPE_ENCAPSULATED_UNION
:
338 fc
= RPC_FC_BOGUS_ARRAY
;
341 /* ref pointers cannot just be block copied. unique pointers to
342 * interfaces need special treatment. either case means the array is
344 if (get_pointer_fc(elem_type
) == RPC_FC_RP
||
345 type_get_type(type_pointer_get_ref(elem_type
)) == TYPE_INTERFACE
)
346 fc
= RPC_FC_BOGUS_ARRAY
;
349 /* nothing to do for everything else */
356 int is_struct(unsigned char type
)
363 case RPC_FC_CPSTRUCT
:
364 case RPC_FC_CVSTRUCT
:
365 case RPC_FC_BOGUS_STRUCT
:
372 static int is_non_complex_struct(const type_t
*type
)
374 return (type_get_type(type
) == TYPE_STRUCT
&&
375 get_struct_fc(type
) != RPC_FC_BOGUS_STRUCT
);
378 static int type_has_pointers(const type_t
*type
)
380 if (is_user_type(type
))
383 switch (type_get_type(type
))
388 return type_has_pointers(type_array_get_element(type
));
391 var_list_t
*fields
= type_struct_get_fields(type
);
393 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
395 if (type_has_pointers(field
->type
))
401 case TYPE_ENCAPSULATED_UNION
:
405 fields
= type_union_get_cases(type
);
406 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
408 if (field
->type
&& type_has_pointers(field
->type
))
420 static int type_has_full_pointer(const type_t
*type
)
422 if (is_user_type(type
))
425 switch (type_get_type(type
))
428 if (get_pointer_fc(type
) == RPC_FC_FP
)
433 return type_has_full_pointer(type_array_get_element(type
));
436 var_list_t
*fields
= type_struct_get_fields(type
);
438 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
440 if (type_has_full_pointer(field
->type
))
446 case TYPE_ENCAPSULATED_UNION
:
450 fields
= type_union_get_cases(type
);
451 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
453 if (field
->type
&& type_has_full_pointer(field
->type
))
465 static unsigned short user_type_offset(const char *name
)
468 unsigned short off
= 0;
469 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
471 if (strcmp(name
, ut
->name
) == 0)
475 error("user_type_offset: couldn't find type (%s)\n", name
);
479 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
481 type
->typestring_offset
= offset
;
482 if (file
) type
->tfswrite
= FALSE
;
485 static void guard_rec(type_t
*type
)
487 /* types that contain references to themselves (like a linked list),
488 need to be shielded from infinite recursion when writing embedded
490 if (type
->typestring_offset
)
491 type
->tfswrite
= FALSE
;
493 type
->typestring_offset
= 1;
496 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
500 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
508 if (type_is_alias(t
))
509 t
= type_alias_get_aliasee(t
);
515 int is_user_type(const type_t
*t
)
517 return get_user_type(t
, NULL
) != NULL
;
520 static int is_embedded_complex(const type_t
*type
)
522 if (is_user_type(type
))
525 switch (type_get_type(type
))
529 case TYPE_ENCAPSULATED_UNION
:
533 return type_get_type(type_pointer_get_ref(type
)) == TYPE_INTERFACE
;
539 static const char *get_context_handle_type_name(const type_t
*type
)
543 is_ptr(t
) || type_is_alias(t
);
544 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
545 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
551 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
554 fprintf(file, "/* %2u */\n", typestring_offset); \
555 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
559 static void print_file(FILE *file
, int indent
, const char *format
, ...) __attribute__((format (printf
, 3, 4)));
560 static void print_file(FILE *file
, int indent
, const char *format
, ...)
563 va_start(va
, format
);
564 print(file
, indent
, format
, va
);
568 void print(FILE *file
, int indent
, const char *format
, va_list va
)
572 if (format
[0] != '\n')
575 vfprintf(file
, format
, va
);
580 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
, const char *local_var_prefix
)
582 if (decl_indirect(t
))
584 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
585 local_var_prefix
, n
, local_var_prefix
, n
);
586 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
588 else if (is_ptr(t
) || is_array(t
))
589 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
592 void write_parameters_init(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
596 if (!is_void(type_function_get_rettype(func
->type
)))
597 write_var_init(file
, indent
, type_function_get_rettype(func
->type
), "_RetVal", local_var_prefix
);
599 if (!type_get_function_args(func
->type
))
602 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
603 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
608 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
610 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
611 print_file(f
, indent
, "{\n");
612 print_file(f
, indent
+ 1, "short Pad;\n");
613 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
614 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
615 print_file(f
, indent
, "\n");
618 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
622 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
623 get_size_typeformatstring(stmts
, pred
));
625 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
626 get_size_procformatstring(stmts
, pred
));
629 write_formatdesc(f
, indent
, "TYPE");
630 write_formatdesc(f
, indent
, "PROC");
632 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
633 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
634 print_file(f
, indent
, "\n");
637 int decl_indirect(const type_t
*t
)
641 return (type_get_type(t
) != TYPE_BASIC
&&
642 type_get_type(t
) != TYPE_ENUM
&&
643 type_get_type(t
) != TYPE_POINTER
&&
644 type_get_type(t
) != TYPE_ARRAY
);
647 static unsigned int write_procformatstring_type(FILE *file
, int indent
,
650 const attr_list_t
*attrs
,
655 int is_in
= is_attr(attrs
, ATTR_IN
);
656 int is_out
= is_attr(attrs
, ATTR_OUT
);
658 if (!is_in
&& !is_out
) is_in
= TRUE
;
660 if (type_get_type(type
) == TYPE_BASIC
||
661 type_get_type(type
) == TYPE_ENUM
)
666 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
668 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
670 if (type_get_type(type
) == TYPE_ENUM
)
672 fc
= get_enum_fc(type
);
676 fc
= type_basic_get_fc(type
);
678 if (fc
== RPC_FC_BIND_PRIMITIVE
)
682 print_file(file
, indent
, "0x%02x, /* %s */\n",
683 fc
, string_of_type(fc
));
684 size
= 2; /* includes param type prefix */
689 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
690 else if (is_in
&& is_out
)
691 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
693 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
695 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
697 print_file(file
, indent
, "0x01,\n");
698 print_file(file
, indent
, "NdrFcShort(0x%hx),\n", type
->typestring_offset
);
699 size
= 4; /* includes param type prefix */
704 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
706 const statement_t
*stmt
;
707 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
709 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
711 const statement_t
*stmt_func
;
712 if (!pred(stmt
->u
.type
))
714 STATEMENTS_FOR_EACH_FUNC(stmt_func
, type_iface_get_stmts(stmt
->u
.type
))
716 const var_t
*func
= stmt_func
->u
.var
;
717 if (is_local(func
->attrs
)) continue;
718 /* emit argument data */
719 if (type_get_function_args(func
->type
))
722 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
723 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
726 /* emit return value data */
727 if (is_void(type_function_get_rettype(func
->type
)))
729 print_file(file
, indent
, "0x5b, /* FC_END */\n");
730 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
733 write_procformatstring_type(file
, indent
, "return value", type_function_get_rettype(func
->type
), NULL
, TRUE
);
736 else if (stmt
->type
== STMT_LIBRARY
)
737 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
741 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
745 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
746 print_file(file
, indent
, "{\n");
748 print_file(file
, indent
, "0,\n");
749 print_file(file
, indent
, "{\n");
752 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
754 print_file(file
, indent
, "0x0\n");
756 print_file(file
, indent
, "}\n");
758 print_file(file
, indent
, "};\n");
759 print_file(file
, indent
, "\n");
762 static int write_base_type(FILE *file
, const type_t
*type
, int convert_to_signed_type
, unsigned int *typestring_offset
)
766 if (type_get_type(type
) == TYPE_BASIC
)
767 fc
= type_basic_get_fc(type
);
768 else if (type_get_type(type
) == TYPE_ENUM
)
769 fc
= get_enum_fc(type
);
773 if (convert_to_signed_type
)
789 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
790 *typestring_offset
+= 1;
794 /* write conformance / variance descriptor */
795 static unsigned int write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
796 unsigned int baseoff
, const type_t
*type
,
799 unsigned char operator_type
= 0;
800 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
801 const char *conftype_string
= "";
802 const char *operator_string
= "no operators";
803 const expr_t
*subexpr
;
807 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
813 /* Top-level conformance calculations are done inline. */
814 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
815 RPC_FC_TOP_LEVEL_CONFORMANCE
);
816 print_file (file
, 2, "0x0,\n");
817 print_file (file
, 2, "NdrFcShort(0x0),\n");
823 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
824 error("write_conf_or_var_desc: constant value %ld is greater than "
825 "the maximum constant size of %d\n", expr
->cval
,
826 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
828 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
829 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
830 print_file(file
, 2, "0x%lx,\n", expr
->cval
>> 16);
831 print_file(file
, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr
->cval
);
836 if (is_ptr(type
) || (is_array(type
) && !type
->declarray
))
838 conftype
= RPC_FC_POINTER_CONFORMANCE
;
839 conftype_string
= "field pointer, ";
843 switch (subexpr
->type
)
846 subexpr
= subexpr
->ref
;
847 operator_type
= RPC_FC_DEREFERENCE
;
848 operator_string
= "FC_DEREFERENCE";
851 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
853 subexpr
= subexpr
->ref
;
854 operator_type
= RPC_FC_DIV_2
;
855 operator_string
= "FC_DIV_2";
859 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
861 subexpr
= subexpr
->ref
;
862 operator_type
= RPC_FC_MULT_2
;
863 operator_string
= "FC_MULT_2";
867 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
869 subexpr
= subexpr
->ref
;
870 operator_type
= RPC_FC_SUB_1
;
871 operator_string
= "FC_SUB_1";
875 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
877 subexpr
= subexpr
->ref
;
878 operator_type
= RPC_FC_ADD_1
;
879 operator_string
= "FC_ADD_1";
886 if (subexpr
->type
== EXPR_IDENTIFIER
)
888 const type_t
*correlation_variable
= NULL
;
889 unsigned char param_type
= 0;
890 unsigned int offset
= 0;
892 var_list_t
*fields
= type_struct_get_fields(structure
);
894 if (fields
) LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
896 unsigned int align
= 0;
897 /* FIXME: take alignment into account */
898 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
900 correlation_variable
= var
->type
;
903 offset
+= type_memsize(var
->type
, &align
);
905 if (!correlation_variable
)
906 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
909 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
913 if (type_get_type(correlation_variable
) == TYPE_BASIC
)
915 switch (type_basic_get_fc(correlation_variable
))
919 param_type
= RPC_FC_SMALL
;
923 param_type
= RPC_FC_USMALL
;
927 param_type
= RPC_FC_SHORT
;
930 param_type
= RPC_FC_USHORT
;
933 param_type
= RPC_FC_LONG
;
936 param_type
= RPC_FC_ULONG
;
939 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
940 type_basic_get_fc(correlation_variable
));
943 else if (type_get_type(correlation_variable
) == TYPE_ENUM
)
945 if (get_enum_fc(correlation_variable
) == RPC_FC_ENUM32
)
946 param_type
= RPC_FC_LONG
;
948 param_type
= RPC_FC_SHORT
;
952 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
957 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
958 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
959 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
960 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
965 unsigned int callback_offset
= 0;
966 struct expr_eval_routine
*eval
;
969 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
971 if (!strcmp (eval
->structure
->name
, structure
->name
)
972 && !compare_expr (eval
->expr
, expr
))
982 eval
= xmalloc (sizeof(*eval
));
983 eval
->structure
= structure
;
984 eval
->baseoff
= baseoff
;
986 list_add_tail (&expr_eval_routines
, &eval
->entry
);
989 if (callback_offset
> USHRT_MAX
)
990 error("Maximum number of callback routines reached\n");
992 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
993 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
994 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset
, callback_offset
);
999 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
)
1001 int have_align
= FALSE
;
1002 unsigned int size
= 0;
1005 if (!fields
) return 0;
1006 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1008 unsigned int falign
= 0;
1009 unsigned int fsize
= type_memsize(v
->type
, &falign
);
1015 size
= ROUND_SIZE(size
, falign
);
1019 size
= ROUND_SIZE(size
, *align
);
1023 static unsigned int union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
1025 unsigned int size
, maxs
= 0;
1026 unsigned int align
= *pmaxa
;
1029 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1031 /* we could have an empty default field with NULL type */
1034 size
= type_memsize(v
->type
, &align
);
1035 if (maxs
< size
) maxs
= size
;
1036 if (*pmaxa
< align
) *pmaxa
= align
;
1043 int get_padding(const var_list_t
*fields
)
1045 unsigned short offset
= 0;
1052 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
1054 type_t
*ft
= f
->type
;
1055 unsigned int align
= 0;
1056 unsigned int size
= type_memsize(ft
, &align
);
1059 offset
= ROUND_SIZE(offset
, align
);
1063 return ROUNDING(offset
, salign
);
1066 unsigned int type_memsize(const type_t
*t
, unsigned int *align
)
1068 unsigned int size
= 0;
1070 switch (type_get_type(t
))
1073 switch (type_basic_get_fc(t
))
1080 if (size
> *align
) *align
= size
;
1086 if (size
> *align
) *align
= size
;
1090 case RPC_FC_ERROR_STATUS_T
:
1093 if (size
> *align
) *align
= size
;
1098 if (size
> *align
) *align
= size
;
1101 error("type_memsize: Unknown type 0x%x\n", type_basic_get_fc(t
));
1106 switch (get_enum_fc(t
))
1110 if (size
> *align
) *align
= size
;
1114 if (size
> *align
) *align
= size
;
1117 error("type_memsize: Unknown enum type\n");
1122 size
= fields_memsize(type_struct_get_fields(t
), align
);
1124 case TYPE_ENCAPSULATED_UNION
:
1125 size
= fields_memsize(type_encapsulated_union_get_fields(t
), align
);
1128 size
= union_memsize(type_union_get_cases(t
), align
);
1131 assert( pointer_size
);
1132 size
= pointer_size
;
1133 if (size
> *align
) *align
= size
;
1138 if (is_conformant_array(t
))
1140 type_memsize(type_array_get_element(t
), align
);
1144 size
= type_array_get_dim(t
) *
1145 type_memsize(type_array_get_element(t
), align
);
1147 else /* declared as a pointer */
1149 assert( pointer_size
);
1150 size
= pointer_size
;
1151 if (size
> *align
) *align
= size
;
1154 case TYPE_INTERFACE
:
1160 /* these types should not be encountered here due to language
1161 * restrictions (interface, void, coclass, module), logical
1162 * restrictions (alias - due to type_get_type call above) or
1163 * checking restrictions (function). */
1170 int is_full_pointer_function(const var_t
*func
)
1173 if (type_has_full_pointer(type_function_get_rettype(func
->type
)))
1175 if (!type_get_function_args(func
->type
))
1177 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
1178 if (type_has_full_pointer( var
->type
))
1183 void write_full_pointer_init(FILE *file
, int indent
, const var_t
*func
, int is_server
)
1185 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1186 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
1187 fprintf(file
, "\n");
1190 void write_full_pointer_free(FILE *file
, int indent
, const var_t
*func
)
1192 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1193 fprintf(file
, "\n");
1196 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, unsigned int offset
)
1198 short absoff
= type_pointer_get_ref(type
)->typestring_offset
;
1199 short reloff
= absoff
- (offset
+ 2);
1200 int ptr_attr
= is_ptr(type_pointer_get_ref(type
)) ? 0x10 : 0x0;
1201 unsigned char pointer_fc
= get_pointer_fc(type
);
1203 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
1204 pointer_fc
, ptr_attr
, string_of_type(pointer_fc
));
1205 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
1206 reloff
, reloff
, absoff
);
1210 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
1213 unsigned char pointer_fc
;
1216 /* for historical reasons, write_simple_pointer also handled string types,
1217 * but no longer does. catch bad uses of the function with this check */
1218 if (is_string_type(type
->attrs
, type
))
1219 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
1221 pointer_fc
= get_pointer_fc(type
);
1223 ref
= type_pointer_get_ref(type
);
1224 if (type_get_type(ref
) == TYPE_ENUM
)
1225 fc
= get_enum_fc(ref
);
1227 fc
= type_basic_get_fc(ref
);
1229 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
1230 pointer_fc
, string_of_type(pointer_fc
));
1231 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1232 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1236 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
1238 print_file(file
, 0, "/* %u (", tfsoff
);
1239 write_type_decl(file
, t
, NULL
);
1240 print_file(file
, 0, ") */\n");
1243 static unsigned int write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
1245 unsigned int offset
= *typestring_offset
;
1246 type_t
*ref
= type_pointer_get_ref(type
);
1248 print_start_tfs_comment(file
, type
, offset
);
1249 update_tfsoff(type
, offset
, file
);
1251 if (ref
->typestring_offset
)
1252 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
1253 else if (type_get_type(ref
) == TYPE_BASIC
||
1254 type_get_type(ref
) == TYPE_ENUM
)
1255 *typestring_offset
+= write_simple_pointer(file
, type
);
1260 static int processed(const type_t
*type
)
1262 return type
->typestring_offset
&& !type
->tfswrite
;
1265 static int user_type_has_variable_size(const type_t
*t
)
1269 else if (type_get_type(t
) == TYPE_STRUCT
)
1271 switch (get_struct_fc(t
))
1273 case RPC_FC_PSTRUCT
:
1274 case RPC_FC_CSTRUCT
:
1275 case RPC_FC_CPSTRUCT
:
1276 case RPC_FC_CVSTRUCT
:
1280 /* Note: Since this only applies to user types, we can't have a conformant
1281 array here, and strings should get filed under pointer in this case. */
1285 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1287 unsigned int start
, absoff
, flags
;
1288 unsigned int align
= 0, ualign
= 0;
1289 const char *name
= NULL
;
1290 type_t
*utype
= get_user_type(type
, &name
);
1291 unsigned int usize
= user_type_has_variable_size(utype
) ? 0 : type_memsize(utype
, &ualign
);
1292 unsigned int size
= type_memsize(type
, &align
);
1293 unsigned short funoff
= user_type_offset(name
);
1298 if (type_get_type(utype
) == TYPE_BASIC
||
1299 type_get_type(utype
) == TYPE_ENUM
)
1303 if (type_get_type(utype
) == TYPE_ENUM
)
1304 fc
= get_enum_fc(utype
);
1306 fc
= type_basic_get_fc(utype
);
1309 print_start_tfs_comment(file
, utype
, absoff
);
1310 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1311 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1316 if (!processed(utype
))
1317 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1318 absoff
= utype
->typestring_offset
;
1321 if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
) == RPC_FC_RP
)
1323 else if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
) == RPC_FC_UP
)
1329 update_tfsoff(type
, start
, file
);
1330 print_start_tfs_comment(file
, type
, start
);
1331 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1332 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1333 flags
| (align
- 1), align
- 1, flags
);
1334 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1335 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
1336 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize
, usize
);
1338 reloff
= absoff
- *tfsoff
;
1339 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, absoff
);
1343 static void write_member_type(FILE *file
, const type_t
*cont
,
1344 int cont_is_complex
, const attr_list_t
*attrs
,
1345 const type_t
*type
, unsigned int *corroff
,
1346 unsigned int *tfsoff
)
1348 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1350 unsigned int absoff
;
1353 if (type_get_type(type
) == TYPE_UNION
&& is_attr(attrs
, ATTR_SWITCHIS
))
1360 absoff
= type
->typestring_offset
;
1362 reloff
= absoff
- (*tfsoff
+ 2);
1364 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1365 /* FIXME: actually compute necessary padding */
1366 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1367 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1368 reloff
, reloff
, absoff
);
1371 else if (is_ptr(type
) || is_conformant_array(type
))
1373 unsigned char fc
= cont_is_complex
? RPC_FC_POINTER
: RPC_FC_LONG
;
1374 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1377 else if (!write_base_type(file
, type
, TRUE
, tfsoff
))
1378 error("Unsupported member type %d\n", type_get_type(type
));
1381 static void write_end(FILE *file
, unsigned int *tfsoff
)
1383 if (*tfsoff
% 2 == 0)
1385 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1388 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1392 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1394 unsigned int offset
= 0;
1395 var_list_t
*fs
= type_struct_get_fields(type
);
1398 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1400 unsigned int align
= 0;
1401 type_t
*ft
= f
->type
;
1402 if (type_get_type(ft
) == TYPE_UNION
&& is_attr(f
->attrs
, ATTR_SWITCHIS
))
1404 unsigned int absoff
= ft
->typestring_offset
;
1405 short reloff
= absoff
- (*tfsoff
+ 6);
1406 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1407 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
1408 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1409 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1410 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1411 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1412 reloff
, reloff
, absoff
);
1416 /* FIXME: take alignment into account */
1417 offset
+= type_memsize(ft
, &align
);
1421 static int write_no_repeat_pointer_descriptions(
1422 FILE *file
, type_t
*type
,
1423 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1424 unsigned int *typestring_offset
)
1429 if (is_ptr(type
) || (!type
->declarray
&& is_conformant_array(type
)))
1431 unsigned int memsize
;
1433 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1434 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1436 /* pointer instance */
1437 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1438 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1439 *typestring_offset
+= 6;
1443 if (is_string_type(type
->attrs
, type
))
1444 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1446 write_pointer_tfs(file
, type
, typestring_offset
);
1450 unsigned absoff
= type
->typestring_offset
;
1451 short reloff
= absoff
- (*typestring_offset
+ 2);
1452 /* FIXME: get pointer attributes from field */
1453 print_file(file
, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP
, "FC_UP");
1454 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1455 reloff
, reloff
, absoff
);
1456 *typestring_offset
+= 4;
1460 memsize
= type_memsize(type
, &align
);
1461 *offset_in_memory
+= memsize
;
1462 /* increment these separately as in the case of conformant (varying)
1463 * structures these start at different values */
1464 *offset_in_buffer
+= memsize
;
1469 if (is_non_complex_struct(type
))
1472 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1474 if (offset_in_memory
&& offset_in_buffer
)
1476 unsigned int padding
;
1478 type_memsize(v
->type
, &align
);
1479 padding
= ROUNDING(*offset_in_memory
, align
);
1480 *offset_in_memory
+= padding
;
1481 *offset_in_buffer
+= padding
;
1483 written
+= write_no_repeat_pointer_descriptions(
1485 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1490 unsigned int memsize
;
1492 memsize
= type_memsize(type
, &align
);
1493 *offset_in_memory
+= memsize
;
1494 /* increment these separately as in the case of conformant (varying)
1495 * structures these start at different values */
1496 *offset_in_buffer
+= memsize
;
1502 static int write_pointer_description_offsets(
1503 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1504 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1505 unsigned int *typestring_offset
)
1510 if (is_ptr(type
) && type_get_type(type_pointer_get_ref(type
)) != TYPE_INTERFACE
)
1512 type_t
*ref
= type_pointer_get_ref(type
);
1514 if (offset_in_memory
&& offset_in_buffer
)
1516 unsigned int memsize
;
1518 /* pointer instance */
1519 /* FIXME: sometimes from end of structure, sometimes from beginning */
1520 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1521 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1524 memsize
= type_memsize(type
, &align
);
1525 *offset_in_memory
+= memsize
;
1526 /* increment these separately as in the case of conformant (varying)
1527 * structures these start at different values */
1528 *offset_in_buffer
+= memsize
;
1530 *typestring_offset
+= 4;
1532 if (is_string_type(attrs
, type
))
1533 write_string_tfs(file
, NULL
, type
, NULL
, typestring_offset
);
1534 else if (processed(ref
) || type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
)
1535 write_pointer_tfs(file
, type
, typestring_offset
);
1537 error("write_pointer_description_offsets: type format string unknown\n");
1544 return write_pointer_description_offsets(
1545 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1546 offset_in_buffer
, typestring_offset
);
1548 else if (is_non_complex_struct(type
))
1550 /* otherwise search for interesting fields to parse */
1552 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1554 if (offset_in_memory
&& offset_in_buffer
)
1556 unsigned int padding
;
1558 type_memsize(v
->type
, &align
);
1559 padding
= ROUNDING(*offset_in_memory
, align
);
1560 *offset_in_memory
+= padding
;
1561 *offset_in_buffer
+= padding
;
1563 written
+= write_pointer_description_offsets(
1564 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1570 if (offset_in_memory
&& offset_in_buffer
)
1572 unsigned int memsize
;
1574 memsize
= type_memsize(type
, &align
);
1575 *offset_in_memory
+= memsize
;
1576 /* increment these separately as in the case of conformant (varying)
1577 * structures these start at different values */
1578 *offset_in_buffer
+= memsize
;
1585 /* Note: if file is NULL return value is number of pointers to write, else
1586 * it is the number of type format characters written */
1587 static int write_fixed_array_pointer_descriptions(
1588 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1589 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1590 unsigned int *typestring_offset
)
1593 int pointer_count
= 0;
1595 if (type_get_type(type
) == TYPE_ARRAY
&&
1596 !type_array_has_conformance(type
) && !type_array_has_variance(type
))
1598 unsigned int temp
= 0;
1599 /* unfortunately, this needs to be done in two passes to avoid
1600 * writing out redundant FC_FIXED_REPEAT descriptions */
1601 pointer_count
= write_pointer_description_offsets(
1602 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1603 if (pointer_count
> 0)
1605 unsigned int increment_size
;
1606 unsigned int offset_of_array_pointer_mem
= 0;
1607 unsigned int offset_of_array_pointer_buf
= 0;
1610 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1612 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1613 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1614 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type
), type_array_get_dim(type
));
1615 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1616 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1617 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1618 *typestring_offset
+= 10;
1620 pointer_count
= write_pointer_description_offsets(
1621 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1622 &offset_of_array_pointer_buf
, typestring_offset
);
1625 else if (type_get_type(type
) == TYPE_STRUCT
)
1628 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1630 if (offset_in_memory
&& offset_in_buffer
)
1632 unsigned int padding
;
1634 type_memsize(v
->type
, &align
);
1635 padding
= ROUNDING(*offset_in_memory
, align
);
1636 *offset_in_memory
+= padding
;
1637 *offset_in_buffer
+= padding
;
1639 pointer_count
+= write_fixed_array_pointer_descriptions(
1640 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1646 if (offset_in_memory
&& offset_in_buffer
)
1648 unsigned int memsize
;
1650 memsize
= type_memsize(type
, &align
);
1651 *offset_in_memory
+= memsize
;
1652 /* increment these separately as in the case of conformant (varying)
1653 * structures these start at different values */
1654 *offset_in_buffer
+= memsize
;
1658 return pointer_count
;
1661 /* Note: if file is NULL return value is number of pointers to write, else
1662 * it is the number of type format characters written */
1663 static int write_conformant_array_pointer_descriptions(
1664 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1665 unsigned int offset_in_memory
, unsigned int *typestring_offset
)
1668 int pointer_count
= 0;
1670 if (is_conformant_array(type
) && !type_array_has_variance(type
))
1672 unsigned int temp
= 0;
1673 /* unfortunately, this needs to be done in two passes to avoid
1674 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1675 pointer_count
= write_pointer_description_offsets(
1676 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1677 if (pointer_count
> 0)
1679 unsigned int increment_size
;
1680 unsigned int offset_of_array_pointer_mem
= offset_in_memory
;
1681 unsigned int offset_of_array_pointer_buf
= offset_in_memory
;
1684 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1686 if (increment_size
> USHRT_MAX
)
1687 error("array size of %u bytes is too large\n", increment_size
);
1689 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1690 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1691 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1692 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1693 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1694 *typestring_offset
+= 8;
1696 pointer_count
= write_pointer_description_offsets(
1697 file
, attrs
, type_array_get_element(type
),
1698 &offset_of_array_pointer_mem
, &offset_of_array_pointer_buf
,
1703 return pointer_count
;
1706 /* Note: if file is NULL return value is number of pointers to write, else
1707 * it is the number of type format characters written */
1708 static int write_varying_array_pointer_descriptions(
1709 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1710 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1711 unsigned int *typestring_offset
)
1714 int pointer_count
= 0;
1716 if (is_array(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_VARIABLE_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
;
1728 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1730 if (increment_size
> USHRT_MAX
)
1731 error("array size of %u bytes is too large\n", increment_size
);
1733 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1734 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
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
+= 8;
1740 pointer_count
= write_pointer_description_offsets(
1741 file
, attrs
, type
, offset_in_memory
,
1742 offset_in_buffer
, 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 if (is_array(v
->type
) && type_array_has_variance(v
->type
))
1756 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
1757 /* skip over variance and offset in buffer */
1758 *offset_in_buffer
+= 8;
1762 type_memsize(v
->type
, &align
);
1763 padding
= ROUNDING(*offset_in_memory
, align
);
1764 *offset_in_memory
+= padding
;
1765 *offset_in_buffer
+= padding
;
1767 pointer_count
+= write_varying_array_pointer_descriptions(
1768 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1774 if (offset_in_memory
&& offset_in_buffer
)
1776 unsigned int memsize
;
1778 memsize
= type_memsize(type
, &align
);
1779 *offset_in_memory
+= memsize
;
1780 /* increment these separately as in the case of conformant (varying)
1781 * structures these start at different values */
1782 *offset_in_buffer
+= memsize
;
1786 return pointer_count
;
1789 static void write_pointer_description(FILE *file
, type_t
*type
,
1790 unsigned int *typestring_offset
)
1792 unsigned int offset_in_buffer
;
1793 unsigned int offset_in_memory
;
1795 /* pass 1: search for single instance of a pointer (i.e. don't descend
1797 if (!is_array(type
))
1799 offset_in_memory
= 0;
1800 offset_in_buffer
= 0;
1801 write_no_repeat_pointer_descriptions(
1803 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1806 /* pass 2: search for pointers in fixed arrays */
1807 offset_in_memory
= 0;
1808 offset_in_buffer
= 0;
1809 write_fixed_array_pointer_descriptions(
1811 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1813 /* pass 3: search for pointers in conformant only arrays (but don't descend
1814 * into conformant varying or varying arrays) */
1815 if ((!type
->declarray
|| !current_structure
) && is_conformant_array(type
))
1816 write_conformant_array_pointer_descriptions(
1817 file
, NULL
, type
, 0, typestring_offset
);
1818 else if (type_get_type(type
) == TYPE_STRUCT
&&
1819 get_struct_fc(type
) == RPC_FC_CPSTRUCT
)
1821 unsigned int align
= 0;
1822 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
1823 write_conformant_array_pointer_descriptions(
1825 type_memsize(type
, &align
),
1829 /* pass 4: search for pointers in varying arrays */
1830 offset_in_memory
= 0;
1831 offset_in_buffer
= 0;
1832 write_varying_array_pointer_descriptions(
1834 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1837 int is_declptr(const type_t
*t
)
1839 return is_ptr(t
) || (is_conformant_array(t
) && !t
->declarray
);
1842 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1844 const char *name
, unsigned int *typestring_offset
)
1846 unsigned int start_offset
;
1847 unsigned char rtype
;
1850 if (is_declptr(type
))
1852 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1853 int pointer_type
= is_ptr(type
) ? get_pointer_fc(type
) : get_attrv(attrs
, ATTR_POINTERTYPE
);
1855 pointer_type
= RPC_FC_RP
;
1856 print_start_tfs_comment(file
, type
, *typestring_offset
);
1857 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1858 pointer_type
, flag
, string_of_type(pointer_type
),
1859 flag
? " [simple_pointer]" : "");
1860 *typestring_offset
+= 2;
1863 print_file(file
, 2, "NdrFcShort(0x2),\n");
1864 *typestring_offset
+= 2;
1868 start_offset
= *typestring_offset
;
1869 update_tfsoff(type
, start_offset
, file
);
1872 elem_type
= type_array_get_element(type
);
1874 elem_type
= type_pointer_get_ref(type
);
1876 if (type_get_type(elem_type
) != TYPE_BASIC
)
1878 error("write_string_tfs: Unimplemented for non-basic type %s\n", name
);
1879 return start_offset
;
1882 rtype
= type_basic_get_fc(elem_type
);
1883 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
1885 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
1886 return start_offset
;
1889 if (type
->declarray
&& !is_conformant_array(type
))
1891 unsigned int dim
= type_array_get_dim(type
);
1893 /* FIXME: multi-dimensional array */
1895 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
1896 name
, 0xffffu
, dim
- 0xffffu
);
1898 if (rtype
== RPC_FC_WCHAR
)
1899 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
1901 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
1902 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1903 *typestring_offset
+= 2;
1905 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim
, dim
);
1906 *typestring_offset
+= 2;
1908 return start_offset
;
1910 else if (is_conformant_array(type
))
1912 unsigned int align
= 0;
1914 if (rtype
== RPC_FC_WCHAR
)
1915 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1917 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1918 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
1919 *typestring_offset
+= 2;
1921 *typestring_offset
+= write_conf_or_var_desc(
1922 file
, current_structure
,
1923 (type
->declarray
&& current_structure
1924 ? type_memsize(current_structure
, &align
)
1926 type
, type_array_get_conformance(type
));
1928 return start_offset
;
1932 if (rtype
== RPC_FC_WCHAR
)
1933 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1935 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1936 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1937 *typestring_offset
+= 2;
1939 return start_offset
;
1943 static unsigned int write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1944 const char *name
, unsigned int *typestring_offset
)
1946 const expr_t
*length_is
= type_array_get_variance(type
);
1947 const expr_t
*size_is
= type_array_get_conformance(type
);
1948 unsigned int align
= 0;
1950 unsigned int start_offset
;
1953 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1954 unsigned int baseoff
1955 = type
->declarray
&& current_structure
1956 ? type_memsize(current_structure
, &align
)
1960 pointer_type
= RPC_FC_RP
;
1962 if (write_embedded_types(file
, attrs
, type_array_get_element(type
), name
, FALSE
, typestring_offset
))
1965 has_pointer
= type_has_pointers(type_array_get_element(type
));
1968 size
= type_memsize((is_conformant_array(type
) ? type_array_get_element(type
) : type
), &align
);
1969 fc
= get_array_fc(type
);
1971 start_offset
= *typestring_offset
;
1972 update_tfsoff(type
, start_offset
, file
);
1973 print_start_tfs_comment(file
, type
, start_offset
);
1974 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1975 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1976 *typestring_offset
+= 2;
1979 if (fc
!= RPC_FC_BOGUS_ARRAY
)
1981 if (fc
== RPC_FC_LGFARRAY
|| fc
== RPC_FC_LGVARRAY
)
1983 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", size
, size
);
1984 *typestring_offset
+= 4;
1988 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
1989 *typestring_offset
+= 2;
1992 if (is_conformant_array(type
))
1994 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1997 if (fc
== RPC_FC_SMVARRAY
|| fc
== RPC_FC_LGVARRAY
)
1999 unsigned int elalign
= 0;
2000 unsigned int elsize
= type_memsize(type_array_get_element(type
), &elalign
);
2001 unsigned int dim
= type_array_get_dim(type
);
2003 if (fc
== RPC_FC_LGVARRAY
)
2005 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim
, dim
);
2006 *typestring_offset
+= 4;
2010 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2011 *typestring_offset
+= 2;
2014 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize
, elsize
);
2015 *typestring_offset
+= 2;
2020 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2023 if (has_pointer
&& (!type
->declarray
|| !current_structure
))
2025 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2026 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2027 *typestring_offset
+= 2;
2028 write_pointer_description(file
, type
, typestring_offset
);
2029 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2030 *typestring_offset
+= 1;
2033 write_member_type(file
, type
, FALSE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2034 write_end(file
, typestring_offset
);
2038 unsigned int dim
= size_is
? 0 : type_array_get_dim(type
);
2039 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2040 *typestring_offset
+= 2;
2042 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2045 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2047 write_member_type(file
, type
, TRUE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2048 write_end(file
, typestring_offset
);
2051 return start_offset
;
2054 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
2056 const var_list_t
*fields
= type_struct_get_fields(type
);
2057 const var_t
*last_field
;
2060 if (!fields
|| list_empty(fields
))
2063 last_field
= LIST_ENTRY( list_tail(fields
), const var_t
, entry
);
2064 ft
= last_field
->type
;
2066 if (ft
->declarray
&& is_conformant_array(ft
))
2069 if (type_get_type(ft
) == TYPE_STRUCT
)
2070 return find_array_or_string_in_struct(ft
);
2075 static void write_struct_members(FILE *file
, const type_t
*type
,
2076 int is_complex
, unsigned int *corroff
,
2077 unsigned int *typestring_offset
)
2080 unsigned short offset
= 0;
2083 var_list_t
*fields
= type_struct_get_fields(type
);
2085 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2087 type_t
*ft
= field
->type
;
2088 if (!ft
->declarray
|| !is_conformant_array(ft
))
2090 unsigned int align
= 0;
2091 unsigned int size
= type_memsize(ft
, &align
);
2094 if ((align
- 1) & offset
)
2096 unsigned char fc
= 0;
2100 fc
= RPC_FC_ALIGNM4
;
2103 fc
= RPC_FC_ALIGNM8
;
2106 error("write_struct_members: cannot align type %d\n", type_get_type(ft
));
2108 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2109 offset
= ROUND_SIZE(offset
, align
);
2110 *typestring_offset
+= 1;
2112 write_member_type(file
, type
, is_complex
, field
->attrs
, field
->type
, corroff
,
2118 padding
= ROUNDING(offset
, salign
);
2121 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2122 RPC_FC_STRUCTPAD1
+ padding
- 1,
2124 *typestring_offset
+= 1;
2127 write_end(file
, typestring_offset
);
2130 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
,
2131 const char *name
, unsigned int *tfsoff
)
2133 const type_t
*save_current_structure
= current_structure
;
2134 unsigned int total_size
;
2136 unsigned int start_offset
;
2137 unsigned int array_offset
;
2138 int has_pointers
= 0;
2139 unsigned int align
= 0;
2140 unsigned int corroff
;
2142 unsigned char fc
= get_struct_fc(type
);
2143 var_list_t
*fields
= type_struct_get_fields(type
);
2146 current_structure
= type
;
2148 total_size
= type_memsize(type
, &align
);
2149 if (total_size
> USHRT_MAX
)
2150 error("structure size for %s exceeds %d bytes by %d bytes\n",
2151 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
2153 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2154 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
2156 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
2158 array
= find_array_or_string_in_struct(type
);
2159 if (array
&& !processed(array
->type
))
2161 = is_string_type(array
->attrs
, array
->type
)
2162 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
2163 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
2166 write_descriptors(file
, type
, tfsoff
);
2168 start_offset
= *tfsoff
;
2169 update_tfsoff(type
, start_offset
, file
);
2170 print_start_tfs_comment(file
, type
, start_offset
);
2171 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2172 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2173 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size
, total_size
);
2178 unsigned int absoff
= array
->type
->typestring_offset
;
2179 short reloff
= absoff
- *tfsoff
;
2180 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2181 reloff
, reloff
, absoff
);
2184 else if (fc
== RPC_FC_BOGUS_STRUCT
)
2186 print_file(file
, 2, "NdrFcShort(0x0),\n");
2190 if (fc
== RPC_FC_BOGUS_STRUCT
)
2192 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2193 nothing is written to file yet. On the actual writing pass,
2194 this will have been updated. */
2195 unsigned int absoff
= type_get_real_type(type
)->ptrdesc
?
2196 type_get_real_type(type
)->ptrdesc
: *tfsoff
;
2197 int reloff
= absoff
- *tfsoff
;
2198 assert( reloff
>= 0 );
2199 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2200 reloff
, reloff
, absoff
);
2203 else if ((fc
== RPC_FC_PSTRUCT
) ||
2204 (fc
== RPC_FC_CPSTRUCT
) ||
2205 (fc
== RPC_FC_CVSTRUCT
&& has_pointers
))
2207 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2208 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2210 write_pointer_description(file
, type
, tfsoff
);
2211 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2215 write_struct_members(file
, type
, fc
== RPC_FC_BOGUS_STRUCT
, &corroff
,
2218 if (fc
== RPC_FC_BOGUS_STRUCT
)
2222 type_get_real_type(type
)->ptrdesc
= *tfsoff
;
2223 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
2225 type_t
*ft
= f
->type
;
2228 if (is_string_type(f
->attrs
, ft
))
2229 write_string_tfs(file
, f
->attrs
, ft
, f
->name
, tfsoff
);
2231 write_pointer_tfs(file
, ft
, tfsoff
);
2233 else if (!ft
->declarray
&& is_conformant_array(ft
))
2235 unsigned int absoff
= ft
->typestring_offset
;
2236 short reloff
= absoff
- (*tfsoff
+ 2);
2237 int ptr_type
= get_attrv(f
->attrs
, ATTR_POINTERTYPE
);
2238 /* FIXME: We need to store pointer attributes for arrays
2239 so we don't lose pointer_default info. */
2241 ptr_type
= RPC_FC_UP
;
2242 print_file(file
, 0, "/* %d */\n", *tfsoff
);
2243 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2244 string_of_type(ptr_type
));
2245 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2246 reloff
, reloff
, absoff
);
2250 if (type_get_real_type(type
)->ptrdesc
== *tfsoff
)
2251 type_get_real_type(type
)->ptrdesc
= 0;
2254 current_structure
= save_current_structure
;
2255 return start_offset
;
2258 static unsigned int write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
2259 unsigned char flags
, unsigned int offset
,
2260 unsigned int *typeformat_offset
)
2262 unsigned int start_offset
= *typeformat_offset
;
2263 short reloff
= offset
- (*typeformat_offset
+ 2);
2264 int in_attr
, out_attr
;
2265 in_attr
= is_attr(attrs
, ATTR_IN
);
2266 out_attr
= is_attr(attrs
, ATTR_OUT
);
2267 if (!in_attr
&& !out_attr
) in_attr
= 1;
2269 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
2272 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
2275 string_of_type(pointer_type
));
2279 fprintf(file
, " [allocated_on_stack]");
2281 fprintf(file
, " [pointer_deref]");
2282 fprintf(file
, " */\n");
2285 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", reloff
, offset
);
2286 *typeformat_offset
+= 4;
2288 return start_offset
;
2291 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
2295 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
2299 if (type_get_type(t
) == TYPE_BASIC
|| type_get_type(t
) == TYPE_ENUM
)
2302 if (type_get_type(t
) == TYPE_BASIC
)
2303 fc
= type_basic_get_fc(t
);
2305 fc
= get_enum_fc(t
);
2306 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2307 fc
, string_of_type(fc
));
2309 else if (t
->typestring_offset
)
2311 short reloff
= t
->typestring_offset
- *tfsoff
;
2312 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2313 reloff
, reloff
, t
->typestring_offset
);
2316 error("write_branch_type: type unimplemented %d\n", type_get_type(t
));
2322 static unsigned int write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2325 unsigned int start_offset
;
2328 unsigned int nbranch
= 0;
2329 type_t
*deftype
= NULL
;
2330 short nodeftype
= 0xffff;
2336 size
= type_memsize(type
, &align
);
2338 fields
= type_union_get_cases(type
);
2340 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2342 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2344 nbranch
+= list_count(cases
);
2346 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2349 start_offset
= *tfsoff
;
2350 update_tfsoff(type
, start_offset
, file
);
2351 print_start_tfs_comment(file
, type
, start_offset
);
2352 if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
2354 const var_t
*sv
= type_union_get_switch_value(type
);
2355 const type_t
*st
= sv
->type
;
2358 if (type_get_type(st
) == TYPE_BASIC
)
2360 switch (type_basic_get_fc(st
))
2371 fc
= type_basic_get_fc(st
);
2375 error("union switch type must be an integer, char, or enum\n");
2378 else if (type_get_type(st
) == TYPE_ENUM
)
2379 fc
= get_enum_fc(st
);
2381 error("union switch type must be an integer, char, or enum\n");
2383 print_file(file
, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION
);
2384 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2385 0x40 | fc
, string_of_type(fc
));
2388 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
2390 static const expr_t dummy_expr
; /* FIXME */
2391 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
2394 if (type_get_type(st
) == TYPE_BASIC
)
2396 switch (type_basic_get_fc(st
))
2407 fc
= type_basic_get_fc(st
);
2411 error("union switch type must be an integer, char, or enum\n");
2414 else if (type_get_type(st
) == TYPE_ENUM
)
2415 fc
= get_enum_fc(st
);
2417 error("union switch type must be an integer, char, or enum\n");
2419 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
2420 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2421 fc
, string_of_type(fc
));
2424 *tfsoff
+= write_conf_or_var_desc(file
, NULL
, *tfsoff
, st
, &dummy_expr
);
2425 print_file(file
, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff
+ 2);
2429 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size
, size
);
2430 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch
, nbranch
);
2433 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2435 type_t
*ft
= f
->type
;
2436 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2437 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2440 if (cases
== NULL
&& !deflt
)
2441 error("union field %s with neither case nor default attribute\n", f
->name
);
2443 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2445 /* MIDL doesn't check for duplicate cases, even though that seems
2446 like a reasonable thing to do, it just dumps them to the TFS
2447 like we're going to do here. */
2448 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c
->cval
, c
->cval
);
2450 write_branch_type(file
, ft
, tfsoff
);
2453 /* MIDL allows multiple default branches, even though that seems
2454 illogical, it just chooses the last one, which is what we will
2465 write_branch_type(file
, deftype
, tfsoff
);
2469 print_file(file
, 2, "NdrFcShort(0x%hx),\n", nodeftype
);
2473 return start_offset
;
2476 static unsigned int write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2477 unsigned int *typeformat_offset
)
2480 unsigned int start_offset
= *typeformat_offset
;
2481 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2485 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2486 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2488 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2492 const type_t
*base
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
2493 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2496 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2498 update_tfsoff(type
, start_offset
, file
);
2499 print_start_tfs_comment(file
, type
, start_offset
);
2500 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2501 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2502 print_file(file
, 2, "NdrFcLong(0x%08x),\n", uuid
->Data1
);
2503 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2504 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2505 for (i
= 0; i
< 8; ++i
)
2506 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2509 fprintf(file
, "\n");
2511 *typeformat_offset
+= 18;
2513 return start_offset
;
2516 static unsigned int write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2518 unsigned int *typeformat_offset
)
2520 unsigned int start_offset
= *typeformat_offset
;
2521 unsigned char flags
= 0;
2523 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2524 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2528 if (is_attr(var
->attrs
, ATTR_IN
))
2531 if (!is_attr(var
->attrs
, ATTR_OUT
))
2532 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2534 if (is_attr(var
->attrs
, ATTR_OUT
))
2537 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2538 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2539 /* return and can't be null values overlap */
2540 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2541 print_file(file
, 0, "can't be null, ");
2542 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2543 print_file(file
, 0, "serialize, ");
2544 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2545 print_file(file
, 0, "no serialize, ");
2546 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2547 print_file(file
, 0, "strict, ");
2548 if ((flags
& 0x21) == 0x20)
2549 print_file(file
, 0, "out, ");
2550 if ((flags
& 0x21) == 0x21)
2551 print_file(file
, 0, "return, ");
2553 print_file(file
, 0, "in, ");
2555 print_file(file
, 0, "via ptr, ");
2556 print_file(file
, 0, "*/\n");
2557 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2558 print_file(file
, 2, "0, /* FIXME: param num */\n");
2559 *typeformat_offset
+= 4;
2561 return start_offset
;
2564 static unsigned int write_typeformatstring_var(FILE *file
, int indent
, const var_t
*func
,
2565 type_t
*type
, const var_t
*var
,
2566 unsigned int *typeformat_offset
)
2568 unsigned int offset
;
2570 if (is_context_handle(type
))
2571 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2573 if (is_user_type(type
))
2575 write_user_tfs(file
, type
, typeformat_offset
);
2576 return type
->typestring_offset
;
2579 if (is_string_type(var
->attrs
, type
))
2580 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2586 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2587 ptr_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2588 /* Top level pointers to conformant arrays may be handled specially
2589 since we can bypass the pointer, but if the array is buried
2590 beneath another pointer (e.g., "[size_is(,n)] int **p" then we
2591 always need to write the pointer. */
2592 if (!ptr_type
&& var
->type
!= type
)
2593 /* FIXME: This should use pointer_default, but the information
2594 isn't kept around for arrays. */
2595 ptr_type
= RPC_FC_UP
;
2596 if (ptr_type
&& ptr_type
!= RPC_FC_RP
)
2598 unsigned int absoff
= type
->typestring_offset
;
2599 short reloff
= absoff
- (*typeformat_offset
+ 2);
2600 off
= *typeformat_offset
;
2601 print_file(file
, 0, "/* %d */\n", off
);
2602 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2603 string_of_type(ptr_type
));
2604 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2605 reloff
, reloff
, absoff
);
2606 *typeformat_offset
+= 4;
2613 switch (type_get_type(type
))
2616 if (processed(type
)) return type
->typestring_offset
;
2617 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2619 case TYPE_ENCAPSULATED_UNION
:
2620 if (processed(type
)) return type
->typestring_offset
;
2621 return write_union_tfs(file
, type
, typeformat_offset
);
2627 error("write_typeformatstring_var: Unsupported type %d for variable %s\n",
2628 type_get_type(type
), var
->name
);
2631 else if (last_ptr(type
))
2633 unsigned int start_offset
= *typeformat_offset
;
2634 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2635 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2636 const type_t
*ref
= type_pointer_get_ref(type
);
2638 if (type_get_type(ref
) == TYPE_INTERFACE
2639 || (type_get_type(ref
) == TYPE_VOID
2640 && is_attr(var
->attrs
, ATTR_IIDIS
)))
2642 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2645 /* special case for pointers to base types */
2646 if (type_get_type(ref
) == TYPE_BASIC
||
2647 type_get_type(ref
) == TYPE_ENUM
)
2651 if (type_get_type(ref
) == TYPE_ENUM
)
2652 fc
= get_enum_fc(ref
);
2654 fc
= type_basic_get_fc(ref
);
2656 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2657 get_pointer_fc(type
),
2658 (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2659 string_of_type(get_pointer_fc(type
)),
2660 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2661 print_file(file
, indent
, "0x%02x, /* %s */\n",
2662 fc
, string_of_type(fc
));
2663 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2664 *typeformat_offset
+= 4;
2665 return start_offset
;
2669 assert(is_ptr(type
));
2671 offset
= write_typeformatstring_var(file
, indent
, func
,
2672 type_pointer_get_ref(type
), var
,
2675 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2676 return write_pointer_only_tfs(file
, var
->attrs
, get_pointer_fc(type
),
2677 !last_ptr(type
) ? 0x10 : 0,
2678 offset
, typeformat_offset
);
2681 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2682 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2686 if (is_user_type(type
))
2688 write_user_tfs(file
, type
, tfsoff
);
2690 else if (is_string_type(attrs
, type
))
2692 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2694 else switch (type_get_type(type
))
2698 type_t
*ref
= type_pointer_get_ref(type
);
2700 if (type_get_type(ref
) == TYPE_INTERFACE
2701 || (type_get_type(ref
) == TYPE_VOID
2702 && is_attr(attrs
, ATTR_IIDIS
)))
2704 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2708 if (!processed(ref
) && type_get_type(ref
) != TYPE_BASIC
)
2709 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2712 write_pointer_tfs(file
, type
, tfsoff
);
2719 /* conformant arrays and strings are handled specially */
2720 if (!type
->declarray
|| !is_conformant_array(type
))
2722 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2723 if (is_conformant_array(type
))
2728 if (!processed(type
))
2729 write_struct_tfs(file
, type
, name
, tfsoff
);
2732 case TYPE_ENCAPSULATED_UNION
:
2733 if (!processed(type
))
2734 write_union_tfs(file
, type
, tfsoff
);
2745 case TYPE_INTERFACE
:
2752 static unsigned int process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2753 type_pred_t pred
, unsigned int *typeformat_offset
)
2756 const statement_t
*stmt
;
2758 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2760 const type_t
*iface
;
2761 const statement_t
*stmt_func
;
2763 if (stmt
->type
== STMT_LIBRARY
)
2765 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2768 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
2771 iface
= stmt
->u
.type
;
2775 current_iface
= iface
;
2776 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
2778 const var_t
*func
= stmt_func
->u
.var
;
2779 if (is_local(func
->attrs
)) continue;
2781 if (!is_void(type_function_get_rettype(func
->type
)))
2784 v
.type
= type_function_get_rettype(func
->type
);
2785 update_tfsoff(type_function_get_rettype(func
->type
),
2786 write_typeformatstring_var(
2788 type_function_get_rettype(func
->type
),
2789 &v
, typeformat_offset
),
2793 current_func
= func
;
2794 if (type_get_function_args(func
->type
))
2795 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2798 write_typeformatstring_var(
2799 file
, 2, func
, var
->type
, var
,
2805 return *typeformat_offset
+ 1;
2808 static unsigned int process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2810 unsigned int typeformat_offset
= 2;
2812 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2816 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2820 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2821 print_file(file
, indent
, "{\n");
2823 print_file(file
, indent
, "0,\n");
2824 print_file(file
, indent
, "{\n");
2826 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2828 set_all_tfswrite(TRUE
);
2829 process_tfs(file
, stmts
, pred
);
2831 print_file(file
, indent
, "0x0\n");
2833 print_file(file
, indent
, "}\n");
2835 print_file(file
, indent
, "};\n");
2836 print_file(file
, indent
, "\n");
2839 static unsigned int get_required_buffer_size_type(
2840 const type_t
*type
, const char *name
, unsigned int *alignment
)
2843 const type_t
*utype
;
2846 if ((utype
= get_user_type(type
, &uname
)))
2848 return get_required_buffer_size_type(utype
, uname
, alignment
);
2852 switch (type_get_type(type
))
2855 switch (type_basic_get_fc(type
))
2873 case RPC_FC_ERROR_STATUS_T
:
2883 case RPC_FC_BIND_PRIMITIVE
:
2887 error("get_required_buffer_size: unknown basic type 0x%02x\n",
2888 type_basic_get_fc(type
));
2894 switch (get_enum_fc(type
))
2906 if (get_struct_fc(type
) == RPC_FC_STRUCT
)
2908 if (!type_struct_get_fields(type
)) return 0;
2909 return fields_memsize(type_struct_get_fields(type
), alignment
);
2914 if (get_pointer_fc(type
) == RPC_FC_RP
)
2916 const type_t
*ref
= type_pointer_get_ref(type
);
2917 return (type_get_type(ref
) == TYPE_BASIC
||
2918 type_get_type(ref
) == TYPE_ENUM
||
2919 (type_get_type(ref
) == TYPE_STRUCT
&& get_struct_fc(ref
) == RPC_FC_STRUCT
)) ?
2920 get_required_buffer_size_type( ref
, name
, alignment
) : 0;
2925 return type_array_get_dim(type
) *
2926 get_required_buffer_size_type(type_array_get_element(type
), name
, alignment
);
2935 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2937 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2938 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2940 if (!in_attr
&& !out_attr
)
2945 if ((pass
== PASS_IN
&& in_attr
) || (pass
== PASS_OUT
&& out_attr
) ||
2946 pass
== PASS_RETURN
)
2948 if (is_ptrchain_attr(var
, ATTR_CONTEXTHANDLE
))
2954 if (!is_string_type(var
->attrs
, var
->type
))
2955 return get_required_buffer_size_type(var
->type
, var
->name
,
2961 static unsigned int get_function_buffer_size( const var_t
*func
, enum pass pass
)
2964 unsigned int total_size
= 0, alignment
;
2966 if (type_get_function_args(func
->type
))
2968 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2970 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2971 total_size
+= alignment
;
2975 if (pass
== PASS_OUT
&& !is_void(type_function_get_rettype(func
->type
)))
2978 v
.type
= type_function_get_rettype(func
->type
);
2979 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
2980 total_size
+= alignment
;
2985 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2986 const char *local_var_prefix
, enum remoting_phase phase
,
2987 const var_t
*var
, unsigned int type_offset
)
2989 const char *function
;
2992 case PHASE_BUFFERSIZE
:
2993 function
= "BufferSize";
2996 function
= "Marshall";
2998 case PHASE_UNMARSHAL
:
2999 function
= "Unmarshall";
3009 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
3011 print_file(file
, indent
, "&__frame->_StubMsg,\n");
3012 print_file(file
, indent
, "%s%s%s%s%s,\n",
3013 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
3014 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
3016 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
3018 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3019 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
3020 if (phase
== PHASE_UNMARSHAL
)
3021 print_file(file
, indent
, "0);\n");
3025 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
3026 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
3027 const char *varname
)
3029 type_t
*type
= var
->type
;
3031 unsigned int alignment
= 0;
3034 /* no work to do for other phases, buffer sizing is done elsewhere */
3035 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
3038 ref
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3039 if (type_get_type(ref
) == TYPE_ENUM
)
3041 if (get_enum_fc(ref
) == RPC_FC_ENUM32
)
3046 else /* RPC_FC_ENUM16 */
3054 switch (type_basic_get_fc(ref
))
3074 case RPC_FC_ERROR_STATUS_T
:
3086 case RPC_FC_BIND_PRIMITIVE
:
3087 /* no marshalling needed */
3091 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3092 var
->name
, type_basic_get_fc(ref
));
3097 if (phase
== PHASE_MARSHAL
)
3098 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
3099 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3100 alignment
- 1, alignment
- 1);
3102 if (phase
== PHASE_MARSHAL
)
3104 print_file(file
, indent
, "*(");
3105 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3107 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
3109 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
3110 fprintf(file
, "%s%s", local_var_prefix
, varname
);
3111 fprintf(file
, ";\n");
3113 else if (phase
== PHASE_UNMARSHAL
)
3115 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
3116 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3117 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
3118 print_file(file
, indent
, "{\n");
3119 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3120 print_file(file
, indent
, "}\n");
3121 print_file(file
, indent
, "%s%s%s",
3122 (pass
== PASS_IN
|| pass
== PASS_RETURN
) ? "" : "*",
3123 local_var_prefix
, varname
);
3124 if (pass
== PASS_IN
&& is_ptr(type
))
3125 fprintf(file
, " = (");
3127 fprintf(file
, " = *(");
3128 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3129 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
3132 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
3133 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3134 fprintf(file
, ");\n");
3137 /* returns whether the MaxCount, Offset or ActualCount members need to be
3138 * filled in for the specified phase */
3139 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
3141 return (phase
!= PHASE_UNMARSHAL
);
3144 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
3148 for ( ; is_array(t
); t
= type_array_get_element(t
))
3149 if (type_array_has_conformance(t
))
3152 x
= type_array_get_conformance(t
);
3154 error("%s: multidimensional conformant"
3155 " arrays not supported at the top level\n",
3162 static void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
3163 enum remoting_phase phase
, const var_t
*var
)
3165 const type_t
*type
= var
->type
;
3166 /* get fundamental type for the argument */
3169 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
3171 else if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
))
3173 else if (type_is_alias(type
))
3174 type
= type_alias_get_aliasee(type
);
3175 else if (is_array(type
) || is_string_type(var
->attrs
, type
))
3177 if (is_conformance_needed_for_phase(phase
) && is_array(type
))
3179 if (type_array_has_conformance(type
))
3181 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3182 write_expr(file
, type_array_get_conformance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3183 fprintf(file
, ";\n\n");
3185 if (type_array_has_variance(type
))
3187 print_file(file
, indent
, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3188 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3189 write_expr(file
, type_array_get_variance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3190 fprintf(file
, ";\n\n");
3195 else if (type_get_type(type
) == TYPE_UNION
)
3197 if (is_conformance_needed_for_phase(phase
))
3199 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3200 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
3201 fprintf(file
, ";\n\n");
3205 else if (type_get_type(type
) == TYPE_INTERFACE
|| is_void(type
))
3209 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3211 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3212 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
3213 fprintf( file
, ";\n\n" );
3217 else if (is_ptr(type
))
3218 type
= type_pointer_get_ref(type
);
3224 static void write_remoting_arg(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3225 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
3227 int in_attr
, out_attr
, pointer_type
;
3228 const type_t
*type
= var
->type
;
3229 unsigned int start_offset
= type
->typestring_offset
;
3231 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
3233 pointer_type
= RPC_FC_RP
;
3235 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3236 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3237 if (!in_attr
&& !out_attr
)
3240 if (phase
!= PHASE_FREE
)
3244 if (!in_attr
) return;
3247 if (!out_attr
) return;
3253 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
);
3255 if (is_context_handle(type
))
3257 if (phase
== PHASE_MARSHAL
)
3259 if (pass
== PASS_IN
)
3261 /* if the context_handle attribute appears in the chain of types
3262 * without pointers being followed, then the context handle must
3263 * be direct, otherwise it is a pointer */
3264 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
3265 print_file(file
, indent
, "NdrClientContextMarshall(\n");
3266 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3267 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr
? "*" : "", local_var_prefix
, var
->name
);
3268 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
3272 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
3273 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3274 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
3275 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
3276 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3279 else if (phase
== PHASE_UNMARSHAL
)
3281 if (pass
== PASS_OUT
)
3284 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
3285 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
3286 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3287 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
3288 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
3292 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
3293 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3294 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3298 else if (is_user_type(var
->type
))
3300 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
3302 else if (is_string_type(var
->attrs
, var
->type
))
3304 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
||
3305 pointer_type
!= RPC_FC_RP
)
3307 unsigned int ptr_start_offset
= (start_offset
- (is_conformant_array(type
) ? 4 : 2));
3308 print_phase_function(file
, indent
, "Pointer", local_var_prefix
,
3309 phase
, var
, ptr_start_offset
);
3313 if (is_array(type
) && !is_conformant_array(type
))
3314 print_phase_function(file
, indent
, "NonConformantString",
3315 local_var_prefix
, phase
, var
,
3318 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
3319 phase
, var
, start_offset
);
3322 else switch (type_get_type(type
))
3326 unsigned char tc
= get_array_fc(type
);
3327 const char *array_type
= "FixedArray";
3329 /* We already have the size_is expression since it's at the
3330 top level, but do checks for multidimensional conformant
3331 arrays. When we handle them, we'll need to extend this
3332 function to return a list, and then we'll actually use
3333 the return value. */
3334 get_size_is_expr(type
, var
->name
);
3336 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
3338 array_type
= "VaryingArray";
3340 else if (tc
== RPC_FC_CARRAY
)
3342 array_type
= "ConformantArray";
3344 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
3346 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
3348 : "ConformantVaryingArray");
3351 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
3352 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
3353 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
3355 /* these are all unmarshalled by allocating memory */
3356 if (tc
== RPC_FC_BOGUS_ARRAY
||
3357 tc
== RPC_FC_CVARRAY
||
3358 ((tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
) && in_attr
) ||
3359 (tc
== RPC_FC_CARRAY
&& !in_attr
))
3361 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3363 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3369 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3370 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3373 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3375 if (phase
== PHASE_MARSHAL
)
3376 print_file(file
, indent
, "NdrSimpleTypeMarshall(\n");
3378 print_file(file
, indent
, "NdrSimpleTypeUnmarshall(\n");
3379 print_file(file
, indent
+1, "&__frame->_StubMsg,\n");
3380 print_file(file
, indent
+1, "(unsigned char *)&%s%s,\n",
3383 print_file(file
, indent
+1, "0x%02x /* %s */);\n", get_enum_fc(type
), string_of_type(get_enum_fc(type
)));
3387 switch (get_struct_fc(type
))
3390 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3391 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3393 case RPC_FC_PSTRUCT
:
3394 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3396 case RPC_FC_CSTRUCT
:
3397 case RPC_FC_CPSTRUCT
:
3398 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
3400 case RPC_FC_CVSTRUCT
:
3401 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
3403 case RPC_FC_BOGUS_STRUCT
:
3404 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
3407 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(type
));
3411 case TYPE_ENCAPSULATED_UNION
:
3413 const char *union_type
= NULL
;
3415 if (type_get_type(type
) == TYPE_UNION
)
3416 union_type
= "NonEncapsulatedUnion";
3417 else if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
3418 union_type
= "EncapsulatedUnion";
3420 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3421 phase
, var
, start_offset
);
3426 const type_t
*ref
= type_pointer_get_ref(type
);
3427 if (get_pointer_fc(type
) == RPC_FC_RP
&& !is_user_type(ref
)) switch (type_get_type(ref
))
3430 /* base types have known sizes, so don't need a sizing pass
3431 * and don't have any memory to free and so don't need a
3433 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3434 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3437 /* base types have known sizes, so don't need a sizing pass
3438 * and don't have any memory to free and so don't need a
3440 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3441 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3445 const char *struct_type
= NULL
;
3446 switch (get_struct_fc(ref
))
3449 /* simple structs have known sizes, so don't need a sizing
3450 * pass and don't have any memory to free and so don't
3451 * need a freeing pass */
3452 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3453 struct_type
= "SimpleStruct";
3455 case RPC_FC_PSTRUCT
:
3456 struct_type
= "SimpleStruct";
3458 case RPC_FC_CSTRUCT
:
3459 case RPC_FC_CPSTRUCT
:
3460 struct_type
= "ConformantStruct";
3462 case RPC_FC_CVSTRUCT
:
3463 struct_type
= "ConformantVaryingStruct";
3465 case RPC_FC_BOGUS_STRUCT
:
3466 struct_type
= "ComplexStruct";
3469 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(ref
));
3474 if (phase
== PHASE_FREE
)
3475 struct_type
= "Pointer";
3477 start_offset
= ref
->typestring_offset
;
3478 print_phase_function(file
, indent
, struct_type
, local_var_prefix
, phase
, var
, start_offset
);
3483 case TYPE_ENCAPSULATED_UNION
:
3485 const char *union_type
= NULL
;
3486 if (phase
== PHASE_FREE
)
3487 union_type
= "Pointer";
3490 if (type_get_type(ref
) == TYPE_UNION
)
3491 union_type
= "NonEncapsulatedUnion";
3492 else if (type_get_type(ref
) == TYPE_ENCAPSULATED_UNION
)
3493 union_type
= "EncapsulatedUnion";
3495 start_offset
= ref
->typestring_offset
;
3498 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3499 phase
, var
, start_offset
);
3504 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3506 case TYPE_INTERFACE
:
3507 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3517 else if (type_get_type(ref
) == TYPE_INTERFACE
)
3518 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3520 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3528 case TYPE_INTERFACE
:
3532 fprintf(file
, "\n");
3535 void write_remoting_arguments(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3536 enum pass pass
, enum remoting_phase phase
)
3538 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3540 unsigned int size
= get_function_buffer_size( func
, pass
);
3541 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
3544 if (pass
== PASS_RETURN
)
3548 var
.type
= type_function_get_rettype(func
->type
);
3549 var
.name
= xstrdup( "_RetVal" );
3550 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, &var
);
3556 if (!type_get_function_args(func
->type
))
3558 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3559 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
3564 unsigned int get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3566 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3570 unsigned int get_size_procformatstring_func(const var_t
*func
)
3573 unsigned int size
= 0;
3575 /* argument list size */
3576 if (type_get_function_args(func
->type
))
3577 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3578 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3580 /* return value size */
3581 if (is_void(type_function_get_rettype(func
->type
)))
3582 size
+= 2; /* FC_END and FC_PAD */
3584 size
+= get_size_procformatstring_type("return value", type_function_get_rettype(func
->type
), NULL
);
3589 unsigned int get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3591 const statement_t
*stmt
;
3592 unsigned int size
= 1;
3594 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3596 const type_t
*iface
;
3597 const statement_t
*stmt_func
;
3599 if (stmt
->type
== STMT_LIBRARY
)
3601 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3604 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
3607 iface
= stmt
->u
.type
;
3611 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
3613 const var_t
*func
= stmt_func
->u
.var
;
3614 if (!is_local(func
->attrs
))
3615 size
+= get_size_procformatstring_func( func
);
3621 unsigned int get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3623 set_all_tfswrite(FALSE
);
3624 return process_tfs(NULL
, stmts
, pred
);
3627 void declare_stub_args( FILE *file
, int indent
, const var_t
*func
)
3629 int in_attr
, out_attr
;
3633 /* declare return value '_RetVal' */
3634 if (!is_void(type_function_get_rettype(func
->type
)))
3636 print_file(file
, indent
, "%s", "");
3637 write_type_decl_left(file
, type_function_get_rettype(func
->type
));
3638 fprintf(file
, " _RetVal;\n");
3641 if (!type_get_function_args(func
->type
))
3644 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3646 int is_string
= is_string_type(var
->attrs
, var
->type
);
3648 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3649 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3650 if (!out_attr
&& !in_attr
)
3653 if (is_context_handle(var
->type
))
3654 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3657 if (!in_attr
&& !is_conformant_array(var
->type
) && !is_string
)
3659 type_t
*type_to_print
;
3661 print_file(file
, indent
, "%s", "");
3662 if (var
->type
->declarray
)
3663 type_to_print
= var
->type
;
3665 type_to_print
= type_pointer_get_ref(var
->type
);
3666 sprintf(name
, "_W%u", i
++);
3667 write_type_decl(file
, type_to_print
, name
);
3668 fprintf(file
, ";\n");
3671 print_file(file
, indent
, "%s", "");
3672 write_type_decl_left(file
, var
->type
);
3674 if (var
->type
->declarray
) {
3675 fprintf(file
, "(*%s)", var
->name
);
3677 fprintf(file
, "%s", var
->name
);
3678 write_type_right(file
, var
->type
, FALSE
);
3679 fprintf(file
, ";\n");
3681 if (decl_indirect(var
->type
))
3682 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
3688 void assign_stub_out_args( FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
3690 int in_attr
, out_attr
;
3694 if (!type_get_function_args(func
->type
))
3697 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3699 int is_string
= is_string_type(var
->attrs
, var
->type
);
3700 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3701 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3702 if (!out_attr
&& !in_attr
)
3707 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
3709 if (is_context_handle(var
->type
))
3711 fprintf(file
, " = NdrContextHandleInitialize(\n");
3712 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3713 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3714 var
->type
->typestring_offset
);
3716 else if (is_array(var
->type
) &&
3717 type_array_has_conformance(var
->type
))
3719 unsigned int size
, align
= 0;
3720 type_t
*type
= var
->type
;
3722 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
3724 is_array(type
) && type_array_has_conformance(type
);
3725 type
= type_array_get_element(type
))
3727 write_expr(file
, type_array_get_conformance(type
), TRUE
,
3728 TRUE
, NULL
, NULL
, local_var_prefix
);
3729 fprintf(file
, " * ");
3731 size
= type_memsize(type
, &align
);
3732 fprintf(file
, "%u);\n", size
);
3734 else if (!is_string
)
3736 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
3737 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
3738 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
3746 fprintf(file
, "\n");
3750 int write_expr_eval_routines(FILE *file
, const char *iface
)
3752 static const char *var_name
= "pS";
3753 static const char *var_name_expr
= "pS->";
3755 struct expr_eval_routine
*eval
;
3756 unsigned short callback_offset
= 0;
3758 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3760 const char *name
= eval
->structure
->name
;
3763 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3764 iface
, name
, callback_offset
);
3765 print_file(file
, 0, "{\n");
3766 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3767 name
, var_name
, name
, eval
->baseoff
);
3768 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3769 print_file(file
, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3770 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
, "");
3771 fprintf(file
, ";\n");
3772 print_file(file
, 0, "}\n\n");
3778 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3780 struct expr_eval_routine
*eval
;
3781 struct expr_eval_routine
*cursor
;
3782 unsigned short callback_offset
= 0;
3784 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3785 fprintf(file
, "{\n");
3787 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3789 const char *name
= eval
->structure
->name
;
3790 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3792 list_remove(&eval
->entry
);
3796 fprintf(file
, "};\n\n");
3799 void write_user_quad_list(FILE *file
)
3803 if (list_empty(&user_type_list
))
3806 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3807 fprintf(file
, "{\n");
3808 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3810 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3811 print_file(file
, 1, "{\n");
3812 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3813 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3814 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3815 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3816 print_file(file
, 1, "}%s\n", sep
);
3818 fprintf(file
, "};\n\n");
3821 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3823 const struct str_list_entry_t
*endpoint
;
3826 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3827 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3828 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3830 print_file( f
, 1, "{ (const unsigned char *)\"" );
3831 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3833 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3836 if (!*p
) goto error
;
3837 if (p
[1] != '[') goto error
;
3839 fprintf( f
, "\", (const unsigned char *)\"" );
3840 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3842 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3845 if (*p
!= ']') goto error
;
3846 fprintf( f
, "\" },\n" );
3848 print_file( f
, 0, "};\n\n" );
3852 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
3855 void write_exceptions( FILE *file
)
3857 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
3858 fprintf( file
, "\n");
3859 fprintf( file
, "#include \"wine/exception.h\"\n");
3860 fprintf( file
, "#undef RpcTryExcept\n");
3861 fprintf( file
, "#undef RpcExcept\n");
3862 fprintf( file
, "#undef RpcEndExcept\n");
3863 fprintf( file
, "#undef RpcTryFinally\n");
3864 fprintf( file
, "#undef RpcFinally\n");
3865 fprintf( file
, "#undef RpcEndFinally\n");
3866 fprintf( file
, "#undef RpcExceptionCode\n");
3867 fprintf( file
, "#undef RpcAbnormalTermination\n");
3868 fprintf( file
, "\n");
3869 fprintf( file
, "struct __exception_frame;\n");
3870 fprintf( file
, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
3871 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
3872 fprintf( file
, "\n");
3873 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
3874 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
3875 fprintf( file
, " __filter_func filter; \\\n");
3876 fprintf( file
, " __finally_func finally; \\\n");
3877 fprintf( file
, " sigjmp_buf jmp; \\\n");
3878 fprintf( file
, " DWORD code; \\\n");
3879 fprintf( file
, " unsigned char abnormal_termination; \\\n");
3880 fprintf( file
, " unsigned char filter_level; \\\n");
3881 fprintf( file
, " unsigned char finally_level;\n");
3882 fprintf( file
, "\n");
3883 fprintf( file
, "struct __exception_frame\n{\n");
3884 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
3885 fprintf( file
, "};\n");
3886 fprintf( file
, "\n");
3887 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
3888 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
3889 fprintf( file
, " CONTEXT *context,\n");
3890 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
3891 fprintf( file
, "{\n");
3892 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
3893 fprintf( file
, "\n");
3894 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
3895 fprintf( file
, " {\n" );
3896 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3897 fprintf( file
, " {\n" );
3898 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
3899 fprintf( file
, " exc_frame->finally( exc_frame );\n");
3900 fprintf( file
, " }\n" );
3901 fprintf( file
, " return ExceptionContinueSearch;\n");
3902 fprintf( file
, " }\n" );
3903 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
3904 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
3905 fprintf( file
, " {\n");
3906 fprintf( file
, " __wine_rtl_unwind( frame, record );\n");
3907 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
3908 fprintf( file
, " {\n");
3909 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
3910 fprintf( file
, " exc_frame->finally( exc_frame );\n");
3911 fprintf( file
, " __wine_pop_frame( frame );\n");
3912 fprintf( file
, " }\n");
3913 fprintf( file
, " exc_frame->filter_level = 0;\n");
3914 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
3915 fprintf( file
, " }\n");
3916 fprintf( file
, " return ExceptionContinueSearch;\n");
3917 fprintf( file
, "}\n");
3918 fprintf( file
, "\n");
3919 fprintf( file
, "#define RpcTryExcept \\\n");
3920 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
3921 fprintf( file
, " { \\\n");
3922 fprintf( file
, " if (!__frame->finally_level) \\\n" );
3923 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
3924 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
3925 fprintf( file
, "\n");
3926 fprintf( file
, "#define RpcExcept(expr) \\\n");
3927 fprintf( file
, " if (!__frame->finally_level) \\\n" );
3928 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
3929 fprintf( file
, " __frame->filter_level = 0; \\\n" );
3930 fprintf( file
, " } \\\n");
3931 fprintf( file
, " else \\\n");
3932 fprintf( file
, "\n");
3933 fprintf( file
, "#define RpcEndExcept\n");
3934 fprintf( file
, "\n");
3935 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
3936 fprintf( file
, "\n");
3937 fprintf( file
, "#define RpcTryFinally \\\n");
3938 fprintf( file
, " if (!__frame->filter_level) \\\n");
3939 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
3940 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
3941 fprintf( file
, "\n");
3942 fprintf( file
, "#define RpcFinally \\\n");
3943 fprintf( file
, " if (!__frame->filter_level) \\\n");
3944 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
3945 fprintf( file
, " __frame->finally_level = 0;\n");
3946 fprintf( file
, "\n");
3947 fprintf( file
, "#define RpcEndFinally\n");
3948 fprintf( file
, "\n");
3949 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
3950 fprintf( file
, "\n");
3951 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3952 fprintf( file
, " do { \\\n");
3953 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
3954 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
3955 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
3956 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
3957 fprintf( file
, " __frame->filter_level = 0; \\\n");
3958 fprintf( file
, " __frame->finally_level = 0; \\\n");
3959 fprintf( file
, " } while (0)\n");
3960 fprintf( file
, "\n");
3961 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
3962 fprintf( file
, "\n");
3963 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
3964 fprintf( file
, " do { (void)(filter_func); } while(0)\n");
3965 fprintf( file
, "\n");
3966 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
3967 fprintf( file
, " DWORD code;\n");
3968 fprintf( file
, "\n");
3969 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");