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 static 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";
119 case RPC_FC_INT3264
: return "FC_INT3264";
120 case RPC_FC_UINT3264
: return "FC_UINT3264";
122 error("string_of_type: unknown type 0x%02x\n", type
);
127 static void *get_aliaschain_attrp(const type_t
*type
, enum attr_type attr
)
129 const type_t
*t
= type
;
132 if (is_attr(t
->attrs
, attr
))
133 return get_attrp(t
->attrs
, attr
);
134 else if (type_is_alias(t
))
135 t
= type_alias_get_aliasee(t
);
140 unsigned char get_basic_fc(const type_t
*type
)
142 int sign
= type_basic_get_sign(type
);
143 switch (type_basic_get_type(type
))
145 case TYPE_BASIC_INT8
: return (sign
<= 0 ? RPC_FC_SMALL
: RPC_FC_USMALL
);
146 case TYPE_BASIC_INT16
: return (sign
<= 0 ? RPC_FC_SHORT
: RPC_FC_USHORT
);
147 case TYPE_BASIC_INT32
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
148 case TYPE_BASIC_INT64
: return RPC_FC_HYPER
;
149 case TYPE_BASIC_INT
: return (sign
<= 0 ? RPC_FC_LONG
: RPC_FC_ULONG
);
150 case TYPE_BASIC_INT3264
: return (sign
<= 0 ? RPC_FC_INT3264
: RPC_FC_UINT3264
);
151 case TYPE_BASIC_BYTE
: return RPC_FC_BYTE
;
152 case TYPE_BASIC_CHAR
: return RPC_FC_CHAR
;
153 case TYPE_BASIC_WCHAR
: return RPC_FC_WCHAR
;
154 case TYPE_BASIC_HYPER
: return RPC_FC_HYPER
;
155 case TYPE_BASIC_FLOAT
: return RPC_FC_FLOAT
;
156 case TYPE_BASIC_DOUBLE
: return RPC_FC_DOUBLE
;
157 case TYPE_BASIC_ERROR_STATUS_T
: return RPC_FC_ERROR_STATUS_T
;
158 case TYPE_BASIC_HANDLE
: return RPC_FC_BIND_PRIMITIVE
;
163 static inline unsigned int clamp_align(unsigned int align
)
165 unsigned int packing
= (pointer_size
== 4) ? win32_packing
: win64_packing
;
166 if(align
> packing
) align
= packing
;
170 unsigned char get_pointer_fc(const type_t
*type
, const attr_list_t
*attrs
, int toplevel_param
)
175 assert(is_ptr(type
) || is_array(type
));
177 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
181 for (t
= type
; type_is_alias(t
); t
= type_alias_get_aliasee(t
))
183 pointer_type
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
190 else if (is_ptr(type
))
191 return type_pointer_get_default_fc(type
);
193 return type_array_get_ptr_default_fc(type
);
196 static unsigned char get_enum_fc(const type_t
*type
)
198 assert(type_get_type(type
) == TYPE_ENUM
);
199 if (is_aliaschain_attr(type
, ATTR_V1ENUM
))
200 return RPC_FC_ENUM32
;
202 return RPC_FC_ENUM16
;
205 enum typegen_type
typegen_detect_type(const type_t
*type
, const attr_list_t
*attrs
, unsigned int flags
)
207 if (is_user_type(type
))
208 return TGT_USER_TYPE
;
210 if (is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
))
211 return TGT_CTXT_HANDLE
;
213 if (!(flags
& TDT_IGNORE_STRINGS
) && is_string_type(attrs
, type
))
216 switch (type_get_type(type
))
219 if (!(flags
& TDT_IGNORE_RANGES
) &&
220 (is_attr(attrs
, ATTR_RANGE
) || is_aliaschain_attr(type
, ATTR_RANGE
)))
224 if (!(flags
& TDT_IGNORE_RANGES
) &&
225 (is_attr(attrs
, ATTR_RANGE
) || is_aliaschain_attr(type
, ATTR_RANGE
)))
229 if (type_get_type(type_pointer_get_ref(type
)) == TYPE_INTERFACE
||
230 (type_get_type(type_pointer_get_ref(type
)) == TYPE_VOID
&& is_attr(attrs
, ATTR_IIDIS
)))
231 return TGT_IFACE_POINTER
;
232 else if (is_aliaschain_attr(type_pointer_get_ref(type
), ATTR_CONTEXTHANDLE
))
233 return TGT_CTXT_HANDLE_POINTER
;
238 case TYPE_ENCAPSULATED_UNION
:
255 unsigned char get_struct_fc(const type_t
*type
)
258 int has_conformance
= 0;
259 int has_variance
= 0;
263 fields
= type_struct_get_fields(type
);
265 if (get_padding(fields
))
266 return RPC_FC_BOGUS_STRUCT
;
268 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, var_t
, entry
)
270 type_t
*t
= field
->type
;
271 enum typegen_type typegen_type
;
273 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
275 if (typegen_type
== TGT_ARRAY
&& !type_array_is_decl_as_ptr(t
))
277 if (is_string_type(field
->attrs
, field
->type
))
279 if (is_conformant_array(t
))
285 if (is_array(type_array_get_element(field
->type
)))
286 return RPC_FC_BOGUS_STRUCT
;
288 if (type_array_has_conformance(field
->type
))
291 if (list_next(fields
, &field
->entry
))
292 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
295 if (type_array_has_variance(t
))
298 t
= type_array_get_element(t
);
299 typegen_type
= typegen_detect_type(t
, field
->attrs
, TDT_IGNORE_STRINGS
);
302 switch (typegen_type
)
305 case TGT_IFACE_POINTER
:
306 return RPC_FC_BOGUS_STRUCT
;
308 if (type_basic_get_type(t
) == TYPE_BASIC_INT3264
&& pointer_size
!= 4)
309 return RPC_FC_BOGUS_STRUCT
;
312 if (get_enum_fc(t
) == RPC_FC_ENUM16
)
313 return RPC_FC_BOGUS_STRUCT
;
317 if (get_pointer_fc(t
, field
->attrs
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
318 return RPC_FC_BOGUS_STRUCT
;
322 return RPC_FC_BOGUS_STRUCT
;
325 unsigned char fc
= get_struct_fc(t
);
330 case RPC_FC_CVSTRUCT
:
336 case RPC_FC_CPSTRUCT
:
338 if (list_next( fields
, &field
->entry
))
339 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
346 if (list_next( fields
, &field
->entry
))
347 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
356 error_loc("Unknown struct member %s with type (0x%02x)\n", field
->name
, fc
);
357 /* fallthru - treat it as complex */
359 /* as soon as we see one of these these members, it's bogus... */
360 case RPC_FC_BOGUS_STRUCT
:
361 return RPC_FC_BOGUS_STRUCT
;
366 return RPC_FC_BOGUS_STRUCT
;
368 /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
370 case TGT_CTXT_HANDLE
:
371 case TGT_CTXT_HANDLE_POINTER
:
372 /* checking after parsing should mean that we don't get here. if we do,
373 * it's a checker bug */
380 if ( has_conformance
)
381 return RPC_FC_CVSTRUCT
;
383 return RPC_FC_BOGUS_STRUCT
;
385 if( has_conformance
&& has_pointer
)
386 return RPC_FC_CPSTRUCT
;
387 if( has_conformance
)
388 return RPC_FC_CSTRUCT
;
390 return RPC_FC_PSTRUCT
;
391 return RPC_FC_STRUCT
;
394 static unsigned char get_array_fc(const type_t
*type
)
397 const expr_t
*size_is
;
398 const type_t
*elem_type
;
400 elem_type
= type_array_get_element(type
);
401 size_is
= type_array_get_conformance(type
);
405 unsigned int align
= 0;
406 unsigned int size
= type_memsize(elem_type
, &align
);
407 if (size
* type_array_get_dim(type
) > 0xffffuL
)
408 fc
= RPC_FC_LGFARRAY
;
410 fc
= RPC_FC_SMFARRAY
;
415 if (type_array_has_variance(type
))
417 if (fc
== RPC_FC_SMFARRAY
)
418 fc
= RPC_FC_SMVARRAY
;
419 else if (fc
== RPC_FC_LGFARRAY
)
420 fc
= RPC_FC_LGVARRAY
;
421 else if (fc
== RPC_FC_CARRAY
)
425 switch (typegen_detect_type(elem_type
, NULL
, TDT_IGNORE_STRINGS
))
428 fc
= RPC_FC_BOGUS_ARRAY
;
431 if (type_basic_get_type(elem_type
) == TYPE_BASIC_INT3264
&&
433 fc
= RPC_FC_BOGUS_ARRAY
;
436 switch (get_struct_fc(elem_type
))
438 case RPC_FC_BOGUS_STRUCT
:
439 fc
= RPC_FC_BOGUS_ARRAY
;
444 /* is 16-bit enum - if so, wire size differs from mem size and so
445 * the array cannot be block copied, which means the array is complex */
446 if (get_enum_fc(elem_type
) == RPC_FC_ENUM16
)
447 fc
= RPC_FC_BOGUS_ARRAY
;
450 case TGT_IFACE_POINTER
:
451 fc
= RPC_FC_BOGUS_ARRAY
;
454 /* ref pointers cannot just be block copied. unique pointers to
455 * interfaces need special treatment. either case means the array is
457 if (get_pointer_fc(elem_type
, NULL
, FALSE
) == RPC_FC_RP
|| pointer_size
!= 4)
458 fc
= RPC_FC_BOGUS_ARRAY
;
461 fc
= RPC_FC_BOGUS_ARRAY
;
463 case TGT_CTXT_HANDLE
:
464 case TGT_CTXT_HANDLE_POINTER
:
468 /* nothing to do for everything else */
475 int is_struct(unsigned char type
)
482 case RPC_FC_CPSTRUCT
:
483 case RPC_FC_CVSTRUCT
:
484 case RPC_FC_BOGUS_STRUCT
:
491 static int is_non_complex_struct(const type_t
*type
)
493 return (type_get_type(type
) == TYPE_STRUCT
&&
494 get_struct_fc(type
) != RPC_FC_BOGUS_STRUCT
);
497 static int type_has_pointers(const type_t
*type
)
499 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
506 return type_array_is_decl_as_ptr(type
) || type_has_pointers(type_array_get_element(type
));
509 var_list_t
*fields
= type_struct_get_fields(type
);
511 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
513 if (type_has_pointers(field
->type
))
522 fields
= type_union_get_cases(type
);
523 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
525 if (field
->type
&& type_has_pointers(field
->type
))
530 case TGT_CTXT_HANDLE
:
531 case TGT_CTXT_HANDLE_POINTER
:
533 case TGT_IFACE_POINTER
:
544 static int type_has_full_pointer(const type_t
*type
, const attr_list_t
*attrs
,
547 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
))
552 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
557 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_FP
)
560 return type_has_full_pointer(type_array_get_element(type
), NULL
, FALSE
);
563 var_list_t
*fields
= type_struct_get_fields(type
);
565 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
567 if (type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
576 fields
= type_union_get_cases(type
);
577 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
579 if (field
->type
&& type_has_full_pointer(field
->type
, field
->attrs
, FALSE
))
584 case TGT_CTXT_HANDLE
:
585 case TGT_CTXT_HANDLE_POINTER
:
587 case TGT_IFACE_POINTER
:
598 static unsigned short user_type_offset(const char *name
)
601 unsigned short off
= 0;
602 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
604 if (strcmp(name
, ut
->name
) == 0)
608 error("user_type_offset: couldn't find type (%s)\n", name
);
612 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
614 type
->typestring_offset
= offset
;
615 if (file
) type
->tfswrite
= FALSE
;
618 static void guard_rec(type_t
*type
)
620 /* types that contain references to themselves (like a linked list),
621 need to be shielded from infinite recursion when writing embedded
623 if (type
->typestring_offset
)
624 type
->tfswrite
= FALSE
;
626 type
->typestring_offset
= 1;
629 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
633 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
641 if (type_is_alias(t
))
642 t
= type_alias_get_aliasee(t
);
648 int is_user_type(const type_t
*t
)
650 return get_user_type(t
, NULL
) != NULL
;
653 static int is_embedded_complex(const type_t
*type
)
655 switch (typegen_detect_type(type
, NULL
, TDT_ALL_TYPES
))
661 case TGT_IFACE_POINTER
:
668 static const char *get_context_handle_type_name(const type_t
*type
)
672 is_ptr(t
) || type_is_alias(t
);
673 t
= type_is_alias(t
) ? type_alias_get_aliasee(t
) : type_pointer_get_ref(t
))
674 if (is_attr(t
->attrs
, ATTR_CONTEXTHANDLE
))
680 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
683 fprintf(file, "/* %2u */\n", typestring_offset); \
684 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
688 static void print_file(FILE *file
, int indent
, const char *format
, ...) __attribute__((format (printf
, 3, 4)));
689 static void print_file(FILE *file
, int indent
, const char *format
, ...)
692 va_start(va
, format
);
693 print(file
, indent
, format
, va
);
697 void print(FILE *file
, int indent
, const char *format
, va_list va
)
701 if (format
[0] != '\n')
704 vfprintf(file
, format
, va
);
709 static void write_var_init(FILE *file
, int indent
, const type_t
*t
, const char *n
, const char *local_var_prefix
)
711 if (decl_indirect(t
))
713 print_file(file
, indent
, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
714 local_var_prefix
, n
, local_var_prefix
, n
);
715 print_file(file
, indent
, "%s_p_%s = &%s%s;\n", local_var_prefix
, n
, local_var_prefix
, n
);
717 else if (is_ptr(t
) || is_array(t
))
718 print_file(file
, indent
, "%s%s = 0;\n", local_var_prefix
, n
);
721 void write_parameters_init(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
725 if (!is_void(type_function_get_rettype(func
->type
)))
726 write_var_init(file
, indent
, type_function_get_rettype(func
->type
), "_RetVal", local_var_prefix
);
728 if (!type_get_function_args(func
->type
))
731 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
732 write_var_init(file
, indent
, var
->type
, var
->name
, local_var_prefix
);
737 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
739 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
740 print_file(f
, indent
, "{\n");
741 print_file(f
, indent
+ 1, "short Pad;\n");
742 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
743 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
744 print_file(f
, indent
, "\n");
747 void write_formatstringsdecl(FILE *f
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
751 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
752 get_size_typeformatstring(stmts
, pred
));
754 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
755 get_size_procformatstring(stmts
, pred
));
758 write_formatdesc(f
, indent
, "TYPE");
759 write_formatdesc(f
, indent
, "PROC");
761 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
762 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
763 print_file(f
, indent
, "\n");
766 int decl_indirect(const type_t
*t
)
770 return (type_get_type(t
) != TYPE_BASIC
&&
771 type_get_type(t
) != TYPE_ENUM
&&
772 type_get_type(t
) != TYPE_POINTER
&&
773 type_get_type(t
) != TYPE_ARRAY
);
776 static unsigned int write_procformatstring_type(FILE *file
, int indent
,
779 const attr_list_t
*attrs
,
784 int is_in
= is_attr(attrs
, ATTR_IN
);
785 int is_out
= is_attr(attrs
, ATTR_OUT
);
787 if (!is_in
&& !is_out
) is_in
= TRUE
;
789 if (type_get_type(type
) == TYPE_BASIC
||
790 type_get_type(type
) == TYPE_ENUM
)
795 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
797 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
799 if (type_get_type(type
) == TYPE_ENUM
)
801 fc
= get_enum_fc(type
);
805 fc
= get_basic_fc(type
);
807 if (fc
== RPC_FC_BIND_PRIMITIVE
)
811 print_file(file
, indent
, "0x%02x, /* %s */\n",
812 fc
, string_of_type(fc
));
813 size
= 2; /* includes param type prefix */
818 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
819 else if (is_in
&& is_out
)
820 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
822 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
824 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
826 print_file(file
, indent
, "0x01,\n");
827 print_file(file
, indent
, "NdrFcShort(0x%hx),\n", type
->typestring_offset
);
828 size
= 4; /* includes param type prefix */
833 static void write_procformatstring_stmts(FILE *file
, int indent
, const statement_list_t
*stmts
, type_pred_t pred
)
835 const statement_t
*stmt
;
836 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
838 if (stmt
->type
== STMT_TYPE
&& type_get_type(stmt
->u
.type
) == TYPE_INTERFACE
)
840 const statement_t
*stmt_func
;
841 if (!pred(stmt
->u
.type
))
843 STATEMENTS_FOR_EACH_FUNC(stmt_func
, type_iface_get_stmts(stmt
->u
.type
))
845 const var_t
*func
= stmt_func
->u
.var
;
846 if (is_local(func
->attrs
)) continue;
847 /* emit argument data */
848 if (type_get_function_args(func
->type
))
851 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
852 write_procformatstring_type(file
, indent
, var
->name
, var
->type
, var
->attrs
, FALSE
);
855 /* emit return value data */
856 if (is_void(type_function_get_rettype(func
->type
)))
858 print_file(file
, indent
, "0x5b, /* FC_END */\n");
859 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
862 write_procformatstring_type(file
, indent
, "return value", type_function_get_rettype(func
->type
), NULL
, TRUE
);
865 else if (stmt
->type
== STMT_LIBRARY
)
866 write_procformatstring_stmts(file
, indent
, stmt
->u
.lib
->stmts
, pred
);
870 void write_procformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
874 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
875 print_file(file
, indent
, "{\n");
877 print_file(file
, indent
, "0,\n");
878 print_file(file
, indent
, "{\n");
881 write_procformatstring_stmts(file
, indent
, stmts
, pred
);
883 print_file(file
, indent
, "0x0\n");
885 print_file(file
, indent
, "}\n");
887 print_file(file
, indent
, "};\n");
888 print_file(file
, indent
, "\n");
891 static int write_base_type(FILE *file
, const type_t
*type
, int convert_to_signed_type
, unsigned int *typestring_offset
)
895 if (type_get_type(type
) == TYPE_BASIC
)
896 fc
= get_basic_fc(type
);
897 else if (type_get_type(type
) == TYPE_ENUM
)
898 fc
= get_enum_fc(type
);
902 if (convert_to_signed_type
)
918 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
919 *typestring_offset
+= 1;
923 /* write conformance / variance descriptor */
924 static unsigned int write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
925 unsigned int baseoff
, const type_t
*type
,
928 unsigned char operator_type
= 0;
929 unsigned char conftype
= RPC_FC_NORMAL_CONFORMANCE
;
930 const char *conftype_string
= "";
931 const char *operator_string
= "no operators";
932 const expr_t
*subexpr
;
936 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
942 /* Top-level conformance calculations are done inline. */
943 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
944 RPC_FC_TOP_LEVEL_CONFORMANCE
);
945 print_file (file
, 2, "0x0,\n");
946 print_file (file
, 2, "NdrFcShort(0x0),\n");
952 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
953 error("write_conf_or_var_desc: constant value %ld is greater than "
954 "the maximum constant size of %d\n", expr
->cval
,
955 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
957 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
958 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
959 print_file(file
, 2, "0x%lx,\n", expr
->cval
>> 16);
960 print_file(file
, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr
->cval
);
965 if (is_ptr(type
) || (is_array(type
) && type_array_is_decl_as_ptr(type
)))
967 conftype
= RPC_FC_POINTER_CONFORMANCE
;
968 conftype_string
= "field pointer, ";
972 switch (subexpr
->type
)
975 subexpr
= subexpr
->ref
;
976 operator_type
= RPC_FC_DEREFERENCE
;
977 operator_string
= "FC_DEREFERENCE";
980 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
982 subexpr
= subexpr
->ref
;
983 operator_type
= RPC_FC_DIV_2
;
984 operator_string
= "FC_DIV_2";
988 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
990 subexpr
= subexpr
->ref
;
991 operator_type
= RPC_FC_MULT_2
;
992 operator_string
= "FC_MULT_2";
996 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
998 subexpr
= subexpr
->ref
;
999 operator_type
= RPC_FC_SUB_1
;
1000 operator_string
= "FC_SUB_1";
1004 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
1006 subexpr
= subexpr
->ref
;
1007 operator_type
= RPC_FC_ADD_1
;
1008 operator_string
= "FC_ADD_1";
1015 if (subexpr
->type
== EXPR_IDENTIFIER
)
1017 const type_t
*correlation_variable
= NULL
;
1018 unsigned char param_type
= 0;
1019 unsigned int offset
= 0;
1021 var_list_t
*fields
= type_struct_get_fields(structure
);
1023 if (fields
) LIST_FOR_EACH_ENTRY( var
, fields
, const var_t
, entry
)
1025 unsigned int size
= field_memsize( var
->type
, &offset
);
1026 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
1028 correlation_variable
= var
->type
;
1033 if (!correlation_variable
)
1034 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
1037 correlation_variable
= expr_resolve_type(NULL
, structure
, expr
);
1041 if (type_get_type(correlation_variable
) == TYPE_BASIC
)
1043 switch (get_basic_fc(correlation_variable
))
1047 param_type
= RPC_FC_SMALL
;
1051 param_type
= RPC_FC_USMALL
;
1055 param_type
= RPC_FC_SHORT
;
1058 param_type
= RPC_FC_USHORT
;
1061 param_type
= RPC_FC_LONG
;
1064 param_type
= RPC_FC_ULONG
;
1067 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
1068 get_basic_fc(correlation_variable
));
1071 else if (type_get_type(correlation_variable
) == TYPE_ENUM
)
1073 if (get_enum_fc(correlation_variable
) == RPC_FC_ENUM32
)
1074 param_type
= RPC_FC_LONG
;
1076 param_type
= RPC_FC_SHORT
;
1080 error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
1085 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
1086 conftype
| param_type
, conftype_string
, string_of_type(param_type
));
1087 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
1088 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
1093 unsigned int callback_offset
= 0;
1094 struct expr_eval_routine
*eval
;
1097 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
1099 if (!strcmp (eval
->structure
->name
, structure
->name
)
1100 && !compare_expr (eval
->expr
, expr
))
1110 eval
= xmalloc (sizeof(*eval
));
1111 eval
->structure
= structure
;
1112 eval
->baseoff
= baseoff
;
1114 list_add_tail (&expr_eval_routines
, &eval
->entry
);
1117 if (callback_offset
> USHRT_MAX
)
1118 error("Maximum number of callback routines reached\n");
1120 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n", conftype
, conftype_string
);
1121 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
1122 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", callback_offset
, callback_offset
);
1127 /* return size and start offset of a data field based on current offset */
1128 static unsigned int field_memsize(const type_t
*type
, unsigned int *offset
)
1130 unsigned int align
= 0;
1131 unsigned int size
= type_memsize( type
, &align
);
1133 *offset
= ROUND_SIZE( *offset
, align
);
1137 static unsigned int fields_memsize(const var_list_t
*fields
, unsigned int *align
)
1139 unsigned int size
= 0;
1140 unsigned int max_align
;
1143 if (!fields
) return 0;
1144 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1146 unsigned int falign
= 0;
1147 unsigned int fsize
= type_memsize(v
->type
, &falign
);
1148 if (*align
< falign
) *align
= falign
;
1149 falign
= clamp_align(falign
);
1150 size
= ROUND_SIZE(size
, falign
);
1154 max_align
= clamp_align(*align
);
1155 size
= ROUND_SIZE(size
, max_align
);
1160 static unsigned int union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
1162 unsigned int size
, maxs
= 0;
1163 unsigned int align
= *pmaxa
;
1166 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
1168 /* we could have an empty default field with NULL type */
1171 size
= type_memsize(v
->type
, &align
);
1172 if (maxs
< size
) maxs
= size
;
1173 if (*pmaxa
< align
) *pmaxa
= align
;
1180 int get_padding(const var_list_t
*fields
)
1182 unsigned short offset
= 0;
1183 unsigned int salign
= 1;
1189 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
1191 type_t
*ft
= f
->type
;
1192 unsigned int align
= 0;
1193 unsigned int size
= type_memsize(ft
, &align
);
1194 align
= clamp_align(align
);
1195 if (align
> salign
) salign
= align
;
1196 offset
= ROUND_SIZE(offset
, align
);
1200 return ROUNDING(offset
, salign
);
1203 unsigned int type_memsize(const type_t
*t
, unsigned int *align
)
1205 unsigned int size
= 0;
1207 switch (type_get_type(t
))
1210 switch (get_basic_fc(t
))
1217 if (size
> *align
) *align
= size
;
1223 if (size
> *align
) *align
= size
;
1227 case RPC_FC_ERROR_STATUS_T
:
1230 if (size
> *align
) *align
= size
;
1235 if (size
> *align
) *align
= size
;
1237 case RPC_FC_INT3264
:
1238 case RPC_FC_UINT3264
:
1239 assert( pointer_size
);
1240 size
= pointer_size
;
1241 if (size
> *align
) *align
= size
;
1244 error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t
));
1249 switch (get_enum_fc(t
))
1254 if (size
> *align
) *align
= size
;
1257 error("type_memsize: Unknown enum type\n");
1262 size
= fields_memsize(type_struct_get_fields(t
), align
);
1264 case TYPE_ENCAPSULATED_UNION
:
1265 size
= fields_memsize(type_encapsulated_union_get_fields(t
), align
);
1268 size
= union_memsize(type_union_get_cases(t
), align
);
1271 assert( pointer_size
);
1272 size
= pointer_size
;
1273 if (size
> *align
) *align
= size
;
1276 if (!type_array_is_decl_as_ptr(t
))
1278 if (is_conformant_array(t
))
1280 type_memsize(type_array_get_element(t
), align
);
1284 size
= type_array_get_dim(t
) *
1285 type_memsize(type_array_get_element(t
), align
);
1287 else /* declared as a pointer */
1289 assert( pointer_size
);
1290 size
= pointer_size
;
1291 if (size
> *align
) *align
= size
;
1294 case TYPE_INTERFACE
:
1301 /* these types should not be encountered here due to language
1302 * restrictions (interface, void, coclass, module), logical
1303 * restrictions (alias - due to type_get_type call above) or
1304 * checking restrictions (function, bitfield). */
1311 int is_full_pointer_function(const var_t
*func
)
1314 if (type_has_full_pointer(type_function_get_rettype(func
->type
), func
->attrs
, TRUE
))
1316 if (!type_get_function_args(func
->type
))
1318 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
1319 if (type_has_full_pointer( var
->type
, var
->attrs
, TRUE
))
1324 void write_full_pointer_init(FILE *file
, int indent
, const var_t
*func
, int is_server
)
1326 print_file(file
, indent
, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
1327 is_server
? "XLAT_SERVER" : "XLAT_CLIENT");
1328 fprintf(file
, "\n");
1331 void write_full_pointer_free(FILE *file
, int indent
, const var_t
*func
)
1333 print_file(file
, indent
, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
1334 fprintf(file
, "\n");
1337 static unsigned int write_nonsimple_pointer(FILE *file
, const attr_list_t
*attrs
,
1340 unsigned int offset
,
1341 unsigned int *typeformat_offset
)
1343 unsigned int start_offset
= *typeformat_offset
;
1344 short reloff
= offset
- (*typeformat_offset
+ 2);
1345 int in_attr
, out_attr
;
1347 unsigned char flags
= 0;
1349 pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
1351 in_attr
= is_attr(attrs
, ATTR_IN
);
1352 out_attr
= is_attr(attrs
, ATTR_OUT
);
1353 if (!in_attr
&& !out_attr
) in_attr
= 1;
1355 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1356 flags
|= RPC_FC_P_ONSTACK
;
1358 if (is_ptr(type
) && !last_ptr(type
))
1359 flags
|= RPC_FC_P_DEREF
;
1361 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1364 string_of_type(pointer_type
));
1367 if (flags
& RPC_FC_P_ONSTACK
)
1368 fprintf(file
, " [allocated_on_stack]");
1369 if (flags
& RPC_FC_P_DEREF
)
1370 fprintf(file
, " [pointer_deref]");
1371 fprintf(file
, " */\n");
1374 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, offset
);
1375 *typeformat_offset
+= 4;
1377 return start_offset
;
1380 static unsigned int write_simple_pointer(FILE *file
, const attr_list_t
*attrs
, const type_t
*type
, int toplevel_param
)
1383 unsigned char pointer_fc
;
1386 /* for historical reasons, write_simple_pointer also handled string types,
1387 * but no longer does. catch bad uses of the function with this check */
1388 if (is_string_type(attrs
, type
))
1389 error("write_simple_pointer: can't handle type %s which is a string type\n", type
->name
);
1391 pointer_fc
= get_pointer_fc(type
, attrs
, toplevel_param
);
1393 ref
= type_pointer_get_ref(type
);
1394 if (type_get_type(ref
) == TYPE_ENUM
)
1395 fc
= get_enum_fc(ref
);
1397 fc
= get_basic_fc(ref
);
1399 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s [simple_pointer] */\n",
1400 pointer_fc
, RPC_FC_P_SIMPLEPOINTER
, string_of_type(pointer_fc
));
1401 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
1402 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1406 static void print_start_tfs_comment(FILE *file
, type_t
*t
, unsigned int tfsoff
)
1408 print_file(file
, 0, "/* %u (", tfsoff
);
1409 write_type_decl(file
, t
, NULL
);
1410 print_file(file
, 0, ") */\n");
1413 static unsigned int write_pointer_tfs(FILE *file
, const attr_list_t
*attrs
,
1414 type_t
*type
, int toplevel_param
,
1415 unsigned int *typestring_offset
)
1417 unsigned int offset
= *typestring_offset
;
1418 type_t
*ref
= type_pointer_get_ref(type
);
1420 print_start_tfs_comment(file
, type
, offset
);
1421 update_tfsoff(type
, offset
, file
);
1423 if (ref
->typestring_offset
)
1424 write_nonsimple_pointer(file
, attrs
, type
,
1426 type_pointer_get_ref(type
)->typestring_offset
,
1428 else if (type_get_type(ref
) == TYPE_BASIC
||
1429 type_get_type(ref
) == TYPE_ENUM
)
1430 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
,
1436 static int processed(const type_t
*type
)
1438 return type
->typestring_offset
&& !type
->tfswrite
;
1441 static int user_type_has_variable_size(const type_t
*t
)
1445 else if (type_get_type(t
) == TYPE_STRUCT
)
1447 switch (get_struct_fc(t
))
1449 case RPC_FC_PSTRUCT
:
1450 case RPC_FC_CSTRUCT
:
1451 case RPC_FC_CPSTRUCT
:
1452 case RPC_FC_CVSTRUCT
:
1456 /* Note: Since this only applies to user types, we can't have a conformant
1457 array here, and strings should get filed under pointer in this case. */
1461 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1463 unsigned int start
, absoff
, flags
;
1464 unsigned int align
= 0, ualign
= 0;
1465 const char *name
= NULL
;
1466 type_t
*utype
= get_user_type(type
, &name
);
1467 unsigned int usize
= type_memsize(utype
, &ualign
);
1468 unsigned int size
= type_memsize(type
, &align
);
1469 unsigned short funoff
= user_type_offset(name
);
1474 if(user_type_has_variable_size(utype
)) usize
= 0;
1476 if (type_get_type(utype
) == TYPE_BASIC
||
1477 type_get_type(utype
) == TYPE_ENUM
)
1481 if (type_get_type(utype
) == TYPE_ENUM
)
1482 fc
= get_enum_fc(utype
);
1484 fc
= get_basic_fc(utype
);
1487 print_start_tfs_comment(file
, utype
, absoff
);
1488 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1489 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
1494 if (!processed(utype
))
1495 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
1496 absoff
= utype
->typestring_offset
;
1499 if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_RP
)
1501 else if (type_get_type(utype
) == TYPE_POINTER
&& get_pointer_fc(utype
, NULL
, FALSE
) == RPC_FC_UP
)
1507 update_tfsoff(type
, start
, file
);
1508 print_start_tfs_comment(file
, type
, start
);
1509 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
1510 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
1511 flags
| (ualign
- 1), ualign
- 1, flags
);
1512 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
1513 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
1514 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", usize
, usize
);
1516 reloff
= absoff
- *tfsoff
;
1517 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff
, reloff
, absoff
);
1521 static void write_member_type(FILE *file
, const type_t
*cont
,
1522 int cont_is_complex
, const attr_list_t
*attrs
,
1523 const type_t
*type
, unsigned int *corroff
,
1524 unsigned int *tfsoff
)
1526 if (is_embedded_complex(type
) && !is_conformant_array(type
))
1528 unsigned int absoff
;
1531 if (type_get_type(type
) == TYPE_UNION
&& is_attr(attrs
, ATTR_SWITCHIS
))
1538 absoff
= type
->typestring_offset
;
1540 reloff
= absoff
- (*tfsoff
+ 2);
1542 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1543 /* FIXME: actually compute necessary padding */
1544 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1545 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1546 reloff
, reloff
, absoff
);
1549 else if (is_ptr(type
) || is_conformant_array(type
))
1551 unsigned char fc
= cont_is_complex
? RPC_FC_POINTER
: RPC_FC_LONG
;
1552 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1555 else if (!write_base_type(file
, type
, TRUE
, tfsoff
))
1556 error("Unsupported member type %d\n", type_get_type(type
));
1559 static void write_array_element_type(FILE *file
, const type_t
*type
,
1560 int cont_is_complex
, unsigned int *tfsoff
)
1562 type_t
*elem
= type_array_get_element(type
);
1564 if (!is_embedded_complex(elem
) && is_ptr(elem
))
1566 type_t
*ref
= type_pointer_get_ref(elem
);
1570 write_nonsimple_pointer(file
, NULL
, elem
, FALSE
, ref
->typestring_offset
, tfsoff
);
1573 if (!is_string_type(NULL
, elem
) &&
1574 (type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
))
1576 *tfsoff
+= write_simple_pointer(file
, NULL
, elem
, FALSE
);
1580 return write_member_type(file
, type
, cont_is_complex
, NULL
, elem
, NULL
, tfsoff
);
1583 static void write_end(FILE *file
, unsigned int *tfsoff
)
1585 if (*tfsoff
% 2 == 0)
1587 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1590 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1594 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1596 unsigned int offset
= 0;
1597 var_list_t
*fs
= type_struct_get_fields(type
);
1600 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
1602 type_t
*ft
= f
->type
;
1603 unsigned int size
= field_memsize( ft
, &offset
);
1604 if (type_get_type(ft
) == TYPE_UNION
&& is_attr(f
->attrs
, ATTR_SWITCHIS
))
1607 unsigned int absoff
= ft
->typestring_offset
;
1608 if (is_attr(ft
->attrs
, ATTR_SWITCHTYPE
))
1609 absoff
+= 8; /* we already have a corr descr, skip it */
1610 reloff
= absoff
- (*tfsoff
+ 6);
1611 print_file(file
, 0, "/* %d */\n", *tfsoff
);
1612 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
1613 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
1614 write_conf_or_var_desc(file
, current_structure
, offset
, ft
,
1615 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
1616 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
1617 reloff
, reloff
, absoff
);
1624 static int write_pointer_description_offsets(
1625 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1626 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1627 unsigned int *typestring_offset
)
1632 if ((is_ptr(type
) && type_get_type(type_pointer_get_ref(type
)) != TYPE_INTERFACE
) ||
1633 (is_array(type
) && type_array_is_decl_as_ptr(type
)))
1635 if (offset_in_memory
&& offset_in_buffer
)
1637 unsigned int memsize
;
1639 /* pointer instance */
1640 /* FIXME: sometimes from end of structure, sometimes from beginning */
1641 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1642 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1645 memsize
= type_memsize(type
, &align
);
1646 *offset_in_memory
+= memsize
;
1647 /* increment these separately as in the case of conformant (varying)
1648 * structures these start at different values */
1649 *offset_in_buffer
+= memsize
;
1651 *typestring_offset
+= 4;
1655 type_t
*ref
= type_pointer_get_ref(type
);
1657 if (is_string_type(attrs
, type
))
1658 write_string_tfs(file
, attrs
, type
, FALSE
, NULL
, typestring_offset
);
1659 else if (processed(ref
))
1660 write_nonsimple_pointer(file
, attrs
, type
, FALSE
, ref
->typestring_offset
, typestring_offset
);
1661 else if (type_get_type(ref
) == TYPE_BASIC
|| type_get_type(ref
) == TYPE_ENUM
)
1662 *typestring_offset
+= write_simple_pointer(file
, attrs
, type
, FALSE
);
1664 error("write_pointer_description_offsets: type format string unknown\n");
1668 unsigned int offset
= type
->typestring_offset
;
1669 /* skip over the pointer that is written for strings, since a
1670 * pointer has to be written in-place here */
1671 if (is_string_type(attrs
, type
))
1673 write_nonsimple_pointer(file
, attrs
, type
, FALSE
, offset
, typestring_offset
);
1681 return write_pointer_description_offsets(
1682 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1683 offset_in_buffer
, typestring_offset
);
1685 else if (is_non_complex_struct(type
))
1687 /* otherwise search for interesting fields to parse */
1689 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1691 if (offset_in_memory
&& offset_in_buffer
)
1693 unsigned int padding
;
1695 type_memsize(v
->type
, &align
);
1696 padding
= ROUNDING(*offset_in_memory
, align
);
1697 *offset_in_memory
+= padding
;
1698 *offset_in_buffer
+= padding
;
1700 written
+= write_pointer_description_offsets(
1701 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1707 if (offset_in_memory
&& offset_in_buffer
)
1709 unsigned int memsize
;
1711 memsize
= type_memsize(type
, &align
);
1712 *offset_in_memory
+= memsize
;
1713 /* increment these separately as in the case of conformant (varying)
1714 * structures these start at different values */
1715 *offset_in_buffer
+= memsize
;
1722 static int write_no_repeat_pointer_descriptions(
1723 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1724 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1725 unsigned int *typestring_offset
)
1731 (is_conformant_array(type
) && type_array_is_decl_as_ptr(type
)))
1733 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1734 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1735 *typestring_offset
+= 2;
1737 return write_pointer_description_offsets(file
, attrs
, type
,
1738 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1741 if (is_non_complex_struct(type
))
1744 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1746 if (offset_in_memory
&& offset_in_buffer
)
1748 unsigned int padding
;
1750 type_memsize(v
->type
, &align
);
1751 padding
= ROUNDING(*offset_in_memory
, align
);
1752 *offset_in_memory
+= padding
;
1753 *offset_in_buffer
+= padding
;
1755 written
+= write_no_repeat_pointer_descriptions(
1756 file
, v
->attrs
, v
->type
,
1757 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1762 unsigned int memsize
;
1764 memsize
= type_memsize(type
, &align
);
1765 *offset_in_memory
+= memsize
;
1766 /* increment these separately as in the case of conformant (varying)
1767 * structures these start at different values */
1768 *offset_in_buffer
+= memsize
;
1774 /* Note: if file is NULL return value is number of pointers to write, else
1775 * it is the number of type format characters written */
1776 static int write_fixed_array_pointer_descriptions(
1777 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1778 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1779 unsigned int *typestring_offset
)
1782 int pointer_count
= 0;
1784 if (type_get_type(type
) == TYPE_ARRAY
&&
1785 !type_array_has_conformance(type
) && !type_array_has_variance(type
))
1787 unsigned int temp
= 0;
1788 /* unfortunately, this needs to be done in two passes to avoid
1789 * writing out redundant FC_FIXED_REPEAT descriptions */
1790 pointer_count
= write_pointer_description_offsets(
1791 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1792 if (pointer_count
> 0)
1794 unsigned int increment_size
;
1795 unsigned int offset_of_array_pointer_mem
= 0;
1796 unsigned int offset_of_array_pointer_buf
= 0;
1799 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1801 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1802 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1803 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", type_array_get_dim(type
), type_array_get_dim(type
));
1804 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1805 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1806 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1807 *typestring_offset
+= 10;
1809 pointer_count
= write_pointer_description_offsets(
1810 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1811 &offset_of_array_pointer_buf
, typestring_offset
);
1814 else if (type_get_type(type
) == TYPE_STRUCT
)
1817 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1819 if (offset_in_memory
&& offset_in_buffer
)
1821 unsigned int padding
;
1823 type_memsize(v
->type
, &align
);
1824 padding
= ROUNDING(*offset_in_memory
, align
);
1825 *offset_in_memory
+= padding
;
1826 *offset_in_buffer
+= padding
;
1828 pointer_count
+= write_fixed_array_pointer_descriptions(
1829 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1835 if (offset_in_memory
&& offset_in_buffer
)
1837 unsigned int memsize
;
1839 memsize
= type_memsize(type
, &align
);
1840 *offset_in_memory
+= memsize
;
1841 /* increment these separately as in the case of conformant (varying)
1842 * structures these start at different values */
1843 *offset_in_buffer
+= memsize
;
1847 return pointer_count
;
1850 /* Note: if file is NULL return value is number of pointers to write, else
1851 * it is the number of type format characters written */
1852 static int write_conformant_array_pointer_descriptions(
1853 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1854 unsigned int offset_in_memory
, unsigned int *typestring_offset
)
1857 int pointer_count
= 0;
1859 if (is_conformant_array(type
) && !type_array_has_variance(type
))
1861 unsigned int temp
= 0;
1862 /* unfortunately, this needs to be done in two passes to avoid
1863 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1864 pointer_count
= write_pointer_description_offsets(
1865 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1866 if (pointer_count
> 0)
1868 unsigned int increment_size
;
1869 unsigned int offset_of_array_pointer_mem
= offset_in_memory
;
1870 unsigned int offset_of_array_pointer_buf
= offset_in_memory
;
1873 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1875 if (increment_size
> USHRT_MAX
)
1876 error("array size of %u bytes is too large\n", increment_size
);
1878 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1879 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1880 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1881 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", offset_in_memory
, offset_in_memory
);
1882 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1883 *typestring_offset
+= 8;
1885 pointer_count
= write_pointer_description_offsets(
1886 file
, attrs
, type_array_get_element(type
),
1887 &offset_of_array_pointer_mem
, &offset_of_array_pointer_buf
,
1892 return pointer_count
;
1895 /* Note: if file is NULL return value is number of pointers to write, else
1896 * it is the number of type format characters written */
1897 static int write_varying_array_pointer_descriptions(
1898 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1899 unsigned int *offset_in_memory
, unsigned int *offset_in_buffer
,
1900 unsigned int *typestring_offset
)
1903 int pointer_count
= 0;
1905 if (is_array(type
) && type_array_has_variance(type
))
1907 unsigned int temp
= 0;
1908 /* unfortunately, this needs to be done in two passes to avoid
1909 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1910 pointer_count
= write_pointer_description_offsets(
1911 NULL
, attrs
, type_array_get_element(type
), NULL
, NULL
, &temp
);
1912 if (pointer_count
> 0)
1914 unsigned int increment_size
;
1917 increment_size
= type_memsize(type_array_get_element(type
), &align
);
1919 if (increment_size
> USHRT_MAX
)
1920 error("array size of %u bytes is too large\n", increment_size
);
1922 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1923 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1924 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", increment_size
, increment_size
);
1925 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1926 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1927 *typestring_offset
+= 8;
1929 pointer_count
= write_pointer_description_offsets(
1930 file
, attrs
, type_array_get_element(type
), offset_in_memory
,
1931 offset_in_buffer
, typestring_offset
);
1934 else if (type_get_type(type
) == TYPE_STRUCT
)
1937 LIST_FOR_EACH_ENTRY( v
, type_struct_get_fields(type
), const var_t
, entry
)
1939 if (offset_in_memory
&& offset_in_buffer
)
1941 unsigned int padding
;
1943 if (is_array(v
->type
) && type_array_has_variance(v
->type
))
1945 *offset_in_buffer
= ROUND_SIZE(*offset_in_buffer
, 4);
1946 /* skip over variance and offset in buffer */
1947 *offset_in_buffer
+= 8;
1951 type_memsize(v
->type
, &align
);
1952 padding
= ROUNDING(*offset_in_memory
, align
);
1953 *offset_in_memory
+= padding
;
1954 *offset_in_buffer
+= padding
;
1956 pointer_count
+= write_varying_array_pointer_descriptions(
1957 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1963 if (offset_in_memory
&& offset_in_buffer
)
1965 unsigned int memsize
;
1967 memsize
= type_memsize(type
, &align
);
1968 *offset_in_memory
+= memsize
;
1969 /* increment these separately as in the case of conformant (varying)
1970 * structures these start at different values */
1971 *offset_in_buffer
+= memsize
;
1975 return pointer_count
;
1978 static void write_pointer_description(FILE *file
, type_t
*type
,
1979 unsigned int *typestring_offset
)
1981 unsigned int offset_in_buffer
;
1982 unsigned int offset_in_memory
;
1984 /* pass 1: search for single instance of a pointer (i.e. don't descend
1986 if (!is_array(type
))
1988 offset_in_memory
= 0;
1989 offset_in_buffer
= 0;
1990 write_no_repeat_pointer_descriptions(
1992 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1995 /* pass 2: search for pointers in fixed arrays */
1996 offset_in_memory
= 0;
1997 offset_in_buffer
= 0;
1998 write_fixed_array_pointer_descriptions(
2000 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
2002 /* pass 3: search for pointers in conformant only arrays (but don't descend
2003 * into conformant varying or varying arrays) */
2004 if (is_conformant_array(type
) &&
2005 (type_array_is_decl_as_ptr(type
) || !current_structure
))
2006 write_conformant_array_pointer_descriptions(
2007 file
, NULL
, type
, 0, typestring_offset
);
2008 else if (type_get_type(type
) == TYPE_STRUCT
&&
2009 get_struct_fc(type
) == RPC_FC_CPSTRUCT
)
2011 unsigned int align
= 0;
2012 type_t
*carray
= find_array_or_string_in_struct(type
)->type
;
2013 write_conformant_array_pointer_descriptions(
2015 type_memsize(type
, &align
),
2019 /* pass 4: search for pointers in varying arrays */
2020 offset_in_memory
= 0;
2021 offset_in_buffer
= 0;
2022 write_varying_array_pointer_descriptions(
2024 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
2027 int is_declptr(const type_t
*t
)
2029 return is_ptr(t
) || (type_get_type(t
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(t
));
2032 static unsigned int write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
2033 type_t
*type
, int toplevel_param
,
2034 const char *name
, unsigned int *typestring_offset
)
2036 unsigned int start_offset
;
2037 unsigned char rtype
;
2040 start_offset
= *typestring_offset
;
2041 update_tfsoff(type
, start_offset
, file
);
2043 if (is_declptr(type
))
2045 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
2046 int pointer_type
= get_pointer_fc(type
, attrs
, toplevel_param
);
2048 pointer_type
= RPC_FC_RP
;
2049 print_start_tfs_comment(file
, type
, *typestring_offset
);
2050 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
2051 pointer_type
, flag
, string_of_type(pointer_type
),
2052 flag
? " [simple_pointer]" : "");
2053 *typestring_offset
+= 2;
2056 print_file(file
, 2, "NdrFcShort(0x2),\n");
2057 *typestring_offset
+= 2;
2062 elem_type
= type_array_get_element(type
);
2064 elem_type
= type_pointer_get_ref(type
);
2066 if (type_get_type(elem_type
) != TYPE_BASIC
)
2068 error("write_string_tfs: Unimplemented for non-basic type %s\n", name
);
2069 return start_offset
;
2072 rtype
= get_basic_fc(elem_type
);
2073 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
2075 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
2076 return start_offset
;
2079 if (type_get_type(type
) == TYPE_ARRAY
&& !type_array_has_conformance(type
))
2081 unsigned int dim
= type_array_get_dim(type
);
2083 /* FIXME: multi-dimensional array */
2085 error("array size for parameter %s exceeds %u bytes by %u bytes\n",
2086 name
, 0xffffu
, dim
- 0xffffu
);
2088 if (rtype
== RPC_FC_WCHAR
)
2089 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
2091 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
2092 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2093 *typestring_offset
+= 2;
2095 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", dim
, dim
);
2096 *typestring_offset
+= 2;
2098 return start_offset
;
2100 else if (is_conformant_array(type
))
2102 unsigned int align
= 0;
2104 if (rtype
== RPC_FC_WCHAR
)
2105 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2107 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2108 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
2109 *typestring_offset
+= 2;
2111 *typestring_offset
+= write_conf_or_var_desc(
2112 file
, current_structure
,
2113 (!type_array_is_decl_as_ptr(type
) && current_structure
2114 ? type_memsize(current_structure
, &align
)
2116 type
, type_array_get_conformance(type
));
2118 return start_offset
;
2122 if (rtype
== RPC_FC_WCHAR
)
2123 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
2125 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
2126 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2127 *typestring_offset
+= 2;
2129 return start_offset
;
2133 static unsigned int write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2134 const char *name
, unsigned int *typestring_offset
)
2136 const expr_t
*length_is
= type_array_get_variance(type
);
2137 const expr_t
*size_is
= type_array_get_conformance(type
);
2138 unsigned int align
= 0;
2140 unsigned int start_offset
;
2143 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
2144 unsigned int baseoff
2145 = !type_array_is_decl_as_ptr(type
) && current_structure
2146 ? type_memsize(current_structure
, &align
)
2150 pointer_type
= RPC_FC_RP
;
2152 if (write_embedded_types(file
, attrs
, type_array_get_element(type
), name
, FALSE
, typestring_offset
))
2155 has_pointer
= type_has_pointers(type_array_get_element(type
));
2158 size
= type_memsize((is_conformant_array(type
) ? type_array_get_element(type
) : type
), &align
);
2159 fc
= get_array_fc(type
);
2161 start_offset
= *typestring_offset
;
2162 update_tfsoff(type
, start_offset
, file
);
2163 print_start_tfs_comment(file
, type
, start_offset
);
2164 print_file(file
, 2, "0x%02x,\t/* %s */\n", fc
, string_of_type(fc
));
2165 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2166 *typestring_offset
+= 2;
2169 if (fc
!= RPC_FC_BOGUS_ARRAY
)
2171 if (fc
== RPC_FC_LGFARRAY
|| fc
== RPC_FC_LGVARRAY
)
2173 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", size
, size
);
2174 *typestring_offset
+= 4;
2178 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", size
, size
);
2179 *typestring_offset
+= 2;
2182 if (is_conformant_array(type
))
2184 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2187 if (fc
== RPC_FC_SMVARRAY
|| fc
== RPC_FC_LGVARRAY
)
2189 unsigned int elalign
= 0;
2190 unsigned int elsize
= type_memsize(type_array_get_element(type
), &elalign
);
2191 unsigned int dim
= type_array_get_dim(type
);
2193 if (fc
== RPC_FC_LGVARRAY
)
2195 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim
, dim
);
2196 *typestring_offset
+= 4;
2200 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2201 *typestring_offset
+= 2;
2204 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", elsize
, elsize
);
2205 *typestring_offset
+= 2;
2210 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2213 if (has_pointer
&& (type_array_is_decl_as_ptr(type
) || !current_structure
))
2215 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2216 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2217 *typestring_offset
+= 2;
2218 write_pointer_description(file
, type
, typestring_offset
);
2219 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2220 *typestring_offset
+= 1;
2223 write_array_element_type(file
, type
, FALSE
, typestring_offset
);
2224 write_end(file
, typestring_offset
);
2228 unsigned int dim
= size_is
? 0 : type_array_get_dim(type
);
2229 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %u */\n", dim
, dim
);
2230 *typestring_offset
+= 2;
2232 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2235 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
2238 write_array_element_type(file
, type
, TRUE
, typestring_offset
);
2239 write_end(file
, typestring_offset
);
2242 return start_offset
;
2245 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
2247 const var_list_t
*fields
= type_struct_get_fields(type
);
2248 const var_t
*last_field
;
2251 if (!fields
|| list_empty(fields
))
2254 last_field
= LIST_ENTRY( list_tail(fields
), const var_t
, entry
);
2255 ft
= last_field
->type
;
2257 if (is_conformant_array(ft
) && !type_array_is_decl_as_ptr(ft
))
2260 if (type_get_type(ft
) == TYPE_STRUCT
)
2261 return find_array_or_string_in_struct(ft
);
2266 static void write_struct_members(FILE *file
, const type_t
*type
,
2267 int is_complex
, unsigned int *corroff
,
2268 unsigned int *typestring_offset
)
2271 unsigned short offset
= 0;
2272 unsigned int salign
= 1;
2274 var_list_t
*fields
= type_struct_get_fields(type
);
2276 if (fields
) LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2278 type_t
*ft
= field
->type
;
2279 unsigned int align
= 0;
2280 unsigned int size
= type_memsize(ft
, &align
);
2281 align
= clamp_align(align
);
2282 if (salign
< align
) salign
= align
;
2284 if (!is_conformant_array(ft
) || type_array_is_decl_as_ptr(ft
))
2286 if ((align
- 1) & offset
)
2288 unsigned char fc
= 0;
2292 fc
= RPC_FC_ALIGNM2
;
2295 fc
= RPC_FC_ALIGNM4
;
2298 fc
= RPC_FC_ALIGNM8
;
2301 error("write_struct_members: cannot align type %d\n", type_get_type(ft
));
2303 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2304 offset
= ROUND_SIZE(offset
, align
);
2305 *typestring_offset
+= 1;
2307 write_member_type(file
, type
, is_complex
, field
->attrs
, field
->type
, corroff
,
2313 padding
= ROUNDING(offset
, salign
);
2316 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
2317 RPC_FC_STRUCTPAD1
+ padding
- 1,
2319 *typestring_offset
+= 1;
2322 write_end(file
, typestring_offset
);
2325 static unsigned int write_struct_tfs(FILE *file
, type_t
*type
,
2326 const char *name
, unsigned int *tfsoff
)
2328 const type_t
*save_current_structure
= current_structure
;
2329 unsigned int total_size
;
2331 unsigned int start_offset
;
2332 unsigned int array_offset
;
2333 int has_pointers
= 0;
2334 unsigned int align
= 0;
2335 unsigned int corroff
;
2337 unsigned char fc
= get_struct_fc(type
);
2338 var_list_t
*fields
= type_struct_get_fields(type
);
2341 current_structure
= type
;
2343 total_size
= type_memsize(type
, &align
);
2344 if (total_size
> USHRT_MAX
)
2345 error("structure size for %s exceeds %d bytes by %d bytes\n",
2346 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
2348 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2349 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
2351 if (!has_pointers
) has_pointers
= type_has_pointers(type
);
2353 array
= find_array_or_string_in_struct(type
);
2354 if (array
&& !processed(array
->type
))
2356 = is_string_type(array
->attrs
, array
->type
)
2357 ? write_string_tfs(file
, array
->attrs
, array
->type
, FALSE
, array
->name
, tfsoff
)
2358 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
2361 write_descriptors(file
, type
, tfsoff
);
2363 start_offset
= *tfsoff
;
2364 update_tfsoff(type
, start_offset
, file
);
2365 print_start_tfs_comment(file
, type
, start_offset
);
2366 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2367 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
2368 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", total_size
, total_size
);
2373 unsigned int absoff
= array
->type
->typestring_offset
;
2374 short reloff
= absoff
- *tfsoff
;
2375 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2376 reloff
, reloff
, absoff
);
2379 else if (fc
== RPC_FC_BOGUS_STRUCT
)
2381 print_file(file
, 2, "NdrFcShort(0x0),\n");
2385 if (fc
== RPC_FC_BOGUS_STRUCT
)
2387 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
2388 nothing is written to file yet. On the actual writing pass,
2389 this will have been updated. */
2390 unsigned int absoff
= type
->ptrdesc
? type
->ptrdesc
: *tfsoff
;
2391 int reloff
= absoff
- *tfsoff
;
2392 assert( reloff
>= 0 );
2393 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
2394 reloff
, reloff
, absoff
);
2397 else if ((fc
== RPC_FC_PSTRUCT
) ||
2398 (fc
== RPC_FC_CPSTRUCT
) ||
2399 (fc
== RPC_FC_CVSTRUCT
&& has_pointers
))
2401 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
2402 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
2404 write_pointer_description(file
, type
, tfsoff
);
2405 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
2409 write_struct_members(file
, type
, fc
== RPC_FC_BOGUS_STRUCT
, &corroff
,
2412 if (fc
== RPC_FC_BOGUS_STRUCT
)
2416 type
->ptrdesc
= *tfsoff
;
2417 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
2419 type_t
*ft
= f
->type
;
2422 if (is_string_type(f
->attrs
, ft
))
2423 write_string_tfs(file
, f
->attrs
, ft
, FALSE
, f
->name
, tfsoff
);
2425 write_pointer_tfs(file
, f
->attrs
, ft
, FALSE
, tfsoff
);
2427 else if (type_get_type(ft
) == TYPE_ARRAY
&& type_array_is_decl_as_ptr(ft
))
2429 unsigned int offset
;
2431 print_file(file
, 0, "/* %d */\n", *tfsoff
);
2433 offset
= ft
->typestring_offset
;
2434 /* skip over the pointer that is written for strings, since a
2435 * pointer has to be written in-place here */
2436 if (is_string_type(f
->attrs
, ft
))
2438 write_nonsimple_pointer(file
, f
->attrs
, ft
, FALSE
, offset
, tfsoff
);
2441 if (type
->ptrdesc
== *tfsoff
)
2445 current_structure
= save_current_structure
;
2446 return start_offset
;
2449 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
2453 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
2457 if (type_get_type(t
) == TYPE_BASIC
|| type_get_type(t
) == TYPE_ENUM
)
2460 if (type_get_type(t
) == TYPE_BASIC
)
2461 fc
= get_basic_fc(t
);
2463 fc
= get_enum_fc(t
);
2464 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
2465 fc
, string_of_type(fc
));
2467 else if (t
->typestring_offset
)
2469 short reloff
= t
->typestring_offset
- *tfsoff
;
2470 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
2471 reloff
, reloff
, t
->typestring_offset
);
2474 error("write_branch_type: type unimplemented %d\n", type_get_type(t
));
2480 static unsigned int write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
2483 unsigned int start_offset
;
2486 unsigned int nbranch
= 0;
2487 type_t
*deftype
= NULL
;
2488 short nodeftype
= 0xffff;
2494 size
= type_memsize(type
, &align
);
2496 fields
= type_union_get_cases(type
);
2498 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2500 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2502 nbranch
+= list_count(cases
);
2504 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
2507 start_offset
= *tfsoff
;
2508 update_tfsoff(type
, start_offset
, file
);
2509 print_start_tfs_comment(file
, type
, start_offset
);
2510 if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
2512 const var_t
*sv
= type_union_get_switch_value(type
);
2513 const type_t
*st
= sv
->type
;
2516 if (type_get_type(st
) == TYPE_BASIC
)
2518 switch (get_basic_fc(st
))
2529 fc
= get_basic_fc(st
);
2533 error("union switch type must be an integer, char, or enum\n");
2536 else if (type_get_type(st
) == TYPE_ENUM
)
2537 fc
= get_enum_fc(st
);
2539 error("union switch type must be an integer, char, or enum\n");
2541 print_file(file
, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", RPC_FC_ENCAPSULATED_UNION
);
2542 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2543 0x40 | fc
, string_of_type(fc
));
2546 else if (is_attr(type
->attrs
, ATTR_SWITCHTYPE
))
2548 static const expr_t dummy_expr
; /* FIXME */
2549 const type_t
*st
= get_attrp(type
->attrs
, ATTR_SWITCHTYPE
);
2552 if (type_get_type(st
) == TYPE_BASIC
)
2554 switch (get_basic_fc(st
))
2565 fc
= get_basic_fc(st
);
2569 error("union switch type must be an integer, char, or enum\n");
2572 else if (type_get_type(st
) == TYPE_ENUM
)
2573 fc
= get_enum_fc(st
);
2575 error("union switch type must be an integer, char, or enum\n");
2577 print_file(file
, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", RPC_FC_NON_ENCAPSULATED_UNION
);
2578 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
2579 fc
, string_of_type(fc
));
2582 *tfsoff
+= write_conf_or_var_desc(file
, NULL
, *tfsoff
, st
, &dummy_expr
);
2583 print_file(file
, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff
+ 2);
2585 print_file(file
, 0, "/* %u */\n", *tfsoff
);
2588 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", size
, size
);
2589 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %d */\n", nbranch
, nbranch
);
2592 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
2594 type_t
*ft
= f
->type
;
2595 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
2596 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
2599 if (cases
== NULL
&& !deflt
)
2600 error("union field %s with neither case nor default attribute\n", f
->name
);
2602 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
2604 /* MIDL doesn't check for duplicate cases, even though that seems
2605 like a reasonable thing to do, it just dumps them to the TFS
2606 like we're going to do here. */
2607 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %ld */\n", c
->cval
, c
->cval
);
2609 write_branch_type(file
, ft
, tfsoff
);
2612 /* MIDL allows multiple default branches, even though that seems
2613 illogical, it just chooses the last one, which is what we will
2624 write_branch_type(file
, deftype
, tfsoff
);
2628 print_file(file
, 2, "NdrFcShort(0x%hx),\n", nodeftype
);
2632 return start_offset
;
2635 static unsigned int write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2636 unsigned int *typeformat_offset
)
2639 unsigned int start_offset
= *typeformat_offset
;
2640 expr_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
2644 print_file(file
, 2, "0x2f, /* FC_IP */\n");
2645 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
2647 += write_conf_or_var_desc(file
, NULL
, 0, type
, iid
) + 2;
2651 const type_t
*base
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
2652 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
2655 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
2657 update_tfsoff(type
, start_offset
, file
);
2658 print_start_tfs_comment(file
, type
, start_offset
);
2659 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
2660 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
2661 print_file(file
, 2, "NdrFcLong(0x%08x),\n", uuid
->Data1
);
2662 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
2663 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
2664 for (i
= 0; i
< 8; ++i
)
2665 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
2668 fprintf(file
, "\n");
2670 *typeformat_offset
+= 18;
2672 return start_offset
;
2675 static unsigned int write_contexthandle_tfs(FILE *file
, const type_t
*type
,
2677 unsigned int *typeformat_offset
)
2679 unsigned int start_offset
= *typeformat_offset
;
2680 unsigned char flags
= 0;
2682 if (is_attr(current_iface
->attrs
, ATTR_STRICTCONTEXTHANDLE
))
2683 flags
|= NDR_STRICT_CONTEXT_HANDLE
;
2687 if (is_attr(var
->attrs
, ATTR_IN
))
2690 if (!is_attr(var
->attrs
, ATTR_OUT
))
2691 flags
|= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
;
2693 if (is_attr(var
->attrs
, ATTR_OUT
))
2696 WRITE_FCTYPE(file
, FC_BIND_CONTEXT
, *typeformat_offset
);
2697 print_file(file
, 2, "0x%x,\t/* Context flags: ", flags
);
2698 /* return and can't be null values overlap */
2699 if (((flags
& 0x21) != 0x21) && (flags
& NDR_CONTEXT_HANDLE_CANNOT_BE_NULL
))
2700 print_file(file
, 0, "can't be null, ");
2701 if (flags
& NDR_CONTEXT_HANDLE_SERIALIZE
)
2702 print_file(file
, 0, "serialize, ");
2703 if (flags
& NDR_CONTEXT_HANDLE_NO_SERIALIZE
)
2704 print_file(file
, 0, "no serialize, ");
2705 if (flags
& NDR_STRICT_CONTEXT_HANDLE
)
2706 print_file(file
, 0, "strict, ");
2707 if ((flags
& 0x21) == 0x20)
2708 print_file(file
, 0, "out, ");
2709 if ((flags
& 0x21) == 0x21)
2710 print_file(file
, 0, "return, ");
2712 print_file(file
, 0, "in, ");
2714 print_file(file
, 0, "via ptr, ");
2715 print_file(file
, 0, "*/\n");
2716 print_file(file
, 2, "0, /* FIXME: rundown routine index*/\n");
2717 print_file(file
, 2, "0, /* FIXME: param num */\n");
2718 *typeformat_offset
+= 4;
2720 return start_offset
;
2723 static unsigned int write_range_tfs(FILE *file
, const attr_list_t
*attrs
,
2724 type_t
*type
, expr_list_t
*range_list
,
2725 unsigned int *typeformat_offset
)
2728 unsigned int start_offset
= *typeformat_offset
;
2729 const expr_t
*range_min
= LIST_ENTRY(list_head(range_list
), const expr_t
, entry
);
2730 const expr_t
*range_max
= LIST_ENTRY(list_next(range_list
, list_head(range_list
)), const expr_t
, entry
);
2732 if (type_get_type(type
) == TYPE_BASIC
)
2733 fc
= get_basic_fc(type
);
2735 fc
= get_enum_fc(type
);
2737 /* fc must fit in lower 4-bits of 8-bit field below */
2740 print_file(file
, 0, "/* %u */\n", *typeformat_offset
);
2741 print_file(file
, 2, "0x%x,\t/* FC_RANGE */\n", RPC_FC_RANGE
);
2742 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
2743 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_min
->cval
, range_min
->cval
);
2744 print_file(file
, 2, "NdrFcLong(0x%lx),\t/* %lu */\n", range_max
->cval
, range_max
->cval
);
2745 *typeformat_offset
+= 10;
2747 return start_offset
;
2750 static unsigned int write_typeformatstring_var(FILE *file
, int indent
, const var_t
*func
,
2751 type_t
*type
, const var_t
*var
,
2753 unsigned int *typeformat_offset
)
2755 unsigned int offset
;
2757 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
2759 case TGT_CTXT_HANDLE
:
2760 case TGT_CTXT_HANDLE_POINTER
:
2761 return write_contexthandle_tfs(file
, type
, var
, typeformat_offset
);
2763 write_user_tfs(file
, type
, typeformat_offset
);
2764 return type
->typestring_offset
;
2766 return write_string_tfs(file
, var
->attrs
, type
, toplevel_param
, var
->name
, typeformat_offset
);
2771 off
= write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
2772 ptr_type
= get_pointer_fc(type
, var
->attrs
, toplevel_param
);
2773 if (ptr_type
!= RPC_FC_RP
)
2775 unsigned int absoff
= type
->typestring_offset
;
2776 short reloff
= absoff
- (*typeformat_offset
+ 2);
2777 off
= *typeformat_offset
;
2778 print_file(file
, 0, "/* %d */\n", off
);
2779 print_file(file
, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type
,
2780 string_of_type(ptr_type
));
2781 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
2782 reloff
, reloff
, absoff
);
2783 *typeformat_offset
+= 4;
2788 if (processed(type
)) return type
->typestring_offset
;
2789 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
2791 if (processed(type
)) return type
->typestring_offset
;
2792 return write_union_tfs(file
, type
, typeformat_offset
);
2799 expr_list_t
*range_list
= get_attrp(var
->attrs
, ATTR_RANGE
);
2801 range_list
= get_aliaschain_attrp(type
, ATTR_RANGE
);
2802 return write_range_tfs(file
, var
->attrs
, type
, range_list
, typeformat_offset
);
2804 case TGT_IFACE_POINTER
:
2805 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2809 size_t start_offset
= *typeformat_offset
;
2810 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2811 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2812 const type_t
*ref
= type_pointer_get_ref(type
);
2814 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
2816 /* special case for pointers to base types */
2822 if (type_get_type(ref
) == TYPE_ENUM
)
2823 fc
= get_enum_fc(ref
);
2825 fc
= get_basic_fc(ref
);
2827 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2828 get_pointer_fc(type
, var
->attrs
, toplevel_param
),
2829 (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2830 string_of_type(get_pointer_fc(type
, var
->attrs
, toplevel_param
)),
2831 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2832 print_file(file
, indent
, "0x%02x, /* %s */\n",
2833 fc
, string_of_type(fc
));
2834 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2835 *typeformat_offset
+= 4;
2836 return start_offset
;
2843 offset
= write_typeformatstring_var(file
, indent
, func
,
2844 type_pointer_get_ref(type
), var
,
2845 FALSE
, typeformat_offset
);
2847 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2848 return write_nonsimple_pointer(file
, var
->attrs
, type
,
2850 offset
, typeformat_offset
);
2854 error("invalid type %s for var %s\n", type
->name
, var
->name
);
2858 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2859 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2863 switch (typegen_detect_type(type
, attrs
, TDT_ALL_TYPES
))
2866 write_user_tfs(file
, type
, tfsoff
);
2869 write_string_tfs(file
, attrs
, type
, FALSE
, name
, tfsoff
);
2871 case TGT_IFACE_POINTER
:
2872 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2876 type_t
*ref
= type_pointer_get_ref(type
);
2878 if (!processed(ref
) && type_get_type(ref
) != TYPE_BASIC
)
2879 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2882 write_pointer_tfs(file
, attrs
, type
, FALSE
, tfsoff
);
2888 /* conformant arrays and strings are handled specially */
2889 if (!is_conformant_array(type
) || type_array_is_decl_as_ptr(type
) )
2891 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2892 if (is_conformant_array(type
))
2897 if (!processed(type
))
2898 write_struct_tfs(file
, type
, name
, tfsoff
);
2901 if (!processed(type
))
2902 write_union_tfs(file
, type
, tfsoff
);
2910 expr_list_t
*range_list
= get_attrp(attrs
, ATTR_RANGE
);
2912 range_list
= get_aliaschain_attrp(type
, ATTR_RANGE
);
2913 write_range_tfs(file
, attrs
, type
, range_list
, tfsoff
);
2916 case TGT_CTXT_HANDLE
:
2917 case TGT_CTXT_HANDLE_POINTER
:
2919 error("invalid type %s for var %s\n", type
->name
, name
);
2926 static unsigned int process_tfs_stmts(FILE *file
, const statement_list_t
*stmts
,
2927 type_pred_t pred
, unsigned int *typeformat_offset
)
2930 const statement_t
*stmt
;
2932 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
2934 const type_t
*iface
;
2935 const statement_t
*stmt_func
;
2937 if (stmt
->type
== STMT_LIBRARY
)
2939 process_tfs_stmts(file
, stmt
->u
.lib
->stmts
, pred
, typeformat_offset
);
2942 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
2945 iface
= stmt
->u
.type
;
2949 current_iface
= iface
;
2950 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
2952 const var_t
*func
= stmt_func
->u
.var
;
2953 if (is_local(func
->attrs
)) continue;
2955 if (!is_void(type_function_get_rettype(func
->type
)))
2958 v
.type
= type_function_get_rettype(func
->type
);
2959 update_tfsoff(type_function_get_rettype(func
->type
),
2960 write_typeformatstring_var(
2962 type_function_get_rettype(func
->type
),
2963 &v
, FALSE
, typeformat_offset
),
2967 current_func
= func
;
2968 if (type_get_function_args(func
->type
))
2969 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
2972 write_typeformatstring_var(
2973 file
, 2, func
, var
->type
, var
,
2974 TRUE
, typeformat_offset
),
2979 return *typeformat_offset
+ 1;
2982 static unsigned int process_tfs(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2984 unsigned int typeformat_offset
= 2;
2986 return process_tfs_stmts(file
, stmts
, pred
, &typeformat_offset
);
2990 void write_typeformatstring(FILE *file
, const statement_list_t
*stmts
, type_pred_t pred
)
2994 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2995 print_file(file
, indent
, "{\n");
2997 print_file(file
, indent
, "0,\n");
2998 print_file(file
, indent
, "{\n");
3000 print_file(file
, indent
, "NdrFcShort(0x0),\n");
3002 set_all_tfswrite(TRUE
);
3003 process_tfs(file
, stmts
, pred
);
3005 print_file(file
, indent
, "0x0\n");
3007 print_file(file
, indent
, "}\n");
3009 print_file(file
, indent
, "};\n");
3010 print_file(file
, indent
, "\n");
3013 static unsigned int get_required_buffer_size_type(
3014 const type_t
*type
, const char *name
, const attr_list_t
*attrs
, int toplevel_param
, unsigned int *alignment
)
3017 switch (typegen_detect_type(type
, NULL
, TDT_IGNORE_STRINGS
|TDT_IGNORE_RANGES
))
3022 const type_t
*utype
= get_user_type(type
, &uname
);
3023 return get_required_buffer_size_type(utype
, uname
, NULL
, FALSE
, alignment
);
3026 switch (get_basic_fc(type
))
3044 case RPC_FC_ERROR_STATUS_T
:
3053 case RPC_FC_INT3264
:
3054 case RPC_FC_UINT3264
:
3055 assert( pointer_size
);
3056 *alignment
= pointer_size
;
3057 return pointer_size
;
3060 case RPC_FC_BIND_PRIMITIVE
:
3064 error("get_required_buffer_size: unknown basic type 0x%02x\n",
3065 get_basic_fc(type
));
3071 switch (get_enum_fc(type
))
3083 if (get_struct_fc(type
) == RPC_FC_STRUCT
)
3085 if (!type_struct_get_fields(type
)) return 0;
3086 return fields_memsize(type_struct_get_fields(type
), alignment
);
3091 if (get_pointer_fc(type
, attrs
, toplevel_param
) == RPC_FC_RP
)
3093 const type_t
*ref
= type_pointer_get_ref(type
);
3094 switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
3099 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
3101 if (get_struct_fc(ref
) == RPC_FC_STRUCT
)
3102 return get_required_buffer_size_type( ref
, name
, NULL
, FALSE
, alignment
);
3105 case TGT_CTXT_HANDLE
:
3106 case TGT_CTXT_HANDLE_POINTER
:
3110 case TGT_IFACE_POINTER
:
3119 /* FIXME: depends on pointer type */
3120 return type_array_get_dim(type
) *
3121 get_required_buffer_size_type(type_array_get_element(type
), name
, NULL
, FALSE
, alignment
);
3129 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
3131 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3132 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3134 if (!in_attr
&& !out_attr
)
3139 if ((pass
== PASS_IN
&& in_attr
) || (pass
== PASS_OUT
&& out_attr
) ||
3140 pass
== PASS_RETURN
)
3142 if (is_ptrchain_attr(var
, ATTR_CONTEXTHANDLE
))
3148 if (!is_string_type(var
->attrs
, var
->type
))
3149 return get_required_buffer_size_type(var
->type
, var
->name
,
3150 var
->attrs
, TRUE
, alignment
);
3155 static unsigned int get_function_buffer_size( const var_t
*func
, enum pass pass
)
3158 unsigned int total_size
= 0, alignment
;
3160 if (type_get_function_args(func
->type
))
3162 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3164 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
3165 total_size
+= alignment
;
3169 if (pass
== PASS_OUT
&& !is_void(type_function_get_rettype(func
->type
)))
3172 v
.type
= type_function_get_rettype(func
->type
);
3173 total_size
+= get_required_buffer_size(&v
, &alignment
, PASS_RETURN
);
3174 total_size
+= alignment
;
3179 static void print_phase_function(FILE *file
, int indent
, const char *type
,
3180 const char *local_var_prefix
, enum remoting_phase phase
,
3181 const var_t
*var
, unsigned int type_offset
)
3183 const char *function
;
3186 case PHASE_BUFFERSIZE
:
3187 function
= "BufferSize";
3190 function
= "Marshall";
3192 case PHASE_UNMARSHAL
:
3193 function
= "Unmarshall";
3203 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
3205 print_file(file
, indent
, "&__frame->_StubMsg,\n");
3206 print_file(file
, indent
, "%s%s%s%s%s,\n",
3207 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
3208 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
3210 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
3212 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
3213 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
3214 if (phase
== PHASE_UNMARSHAL
)
3215 print_file(file
, indent
, "0);\n");
3219 void print_phase_basetype(FILE *file
, int indent
, const char *local_var_prefix
,
3220 enum remoting_phase phase
, enum pass pass
, const var_t
*var
,
3221 const char *varname
)
3223 type_t
*type
= var
->type
;
3225 unsigned int alignment
= 0;
3227 /* no work to do for other phases, buffer sizing is done elsewhere */
3228 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
3231 if (type_get_type(type
) == TYPE_ENUM
||
3232 (type_get_type(type
) == TYPE_BASIC
&&
3233 type_basic_get_type(type
) == TYPE_BASIC_INT3264
&&
3238 if (type_get_type(type
) == TYPE_ENUM
)
3239 fc
= get_enum_fc(type
);
3241 fc
= get_basic_fc(type
);
3243 if (phase
== PHASE_MARSHAL
)
3244 print_file(file
, indent
, "NdrSimpleTypeMarshall(\n");
3246 print_file(file
, indent
, "NdrSimpleTypeUnmarshall(\n");
3247 print_file(file
, indent
+1, "&__frame->_StubMsg,\n");
3248 print_file(file
, indent
+1, "(unsigned char *)&%s%s,\n",
3251 print_file(file
, indent
+1, "0x%02x /* %s */);\n", fc
, string_of_type(fc
));
3255 const type_t
*ref
= is_ptr(type
) ? type_pointer_get_ref(type
) : type
;
3256 switch (get_basic_fc(ref
))
3276 case RPC_FC_ERROR_STATUS_T
:
3277 /* pointer_size must be 4 if we got here in these two cases */
3278 case RPC_FC_INT3264
:
3279 case RPC_FC_UINT3264
:
3291 case RPC_FC_BIND_PRIMITIVE
:
3292 /* no marshalling needed */
3296 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
3297 var
->name
, get_basic_fc(ref
));
3301 if (phase
== PHASE_MARSHAL
&& alignment
> 1)
3302 print_file(file
, indent
, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment
, alignment
- 1);
3303 print_file(file
, indent
, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
3304 alignment
- 1, alignment
- 1);
3306 if (phase
== PHASE_MARSHAL
)
3308 print_file(file
, indent
, "*(");
3309 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3311 fprintf(file
, " *)__frame->_StubMsg.Buffer = *");
3313 fprintf(file
, " *)__frame->_StubMsg.Buffer = ");
3314 fprintf(file
, "%s%s", local_var_prefix
, varname
);
3315 fprintf(file
, ";\n");
3317 else if (phase
== PHASE_UNMARSHAL
)
3319 print_file(file
, indent
, "if (__frame->_StubMsg.Buffer + sizeof(");
3320 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3321 fprintf(file
, ") > __frame->_StubMsg.BufferEnd)\n");
3322 print_file(file
, indent
, "{\n");
3323 print_file(file
, indent
+ 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
3324 print_file(file
, indent
, "}\n");
3325 print_file(file
, indent
, "%s%s%s",
3326 (pass
== PASS_IN
|| pass
== PASS_RETURN
) ? "" : "*",
3327 local_var_prefix
, varname
);
3328 if (pass
== PASS_IN
&& is_ptr(type
))
3329 fprintf(file
, " = (");
3331 fprintf(file
, " = *(");
3332 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3333 fprintf(file
, " *)__frame->_StubMsg.Buffer;\n");
3336 print_file(file
, indent
, "__frame->_StubMsg.Buffer += sizeof(");
3337 write_type_decl(file
, is_ptr(type
) ? type_pointer_get_ref(type
) : type
, NULL
);
3338 fprintf(file
, ");\n");
3342 /* returns whether the MaxCount, Offset or ActualCount members need to be
3343 * filled in for the specified phase */
3344 static inline int is_conformance_needed_for_phase(enum remoting_phase phase
)
3346 return (phase
!= PHASE_UNMARSHAL
);
3349 expr_t
*get_size_is_expr(const type_t
*t
, const char *name
)
3353 for ( ; is_array(t
); t
= type_array_get_element(t
))
3354 if (type_array_has_conformance(t
))
3357 x
= type_array_get_conformance(t
);
3359 error("%s: multidimensional conformant"
3360 " arrays not supported at the top level\n",
3367 static void write_parameter_conf_or_var_exprs(FILE *file
, int indent
, const char *local_var_prefix
,
3368 enum remoting_phase phase
, const var_t
*var
)
3370 const type_t
*type
= var
->type
;
3371 /* get fundamental type for the argument */
3374 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
3376 else if (is_attr(type
->attrs
, ATTR_CONTEXTHANDLE
))
3378 else if (type_is_alias(type
))
3379 type
= type_alias_get_aliasee(type
);
3380 else if (is_array(type
))
3382 if (is_conformance_needed_for_phase(phase
) && is_array(type
))
3384 if (type_array_has_conformance(type
))
3386 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3387 write_expr(file
, type_array_get_conformance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3388 fprintf(file
, ";\n\n");
3390 if (type_array_has_variance(type
))
3392 print_file(file
, indent
, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
3393 print_file(file
, indent
, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
3394 write_expr(file
, type_array_get_variance(type
), 1, 1, NULL
, NULL
, local_var_prefix
);
3395 fprintf(file
, ";\n\n");
3400 else if (type_get_type(type
) == TYPE_UNION
)
3402 if (is_conformance_needed_for_phase(phase
))
3404 print_file(file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
3405 write_expr(file
, get_attrp(var
->attrs
, ATTR_SWITCHIS
), 1, 1, NULL
, NULL
, local_var_prefix
);
3406 fprintf(file
, ";\n\n");
3410 else if (type_get_type(type
) == TYPE_INTERFACE
|| is_void(type
))
3414 if (is_conformance_needed_for_phase(phase
) && (iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
3416 print_file( file
, indent
, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
3417 write_expr( file
, iid
, 1, 1, NULL
, NULL
, local_var_prefix
);
3418 fprintf( file
, ";\n\n" );
3422 else if (is_ptr(type
))
3423 type
= type_pointer_get_ref(type
);
3429 static void write_remoting_arg(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3430 enum pass pass
, enum remoting_phase phase
, const var_t
*var
)
3432 int in_attr
, out_attr
, pointer_type
;
3433 const type_t
*type
= var
->type
;
3434 unsigned int start_offset
= type
->typestring_offset
;
3436 if (is_ptr(type
) || is_array(type
))
3437 pointer_type
= get_pointer_fc(type
, var
->attrs
, pass
!= PASS_RETURN
);
3441 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3442 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3443 if (!in_attr
&& !out_attr
)
3446 if (phase
!= PHASE_FREE
)
3450 if (!in_attr
) return;
3453 if (!out_attr
) return;
3459 write_parameter_conf_or_var_exprs(file
, indent
, local_var_prefix
, phase
, var
);
3461 switch (typegen_detect_type(type
, var
->attrs
, TDT_ALL_TYPES
))
3463 case TGT_CTXT_HANDLE
:
3464 case TGT_CTXT_HANDLE_POINTER
:
3465 if (phase
== PHASE_MARSHAL
)
3467 if (pass
== PASS_IN
)
3469 /* if the context_handle attribute appears in the chain of types
3470 * without pointers being followed, then the context handle must
3471 * be direct, otherwise it is a pointer */
3472 int is_ch_ptr
= is_aliaschain_attr(type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
3473 print_file(file
, indent
, "NdrClientContextMarshall(\n");
3474 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3475 print_file(file
, indent
+ 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr
? "*" : "", local_var_prefix
, var
->name
);
3476 print_file(file
, indent
+ 1, "%s);\n", in_attr
&& out_attr
? "1" : "0");
3480 print_file(file
, indent
, "NdrServerContextNewMarshall(\n");
3481 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3482 print_file(file
, indent
+ 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix
, var
->name
);
3483 print_file(file
, indent
+ 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var
->type
));
3484 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3487 else if (phase
== PHASE_UNMARSHAL
)
3489 if (pass
== PASS_OUT
)
3492 print_file(file
, indent
, "*%s%s = 0;\n", local_var_prefix
, var
->name
);
3493 print_file(file
, indent
, "NdrClientContextUnmarshall(\n");
3494 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3495 print_file(file
, indent
+ 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix
, var
->name
);
3496 print_file(file
, indent
+ 1, "__frame->_Handle);\n");
3500 print_file(file
, indent
, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix
, var
->name
);
3501 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3502 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset
);
3507 print_phase_function(file
, indent
, "UserMarshal", local_var_prefix
, phase
, var
, start_offset
);
3510 if (phase
== PHASE_FREE
|| pass
== PASS_RETURN
||
3511 pointer_type
!= RPC_FC_RP
)
3513 if (pointer_type
== RPC_FC_RP
&& phase
== PHASE_FREE
&&
3514 !in_attr
&& is_conformant_array(type
))
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
);
3520 /* strings returned are assumed to be global and hence don't
3522 else if (is_declptr(type
) &&
3523 !(phase
== PHASE_FREE
&& pass
== PASS_RETURN
))
3524 print_phase_function(file
, indent
, "Pointer", local_var_prefix
,
3525 phase
, var
, start_offset
);
3529 unsigned int real_start_offset
= start_offset
;
3530 /* skip over pointer description straight to string description */
3531 if (is_declptr(type
))
3533 if (is_conformant_array(type
))
3534 real_start_offset
+= 4;
3536 real_start_offset
+= 2;
3538 if (is_array(type
) && !is_conformant_array(type
))
3539 print_phase_function(file
, indent
, "NonConformantString",
3540 local_var_prefix
, phase
, var
,
3543 print_phase_function(file
, indent
, "ConformantString", local_var_prefix
,
3544 phase
, var
, real_start_offset
);
3549 unsigned char tc
= get_array_fc(type
);
3550 const char *array_type
= "FixedArray";
3552 /* We already have the size_is expression since it's at the
3553 top level, but do checks for multidimensional conformant
3554 arrays. When we handle them, we'll need to extend this
3555 function to return a list, and then we'll actually use
3556 the return value. */
3557 get_size_is_expr(type
, var
->name
);
3559 if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
3561 array_type
= "VaryingArray";
3563 else if (tc
== RPC_FC_CARRAY
)
3565 array_type
= "ConformantArray";
3567 else if (tc
== RPC_FC_CVARRAY
|| tc
== RPC_FC_BOGUS_ARRAY
)
3569 array_type
= (tc
== RPC_FC_BOGUS_ARRAY
3571 : "ConformantVaryingArray");
3574 if (pointer_type
!= RPC_FC_RP
) array_type
= "Pointer";
3575 print_phase_function(file
, indent
, array_type
, local_var_prefix
, phase
, var
, start_offset
);
3576 if (phase
== PHASE_FREE
&& pointer_type
== RPC_FC_RP
)
3578 /* these are all unmarshalled by allocating memory */
3579 if (tc
== RPC_FC_BOGUS_ARRAY
||
3580 tc
== RPC_FC_CVARRAY
||
3581 ((tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
) && in_attr
) ||
3582 (tc
== RPC_FC_CARRAY
&& !in_attr
))
3584 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3586 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3592 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3595 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3598 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3599 /* Note: this goes beyond what MIDL does - it only supports arguments
3600 * with the [range] attribute in Oicf mode */
3601 if (phase
== PHASE_UNMARSHAL
)
3603 const expr_t
*range_min
;
3604 const expr_t
*range_max
;
3605 expr_list_t
*range_list
= get_attrp(var
->attrs
, ATTR_RANGE
);
3607 range_list
= get_aliaschain_attrp(type
, ATTR_RANGE
);
3608 range_min
= LIST_ENTRY(list_head(range_list
), const expr_t
, entry
);
3609 range_max
= LIST_ENTRY(list_next(range_list
, list_head(range_list
)), const expr_t
, entry
);
3611 print_file(file
, indent
, "if ((%s%s < (", local_var_prefix
, var
->name
);
3612 write_type_decl(file
, var
->type
, NULL
);
3613 fprintf(file
, ")0x%lx) || (%s%s > (", range_min
->cval
, local_var_prefix
, var
->name
);
3614 write_type_decl(file
, var
->type
, NULL
);
3615 fprintf(file
, ")0x%lx))\n", range_max
->cval
);
3616 print_file(file
, indent
, "{\n");
3617 print_file(file
, indent
+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
3618 print_file(file
, indent
, "}\n");
3622 switch (get_struct_fc(type
))
3625 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3626 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3628 case RPC_FC_PSTRUCT
:
3629 print_phase_function(file
, indent
, "SimpleStruct", local_var_prefix
, phase
, var
, start_offset
);
3631 case RPC_FC_CSTRUCT
:
3632 case RPC_FC_CPSTRUCT
:
3633 print_phase_function(file
, indent
, "ConformantStruct", local_var_prefix
, phase
, var
, start_offset
);
3635 case RPC_FC_CVSTRUCT
:
3636 print_phase_function(file
, indent
, "ConformantVaryingStruct", local_var_prefix
, phase
, var
, start_offset
);
3638 case RPC_FC_BOGUS_STRUCT
:
3639 print_phase_function(file
, indent
, "ComplexStruct", local_var_prefix
, phase
, var
, start_offset
);
3642 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(type
));
3647 const char *union_type
= NULL
;
3649 if (type_get_type(type
) == TYPE_UNION
)
3650 union_type
= "NonEncapsulatedUnion";
3651 else if (type_get_type(type
) == TYPE_ENCAPSULATED_UNION
)
3652 union_type
= "EncapsulatedUnion";
3654 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3655 phase
, var
, start_offset
);
3660 const type_t
*ref
= type_pointer_get_ref(type
);
3661 if (pointer_type
== RPC_FC_RP
) switch (typegen_detect_type(ref
, NULL
, TDT_ALL_TYPES
))
3664 print_phase_basetype(file
, indent
, local_var_prefix
, phase
, pass
, var
, var
->name
);
3667 /* base types have known sizes, so don't need a sizing pass
3668 * and don't have any memory to free and so don't need a
3670 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3671 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3675 const char *struct_type
= NULL
;
3676 switch (get_struct_fc(ref
))
3679 /* simple structs have known sizes, so don't need a sizing
3680 * pass and don't have any memory to free and so don't
3681 * need a freeing pass */
3682 if (phase
== PHASE_MARSHAL
|| phase
== PHASE_UNMARSHAL
)
3683 struct_type
= "SimpleStruct";
3684 else if (phase
== PHASE_FREE
&& pass
== PASS_RETURN
)
3686 print_file(file
, indent
, "if (%s%s)\n", local_var_prefix
, var
->name
);
3688 print_file(file
, indent
, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix
, var
->name
);
3692 case RPC_FC_PSTRUCT
:
3693 struct_type
= "SimpleStruct";
3695 case RPC_FC_CSTRUCT
:
3696 case RPC_FC_CPSTRUCT
:
3697 struct_type
= "ConformantStruct";
3699 case RPC_FC_CVSTRUCT
:
3700 struct_type
= "ConformantVaryingStruct";
3702 case RPC_FC_BOGUS_STRUCT
:
3703 struct_type
= "ComplexStruct";
3706 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, get_struct_fc(ref
));
3711 if (phase
== PHASE_FREE
)
3712 struct_type
= "Pointer";
3714 start_offset
= ref
->typestring_offset
;
3715 print_phase_function(file
, indent
, struct_type
, local_var_prefix
, phase
, var
, start_offset
);
3721 const char *union_type
= NULL
;
3722 if (phase
== PHASE_FREE
)
3723 union_type
= "Pointer";
3726 if (type_get_type(ref
) == TYPE_UNION
)
3727 union_type
= "NonEncapsulatedUnion";
3728 else if (type_get_type(ref
) == TYPE_ENCAPSULATED_UNION
)
3729 union_type
= "EncapsulatedUnion";
3731 start_offset
= ref
->typestring_offset
;
3734 print_phase_function(file
, indent
, union_type
, local_var_prefix
,
3735 phase
, var
, start_offset
);
3742 case TGT_IFACE_POINTER
:
3744 case TGT_CTXT_HANDLE
:
3745 case TGT_CTXT_HANDLE_POINTER
:
3746 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3753 print_phase_function(file
, indent
, "Pointer", local_var_prefix
, phase
, var
, start_offset
);
3756 case TGT_IFACE_POINTER
:
3757 print_phase_function(file
, indent
, "InterfacePointer", local_var_prefix
, phase
, var
, start_offset
);
3763 fprintf(file
, "\n");
3766 void write_remoting_arguments(FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
,
3767 enum pass pass
, enum remoting_phase phase
)
3769 if (phase
== PHASE_BUFFERSIZE
&& pass
!= PASS_RETURN
)
3771 unsigned int size
= get_function_buffer_size( func
, pass
);
3772 print_file(file
, indent
, "__frame->_StubMsg.BufferLength = %u;\n", size
);
3775 if (pass
== PASS_RETURN
)
3779 var
.type
= type_function_get_rettype(func
->type
);
3780 var
.name
= xstrdup( "_RetVal" );
3781 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, &var
);
3787 if (!type_get_function_args(func
->type
))
3789 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3790 write_remoting_arg( file
, indent
, func
, local_var_prefix
, pass
, phase
, var
);
3795 unsigned int get_size_procformatstring_type(const char *name
, const type_t
*type
, const attr_list_t
*attrs
)
3797 return write_procformatstring_type(NULL
, 0, name
, type
, attrs
, FALSE
);
3801 unsigned int get_size_procformatstring_func(const var_t
*func
)
3804 unsigned int size
= 0;
3806 /* argument list size */
3807 if (type_get_function_args(func
->type
))
3808 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3809 size
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
3811 /* return value size */
3812 if (is_void(type_function_get_rettype(func
->type
)))
3813 size
+= 2; /* FC_END and FC_PAD */
3815 size
+= get_size_procformatstring_type("return value", type_function_get_rettype(func
->type
), NULL
);
3820 unsigned int get_size_procformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3822 const statement_t
*stmt
;
3823 unsigned int size
= 1;
3825 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
3827 const type_t
*iface
;
3828 const statement_t
*stmt_func
;
3830 if (stmt
->type
== STMT_LIBRARY
)
3832 size
+= get_size_procformatstring(stmt
->u
.lib
->stmts
, pred
) - 1;
3835 else if (stmt
->type
!= STMT_TYPE
|| type_get_type(stmt
->u
.type
) != TYPE_INTERFACE
)
3838 iface
= stmt
->u
.type
;
3842 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(iface
) )
3844 const var_t
*func
= stmt_func
->u
.var
;
3845 if (!is_local(func
->attrs
))
3846 size
+= get_size_procformatstring_func( func
);
3852 unsigned int get_size_typeformatstring(const statement_list_t
*stmts
, type_pred_t pred
)
3854 set_all_tfswrite(FALSE
);
3855 return process_tfs(NULL
, stmts
, pred
);
3858 void declare_stub_args( FILE *file
, int indent
, const var_t
*func
)
3860 int in_attr
, out_attr
;
3864 /* declare return value '_RetVal' */
3865 if (!is_void(type_function_get_rettype(func
->type
)))
3867 print_file(file
, indent
, "%s", "");
3868 write_type_decl_left(file
, type_function_get_rettype(func
->type
));
3869 fprintf(file
, " _RetVal;\n");
3872 if (!type_get_function_args(func
->type
))
3875 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3877 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3878 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3879 if (!out_attr
&& !in_attr
)
3882 if (is_context_handle(var
->type
))
3883 print_file(file
, indent
, "NDR_SCONTEXT %s;\n", var
->name
);
3886 if (!in_attr
&& !is_conformant_array(var
->type
))
3888 type_t
*type_to_print
;
3890 print_file(file
, indent
, "%s", "");
3891 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3892 !type_array_is_decl_as_ptr(var
->type
))
3893 type_to_print
= var
->type
;
3895 type_to_print
= type_pointer_get_ref(var
->type
);
3896 sprintf(name
, "_W%u", i
++);
3897 write_type_decl(file
, type_to_print
, name
);
3898 fprintf(file
, ";\n");
3901 print_file(file
, indent
, "%s", "");
3902 write_type_decl_left(file
, var
->type
);
3904 if (type_get_type(var
->type
) == TYPE_ARRAY
&&
3905 !type_array_is_decl_as_ptr(var
->type
)) {
3906 fprintf(file
, "(*%s)", var
->name
);
3908 fprintf(file
, "%s", var
->name
);
3909 write_type_right(file
, var
->type
, FALSE
);
3910 fprintf(file
, ";\n");
3912 if (decl_indirect(var
->type
))
3913 print_file(file
, indent
, "void *_p_%s;\n", var
->name
);
3919 void assign_stub_out_args( FILE *file
, int indent
, const var_t
*func
, const char *local_var_prefix
)
3921 int in_attr
, out_attr
;
3925 if (!type_get_function_args(func
->type
))
3928 LIST_FOR_EACH_ENTRY( var
, type_get_function_args(func
->type
), const var_t
, entry
)
3930 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
3931 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
3932 if (!out_attr
&& !in_attr
)
3937 print_file(file
, indent
, "%s%s", local_var_prefix
, var
->name
);
3939 if (is_context_handle(var
->type
))
3941 fprintf(file
, " = NdrContextHandleInitialize(\n");
3942 print_file(file
, indent
+ 1, "&__frame->_StubMsg,\n");
3943 print_file(file
, indent
+ 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
3944 var
->type
->typestring_offset
);
3946 else if (is_array(var
->type
) &&
3947 type_array_has_conformance(var
->type
))
3949 unsigned int size
, align
= 0;
3950 type_t
*type
= var
->type
;
3952 fprintf(file
, " = NdrAllocate(&__frame->_StubMsg, ");
3954 is_array(type
) && type_array_has_conformance(type
);
3955 type
= type_array_get_element(type
))
3957 write_expr(file
, type_array_get_conformance(type
), TRUE
,
3958 TRUE
, NULL
, NULL
, local_var_prefix
);
3959 fprintf(file
, " * ");
3961 size
= type_memsize(type
, &align
);
3962 fprintf(file
, "%u);\n", size
);
3966 fprintf(file
, " = &%s_W%u;\n", local_var_prefix
, i
);
3967 switch (typegen_detect_type(type_pointer_get_ref(var
->type
), var
->attrs
, TDT_IGNORE_STRINGS
))
3973 print_file(file
, indent
, "%s_W%u = 0;\n", local_var_prefix
, i
);
3978 case TGT_IFACE_POINTER
:
3980 case TGT_CTXT_HANDLE
:
3981 case TGT_CTXT_HANDLE_POINTER
:
3984 /* not initialised */
3994 fprintf(file
, "\n");
3998 int write_expr_eval_routines(FILE *file
, const char *iface
)
4000 static const char *var_name
= "pS";
4001 static const char *var_name_expr
= "pS->";
4003 struct expr_eval_routine
*eval
;
4004 unsigned short callback_offset
= 0;
4006 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
4008 const char *name
= eval
->structure
->name
;
4011 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
4012 iface
, name
, callback_offset
);
4013 print_file(file
, 0, "{\n");
4014 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
4015 name
, var_name
, name
, eval
->baseoff
);
4016 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
4017 print_file(file
, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
4018 write_expr(file
, eval
->expr
, 1, 1, var_name_expr
, eval
->structure
, "");
4019 fprintf(file
, ";\n");
4020 print_file(file
, 0, "}\n\n");
4026 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
4028 struct expr_eval_routine
*eval
;
4029 struct expr_eval_routine
*cursor
;
4030 unsigned short callback_offset
= 0;
4032 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
4033 fprintf(file
, "{\n");
4035 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
4037 const char *name
= eval
->structure
->name
;
4038 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
4040 list_remove(&eval
->entry
);
4044 fprintf(file
, "};\n\n");
4047 void write_user_quad_list(FILE *file
)
4051 if (list_empty(&user_type_list
))
4054 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
4055 fprintf(file
, "{\n");
4056 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
4058 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
4059 print_file(file
, 1, "{\n");
4060 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
4061 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
4062 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
4063 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
4064 print_file(file
, 1, "}%s\n", sep
);
4066 fprintf(file
, "};\n\n");
4069 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
4071 const struct str_list_entry_t
*endpoint
;
4074 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
4075 print_file( f
, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
4076 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
4078 print_file( f
, 1, "{ (const unsigned char *)\"" );
4079 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
4081 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
4084 if (!*p
) goto error
;
4085 if (p
[1] != '[') goto error
;
4087 fprintf( f
, "\", (const unsigned char *)\"" );
4088 for (p
+= 2; *p
&& *p
!= ']'; p
++)
4090 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
4093 if (*p
!= ']') goto error
;
4094 fprintf( f
, "\" },\n" );
4096 print_file( f
, 0, "};\n\n" );
4100 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);
4103 void write_exceptions( FILE *file
)
4105 fprintf( file
, "#ifndef USE_COMPILER_EXCEPTIONS\n");
4106 fprintf( file
, "\n");
4107 fprintf( file
, "#include \"wine/exception.h\"\n");
4108 fprintf( file
, "#undef RpcTryExcept\n");
4109 fprintf( file
, "#undef RpcExcept\n");
4110 fprintf( file
, "#undef RpcEndExcept\n");
4111 fprintf( file
, "#undef RpcTryFinally\n");
4112 fprintf( file
, "#undef RpcFinally\n");
4113 fprintf( file
, "#undef RpcEndFinally\n");
4114 fprintf( file
, "#undef RpcExceptionCode\n");
4115 fprintf( file
, "#undef RpcAbnormalTermination\n");
4116 fprintf( file
, "\n");
4117 fprintf( file
, "struct __exception_frame;\n");
4118 fprintf( file
, "typedef int (*__filter_func)(struct __exception_frame *);\n");
4119 fprintf( file
, "typedef void (*__finally_func)(struct __exception_frame *);\n");
4120 fprintf( file
, "\n");
4121 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4122 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
4123 fprintf( file
, " __filter_func filter; \\\n");
4124 fprintf( file
, " __finally_func finally; \\\n");
4125 fprintf( file
, " sigjmp_buf jmp; \\\n");
4126 fprintf( file
, " DWORD code; \\\n");
4127 fprintf( file
, " unsigned char abnormal_termination; \\\n");
4128 fprintf( file
, " unsigned char filter_level; \\\n");
4129 fprintf( file
, " unsigned char finally_level;\n");
4130 fprintf( file
, "\n");
4131 fprintf( file
, "struct __exception_frame\n{\n");
4132 fprintf( file
, " __DECL_EXCEPTION_FRAME\n");
4133 fprintf( file
, "};\n");
4134 fprintf( file
, "\n");
4135 fprintf( file
, "static inline void __widl_unwind_target(void)\n" );
4136 fprintf( file
, "{\n");
4137 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
4138 fprintf( file
, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
4139 fprintf( file
, " {\n");
4140 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4141 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4142 fprintf( file
, " __wine_pop_frame( &exc_frame->frame );\n");
4143 fprintf( file
, " }\n");
4144 fprintf( file
, " exc_frame->filter_level = 0;\n");
4145 fprintf( file
, " siglongjmp( exc_frame->jmp, 1 );\n");
4146 fprintf( file
, "}\n");
4147 fprintf( file
, "\n");
4148 fprintf( file
, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
4149 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
4150 fprintf( file
, " CONTEXT *context,\n");
4151 fprintf( file
, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
4152 fprintf( file
, "{\n");
4153 fprintf( file
, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
4154 fprintf( file
, "\n");
4155 fprintf( file
, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
4156 fprintf( file
, " {\n" );
4157 fprintf( file
, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
4158 fprintf( file
, " {\n" );
4159 fprintf( file
, " exc_frame->abnormal_termination = 1;\n");
4160 fprintf( file
, " exc_frame->finally( exc_frame );\n");
4161 fprintf( file
, " }\n" );
4162 fprintf( file
, " return ExceptionContinueSearch;\n");
4163 fprintf( file
, " }\n" );
4164 fprintf( file
, " exc_frame->code = record->ExceptionCode;\n");
4165 fprintf( file
, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
4166 fprintf( file
, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
4167 fprintf( file
, " return ExceptionContinueSearch;\n");
4168 fprintf( file
, "}\n");
4169 fprintf( file
, "\n");
4170 fprintf( file
, "#define RpcTryExcept \\\n");
4171 fprintf( file
, " if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
4172 fprintf( file
, " { \\\n");
4173 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4174 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4175 fprintf( file
, " __frame->filter_level = __frame->finally_level + 1;\n" );
4176 fprintf( file
, "\n");
4177 fprintf( file
, "#define RpcExcept(expr) \\\n");
4178 fprintf( file
, " if (!__frame->finally_level) \\\n" );
4179 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4180 fprintf( file
, " __frame->filter_level = 0; \\\n" );
4181 fprintf( file
, " } \\\n");
4182 fprintf( file
, " else \\\n");
4183 fprintf( file
, "\n");
4184 fprintf( file
, "#define RpcEndExcept\n");
4185 fprintf( file
, "\n");
4186 fprintf( file
, "#define RpcExceptionCode() (__frame->code)\n");
4187 fprintf( file
, "\n");
4188 fprintf( file
, "#define RpcTryFinally \\\n");
4189 fprintf( file
, " if (!__frame->filter_level) \\\n");
4190 fprintf( file
, " __wine_push_frame( &__frame->frame ); \\\n");
4191 fprintf( file
, " __frame->finally_level = __frame->filter_level + 1;\n");
4192 fprintf( file
, "\n");
4193 fprintf( file
, "#define RpcFinally \\\n");
4194 fprintf( file
, " if (!__frame->filter_level) \\\n");
4195 fprintf( file
, " __wine_pop_frame( &__frame->frame ); \\\n");
4196 fprintf( file
, " __frame->finally_level = 0;\n");
4197 fprintf( file
, "\n");
4198 fprintf( file
, "#define RpcEndFinally\n");
4199 fprintf( file
, "\n");
4200 fprintf( file
, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
4201 fprintf( file
, "\n");
4202 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4203 fprintf( file
, " do { \\\n");
4204 fprintf( file
, " __frame->frame.Handler = __widl_exception_handler; \\\n");
4205 fprintf( file
, " __frame->filter = (__filter_func)(filter_func); \\\n" );
4206 fprintf( file
, " __frame->finally = (__finally_func)(finally_func); \\\n");
4207 fprintf( file
, " __frame->abnormal_termination = 0; \\\n");
4208 fprintf( file
, " __frame->filter_level = 0; \\\n");
4209 fprintf( file
, " __frame->finally_level = 0; \\\n");
4210 fprintf( file
, " } while (0)\n");
4211 fprintf( file
, "\n");
4212 fprintf( file
, "#else /* USE_COMPILER_EXCEPTIONS */\n");
4213 fprintf( file
, "\n");
4214 fprintf( file
, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
4215 fprintf( file
, " do { (void)(filter_func); } while(0)\n");
4216 fprintf( file
, "\n");
4217 fprintf( file
, "#define __DECL_EXCEPTION_FRAME \\\n");
4218 fprintf( file
, " DWORD code;\n");
4219 fprintf( file
, "\n");
4220 fprintf( file
, "#endif /* USE_COMPILER_EXCEPTIONS */\n");