2 * Format String Generator for IDL Compiler
4 * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 expr_t
*array
);
63 static size_t fields_memsize(const var_t
*v
);
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
;
76 return a
->u
.lval
- b
->u
.lval
;
78 return strcmp(a
->u
.sval
, b
->u
.sval
);
80 ret
= compare_expr(a
->ref
, b
->ref
);
83 ret
= compare_expr(a
->u
.ext
, b
->u
.ext
);
86 return compare_expr(a
->ext2
, b
->ext2
);
95 ret
= compare_expr(a
->ref
, b
->ref
);
98 return compare_expr(a
->u
.ext
, b
->u
.ext
);
104 return compare_expr(a
->ref
, b
->ref
);
111 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
114 fprintf(file, "/* %2u */\n", typestring_offset); \
115 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
119 static int print_file(FILE *file
, int indent
, const char *format
, ...)
126 va_start(va
, format
);
127 for (i
= 0; i
< indent
; i
++)
129 r
= vfprintf(file
, format
, va
);
134 static inline int type_has_ref(const type_t
*type
)
136 return (type
->type
== 0 && type
->ref
);
139 static inline int is_base_type(unsigned char type
)
158 case RPC_FC_ERROR_STATUS_T
:
166 static size_t write_procformatstring_var(FILE *file
, int indent
,
167 const var_t
*var
, int is_return
, unsigned int *type_offset
)
170 int ptr_level
= var
->ptr_level
;
171 const type_t
*type
= var
->type
;
173 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
174 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
176 if (!is_in
&& !is_out
) is_in
= TRUE
;
178 if (ptr_level
== 0 && type_has_ref(type
))
181 if (ptr_level
== 0 && !var
->array
&& is_base_type(type
->type
))
184 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
186 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
190 #define CASE_BASETYPE(fctype) \
192 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
193 size = 2; /* includes param type prefix */ \
196 CASE_BASETYPE(FC_BYTE
);
197 CASE_BASETYPE(FC_CHAR
);
198 CASE_BASETYPE(FC_WCHAR
);
199 CASE_BASETYPE(FC_USHORT
);
200 CASE_BASETYPE(FC_SHORT
);
201 CASE_BASETYPE(FC_ULONG
);
202 CASE_BASETYPE(FC_LONG
);
203 CASE_BASETYPE(FC_HYPER
);
204 CASE_BASETYPE(FC_IGNORE
);
205 CASE_BASETYPE(FC_USMALL
);
206 CASE_BASETYPE(FC_SMALL
);
207 CASE_BASETYPE(FC_FLOAT
);
208 CASE_BASETYPE(FC_DOUBLE
);
209 CASE_BASETYPE(FC_ERROR_STATUS_T
);
212 error("Unknown/unsupported type: %s (0x%02x)\n", var
->name
, type
->type
);
219 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
220 else if (is_in
&& is_out
)
221 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
223 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
225 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
227 print_file(file
, indent
, "0x01,\n");
228 print_file(file
, indent
, "NdrFcShort(0x%x),\n", *type_offset
);
229 size
= 4; /* includes param type prefix */
231 *type_offset
+= get_size_typeformatstring_var(var
);
235 void write_procformatstring(FILE *file
, type_t
*iface
)
239 unsigned int type_offset
= 2;
241 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
242 print_file(file
, indent
, "{\n");
244 print_file(file
, indent
, "0,\n");
245 print_file(file
, indent
, "{\n");
250 func_t
*func
= iface
->funcs
;
251 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
252 for (; func
; func
= PREV_LINK(func
))
254 /* emit argument data */
258 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
261 write_procformatstring_var(file
, indent
, var
, FALSE
,
264 var
= PREV_LINK(var
);
268 /* emit return value data */
270 if (is_void(var
->type
, NULL
))
272 print_file(file
, indent
, "0x5b, /* FC_END */\n");
273 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
276 write_procformatstring_var(file
, indent
, var
, TRUE
,
281 print_file(file
, indent
, "0x0\n");
283 print_file(file
, indent
, "}\n");
285 print_file(file
, indent
, "};\n");
286 print_file(file
, indent
, "\n");
289 /* write conformance / variance descriptor */
290 static size_t write_conf_or_var_desc(FILE *file
, const func_t
*func
, const type_t
*structure
, const expr_t
*expr
)
292 unsigned char operator_type
= 0;
293 const char *operator_string
= "no operators";
294 const expr_t
*subexpr
= expr
;
295 unsigned char correlation_type
;
297 if (!file
) return 4; /* optimisation for sizing pass */
301 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
302 error("write_conf_or_var_desc: constant value %ld is greater than "
303 "the maximum constant size of %d\n", expr
->cval
,
304 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
306 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
307 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
308 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
309 print_file(file
, 2, "NdrShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
314 switch (subexpr
->type
)
317 subexpr
= subexpr
->ref
;
318 operator_type
= RPC_FC_DEREFERENCE
;
319 operator_string
= "FC_DEREFERENCE";
322 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
324 subexpr
= subexpr
->ref
;
325 operator_type
= RPC_FC_DIV_2
;
326 operator_string
= "FC_DIV_2";
330 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
332 subexpr
= subexpr
->ref
;
333 operator_type
= RPC_FC_MULT_2
;
334 operator_string
= "FC_MULT_2";
338 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
340 subexpr
= subexpr
->ref
;
341 operator_type
= RPC_FC_SUB_1
;
342 operator_string
= "FC_SUB_1";
346 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
348 subexpr
= subexpr
->ref
;
349 operator_type
= RPC_FC_ADD_1
;
350 operator_string
= "FC_ADD_1";
357 if (subexpr
->type
== EXPR_IDENTIFIER
)
359 const type_t
*correlation_variable
= NULL
;
360 unsigned char param_type
= 0;
361 const char *param_type_string
= NULL
;
368 for (offset
= 0, var
= structure
->fields
; var
; var
= NEXT_LINK(var
))
370 offset
-= type_memsize(var
->type
, var
->ptr_level
, var
->array
);
371 if (!strcmp(var
->name
, subexpr
->u
.sval
))
373 correlation_variable
= var
->type
;
377 if (!correlation_variable
)
378 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
381 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
385 const var_t
*var
= func
->args
;
387 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
388 /* FIXME: not all stack variables are sizeof(void *) */
389 for (offset
= 0; var
; offset
+= sizeof(void *), var
= PREV_LINK(var
))
391 if (!strcmp(var
->name
, subexpr
->u
.sval
))
393 correlation_variable
= var
->type
;
397 if (!correlation_variable
)
398 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
401 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
404 while (type_has_ref(correlation_variable
))
405 correlation_variable
= correlation_variable
->ref
;
407 switch (correlation_variable
->type
)
411 param_type
= RPC_FC_SMALL
;
412 param_type_string
= "FC_SMALL";
416 param_type
= RPC_FC_USMALL
;
417 param_type_string
= "FC_USMALL";
421 param_type
= RPC_FC_SHORT
;
422 param_type_string
= "FC_SHORT";
425 param_type
= RPC_FC_USHORT
;
426 param_type_string
= "FC_USHORT";
429 param_type
= RPC_FC_LONG
;
430 param_type_string
= "FC_LONG";
433 param_type
= RPC_FC_ULONG
;
434 param_type_string
= "FC_ULONG";
437 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
438 correlation_variable
->type
);
441 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
442 correlation_type
| param_type
,
443 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter, " : "",
445 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
446 print_file(file
, 2, "NdrShort(0x%x), /* %soffset = %d */\n",
448 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "x86 stack size / " : "",
453 unsigned int callback_offset
= 0;
457 struct expr_eval_routine
*eval
;
460 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
462 if (!strcmp(eval
->structure
->name
, structure
->name
) &&
463 !compare_expr(eval
->expr
, expr
))
473 eval
= xmalloc(sizeof(*eval
));
474 eval
->structure
= structure
;
475 eval
->structure_size
= fields_memsize(structure
->fields
);
477 list_add_tail(&expr_eval_routines
, &eval
->entry
);
480 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
484 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
485 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
488 if (callback_offset
> USHRT_MAX
)
489 error("Maximum number of callback routines reached\n");
491 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n",
493 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter" : "");
494 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
495 print_file(file
, 2, "NdrShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
500 static size_t fields_memsize(const var_t
*v
)
503 const var_t
*first
= v
;
505 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
507 size
+= type_memsize(v
->type
, v
->ptr_level
, v
->array
);
508 if (v
== first
) break;
514 static size_t type_memsize(const type_t
*t
, int ptr_level
, const expr_t
*array
)
519 return sizeof(void *);
537 case RPC_FC_ERROR_STATUS_T
:
547 case RPC_FC_CVSTRUCT
:
548 case RPC_FC_CPSTRUCT
:
551 case RPC_FC_BOGUS_STRUCT
:
552 case RPC_FC_ENCAPSULATED_UNION
:
553 case RPC_FC_NON_ENCAPSULATED_UNION
:
554 size
= fields_memsize(t
->fields
);
557 error("type_memsize: Unknown type %d", t
->type
);
572 static size_t write_string_tfs(FILE *file
, const attr_t
*attrs
,
573 const type_t
*type
, const expr_t
*array
,
574 const char *name
, size_t *typestring_offset
)
576 const expr_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
577 int has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
);
578 size_t start_offset
= *typestring_offset
;
580 if ((type
->type
!= RPC_FC_CHAR
) && (type
->type
!= RPC_FC_WCHAR
))
582 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", type
->type
, name
);
586 if (array
&& array
->is_const
)
588 if (array
->cval
> USHRT_MAX
)
589 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
590 name
, USHRT_MAX
, array
->cval
- USHRT_MAX
);
592 if (type
->type
== RPC_FC_CHAR
)
593 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
595 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
596 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
597 *typestring_offset
+= 2;
599 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", array
->cval
, array
->cval
);
600 *typestring_offset
+= 2;
606 if (type
->type
== RPC_FC_CHAR
)
607 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
609 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
610 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
611 *typestring_offset
+= 2;
613 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
, NULL
, size_is
);
619 if (type
->type
== RPC_FC_CHAR
)
620 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
622 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
623 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
624 *typestring_offset
+= 2;
630 static size_t write_array_tfs(FILE *file
, const attr_t
*attrs
,
631 const type_t
*type
, const expr_t
*array
,
632 const char *name
, size_t *typestring_offset
)
634 const expr_t
*length_is
= get_attrp(attrs
, ATTR_LENGTHIS
);
635 const expr_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
636 int has_length
= length_is
&& (length_is
->type
!= EXPR_VOID
);
637 int has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
) && !array
->is_const
;
638 size_t start_offset
= *typestring_offset
;
640 /* FIXME: need to analyse type for pointers */
642 if (NEXT_LINK(array
)) /* multi-dimensional array */
644 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name
);
649 if (!has_length
&& !has_size
)
652 size_t size
= type_memsize(type
, 0, array
);
653 if (size
< USHRT_MAX
)
655 WRITE_FCTYPE(file
, FC_SMFARRAY
, *typestring_offset
);
657 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
659 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", size
, size
);
660 *typestring_offset
+= 4;
664 WRITE_FCTYPE(file
, FC_LGFARRAY
, *typestring_offset
);
666 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
668 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", size
, size
);
669 *typestring_offset
+= 6;
672 /* FIXME: write out pointer descriptor if necessary */
674 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
675 print_file(file
, 2, "FC_END,\n");
676 *typestring_offset
+= 2;
680 else if (has_length
&& !has_size
)
683 size_t element_size
= type_memsize(type
, 0, NULL
);
684 size_t elements
= array
->cval
;
685 size_t total_size
= element_size
* elements
;
687 if (total_size
< USHRT_MAX
)
689 WRITE_FCTYPE(file
, FC_SMVARRAY
, *typestring_offset
);
691 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
693 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", total_size
, total_size
);
694 /* number of elements */
695 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", elements
, elements
);
696 *typestring_offset
+= 6;
700 WRITE_FCTYPE(file
, FC_LGVARRAY
, *typestring_offset
);
702 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
704 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", total_size
, total_size
);
705 /* number of elements */
706 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", elements
, elements
);
707 *typestring_offset
+= 10;
710 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
711 *typestring_offset
+= 2;
713 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
717 /* FIXME: write out pointer descriptor if necessary */
719 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
720 print_file(file
, 2, "FC_END,\n");
721 *typestring_offset
+= 2;
725 else if (!has_length
&& has_size
)
727 /* conformant array */
728 size_t element_size
= type_memsize(type
, 0, NULL
);
730 WRITE_FCTYPE(file
, FC_CARRAY
, *typestring_offset
);
732 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
734 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
735 *typestring_offset
+= 4;
737 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
738 current_structure
, size_is
);
740 /* FIXME: write out pointer descriptor if necessary */
742 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
743 print_file(file
, 2, "FC_END,\n");
744 *typestring_offset
+= 2;
750 /* conformant varying array */
751 size_t element_size
= type_memsize(type
, 0, NULL
);
753 WRITE_FCTYPE(file
, FC_CVARRAY
, *typestring_offset
);
755 print_file(file
, 2, "0x%x, /* 0 */\n", 0);
757 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
758 *typestring_offset
+= 4;
760 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
761 current_structure
, size_is
);
762 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
766 /* FIXME: write out pointer descriptor if necessary */
768 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
769 print_file(file
, 2, "FC_END,\n");
770 *typestring_offset
+= 2;
777 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
779 /* last field is the first in the fields linked list */
780 const var_t
*last_field
= type
->fields
;
781 if (is_array_type(last_field
->attrs
, last_field
->ptr_level
, last_field
->array
))
784 assert((last_field
->type
->type
== RPC_FC_CSTRUCT
) ||
785 (last_field
->type
->type
== RPC_FC_CPSTRUCT
) ||
786 (last_field
->type
->type
== RPC_FC_CVSTRUCT
));
788 return find_array_or_string_in_struct(last_field
->type
);
791 static size_t write_struct_tfs(FILE *file
, const type_t
*type
,
792 const char *name
, size_t *typestring_offset
)
801 total_size
= type_memsize(type
, 0, NULL
);
803 if (total_size
> USHRT_MAX
)
804 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
805 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
807 start_offset
= *typestring_offset
;
808 WRITE_FCTYPE(file
, FC_STRUCT
, *typestring_offset
);
810 print_file(file
, 2, "0x0,\n");
812 print_file(file
, 2, "NdrShort(0x%x), /* %u */\n", total_size
, total_size
);
813 *typestring_offset
+= 4;
816 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
817 print_file(file
, 2, "FC_END,\n");
819 *typestring_offset
+= 2;
822 total_size
= type_memsize(type
, 0, NULL
);
824 if (total_size
> USHRT_MAX
)
825 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
826 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
828 start_offset
= *typestring_offset
;
829 WRITE_FCTYPE(file
, FC_CSTRUCT
, *typestring_offset
);
831 print_file(file
, 2, "0x0,\n");
833 print_file(file
, 2, "NdrShort(0x%x), /* %u */\n", total_size
, total_size
);
834 *typestring_offset
+= 4;
835 /* FIXME: a fixed offset won't work when pointer layout is present */
836 print_file(file
, 2, "NdrShort(0x3), /* 3 */\n");
837 *typestring_offset
+= 2;
838 print_file(file
, 2, "FC_END,\n");
839 *typestring_offset
+= 1;
841 array
= find_array_or_string_in_struct(type
);
842 current_structure
= type
;
843 write_array_tfs(file
, array
->attrs
, array
->type
,
844 array
->array
, array
->name
,
846 current_structure
= NULL
;
849 case RPC_FC_CVSTRUCT
:
850 total_size
= type_memsize(type
, 0, NULL
);
852 if (total_size
> USHRT_MAX
)
853 error("structure size for parameter %s exceeds %d bytes by %d bytes\n",
854 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
856 start_offset
= *typestring_offset
;
857 WRITE_FCTYPE(file
, FC_CVSTRUCT
, *typestring_offset
);
859 print_file(file
, 2, "0x0,\n");
861 print_file(file
, 2, "NdrShort(0x%x), /* %u */\n", total_size
, total_size
);
862 *typestring_offset
+= 4;
863 /* FIXME: a fixed offset won't work when pointer layout is present */
864 print_file(file
, 2, "NdrShort(0x3), /* 3 */\n");
865 *typestring_offset
+= 2;
866 print_file(file
, 2, "FC_END,\n");
867 *typestring_offset
+= 1;
869 array
= find_array_or_string_in_struct(type
);
870 current_structure
= type
;
871 if (is_attr(array
->attrs
, ATTR_STRING
))
872 write_string_tfs(file
, array
->attrs
, array
->type
,
873 array
->array
, array
->name
,
876 write_array_tfs(file
, array
->attrs
, array
->type
,
877 array
->array
, array
->name
,
879 current_structure
= NULL
;
883 error("write_struct_tfs: Unimplemented for type 0x%x\n", type
->type
);
884 return *typestring_offset
;
888 static size_t write_union_tfs(FILE *file
, const attr_t
*attrs
,
889 const type_t
*type
, const char *name
,
890 size_t *typeformat_offset
)
892 error("write_union_tfs: Unimplemented\n");
893 return *typeformat_offset
;
896 static size_t write_typeformatstring_var(FILE *file
, int indent
,
897 const var_t
*var
, size_t *typeformat_offset
)
899 const type_t
*type
= var
->type
;
900 int ptr_level
= var
->ptr_level
;
906 if (is_string_type(var
->attrs
, ptr_level
, var
->array
))
907 return write_string_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
909 if (is_array_type(var
->attrs
, ptr_level
, var
->array
))
910 return write_array_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
914 /* follow reference if the type has one */
915 if (type_has_ref(type
))
918 /* FIXME: get new ptr_level from type */
922 /* basic types don't need a type format string */
923 if (is_base_type(type
->type
))
931 case RPC_FC_CPSTRUCT
:
932 case RPC_FC_CVSTRUCT
:
933 case RPC_FC_BOGUS_STRUCT
:
934 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
935 case RPC_FC_ENCAPSULATED_UNION
:
936 case RPC_FC_NON_ENCAPSULATED_UNION
:
937 return write_union_tfs(file
, var
->attrs
, type
, var
->name
, typeformat_offset
);
939 case RPC_FC_BIND_PRIMITIVE
:
943 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
946 else if (ptr_level
== 1 && !type_has_ref(type
))
948 size_t start_offset
= *typeformat_offset
;
949 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
950 if (!pointer_type
) pointer_type
= RPC_FC_RP
;
952 /* special case for pointers to base types */
955 #define CASE_BASETYPE(fctype) \
957 print_file(file, indent, "0x%x, 0x08, /* %s [simple_pointer] */\n", \
959 pointer_type == RPC_FC_FP ? "FC_FP" : (pointer_type == RPC_FC_UP ? "FC_UP" : "FC_RP")); \
960 print_file(file, indent, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
961 print_file(file, indent, "0x5c, /* FC_PAD */\n"); \
962 *typeformat_offset += 4; \
964 CASE_BASETYPE(FC_BYTE
);
965 CASE_BASETYPE(FC_CHAR
);
966 CASE_BASETYPE(FC_SMALL
);
967 CASE_BASETYPE(FC_USMALL
);
968 CASE_BASETYPE(FC_WCHAR
);
969 CASE_BASETYPE(FC_SHORT
);
970 CASE_BASETYPE(FC_USHORT
);
971 CASE_BASETYPE(FC_LONG
);
972 CASE_BASETYPE(FC_ULONG
);
973 CASE_BASETYPE(FC_FLOAT
);
974 CASE_BASETYPE(FC_HYPER
);
975 CASE_BASETYPE(FC_DOUBLE
);
976 CASE_BASETYPE(FC_ENUM16
);
977 CASE_BASETYPE(FC_ENUM32
);
978 CASE_BASETYPE(FC_IGNORE
);
979 CASE_BASETYPE(FC_ERROR_STATUS_T
);
985 assert(ptr_level
> 0);
987 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
988 if (!pointer_type
) pointer_type
= RPC_FC_RP
;
990 print_file(file
, indent
, "0x%x, 0x00, /* %s */\n",
992 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"));
993 print_file(file
, indent
, "NdrShort(0x2), /* 2 */\n");
994 *typeformat_offset
+= 4;
1001 void write_typeformatstring(FILE *file
, type_t
*iface
)
1005 size_t typeformat_offset
;
1007 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1008 print_file(file
, indent
, "{\n");
1010 print_file(file
, indent
, "0,\n");
1011 print_file(file
, indent
, "{\n");
1013 print_file(file
, indent
, "NdrFcShort(0x0),\n");
1014 typeformat_offset
= 2;
1018 func_t
*func
= iface
->funcs
;
1019 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
1020 for (; func
; func
= PREV_LINK(func
))
1022 current_func
= func
;
1026 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
1029 write_typeformatstring_var(file
, indent
, var
,
1030 &typeformat_offset
);
1031 var
= PREV_LINK(var
);
1037 print_file(file
, indent
, "0x0\n");
1039 print_file(file
, indent
, "}\n");
1041 print_file(file
, indent
, "};\n");
1042 print_file(file
, indent
, "\n");
1045 static unsigned int get_required_buffer_size_type(
1046 const type_t
*type
, int ptr_level
, const expr_t
*array
,
1047 const char *name
, unsigned int *alignment
)
1050 if (ptr_level
== 0 && !array
&& !type_has_ref(type
))
1069 case RPC_FC_ERROR_STATUS_T
:
1079 case RPC_FC_BIND_PRIMITIVE
:
1086 for (field
= type
->fields
; field
; field
= NEXT_LINK(field
))
1088 unsigned int alignment
;
1089 size
+= get_required_buffer_size_type(
1090 field
->type
, field
->ptr_level
, field
->array
, field
->name
,
1097 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
1101 if (ptr_level
== 0 && type_has_ref(type
))
1102 return get_required_buffer_size_type(type
->ref
, 0 /* FIXME */, array
, name
, alignment
);
1106 unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
)
1108 return get_required_buffer_size_type(var
->type
, var
->ptr_level
, var
->array
, var
->name
, alignment
);
1111 void marshall_arguments(FILE *file
, int indent
, func_t
*func
,
1112 unsigned int *type_offset
, enum pass pass
)
1114 unsigned int last_size
= 0;
1121 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
1122 for (; var
; *type_offset
+= get_size_typeformatstring_var(var
), var
= PREV_LINK(var
))
1124 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1125 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1127 if (!in_attr
&& !out_attr
)
1144 if (is_string_type(var
->attrs
, var
->ptr_level
, var
->array
))
1146 if (var
->array
&& var
->array
->is_const
)
1147 print_file(file
, indent
,
1148 "NdrNonConformantStringMarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1149 var
->name
, *type_offset
);
1151 print_file(file
, indent
,
1152 "NdrConformantStringMarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1153 var
->name
, *type_offset
);
1156 else if (is_array_type(var
->attrs
, var
->ptr_level
, var
->array
))
1158 const expr_t
*length_is
= get_attrp(var
->attrs
, ATTR_LENGTHIS
);
1159 const expr_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
1160 const char *array_type
;
1161 int has_length
= length_is
&& (length_is
->type
!= EXPR_VOID
);
1162 int has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
) && !var
->array
->is_const
;
1164 if (NEXT_LINK(var
->array
)) /* multi-dimensional array */
1165 array_type
= "ComplexArray";
1168 if (!has_length
&& !has_size
)
1169 array_type
= "FixedArray";
1170 else if (has_length
&& !has_size
)
1172 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1173 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
1174 write_expr(file
, length_is
, 1);
1175 fprintf(file
, ";\n\n");
1176 array_type
= "VaryingArray";
1178 else if (!has_length
&& has_size
)
1180 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
1181 write_expr(file
, size_is
? size_is
: var
->array
, 1);
1182 fprintf(file
, ";\n\n");
1183 array_type
= "ConformantArray";
1187 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
1188 write_expr(file
, size_is
? size_is
: var
->array
, 1);
1189 fprintf(file
, ";\n");
1190 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
1191 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
1192 write_expr(file
, length_is
, 1);
1193 fprintf(file
, ";\n\n");
1194 array_type
= "ConformantVaryingArray";
1198 print_file(file
, indent
,
1199 "Ndr%sMarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1200 array_type
, var
->name
, *type_offset
);
1203 else if (var
->ptr_level
== 0 && is_base_type(var
->type
->type
))
1206 unsigned int alignment
= 0;
1207 switch (var
->type
->type
)
1221 if (last_size
!= 0 && last_size
< 2)
1222 alignment
= (2 - last_size
);
1228 case RPC_FC_ERROR_STATUS_T
:
1230 if (last_size
!= 0 && last_size
< 4)
1231 alignment
= (4 - last_size
);
1237 if (last_size
!= 0 && last_size
< 4)
1238 alignment
= (4 - last_size
);
1242 error("marshall_arguments: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, var
->type
->type
);
1247 print_file(file
, indent
, "_StubMsg.Buffer += %u;\n", alignment
);
1249 print_file(file
, indent
, "*(");
1250 write_type(file
, var
->type
, var
, var
->tname
);
1251 fprintf(file
, " *)_StubMsg.Buffer = ");
1252 write_name(file
, var
);
1253 fprintf(file
, ";\n");
1254 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
1255 write_type(file
, var
->type
, var
, var
->tname
);
1256 fprintf(file
, ");\n");
1260 else if (var
->ptr_level
== 0)
1262 const char *ndrtype
;
1264 switch (var
->type
->type
)
1267 ndrtype
= "SimpleStruct";
1269 case RPC_FC_CSTRUCT
:
1270 case RPC_FC_CPSTRUCT
:
1271 ndrtype
= "ConformantStruct";
1273 case RPC_FC_CVSTRUCT
:
1274 ndrtype
= "ConformantVaryingStruct";
1276 case RPC_FC_BOGUS_STRUCT
:
1277 ndrtype
= "ComplexStruct";
1280 case RPC_FC_BIND_PRIMITIVE
:
1281 /* no marshalling needed */
1284 error("marshall_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1285 var
->name
, var
->type
->type
, var
->ptr_level
);
1289 print_file(file
, indent
,
1290 "Ndr%sMarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1291 ndrtype
, var
->name
, *type_offset
);
1296 print_file(file
, indent
,
1297 "NdrPointerMarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d]);\n",
1298 var
->name
, *type_offset
);
1301 fprintf(file
, "\n");
1305 void unmarshall_arguments(FILE *file
, int indent
, func_t
*func
,
1306 unsigned int *type_offset
, enum pass pass
)
1308 unsigned int last_size
= 0;
1315 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
1316 for (; var
; *type_offset
+= get_size_typeformatstring_var(var
), var
= PREV_LINK(var
))
1318 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1319 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1321 if (!in_attr
&& !out_attr
)
1338 if (is_string_type(var
->attrs
, var
->ptr_level
, var
->array
))
1340 if (var
->array
&& var
->array
->is_const
)
1341 print_file(file
, indent
,
1342 "NdrNonConformantStringUnmarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d], 0);\n",
1343 var
->name
, *type_offset
);
1345 print_file(file
, indent
,
1346 "NdrConformantStringUnmarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d], 0);\n",
1347 var
->name
, *type_offset
);
1350 else if (is_array_type(var
->attrs
, var
->ptr_level
, var
->array
))
1352 const expr_t
*length_is
= get_attrp(var
->attrs
, ATTR_LENGTHIS
);
1353 const expr_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
1354 const char *array_type
;
1355 int has_length
= length_is
&& (length_is
->type
!= EXPR_VOID
);
1356 int has_size
= size_is
&& (size_is
->type
!= EXPR_VOID
) && !var
->array
->is_const
;
1358 if (NEXT_LINK(var
->array
)) /* multi-dimensional array */
1359 array_type
= "ComplexArray";
1362 if (!has_length
&& !has_size
)
1363 array_type
= "FixedArray";
1364 else if (has_length
&& !has_size
)
1365 array_type
= "VaryingArray";
1366 else if (!has_length
&& has_size
)
1367 array_type
= "ConformantArray";
1369 array_type
= "ConformantVaryingArray";
1372 print_file(file
, indent
,
1373 "Ndr%sUnmarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d], 0);\n",
1374 array_type
, var
->name
, *type_offset
);
1377 else if (var
->ptr_level
== 0 && is_base_type(var
->type
->type
))
1380 unsigned int alignment
= 0;
1382 switch (var
->type
->type
)
1396 if (last_size
!= 0 && last_size
< 2)
1397 alignment
= (2 - last_size
);
1403 case RPC_FC_ERROR_STATUS_T
:
1405 if (last_size
!= 0 && last_size
< 4)
1406 alignment
= (4 - last_size
);
1412 if (last_size
!= 0 && last_size
< 4)
1413 alignment
= (4 - last_size
);
1417 error("unmarshall_arguments: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, var
->type
->type
);
1422 print_file(file
, indent
, "_StubMsg.Buffer += %u;\n", alignment
);
1424 print_file(file
, indent
, "");
1425 write_name(file
, var
);
1426 fprintf(file
, " = *(");
1427 write_type(file
, var
->type
, var
, var
->tname
);
1428 fprintf(file
, " *)_StubMsg.Buffer;\n");
1429 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
1430 write_type(file
, var
->type
, var
, var
->tname
);
1431 fprintf(file
, ");\n");
1435 else if (var
->ptr_level
== 0)
1437 const char *ndrtype
;
1439 switch (var
->type
->type
)
1442 ndrtype
= "SimpleStruct";
1444 case RPC_FC_CSTRUCT
:
1445 case RPC_FC_CPSTRUCT
:
1446 ndrtype
= "ConformantStruct";
1448 case RPC_FC_CVSTRUCT
:
1449 ndrtype
= "ConformantVaryingStruct";
1451 case RPC_FC_BOGUS_STRUCT
:
1452 ndrtype
= "ComplexStruct";
1455 case RPC_FC_BIND_PRIMITIVE
:
1456 /* no unmarshalling needed */
1459 error("unmarshall_arguments: Unsupported type: %s (0x%02x, ptr_level: %d)\n",
1460 var
->name
, var
->type
->type
, var
->ptr_level
);
1464 print_file(file
, indent
,
1465 "Ndr%sUnmarshall(&_StubMsg, (unsigned char *)%s, &__MIDL_TypeFormatString.Format[%d], 0);\n",
1466 ndrtype
, var
->name
, *type_offset
);
1471 print_file(file
, indent
,
1472 "NdrPointerUnmarshall(&_StubMsg, (unsigned char **)&%s, &__MIDL_TypeFormatString.Format[%d], 0);\n",
1473 var
->name
, *type_offset
);
1476 fprintf(file
, "\n");
1481 size_t get_size_procformatstring_var(const var_t
*var
)
1483 unsigned int type_offset
= 2;
1484 return write_procformatstring_var(NULL
, 0, var
, FALSE
, &type_offset
);
1488 size_t get_size_typeformatstring_var(const var_t
*var
)
1490 unsigned int type_offset
= 0;
1491 write_typeformatstring_var(NULL
, 0, var
, &type_offset
);
1495 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
1496 const var_t
*fields
, const char *structvar
)
1502 fprintf(h
, "%ld", e
->u
.lval
);
1505 fprintf(h
, "0x%lx", e
->u
.lval
);
1507 case EXPR_IDENTIFIER
:
1510 for (field
= fields
; field
; field
= NEXT_LINK(field
))
1512 if (!strcmp(e
->u
.sval
, field
->name
))
1514 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
1518 if (!field
) error("no field found for identifier %s\n", e
->u
.sval
);
1523 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
1527 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
1531 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
1535 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
1537 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
1540 fprintf(h
, "sizeof(");
1541 write_type(h
, e
->u
.tref
->ref
, NULL
, e
->u
.tref
->name
);
1552 if (brackets
) fprintf(h
, "(");
1553 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
1555 case EXPR_SHL
: fprintf(h
, " << "); break;
1556 case EXPR_SHR
: fprintf(h
, " >> "); break;
1557 case EXPR_MUL
: fprintf(h
, " * "); break;
1558 case EXPR_DIV
: fprintf(h
, " / "); break;
1559 case EXPR_ADD
: fprintf(h
, " + "); break;
1560 case EXPR_SUB
: fprintf(h
, " - "); break;
1561 case EXPR_AND
: fprintf(h
, " & "); break;
1562 case EXPR_OR
: fprintf(h
, " | "); break;
1565 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
1566 if (brackets
) fprintf(h
, ")");
1569 if (brackets
) fprintf(h
, "(");
1570 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
1572 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
1574 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
1575 if (brackets
) fprintf(h
, ")");
1580 int write_expr_eval_routines(FILE *file
, const char *iface
)
1583 struct expr_eval_routine
*eval
;
1584 unsigned short callback_offset
= 0;
1586 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
1590 print_file(file
, indent
, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
1591 iface
, eval
->structure
->name
, callback_offset
);
1592 print_file(file
, indent
, "{\n");
1594 print_file(file
, indent
, "struct %s *" STRUCT_EXPR_EVAL_VAR
" = (struct %s *)(pStubMsg->StackTop - %u);\n",
1595 eval
->structure
->name
, eval
->structure
->name
, eval
->structure_size
);
1596 fprintf(file
, "\n");
1597 print_file(file
, indent
, "pStubMsg->Offset = 0;\n"); /* FIXME */
1598 print_file(file
, indent
, "pStubMsg->MaxCount = (unsigned long)");
1599 write_struct_expr(file
, eval
->expr
, 1, eval
->structure
->fields
, STRUCT_EXPR_EVAL_VAR
);
1600 fprintf(file
, ";\n");
1602 print_file(file
, indent
, "}\n\n");
1608 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
1610 struct expr_eval_routine
*eval
;
1611 struct expr_eval_routine
*cursor
;
1612 unsigned short callback_offset
= 0;
1614 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
1615 fprintf(file
, "{\n");
1617 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
1619 print_file(file
, 1, "%s_%sExprEval_%04u,\n",
1620 iface
, eval
->structure
->name
, callback_offset
);
1623 list_remove(&eval
->entry
);
1627 fprintf(file
, "};\n\n");