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 /* name of the structure variable for structure callbacks */
50 #define STRUCT_EXPR_EVAL_VAR "pS"
52 static struct list expr_eval_routines
= LIST_INIT(expr_eval_routines
);
54 struct expr_eval_routine
57 const type_t
*structure
;
58 size_t structure_size
;
62 static size_t type_memsize(const type_t
*t
, const array_dims_t
*array
, unsigned int *align
);
63 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
);
64 static size_t write_struct_tfs(FILE *file
, type_t
*type
, const char *name
, unsigned int *tfsoff
);
65 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
66 const char *name
, const array_dims_t
*array
, int level
,
67 unsigned int *tfsoff
);
69 const char *string_of_type(unsigned char type
)
73 case RPC_FC_BYTE
: return "FC_BYTE";
74 case RPC_FC_CHAR
: return "FC_CHAR";
75 case RPC_FC_SMALL
: return "FC_SMALL";
76 case RPC_FC_USMALL
: return "FC_USMALL";
77 case RPC_FC_WCHAR
: return "FC_WCHAR";
78 case RPC_FC_SHORT
: return "FC_SHORT";
79 case RPC_FC_USHORT
: return "FC_USHORT";
80 case RPC_FC_LONG
: return "FC_LONG";
81 case RPC_FC_ULONG
: return "FC_ULONG";
82 case RPC_FC_FLOAT
: return "FC_FLOAT";
83 case RPC_FC_HYPER
: return "FC_HYPER";
84 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
85 case RPC_FC_ENUM16
: return "FC_ENUM16";
86 case RPC_FC_ENUM32
: return "FC_ENUM32";
87 case RPC_FC_IGNORE
: return "FC_IGNORE";
88 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
89 case RPC_FC_RP
: return "FC_RP";
90 case RPC_FC_UP
: return "FC_UP";
91 case RPC_FC_OP
: return "FC_OP";
92 case RPC_FC_FP
: return "FC_FP";
93 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
94 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
95 case RPC_FC_STRUCT
: return "FC_STRUCT";
96 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
97 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
98 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
99 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
100 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
102 error("string_of_type: unknown type 0x%02x\n", type
);
107 static int is_struct(unsigned char type
)
114 case RPC_FC_CPSTRUCT
:
115 case RPC_FC_CVSTRUCT
:
116 case RPC_FC_BOGUS_STRUCT
:
123 static int is_union(unsigned char type
)
127 case RPC_FC_ENCAPSULATED_UNION
:
128 case RPC_FC_NON_ENCAPSULATED_UNION
:
135 static void update_tfsoff(type_t
*type
, unsigned int offset
, FILE *file
)
137 type
->typestring_offset
= offset
;
138 if (file
) type
->tfswrite
= FALSE
;
141 static void guard_rec(type_t
*type
)
143 /* types that contain references to themselves (like a linked list),
144 need to be shielded from infinite recursion when writing embedded
146 if (type
->typestring_offset
)
147 type
->tfswrite
= FALSE
;
149 type
->typestring_offset
= 1;
152 static int is_embedded_complex(const type_t
*type
)
154 return is_struct(type
->type
) || is_union(type
->type
);
157 static long field_offset(const type_t
*strct
, const char *name
, var_t
**pfield
)
160 var_list_t
*fields
= strct
->fields
;
163 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
165 unsigned int align
= 0;
167 if (f
->name
!= NULL
&& strcmp(name
, f
->name
) == 0)
169 if (pfield
) *pfield
= f
;
174 /* FIXME: handle possible padding */
175 offset
+= type_memsize(f
->type
, f
->array
, &align
);
179 if (pfield
) *pfield
= NULL
;
183 static int compare_expr(const expr_t
*a
, const expr_t
*b
)
187 if (a
->type
!= b
->type
)
188 return a
->type
- b
->type
;
195 return a
->u
.lval
- b
->u
.lval
;
196 case EXPR_IDENTIFIER
:
197 return strcmp(a
->u
.sval
, b
->u
.sval
);
199 ret
= compare_expr(a
->ref
, b
->ref
);
202 ret
= compare_expr(a
->u
.ext
, b
->u
.ext
);
205 return compare_expr(a
->ext2
, b
->ext2
);
214 ret
= compare_expr(a
->ref
, b
->ref
);
217 return compare_expr(a
->u
.ext
, b
->u
.ext
);
223 return compare_expr(a
->ref
, b
->ref
);
230 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
233 fprintf(file, "/* %2u */\n", typestring_offset); \
234 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
238 static int print_file(FILE *file
, int indent
, const char *format
, ...)
245 va_start(va
, format
);
246 for (i
= 0; i
< indent
; i
++)
248 r
= vfprintf(file
, format
, va
);
253 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
255 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
256 print_file(f
, indent
, "{\n");
257 print_file(f
, indent
+ 1, "short Pad;\n");
258 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
259 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
260 print_file(f
, indent
, "\n");
263 void write_formatstringsdecl(FILE *f
, int indent
, ifref_list_t
*ifaces
, int for_objects
)
265 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
266 get_size_typeformatstring(ifaces
, for_objects
));
268 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
269 get_size_procformatstring(ifaces
, for_objects
));
272 write_formatdesc(f
, indent
, "TYPE");
273 write_formatdesc(f
, indent
, "PROC");
275 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
276 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
277 print_file(f
, indent
, "\n");
280 static int is_user_derived(const var_t
*v
)
282 const type_t
*type
= v
->type
;
284 if (v
->attrs
&& is_attr( v
->attrs
, ATTR_WIREMARSHAL
)) return 1;
288 if (type
->attrs
&& is_attr( type
->attrs
, ATTR_WIREMARSHAL
)) return 1;
294 static inline int is_base_type(unsigned char type
)
313 case RPC_FC_ERROR_STATUS_T
:
314 case RPC_FC_BIND_PRIMITIVE
:
322 static size_t write_procformatstring_var(FILE *file
, int indent
,
323 const var_t
*var
, int is_return
)
326 const type_t
*type
= var
->type
;
328 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
329 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
331 if (!is_in
&& !is_out
) is_in
= TRUE
;
333 if (!var
->array
&& is_base_type(type
->type
))
336 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
338 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
340 if (is_base_type(type
->type
))
342 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
343 size
= 2; /* includes param type prefix */
345 else if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
347 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
348 size
= 2; /* includes param type prefix */
352 error("Unknown/unsupported type: %s (0x%02x)\n", var
->name
, type
->type
);
359 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
360 else if (is_in
&& is_out
)
361 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
363 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
365 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
367 print_file(file
, indent
, "0x01,\n");
368 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
369 size
= 4; /* includes param type prefix */
374 void write_procformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
376 const ifref_t
*iface
;
380 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
381 print_file(file
, indent
, "{\n");
383 print_file(file
, indent
, "0,\n");
384 print_file(file
, indent
, "{\n");
387 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
389 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
392 if (iface
->iface
->funcs
)
395 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
397 if (is_local(func
->def
->attrs
)) continue;
398 /* emit argument data */
401 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
402 write_procformatstring_var(file
, indent
, var
, FALSE
);
405 /* emit return value data */
407 if (is_void(var
->type
))
409 print_file(file
, indent
, "0x5b, /* FC_END */\n");
410 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
413 write_procformatstring_var(file
, indent
, var
, TRUE
);
418 print_file(file
, indent
, "0x0\n");
420 print_file(file
, indent
, "}\n");
422 print_file(file
, indent
, "};\n");
423 print_file(file
, indent
, "\n");
426 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
428 if (is_base_type(type
->type
))
430 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
431 *typestring_offset
+= 1;
438 /* write conformance / variance descriptor */
439 static size_t write_conf_or_var_desc(FILE *file
, const func_t
*func
, const type_t
*structure
, const expr_list_t
*expr_list
)
441 unsigned char operator_type
= 0;
442 const char *operator_string
= "no operators";
443 const expr_t
*expr
, *subexpr
;
444 unsigned char correlation_type
;
446 if (!file
) return 4; /* optimisation for sizing pass */
448 if (list_count(expr_list
) > 1)
449 error("write_conf_or_var_desc: multi-dimensional arrays not supported yet\n");
451 expr
= subexpr
= LIST_ENTRY( list_head(expr_list
), const expr_t
, entry
);
455 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
456 error("write_conf_or_var_desc: constant value %ld is greater than "
457 "the maximum constant size of %d\n", expr
->cval
,
458 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
460 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
461 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
462 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
463 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
468 switch (subexpr
->type
)
471 subexpr
= subexpr
->ref
;
472 operator_type
= RPC_FC_DEREFERENCE
;
473 operator_string
= "FC_DEREFERENCE";
476 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
478 subexpr
= subexpr
->ref
;
479 operator_type
= RPC_FC_DIV_2
;
480 operator_string
= "FC_DIV_2";
484 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
486 subexpr
= subexpr
->ref
;
487 operator_type
= RPC_FC_MULT_2
;
488 operator_string
= "FC_MULT_2";
492 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
494 subexpr
= subexpr
->ref
;
495 operator_type
= RPC_FC_SUB_1
;
496 operator_string
= "FC_SUB_1";
500 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
502 subexpr
= subexpr
->ref
;
503 operator_type
= RPC_FC_ADD_1
;
504 operator_string
= "FC_ADD_1";
511 if (subexpr
->type
== EXPR_IDENTIFIER
)
513 const type_t
*correlation_variable
= NULL
;
514 unsigned char correlation_variable_type
;
515 unsigned char param_type
= 0;
516 const char *param_type_string
= NULL
;
524 if (structure
->fields
) LIST_FOR_EACH_ENTRY( var
, structure
->fields
, const var_t
, entry
)
526 unsigned int align
= 0;
527 offset
-= type_memsize(var
->type
, var
->array
, &align
);
528 /* FIXME: take alignment into account */
529 if (!strcmp(var
->name
, subexpr
->u
.sval
))
531 correlation_variable
= var
->type
;
535 if (!correlation_variable
)
536 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
539 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
545 offset
= sizeof(void *);
546 if (func
->args
) LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
548 if (!strcmp(var
->name
, subexpr
->u
.sval
))
550 correlation_variable
= var
->type
;
553 /* FIXME: not all stack variables are sizeof(void *) */
554 offset
+= sizeof(void *);
556 if (!correlation_variable
)
557 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
560 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
563 correlation_variable_type
= correlation_variable
->type
;
565 switch (correlation_variable_type
)
569 param_type
= RPC_FC_SMALL
;
570 param_type_string
= "FC_SMALL";
574 param_type
= RPC_FC_USMALL
;
575 param_type_string
= "FC_USMALL";
579 param_type
= RPC_FC_SHORT
;
580 param_type_string
= "FC_SHORT";
583 param_type
= RPC_FC_USHORT
;
584 param_type_string
= "FC_USHORT";
587 param_type
= RPC_FC_LONG
;
588 param_type_string
= "FC_LONG";
591 param_type
= RPC_FC_ULONG
;
592 param_type_string
= "FC_ULONG";
598 if (sizeof(void *) == 4) /* FIXME */
600 param_type
= RPC_FC_LONG
;
601 param_type_string
= "FC_LONG";
605 param_type
= RPC_FC_HYPER
;
606 param_type_string
= "FC_HYPER";
610 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
611 correlation_variable_type
);
614 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
615 correlation_type
| param_type
,
616 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter, " : "",
618 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
619 print_file(file
, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
621 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "x86 stack size / " : "",
626 unsigned int callback_offset
= 0;
630 struct expr_eval_routine
*eval
;
633 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
635 if (!strcmp(eval
->structure
->name
, structure
->name
) &&
636 !compare_expr(eval
->expr
, expr
))
646 unsigned int align
= 0;
647 eval
= xmalloc(sizeof(*eval
));
648 eval
->structure
= structure
;
649 eval
->structure_size
= fields_memsize(structure
->fields
, &align
);
651 list_add_tail(&expr_eval_routines
, &eval
->entry
);
654 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
658 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
659 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
662 if (callback_offset
> USHRT_MAX
)
663 error("Maximum number of callback routines reached\n");
665 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n",
667 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter" : "");
668 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
669 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
674 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
679 if (!fields
) return 0;
680 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
681 size
+= type_memsize(v
->type
, v
->array
, align
);
686 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
688 size_t size
, maxs
= 0;
689 unsigned int align
= *pmaxa
;
692 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
694 /* we could have an empty default field with NULL type */
697 size
= type_memsize(v
->type
, v
->array
, &align
);
698 if (maxs
< size
) maxs
= size
;
699 if (*pmaxa
< align
) *pmaxa
= align
;
706 static size_t get_array_size( const array_dims_t
*array
)
711 if (!array
) return 0;
713 LIST_FOR_EACH_ENTRY( dim
, array
, expr_t
, entry
)
715 if (!dim
->is_const
) return 0;
722 static size_t type_memsize(const type_t
*t
, const array_dims_t
*array
, unsigned int *align
)
728 size
= sizeof(void *);
729 if (size
> *align
) *align
= size
;
731 else switch (t
->type
)
738 if (size
> *align
) *align
= size
;
745 if (size
> *align
) *align
= size
;
749 case RPC_FC_ERROR_STATUS_T
:
753 if (size
> *align
) *align
= size
;
758 if (size
> *align
) *align
= size
;
761 case RPC_FC_CVSTRUCT
:
762 case RPC_FC_CPSTRUCT
:
765 case RPC_FC_BOGUS_STRUCT
:
766 size
= fields_memsize(t
->fields
, align
);
768 case RPC_FC_ENCAPSULATED_UNION
:
769 case RPC_FC_NON_ENCAPSULATED_UNION
:
770 size
= union_memsize(t
->fields
, align
);
773 error("type_memsize: Unknown type %d\n", t
->type
);
777 if (array
) size
*= get_array_size( array
);
781 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
783 short absoff
= type
->ref
->typestring_offset
;
784 short reloff
= absoff
- (offset
+ 2);
785 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
787 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
788 type
->type
, ptr_attr
, string_of_type(type
->type
));
789 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
790 reloff
, reloff
, absoff
);
794 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
796 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
797 type
->type
, string_of_type(type
->type
));
798 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->ref
->type
,
799 string_of_type(type
->ref
->type
));
800 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
804 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
806 unsigned int offset
= *typestring_offset
;
808 print_file(file
, 0, "/* %d */\n", offset
);
809 update_tfsoff(type
, offset
, file
);
811 if (type
->ref
->typestring_offset
)
812 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
813 else if (is_base_type(type
->ref
->type
))
814 *typestring_offset
+= write_simple_pointer(file
, type
);
819 static int processed(const type_t
*type
)
821 return type
->typestring_offset
&& !type
->tfswrite
;
824 static size_t write_pointer_description(FILE *file
, const attr_list_t
*attrs
,
825 type_t
*type
, size_t mem_offset
,
826 const array_dims_t
*array
, int level
,
827 unsigned int *typestring_offset
)
830 unsigned int align
= 0;
832 /* don't generate a pointer for first-level arrays since we want to
833 * descend into them to write their pointers, not stop here */
834 if ((level
== 0 || !is_ptr(type
)) && is_array_type(attrs
, type
, array
))
836 write_pointer_description(file
, NULL
, type
, mem_offset
, NULL
,
837 level
+ 1, typestring_offset
);
839 else if (is_ptr(type
))
841 print_file(file
, 2, "0x46,\t/* FC_NO_REPEAT */\n");
842 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
843 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset
, mem_offset
);
844 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset
, mem_offset
);
845 *typestring_offset
+= 6;
847 if (processed(type
->ref
) || is_base_type(type
->ref
->type
))
848 write_pointer_tfs(file
, type
, typestring_offset
);
850 error("write_pointer_description: type format string unknown\n");
852 else if (level
== 0 && is_struct(type
->type
))
856 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
858 += write_pointer_description(file
, v
->attrs
, v
->type
,
859 mem_offset
, v
->array
,
865 return type_memsize(type
, array
, &align
);
868 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
869 const type_t
*type
, const array_dims_t
*array
,
870 const char *name
, unsigned int *typestring_offset
)
872 const expr_list_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
873 int has_size
= is_non_void(size_is
);
874 size_t start_offset
= *typestring_offset
;
875 unsigned char flags
= 0;
881 pointer_type
= type
->type
;
885 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
888 pointer_type
= RPC_FC_RP
;
890 if (!get_attrp(attrs
, ATTR_SIZEIS
))
891 flags
|= RPC_FC_P_SIMPLEPOINTER
;
895 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
897 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
901 print_file(file
, 2,"0x%x, 0x%x, /* %s%s */\n",
903 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"),
904 (flags
& RPC_FC_P_SIMPLEPOINTER
) ? " [simple_pointer]" : "");
905 *typestring_offset
+= 2;
907 if (!(flags
& RPC_FC_P_SIMPLEPOINTER
))
909 print_file(file
, 2, "NdrFcShort(0x2),\n");
910 *typestring_offset
+= 2;
913 if (array
&& !is_conformant_array(array
))
915 /* FIXME: multi-dimensional array */
916 const expr_t
*dim
= LIST_ENTRY( list_head( array
), expr_t
, entry
);
917 if (dim
->cval
> USHRT_MAX
)
918 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
919 name
, USHRT_MAX
, dim
->cval
- USHRT_MAX
);
921 if (rtype
== RPC_FC_CHAR
)
922 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
924 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
925 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
926 *typestring_offset
+= 2;
928 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", dim
->cval
, dim
->cval
);
929 *typestring_offset
+= 2;
935 if (rtype
== RPC_FC_CHAR
)
936 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
938 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
939 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
940 *typestring_offset
+= 2;
942 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
, NULL
, size_is
);
948 if (rtype
== RPC_FC_CHAR
)
949 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
951 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
952 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
953 *typestring_offset
+= 2;
959 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
,
960 type_t
*type
, const array_dims_t
*array
,
961 const char *name
, unsigned int *typestring_offset
)
963 const expr_list_t
*length_is
= get_attrp(attrs
, ATTR_LENGTHIS
);
964 const expr_list_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
965 int has_length
= is_non_void(length_is
);
966 int has_size
= is_non_void(size_is
) || is_conformant_array(array
);
968 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
970 pointer_type
= RPC_FC_RP
;
972 print_file(file
, 2, "0x%x, 0x00, /* %s */\n",
974 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"));
975 print_file(file
, 2, "NdrFcShort(0x2),\n");
976 *typestring_offset
+= 4;
978 if (array
&& list_count(array
) > 1) /* multi-dimensional array */
980 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name
);
985 const expr_t
*dim
= array
? LIST_ENTRY( list_head( array
), expr_t
, entry
) : NULL
;
988 if (write_embedded_types(file
, attrs
, type
, name
, array
, 0, typestring_offset
))
991 start_offset
= *typestring_offset
;
993 if (!has_length
&& !has_size
)
996 unsigned int align
= 0;
997 size_t size
= type_memsize(type
, array
, &align
);
998 if (size
< USHRT_MAX
)
1000 WRITE_FCTYPE(file
, FC_SMFARRAY
, *typestring_offset
);
1002 print_file(file
, 2, "0x%02x,\n", align
- 1);
1004 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", size
, size
);
1005 *typestring_offset
+= 4;
1009 WRITE_FCTYPE(file
, FC_LGFARRAY
, *typestring_offset
);
1011 print_file(file
, 2, "0x%02x,\n", align
- 1);
1013 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", size
, size
);
1014 *typestring_offset
+= 6;
1019 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1020 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1021 *typestring_offset
+= 2;
1022 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1023 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1024 *typestring_offset
+= 1;
1027 if (!write_base_type( file
, type
, typestring_offset
))
1029 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1030 *typestring_offset
+= 1;
1032 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1033 *typestring_offset
+= 1;
1035 return start_offset
;
1037 else if (has_length
&& !has_size
)
1040 unsigned int align
= 0;
1041 size_t element_size
= type_memsize(type
, NULL
, &align
);
1042 size_t elements
= dim
->cval
;
1043 size_t total_size
= element_size
* elements
;
1045 if (total_size
< USHRT_MAX
)
1047 WRITE_FCTYPE(file
, FC_SMVARRAY
, *typestring_offset
);
1049 print_file(file
, 2, "0x%02x,\n", align
- 1);
1051 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", total_size
, total_size
);
1052 /* number of elements */
1053 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", elements
, elements
);
1054 *typestring_offset
+= 6;
1058 WRITE_FCTYPE(file
, FC_LGVARRAY
, *typestring_offset
);
1060 print_file(file
, 2, "0x%02x,\n", align
- 1);
1062 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", total_size
, total_size
);
1063 /* number of elements */
1064 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", elements
, elements
);
1065 *typestring_offset
+= 10;
1068 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
1069 *typestring_offset
+= 2;
1071 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1077 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1078 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1079 *typestring_offset
+= 2;
1080 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1081 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1082 *typestring_offset
+= 1;
1085 if (!write_base_type( file
, type
, typestring_offset
))
1087 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1088 *typestring_offset
+= 1;
1090 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1091 *typestring_offset
+= 1;
1093 return start_offset
;
1095 else if (!has_length
&& has_size
)
1097 /* conformant array */
1098 unsigned int align
= 0;
1099 size_t element_size
= type_memsize(type
, NULL
, &align
);
1101 WRITE_FCTYPE(file
, FC_CARRAY
, *typestring_offset
);
1103 print_file(file
, 2, "0x%02x,\n", align
- 1);
1105 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
1106 *typestring_offset
+= 4;
1108 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1110 size_is
? size_is
: array
);
1114 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1115 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1116 *typestring_offset
+= 2;
1117 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1118 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1119 *typestring_offset
+= 1;
1122 if (!write_base_type( file
, type
, typestring_offset
))
1124 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1125 *typestring_offset
+= 1;
1127 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1128 *typestring_offset
+= 1;
1130 return start_offset
;
1134 /* conformant varying array */
1135 unsigned int align
= 0;
1136 size_t element_size
= type_memsize(type
, NULL
, &align
);
1138 WRITE_FCTYPE(file
, FC_CVARRAY
, *typestring_offset
);
1140 print_file(file
, 2, "0x%02x,\n", align
- 1);
1142 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
1143 *typestring_offset
+= 4;
1145 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1147 size_is
? size_is
: array
);
1148 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1154 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1155 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1156 *typestring_offset
+= 2;
1157 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1158 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1159 *typestring_offset
+= 1;
1162 if (!write_base_type( file
, type
, typestring_offset
))
1164 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1165 *typestring_offset
+= 1;
1167 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1168 *typestring_offset
+= 1;
1170 return start_offset
;
1175 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1177 const var_t
*last_field
= LIST_ENTRY( list_tail(type
->fields
), const var_t
, entry
);
1179 if (is_array_type(last_field
->attrs
, last_field
->type
, last_field
->array
))
1182 assert((last_field
->type
->type
== RPC_FC_CSTRUCT
) ||
1183 (last_field
->type
->type
== RPC_FC_CPSTRUCT
) ||
1184 (last_field
->type
->type
== RPC_FC_CVSTRUCT
));
1186 return find_array_or_string_in_struct(last_field
->type
);
1189 static void write_struct_members(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
1193 if (type
->fields
) LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1195 unsigned char rtype
= field
->type
->type
;
1198 write_array_tfs( file
, field
->attrs
, field
->type
, field
->array
,
1199 field
->name
, typestring_offset
);
1200 else if (is_ptr( field
->type
))
1202 /* pointers are handled in detail earlier, here just treat them like longs */
1203 print_file( file
, 2, "0x8,\t/* FC_LONG */\n" );
1204 *typestring_offset
+= 1;
1206 else if (is_embedded_complex(field
->type
))
1208 size_t absoff
= (field
->corrdesc
1210 : field
->type
->typestring_offset
);
1211 short reloff
= absoff
- (*typestring_offset
+ 2);
1213 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1214 /* FIXME: actually compute necessary padding */
1215 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1216 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1217 reloff
, reloff
, absoff
);
1218 *typestring_offset
+= 4;
1220 else if (!write_base_type( file
, field
->type
, typestring_offset
))
1221 error("Unsupported member type 0x%x\n", rtype
);
1224 if (!(*typestring_offset
% 2))
1226 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1227 *typestring_offset
+= 1;
1230 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1231 *typestring_offset
+= 1;
1234 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1235 const char *name
, unsigned int *typestring_offset
)
1237 unsigned int total_size
;
1239 size_t start_offset
;
1240 size_t array_offset
;
1242 unsigned int align
= 0;
1249 case RPC_FC_PSTRUCT
:
1250 total_size
= type_memsize(type
, NULL
, &align
);
1252 if (total_size
> USHRT_MAX
)
1253 error("structure size for %s exceeds %d bytes by %d bytes\n",
1254 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1256 if (type
->type
== RPC_FC_PSTRUCT
)
1257 write_embedded_types(file
, NULL
, type
, name
, NULL
, 0, typestring_offset
);
1259 start_offset
= *typestring_offset
;
1260 update_tfsoff(type
, start_offset
, file
);
1261 if (type
->type
== RPC_FC_STRUCT
)
1262 WRITE_FCTYPE(file
, FC_STRUCT
, *typestring_offset
);
1264 WRITE_FCTYPE(file
, FC_PSTRUCT
, *typestring_offset
);
1266 print_file(file
, 2, "0x%02x,\n", align
- 1);
1268 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1269 *typestring_offset
+= 4;
1271 if (type
->type
== RPC_FC_PSTRUCT
)
1273 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1274 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1275 *typestring_offset
+= 2;
1276 write_pointer_description(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1277 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1278 *typestring_offset
+= 1;
1282 write_struct_members(file
, type
, typestring_offset
);
1283 return start_offset
;
1284 case RPC_FC_CSTRUCT
:
1285 case RPC_FC_CPSTRUCT
:
1286 total_size
= type_memsize(type
, NULL
, &align
);
1288 if (total_size
> USHRT_MAX
)
1289 error("structure size for %s exceeds %d bytes by %d bytes\n",
1290 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1292 array
= find_array_or_string_in_struct(type
);
1293 current_structure
= type
;
1294 array_offset
= write_array_tfs(file
, array
->attrs
, array
->type
,
1295 array
->array
, array
->name
,
1297 current_structure
= NULL
;
1299 if (type
->type
== RPC_FC_CPSTRUCT
)
1300 write_embedded_types(file
, NULL
, type
, name
, NULL
, 0, typestring_offset
);
1302 start_offset
= *typestring_offset
;
1303 update_tfsoff(type
, start_offset
, file
);
1304 if (type
->type
== RPC_FC_CSTRUCT
)
1305 WRITE_FCTYPE(file
, FC_CSTRUCT
, *typestring_offset
);
1307 WRITE_FCTYPE(file
, FC_CPSTRUCT
, *typestring_offset
);
1309 print_file(file
, 2, "0x%02x,\n", align
- 1);
1311 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1312 *typestring_offset
+= 4;
1313 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1314 array_offset
- *typestring_offset
,
1315 array_offset
- *typestring_offset
,
1317 *typestring_offset
+= 2;
1319 if (type
->type
== RPC_FC_CPSTRUCT
)
1321 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1322 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1323 *typestring_offset
+= 2;
1324 write_pointer_description(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1325 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1326 *typestring_offset
+= 1;
1329 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1330 *typestring_offset
+= 1;
1332 return start_offset
;
1333 case RPC_FC_CVSTRUCT
:
1334 total_size
= type_memsize(type
, NULL
, &align
);
1336 if (total_size
> USHRT_MAX
)
1337 error("structure size for %s exceeds %d bytes by %d bytes\n",
1338 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1340 array
= find_array_or_string_in_struct(type
);
1341 current_structure
= type
;
1342 if (is_attr(array
->attrs
, ATTR_STRING
))
1343 array_offset
= write_string_tfs(file
, array
->attrs
, array
->type
,
1344 array
->array
, array
->name
,
1347 array_offset
= write_array_tfs(file
, array
->attrs
, array
->type
,
1348 array
->array
, array
->name
,
1350 current_structure
= NULL
;
1352 has_pointers
= write_embedded_types(file
, NULL
, type
, name
, NULL
, 0,
1355 start_offset
= *typestring_offset
;
1356 update_tfsoff(type
, start_offset
, file
);
1357 WRITE_FCTYPE(file
, FC_CVSTRUCT
, *typestring_offset
);
1359 print_file(file
, 2, "0x%02x,\n", align
- 1);
1361 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1362 *typestring_offset
+= 4;
1363 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1364 array_offset
- *typestring_offset
,
1365 array_offset
- *typestring_offset
,
1367 *typestring_offset
+= 2;
1371 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1372 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1373 *typestring_offset
+= 2;
1374 write_pointer_description(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1375 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1376 *typestring_offset
+= 1;
1379 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1380 *typestring_offset
+= 1;
1382 return start_offset
;
1384 case RPC_FC_BOGUS_STRUCT
:
1385 total_size
= type_memsize(type
, NULL
, &align
);
1386 if (total_size
> USHRT_MAX
)
1387 error("structure size for %s exceeds %d bytes by %d bytes\n",
1388 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1390 write_embedded_types(file
, NULL
, type
, name
, NULL
, 0, typestring_offset
);
1392 start_offset
= *typestring_offset
;
1393 update_tfsoff(type
, start_offset
, file
);
1394 print_file(file
, 0, "/* %d */\n", start_offset
);
1395 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1396 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1397 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1399 /* do conformant array stuff */
1400 print_file(file
, 2, "NdrFcShort(0x0),\t/* FIXME: conformant array stuff */\n");
1402 /* do pointer stuff here */
1403 print_file(file
, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1405 *typestring_offset
+= 8;
1406 write_struct_members(file
, type
, typestring_offset
);
1408 return start_offset
;
1411 error("write_struct_tfs: Unimplemented for type 0x%x\n", type
->type
);
1412 return *typestring_offset
;
1416 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1417 unsigned char flags
, size_t offset
,
1418 unsigned int *typeformat_offset
)
1420 size_t start_offset
= *typeformat_offset
;
1421 short reloff
= offset
- (*typeformat_offset
+ 2);
1422 int in_attr
, out_attr
;
1423 in_attr
= is_attr(attrs
, ATTR_IN
);
1424 out_attr
= is_attr(attrs
, ATTR_OUT
);
1425 if (!in_attr
&& !out_attr
) in_attr
= 1;
1427 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1430 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1433 string_of_type(pointer_type
));
1437 fprintf(file
, " [allocated_on_stack]");
1439 fprintf(file
, " [pointer_deref]");
1440 fprintf(file
, " */\n");
1443 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1444 *typeformat_offset
+= 4;
1446 return start_offset
;
1449 static void write_branch_type(FILE *file
, const type_t
*t
, unsigned int *tfsoff
)
1451 if (is_base_type(t
->type
))
1453 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1454 t
->type
, string_of_type(t
->type
));
1456 else if (t
->typestring_offset
)
1458 short reloff
= t
->typestring_offset
- *tfsoff
;
1459 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1460 reloff
, reloff
, t
->typestring_offset
);
1463 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1468 static size_t write_union_tfs(FILE *file
, type_t
*type
, const char *name
,
1469 unsigned int *tfsoff
)
1471 unsigned int align
= 0;
1472 unsigned int start_offset
;
1473 size_t size
= type_memsize(type
, NULL
, &align
);
1474 var_list_t
*fields
= type
->fields
;
1476 type_t
*deftype
= NULL
;
1477 short nodeftype
= 0xffff;
1482 /* use a level of 1 so pointers always get written */
1483 write_embedded_types(file
, NULL
, type
, name
, NULL
, 1, tfsoff
);
1485 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1487 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1489 nbranch
+= list_count(cases
);
1492 start_offset
= *tfsoff
;
1493 update_tfsoff(type
, start_offset
, file
);
1494 print_file(file
, 0, "/* %d */\n", start_offset
);
1495 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
1496 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
1499 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1501 type_t
*ft
= f
->type
;
1502 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1503 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
1506 if (cases
== NULL
&& !deflt
)
1507 error("union field %s with neither case nor default attribute\n", f
->name
);
1509 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
1511 /* MIDL doesn't check for duplicate cases, even though that seems
1512 like a reasonable thing to do, it just dumps them to the TFS
1513 like we're going to do here. */
1514 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
1516 write_branch_type(file
, ft
, tfsoff
);
1519 /* MIDL allows multiple default branches, even though that seems
1520 illogical, it just chooses the last one, which is what we will
1531 write_branch_type(file
, deftype
, tfsoff
);
1535 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
1539 return start_offset
;
1542 static size_t write_ip_tfs(FILE *file
, const func_t
*func
, const type_t
*type
, const var_t
*var
,
1543 unsigned int *typeformat_offset
)
1546 size_t start_offset
= *typeformat_offset
;
1547 const var_t
*iid
= get_attrp(var
->attrs
, ATTR_IIDIS
);
1552 expr_list_t expr_list
;
1554 expr
.type
= EXPR_IDENTIFIER
;
1556 expr
.u
.sval
= iid
->name
;
1557 expr
.is_const
= FALSE
;
1558 list_init( &expr_list
);
1559 list_add_head( &expr_list
, &expr
.entry
);
1560 print_file(file
, 2, "0x2f, /* FC_IP */\n");
1561 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
1562 *typeformat_offset
+= write_conf_or_var_desc(file
, func
, NULL
, &expr_list
) + 2;
1566 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
1567 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
1570 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
1572 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
1573 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1574 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
1575 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
1576 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
1577 for (i
= 0; i
< 8; ++i
)
1578 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
1581 fprintf(file
, "\n");
1583 *typeformat_offset
+= 18;
1585 return start_offset
;
1588 static int get_ptr_attr(const type_t
*t
, int def_type
)
1592 int ptr_attr
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
1595 if (t
->kind
!= TKIND_ALIAS
)
1601 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
1602 type_t
*type
, const var_t
*var
,
1603 unsigned int *typeformat_offset
)
1608 if (type
== var
->type
) /* top-level pointers */
1610 int pointer_attr
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1611 if (pointer_attr
!= 0 && !is_ptr(type
))
1612 error("'%s': pointer attribute applied to non-pointer type\n", var
->name
);
1614 if (pointer_attr
== 0)
1615 pointer_attr
= get_ptr_attr(type
, RPC_FC_RP
);
1617 pointer_type
= pointer_attr
;
1620 pointer_type
= get_ptr_attr(type
, RPC_FC_UP
);
1622 if (((last_ptr(type
) && var
->array
== NULL
)
1623 || (!is_ptr(type
) && var
->array
!= NULL
))
1624 && is_ptrchain_attr(var
, ATTR_STRING
))
1626 return write_string_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
1629 if (is_array_type(var
->attrs
, type
, var
->array
))
1630 return write_array_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
1634 /* basic types don't need a type format string */
1635 if (is_base_type(type
->type
))
1641 case RPC_FC_PSTRUCT
:
1642 case RPC_FC_CSTRUCT
:
1643 case RPC_FC_CPSTRUCT
:
1644 case RPC_FC_CVSTRUCT
:
1645 case RPC_FC_BOGUS_STRUCT
:
1646 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
1647 case RPC_FC_ENCAPSULATED_UNION
:
1648 case RPC_FC_NON_ENCAPSULATED_UNION
:
1649 return write_union_tfs(file
, type
, var
->name
, typeformat_offset
);
1651 case RPC_FC_BIND_PRIMITIVE
:
1655 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
1658 else if (last_ptr(type
))
1660 size_t start_offset
= *typeformat_offset
;
1661 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1662 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1663 const type_t
*base
= type
->ref
;
1665 if (base
->type
== RPC_FC_IP
)
1667 return write_ip_tfs(file
, func
, type
, var
, typeformat_offset
);
1670 /* special case for pointers to base types */
1671 if (is_base_type(base
->type
))
1673 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1674 pointer_type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
1675 string_of_type(pointer_type
),
1676 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
1677 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
1678 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
1679 *typeformat_offset
+= 4;
1680 return start_offset
;
1684 assert(is_ptr(type
));
1686 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
1688 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
1689 return write_pointer_only_tfs(file
, var
->attrs
, pointer_type
,
1690 !last_ptr(type
) ? 0x10 : 0,
1691 offset
, typeformat_offset
);
1694 static void set_tfswrite(type_t
*type
, int val
)
1696 while (type
->tfswrite
!= val
)
1698 type
->tfswrite
= val
;
1700 if (type
->kind
== TKIND_ALIAS
)
1702 else if (is_ptr(type
))
1709 LIST_FOR_EACH_ENTRY( v
, type
->fields
, var_t
, entry
)
1710 set_tfswrite(v
->type
, val
);
1718 static int write_embedded_types(FILE *file
, const attr_list_t
*attrs
, type_t
*type
,
1719 const char *name
, const array_dims_t
*array
,
1720 int level
, unsigned int *tfsoff
)
1722 var_list_t
*fields
= type
->fields
;
1727 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1729 unsigned int align
= 0;
1730 type_t
*ft
= f
->type
;
1733 else if (ft
->type
== RPC_FC_NON_ENCAPSULATED_UNION
)
1735 expr_t
*swexp
= get_attrp(f
->attrs
, ATTR_SWITCHIS
);
1739 unsigned char corrdesc
, op
= 0;
1740 short creloff
, ureloff
;
1743 error("union %s needs a switch_is attribute\n", f
->name
);
1744 if (swexp
->type
!= EXPR_IDENTIFIER
)
1745 error("%s: only identifiers are supported for switch_is at this time\n",
1749 write_union_tfs(file
, ft
, f
->name
, tfsoff
);
1751 swname
= swexp
->u
.sval
;
1752 corroff
= field_offset(type
, swname
, &swvar
);
1753 corrdesc
= swvar
->type
->type
;
1754 creloff
= corroff
- offset
;
1756 f
->corrdesc
= *tfsoff
;
1757 ureloff
= ft
->typestring_offset
- (f
->corrdesc
+ 6);
1758 print_file(file
, 0, "/* %d */\n", f
->corrdesc
);
1759 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
1760 print_file(file
, 2, "0x8,\t/* FIXME: support other switch types */\n");
1761 print_file(file
, 2, "0x%x,\t/* Corr desc: %s */\n",
1762 corrdesc
, string_of_type(corrdesc
& 0xf));
1763 print_file(file
, 2, "0x%x,\n", op
);
1764 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %hd */\n", creloff
, creloff
);
1765 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1766 ureloff
, ureloff
, ft
->typestring_offset
);
1770 retmask
|= write_embedded_types(file
, attrs
, ft
, f
->name
, array
,
1773 /* FIXME: this doesn't take alignment/padding into account */
1774 offset
+= type_memsize(ft
, NULL
, &align
);
1776 /* don't generate a pointer for first-level arrays since we want to
1777 descend into them to write their pointers, not stop here */
1778 else if ((level
== 0 || !is_ptr(type
)) && is_array_type(attrs
, type
, array
))
1780 return write_embedded_types(file
, NULL
, type
, name
, NULL
, level
+ 1, tfsoff
);
1782 else if (is_ptr(type
))
1784 type_t
*ref
= type
->ref
;
1786 if (!processed(ref
) && !is_base_type(ref
->type
))
1790 retmask
|= write_embedded_types(file
, attrs
, ref
, name
, array
,
1793 else if (is_struct(ref
->type
))
1795 write_struct_tfs(file
, ref
, name
, tfsoff
);
1799 error("write_embedded_types: type format string unknown for %s (0x%x)\n",
1804 /* top-level pointers are handled inline */
1806 write_pointer_tfs(file
, type
, tfsoff
);
1810 else if (!is_base_type(type
->type
))
1811 error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
1817 static void set_all_tfswrite(const ifref_list_t
*ifaces
, int val
)
1819 const ifref_t
* iface
;
1824 LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
1825 if (iface
->iface
->funcs
)
1826 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
1828 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1829 set_tfswrite(var
->type
, val
);
1832 static size_t process_tfs(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
1835 const ifref_t
*iface
;
1836 unsigned int typeformat_offset
= 2;
1838 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
1840 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
1843 if (iface
->iface
->funcs
)
1846 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
1848 if (is_local(func
->def
->attrs
)) continue;
1850 current_func
= func
;
1852 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1855 write_typeformatstring_var(
1856 file
, 2, func
, var
->type
, var
,
1857 &typeformat_offset
),
1863 return typeformat_offset
+ 1;
1867 void write_typeformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
1871 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1872 print_file(file
, indent
, "{\n");
1874 print_file(file
, indent
, "0,\n");
1875 print_file(file
, indent
, "{\n");
1877 print_file(file
, indent
, "NdrFcShort(0x0),\n");
1879 set_all_tfswrite(ifaces
, TRUE
);
1880 process_tfs(file
, ifaces
, for_objects
);
1882 print_file(file
, indent
, "0x0\n");
1884 print_file(file
, indent
, "}\n");
1886 print_file(file
, indent
, "};\n");
1887 print_file(file
, indent
, "\n");
1890 static unsigned int get_required_buffer_size_type(
1891 const type_t
*type
, const array_dims_t
*array
,
1892 const char *name
, unsigned int *alignment
)
1919 case RPC_FC_ERROR_STATUS_T
:
1931 case RPC_FC_BIND_PRIMITIVE
:
1935 case RPC_FC_PSTRUCT
:
1938 if (!type
->fields
) return 0;
1939 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1941 unsigned int alignment
;
1942 size
+= get_required_buffer_size_type(
1943 field
->type
, field
->array
, field
->name
,
1950 if (is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
)
1951 size
= get_required_buffer_size_type( type
->ref
, NULL
, name
, alignment
);
1955 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
1958 if (array
) size
*= get_array_size( array
);
1963 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
1965 expr_list_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
1966 int has_size
= is_non_void(size_is
);
1967 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1968 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1970 if (!in_attr
&& !out_attr
)
1975 if (pass
== PASS_OUT
)
1977 if (out_attr
&& is_ptr(var
->type
))
1979 type_t
*type
= var
->type
;
1981 if (type
->type
== RPC_FC_STRUCT
)
1984 unsigned int size
= 36;
1986 if (!type
->fields
) return size
;
1987 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1990 size
+= get_required_buffer_size_type(
1991 field
->type
, field
->array
, field
->name
,
2001 if ((!out_attr
|| in_attr
) && !has_size
&& !is_attr(var
->attrs
, ATTR_STRING
) && !var
->array
)
2003 if (is_ptr(var
->type
))
2005 type_t
*type
= var
->type
;
2007 if (is_base_type(type
->type
))
2011 else if (type
->type
== RPC_FC_STRUCT
)
2013 unsigned int size
= 36;
2016 if (!type
->fields
) return size
;
2017 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2020 size
+= get_required_buffer_size_type(
2021 field
->type
, field
->array
, field
->name
,
2029 return get_required_buffer_size_type(var
->type
, var
->array
, var
->name
, alignment
);
2033 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2036 unsigned int total_size
= 0, alignment
;
2040 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2042 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2043 total_size
+= alignment
;
2047 if (pass
== PASS_OUT
&& !is_void(func
->def
->type
))
2049 total_size
+= get_required_buffer_size(func
->def
, &alignment
, PASS_RETURN
);
2050 total_size
+= alignment
;
2055 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2056 enum remoting_phase phase
,
2057 const char *varname
, unsigned int type_offset
)
2059 const char *function
;
2062 case PHASE_BUFFERSIZE
:
2063 function
= "BufferSize";
2066 function
= "Marshall";
2068 case PHASE_UNMARSHAL
:
2069 function
= "Unmarshall";
2079 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2081 print_file(file
, indent
, "&_StubMsg,\n");
2082 print_file(file
, indent
, "%s%s,\n",
2083 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)&" : "(unsigned char *)",
2085 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2086 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2087 if (phase
== PHASE_UNMARSHAL
)
2088 print_file(file
, indent
, "0);\n");
2092 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2093 enum pass pass
, const var_t
*var
,
2094 const char *varname
)
2096 type_t
*type
= var
->type
;
2098 unsigned int alignment
= 0;
2099 unsigned char rtype
;
2101 /* no work to do for other phases, buffer sizing is done elsewhere */
2102 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2105 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2127 case RPC_FC_ERROR_STATUS_T
:
2139 case RPC_FC_BIND_PRIMITIVE
:
2140 /* no marshalling needed */
2144 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2148 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2149 alignment
- 1, alignment
- 1);
2151 if (phase
== PHASE_MARSHAL
)
2153 print_file(file
, indent
, "*(");
2154 write_type(file
, is_ptr(type
) ? type
->ref
: type
);
2156 fprintf(file
, " *)_StubMsg.Buffer = *");
2158 fprintf(file
, " *)_StubMsg.Buffer = ");
2159 fprintf(file
, varname
);
2160 fprintf(file
, ";\n");
2162 else if (phase
== PHASE_UNMARSHAL
)
2164 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2165 print_file(file
, indent
, "");
2167 print_file(file
, indent
, "*");
2168 fprintf(file
, varname
);
2169 if (pass
== PASS_IN
&& is_ptr(type
))
2170 fprintf(file
, " = (");
2172 fprintf(file
, " = *(");
2173 write_type(file
, is_ptr(type
) ? type
->ref
: type
);
2174 fprintf(file
, " *)_StubMsg.Buffer;\n");
2177 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2178 write_type(file
, var
->type
);
2179 fprintf(file
, ");\n");
2182 /* returns whether the MaxCount, Offset or ActualCount members need to be
2183 * filled in for the specified phase */
2184 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2186 return (phase
!= PHASE_UNMARSHAL
);
2189 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
2190 enum pass pass
, enum remoting_phase phase
)
2192 const expr_list_t
*length_is
;
2193 const expr_list_t
*size_is
;
2194 int in_attr
, out_attr
, has_length
, has_size
, pointer_type
;
2200 if (phase
== PHASE_BUFFERSIZE
)
2202 unsigned int size
= get_function_buffer_size( func
, pass
);
2203 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
2206 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2208 const type_t
*type
= var
->type
;
2209 unsigned char rtype
;
2210 size_t start_offset
= type
->typestring_offset
;
2212 length_is
= get_attrp(var
->attrs
, ATTR_LENGTHIS
);
2213 size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2214 has_length
= is_non_void(length_is
);
2215 has_size
= is_non_void(size_is
) || (var
->array
&& is_conformant_array(var
->array
));
2217 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2219 pointer_type
= RPC_FC_RP
;
2221 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2222 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2223 if (!in_attr
&& !out_attr
)
2229 if (!in_attr
) continue;
2232 if (!out_attr
) continue;
2240 if (is_user_derived( var
))
2242 print_phase_function(file
, indent
, "UserMarshal", phase
, var
->name
, start_offset
);
2244 else if (is_string_type(var
->attrs
, var
->type
, var
->array
))
2246 if (var
->array
&& !is_conformant_array(var
->array
))
2247 print_phase_function(file
, indent
, "NonConformantString", phase
, var
->name
, start_offset
);
2250 if (size_is
&& is_size_needed_for_phase(phase
))
2252 const expr_t
*size
= LIST_ENTRY( list_head(size_is
), const expr_t
, entry
);
2253 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2254 write_expr(file
, size
, 1);
2255 fprintf(file
, ";\n");
2258 if ((phase
== PHASE_FREE
) || (pointer_type
== RPC_FC_UP
))
2259 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2261 print_phase_function(file
, indent
, "ConformantString", phase
, var
->name
,
2262 start_offset
+ (has_size
? 4 : 2));
2265 else if (is_array_type(var
->attrs
, var
->type
, var
->array
))
2267 const char *array_type
;
2269 if (var
->array
&& list_count(var
->array
) > 1) /* multi-dimensional array */
2270 array_type
= "ComplexArray";
2273 if (!has_length
&& !has_size
)
2274 array_type
= "FixedArray";
2275 else if (has_length
&& !has_size
)
2277 if (is_size_needed_for_phase(phase
))
2279 const expr_t
*length
= LIST_ENTRY( list_head(length_is
), const expr_t
, entry
);
2280 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2281 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2282 write_expr(file
, length
, 1);
2283 fprintf(file
, ";\n\n");
2285 array_type
= "VaryingArray";
2287 else if (!has_length
&& has_size
)
2289 if (is_size_needed_for_phase(phase
) && phase
!= PHASE_FREE
)
2291 const expr_t
*size
= LIST_ENTRY( list_head(size_is
? size_is
: var
->array
),
2292 const expr_t
, entry
);
2293 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2294 write_expr(file
, size
, 1);
2295 fprintf(file
, ";\n\n");
2297 array_type
= "ConformantArray";
2301 if (is_size_needed_for_phase(phase
))
2303 const expr_t
*length
= LIST_ENTRY( list_head(length_is
), const expr_t
, entry
);
2304 const expr_t
*size
= LIST_ENTRY( list_head(size_is
? size_is
: var
->array
),
2305 const expr_t
, entry
);
2306 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2307 write_expr(file
, size
, 1);
2308 fprintf(file
, ";\n");
2309 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2310 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2311 write_expr(file
, length
, 1);
2312 fprintf(file
, ";\n\n");
2314 array_type
= "ConformantVaryingArray";
2318 if (!in_attr
&& phase
== PHASE_FREE
)
2320 print_file(file
, indent
, "if (%s)\n", var
->name
);
2322 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2324 else if (phase
!= PHASE_FREE
)
2326 if (pointer_type
== RPC_FC_UP
)
2327 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2329 print_phase_function(file
, indent
, array_type
, phase
, var
->name
, start_offset
);
2332 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2334 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2336 else if (!is_ptr(var
->type
))
2341 case RPC_FC_PSTRUCT
:
2342 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, start_offset
);
2344 case RPC_FC_CSTRUCT
:
2345 case RPC_FC_CPSTRUCT
:
2346 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
->name
, start_offset
);
2348 case RPC_FC_CVSTRUCT
:
2349 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
->name
, start_offset
);
2351 case RPC_FC_BOGUS_STRUCT
:
2352 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
->name
, start_offset
);
2355 if (is_base_type( var
->type
->ref
->type
))
2357 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2359 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2361 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2362 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, start_offset
+ 4);
2367 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2368 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2369 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2373 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2378 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2380 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2382 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2384 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2385 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, start_offset
+ 4);
2390 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2391 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2392 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2395 fprintf(file
, "\n");
2400 size_t get_size_procformatstring_var(const var_t
*var
)
2402 return write_procformatstring_var(NULL
, 0, var
, FALSE
);
2406 size_t get_size_procformatstring_func(const func_t
*func
)
2411 /* argument list size */
2413 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2414 size
+= get_size_procformatstring_var(var
);
2416 /* return value size */
2417 if (is_void(func
->def
->type
))
2418 size
+= 2; /* FC_END and FC_PAD */
2420 size
+= get_size_procformatstring_var(func
->def
);
2425 size_t get_size_procformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2427 const ifref_t
*iface
;
2431 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2433 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2436 if (iface
->iface
->funcs
)
2437 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2438 if (!is_local(func
->def
->attrs
))
2439 size
+= get_size_procformatstring_func( func
);
2444 size_t get_size_typeformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2446 set_all_tfswrite(ifaces
, FALSE
);
2447 return process_tfs(NULL
, ifaces
, for_objects
);
2450 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
2451 const var_list_t
*fields
, const char *structvar
)
2457 fprintf(h
, "%lu", e
->u
.lval
);
2460 fprintf(h
, "0x%lx", e
->u
.lval
);
2462 case EXPR_TRUEFALSE
:
2464 fprintf(h
, "FALSE");
2468 case EXPR_IDENTIFIER
:
2471 LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2472 if (!strcmp(e
->u
.sval
, field
->name
))
2474 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
2478 if (&field
->entry
== fields
) error("no field found for identifier %s\n", e
->u
.sval
);
2483 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2487 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2491 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2495 write_type(h
, e
->u
.tref
);
2497 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2500 fprintf(h
, "sizeof(");
2501 write_type(h
, e
->u
.tref
);
2512 if (brackets
) fprintf(h
, "(");
2513 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2515 case EXPR_SHL
: fprintf(h
, " << "); break;
2516 case EXPR_SHR
: fprintf(h
, " >> "); break;
2517 case EXPR_MUL
: fprintf(h
, " * "); break;
2518 case EXPR_DIV
: fprintf(h
, " / "); break;
2519 case EXPR_ADD
: fprintf(h
, " + "); break;
2520 case EXPR_SUB
: fprintf(h
, " - "); break;
2521 case EXPR_AND
: fprintf(h
, " & "); break;
2522 case EXPR_OR
: fprintf(h
, " | "); break;
2525 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2526 if (brackets
) fprintf(h
, ")");
2529 if (brackets
) fprintf(h
, "(");
2530 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2532 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2534 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
2535 if (brackets
) fprintf(h
, ")");
2541 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
2543 int in_attr
, out_attr
;
2545 const var_t
*def
= func
->def
;
2548 /* declare return value '_RetVal' */
2549 if (!is_void(def
->type
))
2551 print_file(file
, indent
, "");
2552 write_type(file
, def
->type
);
2553 fprintf(file
, " _RetVal;\n");
2559 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2561 const expr_list_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2562 int has_size
= is_non_void(size_is
);
2563 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2565 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2566 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2567 if (!out_attr
&& !in_attr
)
2570 if (!in_attr
&& !has_size
&& !is_string
)
2572 print_file(file
, indent
, "");
2573 write_type(file
, var
->type
->ref
);
2574 fprintf(file
, " _W%u;\n", i
++);
2577 print_file(file
, indent
, "");
2578 write_type(file
, var
->type
);
2581 fprintf(file
, "( *");
2582 write_name(file
, var
);
2583 fprintf(file
, " )");
2585 write_name(file
, var
);
2586 write_array(file
, var
->array
, 0);
2587 fprintf(file
, ";\n");
2592 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
2594 int in_attr
, out_attr
;
2597 const expr_list_t
*size_is
;
2603 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2605 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2606 size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2607 has_size
= is_non_void(size_is
);
2608 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2609 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2610 if (!out_attr
&& !in_attr
)
2615 print_file(file
, indent
, "");
2616 write_name(file
, var
);
2621 unsigned int size
, align
= 0;
2622 type_t
*type
= var
->type
;
2624 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
2625 LIST_FOR_EACH_ENTRY( expr
, size_is
, const expr_t
, entry
)
2627 if (expr
->type
== EXPR_VOID
) continue;
2628 write_expr( file
, expr
, 1 );
2629 fprintf(file
, " * ");
2631 size
= type_memsize(type
, NULL
, &align
);
2632 fprintf(file
, "%u);\n", size
);
2634 else if (!is_string
)
2636 fprintf(file
, " = &_W%u;\n", i
);
2637 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
2638 print_file(file
, indent
, "_W%u = 0;\n", i
);
2646 fprintf(file
, "\n");
2650 int write_expr_eval_routines(FILE *file
, const char *iface
)
2653 struct expr_eval_routine
*eval
;
2654 unsigned short callback_offset
= 0;
2656 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2660 print_file(file
, indent
, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2661 iface
, eval
->structure
->name
, callback_offset
);
2662 print_file(file
, indent
, "{\n");
2664 print_file(file
, indent
, "struct %s *" STRUCT_EXPR_EVAL_VAR
" = (struct %s *)(pStubMsg->StackTop - %u);\n",
2665 eval
->structure
->name
, eval
->structure
->name
, eval
->structure_size
);
2666 fprintf(file
, "\n");
2667 print_file(file
, indent
, "pStubMsg->Offset = 0;\n"); /* FIXME */
2668 print_file(file
, indent
, "pStubMsg->MaxCount = (unsigned long)");
2669 write_struct_expr(file
, eval
->expr
, 1, eval
->structure
->fields
, STRUCT_EXPR_EVAL_VAR
);
2670 fprintf(file
, ";\n");
2672 print_file(file
, indent
, "}\n\n");
2678 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
2680 struct expr_eval_routine
*eval
;
2681 struct expr_eval_routine
*cursor
;
2682 unsigned short callback_offset
= 0;
2684 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2685 fprintf(file
, "{\n");
2687 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2689 print_file(file
, 1, "%s_%sExprEval_%04u,\n",
2690 iface
, eval
->structure
->name
, callback_offset
);
2693 list_remove(&eval
->entry
);
2697 fprintf(file
, "};\n\n");
2701 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
2703 const struct str_list_entry_t
*endpoint
;
2706 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2707 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
2708 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
2710 print_file( f
, 1, "{ (const unsigned char *)\"" );
2711 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
2713 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
2716 if (!*p
) goto error
;
2717 if (p
[1] != '[') goto error
;
2719 fprintf( f
, "\", (const unsigned char *)\"" );
2720 for (p
+= 2; *p
&& *p
!= ']'; p
++)
2722 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
2725 if (*p
!= ']') goto error
;
2726 fprintf( f
, "\" },\n" );
2728 print_file( f
, 0, "};\n\n" );
2732 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);