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 void write_embedded_types(FILE *file
, const type_t
*type
, size_t *tfsoff
);
67 const char *string_of_type(unsigned char type
)
71 case RPC_FC_BYTE
: return "FC_BYTE";
72 case RPC_FC_CHAR
: return "FC_CHAR";
73 case RPC_FC_SMALL
: return "FC_SMALL";
74 case RPC_FC_USMALL
: return "FC_USMALL";
75 case RPC_FC_WCHAR
: return "FC_WCHAR";
76 case RPC_FC_SHORT
: return "FC_SHORT";
77 case RPC_FC_USHORT
: return "FC_USHORT";
78 case RPC_FC_LONG
: return "FC_LONG";
79 case RPC_FC_ULONG
: return "FC_ULONG";
80 case RPC_FC_FLOAT
: return "FC_FLOAT";
81 case RPC_FC_HYPER
: return "FC_HYPER";
82 case RPC_FC_DOUBLE
: return "FC_DOUBLE";
83 case RPC_FC_ENUM16
: return "FC_ENUM16";
84 case RPC_FC_ENUM32
: return "FC_ENUM32";
85 case RPC_FC_IGNORE
: return "FC_IGNORE";
86 case RPC_FC_ERROR_STATUS_T
: return "FC_ERROR_STATUS_T";
87 case RPC_FC_RP
: return "FC_RP";
88 case RPC_FC_UP
: return "FC_UP";
89 case RPC_FC_OP
: return "FC_OP";
90 case RPC_FC_FP
: return "FC_FP";
91 case RPC_FC_ENCAPSULATED_UNION
: return "FC_ENCAPSULATED_UNION";
92 case RPC_FC_NON_ENCAPSULATED_UNION
: return "FC_NON_ENCAPSULATED_UNION";
93 case RPC_FC_STRUCT
: return "FC_STRUCT";
94 case RPC_FC_PSTRUCT
: return "FC_PSTRUCT";
95 case RPC_FC_CSTRUCT
: return "FC_CSTRUCT";
96 case RPC_FC_CPSTRUCT
: return "FC_CPSTRUCT";
97 case RPC_FC_CVSTRUCT
: return "FC_CVSTRUCT";
98 case RPC_FC_BOGUS_STRUCT
: return "FC_BOGUS_STRUCT";
100 error("string_of_type: unknown type 0x%02x\n", type
);
105 static int is_struct(unsigned char type
)
112 case RPC_FC_CPSTRUCT
:
113 case RPC_FC_CVSTRUCT
:
114 case RPC_FC_BOGUS_STRUCT
:
121 static int is_union(unsigned char type
)
125 case RPC_FC_ENCAPSULATED_UNION
:
126 case RPC_FC_NON_ENCAPSULATED_UNION
:
133 static int is_embedded_complex(const type_t
*type
)
135 return is_struct(type
->type
) || is_union(type
->type
);
138 static long field_offset(const type_t
*strct
, const char *name
, var_t
**pfield
)
141 var_list_t
*fields
= strct
->fields
;
144 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
146 unsigned int align
= 0;
148 if (f
->name
!= NULL
&& strcmp(name
, f
->name
) == 0)
150 if (pfield
) *pfield
= f
;
155 /* FIXME: handle possible padding */
156 offset
+= type_memsize(f
->type
, f
->array
, &align
);
160 if (pfield
) *pfield
= NULL
;
164 static int compare_expr(const expr_t
*a
, const expr_t
*b
)
168 if (a
->type
!= b
->type
)
169 return a
->type
- b
->type
;
176 return a
->u
.lval
- b
->u
.lval
;
177 case EXPR_IDENTIFIER
:
178 return strcmp(a
->u
.sval
, b
->u
.sval
);
180 ret
= compare_expr(a
->ref
, b
->ref
);
183 ret
= compare_expr(a
->u
.ext
, b
->u
.ext
);
186 return compare_expr(a
->ext2
, b
->ext2
);
195 ret
= compare_expr(a
->ref
, b
->ref
);
198 return compare_expr(a
->u
.ext
, b
->u
.ext
);
204 return compare_expr(a
->ref
, b
->ref
);
211 #define WRITE_FCTYPE(file, fctype, typestring_offset) \
214 fprintf(file, "/* %2u */\n", typestring_offset); \
215 print_file((file), 2, "0x%02x, /* " #fctype " */\n", RPC_##fctype); \
219 static int print_file(FILE *file
, int indent
, const char *format
, ...)
226 va_start(va
, format
);
227 for (i
= 0; i
< indent
; i
++)
229 r
= vfprintf(file
, format
, va
);
234 static void write_formatdesc(FILE *f
, int indent
, const char *str
)
236 print_file(f
, indent
, "typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
237 print_file(f
, indent
, "{\n");
238 print_file(f
, indent
+ 1, "short Pad;\n");
239 print_file(f
, indent
+ 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
240 print_file(f
, indent
, "} MIDL_%s_FORMAT_STRING;\n", str
);
241 print_file(f
, indent
, "\n");
244 void write_formatstringsdecl(FILE *f
, int indent
, ifref_list_t
*ifaces
, int for_objects
)
246 print_file(f
, indent
, "#define TYPE_FORMAT_STRING_SIZE %d\n",
247 get_size_typeformatstring(ifaces
, for_objects
));
249 print_file(f
, indent
, "#define PROC_FORMAT_STRING_SIZE %d\n",
250 get_size_procformatstring(ifaces
, for_objects
));
253 write_formatdesc(f
, indent
, "TYPE");
254 write_formatdesc(f
, indent
, "PROC");
256 print_file(f
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
257 print_file(f
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
258 print_file(f
, indent
, "\n");
261 static int is_user_derived(const var_t
*v
)
263 const type_t
*type
= v
->type
;
265 if (v
->attrs
&& is_attr( v
->attrs
, ATTR_WIREMARSHAL
)) return 1;
269 if (type
->attrs
&& is_attr( type
->attrs
, ATTR_WIREMARSHAL
)) return 1;
275 static inline int is_base_type(unsigned char type
)
294 case RPC_FC_ERROR_STATUS_T
:
295 case RPC_FC_BIND_PRIMITIVE
:
303 static size_t write_procformatstring_var(FILE *file
, int indent
,
304 const var_t
*var
, int is_return
)
307 const type_t
*type
= var
->type
;
309 int is_in
= is_attr(var
->attrs
, ATTR_IN
);
310 int is_out
= is_attr(var
->attrs
, ATTR_OUT
);
312 if (!is_in
&& !is_out
) is_in
= TRUE
;
314 if (!var
->array
&& is_base_type(type
->type
))
317 print_file(file
, indent
, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
319 print_file(file
, indent
, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
321 if (is_base_type(type
->type
))
323 print_file(file
, indent
, "0x%02x, /* %s */\n", type
->type
, string_of_type(type
->type
));
324 size
= 2; /* includes param type prefix */
326 else if (type
->type
== RPC_FC_BIND_PRIMITIVE
)
328 print_file(file
, indent
, "0x%02x, /* FC_IGNORE */\n", RPC_FC_IGNORE
);
329 size
= 2; /* includes param type prefix */
333 error("Unknown/unsupported type: %s (0x%02x)\n", var
->name
, type
->type
);
340 print_file(file
, indent
, "0x52, /* FC_RETURN_PARAM */\n");
341 else if (is_in
&& is_out
)
342 print_file(file
, indent
, "0x50, /* FC_IN_OUT_PARAM */\n");
344 print_file(file
, indent
, "0x51, /* FC_OUT_PARAM */\n");
346 print_file(file
, indent
, "0x4d, /* FC_IN_PARAM */\n");
348 print_file(file
, indent
, "0x01,\n");
349 print_file(file
, indent
, "NdrFcShort(0x%x),\n", type
->typestring_offset
);
350 size
= 4; /* includes param type prefix */
355 void write_procformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
357 const ifref_t
*iface
;
361 print_file(file
, indent
, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
362 print_file(file
, indent
, "{\n");
364 print_file(file
, indent
, "0,\n");
365 print_file(file
, indent
, "{\n");
368 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
370 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
373 if (iface
->iface
->funcs
)
376 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
378 if (is_local(func
->def
->attrs
)) continue;
379 /* emit argument data */
382 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
383 write_procformatstring_var(file
, indent
, var
, FALSE
);
386 /* emit return value data */
388 if (is_void(var
->type
))
390 print_file(file
, indent
, "0x5b, /* FC_END */\n");
391 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
394 write_procformatstring_var(file
, indent
, var
, TRUE
);
399 print_file(file
, indent
, "0x0\n");
401 print_file(file
, indent
, "}\n");
403 print_file(file
, indent
, "};\n");
404 print_file(file
, indent
, "\n");
407 static int write_base_type(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
409 if (is_base_type(type
->type
))
411 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
412 *typestring_offset
+= 1;
419 /* write conformance / variance descriptor */
420 static size_t write_conf_or_var_desc(FILE *file
, const func_t
*func
, const type_t
*structure
, const expr_list_t
*expr_list
)
422 unsigned char operator_type
= 0;
423 const char *operator_string
= "no operators";
424 const expr_t
*expr
, *subexpr
;
425 unsigned char correlation_type
;
427 if (!file
) return 4; /* optimisation for sizing pass */
429 if (list_count(expr_list
) > 1)
430 error("write_conf_or_var_desc: multi-dimensional arrays not supported yet\n");
432 expr
= subexpr
= LIST_ENTRY( list_head(expr_list
), const expr_t
, entry
);
436 if (expr
->cval
> UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
)
437 error("write_conf_or_var_desc: constant value %ld is greater than "
438 "the maximum constant size of %d\n", expr
->cval
,
439 UCHAR_MAX
* (USHRT_MAX
+ 1) + USHRT_MAX
);
441 print_file(file
, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
442 RPC_FC_CONSTANT_CONFORMANCE
, expr
->cval
);
443 print_file(file
, 2, "0x%x,\n", expr
->cval
& ~USHRT_MAX
);
444 print_file(file
, 2, "NdrFcShort(0x%x),\n", expr
->cval
& USHRT_MAX
);
449 switch (subexpr
->type
)
452 subexpr
= subexpr
->ref
;
453 operator_type
= RPC_FC_DEREFERENCE
;
454 operator_string
= "FC_DEREFERENCE";
457 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
459 subexpr
= subexpr
->ref
;
460 operator_type
= RPC_FC_DIV_2
;
461 operator_string
= "FC_DIV_2";
465 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 2))
467 subexpr
= subexpr
->ref
;
468 operator_type
= RPC_FC_MULT_2
;
469 operator_string
= "FC_MULT_2";
473 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
475 subexpr
= subexpr
->ref
;
476 operator_type
= RPC_FC_SUB_1
;
477 operator_string
= "FC_SUB_1";
481 if (subexpr
->u
.ext
->is_const
&& (subexpr
->u
.ext
->cval
== 1))
483 subexpr
= subexpr
->ref
;
484 operator_type
= RPC_FC_ADD_1
;
485 operator_string
= "FC_ADD_1";
492 if (subexpr
->type
== EXPR_IDENTIFIER
)
494 const type_t
*correlation_variable
= NULL
;
495 unsigned char correlation_variable_type
;
496 unsigned char param_type
= 0;
497 const char *param_type_string
= NULL
;
505 if (structure
->fields
) LIST_FOR_EACH_ENTRY( var
, structure
->fields
, const var_t
, entry
)
507 unsigned int align
= 0;
508 offset
-= type_memsize(var
->type
, var
->array
, &align
);
509 /* FIXME: take alignment into account */
510 if (!strcmp(var
->name
, subexpr
->u
.sval
))
512 correlation_variable
= var
->type
;
516 if (!correlation_variable
)
517 error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
520 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
526 offset
= sizeof(void *);
527 if (func
->args
) LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
529 if (!strcmp(var
->name
, subexpr
->u
.sval
))
531 correlation_variable
= var
->type
;
534 /* FIXME: not all stack variables are sizeof(void *) */
535 offset
+= sizeof(void *);
537 if (!correlation_variable
)
538 error("write_conf_or_var_desc: couldn't find variable %s in function\n",
541 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
544 correlation_variable_type
= correlation_variable
->type
;
546 switch (correlation_variable_type
)
550 param_type
= RPC_FC_SMALL
;
551 param_type_string
= "FC_SMALL";
555 param_type
= RPC_FC_USMALL
;
556 param_type_string
= "FC_USMALL";
560 param_type
= RPC_FC_SHORT
;
561 param_type_string
= "FC_SHORT";
564 param_type
= RPC_FC_USHORT
;
565 param_type_string
= "FC_USHORT";
568 param_type
= RPC_FC_LONG
;
569 param_type_string
= "FC_LONG";
572 param_type
= RPC_FC_ULONG
;
573 param_type_string
= "FC_ULONG";
579 if (sizeof(void *) == 4) /* FIXME */
581 param_type
= RPC_FC_LONG
;
582 param_type_string
= "FC_LONG";
586 param_type
= RPC_FC_HYPER
;
587 param_type_string
= "FC_HYPER";
591 error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
592 correlation_variable_type
);
595 print_file(file
, 2, "0x%x, /* Corr desc: %s%s */\n",
596 correlation_type
| param_type
,
597 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter, " : "",
599 print_file(file
, 2, "0x%x, /* %s */\n", operator_type
, operator_string
);
600 print_file(file
, 2, "NdrFcShort(0x%x), /* %soffset = %d */\n",
602 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "x86 stack size / " : "",
607 unsigned int callback_offset
= 0;
611 struct expr_eval_routine
*eval
;
614 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
616 if (!strcmp(eval
->structure
->name
, structure
->name
) &&
617 !compare_expr(eval
->expr
, expr
))
627 unsigned int align
= 0;
628 eval
= xmalloc(sizeof(*eval
));
629 eval
->structure
= structure
;
630 eval
->structure_size
= fields_memsize(structure
->fields
, &align
);
632 list_add_tail(&expr_eval_routines
, &eval
->entry
);
635 correlation_type
= RPC_FC_NORMAL_CONFORMANCE
;
639 error("write_conf_or_var_desc: top-level callback conformance unimplemented\n");
640 correlation_type
= RPC_FC_TOP_LEVEL_CONFORMANCE
;
643 if (callback_offset
> USHRT_MAX
)
644 error("Maximum number of callback routines reached\n");
646 print_file(file
, 2, "0x%x, /* Corr desc: %s */\n",
648 correlation_type
== RPC_FC_TOP_LEVEL_CONFORMANCE
? "parameter" : "");
649 print_file(file
, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK
, "FC_CALLBACK");
650 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset
, callback_offset
);
655 static size_t fields_memsize(const var_list_t
*fields
, unsigned int *align
)
660 if (!fields
) return 0;
661 LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
662 size
+= type_memsize(v
->type
, v
->array
, align
);
667 static size_t union_memsize(const var_list_t
*fields
, unsigned int *pmaxa
)
669 size_t size
, maxs
= 0;
670 unsigned int align
= *pmaxa
;
673 if (fields
) LIST_FOR_EACH_ENTRY( v
, fields
, const var_t
, entry
)
675 /* we could have an empty default field with NULL type */
678 size
= type_memsize(v
->type
, v
->array
, &align
);
679 if (maxs
< size
) maxs
= size
;
680 if (*pmaxa
< align
) *pmaxa
= align
;
687 static size_t get_array_size( const array_dims_t
*array
)
692 if (!array
) return 0;
694 LIST_FOR_EACH_ENTRY( dim
, array
, expr_t
, entry
)
696 if (!dim
->is_const
) return 0;
703 static size_t type_memsize(const type_t
*t
, const array_dims_t
*array
, unsigned int *align
)
709 size
= sizeof(void *);
710 if (size
> *align
) *align
= size
;
712 else switch (t
->type
)
719 if (size
> *align
) *align
= size
;
726 if (size
> *align
) *align
= size
;
730 case RPC_FC_ERROR_STATUS_T
:
734 if (size
> *align
) *align
= size
;
739 if (size
> *align
) *align
= size
;
742 case RPC_FC_CVSTRUCT
:
743 case RPC_FC_CPSTRUCT
:
746 case RPC_FC_BOGUS_STRUCT
:
747 size
= fields_memsize(t
->fields
, align
);
749 case RPC_FC_ENCAPSULATED_UNION
:
750 case RPC_FC_NON_ENCAPSULATED_UNION
:
751 size
= union_memsize(t
->fields
, align
);
754 error("type_memsize: Unknown type %d\n", t
->type
);
758 if (array
) size
*= get_array_size( array
);
762 static unsigned int write_nonsimple_pointer(FILE *file
, const type_t
*type
, size_t offset
)
764 short absoff
= type
->ref
->typestring_offset
;
765 short reloff
= absoff
- (offset
+ 2);
766 int ptr_attr
= is_ptr(type
->ref
) ? 0x10 : 0x0;
768 print_file(file
, 2, "0x%02x, 0x%x,\t/* %s */\n",
769 type
->type
, ptr_attr
, string_of_type(type
->type
));
770 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
771 reloff
, reloff
, absoff
);
775 static unsigned int write_simple_pointer(FILE *file
, const type_t
*type
)
777 print_file(file
, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
778 type
->type
, string_of_type(type
->type
));
779 print_file(file
, 2, "0x%02x,\t/* %s */\n", type
->ref
->type
,
780 string_of_type(type
->ref
->type
));
781 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
785 static size_t write_pointer_tfs(FILE *file
, type_t
*type
, unsigned int *typestring_offset
)
787 unsigned int offset
= *typestring_offset
;
789 print_file(file
, 0, "/* %d */\n", offset
);
790 type
->typestring_offset
= offset
;
792 if (type
->ref
->typestring_offset
)
793 *typestring_offset
+= write_nonsimple_pointer(file
, type
, offset
);
794 else if (is_base_type(type
->ref
->type
))
795 *typestring_offset
+= write_simple_pointer(file
, type
);
800 static int has_known_tfs(const type_t
*type
)
802 return type
->typestring_offset
|| is_base_type(type
->type
);
805 static int write_pointers(FILE *file
, const attr_list_t
*attrs
,
806 type_t
*type
, const char *name
,
807 const array_dims_t
*array
, int level
,
808 unsigned int *typestring_offset
)
812 /* don't generate a pointer for first-level arrays since we want to
813 * descend into them to write their pointers, not stop here */
814 if ((level
== 0 || !is_ptr(type
)) && is_array_type(attrs
, type
, array
))
816 return write_pointers(file
, NULL
, type
, name
, NULL
, level
+ 1, typestring_offset
);
818 else if (is_ptr(type
))
820 type_t
*ref
= type
->ref
;
822 if (!has_known_tfs(ref
))
826 write_pointers(file
, attrs
, ref
, name
, array
, level
+ 1,
829 else if (is_struct(ref
->type
))
831 write_struct_tfs(file
, ref
, name
, typestring_offset
);
835 error("write_pointers: type format string unknown for %s (0x%02x)\n",
840 /* top-level pointers are handled by write_pointer_description */
842 write_pointer_tfs(file
, type
, typestring_offset
);
846 else if (is_struct(type
->type
))
848 int pointers_written
= 0;
851 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
852 pointers_written
+= write_pointers(file
, v
->attrs
, v
->type
,
857 return pointers_written
;
862 static size_t write_pointer_description(FILE *file
, const attr_list_t
*attrs
,
863 type_t
*type
, size_t mem_offset
,
864 const array_dims_t
*array
, int level
,
865 unsigned int *typestring_offset
)
868 unsigned int align
= 0;
870 /* don't generate a pointer for first-level arrays since we want to
871 * descend into them to write their pointers, not stop here */
872 if ((level
== 0 || !is_ptr(type
)) && is_array_type(attrs
, type
, array
))
874 write_pointer_description(file
, NULL
, type
, mem_offset
, NULL
,
875 level
+ 1, typestring_offset
);
877 else if (is_ptr(type
))
879 print_file(file
, 2, "0x46,\t/* FC_NO_REPEAT */\n");
880 print_file(file
, 2, "0x5c,\t/* FC_PAD */\n");
881 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset
, mem_offset
);
882 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", mem_offset
, mem_offset
);
883 *typestring_offset
+= 6;
885 if (has_known_tfs(type
->ref
))
886 write_pointer_tfs(file
, type
, typestring_offset
);
888 error("write_pointer_description: type format string unknown\n");
890 else if (level
== 0 && is_struct(type
->type
))
894 LIST_FOR_EACH_ENTRY( v
, type
->fields
, const var_t
, entry
)
896 += write_pointer_description(file
, v
->attrs
, v
->type
,
897 mem_offset
, v
->array
,
903 return type_memsize(type
, array
, &align
);
906 static size_t write_string_tfs(FILE *file
, const attr_list_t
*attrs
,
907 const type_t
*type
, const array_dims_t
*array
,
908 const char *name
, unsigned int *typestring_offset
)
910 const expr_list_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
911 int has_size
= is_non_void(size_is
);
912 size_t start_offset
= *typestring_offset
;
913 unsigned char flags
= 0;
919 pointer_type
= type
->type
;
923 pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
926 pointer_type
= RPC_FC_RP
;
928 if (!get_attrp(attrs
, ATTR_SIZEIS
))
929 flags
|= RPC_FC_P_SIMPLEPOINTER
;
933 if ((rtype
!= RPC_FC_BYTE
) && (rtype
!= RPC_FC_CHAR
) && (rtype
!= RPC_FC_WCHAR
))
935 error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype
, name
);
939 print_file(file
, 2,"0x%x, 0x%x, /* %s%s */\n",
941 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"),
942 (flags
& RPC_FC_P_SIMPLEPOINTER
) ? " [simple_pointer]" : "");
943 *typestring_offset
+= 2;
945 if (!(flags
& RPC_FC_P_SIMPLEPOINTER
))
947 print_file(file
, 2, "NdrFcShort(0x2),\n");
948 *typestring_offset
+= 2;
951 if (array
&& !is_conformant_array(array
))
953 /* FIXME: multi-dimensional array */
954 const expr_t
*dim
= LIST_ENTRY( list_head( array
), expr_t
, entry
);
955 if (dim
->cval
> USHRT_MAX
)
956 error("array size for parameter %s exceeds %d bytes by %ld bytes\n",
957 name
, USHRT_MAX
, dim
->cval
- USHRT_MAX
);
959 if (rtype
== RPC_FC_CHAR
)
960 WRITE_FCTYPE(file
, FC_CSTRING
, *typestring_offset
);
962 WRITE_FCTYPE(file
, FC_WSTRING
, *typestring_offset
);
963 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
964 *typestring_offset
+= 2;
966 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", dim
->cval
, dim
->cval
);
967 *typestring_offset
+= 2;
973 if (rtype
== RPC_FC_CHAR
)
974 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
976 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
977 print_file(file
, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED
);
978 *typestring_offset
+= 2;
980 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
, NULL
, size_is
);
986 if (rtype
== RPC_FC_CHAR
)
987 WRITE_FCTYPE(file
, FC_C_CSTRING
, *typestring_offset
);
989 WRITE_FCTYPE(file
, FC_C_WSTRING
, *typestring_offset
);
990 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
991 *typestring_offset
+= 2;
997 static size_t write_array_tfs(FILE *file
, const attr_list_t
*attrs
,
998 type_t
*type
, const array_dims_t
*array
,
999 const char *name
, unsigned int *typestring_offset
)
1001 const expr_list_t
*length_is
= get_attrp(attrs
, ATTR_LENGTHIS
);
1002 const expr_list_t
*size_is
= get_attrp(attrs
, ATTR_SIZEIS
);
1003 int has_length
= is_non_void(length_is
);
1004 int has_size
= is_non_void(size_is
) || is_conformant_array(array
);
1005 size_t start_offset
;
1006 int pointer_type
= get_attrv(attrs
, ATTR_POINTERTYPE
);
1008 pointer_type
= RPC_FC_RP
;
1010 print_file(file
, 2, "0x%x, 0x00, /* %s */\n",
1012 pointer_type
== RPC_FC_FP
? "FC_FP" : (pointer_type
== RPC_FC_UP
? "FC_UP" : "FC_RP"));
1013 print_file(file
, 2, "NdrFcShort(0x2),\n");
1014 *typestring_offset
+= 4;
1016 if (array
&& list_count(array
) > 1) /* multi-dimensional array */
1018 error("write_array_tfs: Multi-dimensional arrays not implemented yet (param %s)\n", name
);
1023 const expr_t
*dim
= array
? LIST_ENTRY( list_head( array
), expr_t
, entry
) : NULL
;
1024 int has_pointer
= 0;
1026 if (write_pointers(file
, attrs
, type
, name
, array
, 0, typestring_offset
) > 0)
1029 start_offset
= *typestring_offset
;
1031 if (!has_length
&& !has_size
)
1034 unsigned int align
= 0;
1035 size_t size
= type_memsize(type
, array
, &align
);
1036 if (size
< USHRT_MAX
)
1038 WRITE_FCTYPE(file
, FC_SMFARRAY
, *typestring_offset
);
1040 print_file(file
, 2, "0x%02x,\n", align
- 1);
1042 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", size
, size
);
1043 *typestring_offset
+= 4;
1047 WRITE_FCTYPE(file
, FC_LGFARRAY
, *typestring_offset
);
1049 print_file(file
, 2, "0x%02x,\n", align
- 1);
1051 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", size
, size
);
1052 *typestring_offset
+= 6;
1057 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1058 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1059 *typestring_offset
+= 2;
1060 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1061 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1062 *typestring_offset
+= 1;
1065 if (!write_base_type( file
, type
, typestring_offset
))
1067 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1068 *typestring_offset
+= 1;
1070 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1071 *typestring_offset
+= 1;
1073 return start_offset
;
1075 else if (has_length
&& !has_size
)
1078 unsigned int align
= 0;
1079 size_t element_size
= type_memsize(type
, NULL
, &align
);
1080 size_t elements
= dim
->cval
;
1081 size_t total_size
= element_size
* elements
;
1083 if (total_size
< USHRT_MAX
)
1085 WRITE_FCTYPE(file
, FC_SMVARRAY
, *typestring_offset
);
1087 print_file(file
, 2, "0x%02x,\n", align
- 1);
1089 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", total_size
, total_size
);
1090 /* number of elements */
1091 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", elements
, elements
);
1092 *typestring_offset
+= 6;
1096 WRITE_FCTYPE(file
, FC_LGVARRAY
, *typestring_offset
);
1098 print_file(file
, 2, "0x%02x,\n", align
- 1);
1100 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", total_size
, total_size
);
1101 /* number of elements */
1102 print_file(file
, 2, "NdrFcLong(0x%x), /* %d */\n", elements
, elements
);
1103 *typestring_offset
+= 10;
1106 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
1107 *typestring_offset
+= 2;
1109 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1115 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1116 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1117 *typestring_offset
+= 2;
1118 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1119 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1120 *typestring_offset
+= 1;
1123 if (!write_base_type( file
, type
, typestring_offset
))
1125 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1126 *typestring_offset
+= 1;
1128 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1129 *typestring_offset
+= 1;
1131 return start_offset
;
1133 else if (!has_length
&& has_size
)
1135 /* conformant array */
1136 unsigned int align
= 0;
1137 size_t element_size
= type_memsize(type
, NULL
, &align
);
1139 WRITE_FCTYPE(file
, FC_CARRAY
, *typestring_offset
);
1141 print_file(file
, 2, "0x%02x,\n", align
- 1);
1143 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
1144 *typestring_offset
+= 4;
1146 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1148 size_is
? size_is
: array
);
1152 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1153 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1154 *typestring_offset
+= 2;
1155 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1156 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1157 *typestring_offset
+= 1;
1160 if (!write_base_type( file
, type
, typestring_offset
))
1162 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1163 *typestring_offset
+= 1;
1165 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1166 *typestring_offset
+= 1;
1168 return start_offset
;
1172 /* conformant varying array */
1173 unsigned int align
= 0;
1174 size_t element_size
= type_memsize(type
, NULL
, &align
);
1176 WRITE_FCTYPE(file
, FC_CVARRAY
, *typestring_offset
);
1178 print_file(file
, 2, "0x%02x,\n", align
- 1);
1180 print_file(file
, 2, "NdrFcShort(0x%x), /* %d */\n", element_size
, element_size
);
1181 *typestring_offset
+= 4;
1183 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1185 size_is
? size_is
: array
);
1186 *typestring_offset
+= write_conf_or_var_desc(file
, current_func
,
1192 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1193 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1194 *typestring_offset
+= 2;
1195 write_pointer_description(file
, attrs
, type
, 0, array
, 0, typestring_offset
);
1196 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1197 *typestring_offset
+= 1;
1200 if (!write_base_type( file
, type
, typestring_offset
))
1202 print_file(file
, 2, "0x0, /* FIXME: write out conversion data */\n");
1203 *typestring_offset
+= 1;
1205 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1206 *typestring_offset
+= 1;
1208 return start_offset
;
1213 static const var_t
*find_array_or_string_in_struct(const type_t
*type
)
1215 const var_t
*last_field
= LIST_ENTRY( list_tail(type
->fields
), const var_t
, entry
);
1217 if (is_array_type(last_field
->attrs
, last_field
->type
, last_field
->array
))
1220 assert((last_field
->type
->type
== RPC_FC_CSTRUCT
) ||
1221 (last_field
->type
->type
== RPC_FC_CPSTRUCT
) ||
1222 (last_field
->type
->type
== RPC_FC_CVSTRUCT
));
1224 return find_array_or_string_in_struct(last_field
->type
);
1227 static void write_struct_members(FILE *file
, const type_t
*type
, unsigned int *typestring_offset
)
1231 if (type
->fields
) LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1233 unsigned char rtype
= field
->type
->type
;
1236 write_array_tfs( file
, field
->attrs
, field
->type
, field
->array
,
1237 field
->name
, typestring_offset
);
1238 else if (is_ptr( field
->type
))
1240 /* pointers are handled in detail earlier, here just treat them like longs */
1241 print_file( file
, 2, "0x8,\t/* FC_LONG */\n" );
1242 *typestring_offset
+= 1;
1244 else if (is_embedded_complex(field
->type
))
1246 size_t absoff
= (field
->corrdesc
1248 : field
->type
->typestring_offset
);
1249 short reloff
= absoff
- (*typestring_offset
+ 2);
1251 print_file(file
, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
1252 /* FIXME: actually compute necessary padding */
1253 print_file(file
, 2, "0x0,\t/* FIXME: padding */\n");
1254 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1255 reloff
, reloff
, absoff
);
1256 *typestring_offset
+= 4;
1258 else if (!write_base_type( file
, field
->type
, typestring_offset
))
1259 error("Unsupported member type 0x%x\n", rtype
);
1262 if (!(*typestring_offset
% 2))
1264 print_file(file
, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD
);
1265 *typestring_offset
+= 1;
1268 print_file(file
, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END
);
1269 *typestring_offset
+= 1;
1272 static size_t write_struct_tfs(FILE *file
, type_t
*type
,
1273 const char *name
, unsigned int *typestring_offset
)
1275 unsigned int total_size
;
1277 size_t start_offset
;
1278 size_t array_offset
;
1280 unsigned int align
= 0;
1285 case RPC_FC_PSTRUCT
:
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 if (type
->type
== RPC_FC_PSTRUCT
)
1293 write_pointers(file
, NULL
, type
, name
, NULL
, 0, typestring_offset
);
1295 start_offset
= *typestring_offset
;
1296 type
->typestring_offset
= start_offset
;
1297 if (type
->type
== RPC_FC_STRUCT
)
1298 WRITE_FCTYPE(file
, FC_STRUCT
, *typestring_offset
);
1300 WRITE_FCTYPE(file
, FC_PSTRUCT
, *typestring_offset
);
1302 print_file(file
, 2, "0x%02x,\n", align
- 1);
1304 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1305 *typestring_offset
+= 4;
1307 if (type
->type
== RPC_FC_PSTRUCT
)
1309 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1310 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1311 *typestring_offset
+= 2;
1312 write_pointer_description(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1313 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1314 *typestring_offset
+= 1;
1318 write_struct_members(file
, type
, typestring_offset
);
1319 return start_offset
;
1320 case RPC_FC_CSTRUCT
:
1321 case RPC_FC_CPSTRUCT
:
1322 total_size
= type_memsize(type
, NULL
, &align
);
1324 if (total_size
> USHRT_MAX
)
1325 error("structure size for %s exceeds %d bytes by %d bytes\n",
1326 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1328 array
= find_array_or_string_in_struct(type
);
1329 current_structure
= type
;
1330 array_offset
= write_array_tfs(file
, array
->attrs
, array
->type
,
1331 array
->array
, array
->name
,
1333 current_structure
= NULL
;
1335 if (type
->type
== RPC_FC_CPSTRUCT
)
1336 write_pointers(file
, NULL
, type
, name
, NULL
, 0, typestring_offset
);
1338 start_offset
= *typestring_offset
;
1339 type
->typestring_offset
= start_offset
;
1340 if (type
->type
== RPC_FC_CSTRUCT
)
1341 WRITE_FCTYPE(file
, FC_CSTRUCT
, *typestring_offset
);
1343 WRITE_FCTYPE(file
, FC_CPSTRUCT
, *typestring_offset
);
1345 print_file(file
, 2, "0x%02x,\n", align
- 1);
1347 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1348 *typestring_offset
+= 4;
1349 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1350 array_offset
- *typestring_offset
,
1351 array_offset
- *typestring_offset
,
1353 *typestring_offset
+= 2;
1355 if (type
->type
== RPC_FC_CPSTRUCT
)
1357 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1358 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1359 *typestring_offset
+= 2;
1360 write_pointer_description(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1361 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1362 *typestring_offset
+= 1;
1365 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1366 *typestring_offset
+= 1;
1368 return start_offset
;
1369 case RPC_FC_CVSTRUCT
:
1370 total_size
= type_memsize(type
, NULL
, &align
);
1372 if (total_size
> USHRT_MAX
)
1373 error("structure size for %s exceeds %d bytes by %d bytes\n",
1374 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1376 array
= find_array_or_string_in_struct(type
);
1377 current_structure
= type
;
1378 if (is_attr(array
->attrs
, ATTR_STRING
))
1379 array_offset
= write_string_tfs(file
, array
->attrs
, array
->type
,
1380 array
->array
, array
->name
,
1383 array_offset
= write_array_tfs(file
, array
->attrs
, array
->type
,
1384 array
->array
, array
->name
,
1386 current_structure
= NULL
;
1388 has_pointers
= write_pointers(file
, NULL
, type
, name
, NULL
, 0, typestring_offset
);
1390 start_offset
= *typestring_offset
;
1391 type
->typestring_offset
= start_offset
;
1392 WRITE_FCTYPE(file
, FC_CVSTRUCT
, *typestring_offset
);
1394 print_file(file
, 2, "0x%02x,\n", align
- 1);
1396 print_file(file
, 2, "NdrFcShort(0x%x), /* %u */\n", total_size
, total_size
);
1397 *typestring_offset
+= 4;
1398 print_file(file
, 2, "NdrFcShort(0x%x), /* offset = %d (%u) */\n",
1399 array_offset
- *typestring_offset
,
1400 array_offset
- *typestring_offset
,
1402 *typestring_offset
+= 2;
1406 print_file(file
, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP
);
1407 print_file(file
, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD
);
1408 *typestring_offset
+= 2;
1409 write_pointer_description(file
, NULL
, type
, 0, NULL
, 0, typestring_offset
);
1410 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1411 *typestring_offset
+= 1;
1414 print_file(file
, 2, "0x%x, /* FC_END */\n", RPC_FC_END
);
1415 *typestring_offset
+= 1;
1417 return start_offset
;
1419 case RPC_FC_BOGUS_STRUCT
:
1420 total_size
= type_memsize(type
, NULL
, &align
);
1421 if (total_size
> USHRT_MAX
)
1422 error("structure size for %s exceeds %d bytes by %d bytes\n",
1423 name
, USHRT_MAX
, total_size
- USHRT_MAX
);
1425 write_embedded_types(file
, type
, typestring_offset
);
1427 start_offset
= *typestring_offset
;
1428 print_file(file
, 0, "/* %d */\n", start_offset
);
1429 print_file(file
, 2, "0x%x,\t/* %s */\n", type
->type
, string_of_type(type
->type
));
1430 print_file(file
, 2, "0x%x,\t/* %d */\n", align
- 1, align
- 1);
1431 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size
, total_size
);
1433 /* do conformant array stuff */
1434 print_file(file
, 2, "NdrFcShort(0x0),\t/* FIXME: conformant array stuff */\n");
1436 /* do pointer stuff here */
1437 print_file(file
, 2, "NdrFcShort(0x0),\t/* FIXME: pointer stuff */\n");
1439 *typestring_offset
+= 8;
1440 write_struct_members(file
, type
, typestring_offset
);
1442 return start_offset
;
1445 error("write_struct_tfs: Unimplemented for type 0x%x\n", type
->type
);
1446 return *typestring_offset
;
1450 static size_t write_pointer_only_tfs(FILE *file
, const attr_list_t
*attrs
, int pointer_type
,
1451 unsigned char flags
, size_t offset
,
1452 unsigned int *typeformat_offset
)
1454 size_t start_offset
= *typeformat_offset
;
1455 short reloff
= offset
- (*typeformat_offset
+ 2);
1456 int in_attr
, out_attr
;
1457 in_attr
= is_attr(attrs
, ATTR_IN
);
1458 out_attr
= is_attr(attrs
, ATTR_OUT
);
1459 if (!in_attr
&& !out_attr
) in_attr
= 1;
1461 if (out_attr
&& !in_attr
&& pointer_type
== RPC_FC_RP
)
1464 print_file(file
, 2, "0x%x, 0x%x,\t\t/* %s",
1467 string_of_type(pointer_type
));
1471 fprintf(file
, " [allocated_on_stack]");
1473 fprintf(file
, " [pointer_deref]");
1474 fprintf(file
, " */\n");
1477 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff
, offset
);
1478 *typeformat_offset
+= 4;
1480 return start_offset
;
1483 static void write_branch_type(FILE *file
, const type_t
*t
, size_t *tfsoff
)
1485 if (is_base_type(t
->type
))
1487 print_file(file
, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
1488 t
->type
, string_of_type(t
->type
));
1490 else if (t
->typestring_offset
)
1492 short reloff
= t
->typestring_offset
- (*tfsoff
+ 2);
1493 print_file(file
, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
1494 reloff
, reloff
, t
->typestring_offset
);
1497 error("write_branch_type: type unimplemented (0x%x)\n", t
->type
);
1502 static size_t write_union_tfs(FILE *file
, type_t
*type
, size_t *tfsoff
)
1505 size_t start_offset
;
1506 size_t size
= type_memsize(type
, NULL
, &align
);
1507 var_list_t
*fields
= type
->fields
;
1509 type_t
*deftype
= NULL
;
1510 short nodeftype
= 0xffff;
1513 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1515 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1517 nbranch
+= list_count(cases
);
1520 start_offset
= *tfsoff
;
1521 type
->typestring_offset
= start_offset
;
1522 print_file(file
, 0, "/* %d */\n", start_offset
);
1523 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", size
, size
);
1524 print_file(file
, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch
, nbranch
);
1527 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1529 type_t
*ft
= f
->type
;
1530 expr_list_t
*cases
= get_attrp(f
->attrs
, ATTR_CASE
);
1531 int deflt
= is_attr(f
->attrs
, ATTR_DEFAULT
);
1534 if (cases
== NULL
&& !deflt
)
1535 error("union field %s with neither case nor default attribute\n", f
->name
);
1537 if (cases
) LIST_FOR_EACH_ENTRY(c
, cases
, expr_t
, entry
)
1539 /* MIDL doesn't check for duplicate cases, even though that seems
1540 like a reasonable thing to do, it just dumps them to the TFS
1541 like we're going to do here. */
1542 print_file(file
, 2, "NdrFcLong(0x%x),\t/* %d */\n", c
->cval
, c
->cval
);
1544 write_branch_type(file
, ft
, tfsoff
);
1547 /* MIDL allows multiple default branches, even though that seems
1548 illogical, it just chooses the last one, which is what we will
1559 write_branch_type(file
, deftype
, tfsoff
);
1563 print_file(file
, 2, "NdrFcShort(0x%x),\n", nodeftype
);
1567 return start_offset
;
1570 static size_t write_ip_tfs(FILE *file
, const func_t
*func
, const type_t
*type
, const var_t
*var
,
1571 unsigned int *typeformat_offset
)
1574 size_t start_offset
= *typeformat_offset
;
1575 const var_t
*iid
= get_attrp(var
->attrs
, ATTR_IIDIS
);
1580 expr_list_t expr_list
;
1582 expr
.type
= EXPR_IDENTIFIER
;
1584 expr
.u
.sval
= iid
->name
;
1585 expr
.is_const
= FALSE
;
1586 list_init( &expr_list
);
1587 list_add_head( &expr_list
, &expr
.entry
);
1588 print_file(file
, 2, "0x2f, /* FC_IP */\n");
1589 print_file(file
, 2, "0x5c, /* FC_PAD */\n");
1590 *typeformat_offset
+= write_conf_or_var_desc(file
, func
, NULL
, &expr_list
) + 2;
1594 const type_t
*base
= is_ptr(type
) ? type
->ref
: type
;
1595 const UUID
*uuid
= get_attrp(base
->attrs
, ATTR_UUID
);
1598 error("%s: interface %s missing UUID\n", __FUNCTION__
, base
->name
);
1600 print_file(file
, 2, "0x2f,\t/* FC_IP */\n");
1601 print_file(file
, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
1602 print_file(file
, 2, "NdrFcLong(0x%08lx),\n", uuid
->Data1
);
1603 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data2
);
1604 print_file(file
, 2, "NdrFcShort(0x%04x),\n", uuid
->Data3
);
1605 for (i
= 0; i
< 8; ++i
)
1606 print_file(file
, 2, "0x%02x,\n", uuid
->Data4
[i
]);
1609 fprintf(file
, "\n");
1611 *typeformat_offset
+= 18;
1613 return start_offset
;
1616 static int get_ptr_attr(const type_t
*t
, int def_type
)
1620 int ptr_attr
= get_attrv(t
->attrs
, ATTR_POINTERTYPE
);
1623 if (t
->kind
!= TKIND_ALIAS
)
1629 static size_t write_typeformatstring_var(FILE *file
, int indent
, const func_t
*func
,
1630 type_t
*type
, const var_t
*var
,
1631 unsigned int *typeformat_offset
)
1636 if (type
== var
->type
) /* top-level pointers */
1638 int pointer_attr
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
1639 if (pointer_attr
!= 0 && !is_ptr(type
))
1640 error("'%s': pointer attribute applied to non-pointer type\n", var
->name
);
1642 if (pointer_attr
== 0)
1643 pointer_attr
= get_ptr_attr(type
, RPC_FC_RP
);
1645 pointer_type
= pointer_attr
;
1648 pointer_type
= get_ptr_attr(type
, RPC_FC_UP
);
1650 if (((last_ptr(type
) && var
->array
== NULL
)
1651 || (!is_ptr(type
) && var
->array
!= NULL
))
1652 && is_ptrchain_attr(var
, ATTR_STRING
))
1654 return write_string_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
1657 if (is_array_type(var
->attrs
, type
, var
->array
))
1658 return write_array_tfs(file
, var
->attrs
, type
, var
->array
, var
->name
, typeformat_offset
);
1662 /* basic types don't need a type format string */
1663 if (is_base_type(type
->type
))
1669 case RPC_FC_PSTRUCT
:
1670 case RPC_FC_CSTRUCT
:
1671 case RPC_FC_CPSTRUCT
:
1672 case RPC_FC_CVSTRUCT
:
1673 case RPC_FC_BOGUS_STRUCT
:
1674 return write_struct_tfs(file
, type
, var
->name
, typeformat_offset
);
1675 case RPC_FC_ENCAPSULATED_UNION
:
1676 case RPC_FC_NON_ENCAPSULATED_UNION
:
1677 return write_union_tfs(file
, type
, typeformat_offset
);
1679 case RPC_FC_BIND_PRIMITIVE
:
1683 error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type
->type
, var
->name
);
1686 else if (last_ptr(type
))
1688 size_t start_offset
= *typeformat_offset
;
1689 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1690 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1691 const type_t
*base
= type
->ref
;
1693 if (base
->type
== RPC_FC_IP
)
1695 return write_ip_tfs(file
, func
, type
, var
, typeformat_offset
);
1698 /* special case for pointers to base types */
1699 if (is_base_type(base
->type
))
1701 print_file(file
, indent
, "0x%x, 0x%x, /* %s %s[simple_pointer] */\n",
1702 pointer_type
, (!in_attr
&& out_attr
) ? 0x0C : 0x08,
1703 string_of_type(pointer_type
),
1704 (!in_attr
&& out_attr
) ? "[allocated_on_stack] " : "");
1705 print_file(file
, indent
, "0x%02x, /* %s */\n", base
->type
, string_of_type(base
->type
));
1706 print_file(file
, indent
, "0x5c, /* FC_PAD */\n");
1707 *typeformat_offset
+= 4;
1708 return start_offset
;
1712 assert(is_ptr(type
));
1714 offset
= write_typeformatstring_var(file
, indent
, func
, type
->ref
, var
, typeformat_offset
);
1716 fprintf(file
, "/* %2u */\n", *typeformat_offset
);
1717 return write_pointer_only_tfs(file
, var
->attrs
, pointer_type
,
1718 !last_ptr(type
) ? 0x10 : 0,
1719 offset
, typeformat_offset
);
1722 static void clear_tfsoff(type_t
*type
)
1726 type
->typestring_offset
= 0;
1728 if (type
->kind
== TKIND_ALIAS
)
1730 else if (is_ptr(type
))
1737 LIST_FOR_EACH_ENTRY( v
, type
->fields
, var_t
, entry
)
1738 clear_tfsoff(v
->type
);
1746 static void write_embedded_types(FILE *file
, const type_t
*type
, size_t *tfsoff
)
1748 var_list_t
*fields
= type
->fields
;
1752 if (fields
) LIST_FOR_EACH_ENTRY(f
, fields
, var_t
, entry
)
1754 unsigned int align
= 0;
1755 type_t
*ft
= f
->type
;
1758 if (ft
->type
== RPC_FC_NON_ENCAPSULATED_UNION
)
1760 expr_t
*swexp
= get_attrp(f
->attrs
, ATTR_SWITCHIS
);
1763 unsigned char corrdesc
, op
= 0;
1764 short creloff
, ureloff
;
1767 error("union %s needs a switch_is attribute\n", f
->name
);
1768 if (swexp
->type
!= EXPR_IDENTIFIER
)
1769 error("%s: only identifiers are supported for switch_is at this time\n",
1772 if (ft
->typestring_offset
== 0)
1773 write_union_tfs(file
, ft
, tfsoff
);
1775 swname
= swexp
->u
.sval
;
1776 corroff
= field_offset(type
, swname
, &swvar
);
1777 corrdesc
= swvar
->type
->type
;
1778 creloff
= corroff
- offset
;
1780 f
->corrdesc
= *tfsoff
;
1781 ureloff
= ft
->typestring_offset
- (f
->corrdesc
+ 6);
1782 print_file(file
, 0, "/* %d */\n", f
->corrdesc
);
1783 print_file(file
, 2, "0x%x,\t/* %s */\n", ft
->type
, string_of_type(ft
->type
));
1784 print_file(file
, 2, "0x8,\t/* FIXME: support other switch types */\n");
1785 print_file(file
, 2, "0x%x,\t/* Corr desc: %s */\n",
1786 corrdesc
, string_of_type(corrdesc
& 0xf));
1787 print_file(file
, 2, "0x%x,\n", op
);
1788 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* %hd */\n", creloff
, creloff
);
1789 print_file(file
, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
1790 ureloff
, ureloff
, ft
->typestring_offset
);
1793 else if (!is_base_type(ft
->type
))
1794 error("write_embedded_types: unknown type (0x%x)\n", ft
->type
);
1796 /* FIXME: this doesn't take alignment/padding into account */
1797 offset
+= type_memsize(ft
, NULL
, &align
);
1801 static void clear_all_tfsoffs(const ifref_list_t
*ifaces
)
1803 const ifref_t
* iface
;
1808 LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
1809 if (iface
->iface
->funcs
)
1810 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
1812 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1813 clear_tfsoff(var
->type
);
1816 static size_t process_tfs(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
1819 const ifref_t
*iface
;
1820 unsigned int typeformat_offset
= 2;
1822 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
1824 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
1827 if (iface
->iface
->funcs
)
1830 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
1832 if (is_local(func
->def
->attrs
)) continue;
1834 current_func
= func
;
1836 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
1837 var
->type
->typestring_offset
1838 = write_typeformatstring_var(file
, 2, func
, var
->type
,
1839 var
, &typeformat_offset
);
1844 return typeformat_offset
+ 1;
1848 void write_typeformatstring(FILE *file
, const ifref_list_t
*ifaces
, int for_objects
)
1852 print_file(file
, indent
, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
1853 print_file(file
, indent
, "{\n");
1855 print_file(file
, indent
, "0,\n");
1856 print_file(file
, indent
, "{\n");
1858 print_file(file
, indent
, "NdrFcShort(0x0),\n");
1860 clear_all_tfsoffs(ifaces
);
1861 process_tfs(file
, ifaces
, for_objects
);
1863 print_file(file
, indent
, "0x0\n");
1865 print_file(file
, indent
, "}\n");
1867 print_file(file
, indent
, "};\n");
1868 print_file(file
, indent
, "\n");
1871 static unsigned int get_required_buffer_size_type(
1872 const type_t
*type
, const array_dims_t
*array
,
1873 const char *name
, unsigned int *alignment
)
1900 case RPC_FC_ERROR_STATUS_T
:
1912 case RPC_FC_BIND_PRIMITIVE
:
1916 case RPC_FC_PSTRUCT
:
1919 if (!type
->fields
) return 0;
1920 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1922 unsigned int alignment
;
1923 size
+= get_required_buffer_size_type(
1924 field
->type
, field
->array
, field
->name
,
1931 if (is_base_type( type
->ref
->type
) || type
->ref
->type
== RPC_FC_STRUCT
)
1932 size
= get_required_buffer_size_type( type
->ref
, NULL
, name
, alignment
);
1936 error("get_required_buffer_size: Unknown/unsupported type: %s (0x%02x)\n", name
, type
->type
);
1939 if (array
) size
*= get_array_size( array
);
1944 static unsigned int get_required_buffer_size(const var_t
*var
, unsigned int *alignment
, enum pass pass
)
1946 expr_list_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
1947 int has_size
= is_non_void(size_is
);
1948 int in_attr
= is_attr(var
->attrs
, ATTR_IN
);
1949 int out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
1951 if (!in_attr
&& !out_attr
)
1956 if (pass
== PASS_OUT
)
1958 if (out_attr
&& is_ptr(var
->type
))
1960 type_t
*type
= var
->type
;
1962 if (type
->type
== RPC_FC_STRUCT
)
1965 unsigned int size
= 36;
1967 if (!type
->fields
) return size
;
1968 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
1971 size
+= get_required_buffer_size_type(
1972 field
->type
, field
->array
, field
->name
,
1982 if ((!out_attr
|| in_attr
) && !has_size
&& !is_attr(var
->attrs
, ATTR_STRING
) && !var
->array
)
1984 if (is_ptr(var
->type
))
1986 type_t
*type
= var
->type
;
1988 if (is_base_type(type
->type
))
1992 else if (type
->type
== RPC_FC_STRUCT
)
1994 unsigned int size
= 36;
1997 if (!type
->fields
) return size
;
1998 LIST_FOR_EACH_ENTRY( field
, type
->fields
, const var_t
, entry
)
2001 size
+= get_required_buffer_size_type(
2002 field
->type
, field
->array
, field
->name
,
2010 return get_required_buffer_size_type(var
->type
, var
->array
, var
->name
, alignment
);
2014 static unsigned int get_function_buffer_size( const func_t
*func
, enum pass pass
)
2017 unsigned int total_size
= 0, alignment
;
2021 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2023 total_size
+= get_required_buffer_size(var
, &alignment
, pass
);
2024 total_size
+= alignment
;
2028 if (pass
== PASS_OUT
&& !is_void(func
->def
->type
))
2030 total_size
+= get_required_buffer_size(func
->def
, &alignment
, PASS_RETURN
);
2031 total_size
+= alignment
;
2036 static void print_phase_function(FILE *file
, int indent
, const char *type
,
2037 enum remoting_phase phase
,
2038 const char *varname
, unsigned int type_offset
)
2040 const char *function
;
2043 case PHASE_BUFFERSIZE
:
2044 function
= "BufferSize";
2047 function
= "Marshall";
2049 case PHASE_UNMARSHAL
:
2050 function
= "Unmarshall";
2060 print_file(file
, indent
, "Ndr%s%s(\n", type
, function
);
2062 print_file(file
, indent
, "&_StubMsg,\n");
2063 print_file(file
, indent
, "%s%s,\n",
2064 (phase
== PHASE_UNMARSHAL
) ? "(unsigned char **)&" : "(unsigned char *)",
2066 print_file(file
, indent
, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
2067 type_offset
, (phase
== PHASE_UNMARSHAL
) ? "," : ");");
2068 if (phase
== PHASE_UNMARSHAL
)
2069 print_file(file
, indent
, "0);\n");
2073 void print_phase_basetype(FILE *file
, int indent
, enum remoting_phase phase
,
2074 enum pass pass
, const var_t
*var
,
2075 const char *varname
)
2077 type_t
*type
= var
->type
;
2079 unsigned int alignment
= 0;
2080 unsigned char rtype
;
2082 /* no work to do for other phases, buffer sizing is done elsewhere */
2083 if (phase
!= PHASE_MARSHAL
&& phase
!= PHASE_UNMARSHAL
)
2086 rtype
= is_ptr(type
) ? type
->ref
->type
: type
->type
;
2108 case RPC_FC_ERROR_STATUS_T
:
2120 case RPC_FC_BIND_PRIMITIVE
:
2121 /* no marshalling needed */
2125 error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var
->name
, rtype
);
2129 print_file(file
, indent
, "_StubMsg.Buffer = (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
2130 alignment
- 1, alignment
- 1);
2132 if (phase
== PHASE_MARSHAL
)
2134 print_file(file
, indent
, "*(");
2135 write_type(file
, is_ptr(type
) ? type
->ref
: type
);
2137 fprintf(file
, " *)_StubMsg.Buffer = *");
2139 fprintf(file
, " *)_StubMsg.Buffer = ");
2140 fprintf(file
, varname
);
2141 fprintf(file
, ";\n");
2143 else if (phase
== PHASE_UNMARSHAL
)
2145 if (pass
== PASS_IN
|| pass
== PASS_RETURN
)
2146 print_file(file
, indent
, "");
2148 print_file(file
, indent
, "*");
2149 fprintf(file
, varname
);
2150 if (pass
== PASS_IN
&& is_ptr(type
))
2151 fprintf(file
, " = (");
2153 fprintf(file
, " = *(");
2154 write_type(file
, is_ptr(type
) ? type
->ref
: type
);
2155 fprintf(file
, " *)_StubMsg.Buffer;\n");
2158 print_file(file
, indent
, "_StubMsg.Buffer += sizeof(");
2159 write_type(file
, var
->type
);
2160 fprintf(file
, ");\n");
2163 /* returns whether the MaxCount, Offset or ActualCount members need to be
2164 * filled in for the specified phase */
2165 static inline int is_size_needed_for_phase(enum remoting_phase phase
)
2167 return (phase
!= PHASE_UNMARSHAL
);
2170 void write_remoting_arguments(FILE *file
, int indent
, const func_t
*func
,
2171 enum pass pass
, enum remoting_phase phase
)
2173 const expr_list_t
*length_is
;
2174 const expr_list_t
*size_is
;
2175 int in_attr
, out_attr
, has_length
, has_size
, pointer_type
;
2181 if (phase
== PHASE_BUFFERSIZE
)
2183 unsigned int size
= get_function_buffer_size( func
, pass
);
2184 print_file(file
, indent
, "_StubMsg.BufferLength = %u;\n", size
);
2187 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2189 const type_t
*type
= var
->type
;
2190 unsigned char rtype
;
2191 size_t start_offset
= type
->typestring_offset
;
2193 length_is
= get_attrp(var
->attrs
, ATTR_LENGTHIS
);
2194 size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2195 has_length
= is_non_void(length_is
);
2196 has_size
= is_non_void(size_is
) || (var
->array
&& is_conformant_array(var
->array
));
2198 pointer_type
= get_attrv(var
->attrs
, ATTR_POINTERTYPE
);
2200 pointer_type
= RPC_FC_RP
;
2202 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2203 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2204 if (!in_attr
&& !out_attr
)
2210 if (!in_attr
) continue;
2213 if (!out_attr
) continue;
2221 if (is_user_derived( var
))
2223 print_phase_function(file
, indent
, "UserMarshal", phase
, var
->name
, start_offset
);
2225 else if (is_string_type(var
->attrs
, var
->type
, var
->array
))
2227 if (var
->array
&& !is_conformant_array(var
->array
))
2228 print_phase_function(file
, indent
, "NonConformantString", phase
, var
->name
, start_offset
);
2231 if (size_is
&& is_size_needed_for_phase(phase
))
2233 const expr_t
*size
= LIST_ENTRY( list_head(size_is
), const expr_t
, entry
);
2234 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2235 write_expr(file
, size
, 1);
2236 fprintf(file
, ";\n");
2239 if ((phase
== PHASE_FREE
) || (pointer_type
== RPC_FC_UP
))
2240 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2242 print_phase_function(file
, indent
, "ConformantString", phase
, var
->name
,
2243 start_offset
+ (has_size
? 4 : 2));
2246 else if (is_array_type(var
->attrs
, var
->type
, var
->array
))
2248 const char *array_type
;
2250 if (var
->array
&& list_count(var
->array
) > 1) /* multi-dimensional array */
2251 array_type
= "ComplexArray";
2254 if (!has_length
&& !has_size
)
2255 array_type
= "FixedArray";
2256 else if (has_length
&& !has_size
)
2258 if (is_size_needed_for_phase(phase
))
2260 const expr_t
*length
= LIST_ENTRY( list_head(length_is
), const expr_t
, entry
);
2261 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2262 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2263 write_expr(file
, length
, 1);
2264 fprintf(file
, ";\n\n");
2266 array_type
= "VaryingArray";
2268 else if (!has_length
&& has_size
)
2270 if (is_size_needed_for_phase(phase
) && phase
!= PHASE_FREE
)
2272 const expr_t
*size
= LIST_ENTRY( list_head(size_is
? size_is
: var
->array
),
2273 const expr_t
, entry
);
2274 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2275 write_expr(file
, size
, 1);
2276 fprintf(file
, ";\n\n");
2278 array_type
= "ConformantArray";
2282 if (is_size_needed_for_phase(phase
))
2284 const expr_t
*length
= LIST_ENTRY( list_head(length_is
), const expr_t
, entry
);
2285 const expr_t
*size
= LIST_ENTRY( list_head(size_is
? size_is
: var
->array
),
2286 const expr_t
, entry
);
2287 print_file(file
, indent
, "_StubMsg.MaxCount = (unsigned long)");
2288 write_expr(file
, size
, 1);
2289 fprintf(file
, ";\n");
2290 print_file(file
, indent
, "_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2291 print_file(file
, indent
, "_StubMsg.ActualCount = (unsigned long)");
2292 write_expr(file
, length
, 1);
2293 fprintf(file
, ";\n\n");
2295 array_type
= "ConformantVaryingArray";
2299 if (!in_attr
&& phase
== PHASE_FREE
)
2301 print_file(file
, indent
, "if (%s)\n", var
->name
);
2303 print_file(file
, indent
, "_StubMsg.pfnFree(%s);\n", var
->name
);
2305 else if (phase
!= PHASE_FREE
)
2307 if (pointer_type
== RPC_FC_UP
)
2308 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2310 print_phase_function(file
, indent
, array_type
, phase
, var
->name
, start_offset
);
2313 else if (!is_ptr(var
->type
) && is_base_type(rtype
))
2315 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2317 else if (!is_ptr(var
->type
))
2322 case RPC_FC_PSTRUCT
:
2323 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, start_offset
);
2325 case RPC_FC_CSTRUCT
:
2326 case RPC_FC_CPSTRUCT
:
2327 print_phase_function(file
, indent
, "ConformantStruct", phase
, var
->name
, start_offset
);
2329 case RPC_FC_CVSTRUCT
:
2330 print_phase_function(file
, indent
, "ConformantVaryingStruct", phase
, var
->name
, start_offset
);
2332 case RPC_FC_BOGUS_STRUCT
:
2333 print_phase_function(file
, indent
, "ComplexStruct", phase
, var
->name
, start_offset
);
2336 if (is_base_type( var
->type
->ref
->type
))
2338 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2340 else if (var
->type
->ref
->type
== RPC_FC_STRUCT
)
2342 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2343 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, start_offset
+ 4);
2348 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2349 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2350 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2354 error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var
->name
, rtype
);
2359 if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && is_base_type(rtype
))
2361 print_phase_basetype(file
, indent
, phase
, pass
, var
, var
->name
);
2363 else if (last_ptr(var
->type
) && (pointer_type
== RPC_FC_RP
) && (rtype
== RPC_FC_STRUCT
))
2365 if (phase
!= PHASE_BUFFERSIZE
&& phase
!= PHASE_FREE
)
2366 print_phase_function(file
, indent
, "SimpleStruct", phase
, var
->name
, start_offset
+ 4);
2371 if ((iid
= get_attrp( var
->attrs
, ATTR_IIDIS
)))
2372 print_file( file
, indent
, "_StubMsg.MaxCount = (unsigned long)%s;\n", iid
->name
);
2373 print_phase_function(file
, indent
, "Pointer", phase
, var
->name
, start_offset
);
2376 fprintf(file
, "\n");
2381 size_t get_size_procformatstring_var(const var_t
*var
)
2383 return write_procformatstring_var(NULL
, 0, var
, FALSE
);
2387 size_t get_size_procformatstring_func(const func_t
*func
)
2392 /* argument list size */
2394 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2395 size
+= get_size_procformatstring_var(var
);
2397 /* return value size */
2398 if (is_void(func
->def
->type
))
2399 size
+= 2; /* FC_END and FC_PAD */
2401 size
+= get_size_procformatstring_var(func
->def
);
2406 size_t get_size_procformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2408 const ifref_t
*iface
;
2412 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, const ifref_t
, entry
)
2414 if (for_objects
!= is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
2417 if (iface
->iface
->funcs
)
2418 LIST_FOR_EACH_ENTRY( func
, iface
->iface
->funcs
, const func_t
, entry
)
2419 if (!is_local(func
->def
->attrs
))
2420 size
+= get_size_procformatstring_func( func
);
2425 size_t get_size_typeformatstring(const ifref_list_t
*ifaces
, int for_objects
)
2427 return process_tfs(NULL
, ifaces
, for_objects
);
2430 static void write_struct_expr(FILE *h
, const expr_t
*e
, int brackets
,
2431 const var_list_t
*fields
, const char *structvar
)
2437 fprintf(h
, "%lu", e
->u
.lval
);
2440 fprintf(h
, "0x%lx", e
->u
.lval
);
2442 case EXPR_TRUEFALSE
:
2444 fprintf(h
, "FALSE");
2448 case EXPR_IDENTIFIER
:
2451 LIST_FOR_EACH_ENTRY( field
, fields
, const var_t
, entry
)
2452 if (!strcmp(e
->u
.sval
, field
->name
))
2454 fprintf(h
, "%s->%s", structvar
, e
->u
.sval
);
2458 if (&field
->entry
== fields
) error("no field found for identifier %s\n", e
->u
.sval
);
2463 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2467 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2471 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2475 write_type(h
, e
->u
.tref
);
2477 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2480 fprintf(h
, "sizeof(");
2481 write_type(h
, e
->u
.tref
);
2492 if (brackets
) fprintf(h
, "(");
2493 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2495 case EXPR_SHL
: fprintf(h
, " << "); break;
2496 case EXPR_SHR
: fprintf(h
, " >> "); break;
2497 case EXPR_MUL
: fprintf(h
, " * "); break;
2498 case EXPR_DIV
: fprintf(h
, " / "); break;
2499 case EXPR_ADD
: fprintf(h
, " + "); break;
2500 case EXPR_SUB
: fprintf(h
, " - "); break;
2501 case EXPR_AND
: fprintf(h
, " & "); break;
2502 case EXPR_OR
: fprintf(h
, " | "); break;
2505 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2506 if (brackets
) fprintf(h
, ")");
2509 if (brackets
) fprintf(h
, "(");
2510 write_struct_expr(h
, e
->ref
, 1, fields
, structvar
);
2512 write_struct_expr(h
, e
->u
.ext
, 1, fields
, structvar
);
2514 write_struct_expr(h
, e
->ext2
, 1, fields
, structvar
);
2515 if (brackets
) fprintf(h
, ")");
2521 void declare_stub_args( FILE *file
, int indent
, const func_t
*func
)
2523 int in_attr
, out_attr
;
2525 const var_t
*def
= func
->def
;
2528 /* declare return value '_RetVal' */
2529 if (!is_void(def
->type
))
2531 print_file(file
, indent
, "");
2532 write_type(file
, def
->type
);
2533 fprintf(file
, " _RetVal;\n");
2539 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2541 const expr_list_t
*size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2542 int has_size
= is_non_void(size_is
);
2543 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2545 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2546 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2547 if (!out_attr
&& !in_attr
)
2550 if (!in_attr
&& !has_size
&& !is_string
)
2552 print_file(file
, indent
, "");
2553 write_type(file
, var
->type
->ref
);
2554 fprintf(file
, " _W%u;\n", i
++);
2557 print_file(file
, indent
, "");
2558 write_type(file
, var
->type
);
2561 fprintf(file
, "( *");
2562 write_name(file
, var
);
2563 fprintf(file
, " )");
2565 write_name(file
, var
);
2566 write_array(file
, var
->array
, 0);
2567 fprintf(file
, ";\n");
2572 void assign_stub_out_args( FILE *file
, int indent
, const func_t
*func
)
2574 int in_attr
, out_attr
;
2577 const expr_list_t
*size_is
;
2583 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
2585 int is_string
= is_attr(var
->attrs
, ATTR_STRING
);
2586 size_is
= get_attrp(var
->attrs
, ATTR_SIZEIS
);
2587 has_size
= is_non_void(size_is
);
2588 in_attr
= is_attr(var
->attrs
, ATTR_IN
);
2589 out_attr
= is_attr(var
->attrs
, ATTR_OUT
);
2590 if (!out_attr
&& !in_attr
)
2595 print_file(file
, indent
, "");
2596 write_name(file
, var
);
2601 unsigned int size
, align
= 0;
2602 type_t
*type
= var
->type
;
2604 fprintf(file
, " = NdrAllocate(&_StubMsg, ");
2605 LIST_FOR_EACH_ENTRY( expr
, size_is
, const expr_t
, entry
)
2607 if (expr
->type
== EXPR_VOID
) continue;
2608 write_expr( file
, expr
, 1 );
2609 fprintf(file
, " * ");
2611 size
= type_memsize(type
, NULL
, &align
);
2612 fprintf(file
, "%u);\n", size
);
2614 else if (!is_string
)
2616 fprintf(file
, " = &_W%u;\n", i
);
2617 if (is_ptr(var
->type
) && !last_ptr(var
->type
))
2618 print_file(file
, indent
, "_W%u = 0;\n", i
);
2626 fprintf(file
, "\n");
2630 int write_expr_eval_routines(FILE *file
, const char *iface
)
2633 struct expr_eval_routine
*eval
;
2634 unsigned short callback_offset
= 0;
2636 LIST_FOR_EACH_ENTRY(eval
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2640 print_file(file
, indent
, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
2641 iface
, eval
->structure
->name
, callback_offset
);
2642 print_file(file
, indent
, "{\n");
2644 print_file(file
, indent
, "struct %s *" STRUCT_EXPR_EVAL_VAR
" = (struct %s *)(pStubMsg->StackTop - %u);\n",
2645 eval
->structure
->name
, eval
->structure
->name
, eval
->structure_size
);
2646 fprintf(file
, "\n");
2647 print_file(file
, indent
, "pStubMsg->Offset = 0;\n"); /* FIXME */
2648 print_file(file
, indent
, "pStubMsg->MaxCount = (unsigned long)");
2649 write_struct_expr(file
, eval
->expr
, 1, eval
->structure
->fields
, STRUCT_EXPR_EVAL_VAR
);
2650 fprintf(file
, ";\n");
2652 print_file(file
, indent
, "}\n\n");
2658 void write_expr_eval_routine_list(FILE *file
, const char *iface
)
2660 struct expr_eval_routine
*eval
;
2661 struct expr_eval_routine
*cursor
;
2662 unsigned short callback_offset
= 0;
2664 fprintf(file
, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
2665 fprintf(file
, "{\n");
2667 LIST_FOR_EACH_ENTRY_SAFE(eval
, cursor
, &expr_eval_routines
, struct expr_eval_routine
, entry
)
2669 print_file(file
, 1, "%s_%sExprEval_%04u,\n",
2670 iface
, eval
->structure
->name
, callback_offset
);
2673 list_remove(&eval
->entry
);
2677 fprintf(file
, "};\n\n");
2681 void write_endpoints( FILE *f
, const char *prefix
, const str_list_t
*list
)
2683 const struct str_list_entry_t
*endpoint
;
2686 /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
2687 print_file( f
, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix
);
2688 LIST_FOR_EACH_ENTRY( endpoint
, list
, const struct str_list_entry_t
, entry
)
2690 print_file( f
, 1, "{ (const unsigned char *)\"" );
2691 for (p
= endpoint
->str
; *p
&& *p
!= ':'; p
++)
2693 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
2696 if (!*p
) goto error
;
2697 if (p
[1] != '[') goto error
;
2699 fprintf( f
, "\", (const unsigned char *)\"" );
2700 for (p
+= 2; *p
&& *p
!= ']'; p
++)
2702 if (*p
== '"' || *p
== '\\') fputc( '\\', f
);
2705 if (*p
!= ']') goto error
;
2706 fprintf( f
, "\" },\n" );
2708 print_file( f
, 0, "};\n\n" );
2712 error("Invalid endpoint syntax '%s'\n", endpoint
->str
);