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
, int ptr_level
, const array_dims_t
*array
);
63 static size_t fields_memsize(const var_list_t
*fields
);
65 static int compare_expr(const expr_t
*a
, const expr_t
*b
)
69 if (a
->type
!= b
->type
)
70 return a
->type
- b
->type
;
77 return a
->u
.lval
- b
->u
.lval
;
79 return strcmp(a
->u
.sval
, b
->u
.sval
);
81 ret
= compare_expr(a
->ref
, b
->ref
);
84 ret
= compare_expr(a
->u
.ext
, b
->u
.ext
);
87 return compare_expr(a
->ext2
, b
->ext2
);
96 ret
= compare_expr(a
->ref
, b
->ref
);
99 return compare_expr(a
->u
.ext
, b
->u
.ext
);
105 return compare_expr(a
->ref
, b
->ref
);
112 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
115 fprintf(file, "/* %2u */\n", typestring_offset); \
116 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
120 static int print_file(FILE *file
, int indent
, const char *format
, ...)
127 va_start(va
, format
);
128 for (i
= 0; i
< indent
; i
++)
130 r
= vfprintf(file
, format
, va
);
135 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
137 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
138 print_file(f
, indent
, "{\n");
139 print_file(f
, indent
+ 1, "short Pad;\n");
140 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
141 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
142 print_file(f
, indent
, "\n");
145 void write_formatstringsdecl(FILE *f
, int indent
, ifref_list_t
*ifaces
, int for_objects
)
147 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
148 get_size_typeformatstring(ifaces
, for_objects
));
150 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
151 get_size_procformatstring(ifaces
, for_objects
));
154 write_formatdesc(f
, indent
, "TYPE");
155 write_formatdesc(f
, indent
, "PROC");
157 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
158 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
159 print_file(f
, indent
, "\n");
162 static inline int is_base_type(unsigned char type
)
181 case RPC_FC_ERROR_STATUS_T
:
182 case RPC_FC_BIND_PRIMITIVE
:
190 static size_t write_procformatstring_var(FILE *file
, int indent
,
191 const var_t
*var
, int is_return
, unsigned int *type_offset
)
194 int ptr_level
= var
->ptr_level
;
195 const type_t
*type
= var
->type
;
197 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
198 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
200 if (!is_in
&& !is_out
) is_in
= TRUE
;
202 if (ptr_level
== 0 && !var
->array
&& is_base_type(type
->type
))
205 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
207 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
211 #define CASE_BASETYPE(fctype) \
213 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
214 size = 2; /* includes param type prefix */ \
217 CASE_BASETYPE(FC_BYTE
);
218 CASE_BASETYPE(FC_CHAR
);
219 CASE_BASETYPE(FC_WCHAR
);
220 CASE_BASETYPE(FC_USHORT
);
221 CASE_BASETYPE(FC_SHORT
);
222 CASE_BASETYPE(FC_ULONG
);
223 CASE_BASETYPE(FC_LONG
);
224 CASE_BASETYPE(FC_HYPER
);
225 CASE_BASETYPE(FC_IGNORE
);
226 CASE_BASETYPE(FC_USMALL
);
227 CASE_BASETYPE(FC_SMALL
);
228 CASE_BASETYPE(FC_FLOAT
);
229 CASE_BASETYPE(FC_DOUBLE
);
230 CASE_BASETYPE(FC_ERROR_STATUS_T
);
233 case RPC_FC_BIND_PRIMITIVE
:
234 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
235 size
= 2; /* includes param type prefix */
239 error("Unknown/unsupported type: %s (0x%02x)\n", var
->name
, type
->type
);
246 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
247 else if (is_in
&& is_out
)
248 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
250 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
252 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
254 print_file(file
, indent
, "0x01,\n");
255 print_file(file
, indent
, "NdrFcShort(0x%x),\n", *type_offset
);
256 size
= 4; /* includes param type prefix */
258 *type_offset
+= get_size_typeformatstring_var(var
);
262 void write_procformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
264 const ifref_t
*iface
;
267 unsigned int type_offset
= 2;
269 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
270 print_file(file
, indent
, "{\n");
272 print_file(file
, indent
, "0,\n");
273 print_file(file
, indent
, "{\n");
276 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
278 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
281 if (iface
->iface
->funcs
)
284 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
286 if (is_local(func
->def
->attrs
)) continue;
287 /* emit argument data */
290 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
291 write_procformatstring_var(file
, indent
, var
, FALSE
, &type_offset
);
294 /* emit return value data */
296 if (is_void(var
->type
, NULL
))
298 print_file(file
, indent
, "0x5b, /* FC_END */\n");
299 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
302 write_procformatstring_var(file
, indent
, var
, TRUE
,
308 print_file(file
, indent
, "0x0\n");
310 print_file(file
, indent
, "}\n");
312 print_file(file
, indent
, "};\n");
313 print_file(file
, indent
, "\n");
316 /* write conformance / variance descriptor */
317 static size_t write_conf_or_var_desc(FILE *file
, const func_t
*func
, const type_t
*structure
, const expr_t
*expr
)
319 unsigned char operator_type
= 0;
320 const char *operator_string
= "no operators";
321 const expr_t
*subexpr
= expr
;
322 unsigned char correlation_type
;
324 if (!file
) return 4; /* optimisation for sizing pass */
328 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
329 error("write_conf_or_var_desc: constant value %ld is greater than "
330 "the maximum constant size of %d\n", expr
->cval
,
331 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
333 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
334 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
335 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
336 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
341 switch (subexpr
->type
)
344 subexpr
= subexpr
->ref
;
345 operator_type
= RPC_FC_DEREFERENCE
;
346 operator_string
= "FC_DEREFERENCE";
349 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
351 subexpr
= subexpr
->ref
;
352 operator_type
= RPC_FC_DIV_2
;
353 operator_string
= "FC_DIV_2";
357 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
359 subexpr
= subexpr
->ref
;
360 operator_type
= RPC_FC_MULT_2
;
361 operator_string
= "FC_MULT_2";
365 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
367 subexpr
= subexpr
->ref
;
368 operator_type
= RPC_FC_SUB_1
;
369 operator_string
= "FC_SUB_1";
373 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
375 subexpr
= subexpr
->ref
;
376 operator_type
= RPC_FC_ADD_1
;
377 operator_string
= "FC_ADD_1";
384 if (subexpr
->type
== EXPR_IDENTIFIER
)
386 const type_t
*correlation_variable
= NULL
;
387 unsigned char correlation_variable_type
;
388 unsigned char param_type
= 0;
389 const char *param_type_string
= NULL
;
397 if (structure
->fields
) LIST_FOR_EACH_ENTRY( var
, structure
->fields
, const var_t
, entry
)
399 offset
-= type_memsize(var
->type
, var
->ptr_level
, var
->array
);
400 if (!strcmp(var
->name
, subexpr
->u
.sval
))
402 correlation_variable
= var
->type
;
406 if (!correlation_variable
)
407 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
410 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
417 if (func
->args
) LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
419 if (!strcmp(var
->name
, subexpr
->u
.sval
))
421 correlation_variable
= var
->type
;
424 /* FIXME: not all stack variables are sizeof(void *) */
425 offset
+= sizeof(void *);
427 if (!correlation_variable
)
428 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
431 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
434 correlation_variable_type
= correlation_variable
->type
;
436 switch (correlation_variable_type
)
440 param_type
= RPC_FC_SMALL
;
441 param_type_string
= "FC_SMALL";
445 param_type
= RPC_FC_USMALL
;
446 param_type_string
= "FC_USMALL";
450 param_type
= RPC_FC_SHORT
;
451 param_type_string
= "FC_SHORT";
454 param_type
= RPC_FC_USHORT
;
455 param_type_string
= "FC_USHORT";
458 param_type
= RPC_FC_LONG
;
459 param_type_string
= "FC_LONG";
462 param_type
= RPC_FC_ULONG
;
463 param_type_string
= "FC_ULONG";
466 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
467 correlation_variable_type
);
470 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
471 correlation_type
| param_type
,
472 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter, " : "",
474 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
475 print_file(file
, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
477 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "x86 stack size / " : "",
482 unsigned int callback_offset
= 0;
486 struct expr_eval_routine
*eval
;
489 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
491 if (!strcmp(eval
->structure
->name
, structure
->name
) &&
492 !compare_expr(eval
->expr
, expr
))
502 eval
= xmalloc(sizeof(*eval
));
503 eval
->structure
= structure
;
504 eval
->structure_size
= fields_memsize(structure
->fields
);
506 list_add_tail(&expr_eval_routines
, &eval
->entry
);
509 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
513 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
514 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
517 if (callback_offset
> USHRT_MAX
)
518 error("Maximum number of callback routines reached\n");
520 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n",
522 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter" : "");
523 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
524 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
529 static size_t fields_memsize(const var_list_t
*fields
)
534 if (!fields
) return 0;
535 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
536 size
+= type_memsize(v
->type
, v
->ptr_level
, v
->array
);
541 static size_t type_memsize(const type_t
*t
, int ptr_level
, const array_dims_t
*array
)
546 return sizeof(void *);
564 case RPC_FC_ERROR_STATUS_T
:
574 case RPC_FC_CVSTRUCT
:
575 case RPC_FC_CPSTRUCT
:
578 case RPC_FC_BOGUS_STRUCT
:
579 case RPC_FC_ENCAPSULATED_UNION
:
580 case RPC_FC_NON_ENCAPSULATED_UNION
:
581 size
= fields_memsize(t
->fields
);
584 error("type_memsize: Unknown type %d\n", t
->type
);
591 LIST_FOR_EACH_ENTRY( dim
, array
, expr_t
, entry
)
601 size_t get_type_memsize(const type_t
*type
)
603 return type_memsize(type
, 0, NULL
);
606 static int write_pointers(FILE *file
, const attr_list_t
*attrs
,
607 const type_t
*type
, int ptr_level
,
608 const array_dims_t
*array
, int level
,
609 unsigned int *typestring_offset
)
611 int pointers_written
= 0;
614 /* don't generate a pointer for first-level arrays since we want to
615 * descend into them to write their pointers, not stop here */
616 if ((level
== 0 || ptr_level
== 0) && is_array_type(attrs
, ptr_level
, array
))
618 return write_pointers(file
, NULL
, type
, 0, NULL
, level
+ 1, typestring_offset
);
623 /* FIXME: only general algorithm implemented, not the actual writing */
624 error("write_pointers: Writing type format string for pointer is unimplemented\n");
630 /* note: don't descend into complex structures or unions since these
631 * will always be generated as a separate type */
633 case RPC_FC_CVSTRUCT
:
634 case RPC_FC_CPSTRUCT
:
637 if (!type
->fields
) break;
638 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
639 pointers_written
+= write_pointers(file
, v
->attrs
, v
->type
,
640 v
->ptr_level
, v
->array
,
651 return pointers_written
;
654 static size_t write_pointer_description(FILE *file
, const attr_list_t
*attrs
,
655 const type_t
*type
, int ptr_level
,
656 const array_dims_t
*array
, int level
,
657 size_t typestring_offset
)
662 /* don't generate a pointer for first-level arrays since we want to
663 * descend into them to write their pointers, not stop here */
664 if ((level
== 0 || ptr_level
== 0) && is_array_type(attrs
, ptr_level
, array
))
666 return write_pointer_description(file
, NULL
, type
, 0, NULL
,
667 level
+ 1, typestring_offset
);
672 /* FIXME: only general algorithm implemented, not the actual writing */
673 error("write_pointer_description: Writing pointer description is unimplemented\n");
677 /* FIXME: search through all refs for pointers too */
681 /* note: don't descend into complex structures or unions since these
682 * will always be generated as a separate type */
684 case RPC_FC_CVSTRUCT
:
685 case RPC_FC_CPSTRUCT
:
688 if (!type
->fields
) break;
689 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
690 size
+= write_pointer_description(file
, v
->attrs
, v
->type
,
691 v
->ptr_level
, v
->array
,
705 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
706 const type_t
*type
, const array_dims_t
*array
,
707 const char *name
, unsigned int *typestring_offset
)
709 const expr_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
710 int has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
);
711 size_t start_offset
= *typestring_offset
;
712 unsigned char flags
= 0;
718 pointer_type
= type
->type
;
722 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
725 pointer_type
= RPC_FC_RP
;
727 if (!get_attrp(attrs
, ATTR_SIZEIS
))
728 flags
|= RPC_FC_P_SIMPLEPOINTER
;
732 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
734 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
738 print_file(file
, 2,"0x%x, 0x%x, /* %s%s */\n",
740 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"),
741 (flags
& RPC_FC_P_SIMPLEPOINTER
) ? " [simple_pointer]" : "");
742 *typestring_offset
+= 2;
744 if (!(flags
& RPC_FC_P_SIMPLEPOINTER
))
746 print_file(file
, 2, "NdrFcShort(0x2),\n");
747 *typestring_offset
+= 2;
750 if (array
&& !is_conformant_array(array
))
752 /* FIXME: multi-dimensional array */
753 const expr_t
*dim
= LIST_ENTRY( list_head( array
), expr_t
, entry
);
754 if (dim
->cval
> USHRT_MAX
)
755 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
756 name
, USHRT_MAX
, dim
->cval
- USHRT_MAX
);
758 if (rtype
== RPC_FC_CHAR
)
759 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
761 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
762 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
763 *typestring_offset
+= 2;
765 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", dim
->cval
, dim
->cval
);
766 *typestring_offset
+= 2;
772 if (rtype
== RPC_FC_CHAR
)
773 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
775 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
776 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
777 *typestring_offset
+= 2;
779 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
, NULL
, size_is
);
785 if (rtype
== RPC_FC_CHAR
)
786 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
788 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
789 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
790 *typestring_offset
+= 2;
796 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
,
797 const type_t
*type
, const array_dims_t
*array
,
798 const char *name
, unsigned int *typestring_offset
)
800 const expr_t
*length_is
= get_attrp(attrs
, ATTR_LENGTHIS
);
801 const expr_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
802 int has_length
= length_is
&& (length_is
->type
!= EXPR_VOID
);
803 int has_size
= (size_is
&& (size_is
->type
!= EXPR_VOID
)) || is_conformant_array(array
);
805 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
807 pointer_type
= RPC_FC_RP
;
809 print_file(file
, 2, "0x%x, 0x00, /* %s */\n",
811 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"));
812 print_file(file
, 2, "NdrFcShort(0x2),\n");
813 *typestring_offset
+= 4;
815 if (array
&& list_count(array
) > 1) /* multi-dimensional array */
817 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name
);
822 const expr_t
*dim
= array
? LIST_ENTRY( list_head( array
), expr_t
, entry
) : NULL
;
823 size_t pointer_start_offset
= *typestring_offset
;
826 if (write_pointers(file
, attrs
, type
, 0, array
, 0, typestring_offset
) > 0)
829 start_offset
= *typestring_offset
;
831 if (!has_length
&& !has_size
)
834 size_t size
= type_memsize(type
, 0, array
);
835 if (size
< USHRT_MAX
)
837 WRITE_FCTYPE(file
, FC_SMFARRAY
, *typestring_offset
);
839 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
841 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", size
, size
);
842 *typestring_offset
+= 4;
846 WRITE_FCTYPE(file
, FC_LGFARRAY
, *typestring_offset
);
848 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
850 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", size
, size
);
851 *typestring_offset
+= 6;
856 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
857 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
858 *typestring_offset
+= 2;
859 *typestring_offset
= write_pointer_description(file
, attrs
,
860 type
, 0, array
, 0, pointer_start_offset
);
861 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
862 *typestring_offset
+= 1;
865 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
866 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
867 *typestring_offset
+= 2;
871 else if (has_length
&& !has_size
)
874 size_t element_size
= type_memsize(type
, 0, NULL
);
875 size_t elements
= dim
->cval
;
876 size_t total_size
= element_size
* elements
;
878 if (total_size
< USHRT_MAX
)
880 WRITE_FCTYPE(file
, FC_SMVARRAY
, *typestring_offset
);
882 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
884 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", total_size
, total_size
);
885 /* number of elements */
886 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", elements
, elements
);
887 *typestring_offset
+= 6;
891 WRITE_FCTYPE(file
, FC_LGVARRAY
, *typestring_offset
);
893 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
895 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", total_size
, total_size
);
896 /* number of elements */
897 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", elements
, elements
);
898 *typestring_offset
+= 10;
901 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
902 *typestring_offset
+= 2;
904 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
910 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
911 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
912 *typestring_offset
+= 2;
913 *typestring_offset
+= write_pointer_description(file
, attrs
,
914 type
, 0, array
, 0, pointer_start_offset
);
915 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
916 *typestring_offset
+= 1;
919 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
920 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
921 *typestring_offset
+= 2;
925 else if (!has_length
&& has_size
)
927 /* conformant array */
928 size_t element_size
= type_memsize(type
, 0, NULL
);
930 WRITE_FCTYPE(file
, FC_CARRAY
, *typestring_offset
);
932 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
934 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
935 *typestring_offset
+= 4;
937 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
939 size_is
? size_is
: dim
);
943 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
944 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
945 *typestring_offset
+= 2;
946 *typestring_offset
+= write_pointer_description(file
, attrs
,
947 type
, 0, array
, 0, pointer_start_offset
);
948 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
949 *typestring_offset
+= 1;
952 print_file(file
, 2, "0x%x, /* FIXME: write out conversion data */\n", type
->type
);
953 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
954 *typestring_offset
+= 2;
960 /* conformant varying array */
961 size_t element_size
= type_memsize(type
, 0, NULL
);
963 WRITE_FCTYPE(file
, FC_CVARRAY
, *typestring_offset
);
965 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
967 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
968 *typestring_offset
+= 4;
970 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
972 size_is
? size_is
: dim
);
973 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
979 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
980 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
981 *typestring_offset
+= 2;
982 *typestring_offset
+= write_pointer_description(file
, attrs
,
983 type
, 0, array
, 0, pointer_start_offset
);
984 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
985 *typestring_offset
+= 1;
988 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
989 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
990 *typestring_offset
+= 2;
997 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
999 const var_t
*last_field
= LIST_ENTRY( list_tail(type
->fields
), const var_t
, entry
);
1001 if (is_array_type(last_field
->attrs
, last_field
->ptr_level
, last_field
->array
))
1004 assert((last_field
->type
->type
== RPC_FC_CSTRUCT
) ||
1005 (last_field
->type
->type
== RPC_FC_CPSTRUCT
) ||
1006 (last_field
->type
->type
== RPC_FC_CVSTRUCT
));
1008 return find_array_or_string_in_struct(last_field
->type
);
1011 static size_t write_struct_members(FILE *file
, const type_t
*type
)
1013 size_t typestring_size
= 0;
1016 if (type
->fields
) LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1018 unsigned char rtype
= field
->type
->type
;
1020 if (is_base_type(rtype
))
1024 #define CASE_BASETYPE(fctype) \
1025 case RPC_##fctype: \
1026 print_file(file, 2, "0x%02x,\t\t/* " #fctype " */\n", RPC_##fctype); \
1027 typestring_size++; \
1029 CASE_BASETYPE(FC_BYTE
);
1030 CASE_BASETYPE(FC_CHAR
);
1031 CASE_BASETYPE(FC_SMALL
);
1032 CASE_BASETYPE(FC_USMALL
);
1033 CASE_BASETYPE(FC_WCHAR
);
1034 CASE_BASETYPE(FC_SHORT
);
1035 CASE_BASETYPE(FC_USHORT
);
1036 CASE_BASETYPE(FC_LONG
);
1037 CASE_BASETYPE(FC_ULONG
);
1038 CASE_BASETYPE(FC_FLOAT
);
1039 CASE_BASETYPE(FC_HYPER
);
1040 CASE_BASETYPE(FC_DOUBLE
);
1041 CASE_BASETYPE(FC_ENUM16
);
1042 CASE_BASETYPE(FC_ENUM32
);
1043 CASE_BASETYPE(FC_IGNORE
);
1044 CASE_BASETYPE(FC_ERROR_STATUS_T
);
1047 #undef CASE_BASETYPE
1051 error("Unsupported member type 0x%x\n", rtype
);
1054 if (!(typestring_size
% 2))
1056 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1060 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1063 return typestring_size
;
1066 static size_t write_struct_tfs(FILE *file
, const type_t
*type
,
1067 const char *name
, unsigned int *typestring_offset
)
1069 unsigned int total_size
;
1071 size_t start_offset
;
1072 size_t array_offset
;
1073 size_t pointer_offset
;
1078 case RPC_FC_PSTRUCT
:
1079 total_size
= type_memsize(type
, 0, NULL
);
1081 if (total_size
> USHRT_MAX
)
1082 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1083 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1085 if (type
->type
== RPC_FC_PSTRUCT
)
1087 pointer_offset
= *typestring_offset
;
1088 write_pointers(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1090 else pointer_offset
= 0; /* silence warning */
1092 start_offset
= *typestring_offset
;
1093 if (type
->type
== RPC_FC_STRUCT
)
1094 WRITE_FCTYPE(file
, FC_STRUCT
, *typestring_offset
);
1096 WRITE_FCTYPE(file
, FC_PSTRUCT
, *typestring_offset
);
1098 print_file(file
, 2, "0x3,\n"); /* FIXME */
1100 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1101 *typestring_offset
+= 4;
1103 if (type
->type
== RPC_FC_PSTRUCT
)
1105 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1106 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1107 *typestring_offset
+= 2;
1108 *typestring_offset
+= write_pointer_description(file
, NULL
,
1109 type
, 0, NULL
, 0, pointer_offset
);
1110 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1111 *typestring_offset
+= 1;
1115 *typestring_offset
+= write_struct_members(file
, type
);
1116 return start_offset
;
1117 case RPC_FC_CSTRUCT
:
1118 case RPC_FC_CPSTRUCT
:
1119 total_size
= type_memsize(type
, 0, NULL
);
1121 if (total_size
> USHRT_MAX
)
1122 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1123 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1125 array
= find_array_or_string_in_struct(type
);
1126 current_structure
= type
;
1127 array_offset
= write_array_tfs(file
, array
->attrs
, array
->type
,
1128 array
->array
, array
->name
,
1130 current_structure
= NULL
;
1132 if (type
->type
== RPC_FC_CPSTRUCT
)
1134 pointer_offset
= *typestring_offset
;
1135 write_pointers(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1137 else pointer_offset
= 0; /* silence warning */
1139 start_offset
= *typestring_offset
;
1140 if (type
->type
== RPC_FC_CSTRUCT
)
1141 WRITE_FCTYPE(file
, FC_CSTRUCT
, *typestring_offset
);
1143 WRITE_FCTYPE(file
, FC_CPSTRUCT
, *typestring_offset
);
1145 print_file(file
, 2, "0x0,\n");
1147 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1148 *typestring_offset
+= 4;
1149 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1150 array_offset
- *typestring_offset
,
1151 array_offset
- *typestring_offset
,
1153 *typestring_offset
+= 2;
1155 if (type
->type
== RPC_FC_CPSTRUCT
)
1157 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1158 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1159 *typestring_offset
+= 2;
1160 *typestring_offset
+= write_pointer_description(file
, NULL
,
1161 type
, 0, NULL
, 0, pointer_offset
);
1162 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1163 *typestring_offset
+= 1;
1166 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1167 *typestring_offset
+= 1;
1169 return start_offset
;
1170 case RPC_FC_CVSTRUCT
:
1171 total_size
= type_memsize(type
, 0, NULL
);
1173 if (total_size
> USHRT_MAX
)
1174 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
1175 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1177 array
= find_array_or_string_in_struct(type
);
1178 current_structure
= type
;
1179 if (is_attr(array
->attrs
, ATTR_STRING
))
1180 array_offset
= write_string_tfs(file
, array
->attrs
, array
->type
,
1181 array
->array
, array
->name
,
1184 array_offset
= write_array_tfs(file
, array
->attrs
, array
->type
,
1185 array
->array
, array
->name
,
1187 current_structure
= NULL
;
1189 pointer_offset
= *typestring_offset
;
1190 if (!write_pointers(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
))
1193 start_offset
= *typestring_offset
;
1194 WRITE_FCTYPE(file
, FC_CVSTRUCT
, *typestring_offset
);
1196 print_file(file
, 2, "0x0,\n");
1198 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1199 *typestring_offset
+= 4;
1200 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1201 array_offset
- *typestring_offset
,
1202 array_offset
- *typestring_offset
,
1204 *typestring_offset
+= 2;
1206 if (pointer_offset
!= 0)
1208 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1209 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1210 *typestring_offset
+= 2;
1211 *typestring_offset
+= write_pointer_description(file
, NULL
,
1212 type
, 0, NULL
, 0, pointer_offset
);
1213 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1214 *typestring_offset
+= 1;
1217 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1218 *typestring_offset
+= 1;
1220 return start_offset
;
1222 error("write_struct_tfs: Unimplemented for type 0x%x\n", type
->type
);
1223 return *typestring_offset
;
1227 static void write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1228 unsigned char flags
, size_t offset
,
1229 unsigned int *typeformat_offset
)
1231 int in_attr
, out_attr
;
1232 in_attr
= is_attr(attrs
, ATTR_IN
);
1233 out_attr
= is_attr(attrs
, ATTR_OUT
);
1234 if (!in_attr
&& !out_attr
) in_attr
= 1;
1236 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1239 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1242 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"));
1246 fprintf(file
, " [allocated_on_stack]");
1248 fprintf(file
, " [pointer_deref]");
1249 fprintf(file
, " */\n");
1252 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", offset
, offset
);
1253 *typeformat_offset
+= 4;
1256 static size_t write_union_tfs(FILE *file
, const attr_list_t
*attrs
,
1257 const type_t
*type
, const char *name
,
1258 unsigned int *typeformat_offset
)
1260 error("write_union_tfs: Unimplemented\n");
1261 return *typeformat_offset
;
1264 static size_t write_ip_tfs(FILE *file
, const attr_list_t
*attrs
,
1265 const char *name
, unsigned int *typeformat_offset
)
1268 size_t start_offset
= *typeformat_offset
;
1269 const UUID
*uuid
= get_attrp(attrs
, ATTR_UUID
);
1272 error("%s: interface %s missing UUID\n", __FUNCTION__
, name
);
1274 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
1275 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1276 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
1277 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
1278 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
1279 for (i
= 0; i
< 8; ++i
)
1280 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
1283 fprintf(file
, "\n");
1285 *typeformat_offset
+= 18;
1286 return start_offset
;
1289 static int get_ptr_attr(const type_t
*t
, int def_type
)
1293 int ptr_attr
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
1296 if (t
->kind
!= TKIND_ALIAS
)
1302 static size_t write_typeformatstring_var(FILE *file
, int indent
,
1303 const var_t
*var
, unsigned int *typeformat_offset
)
1305 const type_t
*type
= var
->type
;
1306 int var_ptrs
= var
->ptr_level
, type_ptrs
= 0;
1307 int is_str
= is_attr(var
->attrs
, ATTR_STRING
);
1309 chat("write_typeformatstring_var: %s\n", var
->name
);
1313 is_str
= is_str
|| is_attr(type
->attrs
, ATTR_STRING
);
1314 if (type
->kind
== TKIND_ALIAS
)
1316 else if (is_ptr(type
))
1330 int ptr_level
= var_ptrs
+ type_ptrs
;
1331 int pointer_type
= 0;
1333 chat("write_typeformatstring: type->type = 0x%x, type->name = %s, ptr_level = %d\n", type
->type
, type
->name
, ptr_level
);
1335 /* var attrs only effect the rightmost pointer */
1336 if ((0 < var
->ptr_level
&& var_ptrs
== var
->ptr_level
)
1337 || (var
->ptr_level
== 0 && type
== var
->type
))
1339 int pointer_attr
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1343 error("'%s': pointer attribute applied to non-pointer type",
1345 pointer_type
= pointer_attr
;
1348 pointer_type
= RPC_FC_RP
;
1350 else /* pointers below other pointers default to unique */
1351 pointer_type
= var_ptrs
? RPC_FC_UP
: get_ptr_attr(type
, RPC_FC_UP
);
1353 if (is_str
&& ptr_level
+ (var
->array
!= NULL
) == 1)
1354 return write_string_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
1356 if (is_array_type(var
->attrs
, ptr_level
, var
->array
))
1357 return write_array_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
1361 /* basic types don't need a type format string */
1362 if (is_base_type(type
->type
))
1368 case RPC_FC_PSTRUCT
:
1369 case RPC_FC_CSTRUCT
:
1370 case RPC_FC_CPSTRUCT
:
1371 case RPC_FC_CVSTRUCT
:
1372 case RPC_FC_BOGUS_STRUCT
:
1373 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
1374 case RPC_FC_ENCAPSULATED_UNION
:
1375 case RPC_FC_NON_ENCAPSULATED_UNION
:
1376 return write_union_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
1378 case RPC_FC_BIND_PRIMITIVE
:
1382 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
1385 else if (ptr_level
== 1)
1387 size_t start_offset
= *typeformat_offset
;
1388 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1389 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1390 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
1392 if (base
->type
== RPC_FC_IP
)
1394 return write_ip_tfs(file
, base
->attrs
, base
->name
, typeformat_offset
);
1397 /* special case for pointers to base types */
1400 #define CASE_BASETYPE(fctype) \
1401 case RPC_##fctype: \
1402 print_file(file, indent, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n", \
1404 (!in_attr && out_attr) ? 0x0C : 0x08, \
1405 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP"), \
1406 (!in_attr && out_attr) ? "[allocated_on_stack] " : ""); \
1407 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
1408 print_file(file, indent, "0x5c, /* FC_PAD */\n"); \
1409 *typeformat_offset += 4; \
1411 CASE_BASETYPE(FC_BYTE
);
1412 CASE_BASETYPE(FC_CHAR
);
1413 CASE_BASETYPE(FC_SMALL
);
1414 CASE_BASETYPE(FC_USMALL
);
1415 CASE_BASETYPE(FC_WCHAR
);
1416 CASE_BASETYPE(FC_SHORT
);
1417 CASE_BASETYPE(FC_USHORT
);
1418 CASE_BASETYPE(FC_LONG
);
1419 CASE_BASETYPE(FC_ULONG
);
1420 CASE_BASETYPE(FC_FLOAT
);
1421 CASE_BASETYPE(FC_HYPER
);
1422 CASE_BASETYPE(FC_DOUBLE
);
1423 CASE_BASETYPE(FC_ENUM16
);
1424 CASE_BASETYPE(FC_ENUM32
);
1425 CASE_BASETYPE(FC_IGNORE
);
1426 CASE_BASETYPE(FC_ERROR_STATUS_T
);
1432 assert(ptr_level
> 0);
1435 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
1436 write_pointer_only_tfs(file
, var
->attrs
, pointer_type
,
1437 1 < ptr_level
? 0x10 : 0,
1438 2, typeformat_offset
);
1451 void write_typeformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
1455 unsigned int typeformat_offset
;
1456 const ifref_t
*iface
;
1458 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1459 print_file(file
, indent
, "{\n");
1461 print_file(file
, indent
, "0,\n");
1462 print_file(file
, indent
, "{\n");
1464 print_file(file
, indent
, "NdrFcShort(0x0),\n");
1465 typeformat_offset
= 2;
1467 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
1469 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
1472 if (iface
->iface
->funcs
)
1475 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
1477 if (is_local(func
->def
->attrs
)) continue;
1478 current_func
= func
;
1480 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1481 write_typeformatstring_var(file
, indent
, var
,
1482 &typeformat_offset
);
1487 print_file(file
, indent
, "0x0\n");
1489 print_file(file
, indent
, "}\n");
1491 print_file(file
, indent
, "};\n");
1492 print_file(file
, indent
, "\n");
1495 static unsigned int get_required_buffer_size_type(
1496 const type_t
*type
, int ptr_level
, const array_dims_t
*array
,
1497 const char *name
, unsigned int *alignment
)
1500 if (ptr_level
== 0 && !array
)
1520 case RPC_FC_ERROR_STATUS_T
:
1530 case RPC_FC_BIND_PRIMITIVE
:
1537 if (!type
->fields
) return 0;
1538 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1540 unsigned int alignment
;
1541 size
+= get_required_buffer_size_type(
1542 field
->type
, field
->ptr_level
, field
->array
, field
->name
,
1549 if (is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
)
1550 return get_required_buffer_size_type( type
->ref
, 0, NULL
, name
, alignment
);
1554 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
1561 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
1563 expr_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
1564 int has_size
= (size_is
&& (size_is
->type
!= EXPR_VOID
));
1565 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1566 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1568 if (!in_attr
&& !out_attr
)
1573 if (pass
== PASS_OUT
)
1575 if (out_attr
&& var
->ptr_level
> 0)
1577 type_t
*type
= var
->type
;
1579 if (type
->type
== RPC_FC_STRUCT
)
1582 unsigned int size
= 36;
1584 if (!type
->fields
) return size
;
1585 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1588 size
+= get_required_buffer_size_type(
1589 field
->type
, field
->ptr_level
, field
->array
, field
->name
,
1599 if ((!out_attr
|| in_attr
) && !has_size
&& !is_attr(var
->attrs
, ATTR_STRING
) && !var
->array
)
1601 if (var
->ptr_level
> 0)
1603 type_t
*type
= var
->type
;
1605 if (is_base_type(type
->type
))
1609 else if (type
->type
== RPC_FC_STRUCT
)
1611 unsigned int size
= 36;
1614 if (!type
->fields
) return size
;
1615 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1618 size
+= get_required_buffer_size_type(
1619 field
->type
, field
->ptr_level
, field
->array
, field
->name
,
1627 return get_required_buffer_size_type(var
->type
, var
->ptr_level
, var
->array
, var
->name
, alignment
);
1631 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
1634 unsigned int total_size
= 0, alignment
;
1638 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1640 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
1641 total_size
+= alignment
;
1645 if (pass
== PASS_OUT
&& !is_void(func
->def
->type
, NULL
))
1647 total_size
+= get_required_buffer_size(func
->def
, &alignment
, PASS_RETURN
);
1648 total_size
+= alignment
;
1653 static void print_phase_function(FILE *file
, int indent
, const char *type
,
1654 enum remoting_phase phase
,
1655 const char *varname
, unsigned int type_offset
)
1657 const char *function
;
1660 case PHASE_BUFFERSIZE
:
1661 function
= "BufferSize";
1664 function
= "Marshall";
1666 case PHASE_UNMARSHAL
:
1667 function
= "Unmarshall";
1677 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
1679 print_file(file
, indent
, "&_StubMsg,\n");
1680 print_file(file
, indent
, "%s%s,\n",
1681 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)&" : "(unsigned char *)",
1683 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
1684 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
1685 if (phase
== PHASE_UNMARSHAL
)
1686 print_file(file
, indent
, "0);\n");
1690 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
1691 enum pass pass
, const var_t
*var
,
1692 const char *varname
)
1694 const type_t
*type
= var
->type
;
1696 unsigned int alignment
= 0;
1697 unsigned char rtype
;
1699 /* no work to do for other phases, buffer sizing is done elsewhere */
1700 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
1725 case RPC_FC_ERROR_STATUS_T
:
1737 case RPC_FC_BIND_PRIMITIVE
:
1738 /* no marshalling needed */
1742 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
1746 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
1747 alignment
- 1, alignment
- 1);
1749 if (phase
== PHASE_MARSHAL
)
1751 print_file(file
, indent
, "*(");
1752 write_type(file
, var
->type
, NULL
, var
->tname
);
1754 fprintf(file
, " *)_StubMsg.Buffer = *");
1756 fprintf(file
, " *)_StubMsg.Buffer = ");
1757 fprintf(file
, varname
);
1758 fprintf(file
, ";\n");
1760 else if (phase
== PHASE_UNMARSHAL
)
1762 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
1763 print_file(file
, indent
, "");
1765 print_file(file
, indent
, "*");
1766 fprintf(file
, varname
);
1767 if (pass
== PASS_IN
&& var
->ptr_level
)
1768 fprintf(file
, " = (");
1770 fprintf(file
, " = *(");
1771 write_type(file
, var
->type
, NULL
, var
->tname
);
1772 fprintf(file
, " *)_StubMsg.Buffer;\n");
1775 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
1776 write_type(file
, var
->type
, NULL
, var
->tname
);
1777 fprintf(file
, ");\n");
1780 /* returns whether the MaxCount, Offset or ActualCount members need to be
1781 * filled in for the specified phase */
1782 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
1784 return (phase
!= PHASE_UNMARSHAL
);
1787 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
1788 unsigned int *type_offset
, enum pass pass
,
1789 enum remoting_phase phase
)
1791 const expr_t
*length_is
;
1792 const expr_t
*size_is
;
1793 int in_attr
, out_attr
, has_length
, has_size
, pointer_type
;
1799 if (phase
== PHASE_BUFFERSIZE
)
1801 unsigned int size
= get_function_buffer_size( func
, pass
);
1802 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
1805 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1807 const type_t
*type
= var
->type
;
1808 unsigned char rtype
;
1810 length_is
= get_attrp(var
->attrs
, ATTR_LENGTHIS
);
1811 size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
1812 has_length
= length_is
&& (length_is
->type
!= EXPR_VOID
);
1813 has_size
= (size_is
&& (size_is
->type
!= EXPR_VOID
)) || (var
->array
&& is_conformant_array(var
->array
));
1815 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1817 pointer_type
= RPC_FC_RP
;
1819 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1820 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1821 if (!in_attr
&& !out_attr
)
1827 if (!in_attr
) goto next
;
1830 if (!out_attr
) goto next
;
1838 if (is_string_type(var
->attrs
, var
->ptr_level
, var
->array
))
1840 if (var
->array
&& !is_conformant_array(var
->array
))
1841 print_phase_function(file
, indent
, "NonConformantString", phase
, var
->name
, *type_offset
);
1844 if (size_is
&& is_size_needed_for_phase(phase
))
1846 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
1847 write_expr(file
, size_is
, 1);
1848 fprintf(file
, ";\n");
1851 if ((phase
== PHASE_FREE
) || (pointer_type
== RPC_FC_UP
))
1852 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, *type_offset
);
1854 print_phase_function(file
, indent
, "ConformantString", phase
, var
->name
,
1855 *type_offset
+ (has_size
? 4 : 2));
1858 else if (is_array_type(var
->attrs
, var
->ptr_level
, var
->array
))
1860 const char *array_type
;
1862 if (var
->array
&& list_count(var
->array
) > 1) /* multi-dimensional array */
1863 array_type
= "ComplexArray";
1866 const expr_t
*dim
= LIST_ENTRY( list_head( var
->array
), expr_t
, entry
);
1867 if (!has_length
&& !has_size
)
1868 array_type
= "FixedArray";
1869 else if (has_length
&& !has_size
)
1871 if (is_size_needed_for_phase(phase
))
1873 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1874 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
1875 write_expr(file
, length_is
, 1);
1876 fprintf(file
, ";\n\n");
1878 array_type
= "VaryingArray";
1880 else if (!has_length
&& has_size
)
1882 if (is_size_needed_for_phase(phase
) && phase
!= PHASE_FREE
)
1884 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
1885 write_expr(file
, size_is
? size_is
: dim
, 1);
1886 fprintf(file
, ";\n\n");
1888 array_type
= "ConformantArray";
1892 if (is_size_needed_for_phase(phase
))
1894 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
1895 write_expr(file
, size_is
? size_is
: dim
, 1);
1896 fprintf(file
, ";\n");
1897 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1898 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
1899 write_expr(file
, length_is
, 1);
1900 fprintf(file
, ";\n\n");
1902 array_type
= "ConformantVaryingArray";
1906 if (!in_attr
&& phase
== PHASE_FREE
)
1908 print_file(file
, indent
, "if (%s)\n", var
->name
);
1910 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
1912 else if (phase
!= PHASE_FREE
)
1914 if (pointer_type
== RPC_FC_UP
)
1915 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, *type_offset
);
1917 print_phase_function(file
, indent
, array_type
, phase
, var
->name
, *type_offset
+ 4);
1920 else if (var
->ptr_level
== 0 && is_base_type(rtype
))
1922 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
1924 else if (var
->ptr_level
== 0)
1929 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, *type_offset
);
1931 case RPC_FC_CSTRUCT
:
1932 case RPC_FC_CPSTRUCT
:
1933 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
->name
, *type_offset
);
1935 case RPC_FC_CVSTRUCT
:
1936 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
->name
, *type_offset
);
1938 case RPC_FC_BOGUS_STRUCT
:
1939 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
->name
, *type_offset
);
1942 if (is_base_type( var
->type
->ref
->type
))
1944 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
1946 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
1948 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
1949 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, *type_offset
+ 4);
1954 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
1955 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
1956 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, *type_offset
);
1960 error("write_remoting_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1961 var
->name
, rtype
, var
->ptr_level
);
1966 if ((var
->ptr_level
== 1) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
1968 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
1970 else if ((var
->ptr_level
== 1) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
1972 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
1973 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, *type_offset
+ 4);
1978 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
1979 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
1980 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, *type_offset
);
1983 fprintf(file
, "\n");
1985 *type_offset
+= get_size_typeformatstring_var(var
);
1990 size_t get_size_procformatstring_var(const var_t
*var
)
1992 unsigned int type_offset
= 2;
1993 return write_procformatstring_var(NULL
, 0, var
, FALSE
, &type_offset
);
1997 size_t get_size_procformatstring_func(const func_t
*func
)
2002 /* argument list size */
2004 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2005 size
+= get_size_procformatstring_var(var
);
2007 /* return value size */
2008 if (is_void(func
->def
->type
, NULL
))
2009 size
+= 2; /* FC_END and FC_PAD */
2011 size
+= get_size_procformatstring_var(func
->def
);
2016 size_t get_size_typeformatstring_var(const var_t
*var
)
2018 unsigned int type_offset
= 0;
2019 write_typeformatstring_var(NULL
, 0, var
, &type_offset
);
2023 size_t get_size_procformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2025 const ifref_t
*iface
;
2029 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2031 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2034 if (iface
->iface
->funcs
)
2035 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2036 if (!is_local(func
->def
->attrs
))
2037 size
+= get_size_procformatstring_func( func
);
2042 size_t get_size_typeformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2044 const ifref_t
*iface
;
2049 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2051 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2054 if (iface
->iface
->funcs
)
2056 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2058 if (is_local(func
->def
->attrs
)) continue;
2059 /* argument list size */
2061 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2062 size
+= get_size_typeformatstring_var(var
);
2069 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
2070 const var_list_t
*fields
, const char *structvar
)
2076 fprintf(h
, "%ld", e
->u
.lval
);
2079 fprintf(h
, "0x%lx", e
->u
.lval
);
2081 case EXPR_TRUEFALSE
:
2083 fprintf(h
, "FALSE");
2087 case EXPR_IDENTIFIER
:
2090 LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2091 if (!strcmp(e
->u
.sval
, field
->name
))
2093 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
2097 if (&field
->entry
== fields
) error("no field found for identifier %s\n", e
->u
.sval
);
2102 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2106 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2110 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2114 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
2116 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2119 fprintf(h
, "sizeof(");
2120 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
2131 if (brackets
) fprintf(h
, "(");
2132 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2134 case EXPR_SHL
: fprintf(h
, " << "); break;
2135 case EXPR_SHR
: fprintf(h
, " >> "); break;
2136 case EXPR_MUL
: fprintf(h
, " * "); break;
2137 case EXPR_DIV
: fprintf(h
, " / "); break;
2138 case EXPR_ADD
: fprintf(h
, " + "); break;
2139 case EXPR_SUB
: fprintf(h
, " - "); break;
2140 case EXPR_AND
: fprintf(h
, " & "); break;
2141 case EXPR_OR
: fprintf(h
, " | "); break;
2144 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2145 if (brackets
) fprintf(h
, ")");
2148 if (brackets
) fprintf(h
, "(");
2149 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2151 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2153 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
2154 if (brackets
) fprintf(h
, ")");
2160 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
2162 int in_attr
, out_attr
;
2164 const var_t
*def
= func
->def
;
2167 /* declare return value '_RetVal' */
2168 if (!is_void(def
->type
, NULL
))
2170 print_file(file
, indent
, "");
2171 write_type(file
, def
->type
, def
, def
->tname
);
2172 fprintf(file
, " _RetVal;\n");
2178 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2180 const expr_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2181 int has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
);
2182 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2184 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2185 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2186 if (!out_attr
&& !in_attr
)
2189 if (!in_attr
&& !has_size
&& !is_string
)
2192 print_file(file
, indent
, "");
2193 write_type(file
, var
->type
, NULL
, var
->tname
);
2194 for (indirection
= 0; indirection
< var
->ptr_level
- 1; indirection
++)
2196 fprintf(file
, " _W%u;\n", i
++);
2199 print_file(file
, indent
, "");
2200 write_type(file
, var
->type
, var
, var
->tname
);
2202 write_name(file
, var
);
2203 write_array(file
, var
->array
, 0);
2204 fprintf(file
, ";\n");
2209 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
2211 int in_attr
, out_attr
;
2214 const expr_t
*size_is
;
2220 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2222 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2223 size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2224 has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
);
2225 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2226 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2227 if (!out_attr
&& !in_attr
)
2232 print_file(file
, indent
, "");
2233 write_name(file
, var
);
2238 type_t
*type
= var
->type
;
2240 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
2241 write_expr(file
, size_is
, 1);
2242 size
= get_type_memsize(type
);
2243 fprintf(file
, " * %u);\n", size
);
2245 else if (!is_string
)
2247 fprintf(file
, " = &_W%u;\n", i
);
2248 if (var
->ptr_level
> 1)
2249 print_file(file
, indent
, "_W%u = 0;\n", i
);
2257 fprintf(file
, "\n");
2261 int write_expr_eval_routines(FILE *file
, const char *iface
)
2264 struct expr_eval_routine
*eval
;
2265 unsigned short callback_offset
= 0;
2267 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2271 print_file(file
, indent
, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2272 iface
, eval
->structure
->name
, callback_offset
);
2273 print_file(file
, indent
, "{\n");
2275 print_file(file
, indent
, "struct %s *" STRUCT_EXPR_EVAL_VAR
" = (struct %s *)(pStubMsg->StackTop - %u);\n",
2276 eval
->structure
->name
, eval
->structure
->name
, eval
->structure_size
);
2277 fprintf(file
, "\n");
2278 print_file(file
, indent
, "pStubMsg->Offset = 0;\n"); /* FIXME */
2279 print_file(file
, indent
, "pStubMsg->MaxCount = (unsigned long)");
2280 write_struct_expr(file
, eval
->expr
, 1, eval
->structure
->fields
, STRUCT_EXPR_EVAL_VAR
);
2281 fprintf(file
, ";\n");
2283 print_file(file
, indent
, "}\n\n");
2289 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
2291 struct expr_eval_routine
*eval
;
2292 struct expr_eval_routine
*cursor
;
2293 unsigned short callback_offset
= 0;
2295 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2296 fprintf(file
, "{\n");
2298 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2300 print_file(file
, 1, "%s_%sExprEval_%04u,\n",
2301 iface
, eval
->structure
->name
, callback_offset
);
2304 list_remove(&eval
->entry
);
2308 fprintf(file
, "};\n\n");