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 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
);
1451 unsigned int baseoff
1452 = type
->declarray
&& current_structure
1453 ? type_memsize(current_structure
, &align
)
1457 pointer_type
= RPC_FC_RP
;
1459 has_pointer
= FALSE
;
1460 if (write_embedded_types(file
, attrs
, type
->ref
, name
, FALSE
, typestring_offset
))
1463 size
= type_memsize(type
, &align
);
1464 if (size
== 0) /* conformant array */
1465 size
= type_memsize(type
->ref
, &align
);
1467 start_offset
= *typestring_offset
;
1468 update_tfsoff(type
, start_offset
, file
);
1469 print_file(file
, 0, "/* %lu */\n", start_offset
);
1470 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1471 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1472 *typestring_offset
+= 2;
1475 if (type
->type
!= RPC_FC_BOGUS_ARRAY
)
1477 unsigned char tc
= type
->type
;
1479 if (tc
== RPC_FC_LGFARRAY
|| tc
== RPC_FC_LGVARRAY
)
1481 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size
, size
);
1482 *typestring_offset
+= 4;
1486 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size
, size
);
1487 *typestring_offset
+= 2;
1490 if (is_conformant_array(type
))
1492 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1495 if (type
->type
== RPC_FC_SMVARRAY
|| type
->type
== RPC_FC_LGVARRAY
)
1497 unsigned int elalign
= 0;
1498 size_t elsize
= type_memsize(type
->ref
, &elalign
);
1500 if (type
->type
== RPC_FC_LGVARRAY
)
1502 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1503 *typestring_offset
+= 4;
1507 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type
->dim
, type
->dim
);
1508 *typestring_offset
+= 2;
1511 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize
, elsize
);
1512 *typestring_offset
+= 2;
1517 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1522 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1523 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1524 *typestring_offset
+= 2;
1525 write_pointer_description(file
, type
, typestring_offset
);
1526 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1527 *typestring_offset
+= 1;
1530 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1531 write_end(file
, typestring_offset
);
1535 unsigned int dim
= size_is
? 0 : type
->dim
;
1536 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim
, dim
);
1537 *typestring_offset
+= 2;
1539 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1542 += write_conf_or_var_desc(file
, current_structure
, baseoff
,
1544 write_member_type(file
, type
, NULL
, type
->ref
, NULL
, typestring_offset
);
1545 write_end(file
, typestring_offset
);
1548 return start_offset
;
1551 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1553 const var_t
*last_field
= LIST_ENTRY( list_tail(type
->fields
), const var_t
, entry
);
1554 const type_t
*ft
= last_field
->type
;
1556 if (ft
->declarray
&& is_conformant_array(ft
))
1559 if (ft
->type
== RPC_FC_CSTRUCT
|| ft
->type
== RPC_FC_CPSTRUCT
|| ft
->type
== RPC_FC_CVSTRUCT
)
1560 return find_array_or_string_in_struct(last_field
->type
);
1565 static void write_struct_members(FILE *file
, const type_t
*type
,
1566 unsigned int *corroff
, unsigned int *typestring_offset
)
1569 unsigned short offset
= 0;
1573 if (type
->fields
) LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1575 type_t
*ft
= field
->type
;
1576 if (!ft
->declarray
|| !is_conformant_array(ft
))
1578 unsigned int align
= 0;
1579 size_t size
= type_memsize(ft
, &align
);
1582 if ((align
- 1) & offset
)
1584 unsigned char fc
= 0;
1588 fc
= RPC_FC_ALIGNM4
;
1591 fc
= RPC_FC_ALIGNM8
;
1594 error("write_struct_members: cannot align type %d", ft
->type
);
1596 print_file(file
, 2, "0x%x,\t/* %s */\n", fc
, string_of_type(fc
));
1597 offset
= (offset
+ (align
- 1)) & ~(align
- 1);
1598 *typestring_offset
+= 1;
1600 write_member_type(file
, type
, field
->attrs
, field
->type
, corroff
,
1606 padding
= ((offset
+ (salign
- 1)) & ~(salign
- 1)) - offset
;
1609 print_file(file
, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
1610 RPC_FC_STRUCTPAD1
+ padding
- 1,
1612 *typestring_offset
+= 1;
1615 write_end(file
, typestring_offset
);
1618 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1619 const char *name
, unsigned int *tfsoff
)
1621 const type_t
*save_current_structure
= current_structure
;
1622 unsigned int total_size
;
1624 size_t start_offset
;
1625 size_t array_offset
;
1626 int has_pointers
= 0;
1627 unsigned int align
= 0;
1628 unsigned int corroff
;
1632 current_structure
= type
;
1634 total_size
= type_memsize(type
, &align
);
1635 if (total_size
> USHRT_MAX
)
1636 error("structure size for %s exceeds %d bytes by %d bytes\n",
1637 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1639 if (type
->fields
) LIST_FOR_EACH_ENTRY(f
, type
->fields
, var_t
, entry
)
1640 has_pointers
|= write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
,
1643 array
= find_array_or_string_in_struct(type
);
1644 if (array
&& !processed(array
->type
))
1646 = is_attr(array
->attrs
, ATTR_STRING
)
1647 ? write_string_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
)
1648 : write_array_tfs(file
, array
->attrs
, array
->type
, array
->name
, tfsoff
);
1651 write_descriptors(file
, type
, tfsoff
);
1653 start_offset
= *tfsoff
;
1654 update_tfsoff(type
, start_offset
, file
);
1655 print_file(file
, 0, "/* %d */\n", start_offset
);
1656 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1657 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1658 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1663 unsigned int absoff
= array
->type
->typestring_offset
;
1664 short reloff
= absoff
- *tfsoff
;
1665 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1666 reloff
, reloff
, absoff
);
1669 else if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1671 print_file(file
, 2, "NdrFcShort(0x0),\n");
1675 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1677 /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
1678 nothing is written to file yet. On the actual writing pass,
1679 this will have been updated. */
1680 short reloff
= type
->ptrdesc
- *tfsoff
;
1681 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1682 reloff
, reloff
, type
->ptrdesc
);
1685 else if ((type
->type
== RPC_FC_PSTRUCT
) ||
1686 (type
->type
== RPC_FC_CPSTRUCT
) ||
1687 (type
->type
== RPC_FC_CVSTRUCT
&& has_pointers
))
1689 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1690 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1692 write_pointer_description(file
, type
, tfsoff
);
1693 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1697 write_struct_members(file
, type
, &corroff
, tfsoff
);
1699 if (type
->type
== RPC_FC_BOGUS_STRUCT
)
1701 const var_list_t
*fs
= type
->fields
;
1704 type
->ptrdesc
= *tfsoff
;
1705 if (fs
) LIST_FOR_EACH_ENTRY(f
, fs
, const var_t
, entry
)
1707 type_t
*ft
= f
->type
;
1709 write_pointer_tfs(file
, ft
, tfsoff
);
1713 current_structure
= save_current_structure
;
1714 return start_offset
;
1717 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1718 unsigned char flags
, size_t offset
,
1719 unsigned int *typeformat_offset
)
1721 size_t start_offset
= *typeformat_offset
;
1722 short reloff
= offset
- (*typeformat_offset
+ 2);
1723 int in_attr
, out_attr
;
1724 in_attr
= is_attr(attrs
, ATTR_IN
);
1725 out_attr
= is_attr(attrs
, ATTR_OUT
);
1726 if (!in_attr
&& !out_attr
) in_attr
= 1;
1728 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1731 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1734 string_of_type(pointer_type
));
1738 fprintf(file
, " [allocated_on_stack]");
1740 fprintf(file
, " [pointer_deref]");
1741 fprintf(file
, " */\n");
1744 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1745 *typeformat_offset
+= 4;
1747 return start_offset
;
1750 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1754 print_file(file
, 2, "NdrFcShort(0x0),\t/* No type */\n");
1756 else if (is_base_type(t
->type
))
1758 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1759 t
->type
, string_of_type(t
->type
));
1761 else if (t
->typestring_offset
)
1763 short reloff
= t
->typestring_offset
- *tfsoff
;
1764 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1765 reloff
, reloff
, t
->typestring_offset
);
1768 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1773 static size_t write_union_tfs(FILE *file
, type_t
*type
, unsigned int *tfsoff
)
1775 unsigned int align
= 0;
1776 unsigned int start_offset
;
1777 size_t size
= type_memsize(type
, &align
);
1780 type_t
*deftype
= NULL
;
1781 short nodeftype
= 0xffff;
1786 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1788 const var_t
*uv
= LIST_ENTRY(list_tail(type
->fields
), const var_t
, entry
);
1789 fields
= uv
->type
->fields
;
1792 fields
= type
->fields
;
1794 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1796 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1798 nbranch
+= list_count(cases
);
1800 write_embedded_types(file
, f
->attrs
, f
->type
, f
->name
, TRUE
, tfsoff
);
1803 start_offset
= *tfsoff
;
1804 update_tfsoff(type
, start_offset
, file
);
1805 print_file(file
, 0, "/* %d */\n", start_offset
);
1806 if (type
->type
== RPC_FC_ENCAPSULATED_UNION
)
1808 const var_t
*sv
= LIST_ENTRY(list_head(type
->fields
), const var_t
, entry
);
1809 const type_t
*st
= sv
->type
;
1822 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1823 print_file(file
, 2, "0x%x,\t/* Switch type= %s */\n",
1824 0x40 | st
->type
, string_of_type(st
->type
));
1828 error("union switch type must be an integer, char, or enum\n");
1831 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
1832 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
1835 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1837 type_t
*ft
= f
->type
;
1838 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1839 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
1842 if (cases
== NULL
&& !deflt
)
1843 error("union field %s with neither case nor default attribute\n", f
->name
);
1845 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
1847 /* MIDL doesn't check for duplicate cases, even though that seems
1848 like a reasonable thing to do, it just dumps them to the TFS
1849 like we're going to do here. */
1850 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
1852 write_branch_type(file
, ft
, tfsoff
);
1855 /* MIDL allows multiple default branches, even though that seems
1856 illogical, it just chooses the last one, which is what we will
1867 write_branch_type(file
, deftype
, tfsoff
);
1871 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
1875 return start_offset
;
1878 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1879 unsigned int *typeformat_offset
)
1882 size_t start_offset
= *typeformat_offset
;
1883 const var_t
*iid
= get_attrp(attrs
, ATTR_IIDIS
);
1889 expr
.type
= EXPR_IDENTIFIER
;
1891 expr
.u
.sval
= iid
->name
;
1892 expr
.is_const
= FALSE
;
1893 print_file(file
, 2, "0x2f, /* FC_IP */\n");
1894 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
1895 *typeformat_offset
+= write_conf_or_var_desc(file
, NULL
, 0, &expr
) + 2;
1899 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
1900 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
1903 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
1905 update_tfsoff(type
, start_offset
, file
);
1906 print_file(file
, 0, "/* %d */\n", start_offset
);
1907 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
1908 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1909 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
1910 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
1911 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
1912 for (i
= 0; i
< 8; ++i
)
1913 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
1916 fprintf(file
, "\n");
1918 *typeformat_offset
+= 18;
1920 return start_offset
;
1923 static int get_ptr_attr(const type_t
*t
, int def_type
)
1927 int ptr_attr
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
1930 if (t
->kind
!= TKIND_ALIAS
)
1936 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
1937 type_t
*type
, const var_t
*var
,
1938 unsigned int *typeformat_offset
)
1943 if (is_user_type(type
))
1945 write_user_tfs(file
, type
, typeformat_offset
);
1946 return type
->typestring_offset
;
1949 if (type
== var
->type
) /* top-level pointers */
1951 int pointer_attr
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1952 if (pointer_attr
!= 0 && !is_ptr(type
) && !is_array(type
))
1953 error("'%s': pointer attribute applied to non-pointer type\n", var
->name
);
1955 if (pointer_attr
== 0)
1956 pointer_attr
= get_ptr_attr(type
, RPC_FC_RP
);
1958 pointer_type
= pointer_attr
;
1961 pointer_type
= get_ptr_attr(type
, RPC_FC_UP
);
1963 if ((last_ptr(type
) || last_array(type
)) && is_ptrchain_attr(var
, ATTR_STRING
))
1964 return write_string_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1967 return write_array_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1971 /* basic types don't need a type format string */
1972 if (is_base_type(type
->type
))
1978 case RPC_FC_PSTRUCT
:
1979 case RPC_FC_CSTRUCT
:
1980 case RPC_FC_CPSTRUCT
:
1981 case RPC_FC_CVSTRUCT
:
1982 case RPC_FC_BOGUS_STRUCT
:
1983 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
1984 case RPC_FC_ENCAPSULATED_UNION
:
1985 case RPC_FC_NON_ENCAPSULATED_UNION
:
1986 return write_union_tfs(file
, type
, typeformat_offset
);
1988 case RPC_FC_BIND_PRIMITIVE
:
1992 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
1995 else if (last_ptr(type
))
1997 size_t start_offset
= *typeformat_offset
;
1998 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1999 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2000 const type_t
*base
= type
->ref
;
2002 if (base
->type
== RPC_FC_IP
)
2004 return write_ip_tfs(file
, var
->attrs
, type
, typeformat_offset
);
2007 /* special case for pointers to base types */
2008 if (is_base_type(base
->type
))
2010 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
2011 pointer_type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
2012 string_of_type(pointer_type
),
2013 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
2014 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
2015 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
2016 *typeformat_offset
+= 4;
2017 return start_offset
;
2021 assert(is_ptr(type
));
2023 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
2025 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
2026 return write_pointer_only_tfs(file
, var
->attrs
, pointer_type
,
2027 !last_ptr(type
) ? 0x10 : 0,
2028 offset
, typeformat_offset
);
2031 static void set_tfswrite(type_t
*type
, int val
)
2033 while (type
->tfswrite
!= val
)
2035 type_t
*utype
= get_user_type(type
, NULL
);
2037 type
->tfswrite
= val
;
2040 set_tfswrite(utype
, val
);
2042 if (type
->kind
== TKIND_ALIAS
)
2044 else if (is_ptr(type
) || is_array(type
))
2051 LIST_FOR_EACH_ENTRY( v
, type
->fields
, var_t
, entry
)
2053 set_tfswrite(v
->type
, val
);
2061 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
2062 const char *name
, int write_ptr
, unsigned int *tfsoff
)
2066 if (is_user_type(type
))
2068 write_user_tfs(file
, type
, tfsoff
);
2070 else if (is_ptr(type
))
2072 type_t
*ref
= type
->ref
;
2074 if (ref
->type
== RPC_FC_IP
)
2076 write_ip_tfs(file
, attrs
, type
, tfsoff
);
2080 if (!processed(ref
) && !is_base_type(ref
->type
))
2081 retmask
|= write_embedded_types(file
, NULL
, ref
, name
, TRUE
, tfsoff
);
2084 write_pointer_tfs(file
, type
, tfsoff
);
2089 else if (last_array(type
) && is_attr(attrs
, ATTR_STRING
))
2091 write_string_tfs(file
, attrs
, type
, name
, tfsoff
);
2093 else if (type
->declarray
&& is_conformant_array(type
))
2094 ; /* conformant arrays and strings are handled specially */
2095 else if (is_array(type
))
2097 write_array_tfs(file
, attrs
, type
, name
, tfsoff
);
2099 else if (is_struct(type
->type
))
2101 if (!processed(type
))
2102 write_struct_tfs(file
, type
, name
, tfsoff
);
2104 else if (is_union(type
->type
))
2106 if (!processed(type
))
2107 write_union_tfs(file
, type
, tfsoff
);
2109 else if (!is_base_type(type
->type
))
2110 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
2116 static void set_all_tfswrite(const ifref_list_t
*ifaces
, int val
)
2118 const ifref_t
* iface
;
2123 LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2124 if (iface
->iface
->funcs
)
2125 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2127 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2128 set_tfswrite(var
->type
, val
);
2131 static size_t process_tfs(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
2134 const ifref_t
*iface
;
2135 unsigned int typeformat_offset
= 2;
2137 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2139 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2142 if (iface
->iface
->funcs
)
2145 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2147 if (is_local(func
->def
->attrs
)) continue;
2149 current_func
= func
;
2151 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2154 write_typeformatstring_var(
2155 file
, 2, func
, var
->type
, var
,
2156 &typeformat_offset
),
2162 return typeformat_offset
+ 1;
2166 void write_typeformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
2170 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
2171 print_file(file
, indent
, "{\n");
2173 print_file(file
, indent
, "0,\n");
2174 print_file(file
, indent
, "{\n");
2176 print_file(file
, indent
, "NdrFcShort(0x0),\n");
2178 set_all_tfswrite(ifaces
, TRUE
);
2179 process_tfs(file
, ifaces
, for_objects
);
2181 print_file(file
, indent
, "0x0\n");
2183 print_file(file
, indent
, "}\n");
2185 print_file(file
, indent
, "};\n");
2186 print_file(file
, indent
, "\n");
2189 static unsigned int get_required_buffer_size_type(
2190 const type_t
*type
, const char *name
, unsigned int *alignment
)
2193 if (is_user_type(type
))
2196 const type_t
*utype
= get_user_type(type
, &uname
);
2197 return get_required_buffer_size_type(utype
, uname
, alignment
);
2221 case RPC_FC_ERROR_STATUS_T
:
2231 case RPC_FC_BIND_PRIMITIVE
:
2235 case RPC_FC_PSTRUCT
:
2239 if (!type
->fields
) return 0;
2240 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2242 unsigned int alignment
;
2243 size
+= get_required_buffer_size_type(field
->type
, field
->name
,
2251 is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
2252 ? get_required_buffer_size_type( type
->ref
, name
, alignment
)
2255 case RPC_FC_SMFARRAY
:
2256 case RPC_FC_LGFARRAY
:
2257 return type
->dim
* get_required_buffer_size_type(type
->ref
, name
, alignment
);
2265 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
2267 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2268 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2270 if (!in_attr
&& !out_attr
)
2275 if (pass
== PASS_OUT
)
2277 if (out_attr
&& is_ptr(var
->type
))
2279 type_t
*type
= var
->type
;
2281 if (type
->type
== RPC_FC_STRUCT
)
2284 unsigned int size
= 36;
2286 if (!type
->fields
) return size
;
2287 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2290 size
+= get_required_buffer_size_type(
2291 field
->type
, field
->name
, &align
);
2300 if ((!out_attr
|| in_attr
) && !var
->type
->size_is
2301 && !is_attr(var
->attrs
, ATTR_STRING
) && !var
->type
->declarray
)
2303 if (is_ptr(var
->type
))
2305 type_t
*type
= var
->type
;
2307 if (is_base_type(type
->type
))
2311 else if (type
->type
== RPC_FC_STRUCT
)
2313 unsigned int size
= 36;
2316 if (!type
->fields
) return size
;
2317 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2320 size
+= get_required_buffer_size_type(
2321 field
->type
, field
->name
, &align
);
2328 return get_required_buffer_size_type(var
->type
, var
->name
, alignment
);
2332 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2335 unsigned int total_size
= 0, alignment
;
2339 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2341 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2342 total_size
+= alignment
;
2346 if (pass
== PASS_OUT
&& !is_void(func
->def
->type
))
2348 total_size
+= get_required_buffer_size(func
->def
, &alignment
, PASS_RETURN
);
2349 total_size
+= alignment
;
2354 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2355 enum remoting_phase phase
,
2356 const var_t
*var
, unsigned int type_offset
)
2358 const char *function
;
2361 case PHASE_BUFFERSIZE
:
2362 function
= "BufferSize";
2365 function
= "Marshall";
2367 case PHASE_UNMARSHAL
:
2368 function
= "Unmarshall";
2378 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2380 print_file(file
, indent
, "&_StubMsg,\n");
2381 print_file(file
, indent
, "%s%s%s%s,\n",
2382 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)" : "(unsigned char *)",
2383 (phase
== PHASE_UNMARSHAL
|| decl_indirect(var
->type
)) ? "&" : "",
2384 (phase
== PHASE_UNMARSHAL
&& decl_indirect(var
->type
)) ? "_p_" : "",
2386 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2387 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2388 if (phase
== PHASE_UNMARSHAL
)
2389 print_file(file
, indent
, "0);\n");
2393 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2394 enum pass pass
, const var_t
*var
,
2395 const char *varname
)
2397 type_t
*type
= var
->type
;
2399 unsigned int alignment
= 0;
2400 unsigned char rtype
;
2402 /* no work to do for other phases, buffer sizing is done elsewhere */
2403 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2406 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2430 case RPC_FC_ERROR_STATUS_T
:
2442 case RPC_FC_BIND_PRIMITIVE
:
2443 /* no marshalling needed */
2447 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2451 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2452 alignment
- 1, alignment
- 1);
2454 if (phase
== PHASE_MARSHAL
)
2456 print_file(file
, indent
, "*(");
2457 write_type(file
, is_ptr(type
) ? type
->ref
: type
, FALSE
, NULL
);
2459 fprintf(file
, " *)_StubMsg.Buffer = *");
2461 fprintf(file
, " *)_StubMsg.Buffer = ");
2462 fprintf(file
, varname
);
2463 fprintf(file
, ";\n");
2465 else if (phase
== PHASE_UNMARSHAL
)
2467 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2468 print_file(file
, indent
, "");
2470 print_file(file
, indent
, "*");
2471 fprintf(file
, varname
);
2472 if (pass
== PASS_IN
&& is_ptr(type
))
2473 fprintf(file
, " = (");
2475 fprintf(file
, " = *(");
2476 write_type(file
, is_ptr(type
) ? type
->ref
: type
, FALSE
, NULL
);
2477 fprintf(file
, " *)_StubMsg.Buffer;\n");
2480 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2481 write_type(file
, var
->type
, FALSE
, NULL
);
2482 fprintf(file
, ");\n");
2485 /* returns whether the MaxCount, Offset or ActualCount members need to be
2486 * filled in for the specified phase */
2487 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2489 return (phase
!= PHASE_UNMARSHAL
);
2492 static int needs_freeing(const attr_list_t
*attrs
, const type_t
*t
, int out
)
2497 && (t
->ref
->type
== RPC_FC_IP
2498 || is_ptr(t
->ref
))))
2499 || (out
&& (is_string_type(attrs
, t
)
2503 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
2504 enum pass pass
, enum remoting_phase phase
)
2506 int in_attr
, out_attr
, pointer_type
;
2512 if (phase
== PHASE_BUFFERSIZE
)
2514 unsigned int size
= get_function_buffer_size( func
, pass
);
2515 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
2518 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2520 const type_t
*type
= var
->type
;
2521 unsigned char rtype
;
2522 size_t start_offset
= type
->typestring_offset
;
2524 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2526 pointer_type
= RPC_FC_RP
;
2528 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2529 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2530 if (!in_attr
&& !out_attr
)
2533 if (phase
== PHASE_FREE
)
2535 if (!needs_freeing(var
->attrs
, type
, out_attr
))
2542 if (!in_attr
) continue;
2545 if (!out_attr
) continue;
2553 if (is_user_type(var
->type
))
2555 print_phase_function(file
, indent
, "UserMarshal", phase
, var
, start_offset
);
2557 else if (is_string_type(var
->attrs
, var
->type
))
2559 if (is_array(type
) && !is_conformant_array(type
))
2560 print_phase_function(file
, indent
, "NonConformantString", phase
, var
, start_offset
);
2563 if (type
->size_is
&& is_size_needed_for_phase(phase
))
2565 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2566 write_expr(file
, type
->size_is
, 1);
2567 fprintf(file
, ";\n");
2570 if ((phase
== PHASE_FREE
) || (pointer_type
== RPC_FC_UP
))
2571 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2573 print_phase_function(file
, indent
, "ConformantString", phase
, var
,
2574 start_offset
+ (type
->size_is
? 4 : 2));
2577 else if (is_array(type
))
2579 unsigned char tc
= type
->type
;
2580 const char *array_type
;
2582 if (tc
== RPC_FC_SMFARRAY
|| tc
== RPC_FC_LGFARRAY
)
2583 array_type
= "FixedArray";
2584 else if (tc
== RPC_FC_SMVARRAY
|| tc
== RPC_FC_LGVARRAY
)
2586 if (is_size_needed_for_phase(phase
))
2588 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2589 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2590 write_expr(file
, type
->length_is
, 1);
2591 fprintf(file
, ";\n\n");
2593 array_type
= "VaryingArray";
2595 else if (tc
== RPC_FC_CARRAY
)
2597 if (is_size_needed_for_phase(phase
) && phase
!= PHASE_FREE
)
2599 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2600 write_expr(file
, type
->size_is
, 1);
2601 fprintf(file
, ";\n\n");
2603 array_type
= "ConformantArray";
2605 else if (tc
== RPC_FC_CVARRAY
)
2607 if (is_size_needed_for_phase(phase
))
2609 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2610 write_expr(file
, type
->size_is
, 1);
2611 fprintf(file
, ";\n");
2612 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2613 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2614 write_expr(file
, type
->length_is
, 1);
2615 fprintf(file
, ";\n\n");
2617 array_type
= "ConformantVaryingArray";
2620 array_type
= "ComplexArray";
2622 if (!in_attr
&& phase
== PHASE_FREE
)
2624 print_file(file
, indent
, "if (%s)\n", var
->name
);
2626 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2628 else if (phase
!= PHASE_FREE
)
2630 if (pointer_type
== RPC_FC_UP
)
2631 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2633 print_phase_function(file
, indent
, array_type
, phase
, var
, start_offset
);
2636 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2638 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2640 else if (!is_ptr(var
->type
))
2645 case RPC_FC_PSTRUCT
:
2646 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
);
2648 case RPC_FC_CSTRUCT
:
2649 case RPC_FC_CPSTRUCT
:
2650 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
, start_offset
);
2652 case RPC_FC_CVSTRUCT
:
2653 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
, start_offset
);
2655 case RPC_FC_BOGUS_STRUCT
:
2656 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
, start_offset
);
2659 if (is_base_type( var
->type
->ref
->type
))
2661 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2663 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2665 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2666 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2671 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2672 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2673 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2677 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2682 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2684 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2686 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2688 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2689 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
, start_offset
+ 4);
2694 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2695 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2696 if (var
->type
->ref
->type
== RPC_FC_IP
)
2697 print_phase_function(file
, indent
, "InterfacePointer", phase
, var
, start_offset
);
2699 print_phase_function(file
, indent
, "Pointer", phase
, var
, start_offset
);
2702 fprintf(file
, "\n");
2707 size_t get_size_procformatstring_var(const var_t
*var
)
2709 return write_procformatstring_var(NULL
, 0, var
, FALSE
);
2713 size_t get_size_procformatstring_func(const func_t
*func
)
2718 /* argument list size */
2720 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2721 size
+= get_size_procformatstring_var(var
);
2723 /* return value size */
2724 if (is_void(func
->def
->type
))
2725 size
+= 2; /* FC_END and FC_PAD */
2727 size
+= get_size_procformatstring_var(func
->def
);
2732 size_t get_size_procformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2734 const ifref_t
*iface
;
2738 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2740 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2743 if (iface
->iface
->funcs
)
2744 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2745 if (!is_local(func
->def
->attrs
))
2746 size
+= get_size_procformatstring_func( func
);
2751 size_t get_size_typeformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2753 set_all_tfswrite(ifaces
, FALSE
);
2754 return process_tfs(NULL
, ifaces
, for_objects
);
2757 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
2758 const var_list_t
*fields
, const char *structvar
)
2764 fprintf(h
, "%lu", e
->u
.lval
);
2767 fprintf(h
, "0x%lx", e
->u
.lval
);
2770 fprintf(h
, "%#.15g", e
->u
.dval
);
2772 case EXPR_TRUEFALSE
:
2774 fprintf(h
, "FALSE");
2778 case EXPR_IDENTIFIER
:
2781 LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2782 if (!strcmp(e
->u
.sval
, field
->name
))
2784 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
2788 if (&field
->entry
== fields
) error("no field found for identifier %s\n", e
->u
.sval
);
2793 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2797 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2801 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2805 write_type(h
, e
->u
.tref
, FALSE
, NULL
);
2807 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2810 fprintf(h
, "sizeof(");
2811 write_type(h
, e
->u
.tref
, FALSE
, NULL
);
2822 if (brackets
) fprintf(h
, "(");
2823 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2825 case EXPR_SHL
: fprintf(h
, " << "); break;
2826 case EXPR_SHR
: fprintf(h
, " >> "); break;
2827 case EXPR_MUL
: fprintf(h
, " * "); break;
2828 case EXPR_DIV
: fprintf(h
, " / "); break;
2829 case EXPR_ADD
: fprintf(h
, " + "); break;
2830 case EXPR_SUB
: fprintf(h
, " - "); break;
2831 case EXPR_AND
: fprintf(h
, " & "); break;
2832 case EXPR_OR
: fprintf(h
, " | "); break;
2835 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2836 if (brackets
) fprintf(h
, ")");
2839 if (brackets
) fprintf(h
, "(");
2840 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2842 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2844 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
2845 if (brackets
) fprintf(h
, ")");
2851 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
2853 int in_attr
, out_attr
;
2855 const var_t
*def
= func
->def
;
2858 /* declare return value '_RetVal' */
2859 if (!is_void(def
->type
))
2861 print_file(file
, indent
, "");
2862 write_type_left(file
, def
->type
);
2863 fprintf(file
, " _RetVal;\n");
2869 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2871 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2873 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2874 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2875 if (!out_attr
&& !in_attr
)
2878 if (!in_attr
&& !var
->type
->size_is
&& !is_string
)
2880 print_file(file
, indent
, "");
2881 write_type(file
, var
->type
->ref
, FALSE
, "_W%u", i
++);
2882 fprintf(file
, ";\n");
2885 print_file(file
, indent
, "");
2886 write_type_left(file
, var
->type
);
2888 if (var
->type
->declarray
) {
2889 fprintf(file
, "( *");
2890 write_name(file
, var
);
2891 fprintf(file
, " )");
2893 write_name(file
, var
);
2894 write_type_right(file
, var
->type
, FALSE
);
2895 fprintf(file
, ";\n");
2897 if (decl_indirect(var
->type
))
2898 print_file(file
, indent
, "void *_p_%s = &%s;\n",
2899 var
->name
, var
->name
);
2904 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
2906 int in_attr
, out_attr
;
2913 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2915 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2916 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2917 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2918 if (!out_attr
&& !in_attr
)
2923 print_file(file
, indent
, "");
2924 write_name(file
, var
);
2926 if (var
->type
->size_is
)
2928 unsigned int size
, align
= 0;
2929 type_t
*type
= var
->type
;
2931 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
2932 for ( ; type
->size_is
; type
= type
->ref
)
2934 write_expr(file
, type
->size_is
, TRUE
);
2935 fprintf(file
, " * ");
2937 size
= type_memsize(type
, &align
);
2938 fprintf(file
, "%u);\n", size
);
2940 else if (!is_string
)
2942 fprintf(file
, " = &_W%u;\n", i
);
2943 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
2944 print_file(file
, indent
, "_W%u = 0;\n", i
);
2952 fprintf(file
, "\n");
2956 int write_expr_eval_routines(FILE *file
, const char *iface
)
2958 static const char *var_name
= "pS";
2960 struct expr_eval_routine
*eval
;
2961 unsigned short callback_offset
= 0;
2963 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2965 const char *name
= eval
->structure
->name
;
2966 const var_list_t
*fields
= eval
->structure
->fields
;
2970 print_file(file
, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2971 iface
, name
, callback_offset
);
2972 print_file(file
, 0, "{\n");
2973 print_file (file
, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
2974 name
, var_name
, name
, type_memsize (eval
->structure
, &align
));
2975 print_file(file
, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
2976 print_file(file
, 1, "pStubMsg->MaxCount = (unsigned long)");
2977 write_struct_expr(file
, eval
->expr
, 1, fields
, var_name
);
2978 fprintf(file
, ";\n");
2979 print_file(file
, 0, "}\n\n");
2985 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
2987 struct expr_eval_routine
*eval
;
2988 struct expr_eval_routine
*cursor
;
2989 unsigned short callback_offset
= 0;
2991 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2992 fprintf(file
, "{\n");
2994 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2996 const char *name
= eval
->structure
->name
;
2997 print_file(file
, 1, "%s_%sExprEval_%04u,\n", iface
, name
, callback_offset
);
2999 list_remove(&eval
->entry
);
3003 fprintf(file
, "};\n\n");
3006 void write_user_quad_list(FILE *file
)
3010 if (list_empty(&user_type_list
))
3013 fprintf(file
, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
3014 fprintf(file
, "{\n");
3015 LIST_FOR_EACH_ENTRY(ut
, &user_type_list
, user_type_t
, entry
)
3017 const char *sep
= &ut
->entry
== list_tail(&user_type_list
) ? "" : ",";
3018 print_file(file
, 1, "{\n");
3019 print_file(file
, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut
->name
);
3020 print_file(file
, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut
->name
);
3021 print_file(file
, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut
->name
);
3022 print_file(file
, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut
->name
);
3023 print_file(file
, 1, "}%s\n", sep
);
3025 fprintf(file
, "};\n\n");
3028 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
3030 const struct str_list_entry_t
*endpoint
;
3033 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
3034 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
3035 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
3037 print_file( f
, 1, "{ (const unsigned char *)\"" );
3038 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
3040 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3043 if (!*p
) goto error
;
3044 if (p
[1] != '[') goto error
;
3046 fprintf( f
, "\", (const unsigned char *)\"" );
3047 for (p
+= 2; *p
&& *p
!= ']'; p
++)
3049 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
3052 if (*p
!= ']') goto error
;
3053 fprintf( f
, "\" },\n" );
3055 print_file( f
, 0, "};\n\n" );
3059 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);