2 * Format String Generator for IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
5 * Copyright 2005-2006 Robert Shearman
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
44 /* round size up to multiple of alignment */
45 #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
46 /* value to add on to round size up to a multiple of alignment */
47 #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
49 static const var_t
*current_func
;
50 static const type_t
*current_structure
;
51 static const type_t
*current_iface
;
53 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
54 struct expr_eval_routine
57 const type_t
*structure
;
62 static unsigned int field_memsize(const type_t
*type
, unsigned int *offset
);
63 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
);
64 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
65 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
66 const char *name
, int write_ptr
, unsigned int *tfsoff
);
67 static const var_t
*find_array_or_string_in_struct(const type_t
*type
);
68 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
69 type_t
*type
, int toplevel_param
,
70 const char *name
, unsigned int *typestring_offset
);
72 const char *string_of_type(unsigned char type
)
76 case RPC_FC_BYTE
: return "FC_BYTE";
77 case RPC_FC_CHAR
: return "FC_CHAR";
78 case RPC_FC_SMALL
: return "FC_SMALL";
79 case RPC_FC_USMALL
: return "FC_USMALL";
80 case RPC_FC_WCHAR
: return "FC_WCHAR";
81 case RPC_FC_SHORT
: return "FC_SHORT";
82 case RPC_FC_USHORT
: return "FC_USHORT";
83 case RPC_FC_LONG
: return "FC_LONG";
84 case RPC_FC_ULONG
: return "FC_ULONG";
85 case RPC_FC_FLOAT
: return "FC_FLOAT";
86 case RPC_FC_HYPER
: return "FC_HYPER";
87 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
88 case RPC_FC_ENUM16
: return "FC_ENUM16";
89 case RPC_FC_ENUM32
: return "FC_ENUM32";
90 case RPC_FC_IGNORE
: return "FC_IGNORE";
91 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
92 case RPC_FC_RP
: return "FC_RP";
93 case RPC_FC_UP
: return "FC_UP";
94 case RPC_FC_OP
: return "FC_OP";
95 case RPC_FC_FP
: return "FC_FP";
96 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
97 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
98 case RPC_FC_STRUCT
: return "FC_STRUCT";
99 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
100 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
101 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
102 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
103 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
104 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
105 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
106 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
107 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
108 case RPC_FC_CARRAY
: return "FC_CARRAY";
109 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
110 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
111 case RPC_FC_ALIGNM2
: return "FC_ALIGNM2";
112 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
113 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
114 case RPC_FC_POINTER
: return "FC_POINTER";
115 case RPC_FC_C_CSTRING
: return "FC_C_CSTRING";
116 case RPC_FC_C_WSTRING
: return "FC_C_WSTRING";
117 case RPC_FC_CSTRING
: return "FC_CSTRING";
118 case RPC_FC_WSTRING
: return "FC_WSTRING";
120 error("string_of_type: unknown type 0x%02x\n", type
);
125 unsigned char get_basic_fc(const type_t
*type
)
127 int sign
= type_basic_get_sign(type
);
128 switch (type_basic_get_type(type
))
130 case TYPE_BASIC_INT8
: return (sign
<= 0 ? RPC_FC_SMALL
: RPC_FC_USMALL
);
131 case TYPE_BASIC_INT16
: return (sign
<= 0 ? RPC_FC_SHORT
: RPC_FC_USHORT
);
132 case TYPE_BASIC_INT32
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
133 case TYPE_BASIC_INT64
: return RPC_FC_HYPER
;
134 case TYPE_BASIC_INT
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
135 case TYPE_BASIC_BYTE
: return RPC_FC_BYTE
;
136 case TYPE_BASIC_CHAR
: return RPC_FC_CHAR
;
137 case TYPE_BASIC_WCHAR
: return RPC_FC_WCHAR
;
138 case TYPE_BASIC_HYPER
: return RPC_FC_HYPER
;
139 case TYPE_BASIC_FLOAT
: return RPC_FC_FLOAT
;
140 case TYPE_BASIC_DOUBLE
: return RPC_FC_DOUBLE
;
141 case TYPE_BASIC_ERROR_STATUS_T
: return RPC_FC_ERROR_STATUS_T
;
142 case TYPE_BASIC_HANDLE
: return RPC_FC_BIND_PRIMITIVE
;
147 static inline unsigned int clamp_align(unsigned int align
)
149 unsigned int packing
= (pointer_size
== 4) ? win32_packing
: win64_packing
;
150 if(align
> packing
) align
= packing
;
154 unsigned char get_pointer_fc(const type_t
*type
, const attr_list_t
*attrs
, int toplevel_param
)
159 assert(is_ptr(type
) || is_array(type
));
161 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
165 for (t
= type
; type_is_alias(t
); t
= type_alias_get_aliasee(t
))
167 pointer_type
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
174 else if (is_ptr(type
))
175 return type_pointer_get_default_fc(type
);
177 return type_array_get_ptr_default_fc(type
);
180 static unsigned char get_enum_fc(const type_t
*type
)
182 assert(type_get_type(type
) == TYPE_ENUM
);
183 if (is_aliaschain_attr(type
, ATTR_V1ENUM
))
184 return RPC_FC_ENUM32
;
186 return RPC_FC_ENUM16
;
189 enum typegen_type
typegen_detect_type(const type_t
*type
, const attr_list_t
*attrs
, unsigned int flags
)
191 if (is_user_type(type
))
192 return TGT_USER_TYPE
;
194 if (is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
))
195 return TGT_CTXT_HANDLE
;
197 if (!(flags
& TDT_IGNORE_STRINGS
) && is_string_type(attrs
, type
))
200 switch (type_get_type(type
))
207 if (type_get_type(type_pointer_get_ref(type
)) == TYPE_INTERFACE
||
208 (type_get_type(type_pointer_get_ref(type
)) == TYPE_VOID
&& is_attr(attrs
, ATTR_IIDIS
)))
209 return TGT_IFACE_POINTER
;
210 else if (is_aliaschain_attr(type_pointer_get_ref(type
), ATTR_CONTEXTHANDLE
))
211 return TGT_CTXT_HANDLE_POINTER
;
216 case TYPE_ENCAPSULATED_UNION
:
232 static type_t
*get_user_type(const type_t
*t
, const char **pname
);
234 static int type_contains_iface(const type_t
*type
)
236 enum typegen_type typegen_type
;
240 typegen_type
= typegen_detect_type(type
, type
->attrs
, TDT_IGNORE_STRINGS
);
245 return type_contains_iface(get_user_type(type
, NULL
));
252 return type_contains_iface(type_pointer_get_ref(type
));
255 return type_contains_iface(type_array_get_element(type
));
257 case TGT_IFACE_POINTER
:
261 fields
= type_struct_get_fields(type
);
262 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
264 if(type_contains_iface(field
->type
))
270 fields
= type_union_get_cases(type
);
271 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
273 if(field
->type
&& type_contains_iface(field
->type
))
279 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
281 case TGT_CTXT_HANDLE
:
282 case TGT_CTXT_HANDLE_POINTER
:
283 /* checking after parsing should mean that we don't get here. if we do,
284 * it's a checker bug */
290 unsigned char get_struct_fc(const type_t
*type
)
293 int has_conformance
= 0;
294 int has_variance
= 0;
298 fields
= type_struct_get_fields(type
);
300 if (get_padding(fields
))
301 return RPC_FC_BOGUS_STRUCT
;
303 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, var_t
, entry
)
305 type_t
*t
= field
->type
;
306 enum typegen_type typegen_type
;
308 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
310 if (typegen_type
== TGT_ARRAY
&& !type_array_is_decl_as_ptr(t
))
312 if (is_string_type(field
->attrs
, field
->type
))
314 if (is_conformant_array(t
))
320 if (is_array(type_array_get_element(field
->type
)))
321 return RPC_FC_BOGUS_STRUCT
;
323 if (type_array_has_conformance(field
->type
))
326 if (list_next(fields
, &field
->entry
))
327 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
330 if (type_array_has_variance(t
))
333 t
= type_array_get_element(t
);
334 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
337 switch (typegen_type
)
340 case TGT_IFACE_POINTER
:
341 return RPC_FC_BOGUS_STRUCT
;
345 if (get_enum_fc(t
) == RPC_FC_ENUM16
)
346 return RPC_FC_BOGUS_STRUCT
;
349 if(type_contains_iface(type_array_get_element(t
)))
350 return RPC_FC_BOGUS_STRUCT
;
352 if (get_pointer_fc(t
, field
->attrs
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
353 return RPC_FC_BOGUS_STRUCT
;
357 return RPC_FC_BOGUS_STRUCT
;
360 unsigned char fc
= get_struct_fc(t
);
365 case RPC_FC_CVSTRUCT
:
371 case RPC_FC_CPSTRUCT
:
373 if (list_next( fields
, &field
->entry
))
374 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
381 if (list_next( fields
, &field
->entry
))
382 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
391 error_loc("Unknown struct member %s with type (0x%02x)\n", field
->name
, fc
);
392 /* fallthru - treat it as complex */
394 /* as soon as we see one of these these members, it's bogus... */
395 case RPC_FC_BOGUS_STRUCT
:
396 return RPC_FC_BOGUS_STRUCT
;
401 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
403 case TGT_CTXT_HANDLE
:
404 case TGT_CTXT_HANDLE_POINTER
:
405 /* checking after parsing should mean that we don't get here. if we do,
406 * it's a checker bug */
413 if ( has_conformance
)
414 return RPC_FC_CVSTRUCT
;
416 return RPC_FC_BOGUS_STRUCT
;
418 if( has_conformance
&& has_pointer
)
419 return RPC_FC_CPSTRUCT
;
420 if( has_conformance
)
421 return RPC_FC_CSTRUCT
;
423 return RPC_FC_PSTRUCT
;
424 return RPC_FC_STRUCT
;
427 unsigned char get_array_fc(const type_t
*type
)
430 const expr_t
*size_is
;
431 const type_t
*elem_type
;
433 elem_type
= type_array_get_element(type
);
434 size_is
= type_array_get_conformance(type
);
438 unsigned int align
= 0;
439 unsigned int size
= type_memsize(elem_type
, &align
);
440 if (size
* type_array_get_dim(type
) > 0xffffuL
)
441 fc
= RPC_FC_LGFARRAY
;
443 fc
= RPC_FC_SMFARRAY
;
448 if (type_array_has_variance(type
))
450 if (fc
== RPC_FC_SMFARRAY
)
451 fc
= RPC_FC_SMVARRAY
;
452 else if (fc
== RPC_FC_LGFARRAY
)
453 fc
= RPC_FC_LGVARRAY
;
454 else if (fc
== RPC_FC_CARRAY
)
458 switch (typegen_detect_type(elem_type
, NULL
, TDT_IGNORE_STRINGS
))
461 fc
= RPC_FC_BOGUS_ARRAY
;
464 switch (get_struct_fc(elem_type
))
466 case RPC_FC_BOGUS_STRUCT
:
467 fc
= RPC_FC_BOGUS_ARRAY
;
472 /* is 16-bit enum - if so, wire size differs from mem size and so
473 * the array cannot be block copied, which means the array is complex */
474 if (get_enum_fc(elem_type
) == RPC_FC_ENUM16
)
475 fc
= RPC_FC_BOGUS_ARRAY
;
478 case TGT_IFACE_POINTER
:
479 fc
= RPC_FC_BOGUS_ARRAY
;
482 /* ref pointers cannot just be block copied. unique pointers to
483 * interfaces need special treatment. either case means the array is
485 if (get_pointer_fc(elem_type
, NULL
, FALSE
) == RPC_FC_RP
)
486 fc
= RPC_FC_BOGUS_ARRAY
;
489 case TGT_CTXT_HANDLE
:
490 case TGT_CTXT_HANDLE_POINTER
:
494 /* nothing to do for everything else */
501 int is_struct(unsigned char type
)
508 case RPC_FC_CPSTRUCT
:
509 case RPC_FC_CVSTRUCT
:
510 case RPC_FC_BOGUS_STRUCT
:
517 static int is_non_complex_struct(const type_t
*type
)
519 return (type_get_type(type
) == TYPE_STRUCT
&&
520 get_struct_fc(type
) != RPC_FC_BOGUS_STRUCT
);
523 static int type_has_pointers(const type_t
*type
)
525 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
532 return type_array_is_decl_as_ptr(type
) || type_has_pointers(type_array_get_element(type
));
535 var_list_t
*fields
= type_struct_get_fields(type
);
537 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
539 if (type_has_pointers(field
->type
))
548 fields
= type_union_get_cases(type
);
549 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
551 if (field
->type
&& type_has_pointers(field
->type
))
556 case TGT_CTXT_HANDLE
:
557 case TGT_CTXT_HANDLE_POINTER
:
559 case TGT_IFACE_POINTER
:
569 static int type_has_full_pointer(const type_t
*type
, const attr_list_t
*attrs
,
572 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
577 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
582 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
585 return type_has_full_pointer(type_array_get_element(type
), NULL
, FALSE
);
588 var_list_t
*fields
= type_struct_get_fields(type
);
590 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
592 if (type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
601 fields
= type_union_get_cases(type
);
602 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
604 if (field
->type
&& type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
609 case TGT_CTXT_HANDLE
:
610 case TGT_CTXT_HANDLE_POINTER
:
612 case TGT_IFACE_POINTER
:
622 static unsigned short user_type_offset(const char *name
)
625 unsigned short off
= 0;
626 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
628 if (strcmp(name
, ut
->name
) == 0)
632 error("user_type_offset: couldn't find type (%s)\n", name
);
636 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
638 type
->typestring_offset
= offset
;
639 if (file
) type
->tfswrite
= FALSE
;
642 static void guard_rec(type_t
*type
)
644 /* types that contain references to themselves (like a linked list),
645 need to be shielded from infinite recursion when writing embedded
647 if (type
->typestring_offset
)
648 type
->tfswrite
= FALSE
;
650 type
->typestring_offset
= 1;
653 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
657 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
665 if (type_is_alias(t
))
666 t
= type_alias_get_aliasee(t
);
672 int is_user_type(const type_t
*t
)
674 return get_user_type(t
, NULL
) != NULL
;
677 static int is_embedded_complex(const type_t
*type
)
679 switch (typegen_detect_type(type
, NULL
, TDT_ALL_TYPES
))
685 case TGT_IFACE_POINTER
:
692 static const char *get_context_handle_type_name(const type_t
*type
)
696 is_ptr(t
) || type_is_alias(t
);
697 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
698 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
704 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
707 fprintf(file, "/* %2u */\n", typestring_offset); \
708 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
712 static void print_file(FILE *file
, int indent
, const char *format
, ...) __attribute__((format (printf
, 3, 4)));
713 static void print_file(FILE *file
, int indent
, const char *format
, ...)
716 va_start(va
, format
);
717 print(file
, indent
, format
, va
);
721 void print(FILE *file
, int indent
, const char *format
, va_list va
)
725 if (format
[0] != '\n')
728 vfprintf(file
, format
, va
);
733 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
, const char *local_var_prefix
)
735 if (decl_indirect(t
))
737 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
738 local_var_prefix
, n
, local_var_prefix
, n
);
739 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
741 else if (is_ptr(t
) || is_array(t
))
742 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
745 void write_parameters_init(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
749 if (!is_void(type_function_get_rettype(func
->type
)))
750 write_var_init(file
, indent
, type_function_get_rettype(func
->type
), "_RetVal", local_var_prefix
);
752 if (!type_get_function_args(func
->type
))
755 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
756 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
761 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
763 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
764 print_file(f
, indent
, "{\n");
765 print_file(f
, indent
+ 1, "short Pad;\n");
766 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
767 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
768 print_file(f
, indent
, "\n");
771 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
775 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
776 get_size_typeformatstring(stmts
, pred
));
778 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
779 get_size_procformatstring(stmts
, pred
));
782 write_formatdesc(f
, indent
, "TYPE");
783 write_formatdesc(f
, indent
, "PROC");
785 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
786 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
787 print_file(f
, indent
, "\n");
790 int decl_indirect(const type_t
*t
)
794 return (type_get_type(t
) != TYPE_BASIC
&&
795 type_get_type(t
) != TYPE_ENUM
&&
796 type_get_type(t
) != TYPE_POINTER
&&
797 type_get_type(t
) != TYPE_ARRAY
);
800 static unsigned int write_procformatstring_type(FILE *file
, int indent
,
803 const attr_list_t
*attrs
,
808 int is_in
= is_attr(attrs
, ATTR_IN
);
809 int is_out
= is_attr(attrs
, ATTR_OUT
);
811 if (!is_in
&& !is_out
) is_in
= TRUE
;
813 if (type_get_type(type
) == TYPE_BASIC
||
814 type_get_type(type
) == TYPE_ENUM
)
819 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
821 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
823 if (type_get_type(type
) == TYPE_ENUM
)
825 fc
= get_enum_fc(type
);
829 fc
= get_basic_fc(type
);
831 if (fc
== RPC_FC_BIND_PRIMITIVE
)
835 print_file(file
, indent
, "0x%02x, /* %s */\n",
836 fc
, string_of_type(fc
));
837 size
= 2; /* includes param type prefix */
842 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
843 else if (is_in
&& is_out
)
844 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
846 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
848 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
850 print_file(file
, indent
, "0x01,\n");
851 print_file(file
, indent
, "NdrFcShort(0x%hx),\n", type
->typestring_offset
);
852 size
= 4; /* includes param type prefix */
857 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
859 const statement_t
*stmt
;
860 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
862 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
864 const statement_t
*stmt_func
;
865 if (!pred(stmt
->u
.type
))
867 STATEMENTS_FOR_EACH_FUNC(stmt_func
, type_iface_get_stmts(stmt
->u
.type
))
869 const var_t
*func
= stmt_func
->u
.var
;
870 if (is_local(func
->attrs
)) continue;
871 /* emit argument data */
872 if (type_get_function_args(func
->type
))
875 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
876 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
879 /* emit return value data */
880 if (is_void(type_function_get_rettype(func
->type
)))
882 print_file(file
, indent
, "0x5b, /* FC_END */\n");
883 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
886 write_procformatstring_type(file
, indent
, "return value", type_function_get_rettype(func
->type
), NULL
, TRUE
);
889 else if (stmt
->type
== STMT_LIBRARY
)
890 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
894 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
898 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
899 print_file(file
, indent
, "{\n");
901 print_file(file
, indent
, "0,\n");
902 print_file(file
, indent
, "{\n");
905 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
907 print_file(file
, indent
, "0x0\n");
909 print_file(file
, indent
, "}\n");
911 print_file(file
, indent
, "};\n");
912 print_file(file
, indent
, "\n");
915 static int write_base_type(FILE *file
, const type_t
*type
, int convert_to_signed_type
, unsigned int *typestring_offset
)
919 if (type_get_type(type
) == TYPE_BASIC
)
920 fc
= get_basic_fc(type
);
921 else if (type_get_type(type
) == TYPE_ENUM
)
922 fc
= get_enum_fc(type
);
926 if (convert_to_signed_type
)
942 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
943 *typestring_offset
+= 1;
947 /* write conformance / variance descriptor */
948 static unsigned int write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
949 unsigned int baseoff
, const type_t
*type
,
952 unsigned char operator_type
= 0;
953 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
954 const char *conftype_string
= "";
955 const char *operator_string
= "no operators";
956 const expr_t
*subexpr
;
960 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
966 /* Top-level conformance calculations are done inline. */
967 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
968 RPC_FC_TOP_LEVEL_CONFORMANCE
);
969 print_file (file
, 2, "0x0,\n");
970 print_file (file
, 2, "NdrFcShort(0x0),\n");
976 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
977 error("write_conf_or_var_desc: constant value %ld is greater than "
978 "the maximum constant size of %d\n", expr
->cval
,
979 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
981 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
982 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
983 print_file(file
, 2, "0x%lx,\n", expr
->cval
>> 16);
984 print_file(file
, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr
->cval
);
989 if (is_ptr(type
) || (is_array(type
) && type_array_is_decl_as_ptr(type
)))
991 conftype
= RPC_FC_POINTER_CONFORMANCE
;
992 conftype_string
= "field pointer, ";
996 switch (subexpr
->type
)
999 subexpr
= subexpr
->ref
;
1000 operator_type
= RPC_FC_DEREFERENCE
;
1001 operator_string
= "FC_DEREFERENCE";
1004 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
1006 subexpr
= subexpr
->ref
;
1007 operator_type
= RPC_FC_DIV_2
;
1008 operator_string
= "FC_DIV_2";
1012 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
1014 subexpr
= subexpr
->ref
;
1015 operator_type
= RPC_FC_MULT_2
;
1016 operator_string
= "FC_MULT_2";
1020 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
1022 subexpr
= subexpr
->ref
;
1023 operator_type
= RPC_FC_SUB_1
;
1024 operator_string
= "FC_SUB_1";
1028 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
1030 subexpr
= subexpr
->ref
;
1031 operator_type
= RPC_FC_ADD_1
;
1032 operator_string
= "FC_ADD_1";
1039 if (subexpr
->type
== EXPR_IDENTIFIER
)
1041 const type_t
*correlation_variable
= NULL
;
1042 unsigned char param_type
= 0;
1043 unsigned int offset
= 0;
1045 var_list_t
*fields
= type_struct_get_fields(structure
);
1047 if (fields
) LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
1049 unsigned int size
= field_memsize( var
->type
, &offset
);
1050 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
1052 correlation_variable
= var
->type
;
1057 if (!correlation_variable
)
1058 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1061 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
1065 if (type_get_type(correlation_variable
) == TYPE_BASIC
)
1067 switch (get_basic_fc(correlation_variable
))
1071 param_type
= RPC_FC_SMALL
;
1075 param_type
= RPC_FC_USMALL
;
1079 param_type
= RPC_FC_SHORT
;
1082 param_type
= RPC_FC_USHORT
;
1085 param_type
= RPC_FC_LONG
;
1088 param_type
= RPC_FC_ULONG
;
1091 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1092 get_basic_fc(correlation_variable
));
1095 else if (type_get_type(correlation_variable
) == TYPE_ENUM
)
1097 if (get_enum_fc(correlation_variable
) == RPC_FC_ENUM32
)
1098 param_type
= RPC_FC_LONG
;
1100 param_type
= RPC_FC_SHORT
;
1104 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1109 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
1110 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
1111 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
1112 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1117 unsigned int callback_offset
= 0;
1118 struct expr_eval_routine
*eval
;
1121 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
1123 if (!strcmp (eval
->structure
->name
, structure
->name
)
1124 && !compare_expr (eval
->expr
, expr
))
1134 eval
= xmalloc (sizeof(*eval
));
1135 eval
->structure
= structure
;
1136 eval
->baseoff
= baseoff
;
1138 list_add_tail (&expr_eval_routines
, &eval
->entry
);
1141 if (callback_offset
> USHRT_MAX
)
1142 error("Maximum number of callback routines reached\n");
1144 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
1145 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
1146 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset
, callback_offset
);
1151 /* return size and start offset of a data field based on current offset */
1152 static unsigned int field_memsize(const type_t
*type
, unsigned int *offset
)
1154 unsigned int align
= 0;
1155 unsigned int size
= type_memsize( type
, &align
);
1157 *offset
= ROUND_SIZE( *offset
, align
);
1161 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
)
1163 unsigned int size
= 0;
1164 unsigned int max_align
;
1167 if (!fields
) return 0;
1168 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1170 unsigned int falign
= 0;
1171 unsigned int fsize
= type_memsize(v
->type
, &falign
);
1172 if (*align
< falign
) *align
= falign
;
1173 falign
= clamp_align(falign
);
1174 size
= ROUND_SIZE(size
, falign
);
1178 max_align
= clamp_align(*align
);
1179 size
= ROUND_SIZE(size
, max_align
);
1184 static unsigned int union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
1186 unsigned int size
, maxs
= 0;
1187 unsigned int align
= *pmaxa
;
1190 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1192 /* we could have an empty default field with NULL type */
1195 size
= type_memsize(v
->type
, &align
);
1196 if (maxs
< size
) maxs
= size
;
1197 if (*pmaxa
< align
) *pmaxa
= align
;
1204 int get_padding(const var_list_t
*fields
)
1206 unsigned short offset
= 0;
1207 unsigned int salign
= 1;
1213 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
1215 type_t
*ft
= f
->type
;
1216 unsigned int align
= 0;
1217 unsigned int size
= type_memsize(ft
, &align
);
1218 align
= clamp_align(align
);
1219 if (align
> salign
) salign
= align
;
1220 offset
= ROUND_SIZE(offset
, align
);
1224 return ROUNDING(offset
, salign
);
1227 unsigned int type_memsize(const type_t
*t
, unsigned int *align
)
1229 unsigned int size
= 0;
1231 switch (type_get_type(t
))
1234 switch (get_basic_fc(t
))
1241 if (size
> *align
) *align
= size
;
1247 if (size
> *align
) *align
= size
;
1251 case RPC_FC_ERROR_STATUS_T
:
1254 if (size
> *align
) *align
= size
;
1259 if (size
> *align
) *align
= size
;
1262 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t
));
1267 switch (get_enum_fc(t
))
1272 if (size
> *align
) *align
= size
;
1275 error("type_memsize: Unknown enum type\n");
1280 size
= fields_memsize(type_struct_get_fields(t
), align
);
1282 case TYPE_ENCAPSULATED_UNION
:
1283 size
= fields_memsize(type_encapsulated_union_get_fields(t
), align
);
1286 size
= union_memsize(type_union_get_cases(t
), align
);
1289 assert( pointer_size
);
1290 size
= pointer_size
;
1291 if (size
> *align
) *align
= size
;
1294 if (!type_array_is_decl_as_ptr(t
))
1296 if (is_conformant_array(t
))
1298 type_memsize(type_array_get_element(t
), align
);
1302 size
= type_array_get_dim(t
) *
1303 type_memsize(type_array_get_element(t
), align
);
1305 else /* declared as a pointer */
1307 assert( pointer_size
);
1308 size
= pointer_size
;
1309 if (size
> *align
) *align
= size
;
1312 case TYPE_INTERFACE
:
1318 /* these types should not be encountered here due to language
1319 * restrictions (interface, void, coclass, module), logical
1320 * restrictions (alias - due to type_get_type call above) or
1321 * checking restrictions (function). */
1328 int is_full_pointer_function(const var_t
*func
)
1331 if (type_has_full_pointer(type_function_get_rettype(func
->type
), func
->attrs
, TRUE
))
1333 if (!type_get_function_args(func
->type
))
1335 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
1336 if (type_has_full_pointer( var
->type
, var
->attrs
, TRUE
))
1341 void write_full_pointer_init(FILE *file
, int indent
, const var_t
*func
, int is_server
)
1343 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1344 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
1345 fprintf(file
, "\n");
1348 void write_full_pointer_free(FILE *file
, int indent
, const var_t
*func
)
1350 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1351 fprintf(file
, "\n");
1354 static unsigned int write_nonsimple_pointer(FILE *file
, const attr_list_t
*attrs
,
1357 unsigned int offset
,
1358 unsigned int *typeformat_offset
)
1360 unsigned int start_offset
= *typeformat_offset
;
1361 short reloff
= offset
- (*typeformat_offset
+ 2);
1362 int in_attr
, out_attr
;
1364 unsigned char flags
= 0;
1366 pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
1368 in_attr
= is_attr(attrs
, ATTR_IN
);
1369 out_attr
= is_attr(attrs
, ATTR_OUT
);
1370 if (!in_attr
&& !out_attr
) in_attr
= 1;
1372 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1373 flags
|= RPC_FC_P_ONSTACK
;
1375 if (is_ptr(type
) && !last_ptr(type
))
1376 flags
|= RPC_FC_P_DEREF
;
1378 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1381 string_of_type(pointer_type
));
1384 if (flags
& RPC_FC_P_ONSTACK
)
1385 fprintf(file
, " [allocated_on_stack]");
1386 if (flags
& RPC_FC_P_DEREF
)
1387 fprintf(file
, " [pointer_deref]");
1388 fprintf(file
, " */\n");
1391 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, offset
);
1392 *typeformat_offset
+= 4;
1394 return start_offset
;
1397 static unsigned int write_simple_pointer(FILE *file
, const attr_list_t
*attrs
, const type_t
*type
, int toplevel_param
)
1400 unsigned char pointer_fc
;
1403 /* for historical reasons, write_simple_pointer also handled string types,
1404 * but no longer does. catch bad uses of the function with this check */
1405 if (is_string_type(attrs
, type
))
1406 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
1408 pointer_fc
= get_pointer_fc(type
, attrs
, toplevel_param
);
1410 ref
= type_pointer_get_ref(type
);
1411 if (type_get_type(ref
) == TYPE_ENUM
)
1412 fc
= get_enum_fc(ref
);
1414 fc
= get_basic_fc(ref
);
1416 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1417 pointer_fc
, RPC_FC_P_SIMPLEPOINTER
, string_of_type(pointer_fc
));
1418 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1419 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1423 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
1425 print_file(file
, 0, "/* %u (", tfsoff
);
1426 write_type_decl(file
, t
, NULL
);
1427 print_file(file
, 0, ") */\n");
1430 static unsigned int write_pointer_tfs(FILE *file
, const attr_list_t
*attrs
,
1431 type_t
*type
, int toplevel_param
,
1432 unsigned int *typestring_offset
)
1434 unsigned int offset
= *typestring_offset
;
1435 type_t
*ref
= type_pointer_get_ref(type
);
1437 print_start_tfs_comment(file
, type
, offset
);
1438 update_tfsoff(type
, offset
, file
);
1440 if (ref
->typestring_offset
)
1441 write_nonsimple_pointer(file
, attrs
, type
,
1443 type_pointer_get_ref(type
)->typestring_offset
,
1445 else if (type_get_type(ref
) == TYPE_BASIC
||
1446 type_get_type(ref
) == TYPE_ENUM
)
1447 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
,
1453 static int processed(const type_t
*type
)
1455 return type
->typestring_offset
&& !type
->tfswrite
;
1458 static int user_type_has_variable_size(const type_t
*t
)
1462 else if (type_get_type(t
) == TYPE_STRUCT
)
1464 switch (get_struct_fc(t
))
1466 case RPC_FC_PSTRUCT
:
1467 case RPC_FC_CSTRUCT
:
1468 case RPC_FC_CPSTRUCT
:
1469 case RPC_FC_CVSTRUCT
:
1473 /* Note: Since this only applies to user types, we can't have a conformant
1474 array here, and strings should get filed under pointer in this case. */
1478 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1480 unsigned int start
, absoff
, flags
;
1481 unsigned int align
= 0, ualign
= 0;
1482 const char *name
= NULL
;
1483 type_t
*utype
= get_user_type(type
, &name
);
1484 unsigned int usize
= type_memsize(utype
, &ualign
);
1485 unsigned int size
= type_memsize(type
, &align
);
1486 unsigned short funoff
= user_type_offset(name
);
1491 if(user_type_has_variable_size(utype
)) usize
= 0;
1493 if (type_get_type(utype
) == TYPE_BASIC
||
1494 type_get_type(utype
) == TYPE_ENUM
)
1498 if (type_get_type(utype
) == TYPE_ENUM
)
1499 fc
= get_enum_fc(utype
);
1501 fc
= get_basic_fc(utype
);
1504 print_start_tfs_comment(file
, utype
, absoff
);
1505 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1506 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1511 if (!processed(utype
))
1512 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1513 absoff
= utype
->typestring_offset
;
1516 if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_RP
)
1518 else if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_UP
)
1524 update_tfsoff(type
, start
, file
);
1525 print_start_tfs_comment(file
, type
, start
);
1526 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1527 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1528 flags
| (ualign
- 1), ualign
- 1, flags
);
1529 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1530 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
1531 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize
, usize
);
1533 reloff
= absoff
- *tfsoff
;
1534 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, absoff
);
1538 static void write_member_type(FILE *file
, const type_t
*cont
,
1539 int cont_is_complex
, const attr_list_t
*attrs
,
1540 const type_t
*type
, unsigned int *corroff
,
1541 unsigned int *tfsoff
)
1543 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1545 unsigned int absoff
;
1548 if (type_get_type(type
) == TYPE_UNION
&& is_attr(attrs
, ATTR_SWITCHIS
))
1555 absoff
= type
->typestring_offset
;
1557 reloff
= absoff
- (*tfsoff
+ 2);
1559 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1560 /* FIXME: actually compute necessary padding */
1561 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1562 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1563 reloff
, reloff
, absoff
);
1566 else if (is_ptr(type
) || is_conformant_array(type
))
1568 unsigned char fc
= cont_is_complex
? RPC_FC_POINTER
: RPC_FC_LONG
;
1569 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1572 else if (!write_base_type(file
, type
, TRUE
, tfsoff
))
1573 error("Unsupported member type %d\n", type_get_type(type
));
1576 static void write_end(FILE *file
, unsigned int *tfsoff
)
1578 if (*tfsoff
% 2 == 0)
1580 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1583 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1587 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1589 unsigned int offset
= 0;
1590 var_list_t
*fs
= type_struct_get_fields(type
);
1593 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1595 type_t
*ft
= f
->type
;
1596 unsigned int size
= field_memsize( ft
, &offset
);
1597 if (type_get_type(ft
) == TYPE_UNION
&& is_attr(f
->attrs
, ATTR_SWITCHIS
))
1600 unsigned int absoff
= ft
->typestring_offset
;
1601 if (is_attr(ft
->attrs
, ATTR_SWITCHTYPE
))
1602 absoff
+= 8; /* we already have a corr descr, skip it */
1603 reloff
= absoff
- (*tfsoff
+ 6);
1604 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1605 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
1606 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1607 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1608 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1609 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1610 reloff
, reloff
, absoff
);
1617 static int write_pointer_description_offsets(
1618 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1619 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1620 unsigned int *typestring_offset
)
1625 if ((is_ptr(type
) && type_get_type(type_pointer_get_ref(type
)) != TYPE_INTERFACE
) ||
1626 (is_array(type
) && type_array_is_decl_as_ptr(type
)))
1628 if (offset_in_memory
&& offset_in_buffer
)
1630 unsigned int memsize
;
1632 /* pointer instance */
1633 /* FIXME: sometimes from end of structure, sometimes from beginning */
1634 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1635 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1638 memsize
= type_memsize(type
, &align
);
1639 *offset_in_memory
+= memsize
;
1640 /* increment these separately as in the case of conformant (varying)
1641 * structures these start at different values */
1642 *offset_in_buffer
+= memsize
;
1644 *typestring_offset
+= 4;
1648 type_t
*ref
= type_pointer_get_ref(type
);
1650 if (is_string_type(attrs
, type
))
1651 write_string_tfs(file
, attrs
, type
, FALSE
, NULL
, typestring_offset
);
1652 else if (processed(ref
))
1653 write_nonsimple_pointer(file
, attrs
, type
, FALSE
, ref
->typestring_offset
, typestring_offset
);
1654 else if (type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
)
1655 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
, FALSE
);
1657 error("write_pointer_description_offsets: type format string unknown\n");
1661 unsigned int offset
= type
->typestring_offset
;
1662 /* skip over the pointer that is written for strings, since a
1663 * pointer has to be written in-place here */
1664 if (is_string_type(attrs
, type
))
1666 write_nonsimple_pointer(file
, attrs
, type
, FALSE
, offset
, typestring_offset
);
1674 return write_pointer_description_offsets(
1675 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1676 offset_in_buffer
, typestring_offset
);
1678 else if (is_non_complex_struct(type
))
1680 /* otherwise search for interesting fields to parse */
1682 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1684 if (offset_in_memory
&& offset_in_buffer
)
1686 unsigned int padding
;
1688 type_memsize(v
->type
, &align
);
1689 padding
= ROUNDING(*offset_in_memory
, align
);
1690 *offset_in_memory
+= padding
;
1691 *offset_in_buffer
+= padding
;
1693 written
+= write_pointer_description_offsets(
1694 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1700 if (offset_in_memory
&& offset_in_buffer
)
1702 unsigned int memsize
;
1704 memsize
= type_memsize(type
, &align
);
1705 *offset_in_memory
+= memsize
;
1706 /* increment these separately as in the case of conformant (varying)
1707 * structures these start at different values */
1708 *offset_in_buffer
+= memsize
;
1715 static int write_no_repeat_pointer_descriptions(
1716 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1717 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1718 unsigned int *typestring_offset
)
1724 (is_conformant_array(type
) && type_array_is_decl_as_ptr(type
)))
1726 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1727 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1728 *typestring_offset
+= 2;
1730 return write_pointer_description_offsets(file
, attrs
, type
,
1731 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1734 if (is_non_complex_struct(type
))
1737 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1739 if (offset_in_memory
&& offset_in_buffer
)
1741 unsigned int padding
;
1743 type_memsize(v
->type
, &align
);
1744 padding
= ROUNDING(*offset_in_memory
, align
);
1745 *offset_in_memory
+= padding
;
1746 *offset_in_buffer
+= padding
;
1748 written
+= write_no_repeat_pointer_descriptions(
1749 file
, v
->attrs
, v
->type
,
1750 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1755 unsigned int memsize
;
1757 memsize
= type_memsize(type
, &align
);
1758 *offset_in_memory
+= memsize
;
1759 /* increment these separately as in the case of conformant (varying)
1760 * structures these start at different values */
1761 *offset_in_buffer
+= memsize
;
1767 /* Note: if file is NULL return value is number of pointers to write, else
1768 * it is the number of type format characters written */
1769 static int write_fixed_array_pointer_descriptions(
1770 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1771 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1772 unsigned int *typestring_offset
)
1775 int pointer_count
= 0;
1777 if (type_get_type(type
) == TYPE_ARRAY
&&
1778 !type_array_has_conformance(type
) && !type_array_has_variance(type
))
1780 unsigned int temp
= 0;
1781 /* unfortunately, this needs to be done in two passes to avoid
1782 * writing out redundant FC_FIXED_REPEAT descriptions */
1783 pointer_count
= write_pointer_description_offsets(
1784 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1785 if (pointer_count
> 0)
1787 unsigned int increment_size
;
1788 unsigned int offset_of_array_pointer_mem
= 0;
1789 unsigned int offset_of_array_pointer_buf
= 0;
1792 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1794 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1795 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1796 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type
), type_array_get_dim(type
));
1797 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1798 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1799 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1800 *typestring_offset
+= 10;
1802 pointer_count
= write_pointer_description_offsets(
1803 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1804 &offset_of_array_pointer_buf
, typestring_offset
);
1807 else if (type_get_type(type
) == TYPE_STRUCT
)
1810 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1812 if (offset_in_memory
&& offset_in_buffer
)
1814 unsigned int padding
;
1816 type_memsize(v
->type
, &align
);
1817 padding
= ROUNDING(*offset_in_memory
, align
);
1818 *offset_in_memory
+= padding
;
1819 *offset_in_buffer
+= padding
;
1821 pointer_count
+= write_fixed_array_pointer_descriptions(
1822 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1828 if (offset_in_memory
&& offset_in_buffer
)
1830 unsigned int memsize
;
1832 memsize
= type_memsize(type
, &align
);
1833 *offset_in_memory
+= memsize
;
1834 /* increment these separately as in the case of conformant (varying)
1835 * structures these start at different values */
1836 *offset_in_buffer
+= memsize
;
1840 return pointer_count
;
1843 /* Note: if file is NULL return value is number of pointers to write, else
1844 * it is the number of type format characters written */
1845 static int write_conformant_array_pointer_descriptions(
1846 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1847 unsigned int offset_in_memory
, unsigned int *typestring_offset
)
1850 int pointer_count
= 0;
1852 if (is_conformant_array(type
) && !type_array_has_variance(type
))
1854 unsigned int temp
= 0;
1855 /* unfortunately, this needs to be done in two passes to avoid
1856 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1857 pointer_count
= write_pointer_description_offsets(
1858 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1859 if (pointer_count
> 0)
1861 unsigned int increment_size
;
1862 unsigned int offset_of_array_pointer_mem
= offset_in_memory
;
1863 unsigned int offset_of_array_pointer_buf
= offset_in_memory
;
1866 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1868 if (increment_size
> USHRT_MAX
)
1869 error("array size of %u bytes is too large\n", increment_size
);
1871 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1872 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1873 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1874 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1875 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1876 *typestring_offset
+= 8;
1878 pointer_count
= write_pointer_description_offsets(
1879 file
, attrs
, type_array_get_element(type
),
1880 &offset_of_array_pointer_mem
, &offset_of_array_pointer_buf
,
1885 return pointer_count
;
1888 /* Note: if file is NULL return value is number of pointers to write, else
1889 * it is the number of type format characters written */
1890 static int write_varying_array_pointer_descriptions(
1891 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1892 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1893 unsigned int *typestring_offset
)
1896 int pointer_count
= 0;
1898 if (is_array(type
) && type_array_has_variance(type
))
1900 unsigned int temp
= 0;
1901 /* unfortunately, this needs to be done in two passes to avoid
1902 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1903 pointer_count
= write_pointer_description_offsets(
1904 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1905 if (pointer_count
> 0)
1907 unsigned int increment_size
;
1910 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1912 if (increment_size
> USHRT_MAX
)
1913 error("array size of %u bytes is too large\n", increment_size
);
1915 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1916 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1917 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1918 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1919 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1920 *typestring_offset
+= 8;
1922 pointer_count
= write_pointer_description_offsets(
1923 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1924 offset_in_buffer
, typestring_offset
);
1927 else if (type_get_type(type
) == TYPE_STRUCT
)
1930 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1932 if (offset_in_memory
&& offset_in_buffer
)
1934 unsigned int padding
;
1936 if (is_array(v
->type
) && type_array_has_variance(v
->type
))
1938 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
1939 /* skip over variance and offset in buffer */
1940 *offset_in_buffer
+= 8;
1944 type_memsize(v
->type
, &align
);
1945 padding
= ROUNDING(*offset_in_memory
, align
);
1946 *offset_in_memory
+= padding
;
1947 *offset_in_buffer
+= padding
;
1949 pointer_count
+= write_varying_array_pointer_descriptions(
1950 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1956 if (offset_in_memory
&& offset_in_buffer
)
1958 unsigned int memsize
;
1960 memsize
= type_memsize(type
, &align
);
1961 *offset_in_memory
+= memsize
;
1962 /* increment these separately as in the case of conformant (varying)
1963 * structures these start at different values */
1964 *offset_in_buffer
+= memsize
;
1968 return pointer_count
;
1971 static void write_pointer_description(FILE *file
, type_t
*type
,
1972 unsigned int *typestring_offset
)
1974 unsigned int offset_in_buffer
;
1975 unsigned int offset_in_memory
;
1977 /* pass 1: search for single instance of a pointer (i.e. don't descend
1979 if (!is_array(type
))
1981 offset_in_memory
= 0;
1982 offset_in_buffer
= 0;
1983 write_no_repeat_pointer_descriptions(
1985 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1988 /* pass 2: search for pointers in fixed arrays */
1989 offset_in_memory
= 0;
1990 offset_in_buffer
= 0;
1991 write_fixed_array_pointer_descriptions(
1993 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1995 /* pass 3: search for pointers in conformant only arrays (but don't descend
1996 * into conformant varying or varying arrays) */
1997 if (is_conformant_array(type
) &&
1998 (type_array_is_decl_as_ptr(type
) || !current_structure
))
1999 write_conformant_array_pointer_descriptions(
2000 file
, NULL
, type
, 0, typestring_offset
);
2001 else if (type_get_type(type
) == TYPE_STRUCT
&&
2002 get_struct_fc(type
) == RPC_FC_CPSTRUCT
)
2004 unsigned int align
= 0;
2005 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
2006 write_conformant_array_pointer_descriptions(
2008 type_memsize(type
, &align
),
2012 /* pass 4: search for pointers in varying arrays */
2013 offset_in_memory
= 0;
2014 offset_in_buffer
= 0;
2015 write_varying_array_pointer_descriptions(
2017 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
2020 int is_declptr(const type_t
*t
)
2022 return is_ptr(t
) || (type_get_type(t
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(t
));
2025 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
2026 type_t
*type
, int toplevel_param
,
2027 const char *name
, unsigned int *typestring_offset
)
2029 unsigned int start_offset
;
2030 unsigned char rtype
;
2033 start_offset
= *typestring_offset
;
2034 update_tfsoff(type
, start_offset
, file
);
2036 if (is_declptr(type
))
2038 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
2039 int pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
2041 pointer_type
= RPC_FC_RP
;
2042 print_start_tfs_comment(file
, type
, *typestring_offset
);
2043 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2044 pointer_type
, flag
, string_of_type(pointer_type
),
2045 flag
? " [simple_pointer]" : "");
2046 *typestring_offset
+= 2;
2049 print_file(file
, 2, "NdrFcShort(0x2),\n");
2050 *typestring_offset
+= 2;
2055 elem_type
= type_array_get_element(type
);
2057 elem_type
= type_pointer_get_ref(type
);
2059 if (type_get_type(elem_type
) != TYPE_BASIC
)
2061 error("write_string_tfs: Unimplemented for non-basic type %s\n", name
);
2062 return start_offset
;
2065 rtype
= get_basic_fc(elem_type
);
2066 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
2068 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
2069 return start_offset
;
2072 if (type_get_type(type
) == TYPE_ARRAY
&& !type_array_has_conformance(type
))
2074 unsigned int dim
= type_array_get_dim(type
);
2076 /* FIXME: multi-dimensional array */
2078 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2079 name
, 0xffffu
, dim
- 0xffffu
);
2081 if (rtype
== RPC_FC_WCHAR
)
2082 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
2084 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
2085 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2086 *typestring_offset
+= 2;
2088 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim
, dim
);
2089 *typestring_offset
+= 2;
2091 return start_offset
;
2093 else if (is_conformant_array(type
))
2095 unsigned int align
= 0;
2097 if (rtype
== RPC_FC_WCHAR
)
2098 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2100 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2101 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
2102 *typestring_offset
+= 2;
2104 *typestring_offset
+= write_conf_or_var_desc(
2105 file
, current_structure
,
2106 (!type_array_is_decl_as_ptr(type
) && current_structure
2107 ? type_memsize(current_structure
, &align
)
2109 type
, type_array_get_conformance(type
));
2111 return start_offset
;
2115 if (rtype
== RPC_FC_WCHAR
)
2116 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2118 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2119 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2120 *typestring_offset
+= 2;
2122 return start_offset
;
2126 static unsigned int write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2127 const char *name
, unsigned int *typestring_offset
)
2129 const expr_t
*length_is
= type_array_get_variance(type
);
2130 const expr_t
*size_is
= type_array_get_conformance(type
);
2131 unsigned int align
= 0;
2133 unsigned int start_offset
;
2136 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
2137 unsigned int baseoff
2138 = !type_array_is_decl_as_ptr(type
) && current_structure
2139 ? type_memsize(current_structure
, &align
)
2143 pointer_type
= RPC_FC_RP
;
2145 if (write_embedded_types(file
, attrs
, type_array_get_element(type
), name
, FALSE
, typestring_offset
))
2148 has_pointer
= type_has_pointers(type_array_get_element(type
));
2151 size
= type_memsize((is_conformant_array(type
) ? type_array_get_element(type
) : type
), &align
);
2152 fc
= get_array_fc(type
);
2154 start_offset
= *typestring_offset
;
2155 update_tfsoff(type
, start_offset
, file
);
2156 print_start_tfs_comment(file
, type
, start_offset
);
2157 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
2158 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2159 *typestring_offset
+= 2;
2162 if (fc
!= RPC_FC_BOGUS_ARRAY
)
2164 if (fc
== RPC_FC_LGFARRAY
|| fc
== RPC_FC_LGVARRAY
)
2166 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", size
, size
);
2167 *typestring_offset
+= 4;
2171 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
2172 *typestring_offset
+= 2;
2175 if (is_conformant_array(type
))
2177 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2180 if (fc
== RPC_FC_SMVARRAY
|| fc
== RPC_FC_LGVARRAY
)
2182 unsigned int elalign
= 0;
2183 unsigned int elsize
= type_memsize(type_array_get_element(type
), &elalign
);
2184 unsigned int dim
= type_array_get_dim(type
);
2186 if (fc
== RPC_FC_LGVARRAY
)
2188 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim
, dim
);
2189 *typestring_offset
+= 4;
2193 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2194 *typestring_offset
+= 2;
2197 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize
, elsize
);
2198 *typestring_offset
+= 2;
2203 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2206 if (has_pointer
&& (type_array_is_decl_as_ptr(type
) || !current_structure
))
2208 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2209 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2210 *typestring_offset
+= 2;
2211 write_pointer_description(file
, type
, typestring_offset
);
2212 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2213 *typestring_offset
+= 1;
2216 write_member_type(file
, type
, FALSE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2217 write_end(file
, typestring_offset
);
2221 unsigned int dim
= size_is
? 0 : type_array_get_dim(type
);
2222 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2223 *typestring_offset
+= 2;
2225 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2228 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2230 write_member_type(file
, type
, TRUE
, NULL
, type_array_get_element(type
), NULL
, typestring_offset
);
2231 write_end(file
, typestring_offset
);
2234 return start_offset
;
2237 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
2239 const var_list_t
*fields
= type_struct_get_fields(type
);
2240 const var_t
*last_field
;
2243 if (!fields
|| list_empty(fields
))
2246 last_field
= LIST_ENTRY( list_tail(fields
), const var_t
, entry
);
2247 ft
= last_field
->type
;
2249 if (is_conformant_array(ft
) && !type_array_is_decl_as_ptr(ft
))
2252 if (type_get_type(ft
) == TYPE_STRUCT
)
2253 return find_array_or_string_in_struct(ft
);
2258 static void write_struct_members(FILE *file
, const type_t
*type
,
2259 int is_complex
, unsigned int *corroff
,
2260 unsigned int *typestring_offset
)
2263 unsigned short offset
= 0;
2264 unsigned int salign
= 1;
2266 var_list_t
*fields
= type_struct_get_fields(type
);
2268 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2270 type_t
*ft
= field
->type
;
2271 unsigned int align
= 0;
2272 unsigned int size
= type_memsize(ft
, &align
);
2273 align
= clamp_align(align
);
2274 if (salign
< align
) salign
= align
;
2276 if (!is_conformant_array(ft
) || type_array_is_decl_as_ptr(ft
))
2278 if ((align
- 1) & offset
)
2280 unsigned char fc
= 0;
2284 fc
= RPC_FC_ALIGNM2
;
2287 fc
= RPC_FC_ALIGNM4
;
2290 fc
= RPC_FC_ALIGNM8
;
2293 error("write_struct_members: cannot align type %d\n", type_get_type(ft
));
2295 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2296 offset
= ROUND_SIZE(offset
, align
);
2297 *typestring_offset
+= 1;
2299 write_member_type(file
, type
, is_complex
, field
->attrs
, field
->type
, corroff
,
2305 padding
= ROUNDING(offset
, salign
);
2308 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2309 RPC_FC_STRUCTPAD1
+ padding
- 1,
2311 *typestring_offset
+= 1;
2314 write_end(file
, typestring_offset
);
2317 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
,
2318 const char *name
, unsigned int *tfsoff
)
2320 const type_t
*save_current_structure
= current_structure
;
2321 unsigned int total_size
;
2323 unsigned int start_offset
;
2324 unsigned int array_offset
;
2325 int has_pointers
= 0;
2326 unsigned int align
= 0;
2327 unsigned int corroff
;
2329 unsigned char fc
= get_struct_fc(type
);
2330 var_list_t
*fields
= type_struct_get_fields(type
);
2333 current_structure
= type
;
2335 total_size
= type_memsize(type
, &align
);
2336 if (total_size
> USHRT_MAX
)
2337 error("structure size for %s exceeds %d bytes by %d bytes\n",
2338 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
2340 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2341 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
2343 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
2345 array
= find_array_or_string_in_struct(type
);
2346 if (array
&& !processed(array
->type
))
2348 = is_string_type(array
->attrs
, array
->type
)
2349 ? write_string_tfs(file
, array
->attrs
, array
->type
, FALSE
, array
->name
, tfsoff
)
2350 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
2353 write_descriptors(file
, type
, tfsoff
);
2355 start_offset
= *tfsoff
;
2356 update_tfsoff(type
, start_offset
, file
);
2357 print_start_tfs_comment(file
, type
, start_offset
);
2358 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2359 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2360 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size
, total_size
);
2365 unsigned int absoff
= array
->type
->typestring_offset
;
2366 short reloff
= absoff
- *tfsoff
;
2367 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2368 reloff
, reloff
, absoff
);
2371 else if (fc
== RPC_FC_BOGUS_STRUCT
)
2373 print_file(file
, 2, "NdrFcShort(0x0),\n");
2377 if (fc
== RPC_FC_BOGUS_STRUCT
)
2379 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2380 nothing is written to file yet. On the actual writing pass,
2381 this will have been updated. */
2382 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
2383 int reloff
= absoff
- *tfsoff
;
2384 assert( reloff
>= 0 );
2385 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2386 reloff
, reloff
, absoff
);
2389 else if ((fc
== RPC_FC_PSTRUCT
) ||
2390 (fc
== RPC_FC_CPSTRUCT
) ||
2391 (fc
== RPC_FC_CVSTRUCT
&& has_pointers
))
2393 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2394 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2396 write_pointer_description(file
, type
, tfsoff
);
2397 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2401 write_struct_members(file
, type
, fc
== RPC_FC_BOGUS_STRUCT
, &corroff
,
2404 if (fc
== RPC_FC_BOGUS_STRUCT
)
2408 type
->ptrdesc
= *tfsoff
;
2409 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
2411 type_t
*ft
= f
->type
;
2414 if (is_string_type(f
->attrs
, ft
))
2415 write_string_tfs(file
, f
->attrs
, ft
, FALSE
, f
->name
, tfsoff
);
2417 write_pointer_tfs(file
, f
->attrs
, ft
, FALSE
, tfsoff
);
2419 else if (type_get_type(ft
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(ft
))
2421 unsigned int offset
;
2423 print_file(file
, 0, "/* %d */\n", *tfsoff
);
2425 offset
= ft
->typestring_offset
;
2426 /* skip over the pointer that is written for strings, since a
2427 * pointer has to be written in-place here */
2428 if (is_string_type(f
->attrs
, ft
))
2430 write_nonsimple_pointer(file
, f
->attrs
, ft
, FALSE
, offset
, tfsoff
);
2433 if (type
->ptrdesc
== *tfsoff
)
2437 current_structure
= save_current_structure
;
2438 return start_offset
;
2441 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
2445 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
2449 if (type_get_type(t
) == TYPE_BASIC
|| type_get_type(t
) == TYPE_ENUM
)
2452 if (type_get_type(t
) == TYPE_BASIC
)
2453 fc
= get_basic_fc(t
);
2455 fc
= get_enum_fc(t
);
2456 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2457 fc
, string_of_type(fc
));
2459 else if (t
->typestring_offset
)
2461 short reloff
= t
->typestring_offset
- *tfsoff
;
2462 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2463 reloff
, reloff
, t
->typestring_offset
);
2466 error("write_branch_type: type unimplemented %d\n", type_get_type(t
));
2472 static unsigned int write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2475 unsigned int start_offset
;
2478 unsigned int nbranch
= 0;
2479 type_t
*deftype
= NULL
;
2480 short nodeftype
= 0xffff;
2486 size
= type_memsize(type
, &align
);
2488 fields
= type_union_get_cases(type
);
2490 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2492 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2494 nbranch
+= list_count(cases
);
2496 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2499 start_offset
= *tfsoff
;
2500 update_tfsoff(type
, start_offset
, file
);
2501 print_start_tfs_comment(file
, type
, start_offset
);
2502 if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
2504 const var_t
*sv
= type_union_get_switch_value(type
);
2505 const type_t
*st
= sv
->type
;
2508 if (type_get_type(st
) == TYPE_BASIC
)
2510 switch (get_basic_fc(st
))
2521 fc
= get_basic_fc(st
);
2525 error("union switch type must be an integer, char, or enum\n");
2528 else if (type_get_type(st
) == TYPE_ENUM
)
2529 fc
= get_enum_fc(st
);
2531 error("union switch type must be an integer, char, or enum\n");
2533 print_file(file
, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION
);
2534 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2535 0x40 | fc
, string_of_type(fc
));
2538 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
2540 static const expr_t dummy_expr
; /* FIXME */
2541 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
2544 if (type_get_type(st
) == TYPE_BASIC
)
2546 switch (get_basic_fc(st
))
2557 fc
= get_basic_fc(st
);
2561 error("union switch type must be an integer, char, or enum\n");
2564 else if (type_get_type(st
) == TYPE_ENUM
)
2565 fc
= get_enum_fc(st
);
2567 error("union switch type must be an integer, char, or enum\n");
2569 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
2570 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2571 fc
, string_of_type(fc
));
2574 *tfsoff
+= write_conf_or_var_desc(file
, NULL
, *tfsoff
, st
, &dummy_expr
);
2575 print_file(file
, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff
+ 2);
2577 print_file(file
, 0, "/* %u */\n", *tfsoff
);
2580 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size
, size
);
2581 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch
, nbranch
);
2584 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2586 type_t
*ft
= f
->type
;
2587 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2588 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2591 if (cases
== NULL
&& !deflt
)
2592 error("union field %s with neither case nor default attribute\n", f
->name
);
2594 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2596 /* MIDL doesn't check for duplicate cases, even though that seems
2597 like a reasonable thing to do, it just dumps them to the TFS
2598 like we're going to do here. */
2599 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c
->cval
, c
->cval
);
2601 write_branch_type(file
, ft
, tfsoff
);
2604 /* MIDL allows multiple default branches, even though that seems
2605 illogical, it just chooses the last one, which is what we will
2616 write_branch_type(file
, deftype
, tfsoff
);
2620 print_file(file
, 2, "NdrFcShort(0x%hx),\n", nodeftype
);
2624 return start_offset
;
2627 static unsigned int write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2628 unsigned int *typeformat_offset
)
2631 unsigned int start_offset
= *typeformat_offset
;
2632 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2636 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2637 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2639 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2643 const type_t
*base
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
2644 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2647 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2649 update_tfsoff(type
, start_offset
, file
);
2650 print_start_tfs_comment(file
, type
, start_offset
);
2651 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2652 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2653 print_file(file
, 2, "NdrFcLong(0x%08x),\n", uuid
->Data1
);
2654 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2655 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2656 for (i
= 0; i
< 8; ++i
)
2657 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2660 fprintf(file
, "\n");
2662 *typeformat_offset
+= 18;
2664 return start_offset
;
2667 static unsigned int write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2669 unsigned int *typeformat_offset
)
2671 unsigned int start_offset
= *typeformat_offset
;
2672 unsigned char flags
= 0;
2674 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2675 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2679 if (is_attr(var
->attrs
, ATTR_IN
))
2682 if (!is_attr(var
->attrs
, ATTR_OUT
))
2683 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2685 if (is_attr(var
->attrs
, ATTR_OUT
))
2688 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2689 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2690 /* return and can't be null values overlap */
2691 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2692 print_file(file
, 0, "can't be null, ");
2693 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2694 print_file(file
, 0, "serialize, ");
2695 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2696 print_file(file
, 0, "no serialize, ");
2697 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2698 print_file(file
, 0, "strict, ");
2699 if ((flags
& 0x21) == 0x20)
2700 print_file(file
, 0, "out, ");
2701 if ((flags
& 0x21) == 0x21)
2702 print_file(file
, 0, "return, ");
2704 print_file(file
, 0, "in, ");
2706 print_file(file
, 0, "via ptr, ");
2707 print_file(file
, 0, "*/\n");
2708 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2709 print_file(file
, 2, "0, /* FIXME: param num */\n");
2710 *typeformat_offset
+= 4;
2712 return start_offset
;
2715 static unsigned int write_typeformatstring_var(FILE *file
, int indent
, const var_t
*func
,
2716 type_t
*type
, const var_t
*var
,
2718 unsigned int *typeformat_offset
)
2720 unsigned int offset
;
2722 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
2724 case TGT_CTXT_HANDLE
:
2725 case TGT_CTXT_HANDLE_POINTER
:
2726 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2728 write_user_tfs(file
, type
, typeformat_offset
);
2729 return type
->typestring_offset
;
2731 return write_string_tfs(file
, var
->attrs
, type
, toplevel_param
, var
->name
, typeformat_offset
);
2736 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2737 ptr_type
= get_pointer_fc(type
, var
->attrs
, toplevel_param
);
2738 if (ptr_type
!= RPC_FC_RP
)
2740 unsigned int absoff
= type
->typestring_offset
;
2741 short reloff
= absoff
- (*typeformat_offset
+ 2);
2742 off
= *typeformat_offset
;
2743 print_file(file
, 0, "/* %d */\n", off
);
2744 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2745 string_of_type(ptr_type
));
2746 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2747 reloff
, reloff
, absoff
);
2748 *typeformat_offset
+= 4;
2753 if (processed(type
)) return type
->typestring_offset
;
2754 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2756 if (processed(type
)) return type
->typestring_offset
;
2757 return write_union_tfs(file
, type
, typeformat_offset
);
2762 case TGT_IFACE_POINTER
:
2763 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2767 size_t start_offset
= *typeformat_offset
;
2768 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2769 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2770 const type_t
*ref
= type_pointer_get_ref(type
);
2772 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
2774 /* special case for pointers to base types */
2780 if (type_get_type(ref
) == TYPE_ENUM
)
2781 fc
= get_enum_fc(ref
);
2783 fc
= get_basic_fc(ref
);
2785 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2786 get_pointer_fc(type
, var
->attrs
, toplevel_param
),
2787 (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2788 string_of_type(get_pointer_fc(type
, var
->attrs
, toplevel_param
)),
2789 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2790 print_file(file
, indent
, "0x%02x, /* %s */\n",
2791 fc
, string_of_type(fc
));
2792 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2793 *typeformat_offset
+= 4;
2794 return start_offset
;
2801 offset
= write_typeformatstring_var(file
, indent
, func
,
2802 type_pointer_get_ref(type
), var
,
2803 FALSE
, typeformat_offset
);
2805 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2806 return write_nonsimple_pointer(file
, var
->attrs
, type
,
2808 offset
, typeformat_offset
);
2812 error("invalid type %s for var %s\n", type
->name
, var
->name
);
2816 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2817 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2821 switch (typegen_detect_type(type
, attrs
, TDT_ALL_TYPES
))
2824 write_user_tfs(file
, type
, tfsoff
);
2827 write_string_tfs(file
, attrs
, type
, FALSE
, name
, tfsoff
);
2829 case TGT_IFACE_POINTER
:
2830 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2834 type_t
*ref
= type_pointer_get_ref(type
);
2836 if (!processed(ref
) && type_get_type(ref
) != TYPE_BASIC
)
2837 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2840 write_pointer_tfs(file
, attrs
, type
, FALSE
, tfsoff
);
2846 /* conformant arrays and strings are handled specially */
2847 if (!is_conformant_array(type
) || type_array_is_decl_as_ptr(type
) )
2849 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2850 if (is_conformant_array(type
))
2855 if (!processed(type
))
2856 write_struct_tfs(file
, type
, name
, tfsoff
);
2859 if (!processed(type
))
2860 write_union_tfs(file
, type
, tfsoff
);
2866 case TGT_CTXT_HANDLE
:
2867 case TGT_CTXT_HANDLE_POINTER
:
2869 error("invalid type %s for var %s\n", type
->name
, name
);
2876 static unsigned int process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2877 type_pred_t pred
, unsigned int *typeformat_offset
)
2880 const statement_t
*stmt
;
2882 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2884 const type_t
*iface
;
2885 const statement_t
*stmt_func
;
2887 if (stmt
->type
== STMT_LIBRARY
)
2889 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2892 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
2895 iface
= stmt
->u
.type
;
2899 current_iface
= iface
;
2900 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
2902 const var_t
*func
= stmt_func
->u
.var
;
2903 if (is_local(func
->attrs
)) continue;
2905 if (!is_void(type_function_get_rettype(func
->type
)))
2908 v
.type
= type_function_get_rettype(func
->type
);
2909 update_tfsoff(type_function_get_rettype(func
->type
),
2910 write_typeformatstring_var(
2912 type_function_get_rettype(func
->type
),
2913 &v
, FALSE
, typeformat_offset
),
2917 current_func
= func
;
2918 if (type_get_function_args(func
->type
))
2919 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2922 write_typeformatstring_var(
2923 file
, 2, func
, var
->type
, var
,
2924 TRUE
, typeformat_offset
),
2929 return *typeformat_offset
+ 1;
2932 static unsigned int process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2934 unsigned int typeformat_offset
= 2;
2936 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2940 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2944 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2945 print_file(file
, indent
, "{\n");
2947 print_file(file
, indent
, "0,\n");
2948 print_file(file
, indent
, "{\n");
2950 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2952 set_all_tfswrite(TRUE
);
2953 process_tfs(file
, stmts
, pred
);
2955 print_file(file
, indent
, "0x0\n");
2957 print_file(file
, indent
, "}\n");
2959 print_file(file
, indent
, "};\n");
2960 print_file(file
, indent
, "\n");
2963 static unsigned int get_required_buffer_size_type(
2964 const type_t
*type
, const char *name
, const attr_list_t
*attrs
, int toplevel_param
, unsigned int *alignment
)
2967 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
2972 const type_t
*utype
= get_user_type(type
, &uname
);
2973 return get_required_buffer_size_type(utype
, uname
, NULL
, FALSE
, alignment
);
2976 switch (get_basic_fc(type
))
2994 case RPC_FC_ERROR_STATUS_T
:
3004 case RPC_FC_BIND_PRIMITIVE
:
3008 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3009 get_basic_fc(type
));
3015 switch (get_enum_fc(type
))
3027 if (get_struct_fc(type
) == RPC_FC_STRUCT
)
3029 if (!type_struct_get_fields(type
)) return 0;
3030 return fields_memsize(type_struct_get_fields(type
), alignment
);
3035 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_RP
)
3037 const type_t
*ref
= type_pointer_get_ref(type
);
3038 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
3042 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
3044 if (get_struct_fc(ref
) == RPC_FC_STRUCT
)
3045 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
3048 case TGT_CTXT_HANDLE
:
3049 case TGT_CTXT_HANDLE_POINTER
:
3053 case TGT_IFACE_POINTER
:
3062 /* FIXME: depends on pointer type */
3063 return type_array_get_dim(type
) *
3064 get_required_buffer_size_type(type_array_get_element(type
), name
, NULL
, FALSE
, alignment
);
3072 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
3074 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3075 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3077 if (!in_attr
&& !out_attr
)
3082 if ((pass
== PASS_IN
&& in_attr
) || (pass
== PASS_OUT
&& out_attr
) ||
3083 pass
== PASS_RETURN
)
3085 if (is_ptrchain_attr(var
, ATTR_CONTEXTHANDLE
))
3091 if (!is_string_type(var
->attrs
, var
->type
))
3092 return get_required_buffer_size_type(var
->type
, var
->name
,
3093 var
->attrs
, TRUE
, alignment
);
3098 static unsigned int get_function_buffer_size( const var_t
*func
, enum pass pass
)
3101 unsigned int total_size
= 0, alignment
;
3103 if (type_get_function_args(func
->type
))
3105 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3107 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
3108 total_size
+= alignment
;
3112 if (pass
== PASS_OUT
&& !is_void(type_function_get_rettype(func
->type
)))
3115 v
.type
= type_function_get_rettype(func
->type
);
3116 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
3117 total_size
+= alignment
;
3122 static void print_phase_function(FILE *file
, int indent
, const char *type
,
3123 const char *local_var_prefix
, enum remoting_phase phase
,
3124 const var_t
*var
, unsigned int type_offset
)
3126 const char *function
;
3129 case PHASE_BUFFERSIZE
:
3130 function
= "BufferSize";
3133 function
= "Marshall";
3135 case PHASE_UNMARSHAL
:
3136 function
= "Unmarshall";
3146 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
3148 print_file(file
, indent
, "&__frame->_StubMsg,\n");
3149 print_file(file
, indent
, "%s%s%s%s%s,\n",
3150 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
3151 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
3153 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
3155 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3156 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
3157 if (phase
== PHASE_UNMARSHAL
)
3158 print_file(file
, indent
, "0);\n");
3162 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
3163 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
3164 const char *varname
)
3166 type_t
*type
= var
->type
;
3168 unsigned int alignment
= 0;
3171 /* no work to do for other phases, buffer sizing is done elsewhere */
3172 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
3175 ref
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3176 if (type_get_type(ref
) == TYPE_ENUM
)
3178 if (get_enum_fc(ref
) == RPC_FC_ENUM32
)
3183 else /* RPC_FC_ENUM16 */
3191 switch (get_basic_fc(ref
))
3211 case RPC_FC_ERROR_STATUS_T
:
3223 case RPC_FC_BIND_PRIMITIVE
:
3224 /* no marshalling needed */
3228 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3229 var
->name
, get_basic_fc(ref
));
3234 if (phase
== PHASE_MARSHAL
)
3235 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
3236 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3237 alignment
- 1, alignment
- 1);
3239 if (phase
== PHASE_MARSHAL
)
3241 print_file(file
, indent
, "*(");
3242 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3244 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
3246 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
3247 fprintf(file
, "%s%s", local_var_prefix
, varname
);
3248 fprintf(file
, ";\n");
3250 else if (phase
== PHASE_UNMARSHAL
)
3252 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
3253 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3254 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
3255 print_file(file
, indent
, "{\n");
3256 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3257 print_file(file
, indent
, "}\n");
3258 print_file(file
, indent
, "%s%s%s",
3259 (pass
== PASS_IN
|| pass
== PASS_RETURN
) ? "" : "*",
3260 local_var_prefix
, varname
);
3261 if (pass
== PASS_IN
&& is_ptr(type
))
3262 fprintf(file
, " = (");
3264 fprintf(file
, " = *(");
3265 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3266 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
3269 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
3270 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3271 fprintf(file
, ");\n");
3274 /* returns whether the MaxCount, Offset or ActualCount members need to be
3275 * filled in for the specified phase */
3276 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
3278 return (phase
!= PHASE_UNMARSHAL
);
3281 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
3285 for ( ; is_array(t
); t
= type_array_get_element(t
))
3286 if (type_array_has_conformance(t
))
3289 x
= type_array_get_conformance(t
);
3291 error("%s: multidimensional conformant"
3292 " arrays not supported at the top level\n",
3299 static void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
3300 enum remoting_phase phase
, const var_t
*var
)
3302 const type_t
*type
= var
->type
;
3303 /* get fundamental type for the argument */
3306 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
3308 else if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
))
3310 else if (type_is_alias(type
))
3311 type
= type_alias_get_aliasee(type
);
3312 else if (is_array(type
))
3314 if (is_conformance_needed_for_phase(phase
) && is_array(type
))
3316 if (type_array_has_conformance(type
))
3318 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3319 write_expr(file
, type_array_get_conformance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3320 fprintf(file
, ";\n\n");
3322 if (type_array_has_variance(type
))
3324 print_file(file
, indent
, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3325 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3326 write_expr(file
, type_array_get_variance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3327 fprintf(file
, ";\n\n");
3332 else if (type_get_type(type
) == TYPE_UNION
)
3334 if (is_conformance_needed_for_phase(phase
))
3336 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3337 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
3338 fprintf(file
, ";\n\n");
3342 else if (type_get_type(type
) == TYPE_INTERFACE
|| is_void(type
))
3346 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3348 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3349 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
3350 fprintf( file
, ";\n\n" );
3354 else if (is_ptr(type
))
3355 type
= type_pointer_get_ref(type
);
3361 static void write_remoting_arg(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3362 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
3364 int in_attr
, out_attr
, pointer_type
;
3365 const type_t
*type
= var
->type
;
3366 unsigned int start_offset
= type
->typestring_offset
;
3368 if (is_ptr(type
) || is_array(type
))
3369 pointer_type
= get_pointer_fc(type
, var
->attrs
, pass
!= PASS_RETURN
);
3373 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3374 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3375 if (!in_attr
&& !out_attr
)
3378 if (phase
!= PHASE_FREE
)
3382 if (!in_attr
) return;
3385 if (!out_attr
) return;
3391 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
);
3393 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
3395 case TGT_CTXT_HANDLE
:
3396 case TGT_CTXT_HANDLE_POINTER
:
3397 if (phase
== PHASE_MARSHAL
)
3399 if (pass
== PASS_IN
)
3401 /* if the context_handle attribute appears in the chain of types
3402 * without pointers being followed, then the context handle must
3403 * be direct, otherwise it is a pointer */
3404 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
3405 print_file(file
, indent
, "NdrClientContextMarshall(\n");
3406 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3407 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr
? "*" : "", local_var_prefix
, var
->name
);
3408 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
3412 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
3413 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3414 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
3415 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
3416 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3419 else if (phase
== PHASE_UNMARSHAL
)
3421 if (pass
== PASS_OUT
)
3424 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
3425 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
3426 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3427 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
3428 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
3432 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
3433 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3434 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3439 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
3442 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
||
3443 pointer_type
!= RPC_FC_RP
)
3445 if (pointer_type
== RPC_FC_RP
&& phase
== PHASE_FREE
&&
3446 !in_attr
&& is_conformant_array(type
))
3448 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3450 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3452 /* strings returned are assumed to be global and hence don't
3454 else if (is_declptr(type
) &&
3455 !(phase
== PHASE_FREE
&& pass
== PASS_RETURN
))
3456 print_phase_function(file
, indent
, "Pointer", local_var_prefix
,
3457 phase
, var
, start_offset
);
3461 unsigned int real_start_offset
= start_offset
;
3462 /* skip over pointer description straight to string description */
3463 if (is_declptr(type
))
3465 if (is_conformant_array(type
))
3466 real_start_offset
+= 4;
3468 real_start_offset
+= 2;
3470 if (is_array(type
) && !is_conformant_array(type
))
3471 print_phase_function(file
, indent
, "NonConformantString",
3472 local_var_prefix
, phase
, var
,
3475 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
3476 phase
, var
, real_start_offset
);
3481 unsigned char tc
= get_array_fc(type
);
3482 const char *array_type
= "FixedArray";
3484 /* We already have the size_is expression since it's at the
3485 top level, but do checks for multidimensional conformant
3486 arrays. When we handle them, we'll need to extend this
3487 function to return a list, and then we'll actually use
3488 the return value. */
3489 get_size_is_expr(type
, var
->name
);
3491 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
3493 array_type
= "VaryingArray";
3495 else if (tc
== RPC_FC_CARRAY
)
3497 array_type
= "ConformantArray";
3499 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
3501 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
3503 : "ConformantVaryingArray");
3506 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
3507 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
3508 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
3510 /* these are all unmarshalled by allocating memory */
3511 if (tc
== RPC_FC_BOGUS_ARRAY
||
3512 tc
== RPC_FC_CVARRAY
||
3513 ((tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
) && in_attr
) ||
3514 (tc
== RPC_FC_CARRAY
&& !in_attr
))
3516 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3518 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3524 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3525 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3528 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3530 if (phase
== PHASE_MARSHAL
)
3531 print_file(file
, indent
, "NdrSimpleTypeMarshall(\n");
3533 print_file(file
, indent
, "NdrSimpleTypeUnmarshall(\n");
3534 print_file(file
, indent
+1, "&__frame->_StubMsg,\n");
3535 print_file(file
, indent
+1, "(unsigned char *)&%s%s,\n",
3538 print_file(file
, indent
+1, "0x%02x /* %s */);\n", get_enum_fc(type
), string_of_type(get_enum_fc(type
)));
3542 switch (get_struct_fc(type
))
3545 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3546 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3548 case RPC_FC_PSTRUCT
:
3549 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3551 case RPC_FC_CSTRUCT
:
3552 case RPC_FC_CPSTRUCT
:
3553 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
3555 case RPC_FC_CVSTRUCT
:
3556 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
3558 case RPC_FC_BOGUS_STRUCT
:
3559 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
3562 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(type
));
3567 const char *union_type
= NULL
;
3569 if (type_get_type(type
) == TYPE_UNION
)
3570 union_type
= "NonEncapsulatedUnion";
3571 else if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
3572 union_type
= "EncapsulatedUnion";
3574 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3575 phase
, var
, start_offset
);
3580 const type_t
*ref
= type_pointer_get_ref(type
);
3581 if (pointer_type
== RPC_FC_RP
&& !is_user_type(ref
)) switch (type_get_type(ref
))
3584 /* base types have known sizes, so don't need a sizing pass
3585 * and don't have any memory to free and so don't need a
3587 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3588 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3591 /* base types have known sizes, so don't need a sizing pass
3592 * and don't have any memory to free and so don't need a
3594 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3595 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3599 const char *struct_type
= NULL
;
3600 switch (get_struct_fc(ref
))
3603 /* simple structs have known sizes, so don't need a sizing
3604 * pass and don't have any memory to free and so don't
3605 * need a freeing pass */
3606 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3607 struct_type
= "SimpleStruct";
3608 else if (phase
== PHASE_FREE
&& pass
== PASS_RETURN
)
3610 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3612 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3616 case RPC_FC_PSTRUCT
:
3617 struct_type
= "SimpleStruct";
3619 case RPC_FC_CSTRUCT
:
3620 case RPC_FC_CPSTRUCT
:
3621 struct_type
= "ConformantStruct";
3623 case RPC_FC_CVSTRUCT
:
3624 struct_type
= "ConformantVaryingStruct";
3626 case RPC_FC_BOGUS_STRUCT
:
3627 struct_type
= "ComplexStruct";
3630 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(ref
));
3635 if (phase
== PHASE_FREE
)
3636 struct_type
= "Pointer";
3638 start_offset
= ref
->typestring_offset
;
3639 print_phase_function(file
, indent
, struct_type
, local_var_prefix
, phase
, var
, start_offset
);
3644 case TYPE_ENCAPSULATED_UNION
:
3646 const char *union_type
= NULL
;
3647 if (phase
== PHASE_FREE
)
3648 union_type
= "Pointer";
3651 if (type_get_type(ref
) == TYPE_UNION
)
3652 union_type
= "NonEncapsulatedUnion";
3653 else if (type_get_type(ref
) == TYPE_ENCAPSULATED_UNION
)
3654 union_type
= "EncapsulatedUnion";
3656 start_offset
= ref
->typestring_offset
;
3659 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3660 phase
, var
, start_offset
);
3665 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3672 case TYPE_INTERFACE
:
3677 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3680 case TGT_IFACE_POINTER
:
3681 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3687 fprintf(file
, "\n");
3690 void write_remoting_arguments(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3691 enum pass pass
, enum remoting_phase phase
)
3693 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3695 unsigned int size
= get_function_buffer_size( func
, pass
);
3696 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
3699 if (pass
== PASS_RETURN
)
3703 var
.type
= type_function_get_rettype(func
->type
);
3704 var
.name
= xstrdup( "_RetVal" );
3705 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, &var
);
3711 if (!type_get_function_args(func
->type
))
3713 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3714 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
3719 unsigned int get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3721 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3725 unsigned int get_size_procformatstring_func(const var_t
*func
)
3728 unsigned int size
= 0;
3730 /* argument list size */
3731 if (type_get_function_args(func
->type
))
3732 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3733 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3735 /* return value size */
3736 if (is_void(type_function_get_rettype(func
->type
)))
3737 size
+= 2; /* FC_END and FC_PAD */
3739 size
+= get_size_procformatstring_type("return value", type_function_get_rettype(func
->type
), NULL
);
3744 unsigned int get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3746 const statement_t
*stmt
;
3747 unsigned int size
= 1;
3749 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3751 const type_t
*iface
;
3752 const statement_t
*stmt_func
;
3754 if (stmt
->type
== STMT_LIBRARY
)
3756 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3759 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
3762 iface
= stmt
->u
.type
;
3766 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
3768 const var_t
*func
= stmt_func
->u
.var
;
3769 if (!is_local(func
->attrs
))
3770 size
+= get_size_procformatstring_func( func
);
3776 unsigned int get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3778 set_all_tfswrite(FALSE
);
3779 return process_tfs(NULL
, stmts
, pred
);
3782 void declare_stub_args( FILE *file
, int indent
, const var_t
*func
)
3784 int in_attr
, out_attr
;
3788 /* declare return value '_RetVal' */
3789 if (!is_void(type_function_get_rettype(func
->type
)))
3791 print_file(file
, indent
, "%s", "");
3792 write_type_decl_left(file
, type_function_get_rettype(func
->type
));
3793 fprintf(file
, " _RetVal;\n");
3796 if (!type_get_function_args(func
->type
))
3799 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3801 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3802 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3803 if (!out_attr
&& !in_attr
)
3806 if (is_context_handle(var
->type
))
3807 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3810 if (!in_attr
&& !is_conformant_array(var
->type
))
3812 type_t
*type_to_print
;
3814 print_file(file
, indent
, "%s", "");
3815 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3816 !type_array_is_decl_as_ptr(var
->type
))
3817 type_to_print
= var
->type
;
3819 type_to_print
= type_pointer_get_ref(var
->type
);
3820 sprintf(name
, "_W%u", i
++);
3821 write_type_decl(file
, type_to_print
, name
);
3822 fprintf(file
, ";\n");
3825 print_file(file
, indent
, "%s", "");
3826 write_type_decl_left(file
, var
->type
);
3828 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3829 !type_array_is_decl_as_ptr(var
->type
)) {
3830 fprintf(file
, "(*%s)", var
->name
);
3832 fprintf(file
, "%s", var
->name
);
3833 write_type_right(file
, var
->type
, FALSE
);
3834 fprintf(file
, ";\n");
3836 if (decl_indirect(var
->type
))
3837 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
3843 void assign_stub_out_args( FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
3845 int in_attr
, out_attr
;
3849 if (!type_get_function_args(func
->type
))
3852 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3854 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3855 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3856 if (!out_attr
&& !in_attr
)
3861 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
3863 if (is_context_handle(var
->type
))
3865 fprintf(file
, " = NdrContextHandleInitialize(\n");
3866 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3867 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3868 var
->type
->typestring_offset
);
3870 else if (is_array(var
->type
) &&
3871 type_array_has_conformance(var
->type
))
3873 unsigned int size
, align
= 0;
3874 type_t
*type
= var
->type
;
3876 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
3878 is_array(type
) && type_array_has_conformance(type
);
3879 type
= type_array_get_element(type
))
3881 write_expr(file
, type_array_get_conformance(type
), TRUE
,
3882 TRUE
, NULL
, NULL
, local_var_prefix
);
3883 fprintf(file
, " * ");
3885 size
= type_memsize(type
, &align
);
3886 fprintf(file
, "%u);\n", size
);
3890 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
3891 switch (typegen_detect_type(type_pointer_get_ref(var
->type
), var
->attrs
, TDT_IGNORE_STRINGS
))
3896 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
3901 case TGT_IFACE_POINTER
:
3903 case TGT_CTXT_HANDLE
:
3904 case TGT_CTXT_HANDLE_POINTER
:
3907 /* not initialised */
3917 fprintf(file
, "\n");
3921 int write_expr_eval_routines(FILE *file
, const char *iface
)
3923 static const char *var_name
= "pS";
3924 static const char *var_name_expr
= "pS->";
3926 struct expr_eval_routine
*eval
;
3927 unsigned short callback_offset
= 0;
3929 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3931 const char *name
= eval
->structure
->name
;
3934 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
3935 iface
, name
, callback_offset
);
3936 print_file(file
, 0, "{\n");
3937 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3938 name
, var_name
, name
, eval
->baseoff
);
3939 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
3940 print_file(file
, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
3941 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
, "");
3942 fprintf(file
, ";\n");
3943 print_file(file
, 0, "}\n\n");
3949 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
3951 struct expr_eval_routine
*eval
;
3952 struct expr_eval_routine
*cursor
;
3953 unsigned short callback_offset
= 0;
3955 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
3956 fprintf(file
, "{\n");
3958 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
3960 const char *name
= eval
->structure
->name
;
3961 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3963 list_remove(&eval
->entry
);
3967 fprintf(file
, "};\n\n");
3970 void write_user_quad_list(FILE *file
)
3974 if (list_empty(&user_type_list
))
3977 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3978 fprintf(file
, "{\n");
3979 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3981 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3982 print_file(file
, 1, "{\n");
3983 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3984 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3985 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3986 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3987 print_file(file
, 1, "}%s\n", sep
);
3989 fprintf(file
, "};\n\n");
3992 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3994 const struct str_list_entry_t
*endpoint
;
3997 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3998 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3999 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
4001 print_file( f
, 1, "{ (const unsigned char *)\"" );
4002 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
4004 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
4007 if (!*p
) goto error
;
4008 if (p
[1] != '[') goto error
;
4010 fprintf( f
, "\", (const unsigned char *)\"" );
4011 for (p
+= 2; *p
&& *p
!= ']'; p
++)
4013 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
4016 if (*p
!= ']') goto error
;
4017 fprintf( f
, "\" },\n" );
4019 print_file( f
, 0, "};\n\n" );
4023 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
4026 void write_exceptions( FILE *file
)
4028 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4029 fprintf( file
, "\n");
4030 fprintf( file
, "#include \"wine/exception.h\"\n");
4031 fprintf( file
, "#undef RpcTryExcept\n");
4032 fprintf( file
, "#undef RpcExcept\n");
4033 fprintf( file
, "#undef RpcEndExcept\n");
4034 fprintf( file
, "#undef RpcTryFinally\n");
4035 fprintf( file
, "#undef RpcFinally\n");
4036 fprintf( file
, "#undef RpcEndFinally\n");
4037 fprintf( file
, "#undef RpcExceptionCode\n");
4038 fprintf( file
, "#undef RpcAbnormalTermination\n");
4039 fprintf( file
, "\n");
4040 fprintf( file
, "struct __exception_frame;\n");
4041 fprintf( file
, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
4042 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4043 fprintf( file
, "\n");
4044 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4045 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4046 fprintf( file
, " __filter_func filter; \\\n");
4047 fprintf( file
, " __finally_func finally; \\\n");
4048 fprintf( file
, " sigjmp_buf jmp; \\\n");
4049 fprintf( file
, " DWORD code; \\\n");
4050 fprintf( file
, " unsigned char abnormal_termination; \\\n");
4051 fprintf( file
, " unsigned char filter_level; \\\n");
4052 fprintf( file
, " unsigned char finally_level;\n");
4053 fprintf( file
, "\n");
4054 fprintf( file
, "struct __exception_frame\n{\n");
4055 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
4056 fprintf( file
, "};\n");
4057 fprintf( file
, "\n");
4058 fprintf( file
, "static inline void __widl_unwind_target(void)\n" );
4059 fprintf( file
, "{\n");
4060 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4061 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4062 fprintf( file
, " {\n");
4063 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4064 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4065 fprintf( file
, " __wine_pop_frame( &exc_frame->frame );\n");
4066 fprintf( file
, " }\n");
4067 fprintf( file
, " exc_frame->filter_level = 0;\n");
4068 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
4069 fprintf( file
, "}\n");
4070 fprintf( file
, "\n");
4071 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4072 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4073 fprintf( file
, " CONTEXT *context,\n");
4074 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4075 fprintf( file
, "{\n");
4076 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4077 fprintf( file
, "\n");
4078 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4079 fprintf( file
, " {\n" );
4080 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4081 fprintf( file
, " {\n" );
4082 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4083 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4084 fprintf( file
, " }\n" );
4085 fprintf( file
, " return ExceptionContinueSearch;\n");
4086 fprintf( file
, " }\n" );
4087 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
4088 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4089 fprintf( file
, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4090 fprintf( file
, " return ExceptionContinueSearch;\n");
4091 fprintf( file
, "}\n");
4092 fprintf( file
, "\n");
4093 fprintf( file
, "#define RpcTryExcept \\\n");
4094 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4095 fprintf( file
, " { \\\n");
4096 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4097 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4098 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
4099 fprintf( file
, "\n");
4100 fprintf( file
, "#define RpcExcept(expr) \\\n");
4101 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4102 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4103 fprintf( file
, " __frame->filter_level = 0; \\\n" );
4104 fprintf( file
, " } \\\n");
4105 fprintf( file
, " else \\\n");
4106 fprintf( file
, "\n");
4107 fprintf( file
, "#define RpcEndExcept\n");
4108 fprintf( file
, "\n");
4109 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
4110 fprintf( file
, "\n");
4111 fprintf( file
, "#define RpcTryFinally \\\n");
4112 fprintf( file
, " if (!__frame->filter_level) \\\n");
4113 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4114 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
4115 fprintf( file
, "\n");
4116 fprintf( file
, "#define RpcFinally \\\n");
4117 fprintf( file
, " if (!__frame->filter_level) \\\n");
4118 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4119 fprintf( file
, " __frame->finally_level = 0;\n");
4120 fprintf( file
, "\n");
4121 fprintf( file
, "#define RpcEndFinally\n");
4122 fprintf( file
, "\n");
4123 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4124 fprintf( file
, "\n");
4125 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4126 fprintf( file
, " do { \\\n");
4127 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4128 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4129 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
4130 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
4131 fprintf( file
, " __frame->filter_level = 0; \\\n");
4132 fprintf( file
, " __frame->finally_level = 0; \\\n");
4133 fprintf( file
, " } while (0)\n");
4134 fprintf( file
, "\n");
4135 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4136 fprintf( file
, "\n");
4137 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4138 fprintf( file
, " do { (void)(filter_func); } while(0)\n");
4139 fprintf( file
, "\n");
4140 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4141 fprintf( file
, " DWORD code;\n");
4142 fprintf( file
, "\n");
4143 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");