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"
41 #include "wine/list.h"
46 static const func_t
*current_func
;
47 static const type_t
*current_structure
;
49 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
50 struct expr_eval_routine
53 const type_t
*structure
;
57 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
);
58 static size_t write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
59 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
60 const char *name
, int write_ptr
, unsigned int *tfsoff
);
62 const char *string_of_type(unsigned char type
)
66 case RPC_FC_BYTE
: return "FC_BYTE";
67 case RPC_FC_CHAR
: return "FC_CHAR";
68 case RPC_FC_SMALL
: return "FC_SMALL";
69 case RPC_FC_USMALL
: return "FC_USMALL";
70 case RPC_FC_WCHAR
: return "FC_WCHAR";
71 case RPC_FC_SHORT
: return "FC_SHORT";
72 case RPC_FC_USHORT
: return "FC_USHORT";
73 case RPC_FC_LONG
: return "FC_LONG";
74 case RPC_FC_ULONG
: return "FC_ULONG";
75 case RPC_FC_FLOAT
: return "FC_FLOAT";
76 case RPC_FC_HYPER
: return "FC_HYPER";
77 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
78 case RPC_FC_ENUM16
: return "FC_ENUM16";
79 case RPC_FC_ENUM32
: return "FC_ENUM32";
80 case RPC_FC_IGNORE
: return "FC_IGNORE";
81 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
82 case RPC_FC_RP
: return "FC_RP";
83 case RPC_FC_UP
: return "FC_UP";
84 case RPC_FC_OP
: return "FC_OP";
85 case RPC_FC_FP
: return "FC_FP";
86 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
87 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
88 case RPC_FC_STRUCT
: return "FC_STRUCT";
89 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
90 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
91 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
92 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
93 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
94 case RPC_FC_SMFARRAY
: return "FC_SMFARRAY";
95 case RPC_FC_LGFARRAY
: return "FC_LGFARRAY";
96 case RPC_FC_SMVARRAY
: return "FC_SMVARRAY";
97 case RPC_FC_LGVARRAY
: return "FC_LGVARRAY";
98 case RPC_FC_CARRAY
: return "FC_CARRAY";
99 case RPC_FC_CVARRAY
: return "FC_CVARRAY";
100 case RPC_FC_BOGUS_ARRAY
: return "FC_BOGUS_ARRAY";
101 case RPC_FC_ALIGNM4
: return "FC_ALIGNM4";
102 case RPC_FC_ALIGNM8
: return "FC_ALIGNM8";
103 case RPC_FC_POINTER
: return "FC_POINTER";
105 error("string_of_type: unknown type 0x%02x\n", type
);
110 int is_struct(unsigned char type
)
117 case RPC_FC_CPSTRUCT
:
118 case RPC_FC_CVSTRUCT
:
119 case RPC_FC_BOGUS_STRUCT
:
126 static int is_non_complex_struct(const type_t
*type
)
133 case RPC_FC_CPSTRUCT
:
134 case RPC_FC_CVSTRUCT
:
141 int is_union(unsigned char type
)
145 case RPC_FC_ENCAPSULATED_UNION
:
146 case RPC_FC_NON_ENCAPSULATED_UNION
:
153 static unsigned short user_type_offset(const char *name
)
156 unsigned short off
= 0;
157 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
159 if (strcmp(name
, ut
->name
) == 0)
163 error("user_type_offset: couldn't find type (%s)\n", name
);
167 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
169 type
->typestring_offset
= offset
;
170 if (file
) type
->tfswrite
= FALSE
;
173 static void guard_rec(type_t
*type
)
175 /* types that contain references to themselves (like a linked list),
176 need to be shielded from infinite recursion when writing embedded
178 if (type
->typestring_offset
)
179 type
->tfswrite
= FALSE
;
181 type
->typestring_offset
= 1;
184 static type_t
*get_user_type(const type_t
*t
, const char **pname
)
188 type_t
*ut
= get_attrp(t
->attrs
, ATTR_WIREMARSHAL
);
196 if (t
->kind
== TKIND_ALIAS
)
203 static int is_user_type(const type_t
*t
)
205 return get_user_type(t
, NULL
) != NULL
;
208 static int is_embedded_complex(const type_t
*type
)
210 unsigned char tc
= type
->type
;
211 return is_struct(tc
) || is_union(tc
) || is_array(type
) || is_user_type(type
)
212 || (is_ptr(type
) && type
->ref
->type
== RPC_FC_IP
);
215 static int compare_expr(const expr_t
*a
, const expr_t
*b
)
219 if (a
->type
!= b
->type
)
220 return a
->type
- b
->type
;
227 return a
->u
.lval
- b
->u
.lval
;
229 return a
->u
.dval
- b
->u
.dval
;
230 case EXPR_IDENTIFIER
:
231 return strcmp(a
->u
.sval
, b
->u
.sval
);
233 ret
= compare_expr(a
->ref
, b
->ref
);
236 ret
= compare_expr(a
->u
.ext
, b
->u
.ext
);
239 return compare_expr(a
->ext2
, b
->ext2
);
248 ret
= compare_expr(a
->ref
, b
->ref
);
251 return compare_expr(a
->u
.ext
, b
->u
.ext
);
257 return compare_expr(a
->ref
, b
->ref
);
264 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
267 fprintf(file, "/* %2u */\n", typestring_offset); \
268 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
272 static void print_file(FILE *file
, int indent
, const char *format
, ...)
275 va_start(va
, format
);
276 print(file
, indent
, format
, va
);
280 void print(FILE *file
, int indent
, const char *format
, va_list va
)
284 if (format
[0] != '\n')
287 vfprintf(file
, format
, va
);
291 void write_parameters_init(FILE *file
, int indent
, const func_t
*func
)
298 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
300 const type_t
*t
= var
->type
;
301 const char *n
= var
->name
;
302 if (decl_indirect(t
))
303 print_file(file
, indent
, "MIDL_memset(&%s, 0, sizeof %s);\n", n
, n
);
304 else if (is_ptr(t
) || is_array(t
))
305 print_file(file
, indent
, "%s = 0;\n", n
);
311 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
313 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
314 print_file(f
, indent
, "{\n");
315 print_file(f
, indent
+ 1, "short Pad;\n");
316 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
317 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
318 print_file(f
, indent
, "\n");
321 void write_formatstringsdecl(FILE *f
, int indent
, ifref_list_t
*ifaces
, int for_objects
)
323 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
324 get_size_typeformatstring(ifaces
, for_objects
));
326 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
327 get_size_procformatstring(ifaces
, for_objects
));
330 write_formatdesc(f
, indent
, "TYPE");
331 write_formatdesc(f
, indent
, "PROC");
333 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
334 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
335 print_file(f
, indent
, "\n");
338 static inline int is_base_type(unsigned char type
)
357 case RPC_FC_ERROR_STATUS_T
:
358 case RPC_FC_BIND_PRIMITIVE
:
366 int decl_indirect(const type_t
*t
)
368 return is_user_type(t
)
369 || (!is_base_type(t
->type
)
374 static size_t write_procformatstring_var(FILE *file
, int indent
,
375 const var_t
*var
, int is_return
)
378 const type_t
*type
= var
->type
;
380 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
381 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
383 if (!is_in
&& !is_out
) is_in
= TRUE
;
385 if (!type
->declarray
&& is_base_type(type
->type
))
388 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
390 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
392 if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
394 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
395 size
= 2; /* includes param type prefix */
397 else if (is_base_type(type
->type
))
399 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
400 size
= 2; /* includes param type prefix */
404 error("Unknown/unsupported type: %s (0x%02x)\n", var
->name
, type
->type
);
411 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
412 else if (is_in
&& is_out
)
413 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
415 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
417 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
419 print_file(file
, indent
, "0x01,\n");
420 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
421 size
= 4; /* includes param type prefix */
426 void write_procformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
428 const ifref_t
*iface
;
432 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
433 print_file(file
, indent
, "{\n");
435 print_file(file
, indent
, "0,\n");
436 print_file(file
, indent
, "{\n");
439 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
441 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
444 if (iface
->iface
->funcs
)
447 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
449 if (is_local(func
->def
->attrs
)) continue;
450 /* emit argument data */
453 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
454 write_procformatstring_var(file
, indent
, var
, FALSE
);
457 /* emit return value data */
459 if (is_void(var
->type
))
461 print_file(file
, indent
, "0x5b, /* FC_END */\n");
462 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
465 write_procformatstring_var(file
, indent
, var
, TRUE
);
470 print_file(file
, indent
, "0x0\n");
472 print_file(file
, indent
, "}\n");
474 print_file(file
, indent
, "};\n");
475 print_file(file
, indent
, "\n");
478 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
480 if (is_base_type(type
->type
))
482 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
483 *typestring_offset
+= 1;
490 /* write conformance / variance descriptor */
491 static size_t write_conf_or_var_desc(FILE *file
, const type_t
*structure
,
492 unsigned int baseoff
, const expr_t
*expr
)
494 unsigned char operator_type
= 0;
495 const char *operator_string
= "no operators";
496 const expr_t
*subexpr
;
500 print_file(file
, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
506 /* Top-level conformance calculations are done inline. */
507 print_file (file
, 2, "0x%x,\t/* Corr desc: parameter */\n",
508 RPC_FC_TOP_LEVEL_CONFORMANCE
);
509 print_file (file
, 2, "0x0,\n");
510 print_file (file
, 2, "NdrFcShort(0x0),\n");
516 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
517 error("write_conf_or_var_desc: constant value %ld is greater than "
518 "the maximum constant size of %d\n", expr
->cval
,
519 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
521 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
522 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
523 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
524 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
530 switch (subexpr
->type
)
533 subexpr
= subexpr
->ref
;
534 operator_type
= RPC_FC_DEREFERENCE
;
535 operator_string
= "FC_DEREFERENCE";
538 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
540 subexpr
= subexpr
->ref
;
541 operator_type
= RPC_FC_DIV_2
;
542 operator_string
= "FC_DIV_2";
546 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
548 subexpr
= subexpr
->ref
;
549 operator_type
= RPC_FC_MULT_2
;
550 operator_string
= "FC_MULT_2";
554 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
556 subexpr
= subexpr
->ref
;
557 operator_type
= RPC_FC_SUB_1
;
558 operator_string
= "FC_SUB_1";
562 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
564 subexpr
= subexpr
->ref
;
565 operator_type
= RPC_FC_ADD_1
;
566 operator_string
= "FC_ADD_1";
573 if (subexpr
->type
== EXPR_IDENTIFIER
)
575 const type_t
*correlation_variable
= NULL
;
576 unsigned char correlation_variable_type
;
577 unsigned char param_type
= 0;
578 const char *param_type_string
= NULL
;
582 if (structure
->fields
) LIST_FOR_EACH_ENTRY( var
, structure
->fields
, const var_t
, entry
)
584 unsigned int align
= 0;
585 /* FIXME: take alignment into account */
586 if (var
->name
&& !strcmp(var
->name
, subexpr
->u
.sval
))
588 correlation_variable
= var
->type
;
591 offset
+= type_memsize(var
->type
, &align
);
593 if (!correlation_variable
)
594 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
598 correlation_variable_type
= correlation_variable
->type
;
600 switch (correlation_variable_type
)
604 param_type
= RPC_FC_SMALL
;
605 param_type_string
= "FC_SMALL";
609 param_type
= RPC_FC_USMALL
;
610 param_type_string
= "FC_USMALL";
615 param_type
= RPC_FC_SHORT
;
616 param_type_string
= "FC_SHORT";
619 param_type
= RPC_FC_USHORT
;
620 param_type_string
= "FC_USHORT";
624 param_type
= RPC_FC_LONG
;
625 param_type_string
= "FC_LONG";
628 param_type
= RPC_FC_ULONG
;
629 param_type_string
= "FC_ULONG";
635 if (sizeof(void *) == 4) /* FIXME */
637 param_type
= RPC_FC_LONG
;
638 param_type_string
= "FC_LONG";
642 param_type
= RPC_FC_HYPER
;
643 param_type_string
= "FC_HYPER";
647 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
648 correlation_variable_type
);
651 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n",
652 RPC_FC_NORMAL_CONFORMANCE
| param_type
, param_type_string
);
653 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
654 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
659 unsigned int callback_offset
= 0;
660 struct expr_eval_routine
*eval
;
663 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
665 if (!strcmp (eval
->structure
->name
, structure
->name
)
666 && !compare_expr (eval
->expr
, expr
))
676 eval
= xmalloc (sizeof(*eval
));
677 eval
->structure
= structure
;
679 list_add_tail (&expr_eval_routines
, &eval
->entry
);
682 if (callback_offset
> USHRT_MAX
)
683 error("Maximum number of callback routines reached\n");
685 print_file(file
, 2, "0x%x, /* Corr desc: */\n", RPC_FC_NORMAL_CONFORMANCE
);
686 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
687 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
692 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
694 int have_align
= FALSE
;
698 if (!fields
) return 0;
699 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
701 unsigned int falign
= 0;
702 size_t fsize
= type_memsize(v
->type
, &falign
);
708 size
= (size
+ (falign
- 1)) & ~(falign
- 1);
712 size
= (size
+ (*align
- 1)) & ~(*align
- 1);
716 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
718 size_t size
, maxs
= 0;
719 unsigned int align
= *pmaxa
;
722 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
724 /* we could have an empty default field with NULL type */
727 size
= type_memsize(v
->type
, &align
);
728 if (maxs
< size
) maxs
= size
;
729 if (*pmaxa
< align
) *pmaxa
= align
;
736 int get_padding(const var_list_t
*fields
)
738 unsigned short offset
= 0;
745 LIST_FOR_EACH_ENTRY(f
, fields
, const var_t
, entry
)
747 type_t
*ft
= f
->type
;
748 unsigned int align
= 0;
749 size_t size
= type_memsize(ft
, &align
);
752 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
756 return ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
759 size_t type_memsize(const type_t
*t
, unsigned int *align
)
763 if (t
->declarray
&& is_conformant_array(t
))
765 type_memsize(t
->ref
, align
);
768 else if (is_ptr(t
) || is_conformant_array(t
))
770 size
= sizeof(void *);
771 if (size
> *align
) *align
= size
;
773 else switch (t
->type
)
780 if (size
> *align
) *align
= size
;
787 if (size
> *align
) *align
= size
;
791 case RPC_FC_ERROR_STATUS_T
:
795 if (size
> *align
) *align
= size
;
800 if (size
> *align
) *align
= size
;
803 case RPC_FC_CVSTRUCT
:
804 case RPC_FC_CPSTRUCT
:
807 case RPC_FC_BOGUS_STRUCT
:
808 size
= fields_memsize(t
->fields
, align
);
810 case RPC_FC_ENCAPSULATED_UNION
:
811 case RPC_FC_NON_ENCAPSULATED_UNION
:
812 size
= union_memsize(t
->fields
, align
);
814 case RPC_FC_SMFARRAY
:
815 case RPC_FC_LGFARRAY
:
816 case RPC_FC_SMVARRAY
:
817 case RPC_FC_LGVARRAY
:
818 case RPC_FC_BOGUS_ARRAY
:
819 size
= t
->dim
* type_memsize(t
->ref
, align
);
822 error("type_memsize: Unknown type %d\n", t
->type
);
829 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
831 short absoff
= type
->ref
->typestring_offset
;
832 short reloff
= absoff
- (offset
+ 2);
833 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
835 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
836 type
->type
, ptr_attr
, string_of_type(type
->type
));
837 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
838 reloff
, reloff
, absoff
);
842 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
844 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
845 type
->type
, string_of_type(type
->type
));
846 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->ref
->type
,
847 string_of_type(type
->ref
->type
));
848 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
852 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
854 unsigned int offset
= *typestring_offset
;
856 print_file(file
, 0, "/* %d */\n", offset
);
857 update_tfsoff(type
, offset
, file
);
859 if (type
->ref
->typestring_offset
)
860 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
861 else if (is_base_type(type
->ref
->type
))
862 *typestring_offset
+= write_simple_pointer(file
, type
);
867 static int processed(const type_t
*type
)
869 return type
->typestring_offset
&& !type
->tfswrite
;
872 static void write_user_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
874 unsigned int start
, absoff
, flags
;
875 unsigned int align
= 0, ualign
= 0;
877 type_t
*utype
= get_user_type(type
, &name
);
878 size_t usize
= type_memsize(utype
, &ualign
);
879 size_t size
= type_memsize(type
, &align
);
880 unsigned short funoff
= user_type_offset(name
);
885 if (is_base_type(utype
->type
))
888 print_file(file
, 0, "/* %d */\n", absoff
);
889 print_file(file
, 2, "0x%x,\t/* %s */\n", utype
->type
, string_of_type(utype
->type
));
890 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
895 if (!processed(utype
))
896 write_embedded_types(file
, NULL
, utype
, utype
->name
, TRUE
, tfsoff
);
897 absoff
= utype
->typestring_offset
;
900 if (utype
->type
== RPC_FC_RP
)
902 else if (utype
->type
== RPC_FC_UP
)
908 update_tfsoff(type
, start
, file
);
909 print_file(file
, 0, "/* %d */\n", start
);
910 print_file(file
, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL
);
911 print_file(file
, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
912 flags
| (align
- 1), align
- 1, flags
);
913 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff
, funoff
);
914 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize
, usize
);
915 print_file(file
, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size
, size
);
917 reloff
= absoff
- *tfsoff
;
918 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff
, reloff
, absoff
);
922 static void write_member_type(FILE *file
, const type_t
*cont
,
923 const attr_list_t
*attrs
, const type_t
*type
,
924 unsigned int *corroff
, unsigned int *tfsoff
)
926 if (is_embedded_complex(type
))
931 if (is_union(type
->type
) && is_attr(attrs
, ATTR_SWITCHIS
))
938 absoff
= type
->typestring_offset
;
940 reloff
= absoff
- (*tfsoff
+ 2);
942 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
943 /* FIXME: actually compute necessary padding */
944 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
945 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
946 reloff
, reloff
, absoff
);
949 else if (is_ptr(type
))
951 unsigned char fc
= (cont
->type
== RPC_FC_BOGUS_STRUCT
954 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
957 else if (!write_base_type(file
, type
, tfsoff
))
958 error("Unsupported member type 0x%x\n", type
->type
);
961 static void write_end(FILE *file
, unsigned int *tfsoff
)
963 if (*tfsoff
% 2 == 0)
965 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
968 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
972 static void write_descriptors(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
974 unsigned int offset
= 0;
975 var_list_t
*fs
= type
->fields
;
978 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, var_t
, entry
)
980 unsigned int align
= 0;
981 type_t
*ft
= f
->type
;
982 if (is_union(ft
->type
) && is_attr(f
->attrs
, ATTR_SWITCHIS
))
984 unsigned int absoff
= ft
->typestring_offset
;
985 short reloff
= absoff
- (*tfsoff
+ 6);
986 print_file(file
, 0, "/* %d */\n", *tfsoff
);
987 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
988 print_file(file
, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG
);
989 write_conf_or_var_desc(file
, current_structure
, offset
,
990 get_attrp(f
->attrs
, ATTR_SWITCHIS
));
991 print_file(file
, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
992 reloff
, reloff
, absoff
);
996 /* FIXME: take alignment into account */
997 offset
+= type_memsize(ft
, &align
);
1001 static int write_no_repeat_pointer_descriptions(
1002 FILE *file
, type_t
*type
,
1003 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1004 unsigned int *typestring_offset
)
1011 print_file(file
, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT
);
1012 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1014 /* pointer instance */
1015 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1016 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1017 *typestring_offset
+= 6;
1019 if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1020 write_pointer_tfs(file
, type
, typestring_offset
);
1022 error("write_pointer_description: type format string unknown\n");
1025 *offset_in_memory
+= type_memsize(type
, &align
);
1026 /* FIXME: is there a case where these two are different? */
1028 *offset_in_buffer
+= type_memsize(type
, &align
);
1033 if (is_non_complex_struct(type
))
1036 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1037 written
+= write_no_repeat_pointer_descriptions(
1039 offset_in_memory
, offset_in_buffer
, typestring_offset
);
1044 *offset_in_memory
+= type_memsize(type
, &align
);
1045 /* FIXME: is there a case where these two are different? */
1047 *offset_in_buffer
+= type_memsize(type
, &align
);
1053 static int write_pointer_description_offsets(
1054 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1055 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1056 unsigned int *typestring_offset
)
1061 if (is_ptr(type
) && type
->ref
->type
!= RPC_FC_IP
)
1063 if (offset_in_memory
&& offset_in_buffer
)
1065 /* pointer instance */
1066 /* FIXME: sometimes from end of structure, sometimes from beginning */
1067 print_file(file
, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory
, *offset_in_memory
);
1068 print_file(file
, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer
, *offset_in_buffer
);
1071 *offset_in_memory
+= type_memsize(type
, &align
);
1072 /* FIXME: is there a case where these two are different? */
1074 *offset_in_buffer
+= type_memsize(type
, &align
);
1076 *typestring_offset
+= 4;
1078 if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
1079 write_pointer_tfs(file
, type
, typestring_offset
);
1081 error("write_pointer_description_offsets: type format string unknown\n");
1088 return write_pointer_description_offsets(
1089 file
, attrs
, type
->ref
, offset_in_memory
, offset_in_buffer
,
1092 else if (is_non_complex_struct(type
))
1094 /* otherwise search for interesting fields to parse */
1096 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1098 written
+= write_pointer_description_offsets(
1099 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1106 if (offset_in_memory
)
1107 *offset_in_memory
+= type_memsize(type
, &align
);
1108 /* FIXME: is there a case where these two are different? */
1110 if (offset_in_buffer
)
1111 *offset_in_buffer
+= type_memsize(type
, &align
);
1117 /* Note: if file is NULL return value is number of pointers to write, else
1118 * it is the number of type format characters written */
1119 static int write_fixed_array_pointer_descriptions(
1120 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1121 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1122 unsigned int *typestring_offset
)
1125 int pointer_count
= 0;
1127 if (type
->type
== RPC_FC_SMFARRAY
|| type
->type
== RPC_FC_LGFARRAY
)
1129 unsigned int temp
= 0;
1130 /* unfortunately, this needs to be done in two passes to avoid
1131 * writing out redundant FC_FIXED_REPEAT descriptions */
1132 pointer_count
= write_pointer_description_offsets(
1133 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1134 if (pointer_count
> 0)
1136 unsigned int increment_size
;
1137 size_t offset_of_array_pointer_mem
= 0;
1138 size_t offset_of_array_pointer_buf
= 0;
1141 increment_size
= type_memsize(type
->ref
, &align
);
1143 print_file(file
, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT
);
1144 print_file(file
, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD
);
1145 print_file(file
, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type
->dim
, type
->dim
);
1146 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1147 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1148 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1149 *typestring_offset
+= 10;
1151 pointer_count
= write_pointer_description_offsets(
1152 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1153 &offset_of_array_pointer_buf
, typestring_offset
);
1156 else if (is_struct(type
->type
))
1159 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1161 pointer_count
+= write_fixed_array_pointer_descriptions(
1162 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1169 if (offset_in_memory
)
1170 *offset_in_memory
+= type_memsize(type
, &align
);
1171 /* FIXME: is there a case where these two are different? */
1173 if (offset_in_buffer
)
1174 *offset_in_buffer
+= type_memsize(type
, &align
);
1177 return pointer_count
;
1180 /* Note: if file is NULL return value is number of pointers to write, else
1181 * it is the number of type format characters written */
1182 static int write_conformant_array_pointer_descriptions(
1183 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1184 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1185 unsigned int *typestring_offset
)
1188 int pointer_count
= 0;
1190 if (is_conformant_array(type
) && !type
->length_is
)
1192 unsigned int temp
= 0;
1193 /* unfortunately, this needs to be done in two passes to avoid
1194 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1195 pointer_count
= write_pointer_description_offsets(
1196 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1197 if (pointer_count
> 0)
1199 unsigned int increment_size
;
1200 size_t offset_of_array_pointer_mem
= 0;
1201 size_t offset_of_array_pointer_buf
= 0;
1204 increment_size
= type_memsize(type
->ref
, &align
);
1206 if (increment_size
> USHRT_MAX
)
1207 error("array size of %u bytes is too large\n", increment_size
);
1209 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1210 print_file(file
, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET
);
1211 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1212 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1213 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1214 *typestring_offset
+= 8;
1216 pointer_count
= write_pointer_description_offsets(
1217 file
, attrs
, type
->ref
, &offset_of_array_pointer_mem
,
1218 &offset_of_array_pointer_buf
, typestring_offset
);
1221 else if (is_struct(type
->type
))
1224 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1226 pointer_count
+= write_conformant_array_pointer_descriptions(
1227 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1234 if (offset_in_memory
)
1235 *offset_in_memory
+= type_memsize(type
, &align
);
1236 /* FIXME: is there a case where these two are different? */
1238 if (offset_in_buffer
)
1239 *offset_in_buffer
+= type_memsize(type
, &align
);
1242 return pointer_count
;
1245 /* Note: if file is NULL return value is number of pointers to write, else
1246 * it is the number of type format characters written */
1247 static int write_varying_array_pointer_descriptions(
1248 FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1249 size_t *offset_in_memory
, size_t *offset_in_buffer
,
1250 unsigned int *typestring_offset
)
1253 int pointer_count
= 0;
1255 /* FIXME: do varying array searching here, but pointer searching in write_pointer_description_offsets */
1257 if (is_array(type
) && type
->length_is
)
1259 unsigned int temp
= 0;
1260 /* unfortunately, this needs to be done in two passes to avoid
1261 * writing out redundant FC_VARIABLE_REPEAT descriptions */
1262 pointer_count
= write_pointer_description_offsets(
1263 NULL
, attrs
, type
->ref
, NULL
, NULL
, &temp
);
1264 if (pointer_count
> 0)
1266 unsigned int increment_size
;
1267 size_t offset_of_array_pointer_mem
= 0;
1268 size_t offset_of_array_pointer_buf
= 0;
1271 increment_size
= type_memsize(type
->ref
, &align
);
1273 if (increment_size
> USHRT_MAX
)
1274 error("array size of %u bytes is too large\n", increment_size
);
1276 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT
);
1277 print_file(file
, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET
);
1278 print_file(file
, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size
, increment_size
);
1279 print_file(file
, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory
, *offset_in_memory
);
1280 print_file(file
, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count
, pointer_count
);
1281 *typestring_offset
+= 8;
1283 pointer_count
= write_pointer_description_offsets(
1284 file
, attrs
, type
, &offset_of_array_pointer_mem
,
1285 &offset_of_array_pointer_buf
, typestring_offset
);
1288 else if (is_struct(type
->type
))
1291 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
1293 pointer_count
+= write_varying_array_pointer_descriptions(
1294 file
, v
->attrs
, v
->type
, offset_in_memory
, offset_in_buffer
,
1301 if (offset_in_memory
)
1302 *offset_in_memory
+= type_memsize(type
, &align
);
1303 /* FIXME: is there a case where these two are different? */
1305 if (offset_in_buffer
)
1306 *offset_in_buffer
+= type_memsize(type
, &align
);
1309 return pointer_count
;
1312 static void write_pointer_description(FILE *file
, type_t
*type
,
1313 unsigned int *typestring_offset
)
1315 size_t offset_in_buffer
;
1316 size_t offset_in_memory
;
1318 /* pass 1: search for single instance of a pointer (i.e. don't descend
1320 offset_in_memory
= 0;
1321 offset_in_buffer
= 0;
1322 write_no_repeat_pointer_descriptions(
1324 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1326 /* pass 2: search for pointers in fixed arrays */
1327 offset_in_memory
= 0;
1328 offset_in_buffer
= 0;
1329 write_fixed_array_pointer_descriptions(
1331 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1333 /* pass 3: search for pointers in conformant only arrays (but don't descend
1334 * into conformant varying or varying arrays) */
1335 offset_in_memory
= 0;
1336 offset_in_buffer
= 0;
1337 write_conformant_array_pointer_descriptions(
1339 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1341 /* pass 4: search for pointers in varying arrays */
1342 offset_in_memory
= 0;
1343 offset_in_buffer
= 0;
1344 write_varying_array_pointer_descriptions(
1346 &offset_in_memory
, &offset_in_buffer
, typestring_offset
);
1349 static int is_declptr(const type_t
*t
)
1351 return is_ptr(t
) || (is_conformant_array(t
) && !t
->declarray
);
1354 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
1356 const char *name
, unsigned int *typestring_offset
)
1358 size_t start_offset
= *typestring_offset
;
1359 unsigned char rtype
;
1361 update_tfsoff(type
, start_offset
, file
);
1363 if (is_declptr(type
))
1365 unsigned char flag
= is_conformant_array(type
) ? 0 : RPC_FC_P_SIMPLEPOINTER
;
1366 int pointer_type
= is_ptr(type
) ? type
->type
: get_attrv(attrs
, ATTR_POINTERTYPE
);
1368 pointer_type
= RPC_FC_RP
;
1369 print_file(file
, 2,"0x%x, 0x%x,\t/* %s%s */\n",
1370 pointer_type
, flag
, string_of_type(pointer_type
),
1371 flag
? " [simple_pointer]" : "");
1372 *typestring_offset
+= 2;
1375 print_file(file
, 2, "NdrFcShort(0x2),\n");
1376 *typestring_offset
+= 2;
1378 rtype
= type
->ref
->type
;
1381 rtype
= type
->ref
->type
;
1383 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
1385 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
1386 return start_offset
;
1389 if (type
->declarray
&& !is_conformant_array(type
))
1391 /* FIXME: multi-dimensional array */
1392 if (0xffffuL
< type
->dim
)
1393 error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
1394 name
, 0xffffu
, type
->dim
- 0xffffu
);
1396 if (rtype
== RPC_FC_CHAR
)
1397 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
1399 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
1400 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1401 *typestring_offset
+= 2;
1403 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", type
->dim
, type
->dim
);
1404 *typestring_offset
+= 2;
1406 return start_offset
;
1408 else if (type
->size_is
)
1410 unsigned int align
= 0;
1412 if (rtype
== RPC_FC_CHAR
)
1413 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1415 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1416 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
1417 *typestring_offset
+= 2;
1419 *typestring_offset
+= write_conf_or_var_desc(
1420 file
, current_structure
,
1421 (type
->declarray
&& current_structure
1422 ? type_memsize(current_structure
, &align
)
1426 return start_offset
;
1430 if (rtype
== RPC_FC_WCHAR
)
1431 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
1433 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
1434 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1435 *typestring_offset
+= 2;
1437 return start_offset
;
1441 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1442 const char *name
, unsigned int *typestring_offset
)
1444 const expr_t
*length_is
= type
->length_is
;
1445 const expr_t
*size_is
= type
->size_is
;
1446 unsigned int align
= 0;
1448 size_t start_offset
;
1450 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1452 pointer_type
= RPC_FC_RP
;
1454 has_pointer
= FALSE
;
1455 if (write_embedded_types(file
, attrs
, type
->ref
, name
, FALSE
, typestring_offset
))
1458 size
= type_memsize(type
, &align
);
1459 if (size
== 0) /* conformant array */
1460 size
= type_memsize(type
->ref
, &align
);
1462 start_offset
= *typestring_offset
;
1463 update_tfsoff(type
, start_offset
, file
);
1464 print_file(file
, 0, "/* %lu */\n", start_offset
);
1465 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1466 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1467 *typestring_offset
+= 2;
1470 if (type
->type
!= RPC_FC_BOGUS_ARRAY
)
1472 unsigned char tc
= type
->type
;
1473 unsigned int baseoff
1474 = type
->declarray
&& current_structure
1475 ? type_memsize(current_structure
, &align
)
1478 if (tc
== RPC_FC_LGFARRAY
|| tc
== RPC_FC_LGVARRAY
)
1480 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size
, size
);
1481 *typestring_offset
+= 4;
1485 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size
, size
);
1486 *typestring_offset
+= 2;
1489 if (is_conformant_array(type
))
1491 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1494 if (type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
)
1496 unsigned int elalign
= 0;
1497 size_t elsize
= type_memsize(type
->ref
, &elalign
);
1499 if (type
->type
== RPC_FC_LGVARRAY
)
1501 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1502 *typestring_offset
+= 4;
1506 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1507 *typestring_offset
+= 2;
1510 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize
, elsize
);
1511 *typestring_offset
+= 2;
1516 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1521 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1522 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1523 *typestring_offset
+= 2;
1524 write_pointer_description(file
, type
, typestring_offset
);
1525 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1526 *typestring_offset
+= 1;
1529 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1530 write_end(file
, typestring_offset
);
1533 error("%s: complex arrays unimplemented\n", name
);
1535 return start_offset
;
1538 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1540 const var_t
*last_field
= LIST_ENTRY( list_tail(type
->fields
), const var_t
, entry
);
1541 const type_t
*ft
= last_field
->type
;
1543 if (ft
->declarray
&& is_conformant_array(ft
))
1546 if (ft
->type
== RPC_FC_CSTRUCT
|| ft
->type
== RPC_FC_CPSTRUCT
|| ft
->type
== RPC_FC_CVSTRUCT
)
1547 return find_array_or_string_in_struct(last_field
->type
);
1552 static void write_struct_members(FILE *file
, const type_t
*type
,
1553 unsigned int *corroff
, unsigned int *typestring_offset
)
1556 unsigned short offset
= 0;
1560 if (type
->fields
) LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1562 type_t
*ft
= field
->type
;
1563 if (!ft
->declarray
|| !is_conformant_array(ft
))
1565 unsigned int align
= 0;
1566 size_t size
= type_memsize(ft
, &align
);
1569 if ((align
- 1) & offset
)
1571 unsigned char fc
= 0;
1575 fc
= RPC_FC_ALIGNM4
;
1578 fc
= RPC_FC_ALIGNM8
;
1581 error("write_struct_members: cannot align type %d", ft
->type
);
1583 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1584 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
1585 *typestring_offset
+= 1;
1587 write_member_type(file
, type
, field
->attrs
, field
->type
, corroff
,
1593 padding
= ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
1596 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1597 RPC_FC_STRUCTPAD1
+ padding
- 1,
1599 *typestring_offset
+= 1;
1602 write_end(file
, typestring_offset
);
1605 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1606 const char *name
, unsigned int *tfsoff
)
1608 const type_t
*save_current_structure
= current_structure
;
1609 unsigned int total_size
;
1611 size_t start_offset
;
1612 size_t array_offset
;
1613 int has_pointers
= 0;
1614 unsigned int align
= 0;
1615 unsigned int corroff
;
1619 current_structure
= type
;
1621 total_size
= type_memsize(type
, &align
);
1622 if (total_size
> USHRT_MAX
)
1623 error("structure size for %s exceeds %d bytes by %d bytes\n",
1624 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1626 if (type
->fields
) LIST_FOR_EACH_ENTRY(f
, type
->fields
, var_t
, entry
)
1627 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
1630 array
= find_array_or_string_in_struct(type
);
1631 if (array
&& !processed(array
->type
))
1633 = is_attr(array
->attrs
, ATTR_STRING
)
1634 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
1635 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
1638 write_descriptors(file
, type
, tfsoff
);
1640 start_offset
= *tfsoff
;
1641 update_tfsoff(type
, start_offset
, file
);
1642 print_file(file
, 0, "/* %d */\n", start_offset
);
1643 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1644 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1645 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1650 unsigned int absoff
= array
->type
->typestring_offset
;
1651 short reloff
= absoff
- *tfsoff
;
1652 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1653 reloff
, reloff
, absoff
);
1656 else if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1658 print_file(file
, 2, "NdrFcShort(0x0),\n");
1662 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1664 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1665 nothing is written to file yet. On the actual writing pass,
1666 this will have been updated. */
1667 short reloff
= type
->ptrdesc
- *tfsoff
;
1668 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1669 reloff
, reloff
, type
->ptrdesc
);
1672 else if ((type
->type
== RPC_FC_PSTRUCT
) ||
1673 (type
->type
== RPC_FC_CPSTRUCT
) ||
1674 (type
->type
== RPC_FC_CVSTRUCT
&& has_pointers
))
1676 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1677 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1679 write_pointer_description(file
, type
, tfsoff
);
1680 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1684 write_struct_members(file
, type
, &corroff
, tfsoff
);
1686 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1688 const var_list_t
*fs
= type
->fields
;
1691 type
->ptrdesc
= *tfsoff
;
1692 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, const var_t
, entry
)
1694 type_t
*ft
= f
->type
;
1696 write_pointer_tfs(file
, ft
, tfsoff
);
1700 current_structure
= save_current_structure
;
1701 return start_offset
;
1704 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1705 unsigned char flags
, size_t offset
,
1706 unsigned int *typeformat_offset
)
1708 size_t start_offset
= *typeformat_offset
;
1709 short reloff
= offset
- (*typeformat_offset
+ 2);
1710 int in_attr
, out_attr
;
1711 in_attr
= is_attr(attrs
, ATTR_IN
);
1712 out_attr
= is_attr(attrs
, ATTR_OUT
);
1713 if (!in_attr
&& !out_attr
) in_attr
= 1;
1715 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1718 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1721 string_of_type(pointer_type
));
1725 fprintf(file
, " [allocated_on_stack]");
1727 fprintf(file
, " [pointer_deref]");
1728 fprintf(file
, " */\n");
1731 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1732 *typeformat_offset
+= 4;
1734 return start_offset
;
1737 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1741 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
1743 else if (is_base_type(t
->type
))
1745 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1746 t
->type
, string_of_type(t
->type
));
1748 else if (t
->typestring_offset
)
1750 short reloff
= t
->typestring_offset
- *tfsoff
;
1751 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1752 reloff
, reloff
, t
->typestring_offset
);
1755 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1760 static size_t write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1762 unsigned int align
= 0;
1763 unsigned int start_offset
;
1764 size_t size
= type_memsize(type
, &align
);
1767 type_t
*deftype
= NULL
;
1768 short nodeftype
= 0xffff;
1773 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1775 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields
), const var_t
, entry
);
1776 fields
= uv
->type
->fields
;
1779 fields
= type
->fields
;
1781 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1783 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1785 nbranch
+= list_count(cases
);
1787 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
1790 start_offset
= *tfsoff
;
1791 update_tfsoff(type
, start_offset
, file
);
1792 print_file(file
, 0, "/* %d */\n", start_offset
);
1793 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1795 const var_t
*sv
= LIST_ENTRY(list_head(type
->fields
), const var_t
, entry
);
1796 const type_t
*st
= sv
->type
;
1809 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1810 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
1811 0x40 | st
->type
, string_of_type(st
->type
));
1815 error("union switch type must be an integer, char, or enum\n");
1818 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
1819 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
1822 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1824 type_t
*ft
= f
->type
;
1825 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1826 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
1829 if (cases
== NULL
&& !deflt
)
1830 error("union field %s with neither case nor default attribute\n", f
->name
);
1832 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
1834 /* MIDL doesn't check for duplicate cases, even though that seems
1835 like a reasonable thing to do, it just dumps them to the TFS
1836 like we're going to do here. */
1837 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
1839 write_branch_type(file
, ft
, tfsoff
);
1842 /* MIDL allows multiple default branches, even though that seems
1843 illogical, it just chooses the last one, which is what we will
1854 write_branch_type(file
, deftype
, tfsoff
);
1858 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
1862 return start_offset
;
1865 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1866 unsigned int *typeformat_offset
)
1869 size_t start_offset
= *typeformat_offset
;
1870 const var_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
1876 expr
.type
= EXPR_IDENTIFIER
;
1878 expr
.u
.sval
= iid
->name
;
1879 expr
.is_const
= FALSE
;
1880 print_file(file
, 2, "0x2f, /* FC_IP */\n");
1881 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
1882 *typeformat_offset
+= write_conf_or_var_desc(file
, NULL
, 0, &expr
) + 2;
1886 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
1887 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
1890 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
1892 update_tfsoff(type
, start_offset
, file
);
1893 print_file(file
, 0, "/* %d */\n", start_offset
);
1894 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
1895 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1896 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
1897 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
1898 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
1899 for (i
= 0; i
< 8; ++i
)
1900 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
1903 fprintf(file
, "\n");
1905 *typeformat_offset
+= 18;
1907 return start_offset
;
1910 static int get_ptr_attr(const type_t
*t
, int def_type
)
1914 int ptr_attr
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
1917 if (t
->kind
!= TKIND_ALIAS
)
1923 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
1924 type_t
*type
, const var_t
*var
,
1925 unsigned int *typeformat_offset
)
1930 if (is_user_type(type
))
1932 write_user_tfs(file
, type
, typeformat_offset
);
1933 return type
->typestring_offset
;
1936 if (type
== var
->type
) /* top-level pointers */
1938 int pointer_attr
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1939 if (pointer_attr
!= 0 && !is_ptr(type
) && !is_array(type
))
1940 error("'%s': pointer attribute applied to non-pointer type\n", var
->name
);
1942 if (pointer_attr
== 0)
1943 pointer_attr
= get_ptr_attr(type
, RPC_FC_RP
);
1945 pointer_type
= pointer_attr
;
1948 pointer_type
= get_ptr_attr(type
, RPC_FC_UP
);
1950 if ((last_ptr(type
) || last_array(type
)) && is_ptrchain_attr(var
, ATTR_STRING
))
1951 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1954 return write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1958 /* basic types don't need a type format string */
1959 if (is_base_type(type
->type
))
1965 case RPC_FC_PSTRUCT
:
1966 case RPC_FC_CSTRUCT
:
1967 case RPC_FC_CPSTRUCT
:
1968 case RPC_FC_CVSTRUCT
:
1969 case RPC_FC_BOGUS_STRUCT
:
1970 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
1971 case RPC_FC_ENCAPSULATED_UNION
:
1972 case RPC_FC_NON_ENCAPSULATED_UNION
:
1973 return write_union_tfs(file
, type
, typeformat_offset
);
1975 case RPC_FC_BIND_PRIMITIVE
:
1979 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
1982 else if (last_ptr(type
))
1984 size_t start_offset
= *typeformat_offset
;
1985 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1986 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1987 const type_t
*base
= type
->ref
;
1989 if (base
->type
== RPC_FC_IP
)
1991 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
1994 /* special case for pointers to base types */
1995 if (is_base_type(base
->type
))
1997 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1998 pointer_type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
1999 string_of_type(pointer_type
),
2000 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2001 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
2002 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2003 *typeformat_offset
+= 4;
2004 return start_offset
;
2008 assert(is_ptr(type
));
2010 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
2012 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2013 return write_pointer_only_tfs(file
, var
->attrs
, pointer_type
,
2014 !last_ptr(type
) ? 0x10 : 0,
2015 offset
, typeformat_offset
);
2018 static void set_tfswrite(type_t
*type
, int val
)
2020 while (type
->tfswrite
!= val
)
2022 type_t
*utype
= get_user_type(type
, NULL
);
2024 type
->tfswrite
= val
;
2027 set_tfswrite(utype
, val
);
2029 if (type
->kind
== TKIND_ALIAS
)
2031 else if (is_ptr(type
) || is_array(type
))
2038 LIST_FOR_EACH_ENTRY( v
, type
->fields
, var_t
, entry
)
2040 set_tfswrite(v
->type
, val
);
2048 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2049 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2053 if (is_user_type(type
))
2055 write_user_tfs(file
, type
, tfsoff
);
2057 else if (is_ptr(type
))
2059 type_t
*ref
= type
->ref
;
2061 if (ref
->type
== RPC_FC_IP
)
2063 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2067 if (!processed(ref
) && !is_base_type(ref
->type
))
2068 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2071 write_pointer_tfs(file
, type
, tfsoff
);
2076 else if (last_array(type
) && is_attr(attrs
, ATTR_STRING
))
2078 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2080 else if (type
->declarray
&& is_conformant_array(type
))
2081 ; /* conformant arrays and strings are handled specially */
2082 else if (is_array(type
))
2084 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2086 else if (is_struct(type
->type
))
2088 if (!processed(type
))
2089 write_struct_tfs(file
, type
, name
, tfsoff
);
2091 else if (is_union(type
->type
))
2093 if (!processed(type
))
2094 write_union_tfs(file
, type
, tfsoff
);
2096 else if (!is_base_type(type
->type
))
2097 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2103 static void set_all_tfswrite(const ifref_list_t
*ifaces
, int val
)
2105 const ifref_t
* iface
;
2110 LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2111 if (iface
->iface
->funcs
)
2112 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2114 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2115 set_tfswrite(var
->type
, val
);
2118 static size_t process_tfs(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
2121 const ifref_t
*iface
;
2122 unsigned int typeformat_offset
= 2;
2124 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2126 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2129 if (iface
->iface
->funcs
)
2132 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2134 if (is_local(func
->def
->attrs
)) continue;
2136 current_func
= func
;
2138 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2141 write_typeformatstring_var(
2142 file
, 2, func
, var
->type
, var
,
2143 &typeformat_offset
),
2149 return typeformat_offset
+ 1;
2153 void write_typeformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
2157 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2158 print_file(file
, indent
, "{\n");
2160 print_file(file
, indent
, "0,\n");
2161 print_file(file
, indent
, "{\n");
2163 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2165 set_all_tfswrite(ifaces
, TRUE
);
2166 process_tfs(file
, ifaces
, for_objects
);
2168 print_file(file
, indent
, "0x0\n");
2170 print_file(file
, indent
, "}\n");
2172 print_file(file
, indent
, "};\n");
2173 print_file(file
, indent
, "\n");
2176 static unsigned int get_required_buffer_size_type(
2177 const type_t
*type
, const char *name
, unsigned int *alignment
)
2182 if (is_user_type(type
))
2185 const type_t
*utype
= get_user_type(type
, &uname
);
2186 size
= get_required_buffer_size_type(utype
, uname
, alignment
);
2188 else if (!is_ptr(type
))
2212 case RPC_FC_ERROR_STATUS_T
:
2224 case RPC_FC_BIND_PRIMITIVE
:
2228 case RPC_FC_PSTRUCT
:
2231 if (!type
->fields
) return 0;
2232 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2234 unsigned int alignment
;
2235 size
+= get_required_buffer_size_type(field
->type
, field
->name
,
2242 if (is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
)
2243 size
= get_required_buffer_size_type( type
->ref
, name
, alignment
);
2246 case RPC_FC_SMFARRAY
:
2247 case RPC_FC_LGFARRAY
:
2248 size
= type
->dim
* get_required_buffer_size_type(type
->ref
, name
, alignment
);
2251 case RPC_FC_SMVARRAY
:
2252 case RPC_FC_LGVARRAY
:
2254 case RPC_FC_CVARRAY
:
2255 case RPC_FC_BOGUS_ARRAY
:
2260 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
2267 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2269 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2270 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2272 if (!in_attr
&& !out_attr
)
2277 if (pass
== PASS_OUT
)
2279 if (out_attr
&& is_ptr(var
->type
))
2281 type_t
*type
= var
->type
;
2283 if (type
->type
== RPC_FC_STRUCT
)
2286 unsigned int size
= 36;
2288 if (!type
->fields
) return size
;
2289 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2292 size
+= get_required_buffer_size_type(
2293 field
->type
, field
->name
, &align
);
2302 if ((!out_attr
|| in_attr
) && !var
->type
->size_is
2303 && !is_attr(var
->attrs
, ATTR_STRING
) && !var
->type
->declarray
)
2305 if (is_ptr(var
->type
))
2307 type_t
*type
= var
->type
;
2309 if (is_base_type(type
->type
))
2313 else if (type
->type
== RPC_FC_STRUCT
)
2315 unsigned int size
= 36;
2318 if (!type
->fields
) return size
;
2319 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2322 size
+= get_required_buffer_size_type(
2323 field
->type
, field
->name
, &align
);
2330 return get_required_buffer_size_type(var
->type
, var
->name
, alignment
);
2334 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2337 unsigned int total_size
= 0, alignment
;
2341 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2343 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2344 total_size
+= alignment
;
2348 if (pass
== PASS_OUT
&& !is_void(func
->def
->type
))
2350 total_size
+= get_required_buffer_size(func
->def
, &alignment
, PASS_RETURN
);
2351 total_size
+= alignment
;
2356 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2357 enum remoting_phase phase
,
2358 const var_t
*var
, unsigned int type_offset
)
2360 const char *function
;
2363 case PHASE_BUFFERSIZE
:
2364 function
= "BufferSize";
2367 function
= "Marshall";
2369 case PHASE_UNMARSHAL
:
2370 function
= "Unmarshall";
2380 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2382 print_file(file
, indent
, "&_StubMsg,\n");
2383 print_file(file
, indent
, "%s%s%s%s,\n",
2384 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
2385 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
2386 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
2388 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2389 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2390 if (phase
== PHASE_UNMARSHAL
)
2391 print_file(file
, indent
, "0);\n");
2395 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2396 enum pass pass
, const var_t
*var
,
2397 const char *varname
)
2399 type_t
*type
= var
->type
;
2401 unsigned int alignment
= 0;
2402 unsigned char rtype
;
2404 /* no work to do for other phases, buffer sizing is done elsewhere */
2405 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2408 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2432 case RPC_FC_ERROR_STATUS_T
:
2444 case RPC_FC_BIND_PRIMITIVE
:
2445 /* no marshalling needed */
2449 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2453 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2454 alignment
- 1, alignment
- 1);
2456 if (phase
== PHASE_MARSHAL
)
2458 print_file(file
, indent
, "*(");
2459 write_type(file
, is_ptr(type
) ? type
->ref
: type
, FALSE
, NULL
);
2461 fprintf(file
, " *)_StubMsg.Buffer = *");
2463 fprintf(file
, " *)_StubMsg.Buffer = ");
2464 fprintf(file
, varname
);
2465 fprintf(file
, ";\n");
2467 else if (phase
== PHASE_UNMARSHAL
)
2469 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2470 print_file(file
, indent
, "");
2472 print_file(file
, indent
, "*");
2473 fprintf(file
, varname
);
2474 if (pass
== PASS_IN
&& is_ptr(type
))
2475 fprintf(file
, " = (");
2477 fprintf(file
, " = *(");
2478 write_type(file
, is_ptr(type
) ? type
->ref
: type
, FALSE
, NULL
);
2479 fprintf(file
, " *)_StubMsg.Buffer;\n");
2482 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2483 write_type(file
, var
->type
, FALSE
, NULL
);
2484 fprintf(file
, ");\n");
2487 /* returns whether the MaxCount, Offset or ActualCount members need to be
2488 * filled in for the specified phase */
2489 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2491 return (phase
!= PHASE_UNMARSHAL
);
2494 static int needs_freeing(const attr_list_t
*attrs
, const type_t
*t
, int out
)
2499 && (t
->ref
->type
== RPC_FC_IP
2500 || is_ptr(t
->ref
))))
2501 || (out
&& (is_string_type(attrs
, t
)
2505 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
2506 enum pass pass
, enum remoting_phase phase
)
2508 int in_attr
, out_attr
, pointer_type
;
2514 if (phase
== PHASE_BUFFERSIZE
)
2516 unsigned int size
= get_function_buffer_size( func
, pass
);
2517 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
2520 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2522 const type_t
*type
= var
->type
;
2523 unsigned char rtype
;
2524 size_t start_offset
= type
->typestring_offset
;
2526 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2528 pointer_type
= RPC_FC_RP
;
2530 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2531 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2532 if (!in_attr
&& !out_attr
)
2535 if (phase
== PHASE_FREE
)
2537 if (!needs_freeing(var
->attrs
, type
, out_attr
))
2544 if (!in_attr
) continue;
2547 if (!out_attr
) continue;
2555 if (is_user_type(var
->type
))
2557 print_phase_function(file
, indent
, "UserMarshal", phase
, var
, start_offset
);
2559 else if (is_string_type(var
->attrs
, var
->type
))
2561 if (is_array(type
) && !is_conformant_array(type
))
2562 print_phase_function(file
, indent
, "NonConformantString", phase
, var
, start_offset
);
2565 if (type
->size_is
&& is_size_needed_for_phase(phase
))
2567 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2568 write_expr(file
, type
->size_is
, 1);
2569 fprintf(file
, ";\n");
2572 if ((phase
== PHASE_FREE
) || (pointer_type
== RPC_FC_UP
))
2573 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2575 print_phase_function(file
, indent
, "ConformantString", phase
, var
,
2576 start_offset
+ (type
->size_is
? 4 : 2));
2579 else if (is_array(type
))
2581 unsigned char tc
= type
->type
;
2582 const char *array_type
;
2584 if (tc
== RPC_FC_SMFARRAY
|| tc
== RPC_FC_LGFARRAY
)
2585 array_type
= "FixedArray";
2586 else if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
2588 if (is_size_needed_for_phase(phase
))
2590 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2591 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2592 write_expr(file
, type
->length_is
, 1);
2593 fprintf(file
, ";\n\n");
2595 array_type
= "VaryingArray";
2597 else if (tc
== RPC_FC_CARRAY
)
2599 if (is_size_needed_for_phase(phase
) && phase
!= PHASE_FREE
)
2601 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2602 write_expr(file
, type
->size_is
, 1);
2603 fprintf(file
, ";\n\n");
2605 array_type
= "ConformantArray";
2607 else if (tc
== RPC_FC_CVARRAY
)
2609 if (is_size_needed_for_phase(phase
))
2611 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2612 write_expr(file
, type
->size_is
, 1);
2613 fprintf(file
, ";\n");
2614 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2615 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2616 write_expr(file
, type
->length_is
, 1);
2617 fprintf(file
, ";\n\n");
2619 array_type
= "ConformantVaryingArray";
2622 array_type
= "ComplexArray";
2624 if (!in_attr
&& phase
== PHASE_FREE
)
2626 print_file(file
, indent
, "if (%s)\n", var
->name
);
2628 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2630 else if (phase
!= PHASE_FREE
)
2632 if (pointer_type
== RPC_FC_UP
)
2633 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2635 print_phase_function(file
, indent
, array_type
, phase
, var
, start_offset
);
2638 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2640 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2642 else if (!is_ptr(var
->type
))
2647 case RPC_FC_PSTRUCT
:
2648 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
);
2650 case RPC_FC_CSTRUCT
:
2651 case RPC_FC_CPSTRUCT
:
2652 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
, start_offset
);
2654 case RPC_FC_CVSTRUCT
:
2655 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
, start_offset
);
2657 case RPC_FC_BOGUS_STRUCT
:
2658 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
, start_offset
);
2661 if (is_base_type( var
->type
->ref
->type
))
2663 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2665 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2667 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2668 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2673 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2674 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2675 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2679 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2684 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2686 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2688 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2690 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2691 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2696 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2697 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2698 if (var
->type
->ref
->type
== RPC_FC_IP
)
2699 print_phase_function(file
, indent
, "InterfacePointer", phase
, var
, start_offset
);
2701 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2704 fprintf(file
, "\n");
2709 size_t get_size_procformatstring_var(const var_t
*var
)
2711 return write_procformatstring_var(NULL
, 0, var
, FALSE
);
2715 size_t get_size_procformatstring_func(const func_t
*func
)
2720 /* argument list size */
2722 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2723 size
+= get_size_procformatstring_var(var
);
2725 /* return value size */
2726 if (is_void(func
->def
->type
))
2727 size
+= 2; /* FC_END and FC_PAD */
2729 size
+= get_size_procformatstring_var(func
->def
);
2734 size_t get_size_procformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2736 const ifref_t
*iface
;
2740 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2742 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2745 if (iface
->iface
->funcs
)
2746 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2747 if (!is_local(func
->def
->attrs
))
2748 size
+= get_size_procformatstring_func( func
);
2753 size_t get_size_typeformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2755 set_all_tfswrite(ifaces
, FALSE
);
2756 return process_tfs(NULL
, ifaces
, for_objects
);
2759 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
2760 const var_list_t
*fields
, const char *structvar
)
2766 fprintf(h
, "%lu", e
->u
.lval
);
2769 fprintf(h
, "0x%lx", e
->u
.lval
);
2772 fprintf(h
, "%#.15g", e
->u
.dval
);
2774 case EXPR_TRUEFALSE
:
2776 fprintf(h
, "FALSE");
2780 case EXPR_IDENTIFIER
:
2783 LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2784 if (!strcmp(e
->u
.sval
, field
->name
))
2786 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
2790 if (&field
->entry
== fields
) error("no field found for identifier %s\n", e
->u
.sval
);
2795 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2799 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2803 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2807 write_type(h
, e
->u
.tref
, FALSE
, NULL
);
2809 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2812 fprintf(h
, "sizeof(");
2813 write_type(h
, e
->u
.tref
, FALSE
, NULL
);
2824 if (brackets
) fprintf(h
, "(");
2825 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2827 case EXPR_SHL
: fprintf(h
, " << "); break;
2828 case EXPR_SHR
: fprintf(h
, " >> "); break;
2829 case EXPR_MUL
: fprintf(h
, " * "); break;
2830 case EXPR_DIV
: fprintf(h
, " / "); break;
2831 case EXPR_ADD
: fprintf(h
, " + "); break;
2832 case EXPR_SUB
: fprintf(h
, " - "); break;
2833 case EXPR_AND
: fprintf(h
, " & "); break;
2834 case EXPR_OR
: fprintf(h
, " | "); break;
2837 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2838 if (brackets
) fprintf(h
, ")");
2841 if (brackets
) fprintf(h
, "(");
2842 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2844 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2846 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
2847 if (brackets
) fprintf(h
, ")");
2853 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
2855 int in_attr
, out_attr
;
2857 const var_t
*def
= func
->def
;
2860 /* declare return value '_RetVal' */
2861 if (!is_void(def
->type
))
2863 print_file(file
, indent
, "");
2864 write_type_left(file
, def
->type
);
2865 fprintf(file
, " _RetVal;\n");
2871 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2873 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2875 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2876 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2877 if (!out_attr
&& !in_attr
)
2880 if (!in_attr
&& !var
->type
->size_is
&& !is_string
)
2882 print_file(file
, indent
, "");
2883 write_type(file
, var
->type
->ref
, FALSE
, "_W%u", i
++);
2884 fprintf(file
, ";\n");
2887 print_file(file
, indent
, "");
2888 write_type_left(file
, var
->type
);
2890 if (var
->type
->declarray
) {
2891 fprintf(file
, "( *");
2892 write_name(file
, var
);
2893 fprintf(file
, " )");
2895 write_name(file
, var
);
2896 write_type_right(file
, var
->type
, FALSE
);
2897 fprintf(file
, ";\n");
2899 if (decl_indirect(var
->type
))
2900 print_file(file
, indent
, "void *_p_%s = &%s;\n",
2901 var
->name
, var
->name
);
2906 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
2908 int in_attr
, out_attr
;
2915 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2917 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2918 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2919 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2920 if (!out_attr
&& !in_attr
)
2925 print_file(file
, indent
, "");
2926 write_name(file
, var
);
2928 if (var
->type
->size_is
)
2930 unsigned int size
, align
= 0;
2931 type_t
*type
= var
->type
;
2933 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
2934 for ( ; type
->size_is
; type
= type
->ref
)
2936 write_expr(file
, type
->size_is
, TRUE
);
2937 fprintf(file
, " * ");
2939 size
= type_memsize(type
, &align
);
2940 fprintf(file
, "%u);\n", size
);
2942 else if (!is_string
)
2944 fprintf(file
, " = &_W%u;\n", i
);
2945 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
2946 print_file(file
, indent
, "_W%u = 0;\n", i
);
2954 fprintf(file
, "\n");
2958 int write_expr_eval_routines(FILE *file
, const char *iface
)
2960 static const char *var_name
= "pS";
2962 struct expr_eval_routine
*eval
;
2963 unsigned short callback_offset
= 0;
2965 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2967 const char *name
= eval
->structure
->name
;
2968 const var_list_t
*fields
= eval
->structure
->fields
;
2972 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2973 iface
, name
, callback_offset
);
2974 print_file(file
, 0, "{\n");
2975 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
2976 name
, var_name
, name
, type_memsize (eval
->structure
, &align
));
2977 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
2978 print_file(file
, 1, "pStubMsg->MaxCount = (unsigned long)");
2979 write_struct_expr(file
, eval
->expr
, 1, fields
, var_name
);
2980 fprintf(file
, ";\n");
2981 print_file(file
, 0, "}\n\n");
2987 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
2989 struct expr_eval_routine
*eval
;
2990 struct expr_eval_routine
*cursor
;
2991 unsigned short callback_offset
= 0;
2993 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2994 fprintf(file
, "{\n");
2996 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2998 const char *name
= eval
->structure
->name
;
2999 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
3001 list_remove(&eval
->entry
);
3005 fprintf(file
, "};\n\n");
3008 void write_user_quad_list(FILE *file
)
3012 if (list_empty(&user_type_list
))
3015 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3016 fprintf(file
, "{\n");
3017 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3019 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3020 print_file(file
, 1, "{\n");
3021 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3022 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3023 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3024 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3025 print_file(file
, 1, "}%s\n", sep
);
3027 fprintf(file
, "};\n\n");
3030 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3032 const struct str_list_entry_t
*endpoint
;
3035 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3036 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3037 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3039 print_file( f
, 1, "{ (const unsigned char *)\"" );
3040 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3042 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3045 if (!*p
) goto error
;
3046 if (p
[1] != '[') goto error
;
3048 fprintf( f
, "\", (const unsigned char *)\"" );
3049 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3051 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3054 if (*p
!= ']') goto error
;
3055 fprintf( f
, "\" },\n" );
3057 print_file( f
, 0, "};\n\n" );
3061 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);